aboutsummaryrefslogtreecommitdiff
path: root/src/indicator-power.c
blob: 84d5da160e78a5d59fed722bf00a62abf87e2f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "indicator-power.h"

typedef struct  {
	char * label;
	char * icon;
	char * time;
} menuitem_t;

static void
block_prelight (GtkWidget * widget, GtkStateType prev, gpointer data)
{
	if (GTK_WIDGET_STATE(widget) == GTK_STATE_PRELIGHT) {
		gtk_widget_set_state(widget, prev);
	}
	return;
}


#define MENU_ITEM_CNT 3
menuitem_t menuitems[MENU_ITEM_CNT] = {
	{"Main", "panel-light-power-battery-medium", "(1:34 left)"},
	{"Mouse", "panel-light-power-mouse-verylow", "(0:40 left)"},
	{"Phone", "panel-light-power-battery-verylow", "(10:40 left)"}
};

GtkWidget *
power_menu (void)
{
	GtkMenu * menu = GTK_MENU(gtk_menu_new());

	GtkSizeGroup * icons  = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
	GtkSizeGroup * labels = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
	GtkSizeGroup * times  = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);

	int i;
	for (i = 0 ; i < MENU_ITEM_CNT; i++) {
		GtkWidget * item = gtk_menu_item_new ();
		g_signal_connect(G_OBJECT(item), "state-changed", G_CALLBACK(block_prelight), NULL);

		GtkWidget * hbox = gtk_hbox_new (FALSE, 3);

		GtkWidget * icon = gtk_image_new_from_icon_name(menuitems[i].icon, GTK_ICON_SIZE_MENU);
		gtk_size_group_add_widget(icons, icon);
		gtk_misc_set_alignment(GTK_MISC(icon), 0.0, 0.5);
		gtk_box_pack_start(GTK_BOX(hbox), icon, FALSE, FALSE, 0);
		gtk_widget_show(icon);

		GtkWidget * label = gtk_label_new(menuitems[i].label);
		gtk_size_group_add_widget(labels, label);
		gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
		gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 3);
		gtk_widget_show(label);

		GtkWidget * time = gtk_label_new(menuitems[i].time);
		gtk_size_group_add_widget(times, time);
		gtk_misc_set_alignment(GTK_MISC(time), 0.0, 0.5);
		gtk_box_pack_start(GTK_BOX(hbox), time, FALSE, FALSE, 8);
		gtk_widget_show(time);

		gtk_container_add(GTK_CONTAINER(item), hbox);
		gtk_widget_show(hbox);

		gtk_menu_append(menu, item);
		gtk_widget_show(item);
	}

	GtkWidget * sep = gtk_separator_menu_item_new();
	gtk_menu_append(menu, sep);
	gtk_widget_show(sep);

	GtkWidget * prefs = gtk_menu_item_new_with_label(_("Power Settings..."));
	gtk_menu_append(menu, prefs);
	gtk_widget_show(prefs);

	return GTK_WIDGET(menu);
}

GtkWidget *
indicator_power_menuitem (void)
{
	GtkWidget * mainmenu = gtk_menu_item_new();

	GtkWidget * hbox = gtk_hbox_new(FALSE, 0);

	GtkWidget * icon = gtk_image_new_from_icon_name("panel-light-power-battery-plugged",
	                                                GTK_ICON_SIZE_MENU);
	gtk_box_pack_start(GTK_BOX(hbox), icon, FALSE, FALSE, 0);
	gtk_widget_show(icon);

	GtkWidget * label = gtk_label_new("2:30");
	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
	gtk_widget_show(label);

	gtk_container_add(GTK_CONTAINER(mainmenu), hbox);
	gtk_widget_show(hbox);

	GtkWidget * submenu = power_menu();
	if (submenu != NULL) {
		gtk_menu_item_set_submenu(GTK_MENU_ITEM(mainmenu), submenu);
		gtk_widget_show(submenu);
	}

	return mainmenu;
}