GTK+2 panel 

GTK+2 panel Git Source Tree

Root/clock.c

1#include "customwidgets.h"
2
3
4typedef struct
5{
6 GtkWidget *widget;
7
8 gchar *time_format;
9 gchar *date_format;
10
11 GtkWidget *time;
12 GtkWidget *date;
13 GtkWidget *calendar;
14
15 guint poll;
16} GpClock;
17
18
19static gbooleangp_clock_update(GpClock*clock);
20static gbooleangp_clock_button_release(GpClock*clock,
21 GdkEventButton*event);
22static voidgp_clock_destroy(GpClock*clock);
23
24
25GtkWidget *
26gp_clock_new (gchar *widget_name,
27 gchar *time_format,
28 gchar *date_format,
29 gint int1,
30 gint int2)
31{
32 GpClock *clock;
33 GtkWidget *vbox;
34 gchar *name;
35
36 clock = g_new0 (GpClock, 1);
37
38 clock->widget = gtk_event_box_new ();
39 gtk_widget_set_name (clock->widget, widget_name);
40 g_signal_connect_swapped (clock->widget, "button-release-event",
41 G_CALLBACK (gp_clock_button_release), clock);
42 g_signal_connect_swapped (clock->widget, "destroy",
43 G_CALLBACK (gp_clock_destroy), clock);
44
45 clock->time_format = g_strdup (time_format ? time_format : "%X");
46 clock->date_format = g_strdup (date_format ? date_format : "%x");
47
48 vbox = gtk_vbox_new (FALSE, 0);
49 gtk_container_add (GTK_CONTAINER (clock->widget), vbox);
50
51 clock->time = gtk_label_new (NULL);
52 name = g_strdup_printf ("%sTime", widget_name);
53 gtk_widget_set_name (clock->time, name);
54 g_free (name);
55 gtk_label_set_use_markup (GTK_LABEL (clock->time), FALSE);
56 gtk_label_set_use_underline (GTK_LABEL (clock->time), FALSE);
57 gtk_label_set_single_line_mode (GTK_LABEL (clock->time), TRUE);
58 gtk_box_pack_start (GTK_BOX (vbox), clock->time, FALSE, FALSE, 0);
59
60 clock->date = gtk_label_new (NULL);
61 name = g_strdup_printf ("%sDate", widget_name);
62 gtk_widget_set_name (clock->date, name);
63 g_free (name);
64 gtk_label_set_use_markup (GTK_LABEL (clock->date), FALSE);
65 gtk_label_set_use_underline (GTK_LABEL (clock->date), FALSE);
66 gtk_label_set_single_line_mode (GTK_LABEL (clock->date), TRUE);
67 gtk_box_pack_start (GTK_BOX (vbox), clock->date, FALSE, FALSE, 0);
68
69 clock->poll = g_timeout_add (60000, (GSourceFunc) gp_clock_update, clock);
70
71 gp_clock_update (clock);
72
73 return clock->widget;
74}
75
76
77static gboolean
78gp_clock_update (GpClock *clock)
79{
80 time_t now;
81 gchar buffer[256];
82
83 now = time (NULL);
84
85 strftime (buffer, 256, clock->time_format, localtime (&now));
86 gtk_label_set_text (GTK_LABEL (clock->time), buffer);
87
88 strftime (buffer, 256, clock->date_format, localtime (&now));
89 gtk_label_set_text (GTK_LABEL (clock->date), buffer);
90
91 return TRUE;
92}
93
94static gboolean
95gp_clock_button_release (GpClock*clock,
96 GdkEventButton *event)
97{
98 if (event->button != 1)
99 return FALSE;
100
101 if G_UNLIKELY (clock->calendar == NULL)
102 {
103 GtkWidget *embedded;
104 gint x, y;
105 gint width, screen_width;
106
107 x = clock->widget->allocation.x;
108 y = clock->widget->allocation.y + clock->widget->allocation.height;
109 screen_width = gdk_screen_width ();
110
111 clock->calendar = gtk_window_new (GTK_WINDOW_POPUP);
112 gtk_window_set_decorated (GTK_WINDOW (clock->calendar), FALSE);
113 gtk_window_stick (GTK_WINDOW (clock->calendar));
114 gtk_window_set_keep_below (GTK_WINDOW (clock->calendar), TRUE);
115
116 embedded = gtk_calendar_new ();
117 gtk_container_add (GTK_CONTAINER (clock->calendar), embedded);
118 gtk_widget_show (embedded);
119
120 gtk_window_get_size (GTK_WINDOW (clock->calendar), &width, NULL);
121 if (x + width > screen_width)
122x = screen_width - width;
123
124 gtk_widget_set_uposition (clock->calendar, x, y);
125 gtk_window_set_resizable (GTK_WINDOW (clock->calendar), FALSE);
126 }
127
128 if (GTK_WIDGET_VISIBLE (clock->calendar))
129 gtk_widget_hide (clock->calendar);
130 else
131 gtk_widget_show (clock->calendar);
132
133 return FALSE;
134}
135
136static void
137gp_clock_destroy (GpClock *clock)
138{
139 g_free (clock->time_format);
140 g_free (clock->date_format);
141 g_source_remove (clock->poll);
142
143 if (clock->calendar)
144 gtk_widget_destroy (clock->calendar);
145
146 g_free (clock);
147}

Archive Download this file

Branches

Tags