def __init__(self, app, actions, config_name): """Initialize. :param app: the main Application object. :param iterable actions: keyboard action names to pass through. :param str config_name: config prefix for saving window size. Use a simple "lowercase_with_underscores" name for the configuration key prefix. See also: `gui.keyboard.KeyboardManager.add_window()`. """ # Superclass Gtk.Window.__init__(self, type=Gtk.WindowType.POPUP) self.set_modal(True) # Internal state self.app = app self._size = None # last recorded size from any show() self._motion_handler_id = None self._prefs_size_key = "%s.window_size" % (config_name, ) self._resize_info = None # state during an edge resize self._outside_grab_active = False self._outside_cursor = Gdk.Cursor(Gdk.CursorType.LEFT_PTR) self._popup_info = None # Initial positioning self._initial_move_pos = None # used when forcing a specific position self._corrected_pos = None # used when keeping the widget on-screen # Resize cursors self._edge_cursors = {} for edge, cursor in self.EDGE_CURSORS.items(): if cursor is not None: cursor = Gdk.Cursor(cursor) self._edge_cursors[edge] = cursor # Default size self.set_gravity(Gdk.Gravity.NORTH_WEST) default_size = (self.MIN_WIDTH, self.MIN_HEIGHT) w, h = app.preferences.get(self._prefs_size_key, default_size) w = clamp(int(w), self.MIN_WIDTH, self.MAX_WIDTH) h = clamp(int(h), self.MIN_HEIGHT, self.MAX_HEIGHT) default_size = (w, h) self.set_transient_for(app.drawWindow) self.set_default_size(*default_size) self.set_position(Gtk.WindowPosition.MOUSE) # Register with the keyboard manager, but only let certain actions be # driven from the keyboard. app.kbm.add_window(self, actions) # Event handlers self.connect("realize", self._realize_cb) self.connect("configure-event", self._configure_cb) self.connect("enter-notify-event", self._crossing_cb) self.connect("leave-notify-event", self._crossing_cb) self.connect("show", self._show_cb) self.connect("hide", self._hide_cb) self.connect("button-press-event", self._button_press_cb) self.connect("button-release-event", self._button_release_cb) self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK) # Appearance self._frame = Gtk.Frame() self._frame.set_shadow_type(Gtk.ShadowType.OUT) self._align = Gtk.Alignment.new(0.5, 0.5, 1.0, 1.0) self._align.set_padding(self.EDGE_SIZE, self.EDGE_SIZE, self.EDGE_SIZE, self.EDGE_SIZE) self._frame.add(self._align) Gtk.Window.add(self, self._frame)
def _test(): logging.basicConfig(level=logging.DEBUG) import os import sys class _TestLabel(Gtk.Label): __gtype_name__ = 'TestLabel' tool_widget_icon_name = 'gtk-ok' tool_widget_description = "Just a test widget" def __init__(self, text): Gtk.Label.__init__(self, text) self.set_size_request(200, 150) class _TestSpinner(Gtk.Spinner): __gtype_name__ = "TestSpinner" tool_widget_icon_name = 'gtk-cancel' tool_widget_description = "Spinner test" def __init__(self): Gtk.Spinner.__init__(self) self.set_size_request(150, 150) self.set_property("active", True) def _tool_shown_cb(*a): logger.debug("TOOL-SHOWN %r", a) def _tool_hidden_cb(*a): logger.debug("TOOL-HIDDEN %r", a) def _floating_window_created(*a): logger.debug("FLOATING-WINDOW-CREATED %r", a) workspace = Workspace() workspace.floating_window_title_suffix = u" - Test" button = Gtk.Button(label="Click to close this demo") frame = Gtk.Frame() frame.add(button) frame.set_shadow_type(Gtk.ShadowType.IN) workspace.set_canvas(frame) window = Gtk.Window() window.add(workspace) window.set_title(os.path.basename(sys.argv[0])) workspace.set_size_request(600, 400) workspace.tool_widget_added += _tool_shown_cb workspace.tool_widget_removed += _tool_hidden_cb workspace.floating_window_created += _floating_window_created workspace.build_from_layout({ 'position': { 'x': 100, 'y': 75, 'h': -100, 'w': -100 }, 'floating': [{ 'position': { 'y': -100, 'h': 189, 'w': 152, 'x': -200 }, 'contents': { 'groups': [{ 'tools': [('TestLabel', "1"), ('TestLabel', "2")], }], } }], 'right_sidebar': { 'w': 400, 'groups': [{ 'tools': [('TestSpinner', ), ("TestLabel", "3")], }], }, 'left_sidebar': { 'w': 250, 'groups': [{ 'tools': [('TestLabel', "4"), ('TestLabel', "5")], }], }, 'maximized': False, 'fullscreen': True, }) window.show_all() def _quit_cb(*a): logger.info("Demo quit, workspace dump follows") print(workspace.get_layout()) Gtk.main_quit() window.connect("destroy", _quit_cb) button.connect("clicked", _quit_cb) Gtk.main()