File: ch07.html

package info (click to toggle)
gtkmm2.0 2.2.12-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 56,872 kB
  • ctags: 51,600
  • sloc: xml: 73,173; cpp: 20,565; sh: 8,608; perl: 2,702; makefile: 1,233
file content (541 lines) | stat: -rw-r--r-- 21,447 bytes parent folder | download | duplicates (2)
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
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter7.Container Widgets</title><meta name="generator" content="DocBook XSL Stylesheets V1.64.1"><link rel="home" href="index.html" title="Programming with gtkmm2"><link rel="up" href="index.html" title="Programming with gtkmm2"><link rel="previous" href="ch06s06.html" title="Tooltips"><link rel="next" href="ch07s02.html" title="Multiple-item widgets "></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter7.Container Widgets</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06s06.html">Prev</a></td><th width="60%" align="center"></th><td width="20%" align="right"><a accesskey="n" href="ch07s02.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="sec-ContainerWidgets"></a>Chapter7.Container Widgets</h2></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch07.html#sec-SingleItemWidgets">Single-item Containers</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch07.html#sec-Frame">Frame</a></span></dt><dt><span class="sect2"><a href="ch07.html#id2506451">Paned</a></span></dt><dt><span class="sect2"><a href="ch07.html#sec-ScrolledWindows">ScrolledWindow</a></span></dt><dt><span class="sect2"><a href="ch07.html#sec-AspectFrames">AspectFrame</a></span></dt><dt><span class="sect2"><a href="ch07.html#sec-Alignment">Alignment</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch07s02.html">Multiple-item widgets </a></span></dt><dd><dl><dt><span class="sect2"><a href="ch07s02.html#id2507422">Packing</a></span></dt><dt><span class="sect2"><a href="ch07s02.html#sec-helloworld2">An improved Hello World</a></span></dt><dt><span class="sect2"><a href="ch07s02.html#sec-stl-style">STL-style APIs</a></span></dt><dt><span class="sect2"><a href="ch07s02.html#sec-Boxes">Boxes</a></span></dt><dt><span class="sect2"><a href="ch07s02.html#sec-ButtonBoxes">ButtonBoxes </a></span></dt><dt><span class="sect2"><a href="ch07s02.html#id2509484">Table</a></span></dt><dt><span class="sect2"><a href="ch07s02.html#sec-Notebook">Notebook</a></span></dt></dl></dd></dl></div><p>All container widgets derive from <tt class="literal">Gtk::Container</tt>, not always directly. Some container widgets, such as <tt class="literal">Gtk::Table</tt> can hold many child widgets, so these typically have more complex interfaces. Others, such as <tt class="literal">Gtk::Frame</tt> contain only one child widget.
</p><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec-SingleItemWidgets"></a>Single-item Containers</h2></div></div><div></div></div><p>The single-item container widgets derive from <tt class="literal">Gtk::Bin</tt>, which provides the <tt class="literal">add()</tt> and <tt class="literal">remove()</tt> methods for the child widget. Note that <tt class="literal">Gtk::Button</tt> and <tt class="literal">Gtk::Window</tt> are technically single-item containers, but we have discussed them already elsewhere. 
</p><p>
We also discuss the <tt class="literal">Gtk::Paned</tt> widget, which allows you to divide
a window into two separate "panes".  This widget actually contains two
child widgets, but the number is fixed so it seems appropriate.
</p><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sec-Frame"></a>Frame</h3></div></div><div></div></div><p>
Frames can enclose one or a group of widgets within a box, optionally with a title. For instance, you might place a group of <tt class="literal">RadioButton</tt>s or <tt class="literal">CheckButton</tt>s in a <tt class="literal">Frame</tt>.
</p><p><a href="../../reference/html/classGtk_1_1Frame.html" target="_top">Reference</a></p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2506338"></a>Example</h4></div></div><div></div></div><div class="figure"><a name="figure-frame"></a><p class="title"><b>Figure7.1.Frame</b></p><div class="screenshot"><div><img src="../figures/frame.png" alt="Frame"></div></div></div><p><a href="../../../examples/book/frame" target="_top">Source Code</a></p><p>File: examplewindow.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();
  virtual ~ExampleWindow();

