File: gtk-migrating-entry-icons.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 (292 lines) | stat: -rw-r--r-- 15,609 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Migrating from SexyIconEntry to GtkEntry: 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="gtk-migrating-tooltips.html" title="Migrating from GtkTooltips to GtkTooltip">
<link rel="next" href="gtk-migrating-label-links.html" title="Migrating from SexyUrlLabel to GtkLabel">
<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="gtk-migrating-tooltips.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="gtk-migrating-label-links.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-entry-icons"></a>Migrating from SexyIconEntry to GtkEntry</h2></div></div></div>
<p>
    GTK+ 2.16 supports showing icons inside a <a class="link" href="GtkEntry.html" title="GtkEntry"><span class="type">GtkEntry</span></a>, similar to
    SexyIconEntry. Porting from SexyIconEntry to GtkEntry is relatively
    straightforward. The main difference between the two APIs is that
    SexyIconEntry uses <a class="link" href="GtkImage.html" title="GtkImage"><span class="type">GtkImage</span></a> widgets in a somewhat awkward way as
    storage vehicles for icons, while GtkEntry allows to specify icons
    via pixbufs, stock ids, icon names or <span class="type">GIcons</span>. So, if your code uses
    e.g.:
</p>
<div class="informalexample">
  <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td class="listing_lines" align="right"><pre>1
2</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="n">image</span> <span class="o">=</span> <span class="n">gtk_image_new_from_stock</span> <span class="p">(</span><span class="n">GTK_STOCK_NEW</span><span class="p">,</span> <span class="n">GTK_ICON_SIZE_MENU</span><span class="p">);</span>
<span class="n">sexy_icon_entry_set_icon</span> <span class="p">(</span><span class="n">entry</span><span class="p">,</span> <span class="n">SEXY_ICON_ENTRY_PRIMARY</span><span class="p">,</span> <span class="n">image</span><span class="p">);</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
    you can get rid of the <em class="parameter"><code>image</code></em>, and directly write:
</p>
<div class="informalexample">
  <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td class="listing_lines" align="right"><pre>1</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="n">gtk_entry_set_icon_from_stock</span> <span class="p">(</span><span class="n">entry</span><span class="p">,</span> <span class="n">GTK_ENTRY_ICON_PRIMARY</span><span class="p">,</span> <span class="n">GTK_STOCK_NEW</span><span class="p">);</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
  </p>
<p>
    The signals SexyIconEntry::icon-pressed and SexyIconEntry::icon-released
    have been renamed to <a class="link" href="GtkEntry.html#GtkEntry-icon-press" title="The “icon-press” signal"><span class="type">“icon-press”</span></a> and <a class="link" href="GtkEntry.html#GtkEntry-icon-release" title="The “icon-release” signal"><span class="type">“icon-release”</span></a>
    to avoid problems due to signal name clashes. Also, the signature of the
    signals has changed from
</p>
<div class="informalexample">
  <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td class="listing_lines" align="right"><pre>1
2
3</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="kt">void</span> <span class="p">(</span><span class="o">*</span><span class="n">icon_pressed</span><span class="p">)</span> <span class="p">(</span><span class="n">SexyIconEntry</span>         <span class="o">*</span><span class="n">entry</span><span class="p">,</span> 
                      <span class="n">SexyIconEntryPosition</span>  <span class="n">icon_pos</span><span class="p">,</span>
                      <span class="kt">int</span>                    <span class="n">button</span><span class="p">)</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
to
</p>
<div class="informalexample">
  <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td class="listing_lines" align="right"><pre>1
