File: gtk-migrating-checklist.html

package info (click to toggle)
gtk%2B2.0 2.24.33-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie, trixie-proposed-updates, trixie-updates
  • size: 124,860 kB
  • sloc: ansic: 574,091; makefile: 5,171; sh: 4,618; xml: 1,193; python: 1,117; perl: 749; awk: 49; cpp: 34
file content (156 lines) | stat: -rw-r--r-- 7,495 bytes parent folder | download | duplicates (3)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Migration Checklist: GTK+ 2 Reference Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="GTK+ 2 Reference Manual">
<link rel="up" href="migrating.html" title="Part IV. Migrating from Previous Versions of GTK+">
<link rel="prev" href="migrating.html" title="Part IV. Migrating from Previous Versions of GTK+">
<link rel="next" href="checklist-gdkeventexpose-region.html" title="Use GdkEventExpose.region">
<meta name="generator" content="GTK-Doc V1.33.0 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="migrating.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="migrating.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="checklist-gdkeventexpose-region.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="chapter">
<div class="titlepage"><div><div><h2 class="title">
<a name="gtk-migrating-checklist"></a>Migration Checklist</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="gtk-migrating-checklist.html#checklist-popup-menu">Implement GtkWidget::popup_menu</a></span></dt>
<dt><span class="section"><a href="checklist-gdkeventexpose-region.html">Use GdkEventExpose.region</a></span></dt>
<dt><span class="section"><a href="checklist-modifiers.html">Test for modifier keys correctly</a></span></dt>
<dt><span class="section"><a href="checklist-named-icons.html">Use named icons</a></span></dt>
</dl></div>
<p>
    This chapter includes a checklist of things you need to do to
    ensure that your programs are good citizens in the GTK+ world.  By
    paying attention to the points in the checklist, you ensure that
    many automatic features of GTK+ will work correctly in your
    program.
  </p>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="checklist-popup-menu"></a>Implement GtkWidget::popup_menu</h2></div></div></div>
<p><b>Why. </b>
	By handling this signal, you let widgets have
	context-sensitive menus that can be invoked with the standard
	key bindings.
      </p>
<p>
      The <a class="link" href="GtkWidget.html#GtkWidget-popup-menu" title="The “popup-menu” signal"><span class="type">“popup-menu”</span></a> signal instructs the widget for which 
      it is emitted to create a context-sensitive popup menu. By default, 
      the key binding mechanism is set to
      emit this signal when the
      <span class="keycap"><strong>Shift</strong></span>+<span class="keycap"><strong>F10</strong></span>
      or <span class="keycap"><strong>Menu</strong></span> keys are pressed while a widget has the
      focus.  If a widget in your application shows a popup menu when
      you press a mouse button, you can make it work as well through
      the normal key binding mechanism in the following fahion:
    </p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
<p>
	  Write a function to create and show a popup menu.  This
	  function needs to know the button number and the event's
	  time to pass them to <a class="link" href="GtkMenu.html#gtk-menu-popup" title="gtk_menu_popup ()"><code class="function">gtk_menu_popup()</code></a>.  You can implement
	  such a function like this:
	</p>
<a name="do_popup_menu"></a><pre class="programlisting">
static void
do_popup_menu (GtkWidget *my_widget, GdkEventButton *event)
{
  GtkWidget *menu;
  int button, event_time;

  menu = gtk_menu_new ();
  g_signal_connect (menu, "deactivate", 
                    G_CALLBACK (gtk_widget_destroy), NULL);

  /* ... add menu items ... */

  if (event)
    {
      button = event-&gt;button;
      event_time = event-&gt;time;
    }
  else
    {
      button = 0;
      event_time = gtk_get_current_event_time ();
    }

  gtk_menu_attach_to_widget (GTK_MENU (menu), my_widget, NULL);
  gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, 
                  button, event_time);
}
	</pre>
</li>
<li class="listitem">
<p>
	  In your <a class="link" href="GtkWidget.html#GtkWidget-button-press-event" title="The “button-press-event” signal"><span class="type">“button-press-event”</span></a> handler, call this function 
          when you need to pop up a menu:
	</p>
<pre class="programlisting">
static gboolean
my_widget_button_press_event_handler (GtkWidget *widget, GdkEventButton *event)
{
  /* Ignore double-clicks and triple-clicks */
  if (event-&gt;button == 3 &amp;&amp; event-&gt;type == GDK_BUTTON_PRESS)
    {
      do_popup_menu (widget, event);
      return TRUE;
    }

  return FALSE;
}
	</pre>
</li>
<li class="listitem">
<p>
	  Implement a handler for the <a class="link" href="GtkWidget.html#GtkWidget-popup-menu" title="The “popup-menu” signal"><span class="type">“popup-menu”</span></a> signal:
	</p>
<pre class="programlisting">
static gboolean
my_widget_popup_menu_handler (GtkWidget *widget)
{
  do_popup_menu (widget, NULL);
  return TRUE;
}
	</pre>
</li>
</ol></div>
<div class="note"><p>
	If you do not pass a positioning function to <a class="link" href="GtkMenu.html#gtk-menu-popup" title="gtk_menu_popup ()"><code class="function">gtk_menu_popup()</code></a>,
	it will show the menu at the mouse position by default.  This
	is what you usually want when the menu is shown as a result of
	pressing a mouse button.  However, if you press the
	<span class="keycap"><strong>Shift</strong></span>+<span class="keycap"><strong>F10</strong></span>
	or <span class="keycap"><strong>Menu</strong></span> keys while the widget is focused, the
	mouse cursor may not be near the widget at all.  In the <a class="link" href="gtk-migrating-checklist.html#do_popup_menu">example above</a>, you may want to
	provide your own <a class="link" href="GtkMenu.html#GtkMenuPositionFunc" title="GtkMenuPositionFunc ()">menu-positioning function</a>
	in the case where the <em class="parameter"><code>event</code></em> is
	<code class="literal">NULL</code>.  This function should compute the desired position for 
        a menu when it is invoked through the keyboard.  For example, 
        <a class="link" href="GtkEntry.html" title="GtkEntry"><span class="type">GtkEntry</span></a> aligns the top edge of its popup menu with the bottom 
        edge of the entry.
      </p></div>
<div class="note"><p>
	For the standard key bindings to work, your widget must be
	able to take the keyboard focus.  In general, widgets should
	be fully usable through the keyboard and not just the mouse.
	The very first step of this is to ensure that your widget
	turns on the <a class="link" href="GtkWidget.html#GTK-CAN-FOCUS:CAPS"><code class="literal">GTK_CAN_FOCUS</code></a> flag.
      </p></div>
</div>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.33.0</div>
</body>
</html>