File: gtk-migrating-2-to-3.html

package info (click to toggle)
gtk%2B3.0 3.22.11-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 182,828 kB
  • ctags: 82,378
  • sloc: ansic: 1,165,043; xml: 9,259; makefile: 6,914; sh: 5,179; python: 402; perl: 370; cpp: 34; sed: 16
file content (764 lines) | stat: -rw-r--r-- 43,144 bytes parent folder | download
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
<!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 GTK+ 2.x to GTK+ 3: GTK+ 3 Reference Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="GTK+ 3 Reference Manual">
<link rel="up" href="migrating.html" title="Part V. Migrating from Previous Versions of GTK+">
<link rel="prev" href="migrating.html" title="Part V. Migrating from Previous Versions of GTK+">
<link rel="next" href="ch26s02.html" title="Changes that need to be done at the time of the switch">
<meta name="generator" content="GTK-Doc V1.25.1 (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="ch26s02.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-2-to-3"></a>Migrating from GTK+ 2.x to GTK+ 3</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="gtk-migrating-2-to-3.html#id-1.6.3.3">Preparation in GTK+ 2.x</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="gtk-migrating-2-to-3.html#id-1.6.3.3.3">Do not include individual headers</a></span></dt>
<dt><span class="section"><a href="gtk-migrating-2-to-3.html#id-1.6.3.3.4">Do not use deprecated symbols</a></span></dt>
<dt><span class="section"><a href="gtk-migrating-2-to-3.html#id-1.6.3.3.5">Use accessor functions instead of direct access</a></span></dt>
<dt><span class="section"><a href="gtk-migrating-2-to-3.html#id-1.6.3.3.6">Replace GDK_&lt;keyname&gt; with GDK_KEY_&lt;keyname&gt;</a></span></dt>
<dt><span class="section"><a href="gtk-migrating-2-to-3.html#id-1.6.3.3.7">Use GIO for launching applications</a></span></dt>
<dt><span class="section"><a href="gtk-migrating-2-to-3.html#id-1.6.3.3.8">Use cairo for drawing</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="ch26s02.html">Changes that need to be done at the time of the switch</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.3">Replace size_request by get_preferred_width/height</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.4">Replace GdkRegion by cairo_region_t</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.5">Replace GdkPixmap by cairo surfaces</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.6">Replace GdkColormap by GdkVisual</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.7">GdkDrawable is gone</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.8">Event filtering</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.9">Backend-specific code</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.10">GtkPlug and GtkSocket</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.11">The GtkWidget::draw signal</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.12">GtkProgressBar orientation</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.13">Check your expand and fill flags</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.14">Scrolling changes</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.15">GtkObject is gone</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.16">GtkEntryCompletion signal parameters</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.17">Resize grips</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.18">Prevent mixed linkage</a></span></dt>
<dt><span class="section"><a href="ch26s02.html#id-1.6.3.4.19">Install GTK+ modules in the right place</a></span></dt>
</dl></dd>
</dl></div>
<p>
    GTK+ 3 is a major new version of GTK+ that breaks both API and ABI
    compared to GTK+ 2.x, which has remained API- and ABI-stable for a
    long time. Thankfully, most of the changes are not hard to adapt to
    and there are a number of steps that you can take to prepare your
    GTK+ 2.x application for the switch to GTK+ 3. After that, there's
    a small number of adjustments that you may have to do when you actually
    switch your application to build against GTK+ 3.
  </p>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="id-1.6.3.3"></a>Preparation in GTK+ 2.x</h2></div></div></div>
<p>
      The steps outlined in the following sections assume that your
      application is working with GTK+ 2.24, which is the final stable
      release of GTK+ 2.x. It includes all the necessary APIs and tools
      to help you port your application to GTK+ 3. If you are still using
      an older version of GTK+ 2.x, you should first get your application
      to build and work with 2.24.
    </p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="id-1.6.3.3.3"></a>Do not include individual headers</h3></div></div></div>
<p>
    With GTK+ 2.x it was common to include just the header files for
    a few widgets that your application was using, which could lead
    to problems with missing definitions, etc. GTK+ 3 tightens the
    rules about which header files you are allowed to include directly.
    The allowed header files are are
    </p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term"><code class="filename">gtk/gtk.h</code></span></p></td>