protected:

  //Child widgets:
  Gtk::Frame m_Frame;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</pre><p>
</p><p>File: examplewindow.cc
</p><pre class="programlisting">
#include "examplewindow.h"

ExampleWindow::ExampleWindow()
{
 /* Set some window properties */
  set_title("Frame Example");
  set_size_request(300, 300);

  /* Sets the border width of the window. */
  set_border_width(10);

  add(m_Frame);

  /* Set the frames label */
  m_Frame.set_label("Gtk::Frame Widget");

  /* Align the label at the right of the frame */
  //m_Frame.set_label_align(Gtk::ALIGN_RIGHT, Gtk::ALIGN_TOP);

  /* Set the style of the frame */
  m_Frame.set_shadow_type(Gtk::SHADOW_ETCHED_OUT);

  show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}


</pre><p>
</p><p>File: main.cc
</p><pre class="programlisting">
#include &lt;gtkmm/main.h&gt;
#include "examplewindow.h"

int main(int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);

  ExampleWindow window;
  Gtk::Main::run(window); //Shows the window and returns when it is closed.

  return 0;
}
</pre><p>
</p></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2506451"></a>Paned</h3></div></div><div></div></div><p>
Panes divide a widget into two halves, separated by a moveable
divider.  There are two such widgets: <tt class="literal">Gtk::HPaned</tt> adds a
horizontal divider, and <tt class="literal">Gtk::VPaned</tt> adds a vertical one.  Other
than the names and the orientations, there's no difference between the
two.
</p><p>
Unlike the other widgets in this chapter, pane widgets contain not
one but two child widgets, one in each pane. Therefore, you should use <tt class="literal">add1()</tt> and <tt class="literal">add2()</tt>
instead of the <tt class="literal">add()</tt> method.</p><p>
You can adjust the position of the divider using the <tt class="literal">set_position()</tt> method, and you will probably need to do so.
</p><p><a href="../../reference/html/classGtk_1_1Paned.html" target="_top">Reference</a></p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2506517"></a>Example</h4></div></div><div></div></div><div class="figure"><a name="figure-paned"></a><p class="title"><b>Figure7.2.Paned</b></p><div class="screenshot"><div><img src="../figures/paned.png" alt="Paned"></div></div></div><p><a href="../../../examples/book/paned" target="_top">Source Code</a></p><p>File: examplewindow.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include "messageslist.h"
#include "messagetext.h"
#include &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();
  virtual ~ExampleWindow();

protected:

  //Child widgets:
  Gtk::VPaned m_VPaned;
  MessagesList m_MessagesList;
  MessageText m_MessageText;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</pre><p>
</p><p>File: messageslist.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLE_MESSAGESLIST_H
#define GTKMM_EXAMPLE_MESSAGESLIST_H

#include &lt;gtkmm.h&gt;

class MessagesList: public Gtk::ScrolledWindow
{
public:
  MessagesList();
  virtual ~MessagesList();

  class ModelColumns : public Gtk::TreeModel::ColumnRecord
  {
  public:

    ModelColumns()
    { add(m_col_text); }

    Gtk::TreeModelColumn&lt;Glib::ustring&gt; m_col_text;
  };

  ModelColumns m_Columns;

protected:
  Glib::RefPtr&lt;Gtk::ListStore&gt; m_refListStore; //The Tree Model.
  Gtk::TreeView m_TreeView; //The Tree View.
};
#endif //GTKMM_EXAMPLE_MESSAGESLIST_H
</pre><p>
</p><p>File: messagetext.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLE_MESSAGETEXT_H
#define GTKMM_EXAMPLE_MESSAGETEXT_H

#include &lt;gtkmm.h&gt;

class MessageText : public Gtk::ScrolledWindow
{
public:
  MessageText();
  virtual ~MessageText();

  virtual void insert_text();

protected:
  Gtk::TextView m_TextView;
};

