using Gtk; using System; public class ToggleButtonApp { int dialog_x = 0; int dialog_y = 0; ToggleButton btn; Menu contextMenu; TreeView categoryListView; TreeIter listIter; ListStore listStore; Gtk.Window dialogWindow; Gtk.Window parentWindow; public static int Main (string[] args) { new ToggleButtonApp(); return 0; } public ToggleButtonApp() { Application.Init (); parentWindow = new Window ("ToggleButton Tester"); parentWindow.SetDefaultSize (150, 25); parentWindow.DeleteEvent += new DeleteEventHandler (Window_Delete); //parentWindow btn = new ToggleButton ("Unselected"); btn.Active = false; btn.Toggled += new EventHandler (btn_toggled); parentWindow.Add (btn); contextMenu = new Menu (); CheckMenuItem menuItem = new CheckMenuItem ("Item 1"); contextMenu.Append (menuItem); CheckMenuItem menuItem1 = new CheckMenuItem ("Item 1"); contextMenu.Append (menuItem1); //contextMenu.AttachToWidget //(btn, new MenuDetachFunc (OnDetachContextMenu)); //contextMenu.ShowAll (); btn.ShowAll (); SetupPopUpWindow (); parentWindow.ShowAll (); Application.Run (); } void OnSelectionChanged(object o, EventArgs args) { Console.WriteLine("Hello!"); } void SetupPopUpWindow () { dialogWindow = new Window ("Categories"); dialogWindow.Decorated = false; categoryListView = new TreeView (); categoryListView.HeadersVisible = false; categoryListView.HeadersClickable = false; categoryListView.Selection.Changed += new EventHandler(OnSelectionChanged); categoryListView.AppendColumn("Categories", new Gtk.CellRendererText (), "text", 0); listStore = new ListStore (typeof(string)); categoryListView.Model = listStore; listIter = new TreeIter (); for(int i=0; i < 6; i++) { string temp = "Hello"; Console.WriteLine ("{0}:\t{1}", i.ToString (), temp); object [] columns = new object [1]; columns [0] = temp + i.ToString (); listIter = listStore.AppendValues (columns); } Frame frame = new Frame (); frame.ShadowType = ShadowType.In; Alignment align = new Alignment (0,0,0,0); uint padding = 1; align.SetPadding (padding, padding, padding, padding); Frame frame2 = new Frame (); frame2.ShadowType = ShadowType.In; frame2.Add (categoryListView); align.Add (frame2); frame.Add (align); dialogWindow.Add (frame); dialogWindow.Gravity = Gdk.Gravity.North; dialogWindow.GetPosition (out dialog_x, out dialog_y); dialogWindow.ShowAll (); dialogWindow.GdkWindow.TypeHint = Gdk.WindowTypeHint.Utility; dialogWindow.Hide (); } void OnDetachContextMenu (Gtk.Widget widget, Gtk.Menu menu) { if(btn.Active) { contextMenu.Show (); } else { contextMenu.Hide (); } } void WindowPosition(Window window) { int height =0; int width = 0; window.GetSize(out width , out height); Console.WriteLine("Width: {0}, Height: {1}" , width , height); } void PositionWidgetPopup(bool rightPosition) { int btn_x = 0; int btn_y = 0; btn.GdkWindow.GetOrigin (out btn_x, out btn_y); int btn_w = btn.Allocation.Width; int btn_h = btn.Allocation.Height; if(rightPosition) { btn_x = btn_x + btn_w; } else { btn_y = btn_y + btn_h; } dialogWindow.Move(btn_x, btn_y); } void btn_toggled (object obj, EventArgs args) { Console.WriteLine ("Button Toggled"); if (btn.Active) { WindowPosition (dialogWindow); PositionWidgetPopup (true); dialogWindow.Show (); btn.Label = "Selected"; } else { PositionWidgetPopup (true); dialogWindow.Hide (); btn.Label = "Unselected"; } } static void Window_Delete (object obj, DeleteEventArgs args) { Application.Quit (); args.RetVal = true; } static void DeactivateMenu (object sender, EventArgs args) { Gtk.Menu menu = (Gtk.Menu) sender; menu.Popdown (); } static void PopupMenu (Gtk.Menu menu, Gdk.EventButton ev) { menu.Deactivated += DeactivateMenu; menu.Popup (null, null, new Gtk.MenuPositionFunc (GetMenuPosition), IntPtr.Zero, (ev == null) ? 0 : ev.Button, (ev == null) ? Gtk.Global.CurrentEventTime : ev.Time); } static void GetMenuPosition (Gtk.Menu menu, out int x, out int y, out bool push_in) { Gtk.Requisition menu_req = menu.SizeRequest (); menu.AttachWidget.GdkWindow.GetOrigin (out x, out y); if (y + menu_req.Height >= menu.AttachWidget.Screen.Height) y -= menu_req.Height; else y += menu.AttachWidget.Allocation.Height; push_in = true; } }