1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
hljs.configure({languages: ['cpp']});
hljs.highlightAll();
</script><title>Popup Menus</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-menus-and-toolbars.html" title="Chapter 15. 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 15. 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="section">
<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">Menu</code>s 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"><code class="code">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);
auto gmenu = m_refBuilder->get_object<Gio::Menu>("menu-examplepopup");
m_MenuPopup.set_menu_model(gmenu);
</code></pre>
<p>
To show the popup menu, use a <code class="classname">Gtk::GestureClick</code>
and connect to its <code class="literal">pressed</code> signal. In the signal handler,
use <code class="classname">Gtk::PopoverMenu</code>'s
<code class="methodname">popup()</code> method. For instance:
</p>
<pre class="programlisting"><code class="code">void ExampleWindow::on_label_pressed(int /* n_press */, double x, double y)
{
const Gdk::Rectangle rect(x, y, 1, 1);
m_MenuPopup.set_pointing_to(rect);
m_MenuPopup.popup();
}</code></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>
|