Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

apress foundations_of gtk plus development 2007 phần 4 ppsx
Nội dung xem thử
Mô tả chi tiết
CHAPTER 5 ■ DIALOGS 157
After learning about the built-in dialogs, you learned about multiple types of built-in
dialogs provided by GTK+:
• Message dialog (GtkMessageDialog): Provide a general message, error message, warning,
or simple yes-no question to the user.
• About dialog (GtkAboutDialog): Show information about the application including version, copyright, license, authors, and others.
• File chooser dialog (GtkFileChooserDialog): Allow the user to choose a file, choose multiple files, save a file, choose a directory, or create a directory.
• Color selection dialog (GtkColorSelectionDialog): Allow the user to choose a color along
with an optional opacity value.
• Font selection dialog (GtkFontSelectionDialog): Allow the user to choose a font and its
size and style properties.
The last section of this chapter showed you a widget called GtkAssistant, which was introduced in GTK+ 2.10. It allows you to create dialogs with multiple stages. It is important to note
that assistants are not actually a type of GtkDialog widget but are directly derived from the
GtkWindow class. This means that you have to handle these by connecting signals in the main
loop instead of calling gtk_dialog_run().
You now have a firm understanding of many important aspects of GTK+. Before we continue on to more advanced widgets, the next chapter will give you a thorough understanding of
GLib. Chapter 6 will cover many GLib data types, idle functions, timeouts, process spawning,
threads, dynamic modules, file utilities, and timers, as well as other important topics.
7931ch05.fm Page 157 Friday, February 9, 2007 12:36 AM
7931ch05.fm Page 158 Friday, February 9, 2007 12:36 AM
159
■ ■ ■
CHAPTER 6
Using GLib
Now that you have a reasonable grasp of GTK+ and a number of simple widgets, it is time to
move to another library. GTK+ depends on GLib, a general-purpose library that provides many
kinds of utility functions, data types, and wrapper functions. In fact, you have already used
some aspects of GLib in previous chapters.
GLib can be run independently of any other library, which means that some of the examples in this chapter do not require the GTK+, GDK, and Pango libraries. However, GTK+ does
depend on GLib.
Not all of the topics throughout this chapter will be used in later chapters, but all are useful
in many GTK+ applications in the real world. Many of the topics are used for very specific tasks.
For example, GModule can be used to create a plug-in system for your application or open a
binary’s symbol table.
The goal of Chapter 6 is not to be a comprehensive guide to everything in GLib. When
using a feature shown in this chapter, you should reference the GLib API documentation for
more information. However, this chapter will introduce you to a wide array of important features so that you have a general understanding of what GLib provides.
In this chapter, you will learn the following:
• The basic data types, macros, and utility functions provided by GLib
• How to give textual feedback to the user about errors and warnings that occur within
your application
• Memory management schemes provided by GLib such as memory slices, g_malloc(),
and friends
• Various utility functions provided by GLib for timing, file manipulation, reading directory contents, and working with the file system
• How the main loop is implemented in GLib and how it implements timeout and idle
functions
• Data structures provided by GLib including strings, linked lists, binary trees, arrays, hash
tables, quarks, keyed data lists, and n-ary trees
• How to us GIOChannel to manipulate files and create pipes as well as how to spawn asynchronous and synchronous processes
• How to dynamically load shared libraries with GModule
7931ch06.fm Page 159 Wednesday, March 7, 2007 8:52 PM
160 CHAPTER 6 ■ USING GLIB
GLib Basics
GLib is a general-purpose utility library that is used to implement many useful nongraphical
features. While it is required by GTK+, it can also be used independently. Because of this, some
applications use GLib without GTK+ and other supporting libraries for the many capabilities it
provides.
One of the main benefits of using GLib is that it provides a cross-platform interface that
allows your code to be run on any of its supported operating systems with little to no rewriting
of code. You will see this illustrated in the examples throughout the rest of this chapter.
Basic Data Types
You have been using many data types in previous chapters that originate in GLib. These data
types provide a set of common data types that are portable to not only other platforms, but also
other programming languages wrapping GTK+.
Table 6-1 is a list of the basic data types provided by GLib. You can find all of the type definitions in the gtypes.h header file. More advanced data structures will be covered later, in the
“Data Types” section.
Table 6-1. GLib Data Types
Type Description
gboolean Since C does not provide a Boolean data type, GLib provides gboolean, which
is set to either TRUE or FALSE.
gchar (guchar) Signed and unsigned data types corresponding to the standard C character type.
gconstpointer A pointer to constant data that is untyped. The data that this type points to
should not be changed. Therefore, it is typically used in function prototypes
to indicate that the function will not alter the data to which it points.
gdouble A data type corresponding to the standard C double type. Possible values are
within the range from -G_MAXDOUBLE to G_MAXDOUBLE. G_MINDOUBLE refers to the
minimum positive value that gdouble can hold.
gfloat A data type corresponding to the standard C float type. Possible values are
within the range from -G_MAXFLOAT to G_MAXFLOAT. G_MINFLOAT refers to the
minimum positive value that gfloat can hold.
gint (guint) Signed and unsigned data types corresponding to the standard C int
type. Signed gint values must be within the range from G_MININT to
G_MAXINT. The maximum guint value is given by G_MAXUINT.
gint8 (guint8) Signed and unsigned integers that are designed to be 8 bits on all
platforms. Signed values are within the range from -128 to 127 (G_MININT8
to G_MAXINT8) and unsigned values from 0 to 255 (G_MAXUINT8).
gint16 (guint16) Signed and unsigned integers that are designed to be 16 bits on all
platforms. Signed values are within the range from -32,768 to 32,767
(G_MININT16 to G_MAXINT16) and unsigned values from 0 to 65,535
(G_MAXUINT16).
7931ch06.fm Page 160 Wednesday, March 7, 2007 8:52 PM