<td>for GTK</td>
</tr>
<tr>
<td><p><span class="term"><code class="filename">gtk/gtkx.h</code></span></p></td>
<td>for the X-specfic widgets <a class="link" href="GtkSocket.html" title="GtkSocket"><span class="type">GtkSocket</span></a> and <a class="link" href="GtkPlug.html" title="GtkPlug"><span class="type">GtkPlug</span></a>
</td>
</tr>
<tr>
<td><p><span class="term"><code class="filename">gtk/gtkunixprint.h</code></span></p></td>
<td>for low-level, UNIX-specific printing functions</td>
</tr>
<tr>
<td><p><span class="term"><code class="filename">gdk/gdk.h</code></span></p></td>
<td>for GDK</td>
</tr>
<tr>
<td><p><span class="term"><code class="filename">gdk/gdkx.h</code></span></p></td>
<td>for GDK functions that are X11-specific</td>
</tr>
<tr>
<td><p><span class="term"><code class="filename">gdk/gdkwin32.h</code></span></p></td>
<td>for GDK functions that are Windows-specific</td>
</tr>
</tbody>
</table></div>
<p>
    (these relative paths are assuming that you are using the include
     paths that are specified in the gtk+-2.0.pc file, as returned by
     <code class="literal">pkg-config --cflags gtk+-2.0.pc</code>.)
  </p>
<p>
    To check that your application only includes the allowed headers,
    you can use defines to disable inclusion of individual headers,
    as follows:
    </p>
<pre class="programlisting">
    make CFLAGS+="-DGTK_DISABLE_SINGLE_INCLUDES"
    </pre>
<p>
  </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="id-1.6.3.3.4"></a>Do not use deprecated symbols</h3></div></div></div>
<p>
    Over the years, a number of functions, and in some cases, entire
    widgets have been deprecated. These deprecations are clearly spelled
    out in the API reference, with hints about the recommended replacements.
    The API reference for GTK+ 2 also includes an
    <a class="ulink" href="https://developer.gnome.org/gtk2/2.24/api-index-deprecated.html" target="_top">index</a> of all deprecated symbols.
  </p>
<p>
    To verify that your program does not use any deprecated symbols,
    you can use defines to remove deprecated symbols from the header files,
    as follows:
    </p>
<pre class="programlisting">
    make CFLAGS+="-DGDK_DISABLE_DEPRECATED -DGTK_DISABLE_DEPRECATED"
   </pre>
<p>
  </p>
<p>
    Note that some parts of our API, such as enumeration values, are
    not well covered by the deprecation warnings. In most cases, using
    them will require you to also use deprecated functions, which will
    trigger warnings. But some things, like the <code class="literal">GTK_DIALOG_NO_SEPARATOR</code>
    flag that has disappeared in GTK+ 3, may not.
  </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="id-1.6.3.3.5"></a>Use accessor functions instead of direct access</h3></div></div></div>
<p>
    GTK+ 3 removes many implementation details and struct members from
    its public headers.
  </p>
<p>
    To ensure that your application does not have problems with this, you
    define the preprocessor symbol <code class="literal">GSEAL_ENABLE</code> while
    building your application against GTK+ 2.x. This will make the compiler
    catch all uses of direct access to struct fields so that you can go
    through them one by one and replace them with a call to an accessor
    function instead.
    </p>
<pre class="programlisting">
    make CFLAGS+="-DGSEAL_ENABLE"
    </pre>
<p>
  </p>
<p>
    While it may be painful to convert, this helps us keep API and ABI
    compatibility when we change internal interfaces. As a quick example,
    when adding GSEAL_ENABLE, if you see an error like:
    </p>
<pre class="programlisting">
    error: 'GtkToggleButton' has no member named 'active'
    </pre>
<p>
    this means that you are accessing the public structure of
    GtkToggleButton directly, perhaps with some code like:
    </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</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="gtkdoc kwb">static void</span>
<span class="function">on_toggled</span> <span class="gtkdoc opt">(</span>GtkToggleButton <span class="gtkdoc opt">*</span>button<span class="gtkdoc opt">)</span>
<span class="gtkdoc opt">{</span>
  <span class="keyword">if</span> <span class="gtkdoc opt">(</span>button<span class="gtkdoc opt">-&gt;</span>active<span class="gtkdoc opt">)</span>
    <span class="function">frob_active</span> <span class="gtkdoc opt">();</span>
  <span class="keyword">else</span>
    <span class="function">frob_inactive</span> <span class="gtkdoc opt">();</span>
<span class="gtkdoc opt">}</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
  </p>
<p>
    In most cases, this can easily be replaced with the correct accessor
    method. The main rule is that if you have code like the above which
    accesses the "active" field of a "GtkToggleButton", then the accessor
    method becomes "gtk_toggle_button_get_active":
    </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</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="gtkdoc kwb">static void</span>
