summaryrefslogtreecommitdiffstats
path: root/DClock.vala
blob: 5f85d62d4fd4964327466d2c06f0222946bdc0cf (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using GLib;
using Gtk;
using Pango;
using Panel;

public class DClock : Panel.Applet {
	private Gtk.ToggleButton button = null;
	private Gtk.Label label = null;
	private Panel.AppletOrient orient = (Panel.AppletOrient)(-1);
	private BonoboUI.Verb[] verbs = null;
	private time_t current_time;
	private uint timeout = 0;
	
	private int size;
	private int fixed_width = -1;
	private int fixed_height = -1;
	private Gtk.Allocation old_allocation;
  
	public DClock() {
	}
  
	static construct {
		Gtk.rc_parse_string("\n" +
							"   style \"dclock-applet-button-style\"\n" +
							"   {\n" +
							"      GtkWidget::focus-line-width=0\n" +
							"      GtkWidget::focus-padding=0\n" +
							"   }\n" +
							"\n" +
							"    widget \"*.dclock-applet-button\" style \"dclock-applet-button-style\"\n" +
							"\n");
	}
  
	private void create() {
		set_flags(Panel.AppletFlags.EXPAND_MINOR);
		
		button = new Gtk.ToggleButton();
		button.set_relief(Gtk.ReliefStyle.NONE);
		button.set_name("dclock-applet-button");
		GLib.Signal.connect(button, "button_press_event", (GLib.Callback)do_not_eat_button_press, null);
		GLib.Signal.connect(this, "change_orient", (GLib.Callback)applet_change_orient, this);
		GLib.Signal.connect(button, "size_allocate", (GLib.Callback)change_pixel_size, this);
		this.add(button);
		
		label = new Gtk.Label(null);
		GLib.Signal.connect(label, "size_request", (GLib.Callback)clock_size_request, this);
		GLib.Signal.connect_swapped(label, "style_set", (GLib.Callback)unfix_size, this);
		label.set_justify(Gtk.Justification.CENTER);
		clock_update_text_gravity(label);
		GLib.Signal.connect(label, "screen-changed", (GLib.Callback)clock_update_text_gravity, null);
		button.add(label);
		
		set_border_width(0);
		size = (int)get_size();
		
		set_background_widget(this);
		
		verbs = new BonoboUI.Verb[2];
		
		verbs[0].cname = "About";
		verbs[0].cb = on_about_clicked;
		verbs[0].user_data = this;
		
		verbs[1].cname = null;
		verbs[1].cb = null;
		verbs[1].user_data = null;
		
		
		string menuxml = "<popup name=\"button3\">" +
		"<menuitem name=\"About Item\" verb=\"About\" _label=\"About\" pixtype=\"stock\" pixname=\"gnome-stock-about\"/>" +
		"</popup>"; 
		setup_menu(menuxml, verbs, this);
		
		show_all();
		
		refresh_clock_timeout();
		applet_change_orient(this, get_orient(), this);
	}
  
	private void unfix_size() {
		fixed_width = -1;
		fixed_height = -1;
		button.queue_resize();
	}
  
	private static void clock_size_request (Gtk.Widget clock, Gtk.Requisition req, DClock data) {
		if(req.width > data.fixed_width)
			data.fixed_width = req.width;
		if (req.height > data.fixed_height)
			data.fixed_height = req.height;
		req.width = data.fixed_width;
		req.height = data.fixed_height;
	}

	private static void clock_update_text_gravity(Gtk.Label label) {
		Pango.Layout layout;
		Pango.Context context;
		
		layout = label.get_layout();
		context = layout.get_context();
		context.set_base_gravity(Pango.Gravity.AUTO);
	}
	private void update_timeformat() {
    
	}
  
	private void update_clock() {
		current_time = time_t();
		string date_str = DDate.with_time_t(current_time).to_string();
		string time_str = Time.local(current_time).format("%H:%M");
		
		label.set_text(date_str + ", " + time_str);
		
		update_orient();
		button.queue_resize();
	}
	
	/*private void refresh_clock() {
		unfix_size();
		update_clock();
		}*/

	private void refresh_clock_timeout() {
		unfix_size();

		update_timeformat();

		if (timeout != 0)
			GLib.Source.remove (timeout);

		update_clock();

		clock_set_timeout(current_time);
	}

	/*private void refresh_click_timeout_time_only() {
		if (timeout != 0)
			GLib.Source.remove (timeout);
		
		clock_timeout_callback(this);
		}*/
	
	private void clock_set_timeout(time_t now) {
		uint timeouttime;
		
		GLib.TimeVal tv = GLib.TimeVal();
		
		timeouttime = (uint)((1000000 - tv.tv_usec)/1000 + 1 + 1000*(59 - now % 60));
		
		timeout = GLib.Timeout.add (timeouttime, clock_timeout_callback);
	}
	
	private bool clock_timeout_callback() {
		time_t new_time = time_t();
		
		if(new_time/60 != current_time/60) {
			update_clock();
		}
		
		clock_set_timeout(new_time);
		
		return false;
	}
	
	private static void on_about_clicked(BonoboUI.Component component, void* user_data, string cname) {
    
	}
  
	private static bool do_not_eat_button_press(Gtk.Widget widget, Gdk.EventButton event) {
		if(event.button != 1)
			GLib.Signal.stop_emission_by_name(widget, "button_press_event");
  
		return false;
	}
  
	private static void change_pixel_size(Gtk.Widget widget, Gtk.Allocation allocation, DClock clock) {
		if(clock.old_allocation.width  == allocation.width && clock.old_allocation.height == allocation.height)
			return;
    
		clock.old_allocation.width  = allocation.width;
		clock.old_allocation.height = allocation.height;
    
		int new_size;
    
		if(clock.orient == Panel.AppletOrient.LEFT || clock.orient == Panel.AppletOrient.RIGHT)
			new_size = allocation.width;
		else
			new_size = allocation.height;
    
		clock.size = new_size;
    
		clock.unfix_size();
		clock.update_timeformat();
		clock.update_clock();
	}
  
	void update_orient() {
		/*string text;
		int min_width;
		double new_angle;
		double angle;*/
    
		//text = gtk_label_get_text (GTK_LABEL (cd->clockw));
		//min_width = calculate_minimum_width (cd->panel_button, text);

		/*if(orient == Panel.AppletOrient.LEFT && min_width > button.allocation.width)
		  new_angle = 270;
		  else if(orient == Panel.AppletOrient.RIGHT && min_width > button.allocation.width)
		  new_angle = 90;
		  else
		  new_angle = 0;*/
    
		/*angle = gtk_label_get_angle(GTK_LABEL (cd->clockw));
		  if (angle != new_angle) {
		  unfix_size (cd);
		  gtk_label_set_angle (GTK_LABEL (cd->clockw), new_angle);
		  gtk_label_set_angle (GTK_LABEL (cd->panel_temperature_label), new_angle);
		  }*/
	}
  
	private static void applet_change_orient(Panel.Applet applet, Panel.AppletOrient orient, DClock clock) {
		if(orient == clock.orient)
			return;

		clock.orient = orient;

		clock.unfix_size();
		clock.update_clock();
		//update_calendar_popup (cd);
	}
  
	private static bool factory(DClock applet, string iid, void *data) {
		applet.create();
		return true;
	}
  
	public static int main(string[] args) {
		Gnome.Program.init("DClock", "0", Gnome.libgnomeui_module, args);
		return Panel.Applet.factory_main("OAFIID:GNOME_DClock_Factory", typeof(DClock), (Panel.AppletFactoryCallback)factory);
	}
}