programming-with-gtkmm.pdf

(2738 KB) Pobierz
13346798 UNPDF
Programming with gtkmm
Murray Cumming
Bernhard Rieder
Jonathon Jongsma
Jason M’Sadoques
Ole Laursen
Gene Ruebsamen
Cedric Gustin
Marko Anastasov
Alan Ott
Programming with gtkmm
by Murray Cumming, Bernhard Rieder, Jonathon Jongsma, Jason M’Sadoques, Ole Laursen, Gene Ruebsamen,
Cedric Gustin, Marko Anastasov, and Alan Ott
Copyright © 2002-2006 Murray Cumming
We very much appreciate any reports of inaccuracies or other errors in this document. Contributions are also most
welcome. Post your suggestions, critiques or addenda to the gtkmm mailing list (mailto:gtkmm-list@gnome.org) --
The gtkmm Development Team
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.
Table of Contents
1. Introduction............................................................................................................................................1
1.1. This book.....................................................................................................................................1
1.2. gtkmm .........................................................................................................................................1
1.2.1. Why use gtkmm instead of GTK+?................................................................................1
1.2.2. gtkmm compared to QT..................................................................................................2
1.2.3. gtkmm is a wrapper ........................................................................................................2
2. Installation..............................................................................................................................................3
2.1. Dependencies ..............................................................................................................................3
2.2. Unix and Linux ...........................................................................................................................3
2.2.1. Prebuilt Packages............................................................................................................3
2.2.2. Installing From Source ...................................................................................................3
2.3. Microsoft Windows .....................................................................................................................4
3. Basics.......................................................................................................................................................6
3.1. Simple Example ..........................................................................................................................6
3.2. Headers and Linking ...................................................................................................................7
3.3. Widgets........................................................................................................................................8
3.4. Signals .........................................................................................................................................9
3.5. Glib::ustring ................................................................................................................................9
3.6. Intermediate types .....................................................................................................................10
3.7. Hello World in gtkmm ..............................................................................................................10
4. Buttons ..................................................................................................................................................15
4.1. Button........................................................................................................................................15
4.1.1. Constructors..................................................................................................................15
4.1.2. Example ........................................................................................................................16
4.1.3. Signals ..........................................................................................................................17
4.2. ToggleButton.............................................................................................................................18
4.3. CheckButton..............................................................................................................................18
4.3.1. Example ........................................................................................................................19
4.4. RadioButton ..............................................................................................................................20
4.4.1. Groups ..........................................................................................................................21
4.4.2. Methods ........................................................................................................................22
4.4.3. Example ........................................................................................................................22
5. Range Widgets .....................................................................................................................................26
5.1. Scrollbar Widgets ......................................................................................................................26
5.2. Scale Widgets ............................................................................................................................26
5.2.1. Useful methods .............................................................................................................26
5.3. Update Policies .........................................................................................................................27
5.4. Example ....................................................................................................................................27
6. Miscellaneous Widgets ........................................................................................................................35
6.1. Label..........................................................................................................................................35
6.1.1. Example ........................................................................................................................35
6.2. Entry..........................................................................................................................................39
6.2.1. Example ........................................................................................................................39
6.3. SpinButton ................................................................................................................................40
iii
6.3.1. Methods ........................................................................................................................41
6.3.2. Example ........................................................................................................................41
6.4. ProgressBar ...............................................................................................................................47
6.4.1. Activity Mode...............................................................................................................47
6.4.2. Example ........................................................................................................................47
6.5. Tooltips......................................................................................................................................51
7. Container Widgets ...............................................................................................................................53
7.1. Single-item Containers..............................................................................................................53
7.1.1. Frame ............................................................................................................................53
7.1.2. Paned ............................................................................................................................56
7.1.3. ScrolledWindow ...........................................................................................................61
7.1.4. AspectFrame.................................................................................................................64
7.1.5. Alignment .....................................................................................................................67
7.2. Multiple-item widgets ...............................................................................................................69
7.2.1. Packing .........................................................................................................................69
7.2.2. An improved Hello World ............................................................................................70
7.2.3. STL-style APIs .............................................................................................................73
7.2.4. Boxes ............................................................................................................................76
7.2.5. ButtonBoxes .................................................................................................................84
7.2.6. Table .............................................................................................................................90
7.2.7. Notebook ......................................................................................................................94
8. The TreeView widget ...........................................................................................................................98
8.1. The Model .................................................................................................................................98
8.1.1. ListStore, for rows ........................................................................................................98
8.1.2. TreeStore, for a hierarchy .............................................................................................99
8.1.3. Model Columns ............................................................................................................99
8.1.4. Adding Rows ..............................................................................................................100
8.1.5. Setting values..............................................................................................................100
8.1.6. Getting values .............................................................................................................101
8.1.7. "Hidden" Columns......................................................................................................101
8.2. The View .................................................................................................................................101
8.2.1. Using a Model ............................................................................................................101
8.2.2. Adding View Columns ...............................................................................................101
8.2.3. More than one Model Column per View Column ......................................................102
8.2.4. Specifying CellRenderer details .................................................................................102
8.2.5. Editable Cells..............................................................................................................103
8.3. Iterating over Model Rows......................................................................................................104
8.3.1. Row children...............................................................................................................104
8.4. The Selection...........................................................................................................................104
8.4.1. Single or multiple selection ........................................................................................105
8.4.2. The selected rows .......................................................................................................105
8.4.3. The "changed" signal..................................................................................................105
8.4.4. Preventing row selection.............................................................................................106
8.4.5. Changing the selection ...............................................................................................106
8.5. Sorting .....................................................................................................................................106
8.5.1. Sorting by clicking on columns ..................................................................................107
8.5.2. Independently sorted views of the same model..........................................................107
iv
8.6. Drag and Drop.........................................................................................................................108
8.6.1. Reorderable rows ........................................................................................................108
8.7. Popup Context Menu...............................................................................................................108
8.7.1. Handling button_press_event .............................................................................108
8.8. Examples .................................................................................................................................109
8.8.1. ListStore .....................................................................................................................109
8.8.2. TreeStore ....................................................................................................................113
8.8.3. Editable Cells..............................................................................................................116
8.8.4. Drag and Drop ............................................................................................................122
8.8.5. Popup Context Menu ..................................................................................................128
9. Combo Boxes ......................................................................................................................................134
9.1. ComboBox ..............................................................................................................................134
9.1.1. The model ...................................................................................................................134
9.1.2. The chosen item..........................................................................................................134
9.1.3. Responding to changes ...............................................................................................135
9.1.4. Full Example ..............................................................................................................135
9.1.5. Simple Text Example..................................................................................................138
9.2. ComboBoxEntry .....................................................................................................................140
9.2.1. The text column ..........................................................................................................140
9.2.2. The entry.....................................................................................................................140
9.2.3. Full Example ..............................................................................................................141
9.2.4. Simple Text Example..................................................................................................144
10. TextView ...........................................................................................................................................146
10.1. The Buffer .............................................................................................................................146
10.1.1. Iterators.....................................................................................................................146
10.1.2. Tags and Formatting .................................................................................................146
10.1.3. Marks ........................................................................................................................147
10.1.4. The View...................................................................................................................148
10.2. Widgets and ChildAnchors ...................................................................................................148
10.3. Examples ...............................................................................................................................149
10.3.1. Simple Example........................................................................................................149
11. Menus and Toolbars ........................................................................................................................153
11.1. Actions ..................................................................................................................................153
11.2. UIManager ............................................................................................................................153
11.3. Popup Menus.........................................................................................................................155
11.4. Examples ...............................................................................................................................155
11.4.1. Main Menu example.................................................................................................156
11.4.2. Popup Menu example ...............................................................................................160
12. Adjustments......................................................................................................................................165
12.1. Creating an Adjustment ........................................................................................................165
12.2. Using Adjustments the Easy Way .........................................................................................165
12.3. Adjustment Internals .............................................................................................................166
13. Widgets Without X-Windows .........................................................................................................168
13.1. EventBox...............................................................................................................................168
13.1.1. Example ....................................................................................................................169
v
Zgłoś jeśli naruszono regulamin