<span class="function">on_toggled</span> <span class="gtkdoc opt">(</span>GtkToggleButton <span class="gtkdoc opt">*</span>button<span class="gtkdoc opt">)</span>
<span class="gtkdoc opt">{</span>
  <span class="keyword">if</span> <span class="gtkdoc opt">(</span><span class="function"><a href="GtkToggleButton.html#gtk-toggle-button-get-active">gtk_toggle_button_get_active</a></span> <span class="gtkdoc opt">(</span>button<span class="gtkdoc opt">))</span>
    <span class="function">frob_active</span> <span class="gtkdoc opt">();</span>
  <span class="keyword">else</span>
    <span class="function">frob_inactive</span> <span class="gtkdoc opt">();</span>
<span class="gtkdoc opt">}</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
  </p>
<p>
    In the case of setting field members directly, there's usually
    a corresponding setter method.
  </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="id-1.6.3.3.6"></a>Replace GDK_&lt;keyname&gt; with GDK_KEY_&lt;keyname&gt;</h3></div></div></div>
<p>
      Key constants have gained a <code class="literal">_KEY_</code> infix.
      For example, <code class="literal">GDK_a</code> is now
      <code class="literal">GDK_KEY_a</code>.  In GTK+ 2, the old names continue
      to be available.  In GTK+ 3 however, the old names will require
      an explicit include of the <code class="literal">gdkkeysyms-compat.h</code> header.
    </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="id-1.6.3.3.7"></a>Use GIO for launching applications</h3></div></div></div>
<p>
      The <code class="literal">gdk_spawn</code> family of functions has been
      deprecated in GDK 2.24 and removed from GDK 3. Various replacements
      exist; the best replacement depends on the circumstances:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">If you are opening a document or URI by launching a command
        like <code class="literal">firefox http://my-favourite-website.com</code> or
        <code class="literal">gnome-open ghelp:epiphany</code>, it is best to just use
        <a class="link" href="gtk3-Filesystem-utilities.html#gtk-show-uri-on-window" title="gtk_show_uri_on_window ()"><code class="function">gtk_show_uri_on_window()</code></a>; as an added benefit, your application will
        henceforth respect the users preference for what application to use and
        correctly open links in sandboxed applications.</li>
<li class="listitem">If you are launching a regular, installed application that
        has a desktop file, it is best to use GIOs <a href="https://developer.gnome.org/gio/unstable/GAppInfo.html#GAppInfo-struct"><span class="type">GAppInfo</span></a> with a suitable
        launch context.
        <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</pre></td>
        <td class="listing_code"><pre class="programlisting">GAppInfo <span class="gtkdoc opt">*</span>info<span class="gtkdoc opt">;</span>
GAppLaunchContext <span class="gtkdoc opt">*</span>context<span class="gtkdoc opt">;</span>
GError <span class="gtkdoc opt">*</span>error <span class="gtkdoc opt">=</span> NULL<span class="gtkdoc opt">;</span>

info <span class="gtkdoc opt">= (</span>GAppInfo<span class="gtkdoc opt">*)</span> <span class="function"><a href="https://developer.gnome.org/gio/unstable/gio-Desktop-file-based-GAppInfo.html#g-desktop-app-info-new">g_desktop_app_info_new</a></span> <span class="gtkdoc opt">(</span><span class="string">&quot;epiphany.desktop&quot;</span><span class="gtkdoc opt">);</span>
context <span class="gtkdoc opt">= (</span>GAppLaunchContext<span class="gtkdoc opt">*)</span> <span class="function"><a href="http://developer.gnome.org/gdk3/GdkDisplay.html#gdk-display-get-app-launch-context">gdk_display_get_app_launch_context</a></span> <span class="gtkdoc opt">(</span>display<span class="gtkdoc opt">);</span>
<span class="function"><a href="https://developer.gnome.org/gio/unstable/GAppInfo.html#g-app-info-launch">g_app_info_launch</a></span> <span class="gtkdoc opt">(</span>info<span class="gtkdoc opt">,</span> NULL<span class="gtkdoc opt">,</span> context<span class="gtkdoc opt">, &amp;</span>error<span class="gtkdoc opt">);</span>

<span class="keyword">if</span> <span class="gtkdoc opt">(</span>error<span class="gtkdoc opt">)</span>
  <span class="gtkdoc opt">{</span>
    <span class="function"><a href="https://developer.gnome.org/glib/unstable/glib-Message-Logging.html#g-warning">g_warning</a></span> <span class="gtkdoc opt">(</span><span class="string">&quot;Failed to launch epiphany: %s&quot;</span><span class="gtkdoc opt">,</span> error<span class="gtkdoc opt">-&gt;</span>message<span class="gtkdoc opt">);</span>
    <span class="function"><a href="https://developer.gnome.org/glib/unstable/glib-Error-Reporting.html#g-error-free">g_error_free</a></span> <span class="gtkdoc opt">(</span>error<span class="gtkdoc opt">);</span>
  <span class="gtkdoc opt">}</span>

