Import _ into each module rather than using a builtin
[zeroinstall.git] / zeroinstall / gtkui / gtkutils.py
blob35855e5d1f3bce13e9104a242ff4c0f281dc945f
1 """Useful utility methods for GTK."""
3 # Copyright (C) 2009, Thomas Leonard
4 # See the README file for details, or visit http://0install.net.
6 from zeroinstall import _
7 import gtk
8 import gtk.glade
10 class Template:
11 """Wrapper for glade widget tree that throws a sensible exception if the widget isn't found."""
12 def __init__(self, gladefile, root):
13 """Constructor.
14 @param gladefile: pathname of the .glade file to load
15 @param root: the name of the top-level widget inside the file"""
16 self.widgets = gtk.glade.XML(gladefile, root)
17 self.gladefile = gladefile
18 self.root = root
20 def get_widget(self, name = None):
21 """Look up a widget by name."""
22 if not name:
23 name = self.root
24 widget = self.widgets.get_widget(name)
25 assert widget, "Widget '%s' not found in glade file '%s'" % (name, self.gladefile)
26 return widget
28 def show_message_box(parent, message, type = gtk.MESSAGE_ERROR):
29 """Display a non-modal message box with an OK button.
30 @param parent: the parent window
31 @param message: the message to be displayed
32 @param type: the type of box (used for the icon)"""
33 box = gtk.MessageDialog(parent, gtk.DIALOG_DESTROY_WITH_PARENT,
34 type, gtk.BUTTONS_OK,
35 str(message))
36 box.set_position(gtk.WIN_POS_CENTER)
37 def resp(b, r):
38 b.destroy()
39 box.connect('response', resp)
40 box.show()
42 _busy_pointer = None
43 def get_busy_pointer():
44 """Get a GDK background-activity cursor.
45 Use this when something is happening, but the GUI is still responsive.
46 @return: the busy cursor (a singleton)
47 @rtype: gdk.Cursor
48 """
49 global _busy_pointer
50 if _busy_pointer is not None:
51 return _busy_pointer
53 # This is crazy. We build a cursor that looks like the old
54 # Netscape busy-with-a-pointer cursor and set that, then the
55 # X server replaces it with a decent-looking one!!
56 # See http://mail.gnome.org/archives/gtk-list/2007-May/msg00100.html
58 bit_data = "\
59 \x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\
60 \x0c\x00\x00\x00\x1c\x00\x00\x00\x3c\x00\x00\x00\
61 \x7c\x00\x00\x00\xfc\x00\x00\x00\xfc\x01\x00\x00\
62 \xfc\x3b\x00\x00\x7c\x38\x00\x00\x6c\x54\x00\x00\
63 \xc4\xdc\x00\x00\xc0\x44\x00\x00\x80\x39\x00\x00\
64 \x80\x39\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
65 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
66 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
67 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
68 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
69 \x00\x00\x00\x00\x00\x00\x00\x00"
71 try:
72 pix = gtk.gdk.bitmap_create_from_data(None, bit_data, 32, 32)
73 color = gtk.gdk.Color()
74 _busy_pointer = gtk.gdk.Cursor(pix, pix, color, color, 2, 2)
75 except:
76 #old bug http://bugzilla.gnome.org/show_bug.cgi?id=103616
77 _busy_pointer = gtk.gdk.Cursor(gtk.gdk.WATCH)
78 return _busy_pointer