<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Popup Menus</title> <link rel="stylesheet" type="text/css" href="style.css"> <meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> <link rel="home" href="index.html" title="Programming with gtkmm 3"> <link rel="up" href="chapter-menus-and-toolbars.html" title="Chapter 12. Menus and Toolbars"> <link rel="prev" href="sec-menubar-and-toolbar.html" title="Menubar and Toolbar"> <link rel="next" href="sec-gio-resource.html" title="Gio::Resource and glib-compile-resources"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr><th colspan="3" align="center">Popup Menus</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="sec-menubar-and-toolbar.html"><img src="icons/prev.png" alt="Prev"></a> </td> <th width="60%" align="center">Chapter 12. Menus and Toolbars</th> <td width="20%" align="right"> <a accesskey="n" href="sec-gio-resource.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> </table> <hr> </div> <div class="sect1"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="sec-menus-popup"></a>Popup Menus</h2></div></div></div> <p> <code class="classname">Menus</code> are normally just added to a window, but they can also be displayed temporarily as the result of a mouse button click. For instance, a context menu might be displayed when the user clicks their right mouse button. </p> <p>For instance: </p> <pre class="programlisting"> Glib::ustring ui_info = "<interface>" " <menu id='menu-examplepopup'>" " <section>" " <item>" " <attribute name='label' translatable='yes'>Edit</attribute>" " <attribute name='action'>examplepopup.edit</attribute>" " </item>" " <item>" " <attribute name='label' translatable='yes'>Process</attribute>" " <attribute name='action'>examplepopup.process</attribute>" " </item>" " <item>" " <attribute name='label' translatable='yes'>Remove</attribute>" " <attribute name='action'>examplepopup.remove</attribute>" " </item>" " </section>" " </menu>" "</interface>"; m_refBuilder->add_from_string(ui_info); Glib::RefPtr<Glib::Object> object = m_refBuilder->get_object("menu-examplepopup"); Glib::RefPtr<Gio::Menu> gmenu = Glib::RefPtr<Gio::Menu>::cast_dynamic(object); m_pMenuPopup = new Gtk::Menu(gmenu); </pre> <p> To show the popup menu, use <code class="classname">Gtk::Menu</code>'s <code class="methodname">popup()</code> method, providing the button identifier and the time of activation, as provided by the <code class="literal">button_press_event</code> signal, which you will need to handle anyway. For instance: </p> <pre class="programlisting"> bool ExampleWindow::on_button_press_event(GdkEventButton* event) { if( (event->type == GDK_BUTTON_PRESS) && (event->button == 3) ) { if(!m_pMenuPopup->get_attach_widget()) m_pMenuPopup->attach_to_widget(*this); m_pMenuPopup->popup(event->button, event->time); return true; //It has been handled. } else return false; } </pre> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="sec-menubar-and-toolbar.html"><img src="icons/prev.png" alt="Prev"></a> </td> <td width="20%" align="center"><a accesskey="u" href="chapter-menus-and-toolbars.html"><img src="icons/up.png" alt="Up"></a></td> <td width="40%" align="right"> <a accesskey="n" href="sec-gio-resource.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Menubar and Toolbar </td> <td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td> <td width="40%" align="right" valign="top"> Gio::Resource and glib-compile-resources</td> </tr> </table> </div> </body> </html>