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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Tree and List Widget Overview: 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="TreeWidgetObjects.html" title="Tree, List and Icon Grid Widgets">
<link rel="prev" href="TreeWidgetObjects.html" title="Tree, List and Icon Grid Widgets">
<link rel="next" href="GtkTreeModel.html" title="GtkTreeModel">
<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="TreeWidgetObjects.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="TreeWidgetObjects.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="GtkTreeModel.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="refentry">
<a name="TreeWidget"></a><div class="titlepage"></div>
<div class="refnamediv"><table width="100%"><tr>
<td valign="top">
<h2><span class="refentrytitle">Tree and List Widget Overview</span></h2>
<p>Tree and List Widget Overview — Overview of GtkTreeModel, GtkTreeView, and friends</p>
</td>
<td class="gallery_image" valign="top" align="right"></td>
</tr></table></div>
<div class="refsect1">
<a name="id-1.4.9.2.3"></a><h2>Overview</h2>
<p>
To create a tree or list in GTK+, use the <a class="link" href="GtkTreeModel.html" title="GtkTreeModel"><span class="type">GtkTreeModel</span></a> interface in
conjunction with the <a class="link" href="GtkTreeView.html" title="GtkTreeView"><span class="type">GtkTreeView</span></a> widget. This widget is
designed around a <em class="firstterm">Model/View/Controller</em>
design and consists of four major parts:
</p>
<table border="0" summary="Simple list" class="simplelist">
<tr><td>The tree view widget (<span class="structname">GtkTreeView</span>)</td></tr>
<tr><td>The view column (<span class="structname">GtkTreeViewColumn</span>)</td></tr>
<tr><td>The cell renderers (<span class="structname">GtkCellRenderer</span> etc.)</td></tr>
<tr><td>The model interface (<span class="structname">GtkTreeModel</span>)</td></tr>
</table>
<p>
The <span class="emphasis"><em>View</em></span> is composed of the first three
objects, while the last is the <span class="emphasis"><em>Model</em></span>. One
of the prime benefits of the MVC design is that multiple views
can be created of a single model. For example, a model mapping
the file system could be created for a file manager. Many views
could be created to display various parts of the file system,
but only one copy need be kept in memory.
</p>
<p>
The purpose of the cell renderers is to provide extensibility to the
widget and to allow multiple ways of rendering the same type of data.
For example, consider how to render a boolean variable. Should it
render it as a string of "True" or "False", "On" or "Off", or should
it be rendered as a checkbox?
</p>
</div>
<div class="refsect1">
<a name="id-1.4.9.2.4"></a><h2>Creating a model</h2>
<p>
GTK+ provides two simple models that can be used: the <a class="link" href="GtkListStore.html" title="GtkListStore"><span class="type">GtkListStore</span></a>
and the <a class="link" href="GtkTreeStore.html" title="GtkTreeStore"><span class="type">GtkTreeStore</span></a>. GtkListStore is used to model list widgets,
while the GtkTreeStore models trees. It is possible to develop a new
type of model, but the existing models should be satisfactory for all
but the most specialized of situations. Creating the model is quite
simple:
</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">GtkListStore</span> <span class="o">*</span><span class="n">store</span> <span class="o">=</span> <span class="n">gtk_list_store_new</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">G_TYPE_STRING</span><span class="p">,</span> <span class="n">G_TYPE_BOOLEAN</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
This creates a list store with two columns: a string column and a boolean
column. Typically the 2 is never passed directly like that; usually an
enum is created wherein the different columns are enumerated, followed by
a token that represents the total number of columns. The next example will
illustrate this, only using a tree store instead of a list store. Creating
a tree store operates almost exactly the same.
</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">enum</span>
<span class="p">{</span>
<span class="n">TITLE_COLUMN</span><span class="p">,</span>
<span class="n">AUTHOR_COLUMN</span><span class="p">,</span>
<span class="n">CHECKED_COLUMN</span><span class="p">,</span>
<span class="n">N_COLUMNS</span>
<span class="p">};</span>
<span class="n">GtkTreeStore</span> <span class="o">*</span><span class="n">store</span> <span class="o">=</span> <span class="n">gtk_tree_store_new</span> <span class="p">(</span><span class="n">N_COLUMNS</span><span class="p">,</span> <span class="cm">/* Total number of columns */</span>
<span class="n">G_TYPE_STRING</span><span class="p">,</span> <span class="cm">/* Book title */</span>
<span class="n">G_TYPE_STRING</span><span class="p">,</span> <span class="cm">/* Author */</span>
<span class="n">G_TYPE_BOOLEAN</span><span class="p">);</span> <span class="cm">/* Is checked out? */</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
Adding data to the model is done using <a class="link" href="GtkTreeStore.html#gtk-tree-store-set" title="gtk_tree_store_set ()"><code class="function">gtk_tree_store_set()</code></a> or
<a class="link" href="GtkListStore.html#gtk-list-store-set" title="gtk_list_store_set ()"><code class="function">gtk_list_store_set()</code></a>, depending upon which sort of model was
created. To do this, a <a class="link" href="GtkTreeModel.html#GtkTreeIter"><span class="type">GtkTreeIter</span></a> must be acquired. The iterator
points to the location where data will be added.
</p>
<p>
Once an iterator has been acquired, <a class="link" href="GtkTreeStore.html#gtk-tree-store-set" title="gtk_tree_store_set ()"><code class="function">gtk_tree_store_set()</code></a> is used to
apply data to the part of the model that the iterator points to.
Consider 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</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">GtkTreeIter</span> <span class="n">iter</span><span class="p">;</span>
<span class="n">gtk_tree_store_append</span> <span class="p">(</span><span class="n">store</span><span class="p">,</span> <span class="o">&</span><span class="n">iter</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span> <span class="cm">/* Acquire an iterator */</span>
<span class="n">gtk_tree_store_set</span> <span class="p">(</span><span class="n">store</span><span class="p">,</span> <span class="o">&</span><span class="n">iter</span><span class="p">,</span>
<span class="n">TITLE_COLUMN</span><span class="p">,</span> <span class="s">"The Principle of Reason"</span><span class="p">,</span>
<span class="n">AUTHOR_COLUMN</span><span class="p">,</span> <span class="s">"Martin Heidegger"</span><span class="p">,</span>
<span class="n">CHECKED_COLUMN</span><span class="p">,</span> <span class="n">FALSE</span><span class="p">,</span>
<span class="o">-</span><span class="mi">1</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
Notice that the last argument is -1. This is always done because
this is a variable-argument function and it needs to know when to stop
processing arguments. It can be used to set the data in any or all
columns in a given row.
</p>
<p>
The third argument to <a class="link" href="GtkTreeStore.html#gtk-tree-store-append" title="gtk_tree_store_append ()"><code class="function">gtk_tree_store_append()</code></a> is the parent iterator. It
is used to add a row to a GtkTreeStore as a child of an existing row. This
means that the new row will only be visible when its parent is visible and
in its expanded state. Consider 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
13
14
15
16
17
18
19
20
21
22
23
24</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">GtkTreeIter</span> <span class="n">iter1</span><span class="p">;</span> <span class="cm">/* Parent iter */</span>
<span class="n">GtkTreeIter</span> <span class="n">iter2</span><span class="p">;</span> <span class="cm">/* Child iter */</span>
<span class="n">gtk_tree_store_append</span> <span class="p">(</span><span class="n">store</span><span class="p">,</span> <span class="o">&</span><span class="n">iter1</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span> <span class="cm">/* Acquire a top-level iterator */</span>
<span class="n">gtk_tree_store_set</span> <span class="p">(</span><span class="n">store</span><span class="p">,</span> <span class="o">&</span><span class="n">iter1</span><span class="p">,</span>
<span class="n">TITLE_COLUMN</span><span class="p">,</span> <span class="s">"The Art of Computer Programming"</span><span class="p">,</span>
<span class="n">AUTHOR_COLUMN</span><span class="p">,</span> <span class="s">"Donald E. Knuth"</span><span class="p">,</span>
<span class="n">CHECKED_COLUMN</span><span class="p">,</span> <span class="n">FALSE</span><span class="p">,</span>
<span class="o">-</span><span class="mi">1</span><span class="p">);</span>
<span class="n">gtk_tree_store_append</span> <span class="p">(</span><span class="n">store</span><span class="p">,</span> <span class="o">&</span><span class="n">iter2</span><span class="p">,</span> <span class="o">&</span><span class="n">iter1</span><span class="p">);</span> <span class="cm">/* Acquire a child iterator */</span>
<span class="n">gtk_tree_store_set</span> <span class="p">(</span><span class="n">store</span><span class="p">,</span> <span class="o">&</span><span class="n">iter2</span><span class="p">,</span>
<span class="n">TITLE_COLUMN</span><span class="p">,</span> <span class="s">"Volume 1: Fundamental Algorithms"</span><span class="p">,</span>
<span class="o">-</span><span class="mi">1</span><span class="p">);</span>
<span class="n">gtk_tree_store_append</span> <span class="p">(</span><span class="n">store</span><span class="p">,</span> <span class="o">&</span><span class="n">iter2</span><span class="p">,</span> <span class="o">&</span><span class="n">iter1</span><span class="p">);</span>
<span class="n">gtk_tree_store_set</span> <span class="p">(</span><span class="n">store</span><span class="p">,</span> <span class="o">&</span><span class="n">iter2</span><span class="p">,</span>
<span class="n">TITLE_COLUMN</span><span class="p">,</span> <span class="s">"Volume 2: Seminumerical Algorithms"</span><span class="p">,</span>
<span class="o">-</span><span class="mi">1</span><span class="p">);</span>
<span class="n">gtk_tree_store_append</span> <span class="p">(</span><span class="n">store</span><span class="p">,</span> <span class="o">&</span><span class="n">iter2</span><span class="p">,</span> <span class="o">&</span><span class="n">iter1</span><span class="p">);</span>
<span class="n">gtk_tree_store_set</span> <span class="p">(</span><span class="n">store</span><span class="p">,</span> <span class="o">&</span><span class="n">iter2</span><span class="p">,</span>
<span class="n">TITLE_COLUMN</span><span class="p">,</span> <span class="s">"Volume 3: Sorting and Searching"</span><span class="p">,</span>
<span class="o">-</span><span class="mi">1</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="refsect1">
<a name="id-1.4.9.2.5"></a><h2>Creating the view component</h2>
<p>
While there are several different models to choose from, there is
only one view widget to deal with. It works with either the list
or the tree store. Setting up a <a class="link" href="GtkTreeView.html" title="GtkTreeView"><span class="type">GtkTreeView</span></a> is not a difficult
matter. It needs a <a class="link" href="GtkTreeModel.html" title="GtkTreeModel"><span class="type">GtkTreeModel</span></a> to know where to retrieve its data
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="n">GtkWidget</span> <span class="o">*</span><span class="n">tree</span><span class="p">;</span>
<span class="n">tree</span> <span class="o">=</span> <span class="n">gtk_tree_view_new_with_model</span> <span class="p">(</span><span class="n">GTK_TREE_MODEL</span> <span class="p">(</span><span class="n">store</span><span class="p">));</span></pre></td>
</tr>
</tbody>
</table>
</div>
<div class="refsect2">
<a name="id-1.4.9.2.5.4"></a><h3>Columns and cell renderers</h3>
<p>
Once the <a class="link" href="GtkTreeView.html" title="GtkTreeView"><span class="type">GtkTreeView</span></a> widget has a model, it will need to know how
to display the model. It does this with columns and cell renderers.
</p>
<p>
Cell renderers are used to draw the data in the tree model in a
way. There are a number of cell renderers that come with GTK+ 2.x,
including the <a class="link" href="GtkCellRendererText.html" title="GtkCellRendererText"><span class="type">GtkCellRendererText</span></a>, <a class="link" href="GtkCellRendererPixbuf.html" title="GtkCellRendererPixbuf"><span class="type">GtkCellRendererPixbuf</span></a> and
the <a class="link" href="GtkCellRendererToggle.html" title="GtkCellRendererToggle"><span class="type">GtkCellRendererToggle</span></a>.
It is relatively easy to write a custom renderer.
</p>
<p>
A <a class="link" href="GtkTreeViewColumn.html" title="GtkTreeViewColumn"><span class="type">GtkTreeViewColumn</span></a> is the object that GtkTreeView uses to organize
the vertical columns in the tree view. It needs to know the name of
the column to label for the user, what type of cell renderer to use,
and which piece of data to retrieve from the model for a given row.
</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="n">GtkCellRenderer</span> <span class="o">*</span><span class="n">renderer</span><span class="p">;</span>
<span class="n">GtkTreeViewColumn</span> <span class="o">*</span><span class="n">column</span><span class="p">;</span>
<span class="n">renderer</span> <span class="o">=</span> <span class="n">gtk_cell_renderer_text_new</span> <span class="p">();</span>
<span class="n">column</span> <span class="o">=</span> <span class="n">gtk_tree_view_column_new_with_attributes</span> <span class="p">(</span><span class="s">"Author"</span><span class="p">,</span>
<span class="n">renderer</span><span class="p">,</span>
<span class="s">"text"</span><span class="p">,</span> <span class="n">AUTHOR_COLUMN</span><span class="p">,</span>
<span class="nb">NULL</span><span class="p">);</span>
<span class="n">gtk_tree_view_append_column</span> <span class="p">(</span><span class="n">GTK_TREE_VIEW</span> <span class="p">(</span><span class="n">tree</span><span class="p">),</span> <span class="n">column</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
At this point, all the steps in creating a displayable tree have been
covered. The model is created, data is stored in it, a tree view is
created and columns are added to it.
</p>
</div>
<hr>
<div class="refsect2">
<a name="id-1.4.9.2.5.5"></a><h3>Selection handling</h3>
<p>
Most applications will need to not only deal with displaying data, but
also receiving input events from users. To do this, simply get a
reference to a selection object and connect to the
<a class="link" href="GtkTreeSelection.html#GtkTreeSelection-changed" title="The “changed” signal"><span class="type">“changed”</span></a> signal.
</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</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="cm">/* Prototype for selection handler callback */</span>
<span class="k">static</span> <span class="kt">void</span> <span class="nf">tree_selection_changed_cb</span> <span class="p">(</span><span class="n">GtkTreeSelection</span> <span class="o">*</span><span class="n">selection</span><span class="p">,</span> <span class="n">gpointer</span> <span class="n">data</span><span class="p">);</span>
<span class="cm">/* Setup the selection handler */</span>
<span class="n">GtkTreeSelection</span> <span class="o">*</span><span class="n">select</span><span class="p">;</span>
<span class="n">select</span> <span class="o">=</span> <span class="n">gtk_tree_view_get_selection</span> <span class="p">(</span><span class="n">GTK_TREE_VIEW</span> <span class="p">(</span><span class="n">tree</span><span class="p">));</span>
<span class="n">gtk_tree_selection_set_mode</span> <span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">GTK_SELECTION_SINGLE</span><span class="p">);</span>
<span class="n">g_signal_connect</span> <span class="p">(</span><span class="n">G_OBJECT</span> <span class="p">(</span><span class="n">select</span><span class="p">),</span> <span class="s">"changed"</span><span class="p">,</span>
<span class="n">G_CALLBACK</span> <span class="p">(</span><span class="n">tree_selection_changed_cb</span><span class="p">),</span>
<span class="nb">NULL</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
Then to retrieve data for the row selected:
</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</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="k">static</span> <span class="kt">void</span>
<span class="nf">tree_selection_changed_cb</span> <span class="p">(</span><span class="n">GtkTreeSelection</span> <span class="o">*</span><span class="n">selection</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">GtkTreeIter</span> <span class="n">iter</span><span class="p">;</span>
<span class="n">GtkTreeModel</span> <span class="o">*</span><span class="n">model</span><span class="p">;</span>
<span class="n">gchar</span> <span class="o">*</span><span class="n">author</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">gtk_tree_selection_get_selected</span> <span class="p">(</span><span class="n">selection</span><span class="p">,</span> <span class="o">&</span><span class="n">model</span><span class="p">,</span> <span class="o">&</span><span class="n">iter</span><span class="p">))</span>
<span class="p">{</span>
<span class="n">gtk_tree_model_get</span> <span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="o">&</span><span class="n">iter</span><span class="p">,</span> <span class="n">AUTHOR_COLUMN</span><span class="p">,</span> <span class="o">&</span><span class="n">author</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">);</span>
<span class="n">g_print</span> <span class="p">(</span><span class="s">"You selected a book by %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">author</span><span class="p">);</span>
<span class="n">g_free</span> <span class="p">(</span><span class="n">author</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span></pre></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="refsect1">
<a name="id-1.4.9.2.6"></a><h2>Simple Example</h2>
<p>
Here is a simple example of using a <a class="link" href="GtkTreeView.html" title="GtkTreeView"><span class="type">GtkTreeView</span></a> widget in context
of the other widgets. It simply creates a simple model and view,
and puts them together. Note that the model is never populated
with data — that is left as an exercise for the reader.
More information can be found on this in the <a class="link" href="GtkTreeModel.html" title="GtkTreeModel"><span class="type">GtkTreeModel</span></a> section.
</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
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</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="k">enum</span>
<span class="p">{</span>
<span class="n">TITLE_COLUMN</span><span class="p">,</span>
<span class="n">AUTHOR_COLUMN</span><span class="p">,</span>
<span class="n">CHECKED_COLUMN</span><span class="p">,</span>
<span class="n">N_COLUMNS</span>
<span class="p">};</span>
<span class="kt">void</span>
<span class="nf">setup_tree</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">GtkTreeStore</span> <span class="o">*</span><span class="n">store</span><span class="p">;</span>
<span class="n">GtkWidget</span> <span class="o">*</span><span class="n">tree</span><span class="p">;</span>
<span class="n">GtkTreeViewColumn</span> <span class="o">*</span><span class="n">column</span><span class="p">;</span>
<span class="n">GtkCellRenderer</span> <span class="o">*</span><span class="n">renderer</span><span class="p">;</span>
<span class="cm">/* Create a model. We are using the store model for now, though we</span>
<span class="cm"> * could use any other GtkTreeModel */</span>
<span class="n">store</span> <span class="o">=</span> <span class="n">gtk_tree_store_new</span> <span class="p">(</span><span class="n">N_COLUMNS</span><span class="p">,</span>
<span class="n">G_TYPE_STRING</span><span class="p">,</span>
<span class="n">G_TYPE_STRING</span><span class="p">,</span>
<span class="n">G_TYPE_BOOLEAN</span><span class="p">);</span>
<span class="cm">/* custom function to fill the model with data */</span>
<span class="n">populate_tree_model</span> <span class="p">(</span><span class="n">store</span><span class="p">);</span>
<span class="cm">/* Create a view */</span>
<span class="n">tree</span> <span class="o">=</span> <span class="n">gtk_tree_view_new_with_model</span> <span class="p">(</span><span class="n">GTK_TREE_MODEL</span> <span class="p">(</span><span class="n">store</span><span class="p">));</span>
<span class="cm">/* The view now holds a reference. We can get rid of our own</span>
<span class="cm"> * reference */</span>
<span class="n">g_object_unref</span> <span class="p">(</span><span class="n">G_OBJECT</span> <span class="p">(</span><span class="n">store</span><span class="p">));</span>
<span class="cm">/* Create a cell render and arbitrarily make it red for demonstration</span>
<span class="cm"> * purposes */</span>
<span class="n">renderer</span> <span class="o">=</span> <span class="n">gtk_cell_renderer_text_new</span> <span class="p">();</span>
<span class="n">g_object_set</span> <span class="p">(</span><span class="n">G_OBJECT</span> <span class="p">(</span><span class="n">renderer</span><span class="p">),</span>
<span class="s">"foreground"</span><span class="p">,</span> <span class="s">"red"</span><span class="p">,</span>
<span class="nb">NULL</span><span class="p">);</span>
<span class="cm">/* Create a column, associating the "text" attribute of the</span>
<span class="cm"> * cell_renderer to the first column of the model */</span>
<span class="n">column</span> <span class="o">=</span> <span class="n">gtk_tree_view_column_new_with_attributes</span> <span class="p">(</span><span class="s">"Author"</span><span class="p">,</span> <span class="n">renderer</span><span class="p">,</span>
<span class="s">"text"</span><span class="p">,</span> <span class="n">AUTHOR_COLUMN</span><span class="p">,</span>
<span class="nb">NULL</span><span class="p">);</span>
<span class="cm">/* Add the column to the view. */</span>
<span class="n">gtk_tree_view_append_column</span> <span class="p">(</span><span class="n">GTK_TREE_VIEW</span> <span class="p">(</span><span class="n">tree</span><span class="p">),</span> <span class="n">column</span><span class="p">);</span>
<span class="cm">/* Second column.. title of the book. */</span>
<span class="n">renderer</span> <span class="o">=</span> <span class="n">gtk_cell_renderer_text_new</span> <span class="p">();</span>
<span class="n">column</span> <span class="o">=</span> <span class="n">gtk_tree_view_column_new_with_attributes</span> <span class="p">(</span><span class="s">"Title"</span><span class="p">,</span>
<span class="n">renderer</span><span class="p">,</span>
<span class="s">"text"</span><span class="p">,</span> <span class="n">TITLE_COLUMN</span><span class="p">,</span>
<span class="nb">NULL</span><span class="p">);</span>
<span class="n">gtk_tree_view_append_column</span> <span class="p">(</span><span class="n">GTK_TREE_VIEW</span> <span class="p">(</span><span class="n">tree</span><span class="p">),</span> <span class="n">column</span><span class="p">);</span>
<span class="cm">/* Last column.. whether a book is checked out. */</span>
<span class="n">renderer</span> <span class="o">=</span> <span class="n">gtk_cell_renderer_toggle_new</span> <span class="p">();</span>
<span class="n">column</span> <span class="o">=</span> <span class="n">gtk_tree_view_column_new_with_attributes</span> <span class="p">(</span><span class="s">"Checked out"</span><span class="p">,</span>
<span class="n">renderer</span><span class="p">,</span>
<span class="s">"active"</span><span class="p">,</span> <span class="n">CHECKED_COLUMN</span><span class="p">,</span>
<span class="nb">NULL</span><span class="p">);</span>
<span class="n">gtk_tree_view_append_column</span> <span class="p">(</span><span class="n">GTK_TREE_VIEW</span> <span class="p">(</span><span class="n">tree</span><span class="p">),</span> <span class="n">column</span><span class="p">);</span>
<span class="cm">/* Now we can manipulate the view just like any other GTK widget */</span>
<span class="p">...</span>
<span class="p">}</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
</p>
</div>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.33.0</div>
</body>
</html>
|