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 = "" + "" + ""; 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); } }