[Gtk-sharp-list] CDateTimeCtrl in GTK#?

Jon García de Salazar Bilbao j.garcia at ziv.es
Mon Jun 26 07:05:29 EDT 2006


Thanks everyone.
I re-wrote Peter's code in Csharp. I also modified it to look like Visual C++'s CDateTimeCtrl.
A lot of improvements need to be made, but I suppose it can be a good starting point.

Besides that, what about the time? The date can be easily modified now, but we should be able
to modify the time of day too... is there any pre-built control for that?

Thanks, Jon.

class DateTimeWidget : VBox
    {
        HBox root_box;
        EventBox real_box;
        ToggleButton button;
        Gtk.Window popdown_window;
        DateTime datetime; //= System.DateTime.Now
        Calendar cal;
        Entry dt_entry;

        public delegate void DateChangedHandler(DateTime date);

        public event DateChangedHandler DateChanged;

        public DateTime Date
        {
            get { return datetime; }
            set { datetime = value; }
        }

        public DateTimeWidget(DateTime date)
        {
            datetime = date;

            root_box = new HBox();
            HBox hbox = new HBox(false, 0);
            real_box = new EventBox();
            button = new ToggleButton();
            button.Toggled += on_toggled;
            Arrow arrow = new Arrow(ArrowType.Down, ShadowType.None);
            dt_entry = new Entry(datetime.ToString());
            hbox.PackStart(dt_entry, false, false, 0);
            hbox.PackStart(button, false, false, 0);
            button.Add(arrow);
            real_box.Add(hbox);
            root_box.PackStart(real_box, true, true, 0);
            PackStart(root_box, false, false, 0);

            // Create our new window
            popdown_window = new Gtk.Window(Gtk.WindowType.Popup);

            // Create our calendar widget, and hook up to the interesting key/button events
            cal = new Calendar();
            cal.KeyPressEvent += new Gtk.KeyPressEventHandler(key_press);
            cal.ButtonPressEvent += new Gtk.ButtonPressEventHandler(btn_press);

            popdown_window.Add(cal);
        }

        private void on_toggled(object o, EventArgs e)
        {
            if (button.Active)
                popup();
            else
                popdown();
        }

        private void popup()
        {
            cal.Date = datetime;

            int x;
            int y;
            int width;
            int height;
            int depth;
            real_box.GdkWindow.GetGeometry(out x, out y, out width, out height, out depth);
            real_box.GdkWindow.GetOrigin(out x, out y);

            popdown_window.Move(x, y + height);
            popdown_window.ShowAll();

            // Grab foo for stealing the keyboard/input focus
            Gdk.Pointer.Grab(popdown_window.GdkWindow, true,
                Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask | Gdk.EventMask.PointerMotionMask | Gdk.EventMask.EnterNotifyMask | Gdk.EventMask.LeaveNotifyMask,
                null, null, Gtk.Global.CurrentEventTime);
            Gdk.Keyboard.Grab(popdown_window.GdkWindow, true, Gtk.Global.CurrentEventTime);
            Gtk.Grab.Add(popdown_window);
        }

        private void key_press(object o, Gtk.KeyPressEventArgs args)
        {
            if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.Escape)
                popdown();
        }

        private void btn_press(object o, Gtk.ButtonPressEventArgs args)
        {
            if (args.Event.Type == EventType.TwoButtonPress)
                popdown();
        }

        private void popdown()
        {
            // Undo our grab foo
            Gtk.Grab.Remove(popdown_window);
            Gdk.Keyboard.Ungrab(Gtk.Global.CurrentEventTime);
            Gdk.Pointer.Ungrab(Gtk.Global.CurrentEventTime);

            button.Active = false;

            popdown_window.Hide();

            if (datetime == cal.Date)
                return;

            datetime = cal.Date;
            dt_entry.Text = datetime.ToString();

            if (DateChanged != null)
                DateChanged(datetime);

        }
   
    }



More information about the Gtk-sharp-list mailing list