<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-unref">g_object_unref</a></span> <span class="gtkdoc opt">(</span>info<span class="gtkdoc opt">);</span>
<span class="function"><a href="https://developer.gnome.org/gobject/unstable/gobject-The-Base-Object-Type.html#g-object-unref">g_object_unref</a></span> <span class="gtkdoc opt">(</span>context<span class="gtkdoc opt">);</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

        Remember that you have to include
        <code class="filename">gio/gdesktopappinfo.h</code>
        and use the <code class="filename">gio-unix-2.0</code> pkg-config file
        when using <a href="https://developer.gnome.org/gio/unstable/gio-Desktop-file-based-GAppInfo.html#g-desktop-app-info-new"><code class="function">g_desktop_app_info_new()</code></a>.
        </li>
<li class="listitem">If you are launching a custom commandline, you can
        still use <a href="https://developer.gnome.org/gio/unstable/GAppInfo.html#g-app-info-launch"><code class="function">g_app_info_launch()</code></a> with a GAppInfo that is constructed
        with <a href="https://developer.gnome.org/gio/unstable/GAppInfo.html#g-app-info-create-from-commandline"><code class="function">g_app_info_create_from_commandline()</code></a>, or you can use the
        more lowlevel <code class="literal">g_spawn</code> family of functions
        (e.g. <a href="https://developer.gnome.org/glib/unstable/glib-Spawning-Processes.html#g-spawn-command-line-async"><code class="function">g_spawn_command_line_async()</code></a>), and pass <code class="envar">DISPLAY</code>
        in the environment. <code class="function">gdk_screen_make_display_name()</code> can be
        used to find the right value for the <code class="envar">DISPLAY</code>
        environment variable.
        </li>
</ul></div>
<p>
    </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="id-1.6.3.3.8"></a>Use cairo for drawing</h3></div></div></div>
<p>
      In GTK+ 3, the GDK drawing API (which closely mimics the X
      drawing API, which is itself modeled after PostScript) has been
      removed. All drawing in GTK+ 3 is done via cairo.
    </p>
<p>
      The <span class="type">GdkGC</span> and <span class="type">GdkImage</span> objects, as well as all the functions using
      them, are gone. This includes the <code class="literal">gdk_draw</code> family
      of functions like <code class="function">gdk_draw_rectangle()</code> and <code class="function">gdk_draw_drawable()</code>. As
      <span class="type">GdkGC</span> is roughly equivalent to <span class="type">cairo_t</span> and <span class="type">GdkImage</span> was used for
      drawing images to GdkWindows, which cairo supports automatically,
      a transition is usually straightforward.
    </p>
<p>
      The following examples show a few common drawing idioms used by
      applications that have been ported to use cairo and how the code
      was replaced.
    </p>
<div class="example">
<a name="id-1.6.3.3.8.5"></a><p class="title"><b>Example 38. Drawing a GdkPixbuf onto a GdkWindow</b></p>
<div class="example-contents">
<p>
           Drawing a pixbuf onto a drawable used to be done like this:
           </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</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="function">gdk_draw_pixbuf</span> <span class="gtkdoc opt">(</span>window<span class="gtkdoc opt">,</span>
                 <span class="function"><a href="GtkWidget.html#gtk-widget-get-style">gtk_widget_get_style</a></span> <span class="gtkdoc opt">(</span>widget<span class="gtkdoc opt">)-&gt;</span>black_gc<span class="gtkdoc opt">,</span>
                 pixbuf<span class="gtkdoc opt">,</span>
                 <span class="number">0</span><span class="gtkdoc opt">,</span> <span class="number">0</span>
                 x<span class="gtkdoc opt">,</span> y<span class="gtkdoc opt">,</span>
                 <span class="function"><a href="http://library.gnome.org/devel/gdk-pixbuf/unstable/gdk-pixbuf-The-GdkPixbuf-Structure.html#gdk-pixbuf-get-width">gdk_pixbuf_get_width</a></span> <span class="gtkdoc opt">(</span>pixbuf<span class="gtkdoc opt">),</span>
                 <span class="function"><a href="http://library.gnome.org/devel/gdk-pixbuf/unstable/gdk-pixbuf-The-GdkPixbuf-Structure.html#gdk-pixbuf-get-height">gdk_pixbuf_get_height</a></span> <span class="gtkdoc opt">(</span>pixbuf<span class="gtkdoc opt">),</span>
                 GDK_RGB_DITHER_NORMAL<span class="gtkdoc opt">,</span>
                 <span class="number">0</span><span class="gtkdoc opt">,</span> <span class="number">0</span><span class="gtkdoc opt">);</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
           Doing the same thing with cairo:
           </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</pre></td>
        <td class="listing_code"><pre class="programlisting">cairo_t <span class="gtkdoc opt">*</span>cr <span class="gtkdoc opt">=</span> <span class="function">gdk_cairo_create</span> <span class="gtkdoc opt">(</span>window<span class="gtkdoc opt">);</span>