2
3</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="kt">void</span> <span class="p">(</span><span class="o">*</span><span class="n">icon_press</span><span class="p">)</span> <span class="p">(</span><span class="n">GtkEntry</span>              <span class="o">*</span><span class="n">entry</span><span class="p">,</span> 
                    <span class="n">GtkEntryIconPosition</span>  <span class="n">icon_pos</span><span class="p">,</span>
                    <span class="n">GdkEventButton</span>       <span class="o">*</span><span class="n">event</span><span class="p">)</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
    The new signature has the advantage that the signal handler can use
    the timestamp of the event, e.g. for passing it to <a class="link" href="GtkMenu.html#gtk-menu-popup" title="gtk_menu_popup ()"><code class="function">gtk_menu_popup()</code></a>.
    When adapting an existing signal handler to the new signature, you 
    should note that the button number is easily available as <em class="parameter"><code>event-&gt;button</code></em>,
    as shown in the following example:
</p>
<div class="informalexample">
  <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9
10
11
12</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="k">static</span> <span class="kt">void</span>
<span class="nf">icon_pressed_cb</span> <span class="p">(</span><span class="n">SexyIconEntry</span>         <span class="o">*</span><span class="n">entry</span><span class="p">,</span>
                 <span class="n">SexyIconEntryPosition</span>  <span class="n">position</span><span class="p">,</span>
                 <span class="kt">int</span>                    <span class="n">button</span><span class="p">,</span>
                 <span class="n">gpointer</span>               <span class="n">data</span><span class="p">)</span>
<span class="p">{</span>
  <span class="n">GtkMenu</span> <span class="o">*</span><span class="n">menu</span> <span class="o">=</span> <span class="n">data</span><span class="p">;</span>

  <span class="k">if</span> <span class="p">(</span><span class="n">position</span> <span class="o">==</span> <span class="n">SEXY_ICON_ENTRY_PRIMARY</span><span class="p">)</span>
    <span class="n">gtk_menu_popup</span> <span class="p">(</span><span class="n">GTK_MENU</span> <span class="p">(</span><span class="n">menu</span><span class="p">),</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span>
                    <span class="n">button</span><span class="p">,</span> <span class="n">GDK_CURRENT_TIME</span><span class="p">);</span>
<span class="p">}</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
    can be ported as:
</p>
<div class="informalexample">
  <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9
10
11
12</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="k">static</span> <span class="kt">void</span>
<span class="nf">icon_press_cb</span> <span class="p">(</span><span class="n">GtkEntry</span>             <span class="o">*</span><span class="n">entry</span><span class="p">,</span>
               <span class="n">GtkEntryIconPosition</span>  <span class="n">position</span><span class="p">,</span>
               <span class="n">GdkEventButton</span>       <span class="o">*</span><span class="n">event</span><span class="p">,</span>
               <span class="n">gpointer</span>              <span class="n">data</span><span class="p">)</span>
<span class="p">{</span>
  <span class="n">GtkMenu</span> <span class="o">*</span><span class="n">menu</span> <span class="o">=</span> <span class="n">data</span><span class="p">;</span>

  <span class="k">if</span> <span class="p">(</span><span class="n">position</span> <span class="o">==</span> <span class="n">GTK_ENTRY_ICON_PRIMARY</span><span class="p">)</span>
    <span class="n">gtk_menu_popup</span> <span class="p">(</span><span class="n">GTK_MENU</span> <span class="p">(</span><span class="n">menu</span><span class="p">),</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span>
                    <span class="n">event</span><span class="o">-&gt;</span><span class="n">button</span><span class="p">,</span> <span class="n">event</span><span class="o">-&gt;</span><span class="n">time</span><span class="p">);</span>
<span class="p">}</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
  </p>
<p>
    Another difference is that SexyIconEntry offers manual control of
    the icon prelighting, via <code class="function">sexy_icon_entry_set_icon_highlight()</code>. 
    <a class="link" href="GtkEntry.html" title="GtkEntry"><span class="type">GtkEntry</span></a> prelights automatically when appropriate, depending on 
    whether the icon is activatable and sensitive. You should make
    sure that your icons are properly marked as activatable or nonactivatable
    and sensitive or insensitive:
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>
        Sensitive, but non-activatable icons are
        good for purely informational purposes.
      </p></li>
