|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +# dockbarx_mate_applet |
| 4 | +# |
| 5 | +# Copyright 2008, 2009, 2010 Aleksey Shaferov and Matias Sars |
| 6 | +# Copyright 2017 Alexey Hohlov |
| 7 | +# |
| 8 | +# DockbarX is free software: you can redistribute it and/or modify |
| 9 | +# it under the terms of the GNU General Public License as published by |
| 10 | +# the Free Software Foundation, either version 3 of the License, or |
| 11 | +# (at your option) any later version. |
| 12 | +# |
| 13 | +# DockbarX is distributed in the hope that it will be useful, |
| 14 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +# GNU General Public License for more details. |
| 17 | +# |
| 18 | +# You should have received a copy of the GNU General Public License |
| 19 | +# along with dockbar. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + |
| 21 | +import gi |
| 22 | +gi.require_version("Gtk", "3.0") |
| 23 | +gi.require_version('MatePanelApplet', '4.0') |
| 24 | +from gi.repository import Gtk, GLib |
| 25 | +from gi.repository import MatePanelApplet |
| 26 | + |
| 27 | +try: |
| 28 | + import sys |
| 29 | + import dockbarx.dockbar |
| 30 | + from dockbarx.log import * |
| 31 | +except Exception as e: |
| 32 | + print(e) |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | +class DockbarMateApplet(object): |
| 36 | + def __init__(self, applet, iid): |
| 37 | + self.applet = applet |
| 38 | + self.size = None |
| 39 | + self.dockbar = dockbarx.dockbar.DockBar(self) |
| 40 | + applet.set_flags(MatePanelApplet.AppletFlags.HAS_HANDLE | \ |
| 41 | + MatePanelApplet.AppletFlags.EXPAND_MINOR | \ |
| 42 | + MatePanelApplet.AppletFlags.EXPAND_MAJOR) |
| 43 | + orients = {MatePanelApplet.AppletOrient.DOWN: "down", |
| 44 | + MatePanelApplet.AppletOrient.UP: "up", |
| 45 | + MatePanelApplet.AppletOrient.LEFT: "left", |
| 46 | + MatePanelApplet.AppletOrient.RIGHT: "right"} |
| 47 | + self.dockbar.set_orient(orients[applet.get_orient()]) |
| 48 | + menu_actions = [("About", Gtk.STOCK_ABOUT, _("About"), None, None, self.dockbar.on_ppm_about), |
| 49 | + ("Pref", Gtk.STOCK_PREFERENCES, _("Preferences"), None, None, self.dockbar.on_ppm_pref), |
| 50 | + ("Reload", Gtk.STOCK_REFRESH, _("Refresh"), None, None, self.dockbar.reload)] |
| 51 | + actiongroup = Gtk.ActionGroup.new("DockBarXAppletActions") |
| 52 | + #actiongroup.set_translation_domain(dockbarx.defs.GETTEXT_PACKAGE) |
| 53 | + actiongroup.add_actions(menu_actions, None) |
| 54 | + applet.setup_menu_from_file("%PREFIX%/share/mate-panel/ui/dockbarx-applet-menu.xml", actiongroup) |
| 55 | + |
| 56 | + # Set the applet coordinates to be way ofscreen until they've |
| 57 | + # been set correctly by a size allocate call. |
| 58 | + self.applet_origin_x = -1000 |
| 59 | + self.applet_origin_y = -1000 |
| 60 | + applet.connect("delete-event", self.__cleanup) |
| 61 | + applet.show_all() |
| 62 | + |
| 63 | + # Most of initializion must happen after dockbarx is |
| 64 | + # realized since python mateapplets crash if it |
| 65 | + # takes too long to realize. |
| 66 | + GLib.idle_add(self.__load_on_realized) |
| 67 | + |
| 68 | + def __load_on_realized(self): |
| 69 | + # Wait while gtk events are pending. |
| 70 | + while Gtk.events_pending(): |
| 71 | + Gtk.main_iteration() |
| 72 | + # Load DockbarX. |
| 73 | + self.dockbar.load() |
| 74 | + # Add it to the applet. |
| 75 | + self.applet.add(self.dockbar.get_container()) |
| 76 | + self.applet.show_all() |
| 77 | + # Connect events. |
| 78 | + self.applet.connect("size-allocate", self.__on_applet_size_alloc) |
| 79 | + #self.applet.connect("change_background", self.__on_change_background) |
| 80 | + self.applet.connect("change-orient", self.__on_change_orient) |
| 81 | + |
| 82 | + def __on_applet_size_alloc(self, widget, allocation): |
| 83 | + if self.applet.get_orient() in (MatePanelApplet.AppletOrient.DOWN, |
| 84 | + MatePanelApplet.AppletOrient.UP): |
| 85 | + size = allocation.height |
| 86 | + else: |
| 87 | + size = allocation.width |
| 88 | + if self.size != size: |
| 89 | + self.dockbar.set_size(size) |
| 90 | + self.size = size |
| 91 | + if not widget.get_window(): |
| 92 | + return |
| 93 | + #x,y = widget.get_window().get_origin() |
| 94 | + x,y = widget.get_window().get_root_coords(0, 0) |
| 95 | + if x == self.applet_origin_x or y == self.applet_origin_y: |
| 96 | + # Nothing moved. |
| 97 | + return |
| 98 | + # Applet and/or panel moved, |
| 99 | + # icon_geo needs to be updated. |
| 100 | + self.applet_origin_x = x |
| 101 | + self.applet_origin_y = y |
| 102 | + self.dockbar.dockbar_moved() |
| 103 | + |
| 104 | + def __on_change_orient(self, arg1, data): |
| 105 | + orients = {MatePanelApplet.AppletOrient.DOWN: "down", |
| 106 | + MatePanelApplet.AppletOrient.UP: "up", |
| 107 | + MatePanelApplet.AppletOrient.LEFT: "left", |
| 108 | + MatePanelApplet.AppletOrient.RIGHT: "right"} |
| 109 | + self.applet.remove(self.dockbar.get_container()) |
| 110 | + self.dockbar.set_orient(orients[self.applet.get_orient()]) |
| 111 | + self.applet.add(self.dockbar.get_container()) |
| 112 | + |
| 113 | + def __on_change_background(self, applet, type, color, pixmap): |
| 114 | + applet.set_style(None) |
| 115 | + rc_style = Gtk.RcStyle() |
| 116 | + applet.modify_style(rc_style) |
| 117 | + if type == MatePanelApplet.AppletBackgroundType.COLOR_BACKGROUND: |
| 118 | + applet.modify_bg(Gtk.StateType.NORMAL, color) |
| 119 | + elif type == MatePanelApplet.AppletBackgroundType.PIXMAP_BACKGROUND: |
| 120 | + style = applet.style |
| 121 | + style.bg_pixmap[Gtk.StateType.NORMAL] = pixmap |
| 122 | + applet.set_style(style) |
| 123 | + return |
| 124 | + |
| 125 | + def readd_container (self, container): |
| 126 | + self.applet.add(container) |
| 127 | + container.show_all() |
| 128 | + |
| 129 | + def __cleanup(self,event): |
| 130 | + if hasattr(self.dockbar, "destroy"): |
| 131 | + self.dockbar.destroy() |
| 132 | + |
| 133 | + |
| 134 | +def applet_factory(applet, iid, data): |
| 135 | + DockbarMateApplet(applet, iid) |
| 136 | + return True |
| 137 | + |
| 138 | +MatePanelApplet.Applet.factory_main("DockbarXAppletFactory", True, |
| 139 | + MatePanelApplet.Applet.__gtype__, |
| 140 | + applet_factory, None) |
0 commit comments