<span class="function"><a href="http://developer.gnome.org/gdk3/gdk4-Cairo-Interaction.html#gdk-cairo-set-source-pixbuf">gdk_cairo_set_source_pixbuf</a></span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">,</span> pixbuf<span class="gtkdoc opt">,</span> x<span class="gtkdoc opt">,</span> y<span class="gtkdoc opt">);</span>
<span class="function">cairo_paint</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">);</span>
<span class="function">cairo_destroy</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">);</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
           Note that very similar code can be used when porting code
           using GdkPixmap to <span class="type">cairo_surface_t</span> by calling
           <code class="function">cairo_set_source_surface()</code> instead of
           <a href="http://developer.gnome.org/gdk3/gdk4-Cairo-Interaction.html#gdk-cairo-set-source-pixbuf"><code class="function">gdk_cairo_set_source_pixbuf()</code></a>.
         </p>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id-1.6.3.3.8.6"></a><p class="title"><b>Example 39. Drawing a tiled GdkPixmap to a GdkWindow</b></p>
<div class="example-contents">
<p>
          Tiled pixmaps are often used for drawing backgrounds.
          Old code looked something like this:
          </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</pre></td>
        <td class="listing_code"><pre class="programlisting">GdkGCValues gc_values<span class="gtkdoc opt">;</span>
GdkGC <span class="gtkdoc opt">*</span>gc<span class="gtkdoc opt">;</span>

<span class="comment">/* setup */</span>
gc <span class="gtkdoc opt">=</span> <span class="function"><a href="GtkWidget.html#gtk-widget-get-style">gtk_widget_get_style</a></span> <span class="gtkdoc opt">(</span>widget<span class="gtkdoc opt">)-&gt;</span>black_gc<span class="gtkdoc opt">;</span>
<span class="function">gdk_gc_set_tile</span> <span class="gtkdoc opt">(</span>gc<span class="gtkdoc opt">,</span> pixmap<span class="gtkdoc opt">);</span>
<span class="function">gdk_gc_set_fill</span> <span class="gtkdoc opt">(</span>gc<span class="gtkdoc opt">,</span> GDK_TILED<span class="gtkdoc opt">);</span>
<span class="function">gdk_gc_set_ts_origin</span> <span class="gtkdoc opt">(</span>gc<span class="gtkdoc opt">,</span> x_origin<span class="gtkdoc opt">,</span> y_origin<span class="gtkdoc opt">);</span>
<span class="comment">/* use */</span>
<span class="function">gdk_draw_rectangle</span> <span class="gtkdoc opt">(</span>window<span class="gtkdoc opt">,</span> gc<span class="gtkdoc opt">,</span> TRUE<span class="gtkdoc opt">,</span> <span class="number">0</span><span class="gtkdoc opt">,</span> <span class="number">0</span><span class="gtkdoc opt">,</span> width<span class="gtkdoc opt">,</span> height<span class="gtkdoc opt">);</span>
<span class="comment">/* restore */</span>
<span class="function">gdk_gc_set_tile</span> <span class="gtkdoc opt">(</span>gc<span class="gtkdoc opt">,</span> NULL<span class="gtkdoc opt">);</span>
<span class="function">gdk_gc_set_fill</span> <span class="gtkdoc opt">(</span>gc<span class="gtkdoc opt">,</span> GDK_SOLID<span class="gtkdoc opt">);</span>
<span class="function">gdk_gc_set_ts_origin</span> <span class="gtkdoc opt">(</span>gc<span class="gtkdoc opt">,</span> <span class="number">0</span><span class="gtkdoc opt">,</span> <span class="number">0</span><span class="gtkdoc opt">);</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
          The equivalent cairo code to draw a tiled surface looks
          like this:
          </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</pre></td>
        <td class="listing_code"><pre class="programlisting">cairo_t <span class="gtkdoc opt">*</span>cr<span class="gtkdoc opt">;</span>
cairo_surface_t <span class="gtkdoc opt">*</span>surface<span class="gtkdoc opt">;</span>

