From f4922a95399da1ddae2928afe8da847cd9cc74d2 Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Fri, 20 Jun 2008 14:58:21 +0100 Subject: [PATCH] Moved some useful GUI code into zeroinstall.gtkui.gtkutils. --- zeroinstall/0launch-gui/dialog.py | 50 ++--------------------- zeroinstall/0launch-gui/mainwindow.py | 6 +-- zeroinstall/gtkui/gtkutils.py | 76 +++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 50 deletions(-) create mode 100644 zeroinstall/gtkui/gtkutils.py diff --git a/zeroinstall/0launch-gui/dialog.py b/zeroinstall/0launch-gui/dialog.py index 904452d..c3545ff 100644 --- a/zeroinstall/0launch-gui/dialog.py +++ b/zeroinstall/0launch-gui/dialog.py @@ -5,6 +5,7 @@ import gtk import gtk.glade import os from zeroinstall.support import tasks +from zeroinstall.gtkui import gtkutils n_windows = 0 @@ -12,18 +13,9 @@ last_error = None gladefile = os.path.join(os.path.dirname(__file__), 'zero-install.glade') -# Wrapped for glade widget tree that throws a sensible exception if the widget isn't found -class Template: +class Template(gtkutils.Template): def __init__(self, root): - self.widgets = gtk.glade.XML(gladefile, root) - self.root = root - - def get_widget(self, name = None): - if not name: - name = self.root - widget = self.widgets.get_widget(name) - assert widget, "Widget '%s' not found in glade file '%s'" % (name, gladefile) - return widget + gtkutils.Template.__init__(self, gladefile, root) class Dialog(gtk.Dialog): __shown = False @@ -66,14 +58,7 @@ def alert(parent, message, type = gtk.MESSAGE_ERROR): global last_error last_error = message - box = gtk.MessageDialog(parent, gtk.DIALOG_DESTROY_WITH_PARENT, - type, gtk.BUTTONS_OK, - str(message)) - box.set_position(gtk.WIN_POS_CENTER) - def resp(b, r): - b.destroy() - box.connect('response', resp) - box.show() + gtkutils.show_message_box(parent, message, type) def MixedButton(message, stock, x_align = 0.5, button = None): if button is None: @@ -110,30 +95,3 @@ def frame(page, title, content, expand = False): else: content.set_border_width(8) page.pack_start(frame, expand, True, 0) - -def get_busy_pointer(gdk_window): - # This is crazy. We build a cursor that looks like the old - # Netscape busy-with-a-pointer cursor and set that, then the - # X server replaces it with a decent-looking one!! - # See http://mail.gnome.org/archives/gtk-list/2007-May/msg00100.html - - bit_data = "\ -\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\ -\x0c\x00\x00\x00\x1c\x00\x00\x00\x3c\x00\x00\x00\ -\x7c\x00\x00\x00\xfc\x00\x00\x00\xfc\x01\x00\x00\ -\xfc\x3b\x00\x00\x7c\x38\x00\x00\x6c\x54\x00\x00\ -\xc4\xdc\x00\x00\xc0\x44\x00\x00\x80\x39\x00\x00\ -\x80\x39\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ -\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ -\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ -\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ -\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ -\x00\x00\x00\x00\x00\x00\x00\x00" - - try: - pix = gtk.gdk.bitmap_create_from_data(None, bit_data, 32, 32) - color = gtk.gdk.Color() - return gtk.gdk.Cursor(pix, pix, color, color, 2, 2) - except: - #old bug http://bugzilla.gnome.org/show_bug.cgi?id=103616 - return gtk.gdk.Cursor(gtk.gdk.WATCH) diff --git a/zeroinstall/0launch-gui/mainwindow.py b/zeroinstall/0launch-gui/mainwindow.py index 8c6b7b8..0364013 100644 --- a/zeroinstall/0launch-gui/mainwindow.py +++ b/zeroinstall/0launch-gui/mainwindow.py @@ -10,6 +10,7 @@ from zeroinstall.injector import download from iface_browser import InterfaceBrowser import help_box import dialog +from zeroinstall.gtkui import gtkutils tips = gtk.Tooltips() @@ -18,7 +19,6 @@ SHOW_PREFERENCES = 0 class MainWindow: progress = None progress_area = None - busy_pointer = None browser = None window = None cancel_download_and_run = None @@ -136,9 +136,7 @@ class MainWindow: if not self.progress_area.get_property('visible'): self.progress_area.show() - if self.busy_pointer is None: - self.busy_pointer = dialog.get_busy_pointer(self.window.window) - self.window.window.set_cursor(self.busy_pointer) + self.window.window.set_cursor(gtkutils.get_busy_pointer()) any_known = False done = total = self.policy.handler.total_bytes_downloaded # Completed downloads diff --git a/zeroinstall/gtkui/gtkutils.py b/zeroinstall/gtkui/gtkutils.py new file mode 100644 index 0000000..28475ef --- /dev/null +++ b/zeroinstall/gtkui/gtkutils.py @@ -0,0 +1,76 @@ +"""Useful utility methods for GTK.""" +# Copyright (C) 2008, Thomas Leonard +# See the README file for details, or visit http://0install.net. + +import gtk +import gtk.glade + +class Template: + """Wrapper for glade widget tree that throws a sensible exception if the widget isn't found.""" + def __init__(self, gladefile, root): + """Constructor. + @param gladefile: pathname of the .glade file to load + @param root: the name of the top-level widget inside the file""" + self.widgets = gtk.glade.XML(gladefile, root) + self.gladefile = gladefile + self.root = root + + def get_widget(self, name = None): + """Look up a widget by name.""" + if not name: + name = self.root + widget = self.widgets.get_widget(name) + assert widget, "Widget '%s' not found in glade file '%s'" % (name, self.gladefile) + return widget + +def show_message_box(parent, message, type = gtk.MESSAGE_ERROR): + """Display a non-modal message box with an OK button. + @param parent: the parent window + @param message: the message to be displayed + @param type: the type of box (used for the icon)""" + box = gtk.MessageDialog(parent, gtk.DIALOG_DESTROY_WITH_PARENT, + type, gtk.BUTTONS_OK, + str(message)) + box.set_position(gtk.WIN_POS_CENTER) + def resp(b, r): + b.destroy() + box.connect('response', resp) + box.show() + +_busy_pointer = None +def get_busy_pointer(): + """Get a GDK background-activity cursor. + Use this when something is happening, but the GUI is still responsive. + @return: the busy cursor (a singleton) + @rtype: gdk.Cursor + """ + global _busy_pointer + if _busy_pointer is not None: + return _busy_pointer + + # This is crazy. We build a cursor that looks like the old + # Netscape busy-with-a-pointer cursor and set that, then the + # X server replaces it with a decent-looking one!! + # See http://mail.gnome.org/archives/gtk-list/2007-May/msg00100.html + + bit_data = "\ +\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\ +\x0c\x00\x00\x00\x1c\x00\x00\x00\x3c\x00\x00\x00\ +\x7c\x00\x00\x00\xfc\x00\x00\x00\xfc\x01\x00\x00\ +\xfc\x3b\x00\x00\x7c\x38\x00\x00\x6c\x54\x00\x00\ +\xc4\xdc\x00\x00\xc0\x44\x00\x00\x80\x39\x00\x00\ +\x80\x39\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ +\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ +\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ +\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ +\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ +\x00\x00\x00\x00\x00\x00\x00\x00" + + try: + pix = gtk.gdk.bitmap_create_from_data(None, bit_data, 32, 32) + color = gtk.gdk.Color() + _busy_pointer = gtk.gdk.Cursor(pix, pix, color, color, 2, 2) + except: + #old bug http://bugzilla.gnome.org/show_bug.cgi?id=103616 + _busy_pointer = gtk.gdk.Cursor(gtk.gdk.WATCH) + return _busy_pointer -- 2.11.4.GIT