Foundations of GTK+ Development
Total Page:16
File Type:pdf, Size:1020Kb
THE EXPERT’S VOICE® IN OPEN SOURCE Foundations of GTK+ Development Build sophisticated graphical applications using one of the world's most powerful cross-platform toolkits! Andrew Krause Foundations of GTK+ Development ■■■ Andrew Krause Foundations of GTK+ Development Copyright © 2007 by Andrew Krause All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-793-4 ISBN-10 (pbk): 1-59059-793-1 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Lead Editors: Jason Gilmore, Matt Wade Technical Reviewers: Christiana Evelyn Johnson, Micah Carrick Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Jeff Pepper, Paul Sarknas, Dominic Shakeshaft, Jim Sumser, Matt Wade Project Manager: Richard Dal Porto Copy Edit Manager: Nicole Flores Copy Editor: Heather Lang Assistant Production Director: Kari Brooks-Copony Production Editor: Katie Stence Compositor: Pat Christenson Proofreader: Elizabeth Berry Indexer: Ann Rogers Artist: April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com in the Source Code/ Download section or at the official book site, http://www.gtkbook.com. I dedicate this book to Mrs. Kaminsky, for never allowing me to settle for anything but my best. I hope you can look at this book and see everything that you have done for me, even though I have yet to broaden the scope of my writing beyond technology. Contents at a Glance About the Author . .xvii Acknowledgments. xix Introduction . xxi ■CHAPTER 1 Getting Started . 1 ■CHAPTER 2 Your First GTK+ Applications . 15 ■CHAPTER 3 Container Widgets . 43 ■CHAPTER 4 Basic Widgets . 75 ■CHAPTER 5 Dialogs. 111 ■CHAPTER 6 Using GLib. 159 ■CHAPTER 7 The Text View Widget . 219 ■CHAPTER 8 The Tree View Widget . 261 ■CHAPTER 9 Menus and Toolbars . 315 ■CHAPTER 10 Dynamic User Interfaces . 355 ■CHAPTER 11 Creating Custom Widgets. 381 ■CHAPTER 12 Additional GTK+ Widgets . 431 ■CHAPTER 13 Putting It All Together . 471 ■APPENDIX A GTK+ Properties . 481 ■APPENDIX B GTK+ Signals . 529 ■APPENDIX C GTK+ Styles . 565 ■APPENDIX D GTK+ Stock Items. 583 ■APPENDIX E GError Types . 587 ■APPENDIX F Exercise Solutions and Hints. 595 ■INDEX . 605 v Contents About the Author . xvii Acknowledgments. xix Introduction . xxi ■CHAPTER 1 Getting Started. 1 A Brief History of GTK+ . 2 The X Window System. 2 GTK+ and Supporting Libraries . 3 GLib . 5 GObject . 6 GDK . 7 GdkPixbuf . 7 Pango . 8 ATK . 9 Language Bindings. 9 Installing GTK+ . 10 Summary . 12 ■CHAPTER 2 Your First GTK+ Applications . 15 Hello World. 15 Initializing GTK+ . 16 Widget Hierarchy. 17 GTK+ Windows . 19 The Main Loop Function. 20 Using GCC and pkg-config to Compile . 21 Extending “Hello World” . 23 Signals and Callbacks . 27 Connecting the Signal . 27 Callback Functions . 28 Emitting and Stopping Signals . 29 Events . 29 Event Types . 31 Using Specific Event Structures . 31 vii viii ■CONTENTS Further GTK+ Functions . 32 GtkWidget Functions. 32 GtkWindow Functions. 33 Process Pending Events. 35 Buttons . 36 Widget Properties . 38 Test Your Understanding . 40 Summary . 41 ■CHAPTER 3 Container Widgets . 43 GtkContainer . ..