surface <span class="gtkdoc opt">= ...</span>
cr <span class="gtkdoc opt">=</span> <span class="function">gdk_cairo_create</span> <span class="gtkdoc opt">(</span>window<span class="gtkdoc opt">);</span>
<span class="function">cairo_set_source_surface</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">,</span> surface<span class="gtkdoc opt">,</span> x_origin<span class="gtkdoc opt">,</span> y_origin<span class="gtkdoc opt">);</span>
<span class="function">cairo_pattern_set_extend</span> <span class="gtkdoc opt">(</span><span class="function">cairo_get_source</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">),</span> CAIRO_EXTEND_REPEAT<span class="gtkdoc opt">);</span>
<span class="function">cairo_rectangle</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">,</span> <span class="number">0</span><span class="gtkdoc opt">,</span> <span class="number">0</span><span class="gtkdoc opt">,</span> width<span class="gtkdoc opt">,</span> height<span class="gtkdoc opt">);</span>
<span class="function">cairo_fill</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">);</span>
<span class="function">cairo_destroy</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">);</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
The surface here can be either an image surface or a X surface,
and can either be created on the spot or kept around for caching purposes.
Another alternative is to use pixbufs instead of surfaces with
<a href="http://developer.gnome.org/gdk3/gdk4-Cairo-Interaction.html#gdk-cairo-set-source-pixbuf"><code class="function">gdk_cairo_set_source_pixbuf()</code></a> instead of <code class="function">cairo_set_source_surface()</code>.
        </p>
</div>
</div>
<br class="example-break"><div class="example">
<a name="id-1.6.3.3.8.7"></a><p class="title"><b>Example 40. Drawing a PangoLayout to a clipped area</b></p>
<div class="example-contents">
<p>
          Drawing layouts clipped is often used to avoid overdraw or to
          allow drawing selections. Code would have looked like this:
          </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</pre></td>
        <td class="listing_code"><pre class="programlisting">GdkGC <span class="gtkdoc opt">*</span>gc<span class="gtkdoc opt">;</span>

<span class="comment">/* setup */</span>
gc <span class="gtkdoc opt">=</span> <span class="function"><a href="GtkWidget.html#gtk-widget-get-style">gtk_widget_get_style</a></span> <span class="gtkdoc opt">(</span>widget<span class="gtkdoc opt">)-&gt;</span>text_gc<span class="gtkdoc opt">[</span>state<span class="gtkdoc opt">];</span>
<span class="function">gdk_gc_set_clip_rectangle</span> <span class="gtkdoc opt">(</span>gc<span class="gtkdoc opt">, &amp;</span>area<span class="gtkdoc opt">);</span>
<span class="comment">/* use */</span>
<span class="function">gdk_draw_layout</span> <span class="gtkdoc opt">(</span>drawable<span class="gtkdoc opt">,</span> gc<span class="gtkdoc opt">,</span> x<span class="gtkdoc opt">,</span> y<span class="gtkdoc opt">,</span> layout<span class="gtkdoc opt">);</span>
<span class="comment">/* restore */</span>
<span class="function">gdk_gc_set_clip_rectangle</span> <span class="gtkdoc opt">(</span>gc<span class="gtkdoc opt">,</span> NULL<span class="gtkdoc opt">);</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
          With cairo, the same effect can be achieved using:
          </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</pre></td>
        <td class="listing_code"><pre class="programlisting">GtkStyleContext <span class="gtkdoc opt">*</span>context<span class="gtkdoc opt">;</span>
GtkStateFlags flags<span class="gtkdoc opt">;</span>
GdkRGBA rgba<span class="gtkdoc opt">;</span>
cairo_t <span class="gtkdoc opt">*</span>cr<span class="gtkdoc opt">;</span>

cr <span class="gtkdoc opt">=</span> <span class="function">gdk_cairo_create</span> <span class="gtkdoc opt">(</span>drawable<span class="gtkdoc opt">);</span>
<span class="comment">/* clip */</span>
<span class="function"><a href="http://developer.gnome.org/gdk3/gdk4-Cairo-Interaction.html#gdk-cairo-rectangle">gdk_cairo_rectangle</a></span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">, &amp;</span>area<span class="gtkdoc opt">);</span>
<span class="function">cairo_clip</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">);</span>
<span class="comment">/* set the correct source color */</span>
context <span class="gtkdoc opt">=</span> <span class="function"><a href="GtkWidget.html#gtk-widget-get-style-context">gtk_widget_get_style_context</a></span> <span class="gtkdoc opt">(</span>widget<span class="gtkdoc opt">));</span>
state <span class="gtkdoc opt">=</span> <span class="function"><a href="GtkWidget.html#gtk-widget-get-state-flags">gtk_widget_get_state_flags</a></span> <span class="gtkdoc opt">(</span>widget<span class="gtkdoc opt">);</span>
<span class="function"><a href="GtkStyleContext.html#gtk-style-context-get-color">gtk_style_context_get_color</a></span> <span class="gtkdoc opt">(</span>context<span class="gtkdoc opt">,</span> state<span class="gtkdoc opt">, &amp;</span>rgba<span class="gtkdoc opt">);</span>
<span class="function"><a href="http://developer.gnome.org/gdk3/gdk4-Cairo-Interaction.html#gdk-cairo-set-source-rgba">gdk_cairo_set_source_rgba</a></span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">, &amp;</span>rgba<span class="gtkdoc opt">);</span>
<span class="comment">/* draw the text */</span>
<span class="function">cairo_move_to</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">,</span> x<span class="gtkdoc opt">,</span> y<span class="gtkdoc opt">);</span>
<span class="function"><a href="/home/mclasen/gnome/share/gtk-doc/html/pango/pango-Cairo-Rendering.html#pango-cairo-show-layout">pango_cairo_show_layout</a></span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">,</span> layout<span class="gtkdoc opt">);</span>
<span class="function">cairo_destroy</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">);</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
          Clipping using <code class="function">cairo_clip()</code> is of course not restricted to text
          rendering and can be used everywhere where GC clips were used.
          And using <code class="function">gdk_cairo_set_source_color()</code> with style colors should
          be used in all the places where a style’s GC was used to achieve
          a particular color.
        </p>
