[gnome-devel-docs] tutorials python: menubutton example and page



commit e38b01a9e410ef12f41341ceac61b0ac48c8fb33
Author: Marta Maria Casetti <mmcasetti gmail com>
Date:   Sun Aug 19 18:58:19 2012 +0100

    tutorials python: menubutton example and page

 platform-demos/C/menubutton.py.page    |   55 ++++++++++++++++++++++++
 platform-demos/C/samples/menubutton.py |   72 ++++++++++++++++++++++++++++++++
 2 files changed, 127 insertions(+), 0 deletions(-)
---
diff --git a/platform-demos/C/menubutton.py.page b/platform-demos/C/menubutton.py.page
new file mode 100644
index 0000000..d03f342
--- /dev/null
+++ b/platform-demos/C/menubutton.py.page
@@ -0,0 +1,55 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<page xmlns="http://projectmallard.org/1.0/";
+      xmlns:xi="http://www.w3.org/2001/XInclude";
+      type="guide" style="task"
+      id="menubutton.py">
+  <info>
+  <title type="text">MenuButton</title>
+    <link type="guide" xref="beginner.py#buttons"/>
+    <revision version="0.1" date="2012-08-19" status="draft"/>
+
+    <credit type="author copyright">
+      <name>Tiffany Antopolski</name>
+      <email>tiffany antopolski gmail com</email>
+      <years>2012</years>
+    </credit>
+
+    <credit type="author copyright">
+      <name>Marta Maria Casetti</name>
+      <email>mmcasetti gmail com</email>
+      <years>2012</years>
+    </credit>
+
+    <desc>A widget that shows a menu when clicked on</desc>
+  </info>
+
+  <title>MenuButton</title>
+  <media type="image" mime="image/png" src="media/menubutton.png"/>
+  <p>The GtkMenuButton widget is used to display a menu when clicked on. This menu can be provided either as a GtkMenu, or an abstract GMenuModel. The GtkMenuButton widget can hold any valid child widget. That is, it can hold almost any other standard GtkWidget. The most commonly used child is the provided GtkArrow.</p>
+
+  <note><p>You need to be running GNOME 3.6 for the MenuButton to work.</p></note>
+
+  <links type="section" />
+    
+  <section id="code">
+  <title>Code used to generate this example</title>
+    <code mime="text/x-csharp" style="numbered"><xi:include href="samples/menubutton.py" parse="text"><xi:fallback/></xi:include></code>
+  </section>
+
+  <section id="methods">
+  <title>Useful methods for a MenuButton widget</title>
+    <p>The positioning of the menu is determined by the "direction" property of the menu button and the "halign" or "valign" properties of the menu. For example, when the direction is <code>Gtk.ArrowType.DOWN</code> (other option: <code>UP</code>) and the horizontal alignment is <code>Gtk.Align.START</code> (other options: <code>CENTER</code> and <code>END</code>), the menu will be positioned below the button, with the starting edge (depending on the text direction) of the menu aligned with the starting edge of the button. If there is not enough space below the button, the menu is popped up above the button instead. If the alignment would move part of the menu offscreen, it is 'pushed in'.</p>
+    
+    <p>In the case of vertical alignment, the possible ArrowType directions are <code>LEFT</code> and <code>RIGHT</code> and the vertical alignment is again <code>START</code>, <code>CENTER</code> or <code>END</code>.</p>
+    
+    <p><code>set_align_widget(alignment)</code> and <code>set_direction(direction)</code> can be used to set these properties.</p>
+  </section>
+  
+  <section id="references">
+  <title>API References</title>
+    <p>In this sample we used the following:</p>
+    <list>
+      <item><p><link href="http://developer.gnome.org/gtk3/unstable/GtkMenuButton.html";>MenuButton</link></p></item>
+    </list>
+  </section>
+</page>
diff --git a/platform-demos/C/samples/menubutton.py b/platform-demos/C/samples/menubutton.py
new file mode 100644
index 0000000..63fbbfe
--- /dev/null
+++ b/platform-demos/C/samples/menubutton.py
@@ -0,0 +1,72 @@
+from gi.repository import Gtk
+from gi.repository import Gio
+import sys
+
+class MyWindow(Gtk.ApplicationWindow):
+    def __init__(self, app):
+        Gtk.Window.__init__(self, title="Menubutton Example", application=app)
+        self.set_default_size(600, 400)
+
+        grid = Gtk.Grid()
+        
+        # a menubutton
+        menubutton = Gtk.MenuButton()
+        menubutton.set_size_request(80, 35)
+        
+        grid.attach(menubutton, 0, 0, 1, 1)
+        
+        # a menu with two actions
+        menumodel = Gio.Menu()
+        menumodel.append("New", "app.new")
+        menumodel.append("About", "win.about")
+
+        # a submenu with one action for the menu
+        submenu = Gio.Menu()
+        submenu.append("Quit", "app.quit")
+        menumodel.append_submenu("Other", submenu)
+
+        # the menu is set as the menu of the menubutton
+        menubutton.set_menu_model(menumodel)
+
+        # the action related to the window (about)
+        about_action = Gio.SimpleAction.new("about", None)
+        about_action.connect("activate", self.about_callback)
+        self.add_action(about_action)
+        
+        self.add(grid)
+
+    # callback for "about"
+    def about_callback(self, action, parameter):
+        print "You clicked \"About\""
+
+class MyApplication(Gtk.Application):
+    def __init__(self):
+        Gtk.Application.__init__(self)
+
+    def do_activate(self):
+        win = MyWindow(self)
+        win.show_all()
+
+    def do_startup(self):
+        Gtk.Application.do_startup(self)
+
+        #  the actions related to the application
+        new_action = Gio.SimpleAction.new("new", None)
+        new_action.connect("activate", self.new_callback)
+        self.add_action(new_action)
+        
+        quit_action = Gio.SimpleAction.new("quit", None)
+        quit_action.connect("activate", self.quit_callback)
+        self.add_action(quit_action)
+    
+    # callback functions for the actions related to the application
+    def new_callback(self, action, parameter):
+        print "You clicked \"New\""
+
+    def quit_callback(self, action, parameter):
+        print "You clicked \"Quit\""
+        self.quit()
+
+app = MyApplication()
+exit_status = app.run(sys.argv)
+sys.exit(exit_status)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]