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

Peter Johanson latexer at gentoo.org
Fri Jun 23 13:47:27 EDT 2006


On Fri, Jun 23, 2006 at 01:02:00PM -0400, JimD wrote:
> 
> Are there any solutions for a drop down calendar with Gtk# similar to 
> the one in MS .Net?  Specifically where the drop down displays the 
> calendar above other controls without messing up/expanding the contents 
> of the whole window to just display the calendar.

The attached boo file shows you how you'd construct such a widget fairly
easily. Beware the particulars of needing a GdkWindow to get coordinates
from (thus the EventBox), as well as all the grabbing stuff for the
popup/popdown methods.

-pete

-------------- next part --------------
import System

import Gtk from 'gtk-sharp'
import Gdk from 'gdk-sharp'

class WindowToggleButton (VBox):
	root_box as HBox
	real_box as EventBox
	fake_box as HBox
	button as ToggleButton
	popdown_window as Gtk.Window
	datetime as System.DateTime = System.DateTime.Now
	cal as Calendar

	def constructor ():
		root_box = HBox ()
		real_box = EventBox ()
		button = ToggleButton (datetime.ToString ())
		button.Toggled += on_toggled
		real_box.Add (button)
		root_box.PackStart (real_box, true, true, 0)
		PackStart (root_box, false, false, 0)

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

		# Create our calendar widget, and hook up to the interesting key/button events
		cal = Calendar ()
		cal.KeyPressEvent += def (o, args as KeyPressEventArgs):
			if args.Event.Key in (Gdk.Key.Return, Gdk.Key.Escape):
				popdown ()
		cal.ButtonPressEvent += def (o, args as ButtonPressEventArgs):
			if args.Event.Type == EventType.TwoButtonPress:
				popdown ()

		popdown_window.Add (cal)
	
	private def on_toggled (o, e as EventArgs):
		if button.Active:
			popup ();
		else:
			popdown ();
	
	private def popup ():
		cal.Date = datetime

		x as int
		y as int
		real_box.GdkWindow.GetOrigin (x, y)
		popdown_window.Move (x, y)
		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 def 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
		datetime = cal.Date
		button.Label = datetime.ToString ()
		popdown_window.Hide ()
		
Application.Init ()

w = Gtk.Window ('test')
w.DeleteEvent += { Application.Quit (); }
w.Add (WindowToggleButton ())
w.ShowAll ()
Application.Run ()


More information about the Gtk-sharp-list mailing list