</div>
</div>
<br class="example-break"><div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id-1.6.3.3.8.8"></a>What should you be aware of ?</h4></div></div></div>
<p><b>No more stippling. </b>
          Stippling is the usage of a bi-level mask, called a <span class="type">GdkBitmap</span>.
          It was often used to achieve a checkerboard effect. You can use
          <code class="function">cairo_mask()</code> to achieve this effect. To get a checkerbox mask,
          you can use code like this:
          </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</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="gtkdoc kwb">static</span> cairo_pattern_t <span class="gtkdoc opt">*</span>
<span class="function">gtk_color_button_get_checkered</span> <span class="gtkdoc opt">(</span><span class="gtkdoc kwb">void</span><span class="gtkdoc opt">)</span>
<span class="gtkdoc opt">{</span>
    <span class="comment">/* need to respect pixman&apos;s stride being a multiple of 4 */</span>
    <span class="gtkdoc kwb">static unsigned char</span> data<span class="gtkdoc opt">[</span><span class="number">8</span><span class="gtkdoc opt">] = {</span> <span class="number">0xFF</span><span class="gtkdoc opt">,</span> <span class="number">0x00</span><span class="gtkdoc opt">,</span> <span class="number">0x00</span><span class="gtkdoc opt">,</span> <span class="number">0x00</span><span class="gtkdoc opt">,</span>
                                     <span class="number">0x00</span><span class="gtkdoc opt">,</span> <span class="number">0xFF</span><span class="gtkdoc opt">,</span> <span class="number">0x00</span><span class="gtkdoc opt">,</span> <span class="number">0x00</span> <span class="gtkdoc opt">};</span>
    cairo_surface_t <span class="gtkdoc opt">*</span>surface<span class="gtkdoc opt">;</span>
    cairo_pattern_t <span class="gtkdoc opt">*</span>pattern<span class="gtkdoc opt">;</span>

    surface <span class="gtkdoc opt">=</span> <span class="function">cairo_image_surface_create_for_data</span> <span class="gtkdoc opt">(</span>data<span class="gtkdoc opt">,</span>
                                                   CAIRO_FORMAT_A8<span class="gtkdoc opt">,</span>
                                                   <span class="number">2</span><span class="gtkdoc opt">,</span> <span class="number">2</span><span class="gtkdoc opt">,</span>
                                                   <span class="number">4</span><span class="gtkdoc opt">);</span>
    pattern <span class="gtkdoc opt">=</span> <span class="function">cairo_pattern_create_for_surface</span> <span class="gtkdoc opt">(</span>surface<span class="gtkdoc opt">);</span>
    <span class="function">cairo_surface_destroy</span> <span class="gtkdoc opt">(</span>surface<span class="gtkdoc opt">);</span>
    <span class="function">cairo_pattern_set_extend</span> <span class="gtkdoc opt">(</span>pattern<span class="gtkdoc opt">,</span> CAIRO_EXTEND_REPEAT<span class="gtkdoc opt">);</span>
    <span class="function">cairo_pattern_set_filter</span> <span class="gtkdoc opt">(</span>pattern<span class="gtkdoc opt">,</span> CAIRO_FILTER_NEAREST<span class="gtkdoc opt">);</span>

    <span class="keyword">return</span> pattern<span class="gtkdoc opt">;</span>
<span class="gtkdoc opt">}</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
          Note that stippling looks very outdated in UIs, and is rarely
          used in modern applications. All properties that made use of
          stippling have been removed from GTK+ 3. Most prominently,
          stippling is absent from text rendering, in particular <a class="link" href="GtkTextTag.html" title="GtkTextTag"><span class="type">GtkTextTag</span></a>.
        </p>
