File: zh_CN.mo

package info (click to toggle)
gtkmm-documentation 3.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 18,628 kB
  • ctags: 2,376
  • sloc: cpp: 12,615; sh: 1,004; makefile: 808; perl: 57
file content (187 lines) | stat: -rw-r--r-- 78,859 bytes parent folder | download | duplicates (4)
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
\7x%ycQU4na/'Yc-F !#:$$%:&'w)R++,-M.//0s13]5j6Vn67898
:s:=M>	@!(@J@	CDxD6E?EFEXNEFF FFGG 3G.TGG
GGGGGG0G#G$ H:EH>HH@HJeJ8KAKL
LL;L7MVMqMMMMM/MM'NOjxQRSSSTT#T,T4T<T
TUUUUUUVVV=}W2YM[p<]]9^w^cN___uab3	b=b+Cbobgdayd1d
e
e(e
4eBe	fgh	h
'h5hLGhh(i0i?i;kGkkcmR%nxnr
oV}p7rCsuPttuDv5v2xcDy	yNyzkz|H0}9y~~z|SЁrnˆi؆NB~*5,`(V9[Ғ&Mŗ#)1PQ>16<
9a֟82L
D%cQ~Ф4*YW08(iHZx2

ۺf!CbdbMfewXt5	aS#w~"5Phu}$'*IdP`p
;7Hc{4>oKs
	GnPk
YQ3US}c-7R_!(	)>?Z"2	N/q79Ex)$-	Z
o
av