#endif //GTKMM_EXAMPLE_MESSAGETEXT_H
</pre><p>
</p><p>File: examplewindow.cc
</p><pre class="programlisting">
#include "examplewindow.h"

ExampleWindow::ExampleWindow()
{
  set_title ("Paned Windows");
  set_border_width(10);
  set_default_size(450, 400);

  /* Add a vpaned widget to our toplevel window */
  add(m_VPaned);

  /* Now add the contents of the two halves of the window */
  m_VPaned.add1(m_MessagesList);
  m_VPaned.add2(m_MessageText);

  show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}


</pre><p>
</p><p>File: messageslist.cc
</p><pre class="programlisting">
#include "messageslist.h"
#include &lt;sstream&gt;

MessagesList::MessagesList()
{
  /* Create a new scrolled window, with scrollbars only if needed */
  set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

  add(m_TreeView);

  /* create list store */
  m_refListStore = Gtk::ListStore::create(m_Columns);

  m_TreeView.set_model(m_refListStore);

  /* Add some messages to the window */
  for(int i = 0; i &lt; 10; ++i)
  {
    std::ostringstream text;
    text &lt;&lt; "message #" &lt;&lt; i;

    Gtk::TreeModel::Row row = *(m_refListStore-&gt;append());
    row[m_Columns.m_col_text] = text.str();
  }

  //Add the Model's column to the View's columns:
  m_TreeView.append_column("Messages", m_Columns.m_col_text);

  show_all_children();
}

MessagesList::~MessagesList()
{
}
</pre><p>
</p><p>File: messagetext.cc
</p><pre class="programlisting">
#include "messagetext.h"

MessageText::MessageText()
{
  set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

  add(m_TextView);
  insert_text();

  show_all_children();
}

MessageText::~MessageText()
{
}

