1"""Useful utility methods for GTK.""" 2 3# Copyright (C) 2009, Thomas Leonard 4# See the README file for details, or visit http://0install.net. 5 6importgtk 7fromzeroinstall.supportimporttasks 8
12"""Constructor. 13 @param builderfile: pathname of the .ui file to load 14 @param root: the name of the top-level widget inside the file""" 15self.builder=gtk.Builder() 16self.builder.set_translation_domain('zero-install') 17self.builder.add_from_file(builderfile) 18self.builderfile=builderfile 19self.root=root
22"""Look up a widget by name.""" 23ifnotname: 24name=self.root 25widget=self.builder.get_object(name) 26assertwidget,"Widget '%s' not found in GtkBuilder file '%s'"%(name,self.builderfile) 27returnwidget
30"""Display a non-modal message box with an OK button. 31 @param parent: the parent window 32 @param message: the message to be displayed 33 @param type: the type of box (used for the icon)""" 34box=gtk.MessageDialog(parent,gtk.DIALOG_DESTROY_WITH_PARENT, 35type,gtk.BUTTONS_OK, 36str(message)) 37box.set_position(gtk.WIN_POS_CENTER) 38defresp(b,r): 39b.destroy()
45"""Get a GDK background-activity cursor. 46 Use this when something is happening, but the GUI is still responsive. 47 @return: the busy cursor (a singleton) 48 @rtype: gdk.Cursor 49 """ 50global_busy_pointer 51if_busy_pointerisnotNone: 52return_busy_pointer 53 54# This is crazy. We build a cursor that looks like the old 55# Netscape busy-with-a-pointer cursor and set that, then the 56# X server replaces it with a decent-looking one!! 57# See http://mail.gnome.org/archives/gtk-list/2007-May/msg00100.html 58 59bit_data="\ 60\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\ 61\x0c\x00\x00\x00\x1c\x00\x00\x00\x3c\x00\x00\x00\ 62\x7c\x00\x00\x00\xfc\x00\x00\x00\xfc\x01\x00\x00\ 63\xfc\x3b\x00\x00\x7c\x38\x00\x00\x6c\x54\x00\x00\ 64\xc4\xdc\x00\x00\xc0\x44\x00\x00\x80\x39\x00\x00\ 65\x80\x39\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\x00\x00\x00\x00\ 70\x00\x00\x00\x00\x00\x00\x00\x00" 71 72try: 73pix=gtk.gdk.bitmap_create_from_data(None,bit_data,32,32) 74color=gtk.gdk.Color() 75_busy_pointer=gtk.gdk.Cursor(pix,pix,color,color,2,2) 76except: 77#old bug http://bugzilla.gnome.org/show_bug.cgi?id=103616 78_busy_pointer=gtk.gdk.Cursor(gtk.gdk.WATCH) 79return_busy_pointer
106"""A GtkButton with a stock icon.107 @since: 1.5"""108ifbuttonisNone:109button=gtk.Button()110111label=gtk.Label('')112label.set_text_with_mnemonic(message)113label.set_mnemonic_widget(button)114115image=gtk.image_new_from_stock(stock,gtk.ICON_SIZE_BUTTON)116box=gtk.HBox()117box.set_homogeneous(False)118box.set_spacing(2)119align=gtk.Alignment(x_align,0.5,0.0,0.0)120121box.pack_start(image,False,False,0)122box.pack_end(label,False,False,0)123124button.add(align)125align.add(box)126returnbutton