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 *widget;
104 GtkWidget *toplevel;
105 GtkWidget *embedded;
106 gint x_toplevel, y_toplevel;
107 gint x_widget, y_widget;
108 gint x, y;
109 gint screen_width, screen_height;
110 gint width, height;
111
112 widget = clock->widget;
113 toplevel = gtk_widget_get_toplevel (widget);
114 gdk_window_get_position (widget->window, &x_widget, &y_widget);
115 gdk_window_get_position (toplevel->window, &x_toplevel, &y_toplevel);
116 screen_width = gdk_screen_width ();
117 screen_height = gdk_screen_height ();
118
119 clock->calendar = gtk_window_new (GTK_WINDOW_POPUP);
120 gtk_window_set_decorated (GTK_WINDOW (clock->calendar), FALSE);
121 gtk_window_stick (GTK_WINDOW (clock->calendar));
122 gtk_window_set_keep_below (GTK_WINDOW (clock->calendar), TRUE);
123
124 embedded = gtk_calendar_new ();
125 gtk_container_add (GTK_CONTAINER (clock->calendar), embedded);
126 gtk_widget_show (embedded);
127
128 gtk_window_get_size (GTK_WINDOW (clock->calendar), &width, &height);
129
130 x = x_toplevel + x_widget;
131 if (x + width > screen_width)
132x = screen_width - width;
133
134 y = y_toplevel + y_widget;
135 if (y > screen_height / 2)
136y -= height;
137 else
138y += widget->allocation.height;
139
140 gtk_widget_set_uposition (clock->calendar, x, y);
141 gtk_window_set_resizable (GTK_WINDOW (clock->calendar), FALSE);
142 }
143
144 if (GTK_WIDGET_VISIBLE (clock->calendar))
145 gtk_widget_hide (clock->calendar);
146 else
147 gtk_widget_show (clock->calendar);
148
149 return FALSE;
150}
151
152static void
153gp_clock_destroy (GpClock *clock)
154{
155 g_free (clock->time_format);
156 g_free (clock->date_format);
157 g_source_remove (clock->poll);
158
159 if (clock->calendar)
160 gtk_widget_destroy (clock->calendar);
161
162 g_free (clock);
163}

Archive Download this file

Branches

Tags