void MessageText::insert_text()
{
  Glib::RefPtr&lt;Gtk::TextBuffer&gt; refTextBuffer = m_TextView.get_buffer();

  Gtk::TextBuffer::iterator iter = refTextBuffer-&gt;get_iter_at_offset(0);
  refTextBuffer-&gt;insert(iter,
    "From: pathfinder@nasa.gov\n"
    "To: mom@nasa.gov\n"
    "Subject: Made it!\n"
    "\n"
    "We just got in this morning. The weather has been\n"
    "great - clear but cold, and there are lots of fun sights.\n"
    "Sojourner says hi. See you soon.\n"
    " -Path\n");
}
</pre><p>
</p></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sec-ScrolledWindows"></a>ScrolledWindow</h3></div></div><div></div></div><p>
<tt class="literal">ScrolledWindow</tt> widgets are used to create a scrollable area.  You can insert any type of widget into a <tt class="literal">ScrolledWindow</tt> window, and
it will be accessible regardless of its size by using the scrollbars. Note that <tt class="literal">ScrolledWindow</tt> is not a <tt class="literal">Gtk::Window</tt> despite the slightly misleading name.
</p><p>
Scrolled windows have <span class="emphasis"><em>scrollbar policies</em></span> which determine whether
the <tt class="literal">Scrollbar</tt>s will be displayed. The policies can be set with the <tt class="literal">set_policy()</tt> method. The policy may be one of <tt class="literal">Gtk::POLICY_AUTOMATIC</tt> or <tt class="literal">Gtk::POLICY_ALWAYS</tt>.
<tt class="literal">Gtk::POLICY_AUTOMATIC</tt> will cause the scrolled window to display
the scrollbar only if the contained widget is larger than the visible area. 
<tt class="literal">Gtk::POLICY_ALWAYS</tt> will cause the scrollbar to be displayed always.
</p><p><a href="../../reference/html/classGtk_1_1ScrolledWindow.html" target="_top">Reference</a></p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2506828"></a>Example</h4></div></div><div></div></div><p>
Here is a simple example that packs 100 toggle buttons into a ScrolledWindow. Try resizing the window to see the scrollbars react.
</p><div class="figure"><a name="figure-scrolledwindow"></a><p class="title"><b>Figure7.3.ScrolledWindow</b></p><div class="screenshot"><div><img src="../figures/scrolledwindow.png" alt="ScrolledWindow"></div></div></div><p><a href="../../../examples/book/scrolledwindow" target="_top">Source Code</a></p><p>File: examplewindow.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Dialog
{
public:
  ExampleWindow();
  virtual ~ExampleWindow();

protected:
  //Signal handlers:
  virtual void on_button_close();

  //Child widgets:
  Gtk::ScrolledWindow m_ScrolledWindow;
  Gtk::Table m_Table;
  Gtk::Button m_Button_Close;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</pre><p>
</p><p>File: examplewindow.cc
</p><pre class="programlisting">
#include "examplewindow.h"
#include &lt;stdio.h&gt; //For sprintf()

ExampleWindow::ExampleWindow()
: m_Table(10, 10),
  m_Button_Close("Close")
{
  set_title("Gtk::ScrolledWindow example");
  set_border_width(0);
  set_size_request(300, 300);

  m_ScrolledWindow.set_border_width(10);

  /* the policy is one of Gtk::POLICY AUTOMATIC, or Gtk::POLICY_ALWAYS.
   * Gtk::POLICY_AUTOMATIC will automatically decide whether you need
   * scrollbars, whereas Gtk::POLICY_ALWAYS will always leave the scrollbars
   * there.  The first one is the horizontal scrollbar, the second,
   * the vertical. */
  m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_ALWAYS);

  get_vbox()-&gt;pack_start(m_ScrolledWindow);

  /* set the spacing to 10 on x and 10 on y */
  m_Table.set_row_spacings(10);
  m_Table.set_col_spacings(10);

  /* pack the table into the scrolled window */
  m_ScrolledWindow.add(m_Table);

  /* this simply creates a grid of toggle buttons on the table
   * to demonstrate the scrolled window. */
  for(int i = 0; i &lt; 10; i++)
  {
     for(int j = 0; j &lt; 10; j++)
     {
        char buffer[32];
        sprintf(buffer, "button (%d,%d)\n", i, j);
        Gtk::Button* pButton = Gtk::manage(new Gtk::ToggleButton(buffer));
        m_Table.attach(*pButton, i, i + 1, j, j + 1);
     }
  }

  /* Add a "close" button to the bottom of the dialog */
  m_Button_Close.signal_clicked().connect(slot(*this, &amp;ExampleWindow::on_button_close));

  /* this makes it so the button is the default. */
  m_Button_Close.set_flags(Gtk::CAN_DEFAULT);

  Gtk::Box* pBox = get_action_area();
  if(pBox)
    pBox-&gt;pack_start(m_Button_Close);

  /* This grabs this button to be the default button. Simply hitting
   * the "Enter" key will cause this button to activate. */
  m_Button_Close.grab_default();

  show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_button_close()
{
  hide();
}


</pre><p>
</p><p>File: main.cc
</p><pre class="programlisting">
#include &lt;gtkmm/main.h&gt;
#include "examplewindow.h"

int main(int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);

  ExampleWindow window;
  Gtk::Main::run(window); //Shows the window and returns when it is closed.

  return 0;
}
</pre><p>
</p></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sec-AspectFrames"></a>AspectFrame</h3></div></div><div></div></div><p>
The <tt class="literal">AspectFrame</tt> widget looks like a <tt class="literal">Frame</tt> widget, but it also
enforces the <span class="emphasis"><em>aspect ratio</em></span> (the ratio of the width to the
height) of the child widget, adding extra
space if necessary. For instance, this would allow you to display a photograph without allowing the user to distort it horizontally or vertically while resizing.
</p><p><a href="../../reference/html/classGtk_1_1AspectFrame.html" target="_top">Reference</a></p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2507029"></a>Example</h4></div></div><div></div></div><p>
The following program uses a <tt class="literal">Gtk::AspectFrame</tt> to present a drawing area whose aspect ratio will always be 2:1, no matter how the user resizes the top-level window.</p><p>TODO: screenshot</p><p><a href="../../../examples/book/aspectframe" target="_top">Source Code</a></p><p>File: examplewindow.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();
  virtual ~ExampleWindow();

