#!/usr/bin/env python3 import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, Gdk import subprocess import shlex from desktop_tool import DesktopToolWidget from desktop_tool import get_icon as get_icon from peppermint_sp_parse_xml import Peppermint_sp #======================================================================= class Config: # Position of NoteBook tabs Gtk.PositionType.TOP or Gtk.PositionType.LEFT TAB_POS = Gtk.PositionType.LEFT # Number of columns NCOLUMNS = 3 # Sizes ICON_SIZE = 48 MAIN_WINDOW_BORDER_WIDTH = 0 BTN_WIDTH = 150 BTN_HEIGHT = 50 # Icons MAIN_WINDOW_ICON = "preferences-desktop" #======================================================================= DEBUG = False def debug(msg): if DEBUG: print("===> debug: " + msg) class Notebook(Gtk.Notebook): def __init__(self): Gtk.Notebook.__init__(self) self.set_tab_pos(Config.TAB_POS) class Table(Gtk.Grid): def __init__(self): Gtk.Grid.__init__(self) def attach(self, row, col, widget): Gtk.Grid.attach(self, widget, col, row, 1, 1) class CategoryFrame(Gtk.Frame) : def __init__(self, category): self.btn_height = Config.BTN_HEIGHT self.btn_width = Config.BTN_WIDTH Gtk.Frame.__init__(self) self.alignment = Gtk.Alignment.new(0.0, 0.0, 1.0, 1.0) self.alignment.set_padding(10, 10, 10, 10) self.add(self.alignment) self.table = Table() self.alignment.add(self.table) self.tool_button = [] self.load_tools(category) def load_tools(self, category): row = 0 col = 0 i = 0 for tool in category.tools: if Config.TAB_POS == Gtk.PositionType.LEFT: orientation = Gtk.Orientation.VERTICAL wrap_len = 20 else: orientation = Gtk.Orientation.HORIZONTAL wrap_len = 0 # no wrap icon_button = DesktopToolWidget(tool.label, tool.icon, Config.ICON_SIZE, orientation, wrap = wrap_len) self.tool_button.insert(i, icon_button) self.tool_button[i].set_size_request(self.btn_width, self.btn_height) self.tool_button[i].connect("button_press_event", self.on_item_activated, tool.action) self.table.attach(row, col, self.tool_button[i]) self.table.set_column_spacing(10) self.table.set_row_spacing(10) i += 1 col += 1 if col == Config.NCOLUMNS: col = 0 row += 1 def on_item_activated(self, button, event, action): if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 1: print("on_item_activated: button [%s]" % action) # Convert utf-8 string to ascii udata = action.encode("utf-8") asciidata = udata.decode("ascii","ignore") # Build the args list expected by subprocess.Popen args = shlex.split(asciidata) # And start action in a subprocess print(args) process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) class MainWindow: def __init__(self): window = Gtk.Window(Gtk.WindowType.TOPLEVEL) window.set_position(Gtk.WindowPosition.CENTER) window.set_margin_end(Config.MAIN_WINDOW_BORDER_WIDTH) pixbuf = get_icon(Config.MAIN_WINDOW_ICON, 16) window.set_icon(pixbuf) window.connect("delete_event", self.onDeleteEvent) # Create a new notebook self.notebook = Notebook() # Add the tab pages cat_index = 0 peppermint_sp = Peppermint_sp("peppermint_sp.xml") window.set_title(peppermint_sp.title) for category in peppermint_sp.categories: category_name = category.title if True: tab_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=2) tab_label = Gtk.Label(category_name) cat_index += 1 pixbuf = get_icon(category.icon, Config.ICON_SIZE) tab_icon = Gtk.Image.new_from_pixbuf(pixbuf) tab_icon.set_size_request(-1, 60) tab_box.pack_start(tab_icon, False, False, 0) tab_box.pack_start(tab_label, False, False, 0) tab_box.show_all() else: tab_box = DesktopToolWidget(category_name, category.icon, Config.ICON_SIZE, Gtk.Orientation.HORIZONTAL) for tool in category.tools: frame = CategoryFrame(category) frame.show_all() self.notebook.append_page(frame, tab_box) # Set what page to start at self.notebook.set_current_page(0) self.notebook.show_all() window.add(self.notebook) window.show_all() def onDeleteEvent(self, widget, event=None): Gtk.main_quit() return False def main(): Gtk.main() return 0 if __name__ == "__main__": try: MainWindow() main() except KeyboardInterrupt: print("\nBye.") sys.exit()