Av(
?&tR4Pc?N.;Q=.!-OB+E7}N!"#%`r'G(*(*.*6*P+V,-->	.H.M.S.<00
090a1{121
1111B3,9hbeYrR&S_@#B.c~ln{52"gfpGL)%T=iW30<1[-A!|D
N	
xkJm`uq*>vw/dyjIHOs7VU6CazMZ4?\];'KE^+QP}8o:XtF($ 
# ./configure
# make
# make install

Gtk::Button* button = new Gtk::Button("example");
gtk_button_do_something_new(button-&gt;gobj());

GtkButton* cbutton = get_a_button();
Gtk::Button* button = Glib::wrap(cbutton);
#include &lt;gtkmm.h&gt;2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010<application>gtkmm</application> allows you to write code using normal C++ techniques such as encapsulation, derivation, and polymorphism. As a C++ programmer you probably already realise that this leads to clearer and better organized code.<application>gtkmm</application> applications consist of windows containing widgets, such as buttons and text boxes. In some other systems, widgets are called "controls". For each widget in your application's windows, there is a C++ object in your application's code. So you just need to call a method of the widget's class to affect the visible widget.<application>gtkmm</application> compared to Qt<application>gtkmm</application> developers tend to prefer <application>gtkmm</application> to Qt because <application>gtkmm</application> does things in a more C++ way. Qt originates from a time when C++ and the standard library were not standardised or well supported by compilers. It therefore duplicates a lot of stuff that is now in the standard library, such as containers and type information. Most significantly, Trolltech modified the C++ language to provide signals, so that Qt classes cannot be used easily with non-Qt classes. <application>gtkmm</application> was able to use standard C++ to provide signals without changing the C++ language. See the FAQ for more detailed differences.<application>gtkmm</application> involves less code compared to GTK+, which uses prefixed function names and lots of cast macros.<application>gtkmm</application> is a C++ wrapper for <ulink url="http://www.gtk.org/">GTK+</ulink>, a library used to create graphical user interfaces. It is licensed using the LGPL license, so you can develop open software, free software, or even commercial non-free software using <application>gtkmm</application> without purchasing licenses.<application>gtkmm</application> is a wrapper<application>gtkmm</application> is more type-safe, so the compiler can detect errors that would only be detected at run time when using C. This use of specific types also makes the API clearer because you can see what types should be used just by looking at a method's declaration.<application>gtkmm</application> is not a native C++ toolkit, but a C++ wrapper of a C toolkit. This separation of interface and implementation has advantages. The <application>gtkmm</application> developers spend most of their time talking about how <application>gtkmm</application> can present the clearest API, without awkward compromises due to obscure technical details. We contribute a little to the underlying GTK+ code base, but so do the C coders, and the Perl coders and the Python coders, etc. Therefore GTK+ benefits from a broader user base than language-specific toolkits - there are more implementers, more developers, more testers, and more users.<application>gtkmm</application> provides four basic types of buttons:<application>gtkmm</application> uses the libsigc++ library to implement signals. Here is an example line of code that connects a Gtk::Button's "clicked" signal with a signal handler called "on_button_clicked": <placeholder-1/><application>gtkmm</application> was originally named gtk-- because GTK+ already has a + in the name. However, as -- is not easily indexed by search engines the package generally went by the name <application>gtkmm</application>, and that's what we stuck with.<application>gtkmm</application>, like most GUI toolkits, is <emphasis>event-driven</emphasis>. When an event occurs, such as the press of a mouse button, the appropriate signal will be <emphasis>emitted</emphasis> by the Widget that was pressed. Each Widget has a different set of signals that it can emit. To make a button click result in an action, we set up a <emphasis>signal handler</emphasis> to catch the button's "clicked" signal.<classname>Glib::ListHandle&lt;Gtk::Widget*&gt;</classname>: Use <classname>std::vector&lt;Gtk::Widget*&gt;</classname>, <classname>std::list&lt;Gtk::Widget*&gt;</classname>, etc.<classname>Glib::SListHandle&lt;Gtk::Widget*&gt;</classname>: Use <classname>std::vector&lt;Gtk::Widget*&gt;</classname>, <classname>std::list&lt;Gtk::Widget*&gt;</classname>, etc.<classname>Glib::StringArrayHandle</classname> or <classname>Glib::ArrayHandle&lt;Glib::ustring&gt;</classname>: Use <classname>std::vector&lt;Glib::ustring&gt;</classname>, <classname>std::list&lt;Glib::ustring&gt;</classname>, <type>const char*[]</type>, etc.<classname>Gtk::Button</classname> is also a container so you could put any other widget, such as a <classname>Gtk::Image</classname> into it.<classname>Gtk::CheckButton</classname> inherits from <classname>Gtk::ToggleButton</classname>. The only real difference between the two is <classname>Gtk::CheckButton</classname>'s appearance. You can check, set, and toggle a checkbox using the same member methods as for <classname>Gtk::ToggleButton</classname>.<classname>Gtk::Scale</classname> and <classname>Gtk::Scrollbar</classname> both inherit from <classname>Gtk::Range</classname> and share much functionality. They contain a "trough" and a "slider" (sometimes called a "thumbwheel" in other GUI environments). Dragging the slider with the pointer moves it within the trough, while clicking in the trough advances the slider towards the location of the click, either completely, or by a designated amount, depending on which mouse button is used. This should be familiar scrollbar behaviour.<classname>Gtk::Scale</classname> widgets (or "sliders") allow the user to visually select and manipulate a value within a specific range. You might use one, for instance, to adjust the magnification level on a zoomed preview of a picture, or to control the brightness of a colour, or to specify the number of minutes of inactivity before a screensaver takes over the screen.<classname>Gtk::ToggleButton</classname> is most useful as a base class for the <classname>Gtk::CheckButton</classname> and <classname>Gtk::RadioButton</classname> classes.<classname>RadioButtons</classname> are "off" when created; this means that when you first make a group of them, they will all be off. Don't forget to turn one of them on using <methodname>set_active()</methodname>:<classname>Scale</classname> widgets can display their current value as a number next to the trough. By default they show the value, but you can change this with the <methodname>set_draw_value()</methodname> method.<classname>ToggleButton</classname>s are like normal <classname>Button</classname>s, but when clicked they remain activated, or pressed, until clicked again.<literal>Gtk::UPDATE_CONTINUOUS</literal> - This is the default. The <literal>value_changed</literal> signal is emitted continuously, i.e. whenever the slider is moved by even the tiniest amount.<literal>Gtk::UPDATE_DELAYED</literal> - The <literal>value_changed</literal> signal is emitted when the user releases the mouse button, or if the slider stops moving for a short period of time.<literal>Gtk::UPDATE_DISCONTINUOUS</literal> - The <literal>value_changed</literal> signal is only emitted once the slider has stopped moving and the user has released the mouse button.<ulink url="http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1Button.html"><classname>Gtk::Button</classname></ulink>. Standard buttons, usually marked with a label or picture. Pushing one triggers an action. See the <link linkend="sec-pushbuttons">Button</link> section.<ulink url="http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1CheckButton.html"><classname>Gtk::CheckButton</classname></ulink>. These act like ToggleButtons, but show their state in small squares, with their label at the side. They should be used in most situations which require an on/off setting. See the <link linkend="sec-checkboxes">CheckBox</link> section.<ulink url="http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1RadioButton.html"><classname>Gtk::RadioButton</classname></ulink>. Named after the station selectors on old car radios, these buttons are used in groups for options which are mutually exclusive. Pressing one causes all the others in its group to turn off. They are similar to CheckBoxes (a small widget with a label at the side), but usually look different. See the <link linkend="sec-radio-buttons">RadioButton</link> section.<ulink url="http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1ToggleButton.html"><classname>Gtk::ToggleButton</classname></ulink>. Unlike a normal Button, which springs back up, a ToggleButton stays down until you press it again. It might be useful as an on/off switch. See the <link linkend="sec-toggle-buttons">ToggleButton</link> section.ATKAfter putting the source code in <literal>simple.cc</literal> you can compile the above program with gcc using: <placeholder-1/> Note that you must surround the <literal>pkg-config</literal> invocation with backquotes. Backquotes cause the shell to execute the command inside them, and to use the command's output as part of the command line.After you've installed all of the dependencies, download the <application>gtkmm</application> source code, unpack it, and change to the newly created directory. <application>gtkmm</application> can be built and installed with the following sequence of commands:AlanAll <application>gtkmm</application> programs must include certain <application>gtkmm</application> headers; <literal>gtkmm.h</literal> includes the entire <application>gtkmm</application> kit. This is usually not a good idea, because it includes a megabyte or so of headers, but for simple programs, it suffices.Also, the value can be drawn in different positions relative to the trough, specified by the <methodname>set_value_pos()</methodname> method.Although <application>gtkmm</application> widget instances have lifetimes and scopes just like those of other C++ classes, <application>gtkmm</application> has an optional time-saving feature that you will see in some of the examples. <function>Gtk::manage()</function> allows you to say that a child widget is owned by the container into which you place it. This allows you to <function>new</function> the widget, add it to the container and forget about deleting it. You can learn more about <application>gtkmm</application> memory management techniques in the <link linkend="chapter-memory">Memory Management chapter</link>.Although we have shown the compilation command for the simple example, you really should use the automake and autoconf tools, as described in "Autoconf, Automake, Libtool", by G. V. Vaughan et al. The examples used in this book are included in the <application>gtkmm</application> package, with appropriate build files, so we won't show the build commands in future. You'll just need to find the appropriate directory and type <literal>make</literal>.Although you can specify the layout and appearance of windows and widgets with C++ code, you will probably find it more convenient to design your user interfaces with <literal>Glade</literal> and load them at runtime with <literal>Gtk::Builder</literal>. See the <link linkend="chapter-builder">Glade and Gtk::Builder</link> chapter.AnastasovAppendix on "Visual Studio 2005".As will be explained in the <link linkend="chapter-adjustment">Adjustment</link> section, all Range widgets are associated with a <classname>Adjustment</classname> object. To change the lower, upper, and current values used by the widget you need to use the methods of its <classname>Adjustment</classname>, which you can get with the <methodname>get_adjustment()</methodname> method. The <classname>Range</classname> widgets' default constructors create an <classname>Adjustment</classname> automatically, or you can specify an existing <classname>Adjustment</classname>, maybe to share it with another widget. See the <link linkend="chapter-adjustment">Adjustments</link> section for further details.As with <classname>Scrollbar</classname>s, there are separate widget types for horizontal and vertical widgets - <classname>Gtk::HScale</classname> and <classname>Gtk::VScale</classname>. The default constructors create an <classname>Adjustment</classname> with all of its values set to <literal>0.0</literal>. This isn't useful so you will need to set some <classname>Adjustment</classname> details to get meaningful behaviour.BasicsBefore attempting to install <application>gtkmm</application> 2.4, you might first need to install these other packages.BernhardButtonButtonsBy default, <application>gtkmm</application> will be installed under the <filename>/usr/local</filename> directory. On some systems you may need to install to a different location. For instance, on Red Hat Linux systems you might use the <literal>--prefix</literal> option with configure, like so: <screen>
# ./configure --prefix=/usr
</screen>CedricChapter on "Drawing Area".Chapter on "Drawing with Cairo".Chapter on "Printing".Chapter on "Recent Files".Chapter on "Timeouts".Chapter on "Win32 Installation".Chapter on "Working with gtkmm's Source Code".CheckButtonCheckboxesConstructorsCummingDanielDependenciesElstnerEmitted when the button is pressed and released.Emitted when the button is pressed.Emitted when the button is released.Emitted when the mouse pointer leaves the button's window.Emitted when the mouse pointer moves over the button's window.ExampleFirst we instantiate an object called <literal>kit</literal>. This is of type <classname>Gtk::Main</classname>. Every <application>gtkmm</application> program must have one of these. We pass our command-line arguments to its constructor. It takes the arguments it wants, and leaves you the rest, as we described earlier.For information about implementing your own signals rather than just connecting to the existing <application>gtkmm</application> signals, see the <link linkend="chapter-custom-signals">appendix</link>.For more detailed information about signals, see the <link linkend="chapter-signals">appendix</link>.GTK+ 2.4GTK+ and <application>gtkmm</application> were designed to work well with Microsoft Windows, and the developers encourage its use on the win32 platform. However, Windows has no standard installation system for development libraries. Please see the <ulink url="http://live.gnome.org/gtkmm/MSWindows">Windows Installation</ulink> page for Windows-specific installation instructions and notes.GeneGlib::ustringGroupsGtk::Button* pButton = new Gtk::Button("_Something", true);Gtk::Button* pButton = new Gtk::Button(Gtk::Stock::OK);Gtk::Main kit(argc, argv);Gtk::Main::run(window);Gtk::Window window;GustinHeaders and LinkingHello WorldHello World in <application>gtkmm</application>HelloWorld::HelloWorld()
:
  m_button ("Hello World")
{
  set_border_width(10);
  m_button.signal_clicked().connect(sigc::mem_fun(*this,
    &amp;HelloWorld::on_button_clicked));
  add(m_button);.
  m_button.show();
}However, this is even simpler when using the <function>PKG_CHECK_MODULES()</function> macro in a standard configure.ac file with autoconf and automake. For instance: <placeholder-1/> This checks for the presence of gtkmm and defines MYAPP_LIBS and MYAPP_CFLAGS for use in your Makefile.am files.If you want to help develop <application>gtkmm</application> or experiment with new features, you can also install <application>gtkmm</application> from git. Most users will never need to do this, but if you're interested in helping with <application>gtkmm</application> development, see the <link linkend="chapter-working-with-source">Working with gtkmm's Source Code</link> appendix.If your distribution does not provide a pre-built <application>gtkmm</application> package, or if you want to install a different version than the one provided by your distribution, you can also install <application>gtkmm</application> from source. The source code for <application>gtkmm</application> can be downloaded from <ulink url="http://www.gtkmm.org/"/>.Inheritance can be used to derive new widgets. The derivation of new widgets in GTK+ C code is so complicated and error prone that almost no C coders do it. As a C++ developer you know that derivation is an essential Object Orientated technique.InstallationInstalling From SourceIntermediate typesIntroductionJasonJonathonJongsmaLaursenLike checkboxes, radio buttons also inherit from <classname>Gtk::ToggleButton</classname>, but these work in groups, and only one RadioButton in a group can be selected at any one time.M'SadoquesMarkoMember instances can be used, simplifying memory management. All GTK+ C widgets are dealt with by use of pointers. As a C++ coder you know that pointers should be avoided where possible.MethodsMicrosoft WindowsMixing C and C++ APIsMost of the chapters in this book deal with specific widgets. See the <link linkend="chapter-container-widgets">Container Widgets</link> section for more details about adding widgets to container widgets.MurrayMurray CummingNext we call the Window's <methodname>set_border_width()</methodname> method. This sets the amount of space between the sides of the window and the widget it contains.Next we make an object of our <classname>HelloWorld</classname> class, whose constructor takes no arguments, but it isn't visible yet. When we call Gtk::Main::run(), giving it the helloworld Window, it shows the Window and starts the <application>gtkmm</application><emphasis>event loop</emphasis>. During the event loop <application>gtkmm</application> idles, waiting for actions from the user, and responding appropriately. When the user closes the Window, run() will return, causing the final line of our main() function be to executed. The application will then finish.Next, we use the Window's <methodname>add()</methodname> method to put <literal>m_button</literal> in the Window. (<methodname>add()</methodname> comes from <classname>Gtk::Container</classname>, which is described in the chapter on container widgets.) The <methodname>add()</methodname> method places the Widget in the Window, but it doesn't display the widget. <application>gtkmm</application> widgets are always invisible when you create them - to display them, you must call their <methodname>show()</methodname> method, which is what we do in the next line.Note that UTF-8 isn't compatible with 8-bit encodings like ISO-8859-1. For instance, German umlauts are not in the ASCII range and need more than 1 byte in the UTF-8 encoding. If your code contains 8-bit string literals, you have to convert them to UTF-8 (e.g. the Bavarian greeting "Grüß Gott" would be "Gr\xC3\xBC\xC3\x9F Gott").Note that if you mention extra modules in addition to gtkmm-2.4, they should be separated by spaces, not commas.Note that you can't just do <placeholder-1/> because the group is modified by <methodname>set_group()</methodname> and therefore non-const.Note that, due to GTK+'s theming system, the appearance of these widgets will vary. In the case of checkboxes and radio buttons, they may vary considerably.Notice that we've used an initialiser statement to give the <literal>m_button</literal> object the label "Hello World".Now let's look at our program's <function>main()</function> function. Here it is, without comments:OleOne of the benefits of UTF-8 is that you don't need to use it unless you want to, so you don't need to retrofit all of your code at once. <classname>std::string</classname> will still work for 7-bit ASCII strings. But when you try to localize your application for languages like Chinese, for instance, you will start to see strange errors, and possible crashes. Then all you need to do is start using <classname>Glib::ustring</classname> instead.Openismus has more <ulink url="http://www.openismus.com/documents/linux/automake/automake.shtml">basic help with automake and autoconf</ulink>.OttPKG_CHECK_MODULES([MYAPP], [gtkmm-2.4 &gt;= 2.8.0])PangoParts of chapter on "Internationalization".Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. You may obtain a copy of the GNU Free Documentation License from the Free Software Foundation by visiting their Web site or by writing to: Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.Prebuilt PackagesPretty thrilling, eh? Let's examine the code. First, the <classname>HelloWorld</classname> class:Programming with <application>gtkmm</application>Push-ButtonsRadio buttonsRadioButtonRange WidgetsRecent versions of <application>gtkmm</application> are packaged by nearly every major Linux distribution these days. So, if you use Linux, you can probably get started with <application>gtkmm</application> by installing the package from the official repository for your distribution. Distributions that include <application>gtkmm</application> in their repositories include Debian, Ubuntu, Red Hat, Fedora, Mandriva, Suse, and many others.ReferenceRemember that on a Unix or Linux operating system, you will probably need to be <literal>root</literal> to install software. The <command>su</command> command will allow you to enter the <literal>root</literal> password and have <literal>root</literal> status temporarily.RiederRuebsamenScale WidgetsScrollbar WidgetsSection "Build Structure" of chapter on "Wrapping C Libraries with gmmproc".See the <link linkend="chapter-internationalization">Internationalization</link> section for information about providing the UTF-8 string literals.SignalsSimple ExampleSome parts of the <application>gtkmm</application> API use intermediate data containers, such as <classname>Glib::StringArrayHandle</classname> instead of a specific Standard C++ container such as <classname>std::vector</classname> or <classname>std::list</classname>. You should not declare these types yourself -- you should use whatever Standard C++ container you prefer instead. <application>gtkmm</application> will do the conversion for you. Here are some of these intermediate types: <placeholder-1/>Source CodeThe <classname>Gtk::Button</classname> widget has the following signals, but most of the time you will just handle the <literal>clicked</literal> signal:The <emphasis>update policy</emphasis> of a <classname>Range</classname> widget defines at what points during user interaction it will change the <literal>value</literal> field of its <classname>Gtk::Adjustment</classname> and emit the <literal>value_changed</literal> signal. The update policies, set with the <methodname>set_update_policy()</methodname> method, are: <placeholder-1/>The <filename>configure</filename> script will check to make sure all of the required dependencies are already installed. If you are missing any dependencies, it will exit and display an error.The following example demonstrates the use of <classname>RadioButton</classname>s:The last line shows the window and enters the <application>gtkmm</application> main processing loop, which will finish when the window is closed.The names of the <application>gtkmm</application> packages vary from distribution to distribution (e.g. <application>libgtkmm2.4-dev</application> on Debian and Ubuntu or <application>gtkmm24-devel</application> on Red Hat Fedora), so check with your distribution's package management program for the correct package name and install it like you would any other package.The next line: <placeholder-1/> creates a <classname>Gtk::Main</classname> object. This is needed in all <application>gtkmm</application> applications. The constructor for this object initializes <application>gtkmm</application>, and checks the arguments passed to your application on the command line, looking for standard options such as <literal>-display</literal>. It takes these from the argument list, leaving anything it does not recognize for your application to parse or ignore. This ensures that all <application>gtkmm</application> applications accept the same set of standard arguments.The next two lines of code create and display a window:The package names will not change when new API/ABI-compatible versions of <application>gtkmm</application> are released. Otherwise they would not be API/ABI-compatible. So don't be surprised, for instance, to find <application>gtkmm</application> 2.8 supplied by Debian's <application>libgtkmm2.4-dev</application> package.The second way to set up radio buttons is to make a group first, and then add radio buttons to it. Here's an example:The value displayed by a scale widget is rounded to one decimal point by default, as is the <literal>value</literal> field in its <classname>Gtk::Adjustment</classname>. You can change this with the <methodname>set_digits()</methodname> method.There are horizontal and vertical scrollbar classes - <classname>Gtk::HScrollbar</classname> and <classname>Gtk::VScrollbar</classname>.There are two ways to create a Button. You can specify a label string in the <classname>Gtk::Button</classname> constructor, or set it later with <methodname>set_label()</methodname>.There are two ways to set up a group of radio buttons. The first way is to create the buttons, and set up their groups afterwards. Only the first two constructors are used. In the following example, we make a new window class called <classname>RadioButtons</classname>, and then put three radio buttons in it:These are standard scrollbars. They should be used only to scroll another widget, such as, a <classname>Gtk::Entry</classname>, or a <classname>Gtk::Viewport</classname>, though it's usually easier to use the <classname>Gtk::ScrolledWindow</classname> widget in most cases.These dependencies have their own dependencies, including the following applications and libraries:This bookThis book assumes a good understanding of C++, and how to create C++ programs.This book explains key concepts of the <application>gtkmm</application> C++ API for creating user interfaces. It also introduces the main user interface elements ("widgets").This book explains key concepts of the <application>gtkmm</application> C++ API for creating user interfaces. It also introduces the main user interface elements ("widgets"). Although it mentions classes, constructors, and methods, it does not go into great detail. Therefore, for full API information you should follow the links into the reference documentation.This chapter will introduce some of the most important aspects of <application>gtkmm</application> coding. These will be demonstrated with simple working example code. However, this is just a taster, so you need to look at the other chapters for more substantial information.This class implements the "Hello World" window. It's derived from <classname>Gtk::Window</classname>, and has a single <classname>Gtk::Button</classname> as a member. We've chosen to use the constructor to do all of the initialisation work for the window, including setting up the signals. Here it is, with the comments omitted:This example creates a button with a picture and a label.This example displays a window with three range widgets all connected to the same adjustment, along with a couple of controls for adjusting some of the parameters mentioned above and in the section on adjustments, so you can see how they affect the way these widgets work for the user.To begin our introduction to <application>gtkmm</application>, we'll start with the simplest program possible. This program will create an empty 200 x 200 pixel window.To define an accelerator key for keyboard navigation, place an underscore before one of the label's characters and specify <literal>true</literal> for the optional <literal>mnemonic</literal> parameter. For instance:To obtain a <application>gtkmm</application> instance from a C GObject instance, use the Glib::wrap() function. For instanceTo retrieve the state of the <classname>ToggleButton</classname>, you can use the <methodname>get_active()</methodname> method. This returns <literal>true</literal> if the button is "down". You can also set the toggle button's state, with <methodname>set_active()</methodname>. Note that, if you do this, and the state actually changes, it causes the "clicked" signal to be emitted. This is usually what you want.To simplify compilation, we use <literal>pkg-config</literal>, which is present in all (properly installed) <application>gtkmm</application> installations. This program 'knows' what compiler switches are needed to compile programs that use <application>gtkmm</application>. The <literal>--cflags</literal> option causes <literal>pkg-config</literal> to output a list of include directories for the compiler to look in; the <literal>--libs</literal> option requests the list of libraries for the compiler to link with and the directories to find them in. Try running it from your shell-prompt to see the results on your system.To use a <application>gtkmm</application> instance with a C function that requires a C GObject instance, use the <function>gobj()</function> function to obtain a pointer to the underlying GObject instance. For instanceToggle buttonsToggleButtonTrolltech's Qt is the closest competition to <application>gtkmm</application>, so it deserves discussion.Try to compile and run it before going on. You should see something like this:Unix and LinuxUnlike the Windows UCS-2 Unicode solution, this does not require any special compiler options to process string literals, and it does not result in Unicode executables and libraries which are incompatible with ASCII ones.Update PoliciesUseful methodsWe made a new group by simply declaring a variable, <literal>group</literal>, of type <classname>Gtk::RadioButton::Group</classname>. Then we made three radio buttons, using a constructor to make each of them part of <literal>group</literal>.We then hook up a signal handler to <literal>m_button</literal>'s <literal>clicked</literal> signal. This prints our friendly greeting to <literal>stdout</literal>.We told <application>gtkmm</application> to put all three <classname>RadioButton</classname>s in the same group by obtaining the group with <methodname>get_group()</methodname> and using <methodname>set_group()</methodname> to tell the other <classname>RadioButton</classname>s to share that group.We will now explain each line of the exampleWe would very much like to hear of any problems you have learning <application>gtkmm</application> with this document, and would appreciate input regarding improvements. Please see the <link linkend="chapter-contributing">Contributing</link> section for further information.We've now learned enough to look at a real example. In accordance with an ancient tradition of computer science, we now introduce Hello World, a la <application>gtkmm</application>:Wherever possible you should use Stock items, to ensure consistency with other applications, and to improve the appearance of your applications by using icons. For instance, <placeholder-1/> This will use standard text, in all languages, with standard keyboard accelerators, with a standard icon.Why use <application>gtkmm</application> instead of GTK+?WidgetsWidgets are arranged inside container widgets such as frames and notebooks, in a hierarchy of widgets within widgets. Some of these container widgets, such as Gtk::VBox, are not visible - they exist only to arrange other widgets. Here is some example code that adds 2 Gtk::Button widgets to a Gtk::VBox container widgets: <placeholder-1/> and here is how to add the Gtk::VBox, containing those buttons, to a Gtk::Frame, which has a visible frame and title: <placeholder-2/>You can use C APIs which do not yet have convenient C++ interfaces. It is generally not a problem to use C APIs from C++, and <application>gtkmm</application> helps by providing access to the underlying C object, and providing an easy way to create a C++ wrapper object from a C object, provided that the C API is also based on the GObject system.You can use the <methodname>toggled()</methodname> method to toggle the button, rather than forcing it to be up or down: This switches the button's state, and causes the <literal>toggled</literal> signal to be emitted.You might be surprised to learn that <application>gtkmm</application> doesn't use <classname>std::string</classname> in it its interfaces. Instead it uses <classname>Glib::ustring</classname>, which is so similar and unobtrusive that you could actually pretend that each Glib::ustring is a <classname>std::string</classname> and ignore the rest of this section. But read on if you want to use languages other than English in your application.You should avoid C-style pointer arithmetic, and functions such as strlen(). In UTF-8, each character might need anywhere from 1 to 6 bytes, so it's not possible to assume that the next byte is another character. <classname>Glib::ustring</classname> worries about the details of this for you so you can use methods such as Glib::ustring::substr() while still thinking in terms of characters instead of bytes.You should be very careful when installing to standard system prefixes such as <filename>/usr</filename>. Linux distributions install software packages to <filename>/usr</filename>, so installing a source package to this prefix could corrupt or conflict with software installed using your distribution's package-management system. Ideally, you should use a separate prefix for all software you install from source.Your existing knowledge of C++ will help you with <application>gtkmm</application> as it would with any library. Unless we state otherwise, you can expect <application>gtkmm</application> classes to behave like any other C++ class, and you can expect to use your existing C++ techniques with <application>gtkmm</application> classes.buttons examplecairocairommclass HelloWorld : public Gtk::Window
{

public:
  HelloWorld();
  virtual ~HelloWorld();

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

  //Member widgets:
  Gtk::Button m_button;
};class RadioButtons : public Gtk::Window
{
public:
    RadioButtons();

protected:
    Gtk::RadioButton m_rb1, m_rb2, m_rb3;
};

RadioButtons::RadioButtons()
  : m_rb1("button1"),
    m_rb2("button2"),
    m_rb3("button3")
{
    Gtk::RadioButton::Group group = m_rb1.get_group();
    m_rb2.set_group(group);
    m_rb3.set_group(group);
}class RadioButtons : public Gtk::Window
{
public:
    RadioButtons();
};

RadioButtons::RadioButtons()
{
    Gtk::RadioButton::Group group;
    Gtk::RadioButton *m_rb1 = Gtk::manage(
      new Gtk::RadioButton(group,"button1"));
    Gtk::RadioButton *m_rb2 = manage(
      new Gtk::RadioButton(group,"button2"));
      Gtk::RadioButton *m_rb3 = manage(
        new Gtk::RadioButton(group,"button3"));
}clickedenterg++ simple.cc -o simple `pkg-config gtkmm-2.4 --cflags --libs`glibgtkmmgtkmm-2.4 is the name of the current stable API. There was an older API called gtkmm-2-0 which installs in parallel when it is available. There are several versions of gtkmm-2.4, such as gtkmm 2.10. Note that the API name does not change for every version because that would be an incompatible API and ABI break. Theoretically, there might be a future gtkmm-3.0 API which would install in parallel with gtkmm-2.4 without affecting existing applications.int main(int argc, char** argv)
{
  Gtk::Main kit(argc, argv);

  HelloWorld helloworld;
  Gtk::Main::run(helloworld);

  return 0;
}leavelibsigc++ 2.0m_box.pack_start(m_Button1);
m_box.pack_start(m_Button2);m_button1.signal_clicked().connect( sigc::mem_fun(*this,
  &amp;HelloWorld::on_button_clicked) );m_frame.add(m_box);m_rb2.set_group(m_rb1.get_group()); //doesn't workpkg-configpressedreleasedstd::string uses 8 bit per character, but 8 bits aren't enough to encode languages such as Arabic, Chinese, and Japanese. Although the encodings for these languages has now been specified by the Unicode Constortium, the C and C++ languages do not yet provide any standardised Unicode support. GTK+ and GNOME chose to implement Unicode using UTF-8, and that's what is wrapped by Glib::ustring. It provides almost exactly the same interface as std::string, along with automatic conversions to and from std::string.translator-creditsProject-Id-Version: gtkmm-documentation master
POT-Creation-Date: 2010-07-20 22:25+0000
PO-Revision-Date: 2010-07-16 23:48+1000
Last-Translator: Tao Wang <dancefire@gmail.com>
Language-Team: Chinese (China) <i18n-zh@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

# ./configure
# make
# make install

Gtk::Button* button = new Gtk::Button("example");
gtk_button_do_something_new(button-&gt;gobj());

GtkButton* cbutton = get_a_button();
Gtk::Button* button = Glib::wrap(cbutton);
#include &lt;gtkmm.h&gt;2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010<application>gtkmm</application> 允许你使用常用的 C++ 技术来撰写代码,比如封装、继承和多态。作为一个 C++ 程序员,你可能已经意识到这会让代码更加清晰、更加良好的管理代码。<application>gtkmm</application> 应用程序由一系列包含了如按钮、文本框之类组件的窗口构成。在一些其它的系统上,组件可能被称为“控件”。对于你的应用程序窗口中的每个组件,在你的代码里就会有一个对应的 C++ 对象。所以当你想控制组件行为的时候,只需要调用这个组件对象的相应方法即可。<application>gtkmm</application> 对比 Qt<application>gtkmm</application> 开发人员更倾向于使用 <application>gtkmm</application> 而不是 Qt,因为 <application>gtkmm</application> 的使用更加符合 C++ 的方式。Qt 是诞生自 C++ 及其库尚未标准化或尚未被大多数编译器所支持的年代。它重复了大量的现存于标准库中的东西,比如容器、类型信息等等。最重要的是,Trolltech 修改了 C++ 语言以提供信号的功能,这样 Qt 的类无法很容易的与非 Qt 类共同使用。<application>gtkmm</application> 则可以使用标准 C++ 提供信号功能,而无需修改 C++ 语言。请看常见问题以得到进一步的差异。<application>gtkmm</application> 比 GTK+ 的代码更加简短,GTK+ 使用了大量带前缀的函数名以及大量的转型宏。<application>gtkmm</application> 是一个 <ulink url="http://www.gtk.org/">GTK+</ulink> 的 C++ 封装,GTK+ 是一个用于创建图形用户界面的软件库。它使用 LGPL 协议,因此您可以使用 <application>gtkmm</application> 开发开放软件、自由软件,甚至商业非免费软件而不需支付任何版权费用。<application>gtkmm</application> 是一个封装<application>gtkmm</application> 更加类型安全,因此编译器可以检测出一些 C 只能在运行时才能检测出来的错误。这种使用特定类型的方法同样使得 API 更加清晰,因为你只需要通过看一下函数声明就可以知道应该使用什么类型了。<application>gtkmm</application> 并不是一个原生的 C++ 工具集,而是一个对于 C 工具集的 C++ 封装。这种分离接口和实现的方式存在诸多优势。<application>gtkmm</application> 开发人员用了他们大量的时间来讨论如何让 <application>gtkmm</application> 提供一个最清晰的 API,而不必为模糊的技术细节尴尬的进行妥协。我们想其他的 C 程序员、Perl 程序员和 Python 程序员等等一样,对底层的 GTK+ 代码库进行了一点点贡献。因此,GTK+ 可以从比某个语言特定的工具集更广泛的用户群中获得好处,有更多的实现、更多的开发人员、更多的测试人员以及更多的用户。<application>gtkmm</application> 提供以下四种基本按钮控件:<application>gtkmm</application> 使用 libsigc++ 来实现信号操作。下面这个例子演示怎样把 Gtk::Button “clicked” 信号连接到命名为“on_button_clicked”的信号处理函数上:<placeholder-1/><application>gtkmm</application> 曾经被称为 gtk--,那是因为 GTK+ 的名字中已经有了一个加号。但是,由于 -- 不容易被搜索引擎索引,所以项目名字就渐渐的变成了<application>gtkmm</application>,后来我们就一直使用这个名字。像大多数 GUI 工具集一样,<application>gtkmm</application> 是<emphasis>事件驱动</emphasis>的。当一个事件发生时,比如说一个鼠标按键被按下,那么所点击的组件就会<emphasis>发出</emphasis>一个合适的信号。每种组件都可以发出一套不同的信号。为了使这个按钮点击产生一个动作,我们可以设置一个<emphasis>信号处理函数(signal handler)</emphasis>来捕捉这个“clicked”信号。<classname>Glib::ListHandle&lt;Gtk::Widget*&gt;</classname>:使用 <classname>std::vector&lt;Gtk::Widget*&gt;</classname>、<classname>std::list&lt;Gtk::Widget*&gt;</classname> 等。<classname>Glib::SListHandle&lt;Gtk::Widget*&gt;</classname>:使用 <classname>std::vector&lt;Gtk::Widget*&gt;</classname>、<classname>std::list&lt;Gtk::Widget*&gt;</classname> 等。<classname>Glib::StringArrayHandle</classname> 或 <classname>Glib::ArrayHandle&lt;Glib::ustring&gt;</classname>:使用 <classname>std::vector&lt;Glib::ustring&gt;</classname>、<classname>std::list&lt;Glib::ustring&gt;</classname>、<type>const char*[]</type> 等。<classname>Gtk::Button</classname> 同时也是一个容器,因此你可以在其中放任何其它的组件,比如放一个 <classname>Gtk::Image</classname> 在里面。<classname>Gtk::CheckButton</classname> 继承自 <classname>Gtk::ToggleButton</classname>。二者实际上只是外观不同而已。因此你可以像 <classname>Gtk::ToggleButton</classname> 一样使用同样的方法来检查、设置和转换多选按钮的状态。<classname>Gtk::Scale</classname> 和 <classname>Gtk::Scrollbar</classname> 都是继承自 <classname>Gtk::Range</classname>,并且共享了很多功能。他们都有一个“滑轨(trough)”和“滑块(slider)”(在其它 GUI 环境中可能被称为“指轮(thumbwheel)”)。在滑轨内可以用鼠标拖动滑块,而在滑轨上点击的话则会让滑块向点击的位置移动,可能是移动到终点,也可能是移动一段距离,这取决于你用的是鼠标的哪个按键。这和滚动条的行为很类似。<classname>Gtk::Scale</classname> 组件 (或者说“滑块”) 可以让用户在指定范围内直观地调节它的值。例如,你可以用它来调节一副预览图片的放大率、或者控制颜色的亮度,或者指定多少分钟后执行屏幕保护。<classname>Gtk::ToggleButton</classname> 类最有用的是作为 <classname>Gtk::CheckButton</classname> 类和 <classname>Gtk::RadioButton</classname> 类的基类。当 <classname>RadioButtons</classname> 创建后默认是未选中的,也就是说当你第一次创建一组单选按钮的时候,它们都会是未选中的。所以别忘了使用 <methodname>set_active()</methodname> 来选中一个:<classname>Scale</classname> 可以在滑轨旁显示它们的当前值。默认情况下是显示这个数值的,但是你可以通过调用 <methodname>set_draw_value()</methodname> 方法来设置是否显示该值。<classname>ToggleButton</classname> 和普通的 <classname>Button</classname> 相似,但是它们在点击后会保持激活(或称为按下的)状态,直到再次点击它为止。<literal>Gtk::UPDATE_CONTINUOUS</literal> - 这是默认的更新规则。会连续不断的发出 <literal>value_changed</literal> 信号,即使滑块只是被移动了一点点,也会发出该信号。<literal>Gtk::UPDATE_DELAYED</literal> - 只有当用户释放鼠标,或者滑块停止移动一小段时间后,才发出 <literal>value_changed</literal> 信号。<literal>Gtk::UPDATE_DISCONTINUOUS</literal> - 只有在滑块停止了移动并且用户释放了鼠标的时候才会发出 <literal>value_changed</literal> 信号。<ulink url="http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1Button.html"><classname>Gtk::Button</classname></ulink>。这是标准按钮,通常标有文字标签或者图片。按一下就会触发一个动作。参看 <link linkend="sec-pushbuttons">Button</link> 小节。<ulink url="http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1CheckButton.html"><classname>Gtk::CheckButton</classname></ulink>。它们用起来和 ToggleButtons 很像,只不过它们用一个小方块来显示自己的状态,而且旁边还有一个文字标签。在大多数需要布尔值设置的情况都可以使用它。参看 <link linkend="sec-checkboxes">CheckBox</link> 小节。<ulink url="http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1RadioButton.html"><classname>Gtk::RadioButton</classname></ulink>。它是根据老式汽车内的收音机的选台器来命名的,选台器是一组选择按钮并且它们是互斥的。按下其中任何一个都会使这一组中的其它按钮都弹起。它们的布局和 CheckButton (一个旁边有标签的小组件)很相似,但是通常看起来不太一样。参看 <link linkend="sec-radio-buttons">RadioButton</link> 小节。<ulink url="http://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1ToggleButton.html"><classname>Gtk::ToggleButton</classname></ulink>。和一般按下后会自动弹起的按钮不一样,ToggleButton 需要你再按一次才会弹起。它作为一个开关可能比较有用。参看 <link linkend="sec-toggle-buttons">ToggleButton</link> 小节。ATK将源代码保存到 <literal>simple.cc</literal> 后,你可以使用 gcc 编译上面的程序:<placeholder-1/> 注意,你必须将 <literal>pkg-config</literal> 的调用包含在一对反单引号中。反单引号会导致其内的命令由 shell 执行,并且将命令的输出做为该命令行的一部分来使用。在安装了所有依赖的库后,下载 <application>gtkmm</application> 源代码、解压缩,并且切换到新创建的目录。然后可以用下列命令序列构件和安装 <application>gtkmm</application>:Alan所有的 <application>gtkmm</application> 程序都需要包含一些特定的 <application>gtkmm</application> 头文件;<literal>gtkmm.h</literal> 包含了所有 <application>gtkmm</application> 的内容。通常来说,这并不是一个好主意,因为它包括了一兆左右的头文件。当然,对于这个简单的程序,这就够了。另外,通过使用 <methodname>set_value_pos()</methodname>,可以使值显示在相对于滑块不同的位置。尽管 <application>gtkmm</application> 组件实例拥有像其它 C++ 类那样的生存时间和作用域,<application>gtkmm</application> 还是有一些可选的节省时间的特性,接下来你会在接下来的一些例子中看到。<function>Gtk::manage()</function> 允许你指定一个子组件是被其容器组件所拥有。这将允许你 <function>new</function> 组件、添加到容器中,然后忘记删除它。如果你需要了解更多关于 <application>gtkmm</application> 内存管理技术的话,请看 <link linkend="chapter-memory">内存管理</link> 章节。虽然我们已经给出了这个简单的例子的编译命令,但是你最好还是使用 automake 和 autoconf 工具,就像 G. V. Vaughan 等在《Autoconf, Automake, Libtool》中所描述的那样。本书中用到的例子都包含在 <application>gtkmm</application> 包内,同时附有适当的编译构建的文件,所以我们以后不会再给出任何编译命令。你只需要找到恰当的目录然后键入 <literal>make</literal>。尽管你可以使用 C++ 代码来指定窗口和组件的外观和布局,但你可能会发现使用 Glade 来设计你的界面,并且使用 <literal>Gtk::Builder</literal> 在运行时动态加载界面是更方便。请参考 <link linkend="chapter-builder">Glade 与 Gtk::Builder</link> 这一章。Anastasov章节《Visual Studio 2005》就象将在后面的<link linkend="chapter-adjustment">Adjustment</link> 小节要谈到的那样,所有的 Range 组件都有一个关联的 <classname>Adjustment</classname> 对象。如果你想修改组件的最小值、最大值以及当前值,你需要调用该组件关联的 <classname>Adjustment</classname> 对象的方法,该对象可以通过调用 <methodname>get_adjustment()</methodname> 方法来得到。<classname>Range</classname> 组件默认构造函数会自动的创建一个 <classname>Adjustment</classname>,当然,你也可以指定一个现有的 <classname>Adjustment</classname>,而且还可以和其它组件共享。请参看 <link linkend="chapter-adjustment">Adjustments</link> 部分以得到更详细的信息。像 <classname>Scrollbar</classname> 一样,也分别有水平和垂直两种组件:<classname>Gtk::HScale</classname> 和 <classname>Gtk::VScale</classname>。默认构造函数会创建一个 <classname>Adjustment</classname>,并且所有值都设为 <literal>0.0</literal>。当然,这显然没有意义,所以你需要设置一些 <classname>Adjustment</classname> 的细节来让它获得有意义的行为。基础在试图安装 <application>gtkmm</application> 2.4之前,你必须已经安装了这些包。BernhardButton (按钮)按钮默认情况下,<application>gtkmm</application> 将会被安装在 <filename>/usr/local</filename> 目录。在有些系统中,你也许需要安装到不同的位置。比如,在 Red Hat Linux 系统中,你也许需要使用 <literal>--prefix</literal> 选项进行配置,例如:<screen>
# ./configure --prefix=/usr
</screen>Cedric章节《绘图区域》章节《使用 Cairo 绘图》章节《打印》章节《最近的文件》章节《超时》章节《Win32 安装》章节《使用 gtkmm 源代码》CheckButton (多选按钮)复选框 (CheckButton)构造函数CummingDaniel依赖关系Elstner当按钮按下并抬起时发出。当按钮按下时发出。当按钮释放后发出。当鼠标指针离开按钮时发出。当鼠标指针移到按钮上时发出。示例我们首先初始化一个名为 <literal>kit</literal> 的对象,它的类型为 <classname>Gtk::Main</classname> 。每一个 <application>gtkmm</application> 程序都有这样的一个东西。我们将命令行参数传递给它的构造函数。它会提取自己需要的参数,将剩下的部分留给我们,就像前面描述过的那样。要想知道如何实现你自己的信号,而不仅仅是连接到现有的 <application>gtkmm</application> 信号,请参考 <link linkend="chapter-custom-signals">附录</link>。要得到更多关于信号的信息,请参考<link linkend="chapter-signals">附录</link>。GTK+ 2.4GTK+ and <application>gtkmm</application> 被设计成在 Microsoft Windows 上也能很好地工作,而且开发者们鼓励在 win32 平台上使用它们。然而,还有没为 Wnidows 平台的开发库提供标准的安装方法。请参考 <ulink url="http://live.gnome.org/gtkmm/MSWindows">Windows Intallation</ulink> (<ulink url="http://live.gnome.org/Chinese/gtkmm/MSWindows">中文翻译</ulink>) 以得到进一步关于 Windows 相关的安装方法和注意事项。GeneGlib::ustring组Gtk::Button* pButton = new Gtk::Button("_Something", true);Gtk::Button* pButton = new Gtk::Button(Gtk::Stock::OK);Gtk::Main kit(argc, argv);Gtk::Main::run(window);Gtk::Window window;Gustin头文件和链接Hello World用 <application>gtkmm</application> 写 Hello WorldHelloWorld::HelloWorld()
:
  m_button ("Hello World")
{
  set_border_width(10);
  m_button.signal_clicked().connect(sigc::mem_fun(*this,
    &amp;HelloWorld::on_button_clicked));
  add(m_button);.
  m_button.show();
}然而,在标准的 configure.ac 中使用 <function>PKG_CHECK_MODULES()</function> 宏,并且运行 autoconf 和 automake 后,这变的更加简单。例如:<placeholder-1/> 这将会检查是否存在 gtkmm,并且定义了可以在你的 Makefile.am 文件中使用的 MYAPP_LIBS 和 MYAPP_CFLAGS 变量。如果你想帮助开发 <application>gtkmm</application>,或实验一些新的功能,你也可以直接从 Git 安装 <application>gtkmm</application>。大多数用户绝对不需要这么做,但是如果你对帮助 <application>gtkmm</application> 开发感兴趣,请看附录:<link linkend="chapter-working-with-source">使用 gtkmm 源代码</link>。如果你的发行版没提供预编译的 <application>gtkmm</application> 包,或者如果你想安装一个和发行版所提供的不同的版本,那么你也可以从源代码安装 <application>gtkmm</application>。可以从 <ulink url="http://www.gtkmm.org/"/> 中下载 <application>gtkmm</application> 的源代码。可以使用继承来衍生新的部件。在 GTK+ 中使用 C 代码来衍生新部件是非常复杂的,并且很容易出错,因此几乎没有 C 的程序员这么做。作为 C++ 开发人员,你知道继承是一个基本的面向对象技术。安装从源代码安装中间类型序言JasonJonathonJongsmaLaursen和 <classname>Gtk::CheckButton</classname> 一样,<classname>Gtk::RadioButton</classname> 也继承自 <classname>Gtk::ToggleButton</classname>,只是它们是按组工作的,并且一个组内一次只能选中一个 RadioButton。M'SadoquesMarko可以使用成员实例,简化了内存管理。所有的 GTK+ 的 C 部件都是使用指针进行操作的。作为 C++ 程序员,你知道指针应当尽量避免使用。方法Microsoft Windows混合使用 C 和 C++ API本书中的大部分章节都是讲解特定的组件。要得到更多关于添加组件到容器组件的信息,请看 <link linkend="chapter-container-widgets">容器组件</link> 这一章。MurrayMurray Cumming接下来调用 Windows 类的 <methodname>set_border_width()</methodname> 方法。设置窗口的边框和它所包含的组件之间的空白距离。接着我们创建了 <classname>HelloWorld</classname> 类的对象,它的构造函数没有参数,当然,它现在还不是可视的。当我们调用 Gtk::Main::run() 并给它提供一个 helloworld 窗口作为参数时,它将显示这个窗口并进入 <application>gtkmm</application><emphasis>消息循环</emphasis>。在消息循环中,<application>gtkmm</application> 是空闲的,它等待用户的操作并作出相应的反应。当用户关闭窗口时,run() 函数将返回,导致 main() 函数的最后一行将被执行。然后程序就结束了。下一步,我们使用 Window 类的 <methodname>add()</methodname> 方法把 <literal>m_button</literal> 加到 Window 类中。(<methodname>add()</methodname> 方法由 <classname>Gtk::Container</classname> 类继承而来,我们将在容器组件这一章中具体介绍。) <methodname>add()</methodname> 方法仅仅将组件添加到窗口中,它并不负责显示这些组件。<application>gtkmm</application> 组件在创建以后都是不可见的。要显示它们,你必须调用 <methodname>show()</methodname> 方法,通常我们会在接下来的一行就调用它。要注意的是 UTF-8 和其它一些8位的编码如 ISO-8859-1 并不兼容。比如,德语中的元音变音并不在 ASCII 码的编码范围内,在 UTF-8 编码中需要额外的一个字节来表示。如果你的代码中包含8位的字符串文字,你需要把它们转变成 UTF-8 编码 (比如,巴伐利亚的问候语“Grüß Gott”可能会显示成“Gr\xC3\xBC\xC3\x9F Gott”)。注意,如果你提到了除 gtkmm-2.4 之外的模块,它们需要以空格分开,而不是逗号。注意,你不能只是 <placeholder-1/> 因为该组会由 <methodname>set_group()</methodname> 修改,所以它不能是一个常量。请注意,由于GTK+的主题不同,这些控件外观可能会不同。特别是复选框和单选按钮可能会很不一样。注意,我们使用初始化列表的方式给 <literal>m_button</literal> 对象添加了一个“Hello World”的标签。现在让我们来看看程序中没有注释过的 <function>main()</function> 函数:OleUTF-8 编码的优点之一是,如果不需要你可以不使用它,所以你不需要一次性的翻新你的代码。<classname>std::string</classname> 还可以继续使用7位的 ASCII 字符串。但是一旦你想本地化你的程序,比如说汉化,那你就会开始遇到各种奇怪的问题,可能还会崩溃。那时,你所需要做的就是开始使用 <classname>Glib::ustring</classname> 来取而代之。Openismus 有更多 <ulink url="http://www.openismus.com/documents/linux/automake/automake.shtml">帮助使用 automake 和 autoconf 的基础知识</ulink>。OttPKG_CHECK_MODULES([MYAPP], [gtkmm-2.4 &gt;= 2.8.0])Pango部分章节《国际化》根据由自由软件基金发布的 GNU 自由文档协议版本1.2或更高版本的条款,授权复制、分发和/或修改此文档;并且,没有不可变章节,没有封面文本,并且没有封底文本。您可以通过访问自由软件基金(FSF)的网站或写邮件至:Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 来获得一份 GNU 自由文档协议。预编译的包真让人激动,不是吗?让我们回过头来看一下代码。首先看一下 <classname>HelloWorld</classname> 类:<application>gtkmm</application> 程序设计按钮 (Button)单选框 (RadioButton)RadioButton (单选按钮)Range 组件<application>gtkmm</application> 最新的版本的软件包几乎存在于今天每一个主流的 Linux 发布版本中。所以,如果你使用 Linux,你可能只需要从官方的软件库安装相应的软件包,然后就可以开始使用 <application>gtkmm</application> 了。已经在它们的软件库里面包含了 <application>gtkmm</application> 的发布版本有:Debian、Ubuntu、Red Hat、Fedora、Mandriva、SuSE 等等。参考记住,在 Unix 或 Linux 操作系统上,你也许需要 <literal>root</literal> 权限以安装软件。<command>su</command> 命令允许你输入 <literal>root</literal> 密码,并且临时拥有 <literal>root</literal> 权限。RiederRuebsamen比例组件 (Scale)滚动条组件 (Scrollbar)《使用 gmmproc 封装 C 库》章的〈构建结构〉节。要得到更多关于 UTF-8 字符的信息,请参考<link linkend="chapter-internationalization">国际化</link>这一节。信号简单的例子部分 <application>gtkmm</application> API 使用了中间数据容器,如 <classname>Glib::StringArrayHandle</classname> 而不是特定的标准 C++ 容器 <classname>std::vector</classname> 或 <classname>std::list</classname>。但是你不应该自己声明这些类型,你应当使用你所倾向的标准 C++ 容器。<application>gtkmm</application> 将会帮你进行类型转换。这里有一些这类中间类型:<placeholder-1/>源代码<classname>Gtk::Button</classname> 组件有下面列出的那些信号,不过大多数情况下,你只需要处理 <literal>clicked</literal> 信号:<classname>Range</classname> 组件的 <emphasis>更新策略</emphasis> 定义了在用户操作期间的什么时候更新 <classname>Gtk::Adjustment</classname> 的值 <literal>value</literal>,并且何时发出 <literal>value_changed</literal> 信号。可以使用 <methodname>set_update_policy()</methodname> 方法来设置更新策略。这些策略包括:<placeholder-1/><filename>configure</filename> 将会检查以确认所有必须的依赖软件包都已经正确的安装了。如果你遗漏了某个依赖软件包的话,它会退出并提示错误信息。接下来的例子中演示了 <classname>RadioButton</classname> 的用法:最后一行显示窗口,并且进入 <application>gtkmm</application> 的主处理循环,它将一直运行到窗口被关闭。<application>gtkmm</application> 软件包的名字可能在不同的发行版中所不同 (例如,在 Debian 和 Ubuntu 上的名字是<application>libgtkmm2.4-dev</application>,而在 Red Hat Fedora 上则是 <application>gtkmm24-devel</application>),因此,在你的发行版的软件包管理程序中查一下正确的名字,然后像其它软件包一样的安装它。下面一行:<placeholder-1/> 创建了一个 <classname>Gtk::Main</classname> 对象。这是所有的 <application>gtkmm</application> 程序中所必须的。这个对象的构造函数初始化了 <application>gtkmm</application>,并且检查传递给你的程序的命令行参数,从中寻找像 <literal>-display</literal> 这样的标准选项。然后它会将这些可以识别的参数从参数列表中删除,留下那些它不能识别的选项,交由你的程序处理或者忽略。这样就保证了 <application>gtkmm</application> 应用程序能和其它程序一样接受所有的标准参数。接下来的两行代码创建并显示了一个窗口:当新的 API/ABI 兼容的 <application>gtkmm</application> 版本发布后,其包的名称将不会改变。否则的话,就说明 API/ABI 不兼容。所以,如果在 Debian 的 <application>libgtkmm2.4-dev</application> 包中发现提供的实际上是 <application>gtkmm</application> 2.8 的话不要太惊讶。第二种创建单选按钮的方式是,先创建一个组,然后再向该组加入按钮。请看下面的示例:默认情况下,Scale 组件会显示 <classname>Gtk::Adjustment</classname> 的 <literal>value</literal> 的数值,并且会被四舍五入到一位小数。你可以通过 <methodname>set_digits()</methodname> 方法来改变显示的小数位数。分别有水平的和垂直的滚动条类:<classname>Gtk::HScrollbar</classname> 和 <classname>Gtk::VScrollbar</classname>。可以通过两种方式来创建按钮,你可以在 <classname>Gtk::Button</classname> 构造函数中指定标签字符串,或者以后调用 <methodname>set_label()</methodname> 来指定标签。有两种方式创建一个单选按钮的组。第一种方式是先创建按钮,然后再设置它们的组。这种方式只用到了前两个构造函数。在下面的示例中,我们创建了一个新的窗口类 <classname>RadioButtons</classname>,然后在其中放置3个单选按钮:这些是标准的滚动条。它们应该只被用于滚动其它组件,比如 <classname>Gtk::Entry</classname> 或 <classname>Gtk::Viewport</classname>,当然,通常使用 <classname>Gtk::ScrolledWindow</classname> 会更容易一些。这些依赖有它们自己所依赖的软件包,包括下列应用程序和软件库:本书本书假定读者拥有对 C++ 良好的理解,并且知道如何去创建一个 C++ 程序。本书说明了使用<application>gtkmm</application> C++ API 创建用户界面的重要概念,并且介绍了主要的用户界面元素(“widget”)。本书说明了使用<application>gtkmm</application> C++ API 创建用户界面的重要概念,并且介绍了主要的用户界面元素(“widget”)。虽然提到了类、构造函数和方法,但是并不会深入其细节。因此,如果需要完整的 API 信息,您应当顺着链接访问参考文档。这一章将介绍 <application>gtkmm</application> 编程中一些重要的方面。这些将由一些可以运行的示例代码来演示。然而,这仅仅是一种尝试,你还需要继续看其它章节,以得到更实质性的信息。这个类实现了“Hello World”窗口。它由 <classname>Gtk::Window</classname> 派生,并且只有一个 <classname>Gtk::Button</classname> 成员。我们已经使用构造函数为该窗口完成了所有的初始化工作,包括挂接消息。在这,已经去掉注释的代码:这个示例创建了一个有图片和文字标签的按钮。这个示例显示了一个窗口,其中有三个关联到同一个 Adjustment 对象的 Range 组件,还有几个控制组件用来调整前文讨论过的一些参数,这样你可以很清楚地看到那些参数是如何影响这些组件的行为方式的。在开始介绍 <application>gtkmm</application> 之前,我们将以一个尽可能简单的程序开始。这个程序创建一个 200 x 200 像素大小的空窗口。如果你想为按钮定义一个快捷键,那么可以在标签字符串中的某个字符前面加上一个下划线,并且指定可选的参数助记符(mnemonic)为 true。例如:要从 C GObject 实例得到一个 <application>gtkmm</application> 实例,则使用 Glib::wrap() 函数。例如要得到 <classname>ToggleButton</classname> 的状态,你可以使用 <methodname>get_active()</methodname> 方法。如果按钮被按下了,就返回 true。当然,你也可以通过 <methodname>set_active()</methodname> 来设置开关按钮的状态。请注意,如果你这样做了,并且它的状态的确改变了,那么这将会引发“clicked”信号。当然,一般来说,这就是你希望看到的结果。为了简化编译的过程,我们使用了 <literal>pkg-config</literal>,它存在于所有的(也许已经安装的) <application>gtkmm</application> 安装文件中。这个程序“知道”编译使用了 <application>gtkmm</application> 的程序所需要的编译器选项。<literal>--cflags</literal> 选项使 <literal>pkg-config</literal> 输出一个包含编译时需要用到的头文件的目录列表;而使用 <literal>--libs</literal> 选项将得到一个需要编译器去链接的库列表和一个用于寻找它们的目录列表。试着在你的命令行提示符下运行它,看看在你的系统上会有什么样的结果。要用 C 函数使用一个 <application>gtkmm</application> 实例需要一个 C GObject 实例,使用 <function>gobj()</function> 函数可以得到一个底层 GObject 实例的指针。例如开关按钮 (ToggleButton)ToggleButton (开关按钮)Trolltech 公司的 Qt 是和 <application>gtkmm</application> 最相似的有竞争力的产品,所以应该对其进行一下讨论。在继续讲解之前,可以尝试着编译并运行这个程序,你会看到这样的结果:Unix 和 Linux不像 Windows 的 UCS-2 Unicode 解决方案,这并不需要编译器提供特别的选项来处理这样字符串,也不会导致为 Unicode 编译的可执行文件或库与含为 ASCII 的不兼容之类的问题。更新策略一些有用的方法我们通过声明一个类型为 <classname>Gtk::RadioButton::Group</classname> 的名为 <literal>group</literal> 的变量。然后我们创建三个单选按钮,用构造函数把它们放置到组 <literal>group</literal> 中。然后,把 <literal>m_button</literal> 的 <literal>clicked</literal> 消息挂接到信号处理函数上。这会向 <literal>stdout</literal> (标准输出) 打印出友好的问候语。我们告诉 <application>gtkmm</application> 把三个 <classname>RadioButton</classname> 都放到一个组内,通过 <methodname>get_group()</methodname> 来得到这个组,然后使用 <methodname>set_group()</methodname> 告诉其它的 <classname>RadioButton</classname> 来共享这个组。我们会逐行讲解这个例子我们非常愿意聆听您在使用此文档学习 <application>gtkmm</application> 时碰到的任何问题,并且感激对此作出的改进。请访问 <link linkend="chapter-contributing">贡献</link> 节以获得进一步的信息。目前我们已经可以自己所学的知识来写一个真正的程序了。根据计算机科学的传统,我们现在以 <application>gtkmm</application> 的方式来介绍 Hello World 程序:任何时候你都应尽可能的使用 Stock Item,这样可以使你的应用程序和其它的保持一致,并且可以通过使用图标来改善程序的外观。例如, <placeholder-1/> 这样在所有的语言中都会使用标准的文字,标准的快捷键,以及标准的图标。为什么要使用 <application>gtkmm</application> 而不是 GTK+?组件组件被安置在褚如 frame、notebook 这样的容器组件中,以一种组件包含组件的层次结构的形式。其中一些容器组件,像 Gtk::VBox 这样的容器组件是不可见的,它们只是被用来安置其它组件的。这有一些示例代码,将两个 Gtk::Button 组件放到一个 Gtk::VBox 容器组件中:<placeholder-1/> 接下来是如何把这个包含两个按钮的 Gtk::VBox 添加到一个 Gtk::Frame 中,它包含一个可视的边框和标题:<placeholder-2/>你可以使用那些尚未提供 C++ 接口的 C API。对于 C++ 而言这一般不是什么问题,<application>gtkmm</application> 可以提供帮助访问底层的 C 对象,并且也提供了一个简单的方式来从一个 C 对象创建一个 C++ 封装的对象,当然,这个 C API 是需要基于 GObject 系统的。你可以使用 <methodname>toggled()</methodname> 方法来改变按钮的状态,而不要强行的是设置它是开还是关:这个方法将会改变按钮的状态,并会引发 <literal>toggled</literal> 信号。你一定很惊讶得知 <application>gtkmm</application> 没有在它的接口上使用 <classname>std::string</classname>,相反,它使用了 <classname>Glib::ustring</classname>。 <classname>Glib::ustring</classname>与 <classname>std::string</classname> 非常相似,甚至在某些场合,你甚至可以把它当成 <classname>std::string</classname> 来使用,而忽略本节后面的内容。但是如果你希望在你的应用程序中使用英语以外的语言的话,那么请继续读下去。你应该尽量避免 C 风格的指针算法,以及像 strlen() 这样的函数。在 UTF-8 编码中,每个字符可能占1到6个字节,所以你并不能假定下一个字节一定是另一个字符。所有的这些细节 <classname>Glib::ustring</classname> 都已经帮你考虑到了,所以你可以从字符的层面上考虑使用 Glib::ustring::substr() 这样的函数,而不用再去考虑字节这样的细节了。在安装到标准系统前缀时,如 <filename>/usr</filename>,你必须非常小心。Linux 发行版会安装软件到 <filename>/usr</filename>,所以安装源代码包到这个位置可能会破坏或与使用软件包管理器安装的软件冲突。理想情况下,你应当将从源代码安装的软件都安装到一个独立位置。你现有的 C++ 知识将会帮助你使用 <application>gtkmm</application>,因为它可以和任何库一同工作。除非我们额外声明,你可以期待 <application>gtkmm</application> 类会像任何其它 C++ 类一样,并且你可以期待在 <application>gtkmm</application> 类上使用你现有的知识。按钮示例cairocairommclass HelloWorld : public Gtk::Window
{

public:
  HelloWorld();
  virtual ~HelloWorld();

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

  //Member widgets:
  Gtk::Button m_button;
};class RadioButtons : public Gtk::Window
{
public:
    RadioButtons();

protected:
    Gtk::RadioButton m_rb1, m_rb2, m_rb3;
};

RadioButtons::RadioButtons()
  : m_rb1("button1"),
    m_rb2("button2"),
    m_rb3("button3")
{
    Gtk::RadioButton::Group group = m_rb1.get_group();
    m_rb2.set_group(group);
    m_rb3.set_group(group);
}class RadioButtons : public Gtk::Window
{
public:
    RadioButtons();
};

RadioButtons::RadioButtons()
{
    Gtk::RadioButton::Group group;
    Gtk::RadioButton *m_rb1 = Gtk::manage(
      new Gtk::RadioButton(group,"button1"));
    Gtk::RadioButton *m_rb2 = manage(
      new Gtk::RadioButton(group,"button2"));
      Gtk::RadioButton *m_rb3 = manage(
        new Gtk::RadioButton(group,"button3"));
}clicked (点击)enter (进入)g++ simple.cc -o simple `pkg-config gtkmm-2.4 --cflags --libs`glibgtkmmgtkmm-2.4 是当前稳定 API 的名字。更早期的 API 叫做 gtkmm-2.0,如果需要的话它可以和 gtkmm-2.4 同时安装到系统。gtkmm-2.4 有许多版本,例如,gtkmm-2.10。注意,API 的名字并不会随着每个版本变化而发生变化,因为如果这个发生变化了,则意味着 API 和 ABI 不再同以前保持兼容。理论上,将来可以有 gtkmm-3.0 API 与现在的 gtkmm-2.4 同时安装到系统,而现有应用程序不会受到影响。int main(int argc, char** argv)
{
  Gtk::Main kit(argc, argv);

  HelloWorld helloworld;
  Gtk::Main::run(helloworld);

  return 0;
}leave (离开)libsigc++ 2.0m_box.pack_start(m_Button1);
m_box.pack_start(m_Button2);m_button1.signal_clicked().connect( sigc::mem_fun(*this,
  &amp;HelloWorld::on_button_clicked) );m_frame.add(m_box);m_rb2.set_group(m_rb1.get_group()); //无法工作pkg-configpressed (按下)released (释放)std::string 对每个字符使用8位编码,但是这对于像阿拉伯语、汉语和日语这样的语言来说,8位是不够的。尽管 Unicode 协会已经详细定义了这些语言的编码,但是 C 和 C++ 仍未提供任何标准的 Unicode 支持。GTK+ 和 GNOME 采用 UTF-8 编码来实现 Unicode,这就是 Glib::ustring 所包装的东西。它提供了和 std::string 几乎相同的接口,以及和 std::string 的自动类型转换的功能。sweord <sweord@msn.com>, 2004
Tao Wang <dancefire@gmail.com>, 2010