protected:

  //Child widgets:
  Gtk::AspectFrame m_AspectFrame;
  Gtk::DrawingArea m_DrawingArea;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</pre><p>
</p><p>File: examplewindow.cc
</p><pre class="programlisting">
#include "examplewindow.h"

ExampleWindow::ExampleWindow()
: m_AspectFrame("2x1", /* label */
    Gtk::ALIGN_CENTER, /* center x */
    Gtk::ALIGN_CENTER, /* center y */
    2.0, /* xsize/ysize = 2 */
    false /* ignore child's aspect */)
{
  set_title("Aspect Frame");
  set_border_width(10);

  // Add a child widget to the aspect frame */
  // Ask for a 200x200 window, but the AspectFrame will give us a 200x100
  // window since we are forcing a 2x1 aspect ratio */
  m_DrawingArea.set_size_request(200, 200);
  m_AspectFrame.add(m_DrawingArea);

  // Add the aspect frame to our toplevel window:
  add(m_AspectFrame);

  show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}


</pre><p>
</p><p>File: main.cc
</p><pre class="programlisting">
#include &lt;gtkmm/main.h&gt;
#include "examplewindow.h"

int main(int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);

  ExampleWindow window;
  Gtk::Main::run(window); //Shows the window and returns when it is closed.

  return 0;
}
</pre><p>
</p></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sec-Alignment"></a>Alignment</h3></div></div><div></div></div><p>
The <tt class="literal">Alignment</tt> widget allows you to place a widget at
a position and size relative to the size of the <tt class="literal">Alignment</tt> widget
itself.  For instance, it might be used to center a widget.
</p><p>
You need to specify the <tt class="literal">Alignment</tt>'s characteristics to the constructor, or to the <tt class="literal">set()</tt> method. In particular, you won't notice much effect unless you specify a number other than 1.0 for the <tt class="literal">xscale</tt> and <tt class="literal">yscale</tt> parameters, because 1.0 simply means that the child widget will expand to fill all available space.
</p><p><a href="../../reference/html/classGtk_1_1Alignment.html" target="_top">Reference</a></p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2507220"></a>Example</h4></div></div><div></div></div><p>
This example right-aligns a button in a window by using an <tt class="literal">Alignment</tt> widget.
</p><div class="figure"><a name="figure-alignment"></a><p class="title"><b>Figure7.4.Alignment</b></p><div class="screenshot"><div><img src="../figures/alignment.png" alt="Alignment"></div></div></div><p><a href="../../../examples/book/alignment" target="_top">Source Code</a></p><p>File: examplewindow.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow();
  virtual ~ExampleWindow();

protected:
  //Signal handlers:
  virtual void on_button_clicked();

  //Child widgets:
  Gtk::Alignment m_Alignment;
  Gtk::Button m_Button;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</pre><p>
</p><p>File: examplewindow.cc
</p><pre class="programlisting">
#include "examplewindow.h"

ExampleWindow::ExampleWindow()
: m_Alignment(Gtk::ALIGN_RIGHT, Gtk::ALIGN_CENTER, 0.0, 0.0),
  m_Button("Close")
{
  set_title("Gtk::Alignement");
  set_border_width(10);
  set_default_size(200, 50);

  add(m_Alignment);

  m_Alignment.add(m_Button);

  m_Button.signal_clicked().connect( SigC::slot(*this, &amp;ExampleWindow::on_button_clicked) );

  show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_button_clicked()
{
  hide();
}


</pre><p>
</p><p>File: main.cc
</p><pre class="programlisting">
#include &lt;gtkmm/main.h&gt;
#include "examplewindow.h"

int main(int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);

  ExampleWindow window;
  Gtk::Main::run(window); //Shows the window and returns when it is closed.

  return 0;
}
</pre><p>
</p><p>
See the <a href="ch06s05.html" title="ProgressBar">ProgressBar</a> section for another example that uses an <tt class="literal">Alignment</tt>.</p></div></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch06s06.html">Prev</a></td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"><a accesskey="n" href="ch07s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Tooltips</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">Multiple-item widgets </td></tr></table></div></body></html>