summaryrefslogtreecommitdiff
path: root/examples/pygtk-demo
diff options
context:
space:
mode:
Diffstat (limited to 'examples/pygtk-demo')
-rw-r--r--examples/pygtk-demo/demos/builder.py50
-rw-r--r--examples/pygtk-demo/demos/demo.ui228
-rw-r--r--examples/pygtk-demo/demos/tooltip.py242
3 files changed, 520 insertions, 0 deletions
diff --git a/examples/pygtk-demo/demos/builder.py b/examples/pygtk-demo/demos/builder.py
new file mode 100644
index 00000000..5df9d10c
--- /dev/null
+++ b/examples/pygtk-demo/demos/builder.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+'''Builder
+
+This is a test of the new gtk builder system. It is a
+fairly straight forward port of the example distributed with gtk.
+'''
+
+import gtk
+
+class BuilderDemo(gtk.Window):
+ def __init__(self, parent=None, filename="demos/demo.ui"):
+ gtk.Window.__init__(self)
+ try:
+ self.set_screen(parent.get_screen())
+ except AttributeError:
+ self.connect('destroy', lambda *w: gtk.main_quit())
+ self.set_title(self.__class__.__name__)
+ self.set_default_size(440, 250)
+
+ # create a new empty Builer object
+ builder = gtk.Builder()
+
+ # load an XML definition
+ builder.add_from_file(filename)
+
+ # We must get the vbox and reparent it to self
+ # since we don't use the main window defined in the ui file
+ vbox = builder.get_object("vbox1")
+ vbox.reparent(self)
+
+ # autoconnect the signals from the instance self
+ builder.connect_signals(self)
+
+ self.show_all()
+
+ def about_activate(self, action, data=None):
+ about_dlg = gtk.AboutDialog()
+ about_dlg.set_name("GtkBuilder demo")
+ about_dlg.run()
+ about_dlg.destroy()
+
+ def quit_activate(self, action, data=None):
+ gtk.main_quit()
+
+ def main(self):
+ gtk.main()
+
+if __name__ == '__main__':
+ build = BuilderDemo(None, "demo.ui")
+ build.main()
diff --git a/examples/pygtk-demo/demos/demo.ui b/examples/pygtk-demo/demos/demo.ui
new file mode 100644
index 00000000..1c84db2b
--- /dev/null
+++ b/examples/pygtk-demo/demos/demo.ui
@@ -0,0 +1,228 @@
+<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
+<interface>
+ <object class="GtkListStore" id="liststore1">
+ <columns>
+ <column type="gchararray"/>
+ <column type="gchararray"/>
+ <column type="gint"/>
+ </columns>
+ <data>
+ <row>
+ <col id="0">John</col>
+ <col id="1">Doe</col>
+ <col id="2">25</col>
+ </row>
+ <row>
+ <col id="0">Mary</col>
+ <col id="1">Dole</col>
+ <col id="2">50</col>
+ </row>
+ </data>
+ </object>
+ <object class="GtkUIManager" id="uimanager">
+ <child>
+ <object class="GtkActionGroup" id="DefaultActions">
+ <child>
+ <object class="GtkAction" id="Copy">
+ <property name="name">Copy</property>
+ <property name="tooltip" translatable="yes">Copy selected object into the clipboard</property>
+ <property name="stock_id">gtk-copy</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkAction" id="Cut">
+ <property name="name">Cut</property>
+ <property name="tooltip" translatable="yes">Cut selected object into the clipboard</property>
+ <property name="stock_id">gtk-cut</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkAction" id="EditMenu">
+ <property name="name">EditMenu</property>
+ <property name="label" translatable="yes">_Edit</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkAction" id="FileMenu">
+ <property name="name">FileMenu</property>
+ <property name="label" translatable="yes">_File</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkAction" id="New">
+ <property name="name">New</property>
+ <property name="tooltip" translatable="yes">Create a new file</property>
+ <property name="stock_id">gtk-new</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkAction" id="Open">
+ <property name="name">Open</property>
+ <property name="tooltip" translatable="yes">Open a file</property>
+ <property name="stock_id">gtk-open</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkAction" id="Paste">
+ <property name="name">Paste</property>
+ <property name="tooltip" translatable="yes">Paste object from the Clipboard</property>
+ <property name="stock_id">gtk-paste</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkAction" id="Quit">
+ <property name="name">Quit</property>
+ <property name="tooltip" translatable="yes">Quit the program</property>
+ <property name="stock_id">gtk-quit</property>
+ <signal handler="quit_activate" name="activate"/>
+ </object>
+ </child>
+ <child>
+ <object class="GtkAction" id="Save">
+ <property name="name">Save</property>
+ <property name="is_important">True</property>
+ <property name="tooltip" translatable="yes">Save a file</property>
+ <property name="stock_id">gtk-save</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkAction" id="SaveAs">
+ <property name="name">SaveAs</property>
+ <property name="tooltip" translatable="yes">Save with a different name</property>
+ <property name="stock_id">gtk-save-as</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkAction" id="HelpMenu">
+ <property name="name">HelpMenu</property>
+ <property name="label" translatable="yes">_Help</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkAction" id="About">
+ <property name="name">About</property>
+ <property name="stock_id">gtk-about</property>
+ <signal handler="about_activate" name="activate"/>
+ </object>
+ <accelerator key="F1"/>
+ </child>
+ </object>
+ </child>
+ <ui>
+ <menubar name="menubar1">
+ <menu action="FileMenu" name="FileMenu">
+ <menuitem action="New" name="New"/>
+ <menuitem action="Open" name="Open"/>
+ <menuitem action="Save" name="Save"/>
+ <menuitem action="SaveAs" name="SaveAs"/>
+ <separator/>
+ <menuitem action="Quit" name="Quit"/>
+ </menu>
+ <menu action="EditMenu">
+ <menuitem action="Copy" name="Copy"/>
+ <menuitem action="Cut" name="Cut"/>
+ <menuitem action="Paste" name="Paste"/>
+ </menu>
+ <menu action="HelpMenu" name="HelpMenu">
+ <menuitem action="About" name="About"/>
+ </menu>
+ </menubar>
+ <toolbar name="toolbar1">
+ <toolitem action="New" name="New"/>
+ <toolitem action="Open" name="Open"/>
+ <toolitem action="Save" name="Save"/>
+ <separator/>
+ <toolitem action="Copy" name="Copy"/>
+ <toolitem action="Cut" name="Cut"/>
+ <toolitem action="Paste" name="Paste"/>
+ </toolbar>
+ </ui>
+ </object>
+
+ <object class="GtkWindow" id="window1">
+ <property name="default_height">250</property>
+ <property name="default_width">440</property>
+ <property name="title">builder</property>
+ <child>
+ <object class="GtkVBox" id="vbox1">
+ <property name="visible">True</property>
+ <child>
+ <object constructor="uimanager" class="GtkMenuBar" id="menubar1">
+ <property name="visible">True</property>
+ </object>
+ <packing>
+ <property name="expand">False</property>
+ </packing>
+ </child>
+ <child>
+ <object constructor="uimanager" class="GtkToolbar" id="toolbar1">
+ <property name="visible">True</property>
+ </object>
+ <packing>
+ <property name="expand">False</property>
+ <property name="position">1</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkScrolledWindow" id="scrolledwindow1">
+ <property name="hscrollbar_policy">automatic</property>
+ <property name="shadow_type">in</property>
+ <property name="visible">True</property>
+ <property name="vscrollbar_policy">automatic</property>
+ <child>
+ <object class="GtkTreeView" id="treeview1">
+ <property name="visible">True</property>
+ <property name="model">liststore1</property>
+ <child>
+ <object class="GtkTreeViewColumn" id="column1">
+ <property name="title">Name</property>
+ <child>
+ <object class="GtkCellRendererText" id="renderer1"/>
+ <attributes>
+ <attribute name="text">0</attribute>
+ </attributes>
+ </child>
+ </object>
+ </child>
+ <child>
+ <object class="GtkTreeViewColumn" id="column2">
+ <property name="title">Surname</property>
+ <child>
+ <object class="GtkCellRendererText" id="renderer2"/>
+ <attributes>
+ <attribute name="text">1</attribute>
+ </attributes>
+ </child>
+ </object>
+ </child>
+ <child>
+ <object class="GtkTreeViewColumn" id="column3">
+ <property name="title">Age</property>
+ <child>
+ <object class="GtkCellRendererText" id="renderer3"/>
+ <attributes>
+ <attribute name="text">2</attribute>
+ </attributes>
+ </child>
+ </object>
+ </child>
+ </object>
+ </child>
+ </object>
+ <packing>
+ <property name="position">2</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkStatusbar" id="statusbar1">
+ <property name="visible">True</property>
+ </object>
+ <packing>
+ <property name="expand">False</property>
+ <property name="position">3</property>
+ </packing>
+ </child>
+ </object>
+ </child>
+ </object>
+</interface>
diff --git a/examples/pygtk-demo/demos/tooltip.py b/examples/pygtk-demo/demos/tooltip.py
new file mode 100644
index 00000000..fb7d0637
--- /dev/null
+++ b/examples/pygtk-demo/demos/tooltip.py
@@ -0,0 +1,242 @@
+#!/usr/bin/env python
+'''Tooltip
+
+This is a test of the new gtk tooltip system. It is a
+fairly straight forward port of the example distributed with gtk.
+'''
+
+import gtk
+import cairo
+import gobject
+import pango
+
+rects = [
+ {"x":10, "y":10, "r":0.0, "g":0.0, "b":0.9, "tooltip":"Blue box!"},
+ {"x":200, "y":170, "r":1.0, "g":0.0, "b":0.0, "tooltip":"Red thing"},
+ {"x":100, "y":50, "r":0.8, "g":0.8, "b":0.0, "tooltip":"Yellow thing"}
+ ]
+
+class TooltipDemo(gtk.Window):
+ def __init__(self, parent=None):
+ gtk.Window.__init__(self)
+ try:
+ self.set_screen(parent.get_screen())
+ except AttributeError:
+ self.connect('destroy', lambda *w: gtk.main_quit())
+ self.set_title(self.__class__.__name__)
+
+ self.set_border_width(10)
+
+ box = gtk.VBox(False, 3)
+ self.add(box)
+
+ # A check button using the tooltip-markup property
+ button = gtk.CheckButton("This one uses the tooltip-markup property")
+ button.set_tooltip_text("Hello, I am a static tooltip.")
+ box.pack_start(button, False, False, 0)
+
+ # A check button using the query-tooltip signal
+ button = gtk.CheckButton("I use the query-tooltip signal")
+ button.props.has_tooltip = True
+ button.connect("query-tooltip", self.query_tooltip_cb)
+ box.pack_start(button, False, False, 0)
+
+ # A label
+ label = gtk.Label("I am just a label")
+ label.set_selectable(False)
+ label.set_tooltip_text("Label & and tooltip")
+ box.pack_start(label, False, False, 0)
+
+ # A selectable label
+ label = gtk.Label("I am a selectable label")
+ label.set_selectable(True)
+ label.set_tooltip_markup("<b>Another</b> Label tooltip")
+ box.pack_start(label, False, False, 0)
+
+ # Another one, with a custom tooltip window
+ button = gtk.CheckButton("This one has a custom tooltip window!")
+ box.pack_start(button, False, False, 0)
+
+ tooltip_window = gtk.Window(gtk.WINDOW_POPUP)
+ tooltip_button = gtk.Label("blaat!")
+ tooltip_window.add(tooltip_button)
+ tooltip_button.show()
+
+ button.set_tooltip_window(tooltip_window)
+ button.connect("query-tooltip", self.query_tooltip_custom_cb)
+ button.props.has_tooltip = True
+
+ # An insensitive button
+ button = gtk.Button("This one is insensitive")
+ button.set_sensitive(False)
+ button.props.tooltip_text = "Insensitive!"
+ box.pack_start(button, False, False, 0)
+
+ # Testcases from Kris without a tree view don't exist
+ tree_view = gtk.TreeView(self.create_model())
+ tree_view.set_size_request(200, 240)
+
+ tree_view.insert_column_with_attributes(0, "Test",
+ gtk.CellRendererText(),
+ text = 0)
+
+ tree_view.props.has_tooltip = True
+ tree_view.connect("query-tooltip", self.query_tooltip_tree_view_cb)
+ tree_view.get_selection().connect("changed",
+ self.selection_changed_cb, tree_view)
+
+ # We cannot get the button on the treeview column directly
+ # so we have to use a ugly hack to get it.
+ column = tree_view.get_column(0)
+ column.set_clickable(True)
+ label = gtk.Label("Test")
+ column.set_widget(label)
+ label.show()
+ button = label.get_parent()
+ button.props.tooltip_text = "Header"
+
+ box.pack_start(tree_view, False, False, 2)
+
+ # And a text view for Matthias
+ buffer = gtk.TextBuffer()
+
+ iter = buffer.get_end_iter()
+ buffer.insert(iter, "Hello, the text ", -1)
+
+ tag = buffer.create_tag("bold")
+ tag.props.weight = pango.WEIGHT_BOLD
+
+ iter = buffer.get_end_iter()
+ buffer.insert_with_tags(iter, "in bold", tag)
+
+ iter = buffer.get_end_iter()
+ buffer.insert(iter, " has a tooltip!", -1)
+
+ text_view = gtk.TextView(buffer)
+ text_view.set_size_request(200, 50)
+
+ text_view.props.has_tooltip = True
+ text_view.connect("query-tooltip", self.query_tooltip_text_view_cb, tag)
+
+ box.pack_start(text_view, False, False, 2)
+
+ # Drawing area
+ drawing_area = gtk.DrawingArea()
+ drawing_area.set_size_request(320, 240)
+ drawing_area.props.has_tooltip = True
+ drawing_area.connect("expose_event", self.drawing_area_expose)
+ drawing_area.connect("query-tooltip",
+ self.query_tooltip_drawing_area_cb)
+ box.pack_start(drawing_area, False, False, 2)
+
+ # Done!
+ self.show_all()
+
+ def query_tooltip_cb(self, widget, x, y, keyboard_tip, tooltip):
+ tooltip.set_markup(widget.get_label())
+ tooltip.set_icon_from_stock(gtk.STOCK_DELETE, gtk.ICON_SIZE_MENU)
+
+ return True
+
+ def query_tooltip_custom_cb(self, widget, x, y, keyboard_tip, tooltip):
+ color = gtk.gdk.Color(0, 65535, 0)
+ window = widget.get_tooltip_window()
+
+ window.modify_bg(gtk.STATE_NORMAL, color)
+
+ return True
+
+ def query_tooltip_text_view_cb(self, widget, x, y,
+ keyboard_tip, tooltip, data):
+ if keyboard_tip:
+ offset= widget.props.buffer.cursor_position
+ iter = widget.props.buffer.get_iter_at_offset(offset)
+ else:
+ coords = widget.window_to_buffer_coords(gtk.TEXT_WINDOW_TEXT, x, y)
+ ret =widget.get_iter_at_position(coords[0], coords[1])
+
+ if ret[0].has_tag(data):
+ tooltip.set_text("Tooltip on text tag")
+ else:
+ return False
+
+ return True
+
+ def query_tooltip_tree_view_cb(self, widget, x, y, keyboard_tip, tooltip):
+ model = widget.get_model()
+
+ if keyboard_tip:
+ # Keyboard mode
+ ret = widget.get_cursor()
+
+ if not ret[0]:
+ return False
+ else:
+ coords = widget.convert_widget_to_bin_window_coords(x, y)
+
+ # Mouse mode
+ tp = widget.get_path_at_pos(coords[0], coords[1])
+ if not tp:
+ return False
+
+ iter = model.get_iter(tp[0])
+ value = model.get(iter, 0)
+
+ tooltip.set_markup("<b>Path %s:</b> %s" %(tp[0][0], value[0]))
+
+ return True
+
+ def query_tooltip_drawing_area_cb(self, widget, x, y, keyboard_tip,
+ tooltip, data=None):
+ if keyboard_tip:
+ return False
+
+ for i in range(len(rects)):
+ if(rects[i]["x"] < x and x < rects[i]["x"] + 50 \
+ and rects[i]["y"] < y and y < rects[i]["y"] + 50):
+ tooltip.set_markup(rects[i]["tooltip"])
+ return True;
+ return False
+
+ def selection_changed_cb(self, selection, tree_view):
+ tree_view.trigger_tooltip_query()
+
+ def create_model(self):
+ store = gtk.TreeStore(gobject.TYPE_STRING);
+
+ # A tree store with some random words ...
+ store.append(None, ("File Manager",))
+ store.append(None, ("Gossip",))
+ store.append(None, ("System Settings",))
+ store.append(None, ("The GIMP",))
+ store.append(None, ("Terminal",))
+ store.append(None, ("Word Processor",))
+
+ return(store)
+
+ def drawing_area_expose(self, drawing_area, event, data=None):
+ cr = drawing_area.window.cairo_create()
+
+ cr.rectangle(0, 0,
+ drawing_area.allocation.width,
+ drawing_area.allocation.height)
+ cr.set_source_rgb(1.0, 1.0, 1.0)
+ cr.fill()
+
+ for i in range(len(rects)):
+ cr.rectangle(rects[i]["x"], rects[i]["y"], 50, 50)
+ cr.set_source_rgb(rects[i]["r"], rects[i]["g"], rects[i]["b"])
+ cr.stroke()
+
+ cr.rectangle(rects[i]["x"], rects[i]["y"], 50, 50)
+ cr.set_source_rgba(rects[i]["r"], rects[i]["g"], rects[i]["b"], 0.5)
+ cr.fill()
+
+ return False
+
+def main():
+ TooltipDemo()
+ gtk.main()
+
+if __name__ == '__main__':
+ main() \ No newline at end of file