<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Popup Menus</title> <link rel="stylesheet" href="style.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.1"> <link rel="home" href="index.html" title="Programming with gtkmm"> <link rel="up" href="chapter-menus-and-toolbars.html" title="Chapter 11. Menus and Toolbars"> <link rel="prev" href="sec-uimanager.html" title="UIManager"> <link rel="next" href="sec-menus-examples.html" title="Examples"> </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-uimanager.html"><img src="icons/prev.png" alt="Prev"></a> </td> <th width="60%" align="center">Chapter 11. Menus and Toolbars</th> <td width="20%" align="right"> <a accesskey="n" href="sec-menus-examples.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> </table> <hr> </div> <div class="sect1" title="Popup Menus"> <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>The UI layout for a popup menu should use the <code class="literal">popup</code> node. For instance: </p> <pre class="programlisting">Glib::ustring ui_info = "<ui>" " <popup name='PopupMenu'>" " <menuitem action='ContextEdit'/>" " <menuitem action='ContextProcess'/>" " <menuitem action='ContextRemove'/>" " </popup>" "</ui>"; m_refUIManager->add_ui_from_string(ui_info);</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) ) { m_Menu_Popup->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-uimanager.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-menus-examples.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> <tr> <td width="40%" align="left" valign="top">UIManager </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"> Examples</td> </tr> </table> </div> </body> </html>