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 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Multiple-item 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="ch07.html" title="Chapter7.Container Widgets"><link rel="previous" href="ch07.html" title="Chapter7.Container Widgets"><link rel="next" href="ch08.html" title="Chapter8.The TreeView widget"></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">Multiple-item widgets </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch07.html">Prev</a></td><th width="60%" align="center">Chapter7.Container Widgets</th><td width="20%" align="right"><a accesskey="n" href="ch08.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="sec-MultiItemWidgets"></a>Multiple-item widgets </h2></div></div><div></div></div><p>
Multiple-item widgets inherit from <tt class="literal">Gtk::Container</tt>; just as
with <tt class="literal">Gtk::Bin</tt>, you use the <tt class="literal">add()</tt> and <tt class="literal">remove()</tt> methods
to add and remove contained widgets. Unlike <tt class="literal">Gtk::Bin::remove()</tt>,
however, the <tt class="literal">remove()</tt> method for <tt class="literal">Gtk::Container</tt> takes an
argument, specifiying which widget to remove.
</p><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="id2507422"></a>Packing</h3></div></div><div></div></div><p>
You've probably noticed that gtkmm windows seem "elastic" - they can usually be stretched in many different ways. This is due to the <span class="emphasis"><em>widget packing</em></span>
system.
</p><p>
Many GUI toolkits require you to precisely place widgets in a window, using absolute positioning, often using a visual editor. This leads to several problems:
</p><p>
</p><div class="itemizedlist"><ul type="disc"><li><p>The widgets don't rearrange themselves when the window is resized. Some widgets are hidden when the window is made smaller, and lots of useless space appears when the window is made larger.</p></li><li><p>It's impossible to predict the amount of space necessary for text after it has been translated to other languages, or displayed in a different font. On Unix it is also impossible to anticipate the effects of every theme and window manager.</p></li><li><p>
Changing the layout of a window "on the fly", to make some extra widgets appear, for instance, is complex. It requires tedious recalculation of every widget's position.</p></li></ul></div><p>
</p><p>
gtkmm uses the packing system to solve these problems. Rather than specifying the position and size of each widget in the window,
you can arrange your widgets in rows, columns,
and/or tables. gtkmm can size your window automatically, based on the
sizes of the widgets it contains. And the sizes of the widgets are, in turn, determined by the amount of text they contain, or the minimum and maximum sizes that you specify, and/or how you have requested that the available space should be shared between sets of widgets.
You can perfect your layout by
specifying padding distance and centering values for each of your widgets. gtkmm then uses
all this information to resize and reposition everything sensibly and smoothly when the user manipulates the window. </p><p>
gtkmm arranges widgets hierarchically, using <span class="emphasis"><em>containers</em></span>. A
Container widget contains other widgets. Most gtkmm widgets
are containers. Windows, Notebook tabs, and Buttons are all
container widgets. There are two flavours of containers:
single-child containers, which are all descendants of <tt class="literal">Gtk::Bin</tt>,
and multiple-child containers, which are descendants of
<tt class="literal">Gtk::Container</tt>. Most widgets in gtkmm are descendants of
<tt class="literal">Gtk::Bin</tt>, including <tt class="literal">Gtk::Window</tt>.
</p><p>
Yes, that's correct: a Window can contain at most one widget.
How, then, can we use a window for anything useful? By placing a
multiple-child container in the window. The most useful container widgets are Gtk:VBox, Gtk::HBox, and Gtk::Table:
</p><p>
</p><div class="itemizedlist"><ul type="disc"><li><p>
<tt class="literal">Gtk::VBox</tt> and <tt class="literal">Gtk::HBox</tt> arrange their child widgets vertically and horizontally, respectively. Use pack_start() and pack_end() to insert child widgets.
</p></li><li><p>
<tt class="literal">Gtk::Table</tt> arranges its widgets in a grid. Use attach() to insert widgets.
</p></li></ul></div><p>
</p><p>
There are several other containers, which we will also discuss.
</p><p>
If you've never used a packing toolkit before, it can take some
getting used to. You'll probably find, however, that you don't
need to rely on visual form editors quite as much as you might with
other toolkits.
</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sec-helloworld2"></a>An improved Hello World</h3></div></div><div></div></div><p>
Let's take a look at a slightly improved <tt class="literal">helloworld</tt>, showing what we've learnt.
</p><div class="figure"><a name="figure-helloworld2"></a><p class="title"><b>Figure7.5.Hello World 2</b></p><div class="screenshot"><div><img src="../figures/helloworld2.png" alt="Hello World 2"></div></div></div><p><a href="../../../examples/book/helloworld2" target="_top">Source Code</a></p><p>File: helloworld.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLE_HELLOWORLD_H
#define GTKMM_EXAMPLE_HELLOWORLD_H
#include <gtkmm/button.h>
#include <gtkmm/box.h>
#include <gtkmm/window.h>
class HelloWorld : public Gtk::Window
{
public:
HelloWorld();
virtual ~HelloWorld();
protected:
// Signal handlers:
// Our new improved on_button_clicked(). (see below)
virtual void on_button_clicked(Glib::ustring data);
// Child widgets:
Gtk::HBox m_box1;
Gtk::Button m_button1, m_button2;
};
#endif // GTKMM_EXAMPLE_HELLOWORLD_H
</pre><p>
</p><p>File: helloworld.cc
</p><pre class="programlisting">
#include "helloworld.h"
#include <iostream>
HelloWorld::HelloWorld()
: m_button1("Button 1"),
m_button2("Button 2")
{
// this is a new call, this just sets the title of our new window to
// "Hello Buttons!"
set_title("Hello Buttons!");
// sets the border width of the window.
set_border_width(10);
// put the box into the main window.
add(m_box1);
// Now when the button is clicked, we call the "on_button_clicked" function
// with a pointer to "button 1" as it's argument
m_button1.signal_clicked().connect(SigC::bind<Glib::ustring>( SigC::slot(*this, &HelloWorld::on_button_clicked), "button 1") );
// instead of gtk_container_add, we pack this button into the invisible
// box, which has been packed into the window.
// note that the pack_start default arguments are Gtk::EXPAND | Gtk::FILL, 0
m_box1.pack_start(m_button1);
// always remember this step, this tells GTK that our preparation
// for this button is complete, and it can be displayed now.
m_button1.show();
// call the same signal handler with a different argument,
// passing a pointer to "button 2" instead.
m_button2.signal_clicked().connect(SigC::bind<Glib::ustring>( SigC::slot(*this, &HelloWorld::on_button_clicked), "button 2") );
m_box1.pack_start(m_button2);
// Show the widgets.
// They will not really be shown until this Window is shown.
m_button2.show();
m_box1.show();
}
HelloWorld::~HelloWorld()
{
}
// Our new improved signal handler. The data passed to this method is
// printed to stdout.
void HelloWorld::on_button_clicked(Glib::ustring data)
{
std::cout << "Hello World - " << data << " was pressed" << std::endl;
}
</pre><p>
</p><p>File: main.cc
</p><pre class="programlisting">
#include <gtkmm/main.h>
#include "helloworld.h"
int main (int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
HelloWorld helloworld;
Gtk::Main::run(helloworld); //Shows the window and returns when it is closed.
return 0;
}
</pre><p>
</p><p>
After building and running this program, try resizing the window to see the behaviour. Also, try
playing with the options to <tt class="literal">pack_start()</tt> while reading the <a href="ch07s02.html#sec-Boxes" title="Boxes">Boxes</a> section.
</p></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sec-stl-style"></a>STL-style APIs</h3></div></div><div></div></div><p>
TODO: Use 'Standard Library' instead of STL.
If you're an accomplished C++ programmer, you'll be happy to hear that
most of the gtkmm <tt class="literal">Container</tt> widgets provide STL-style APIs, available via accessor methods, such as <tt class="literal">Gtk::Box::children()</tt> or <tt class="literal">Gtk::Notebook::pages()</tt>. They don't
use actual STL containers (there are good reasons for this), but they look, feel, and act much like STL
container classes.
</p><p>These APIs are so similar to STL container APIs that, rather than explaining them in detail, we can refer
you to the STL documentation for most of their methods. This is all part of gtkmm's policy of reusing existing standards.
</p><p>However, STL-style APIs can require awkward or lengthy code in some situations, so some people prefer not to use them, while other people use them religiously. Therefore, you are not forced to use them - most container widgets have a simpler non-STL-style API, with methods such as <tt class="literal">append()</tt> and <tt class="literal">prepend()</tt>.
</p><p>
At a minimum, gtkmm container lists support iterators and the usual insertion, deletion, and addition methods. You can
always expect the following methods to be available for gtkmm STL-style APIs:
</p><div class="itemizedlist"><ul type="disc"><li><p>
<tt class="literal">begin()</tt> returns a <tt class="literal">begin</tt> iterator
</p></li><li><p>
<tt class="literal">end()</tt> returns an <tt class="literal">end</tt> iterator
</p></li><li><p>
<tt class="literal">rbegin()</tt> returns a reverse <tt class="literal">begin</tt> iterator
</p></li><li><p>
<tt class="literal">rend()</tt> returns a reverse <tt class="literal">end</tt> iterator
</p></li><li><p>
<tt class="literal">size()</tt>
</p></li><li><p>
<tt class="literal">max_size()</tt>
</p></li><li><p>
<tt class="literal">empty()</tt>
</p></li><li><p>
<tt class="literal">insert()</tt>
</p></li><li><p>
<tt class="literal">push_front()</tt>
</p></li><li><p>
<tt class="literal">push_back()</tt>
</p></li><li><p>
<tt class="literal">pop_front()</tt>
</p></li><li><p>
<tt class="literal">pop_back()</tt>
</p></li><li><p>
<tt class="literal">clear()</tt>
</p></li><li><p>
<tt class="literal">erase()</tt>
</p></li><li><p>
<tt class="literal">remove()</tt>
</p></li><li><p>
<tt class="literal">find()</tt>
</p></li><li><p>
<tt class="literal">front()</tt>
</p></li><li><p>
<tt class="literal">back()</tt>
</p></li></ul></div><p>
</p><p>
Also, the <tt class="literal">[]</tt> operator is overloaded, but that is usually order
N, so if performance is a consideration, or the list has a large
number of elements, think carefully before using it.
</p><p>
The element objects and list objects are defined, for each container,
in a namespace whose name ends in <tt class="literal">_Helpers</tt>. For example, the
helper namespace for the notebook widget is
<tt class="literal">Gtk::Notebook_Helpers</tt>.</p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2508111"></a>Adding items</h4></div></div><div></div></div><p>
There is a major difference between gtkmm STL-style APIs and
real STL containers. Normally, when you use a <tt class="literal">std::vector</tt>, for example,
you expect that whatever you put in, you'll get out, unmodified. You
wouldn't make a <tt class="literal">std::vector<int></tt> and expect to get <tt class="literal">double</tt>s
out of it. But, gtkmm STL-style APIs don't always work like
that - you will often put one kind of object in, and later get a different kind out. Why this odd
behaviour?
</p><p>
Consider a menu widget, which must maintain a hierarchical list of
menus and menu items. Menus can only contain certain objects, such as menu items, separators,
and submenus. To ensure consistency, a "filter" is needed to
keep out illegal objects. Also, since only a few types of objects are
allowed, convenience methods can be provided to make it easy to
build up menus.
</p><p>
gtkmm takes care of both requirements using special
<span class="emphasis"><em>helper elements</em></span>. Helper elements are
temporary - they're typically constructed and passed to an insertion method in the same call.
The list insertion method uses the information in the helper element to construct the real
object, which is then inserted into the container.
</p><p>
As an example, let's look at the <tt class="literal">Notebook</tt> widget (explained in the
section on <a href="ch07s02.html#sec-Notebook" title="Notebook">Notebooks</a>). Notebook widgets contain a series of "pages".
</p><p>
Each page in a notebook requires, at minimum, the following
information:
</p><p>
</p><div class="itemizedlist"><ul type="disc"><li><p>
A child widget (zero or one), to be placed in the page
</p></li><li><p>
A label for the page's tab
</p></li></ul></div><p>
</p><p>
(The gtkmm notebook widget keeps other data for each page as well.)
</p><p>
To insert a new page in a notebook, we can use one of the notebook
helper classes, like this:
</p><pre class="programlisting">
notebook->pages().push_back(
Gtk::Notebook_Helpers::TabElem(*frame, bufferl));
</pre><p>
</p><p>
Let's see what's
going on here. Assume we have a pointer to a Notebook widget called
<tt class="literal">notebook</tt>; we go from that to a member method called
<tt class="literal">pages()</tt>, which returns an STL-like list object. On this we call
the method <tt class="literal">push_back()</tt> (this should be familiar to those who know STL).
</p><p>
The object that the <tt class="literal">pages()</tt> method returns is called a
<tt class="literal">Notebook_Helpers::PageList</tt>. It's one of the STL-like containers
that we keep referring to. Let's take a look at this class (this has been heavily edited for clarity; see
<tt class="literal"><gtkmm/notebook.h></tt> for the actual definition):
</p><p>
</p><pre class="programlisting">
namespace Notebook_Helpers
{
class PageList
{
public:
. . .
void push_back(const Element& e);
. . .
Page* operator[](size_type l);
};
};
</pre><p>
</p><p>
There are two important things to notice here:
</p><div class="itemizedlist"><ul type="disc"><li><p>
The <tt class="literal">push_back()</tt> method takes as argument an <tt class="literal">Element</tt>
object (helper);
</p></li><li><p>
The overloaded <tt class="literal">[]</tt> operator returns a pointer to a
<tt class="literal">Page</tt>.
</p></li></ul></div><p>
</p><p>
This scheme has some important advantages:
</p><p>
</p><div class="itemizedlist"><ul type="disc"><li><p>
We can provide as many different Helper objects as desired,
making it simple to construct complex widgets like Menus.
</p></li><li><p>
Construction of the actual objects can be delayed until an appropriate time. Sometimes we don't have enough information until later
with GTK+.
</p></li><li><p>
The definitions of the objects contained in the list can change;
their interfaces need not concern the programmer. For example, even
if the <tt class="literal">Page</tt> object changes drastically, the programmer need not
be concerned; the <tt class="literal">Element</tt>s need not change, and will continue to
work as expected.
</p></li><li><p>
New <tt class="literal">Element</tt> objects can be added at any time to support new
features, without breaking existing code.
</p></li></ul></div><p>
</p><p>
All multi-item containers have an <tt class="literal">Element</tt> object in their helper
namespaces, and usually there are additional classes
available (like <tt class="literal">TabElem</tt> and <tt class="literal">MenuElem</tt>) which derive from
<tt class="literal">Element</tt>. <tt class="literal">Element</tt> classes vary from container to container,
since each contains different kinds of objects.
</p><p>
It's very important to remember that <tt class="literal">Element</tt>s are
not "real" objects. They exist only temporarily, and they are
never stored in the container. They are used <span class="emphasis"><em>only</em></span> as
temporary "parameter-holders". Therefore, the following segment of
code is illegal:
</p><pre class="programlisting">
MenuElem* m = new MenuElem("hello");
m->right_justify();
items().push_back(*m);
</pre><p>
</p><p>
We constructed a new <tt class="literal">MenuElem</tt> helper object, and then
tried to invoke <tt class="literal">right_justify()</tt> on it before adding it to the
menu. The trouble is that there is no <tt class="literal">right_justify()</tt> method
in the <tt class="literal">MenuElem</tt> class. The correct way to accomplish this would
be:
</p><pre class="programlisting">
items().push_back(MenuElem("hello"));
items().back()->right_justify();
</pre><p>
</p><p>
Here, we've constructed a <tt class="literal">MenuElem</tt> and inserted it into the menu
by passing it to <tt class="literal">push_back()</tt>, causing the real menu item to
be created. We've then called <tt class="literal">right_justify()</tt> on the object
retrieved from the list. This is correct - the object retrieved from
the list is not a <tt class="literal">MenuElem</tt>, but a real <tt class="literal">MenuItem</tt>, and therefore supports the
<tt class="literal">right_justify()</tt> method as expected.
</p></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sec-Boxes"></a>Boxes</h3></div></div><div></div></div><p>
Most packing uses boxes as in the above example. These
are invisible containers into which we can pack our widgets. When
packing widgets into a horizontal box, the objects are inserted
horizontally from left to right or right to left depending on whether <tt class="literal">pack_start()</tt> or <tt class="literal">pack_end()</tt> is used. In a vertical box, widgets are packed from top to bottom or vice
versa. You may use any combination of boxes inside or beside other
boxes to create the desired effect.
</p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2508581"></a>Adding widgets</h4></div></div><div></div></div><div class="sect4" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id2508586"></a>Per-child packing options</h5></div></div><div></div></div><p>
The <tt class="literal">pack_start()</tt> and
<tt class="literal">pack_end()</tt> methods place widgets inside
these containers. The <tt class="literal">pack_start()</tt> method will start at
the top and work its way down in a <tt class="literal">VBox</tt>, or pack left to right in an
<tt class="literal">HBox</tt>. <tt class="literal">pack_end()</tt> will do the opposite, packing from
bottom to top in a <tt class="literal">VBox</tt>, or right to left in an <tt class="literal">HBox</tt>. Using these
methods allows us to right justify or left justify our widgets. We will
use <tt class="literal">pack_start()</tt> in most of our examples.
</p><p>
There are several options governing how widgets are be packed, and this can be confusing at first. If you have difficulties then it is sometimes a good idea to play with the <tt class="literal">glade</tt> GUI designer to see what is possible. You might even decide to use the <tt class="literal">libglademm</tt> API to load your GUI at runtime.
</p><p>
There are basically five
different styles, as shown in this picture:
</p><div class="figure"><a name="figure-box_packing1"></a><p class="title"><b>Figure7.6.Box Packing 1</b></p><div class="screenshot"><div><img src="../figures/box_packing1.png" alt="Box Packing 1"></div></div></div><p>
Each line contains one horizontal box (<tt class="literal">HBox</tt>) with several
buttons. Each of the buttons on a line is packed into the <tt class="literal">HBox</tt> with the same arguments to the
<tt class="literal">pack_start()</tt> method).
</p><p>
This is the declaration of the <tt class="literal">pack_start()</tt> method:
</p><pre class="programlisting">
void pack_start(Gtk::Widget& child, PackOptions options = PACK_EXPAND_WIDGET, guint padding = 0);
</pre><p>
</p><p>
The first argument is the widget you're packing. In our example these are all <tt class="literal">Buttons</tt>s.
</p><p>
The <tt class="literal">options</tt> argument can take one of these three options:
</p><div class="itemizedlist"><ul type="disc"><li><p><tt class="literal">PACK_SHRINK</tt>: Space is contracted to the child widget size. The widget will take up just-enough space and never expand.</p></li><li><p><tt class="literal">PACK_EXPAND_PADDING</tt>: Extra space is filled with padding. The widgets will be spaced out evenly, but their sizes won't change - there will be empty space between the widgets instead. </p></li><li><p><tt class="literal">PACK_EXPAND_WIDGET</tt>: Extra space is taken up by increasing the child widget size, without changing the amount of space between widgets.</p></li></ul></div><p>
</p><p>
The <tt class="literal">padding</tt> argument specifies the width of an extra border area to leave around the
packed widget.
</p><p>Instead of the <tt class="literal">pack_start()</tt> and <tt class="literal">pack_end()</tt> methods, you might prefer to use the STL-style API, available via the <tt class="literal">children</tt> method. See the <a href="ch07s02.html#sec-stl-style" title="STL-style APIs">STL-style APIs</a> section for more details.</p><p><a href="../../reference/html/classGtk_1_1Box.html" target="_top">Reference</a></p></div><div class="sect4" lang="en"><div class="titlepage"><div><div><h5 class="title"><a name="id2508854"></a>Per-container packing options</h5></div></div><div></div></div><p>
Here's the constructor for the box widgets:
</p><pre class="programlisting">
Gtk::Box(bool homogeneous = false, int spacing = 0);
</pre><p>
Passing <tt class="literal">true</tt> for <tt class="literal">homogeneous</tt> will cause all of the contained
widgets to be the same size. <tt class="literal">spacing</tt> is a (minimum) number of
pixels to leave between each widget.
</p><p>
What's the difference between spacing (set when the box is created)
and padding (set when elements are packed)? Spacing is added between
objects, and padding is added on either side of a widget. The following
figure should make it clearer:
</p><div class="figure"><a name="figure-box_packing2"></a><p class="title"><b>Figure7.7.Box Packing 2</b></p><div class="screenshot"><div><img src="../figures/box_packing2.png" alt="Box Packing 2"></div></div></div></div></div><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2508923"></a>Example</h4></div></div><div></div></div><p>
Here is the source code for the example that produced the screenshots above. When you run this example, provide a number between 1 and 3 as a command-line option, to see different packing options in use.</p><p><a href="../../../examples/book/box" target="_top">Source Code</a></p><p>File: examplewindow.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
#include <packbox.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow(int which);
virtual ~ExampleWindow();
protected:
//Signal handlers:
virtual void on_button_quit_clicked();
//Child widgets:
Gtk::Button m_button;
Gtk::VBox m_box1;
Gtk::HBox m_boxQuit;
Gtk::Button m_buttonQuit;
Gtk::Label m_Label1, m_Label2;
Gtk::HSeparator m_seperator1, m_seperator2, m_seperator3, m_seperator4, m_seperator5;
};
#endif //GTKMM_EXAMPLEWINDOW_H
</pre><p>
</p><p>File: packbox.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLE_PACKBOX_H
#define GTKMM_EXAMPLE_PACKBOX_H
#include <gtkmm.h>
class PackBox : public Gtk::HBox
{
public:
PackBox(bool homogeneous, int spacing, Gtk::PackOptions, int padding = 0);
virtual ~PackBox();
protected:
Gtk::Button m_button1, m_button2, m_button3;
Gtk::Button* m_pbutton4;
char padstr[80];
};
#endif //GTKMM_EXAMPLE_PACKBOX_H
</pre><p>
</p><p>File: examplewindow.cc
</p><pre class="programlisting">
#include <iostream>
#include "examplewindow.h"
ExampleWindow::ExampleWindow(int which)
: m_buttonQuit("Quit")
{
set_title("Gtk::Box example");
PackBox *pPackBox1, *pPackBox2, *pPackBox3, *pPackBox4, *pPackBox5;
switch(which)
{
case 1:
{
m_Label1.set_text("Gtk::HBox(false, 0);");
// Align the label to the left side. We'll discuss this function and
// others in the section on Widget Attributes.
m_Label1.set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_TOP);
// Pack the label into the vertical box (vbox box1). Remember that
// widgets added to a vbox will be packed one on top of the other in
// order.
m_box1.pack_start(m_Label1, Gtk::PACK_SHRINK);
// Create a PackBox - homogeneous = false, spacing = 0,
// options = Gtk::PACK_SHRINK, padding = 0
pPackBox1 = Gtk::manage(new PackBox(false, 0, Gtk::PACK_SHRINK));
m_box1.pack_start(*pPackBox1, Gtk::PACK_SHRINK);
// Create a PackBox - homogeneous = false, spacing = 0,
// options = Gtk::PACK_EXPAND_PADDING, padding = 0
pPackBox2 = Gtk::manage(new PackBox(false, 0, Gtk::PACK_EXPAND_PADDING));
m_box1.pack_start(*pPackBox2, Gtk::PACK_SHRINK);
// Create a PackBox - homogeneous = false, spacing = 0,
// options = Gtk::PACK_EXPAND_WIDGET, padding = 0
pPackBox3 = Gtk::manage(new PackBox(false, 0, Gtk::PACK_EXPAND_WIDGET));
m_box1.pack_start(*pPackBox3, Gtk::PACK_SHRINK);
// pack the separator into the vbox. Remember each of these
// widgets are being packed into a vbox, so they'll be stacked
// vertically.
m_box1.pack_start(m_seperator1, Gtk::PACK_SHRINK, 5);
// create another new label, and show it.
m_Label2.set_text("Gtk::HBox(true, 0);");
m_Label2.set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_TOP);
m_box1.pack_start(m_Label2, Gtk::PACK_SHRINK);
// Args are: homogeneous, spacing, options, padding
pPackBox4 = Gtk::manage(new PackBox(true, 0, Gtk::PACK_EXPAND_PADDING));
m_box1.pack_start(*pPackBox4, Gtk::PACK_SHRINK);
// Args are: homogeneous, spacing, options, padding
pPackBox5 = Gtk::manage(new PackBox(true, 0, Gtk::PACK_EXPAND_WIDGET));
m_box1.pack_start(*pPackBox5, Gtk::PACK_SHRINK);
m_box1.pack_start(m_seperator2, Gtk::PACK_SHRINK, 5);
break;
}
case 2:
{
m_Label1.set_text("Gtk::HBox(false, 10);");
m_Label1.set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_TOP);
m_box1.pack_start(m_Label1, Gtk::PACK_SHRINK);
pPackBox1 = Gtk::manage(new PackBox(false, 10, Gtk::PACK_EXPAND_PADDING));
m_box1.pack_start(*pPackBox1, Gtk::PACK_SHRINK);
pPackBox2 = Gtk::manage(new PackBox(false, 10, Gtk::PACK_EXPAND_WIDGET));
m_box1.pack_start(*pPackBox2, Gtk::PACK_SHRINK);
m_box1.pack_start(m_seperator1, Gtk::PACK_SHRINK, 5);
m_Label2.set_text("Gtk::HBox(false, 0);");
m_Label2.set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_TOP);
m_box1.pack_start(m_Label2, Gtk::PACK_SHRINK);
pPackBox3 = Gtk::manage(new PackBox(false, 0, Gtk::PACK_SHRINK, 10));
m_box1.pack_start(*pPackBox3, Gtk::PACK_SHRINK);
pPackBox4 = Gtk::manage(new PackBox(false, 0, Gtk::PACK_EXPAND_WIDGET, 10));
m_box1.pack_start(*pPackBox4, Gtk::PACK_SHRINK);
m_box1.pack_start(m_seperator2, Gtk::PACK_SHRINK, 5);
break;
}
case 3:
{
// This demonstrates the ability to use Gtk::Box::pack_end() to
// right justify widgets. First, we create a new box as before.
pPackBox1 = Gtk::manage(new PackBox(false, 0, Gtk::PACK_SHRINK));
// create the label that will be put at the end.
m_Label1.set_text("end");
// pack it using pack_end(), so it is put on the right side
// of the PackBox.
pPackBox1->pack_end(m_Label1, Gtk::PACK_SHRINK);
m_box1.pack_start(*pPackBox1, Gtk::PACK_SHRINK);
// this explicitly sets the separator to 400 pixels wide by 5 pixels
// high. This is so the hbox we created will also be 400 pixels wide,
// and the "end" label will be separated from the other labels in the
// hbox. Otherwise, all the widgets in the hbox would be packed as
// close together as possible.
m_seperator1.set_size_request(400, 5);
// pack the separator into ourselves
m_box1.pack_start(m_seperator1, Gtk::PACK_SHRINK, 5);
break;
}
default:
{
std::cerr << "Unexpected command-line option." << std::endl;
break;
}
}
// Connect the signal to hide the window:
m_buttonQuit.signal_clicked().connect( SigC::slot(*this, &ExampleWindow::on_button_quit_clicked) );
// pack the button into the quitbox.
// The last 2 arguments to Box::pack_start are: options, padding.
m_boxQuit.pack_start(m_buttonQuit, Gtk::PACK_EXPAND_PADDING);
m_box1.pack_start(m_boxQuit, Gtk::PACK_SHRINK);
// pack the vbox (box1) which now contains all our widgets, into the
// main window.
add(m_box1);
show_all_children();
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_button_quit_clicked()
{
hide();
}
</pre><p>
</p><p>File: main.cc
</p><pre class="programlisting">
#include <iostream>
#include <gtkmm/main.h>
#include "examplewindow.h"
int main(int argc, char *argv[])
{
Gtk::Main main_instance(argc, argv);
if(argc != 2)
{
std::cerr << "usage: packbox num, where num is 1, 2, or 3." << std::endl;
// this just does cleanup in GTK, and exits with an exit status of 1.
gtk_exit (1);
}
ExampleWindow window( atoi(argv[1]) );
Gtk::Main::run(window); //Shows the window and returns when it is closed.
return 0;
}
</pre><p>
</p><p>File: packbox.cc
</p><pre class="programlisting">
#include "packbox.h"
#include <cstdio> //For sprintf().
PackBox::PackBox(bool homogeneous, int spacing, Gtk::PackOptions options, int padding) :
Gtk::HBox(homogeneous, spacing),
m_button1("box.pack_start("),
m_button2("button,"),
m_button3((options == Gtk::PACK_SHRINK) ? "Gtk::PACK_SHRINK" :
((options == Gtk::PACK_EXPAND_PADDING) ? "Gtk::PACK_EXPAND_PADDING" : "Gtk::PACK_EXPAND_WIDGET"))
{
pack_start(m_button1, options, padding);
pack_start(m_button2, options, padding);
pack_start(m_button3, options, padding);
sprintf(padstr, "%d);", padding);
m_pbutton4 = new Gtk::Button(padstr);
pack_start(*m_pbutton4, options, padding);
}
PackBox::~PackBox()
{
delete m_pbutton4;
}
</pre><p>
</p></div></div><div class="sect2" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="sec-ButtonBoxes"></a>ButtonBoxes </h3></div></div><div></div></div><p>
Button boxes are a convenient way to quickly arrange a group of
buttons. They come in both horizontal (<tt class="literal">Gtk::HButtonBox</tt>) and
vertical (<tt class="literal">Gtk::VButtonBox</tt>) flavours. They are exactly
alike, except in name and orientation.
</p><p>
ButtonBoxes help to make applications appear consistent because they use standard settings, such as inter-button spacing and packing.
</p><p>
Buttons are added to a <tt class="literal">ButtonBox</tt> with the <tt class="literal">add()</tt> method.
</p><p>
Button boxes support several layout styles. The style can be retrieved
and changed using <tt class="literal">get_layout()</tt> and <tt class="literal">set_layout()</tt>.
</p><p><a href="../../reference/html/classGtk_1_1ButtonBox.html" target="_top">Reference</a></p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2509295"></a>Example</h4></div></div><div></div></div><div class="figure"><a name="figure-buttonbox"></a><p class="title"><b>Figure7.8.ButtonBox</b></p><div class="screenshot"><div><img src="../figures/buttonbox.png" alt="ButtonBox"></div></div></div><p><a href="../../../examples/book/buttonbox" target="_top">Source Code</a></p><p>File: examplebuttonbox.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLE_BUTTONBOX_H
#define GTKMM_EXAMPLE_BUTTONBOX_H
#include <gtkmm.h>
class ExampleButtonBox : public Gtk::Frame
{
public:
ExampleButtonBox(bool horizontal,
const Glib::ustring& title,
gint spacing,
Gtk::ButtonBoxStyle layout);
protected:
Gtk::Button m_Button_OK, m_Button_Cancel, m_Button_Help;
};
#endif //GTKMM_EXAMPLE_BUTTONBOX_H
</pre><p>
</p><p>File: examplewindow.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
//Signal handlers:
virtual void on_button_clicked();
//Child widgets:
Gtk::VBox m_VBox_Main, m_VBox;
Gtk::HBox m_HBox;
Gtk::Frame m_Frame_Horizontal, m_Frame_Vertical;
};
#endif //GTKMM_EXAMPLEWINDOW_H
</pre><p>
</p><p>File: examplebuttonbox.cc
</p><pre class="programlisting">
#include "examplebuttonbox.h"
ExampleButtonBox::ExampleButtonBox(bool horizontal,
const Glib::ustring& title,
gint spacing,
Gtk::ButtonBoxStyle layout)
: Gtk::Frame(title),
m_Button_OK("OK"),
m_Button_Cancel("Cancel"),
m_Button_Help("Help")
{
Gtk::ButtonBox* bbox = 0;
if(horizontal)
bbox = Gtk::manage( new Gtk::HButtonBox() );
else
bbox = Gtk::manage( new Gtk::VButtonBox() );
bbox->set_border_width(5);
add(*bbox);
/* Set the appearance of the Button Box */
bbox->set_layout(layout);
bbox->set_spacing(spacing);
bbox->add(m_Button_OK);
bbox->add(m_Button_Cancel);
bbox->add(m_Button_Help);
}
</pre><p>
</p><p>File: examplewindow.cc
</p><pre class="programlisting">
#include "examplewindow.h"
#include "examplebuttonbox.h"
ExampleWindow::ExampleWindow()
: m_Frame_Horizontal("Horizontal Button Boxes"),
m_Frame_Vertical("Vertical Button Boxes")
{
set_title("Gtk::ButtonBox");
add(m_VBox_Main);
m_VBox_Main.pack_start(m_Frame_Horizontal, Gtk::PACK_EXPAND_WIDGET, 10);
//The horizontal ButtonBoxes:
m_VBox.set_border_width(10);
m_Frame_Horizontal.add(m_VBox);
m_VBox.pack_start( *Gtk::manage( new ExampleButtonBox(true, "Spread (spacing 40)",
40, Gtk::BUTTONBOX_SPREAD)),
Gtk::PACK_EXPAND_WIDGET, 0);
m_VBox.pack_start( *Gtk::manage( new ExampleButtonBox(true, "Edge (spacing 30)",
30, Gtk::BUTTONBOX_EDGE)),
Gtk::PACK_EXPAND_WIDGET, 5);
m_VBox.pack_start( *Gtk::manage( new ExampleButtonBox(true, "Start (spacing 20)",
20, Gtk::BUTTONBOX_START)),
Gtk::PACK_EXPAND_WIDGET, 5);
m_VBox.pack_start( *Gtk::manage( new ExampleButtonBox(true, "end (spacing 10)",
10, Gtk::BUTTONBOX_END)),
Gtk::PACK_EXPAND_WIDGET, 5);
//The vertical ButtonBoxes:
m_VBox_Main.pack_start(m_Frame_Vertical, Gtk::PACK_EXPAND_WIDGET, 10);
m_HBox.set_border_width(10);
m_Frame_Vertical.add(m_HBox);
m_HBox.pack_start( *Gtk::manage( new ExampleButtonBox(false, "Spread (spacing 5)",
5, Gtk::BUTTONBOX_SPREAD)),
Gtk::PACK_EXPAND_WIDGET, 0);
m_HBox.pack_start( *Gtk::manage( new ExampleButtonBox(false, "Edge (spacing 30)",
30, Gtk::BUTTONBOX_EDGE)),
Gtk::PACK_EXPAND_WIDGET, 5);
m_HBox.pack_start( *Gtk::manage( new ExampleButtonBox(false, "Start (spacing 20)",
20, Gtk::BUTTONBOX_START)),
Gtk::PACK_EXPAND_WIDGET, 5);
m_HBox.pack_start( *Gtk::manage( new ExampleButtonBox(false, "End (spacing 10)",
10, Gtk::BUTTONBOX_END)),
Gtk::PACK_EXPAND_WIDGET, 5);
show_all_children();
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_button_clicked()
{
hide();
}
</pre><p>
</p><p>File: main.cc
</p><pre class="programlisting">
#include <gtkmm/main.h>
#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="id2509484"></a>Table</h3></div></div><div></div></div><p>
Tables allows us to place widgets in a grid.
</p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2509494"></a>Constructor</h4></div></div><div></div></div><p>
The grid's dimensions need to be specified in the constructor:
</p><pre class="programlisting">
Gtk::Table(int rows = 1, int columns = 1, bool homogeneous = false);
</pre><p>
</p><p>
The first argument is the number of rows to make in the table, while
the second, obviously, is the number of columns. If <tt class="literal">homogeneous</tt>
is <tt class="literal">true</tt>, the table cells will all be the same size
(the size of the largest widget in the table).
</p><p>
The rows and columns are indexed starting at 0. If you specify
<tt class="literal">rows</tt> = 2 and <tt class="literal">columns</tt> = 2, the layout would look something
like this:
</p><p>
</p><pre class="programlisting">
0 1 2
0+----------+----------+
| | |
1+----------+----------+
| | |
2+----------+----------+
</pre><p>
</p><p>
Note that the coordinate system starts in the upper left hand corner.
</p></div><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2509562"></a>Adding widgets</h4></div></div><div></div></div><p>
To place a widget into a box, use the following method:
</p><pre class="programlisting">
void Gtk::Table::attach(Gtk::Widget& child,
guint left_attach, guint right_attach,
guint top_attach, guint bottom_attach,
guint xoptions = Gtk::FILL | Gtk::EXPAND,
guint yoptions = Gtk::FILL | Gtk::EXPAND,
guint xpadding = 0, guint ypadding = 0);
</pre><p>
</p><p>
The first argument is the widget you wish to place in the table.
</p><p>
The <tt class="literal">left_attach</tt> and <tt class="literal">right_attach</tt> arguments specify where to
place the widget, and how many boxes to use. For example, if you want
a button in the lower-right cell of a 2 x 2 table, and want it to occupy
that cell <span class="emphasis"><em>only</em></span>, then <tt class="literal">left_attach</tt> would be 1,
<tt class="literal">right_attach</tt> 2, <tt class="literal">top_attach</tt> 1, and <tt class="literal">bottom_attach</tt> 2. If,
on the other hand, you wanted a widget to take up the entire top row
of our 2 x 2 table, you'd set <tt class="literal">left_attach</tt> = 0, <tt class="literal">right_attach</tt> =
2, <tt class="literal">top_attach</tt> = 0, and <tt class="literal">bottom_attach</tt> = 1.
</p><p>
<tt class="literal">xoptions</tt> and <tt class="literal">yoptions</tt> are used to specify packing options
and may be bitwise ORed together to allow multiple options.
These options are:
</p><p>
</p><div class="variablelist"><dl><dt><span class="term"><tt class="literal">Gtk::FILL</tt></span></dt><dd><p>
If the table box is larger than the widget, and
<tt class="literal">Gtk::FILL</tt> is specified, the widget will expand to use all the room available.
</p></dd><dt><span class="term"><tt class="literal">Gtk::SHRINK</tt></span></dt><dd><p>
If the table widget is allocated less
space than it requested (because the user resized the window),
then the widgets will normally just disappear off the bottom of the
window. If <tt class="literal">Gtk::SHRINK</tt> is specified, the widgets
will shrink with the table.
</p></dd><dt><span class="term"><tt class="literal">Gtk::EXPAND</tt></span></dt><dd><p>This will cause the table to expand to use up anyremaining space in the window.
</p></dd></dl></div><p>
</p><p>
The padding arguments work just as they do for <tt class="literal">pack_start()</tt>.
</p></div><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2509771"></a>Other methods</h4></div></div><div></div></div><p>
<tt class="literal">set_row_spacing()</tt> and
<tt class="literal">set_col_spacing()</tt> set the spacing between the rows at
the specified row or column. Note that for columns, the space goes to the right of the column, and for rows, the space goes below the row.
</p><p>
You can also set a consistent spacing for all rows and/or columns with <tt class="literal">set_row_spacings()</tt> and <tt class="literal">set_col_spacings()</tt>. Note that with these calls, the last row and last column do not get
any spacing.
</p><p><a href="../../reference/html/classGtk_1_1Table.html" target="_top">Reference</a></p></div><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2509821"></a>Example</h4></div></div><div></div></div><p>
In the following example, we make a window with three buttons in a 2 x 2
table. The first two buttons will be placed in the upper row. A
third button is placed in the lower row, spanning both columns.
</p><div class="figure"><a name="figure-table"></a><p class="title"><b>Figure7.9.Table</b></p><div class="screenshot"><div><img src="../figures/table.png" alt="Table"></div></div></div><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 <gtkmm.h>
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 <gtkmm/main.h>
#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-Notebook"></a>Notebook</h3></div></div><div></div></div><p>
A <tt class="literal">Notebook</tt> has set of stacked <tt class="literal">pages</tt>, each of which contains widgets. Labelled <tt class="literal">tabs</tt> allow the user to
select the pages. <tt class="literal">Notebooks</tt> allow several sets of widgets to be placed in a small space, by only showing one page at a time. For instance, they are often used in preferences dialogs.
</p><p>Use the <tt class="literal">append_page()</tt>, <tt class="literal">prepend_page()</tt> and <tt class="literal">insert_page()</tt> methods to add tabbed pages to the <tt class="literal">Notebook</tt>, supplying the child widget and the name for the tab.</p><p>To discover the currently visible page, use the <tt class="literal">get_current_page()</tt> method. This returns the page number, and then calling <tt class="literal">get_nth_page()</tt> with that number will give you a pointer to the actual child widget.</p><p>To programmatically change the selected page, use the <tt class="literal">set_page()</tt> method.</p><p>There is also an <a href="ch07s02.html#sec-Notebook-STL-style" title="STL-style API">STL-style API</a> which you might find more obvious.</p><p><a href="../../reference/html/classGtk_1_1Notebook.html" target="_top">Reference</a></p><div class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="id2510077"></a>Example</h4></div></div><div></div></div><div class="figure"><a name="figure-notebook"></a><p class="title"><b>Figure7.10.Notebook</b></p><div class="screenshot"><div><img src="../figures/notebook.png" alt="Notebook"></div></div></div><p><a href="../../../examples/book/notebook/" target="_top">Source Code</a></p><p>File: examplewindow.h
</p><pre class="programlisting">
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
//Signal handlers:
virtual void on_button_quit();
//Child widgets:
Gtk::VBox m_VBox;
Gtk::Notebook m_Notebook;
Gtk::Label m_Label1, m_Label2;
Gtk::HButtonBox m_ButtonBox;
Gtk::Button m_Button_Quit;
};
#endif //GTKMM_EXAMPLEWINDOW_H
</pre><p>
</p><p>File: examplewindow.cc
</p><pre class="programlisting">
#include <iostream>
#include "examplewindow.h"
ExampleWindow::ExampleWindow()
: m_Label1("Contents of tab 1"),
m_Label2("Contents of tab 2"),
m_Button_Quit("Quit")
{
set_title("Gtk::Notebook example");
set_border_width(10);
set_default_size(400, 200);
add(m_VBox);
//Add the Notebook, with the button underneath:
m_Notebook.set_border_width(10);
m_VBox.pack_start(m_Notebook);
m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);
m_ButtonBox.pack_start(m_Button_Quit, Gtk::PACK_SHRINK);
m_Button_Quit.signal_clicked().connect( SigC::slot(*this, &ExampleWindow::on_button_quit) );
//Add the Notebook pages:
m_Notebook.append_page(m_Label1, "First");
m_Notebook.append_page(m_Label2, "Second");
show_all_children();
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_button_quit()
{
hide();
}
</pre><p>
</p><p>File: main.cc
</p><pre class="programlisting">
#include <gtkmm/main.h>
#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 class="sect3" lang="en"><div class="titlepage"><div><div><h4 class="title"><a name="sec-Notebook-STL-style"></a>STL-style API</h4></div></div><div></div></div><p>
The <tt class="literal">Gtk::Notebook</tt> widget has an STL-style API, available via the <tt class="literal">pages()</tt> method, which you might prefer to use to add and access pages. See the <a href="ch07s02.html#sec-stl-style" title="STL-style APIs">STL-style APIs</a> section for generic information.</p><p><a href="../../reference/html/classGtk_1_1Notebook__Helpers_1_1PageList.html" target="_top">PageList Reference</a></p><p>
To insert pages into a notebook, use the <tt class="literal">TabElem</tt> helper class, like so:
</p><pre class="programlisting">
m_Notebook.pages().push_back( Gtk::Notebook_Helpers::TabElem(m_ChildWidget, "tab 1") );
</pre><p>
</p><p><a href="../../reference/html/classGtk_1_1Notebook__Helpers_1_1TabElem.html" target="_top">TabElem Reference</a>. TODO: Correct URL.</p><p>To access an existing child widget, you can call <tt class="literal">get_child()</tt> on one of the <tt class="literal">Page</tt> elements of the <tt class="literal">PageList</tt>:
</p><pre class="programlisting">
Gtk::Widget* pWidget = m_Notebook.pages()[2].get_child();
</pre><p>
</p></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch07.html">Prev</a></td><td width="20%" align="center"><a accesskey="u" href="ch07.html">Up</a></td><td width="40%" align="right"><a accesskey="n" href="ch08.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter7.Container Widgets</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">Chapter8.The TreeView widget</td></tr></table></div></body></html>
|