<p><b>Using the target also as source or mask. </b>
          The <code class="function">gdk_draw_drawable()</code> function allowed using the same drawable
          as source and target. This was often used to achieve a scrolling
          effect. Cairo does not allow this yet. You can however use
          <code class="function">cairo_push_group()</code> to get a different intermediate target that
          you can copy to. So you can replace this code:
          </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</pre></td>
        <td class="listing_code"><pre class="programlisting"><span class="function">gdk_draw_drawable</span> <span class="gtkdoc opt">(</span>pixmap<span class="gtkdoc opt">,</span>
                   gc<span class="gtkdoc opt">,</span>
                   pixmap<span class="gtkdoc opt">,</span>
                   area<span class="gtkdoc opt">.</span>x <span class="gtkdoc opt">+</span> dx<span class="gtkdoc opt">,</span> area<span class="gtkdoc opt">.</span>y <span class="gtkdoc opt">+</span> dy<span class="gtkdoc opt">,</span>
                   area<span class="gtkdoc opt">.</span>x<span class="gtkdoc opt">,</span> area<span class="gtkdoc opt">.</span>y<span class="gtkdoc opt">,</span>
                   area<span class="gtkdoc opt">.</span>width<span class="gtkdoc opt">,</span> area<span class="gtkdoc opt">.</span>height<span class="gtkdoc opt">);</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
          By using this code:
          </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</pre></td>
        <td class="listing_code"><pre class="programlisting">cairo_t <span class="gtkdoc opt">*</span>cr <span class="gtkdoc opt">=</span> <span class="function">cairo_create</span> <span class="gtkdoc opt">(</span>surface<span class="gtkdoc opt">);</span>
<span class="comment">/* clipping restricts the intermediate surface&apos;s size, so it&apos;s a good idea</span>
<span class="comment"> * to use it. */</span>
<span class="function"><a href="http://developer.gnome.org/gdk3/gdk4-Cairo-Interaction.html#gdk-cairo-rectangle">gdk_cairo_rectangle</a></span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">, &amp;</span>area<span class="gtkdoc opt">);</span>
<span class="function">cairo_clip</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">);</span>
<span class="comment">/* Now push a group to change the target */</span>
<span class="function">cairo_push_group</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">);</span>
<span class="function">cairo_set_source_surface</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">,</span> surface<span class="gtkdoc opt">,</span> dx<span class="gtkdoc opt">,</span> dy<span class="gtkdoc opt">);</span>
<span class="function">cairo_paint</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">);</span>
<span class="comment">/* Now copy the intermediate target back */</span>
<span class="function">cairo_pop_group_to_source</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">);</span>
<span class="function">cairo_paint</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">);</span>
<span class="function">cairo_destroy</span> <span class="gtkdoc opt">(</span>cr<span class="gtkdoc opt">);</span></pre></td>
      </tr>
    </tbody>
  </table>
</div>

<p>
The surface here can be either an image surface or a X surface,
and can either be created on the spot or kept around for caching purposes.
Another alternative is to use pixbufs instead of surfaces with
<a href="http://developer.gnome.org/gdk3/gdk4-Cairo-Interaction.html#gdk-cairo-set-source-pixbuf"><code class="function">gdk_cairo_set_source_pixbuf()</code></a> instead of <code class="function">cairo_set_source_surface()</code>.
        
          The cairo developers plan to add self-copies in the future to allow
          exactly this effect, so you might want to keep up on cairo
          development to be able to change your code.
        </p>
<p><b>Using pango_cairo_show_layout() instead of gdk_draw_layout_with_colors(). </b>
          GDK provided a way to ignore the color attributes of text and use
          a hardcoded text color with the <code class="function">gdk_draw_layout_with_colors()</code>
          function. This is often used to draw text shadows or selections.
          Pango’s cairo support does not yet provide this functionality. If
          you use Pango layouts that change colors, the easiest way to achieve
          a similar effect is using <a href="/home/mclasen/gnome/share/gtk-doc/html/pango/pango-Cairo-Rendering.html#pango-cairo-layout-path"><code class="function">pango_cairo_layout_path()</code></a> and <code class="function">cairo_fill()</code>
          instead of <code class="function">gdk_draw_layout_with_colors()</code>. Note that this results in
          a slightly uglier-looking text, as subpixel anti-aliasing is not
          supported.
        </p>
</div>
</div>
</div>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.25.1</div>
</body>
</html>