Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > e40b0b2b839eccdb3c85993a7b738115 > files > 201

gtkmm-documentation-3.24.0-1.mga7.noarch.rpm

<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 =
  "&lt;interface&gt;"
  "  &lt;menu id='menu-examplepopup'&gt;"
  "    &lt;section&gt;"
  "      &lt;item&gt;"
  "        &lt;attribute name='label' translatable='yes'&gt;Edit&lt;/attribute&gt;"
  "        &lt;attribute name='action'&gt;examplepopup.edit&lt;/attribute&gt;"
  "      &lt;/item&gt;"
  "      &lt;item&gt;"
  "        &lt;attribute name='label' translatable='yes'&gt;Process&lt;/attribute&gt;"
  "        &lt;attribute name='action'&gt;examplepopup.process&lt;/attribute&gt;"
  "      &lt;/item&gt;"
  "      &lt;item&gt;"
  "        &lt;attribute name='label' translatable='yes'&gt;Remove&lt;/attribute&gt;"
  "        &lt;attribute name='action'&gt;examplepopup.remove&lt;/attribute&gt;"
  "      &lt;/item&gt;"
  "    &lt;/section&gt;"
  "  &lt;/menu&gt;"
  "&lt;/interface&gt;";

m_refBuilder-&gt;add_from_string(ui_info);

Glib::RefPtr&lt;Glib::Object&gt; object = m_refBuilder-&gt;get_object("menu-examplepopup");
Glib::RefPtr&lt;Gio::Menu&gt; gmenu = Glib::RefPtr&lt;Gio::Menu&gt;::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-&gt;type == GDK_BUTTON_PRESS) &amp;&amp; (event-&gt;button == 3) )
  {
    if(!m_pMenuPopup-&gt;get_attach_widget())
      m_pMenuPopup-&gt;attach_to_widget(*this);

    m_pMenuPopup-&gt;popup(event-&gt;button, event-&gt;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>