<li class="listitem"><p>
        Icons should be marked as insensitive if the
        function that they trigger is currently not available.
      </p></li>
</ul></div>
<p>
  </p>
<p>
    GtkEntry has no direct equivalent of the special-purpose function
    <code class="function">sexy_icon_entry_add_clear_button()</code>. If you need this functionality,
    the following code works:
</p>
<div class="informalexample">
  <table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td class="listing_lines" align="right"><pre>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</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="k">static</span> <span class="kt">void</span>
<span class="nf">icon_pressed_cb</span> <span class="p">(</span><span class="n">GtkEntry</span>       <span class="o">*</span><span class="n">entry</span><span class="p">,</span>
                 <span class="n">gint</span>            <span class="n">position</span><span class="p">,</span>
                 <span class="n">GdkEventButton</span> <span class="o">*</span><span class="n">event</span><span class="p">,</span>
                 <span class="n">gpointer</span>        <span class="n">data</span><span class="p">)</span>
<span class="p">{</span>
  <span class="k">if</span> <span class="p">(</span><span class="n">position</span> <span class="o">==</span> <span class="n">GTK_ENTRY_ICON_SECONDARY</span><span class="p">)</span>
    <span class="n">gtk_entry_set_text</span> <span class="p">(</span><span class="n">entry</span><span class="p">,</span> <span class="s">&quot;&quot;</span><span class="p">);</span>
<span class="p">}</span>

<span class="k">static</span> <span class="kt">void</span>
<span class="nf">text_changed_cb</span> <span class="p">(</span><span class="n">GtkEntry</span>   <span class="o">*</span><span class="n">entry</span><span class="p">,</span>
                 <span class="n">GParamSpec</span> <span class="o">*</span><span class="n">pspec</span><span class="p">,</span>
                 <span class="n">GtkWidget</span>  <span class="o">*</span><span class="n">button</span><span class="p">)</span>
<span class="p">{</span>
  <span class="n">gboolean</span> <span class="n">has_text</span><span class="p">;</span>

  <span class="n">has_text</span> <span class="o">=</span> <span class="n">gtk_entry_get_text_length</span> <span class="p">(</span><span class="n">entry</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">;</span>
  <span class="n">gtk_entry_set_icon_sensitive</span> <span class="p">(</span><span class="n">entry</span><span class="p">,</span>
                                <span class="n">GTK_ENTRY_ICON_SECONDARY</span><span class="p">,</span>
                                <span class="n">has_text</span><span class="p">);</span>
<span class="p">}</span>


  <span class="cm">/* ... */</span>
 
  <span class="cm">/* Set up the clear icon */</span>
  <span class="n">gtk_entry_set_icon_from_stock</span> <span class="p">(</span><span class="n">GTK_ENTRY</span> <span class="p">(</span><span class="n">entry</span><span class="p">),</span>
                                 <span class="n">GTK_ENTRY_ICON_SECONDARY</span><span class="p">,</span>
                                 <span class="n">GTK_STOCK_CLEAR</span><span class="p">);</span>
  <span class="n">g_signal_connect</span> <span class="p">(</span><span class="n">entry</span><span class="p">,</span> <span class="s">&quot;icon-press&quot;</span><span class="p">,</span>
                    <span class="n">G_CALLBACK</span> <span class="p">(</span><span class="n">icon_pressed_cb</span><span class="p">),</span> <span class="nb">NULL</span><span class="p">);</span>
  <span class="n">g_signal_connect</span> <span class="p">(</span><span class="n">entry</span><span class="p">,</span> <span class="s">&quot;notify::text&quot;</span><span class="p">,</span>
                    <span class="n">G_CALLBACK</span> <span class="p">(</span><span class="n">text_changed_cb</span><span class="p">),</span> <span class="n">find_button</span><span class="p">);</span>
 
  <span class="cm">/* ... */</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
  </p>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.33.0</div>
</body>
</html>