1from gi.repository import Gtk, Gdk, GObject 2 3from pychess.widgets import mainwindow 4 5 6class ImageMenu(Gtk.EventBox): 7 def __init__(self, image, child): 8 GObject.GObject.__init__(self) 9 self.add(image) 10 11 self.subwindow = Gtk.Window() 12 self.subwindow.set_transient_for(mainwindow()) 13 self.subwindow.set_decorated(False) 14 self.subwindow.set_resizable(False) 15 self.subwindow.set_type_hint(Gdk.WindowTypeHint.DIALOG) 16 self.subwindow.add(child) 17 self.subwindow.connect_after("draw", self.__sub_onExpose) 18 self.subwindow.connect("button_press_event", self.__sub_onPress) 19 # self.subwindow.connect("motion_notify_event", self.__sub_onMotion) 20 # self.subwindow.connect("leave_notify_event", self.__sub_onMotion) 21 # self.subwindow.connect("delete-event", self.__sub_onDelete) 22 self.subwindow.connect("focus-out-event", self.__sub_onFocusOut) 23 child.show_all() 24 25 self.setOpen(False) 26 self.connect("button_press_event", self.__onPress) 27 28 def setOpen(self, isopen): 29 self.isopen = isopen 30 31 if isopen: 32 topwindow = self.get_parent() 33 while not isinstance(topwindow, Gtk.Window): 34 topwindow = topwindow.get_parent() 35 x_loc, y_loc = topwindow.get_window().get_position() 36 x_loc += self.get_allocation().x + self.get_allocation().width 37 y_loc += self.get_allocation().y 38 self.subwindow.move(x_loc, y_loc) 39 40 self.subwindow.props.visible = isopen 41 self.set_state(self.isopen and Gtk.StateType.SELECTED or 42 Gtk.StateType.NORMAL) 43 44 def __onPress(self, self_, event): 45 if event.button == 1 and event.type == Gdk.EventType.BUTTON_PRESS: 46 self.setOpen(not self.isopen) 47 48 def __sub_setGrabbed(self, grabbed): 49 if grabbed and not Gdk.pointer_is_grabbed(): 50 Gdk.pointer_grab(self.subwindow.get_window(), True, 51 Gdk.EventMask.LEAVE_NOTIFY_MASK | 52 Gdk.EventMask.POINTER_MOTION_MASK | 53 Gdk.EventMask.BUTTON_PRESS_MASK, None, None, 54 Gdk.CURRENT_TIME) 55 Gdk.keyboard_grab(self.subwindow.get_window(), True, 56 Gdk.CURRENT_TIME) 57 elif Gdk.pointer_is_grabbed(): 58 Gdk.pointer_ungrab(Gdk.CURRENT_TIME) 59 Gdk.keyboard_ungrab(Gdk.CURRENT_TIME) 60 61 def __sub_onMotion(self, subwindow, event): 62 allocation = subwindow.get_allocation() 63 self.__sub_setGrabbed(not (0 <= event.x < allocation.width and 0 <= event.y < 64 allocation.height)) 65 66 def __sub_onPress(self, subwindow, event): 67 allocation = subwindow.get_allocation() 68 if not (0 <= event.x < allocation.width and 0 <= event.y < allocation.height): 69 Gdk.pointer_ungrab(event.time) 70 self.setOpen(False) 71 72 def __sub_onExpose(self, subwindow, ctx): 73 allocation = subwindow.get_allocation() 74 context = subwindow.get_window().cairo_create() 75 context.set_line_width(2) 76 context.rectangle(allocation.x, allocation.y, 77 allocation.width, allocation.height) 78 style_ctxt = self.get_style_context() 79 color = style_ctxt.lookup_color("p_dark_color")[1] 80 red, green, blue, alpha = color.red, color.green, color.blue, color.alpha 81 context.set_source_rgba(red, green, blue, alpha) 82 context.stroke() 83 # self.__sub_setGrabbed(self.isopen) 84 85 def __sub_onDelete(self, subwindow, event): 86 self.setOpen(False) 87 return True 88 89 def __sub_onFocusOut(self, subwindow, event): 90 self.setOpen(False) 91 92 93def switchWithImage(image, dialog): 94 parent = image.get_parent() 95 parent.remove(image) 96 imageMenu = ImageMenu(image, dialog) 97 parent.add(imageMenu) 98 imageMenu.show() 99 100 101if __name__ == "__main__": 102 win = Gtk.Window() 103 vbox = Gtk.VBox() 104 vbox.add(Gtk.Label(label="Her er der en kat")) 105 image = Gtk.Image.new_from_icon_name("gtk-properties", Gtk.IconSize.BUTTON) 106 vbox.add(image) 107 vbox.add(Gtk.Label(label="Her er der ikke en kat")) 108 win.add(vbox) 109 110 table = Gtk.Table(2, 2) 111 table.attach(Gtk.Label(label="Minutes:"), 0, 1, 0, 1) 112 spin1 = Gtk.SpinButton() 113 spin1.set_adjustment(Gtk.Adjustment(0, 0, 100, 1)) 114 table.attach(spin1, 1, 2, 0, 1) 115 table.attach(Gtk.Label(label="Gain:"), 0, 1, 1, 2) 116 spin2 = Gtk.SpinButton() 117 spin2.set_adjustment(Gtk.Adjustment(0, 0, 100, 1)) 118 table.attach(spin2, 1, 2, 1, 2) 119 table.set_border_width(6) 120 121 switchWithImage(image, table) 122 123 def onValueChanged(spin): 124 print(spin.get_value()) 125 126 spin1.connect("value-changed", onValueChanged) 127 spin2.connect("value-changed", onValueChanged) 128 129 win.show_all() 130 win.connect("delete-event", Gtk.main_quit) 131 Gtk.main() 132