Siêu thị PDFTải ngay đi em, trời tối mất

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 3 ppt
PREMIUM
Số trang
60
Kích thước
1.7 MB
Định dạng
PDF
Lượt xem
1585

apress foundations_of gtk plus development 2007 phần 3 ppt

Nội dung xem thử

Mô tả chi tiết

CHAPTER 4 ■ BASIC WIDGETS 97

widget_class "GtkWindow.*.GtkLabel" style "stylename"

In addition to basic style directives, the following list shows other top-level directives sup￾ported in RC files:

• include: Include another resource file. You can specify either an absolute or relative

filename.

• module_path: A list of paths separated by colons that will be searched for theme engines

referenced by the RC file.

• *pixmap_path: A list of paths separated by colons that will be searched for theme engines

referenced by the RC file.

If you are planning on using RC files in an application, you should make sure to provide an

example file to the user. You can use the pound (#) symbol to add comments to an RC file to

give the user help in editing the content.

This section only gave you a very basic introduction to RC files. For more information, you

should reference Appendix C. There are also a lot of resources for learning about RC files and

themes with GTK+ found at http://art.gnome.org.

Additional Buttons

While the GtkButton widget allows you to create your own custom buttons, GTK+ provides

three additional button widgets that are at your disposal: the color selection button, file

chooser button, and font selection button.

Each of the sections covering these three widgets will also cover other important concepts

such as the GdkColor structure, file filters, and Pango fonts. These concepts will be used in later

chapters, so it is a good idea to get a grasp of them now.

Color Buttons

The GtkColorButton widget provides a simple way for you to allow your users to select a specific

color. These colors can be specified as six-digit hexadecimal values or the RGB value. The color

button itself displays the selected color in a rectangular block set as the child widget of the but￾ton. An example of this can be viewed in Figure 4-9.

7931ch04.fm Page 97 Monday, February 5, 2007 8:25 PM

98 CHAPTER 4 ■ BASIC WIDGETS

Figure 4-9. A color selection dialog

A GtkColorButton Example

When clicked, the color button opens a dialog that allows the user to enter in the color value or

browse for a choice on the color wheel. The color wheel is provided so the user is not required

to know the numeric values of the colors. Listing 4-9 shows how to use the GtkColorButton wid￾get in an application.

Listing 4-9. Color Buttons and GdkColors (colorbuttons.c)

#include <gtk/gtk.h>

static void color_changed (GtkColorButton*, GtkWidget*);

int main (int argc,

char *argv[])

{

GtkWidget *window, *button, *label, *hbox;

GdkColor color;

gtk_init (&argc, &argv);

7931ch04.fm Page 98 Monday, February 5, 2007 8:25 PM

CHAPTER 4 ■ BASIC WIDGETS 99

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

gtk_window_set_title (GTK_WINDOW (window), "Color Button");

gtk_container_set_border_width (GTK_CONTAINER (window), 10);

/* Set the initial color as #003366 and set the dialog title. */

gdk_color_parse ("#003366", &color);

button = gtk_color_button_new_with_color (&color);

gtk_color_button_set_title (GTK_COLOR_BUTTON (button), "Select a Color");

label = gtk_label_new ("Look at my color!");

gtk_widget_modify_fg (label, GTK_STATE_NORMAL, &color);

g_signal_connect (G_OBJECT (button), "color_set",

G_CALLBACK (color_changed),

(gpointer) label);

hbox = gtk_hbox_new (FALSE, 5);

gtk_box_pack_start_defaults (GTK_BOX (hbox), button);

gtk_box_pack_start_defaults (GTK_BOX (hbox), label);

gtk_container_add (GTK_CONTAINER (window), hbox);

gtk_widget_show_all (window);

gtk_main ();

return 0;

}

/* Retrieve the selected color and set it as the GtkLabel's foreground color. */

static void

color_changed (GtkColorButton *button,

GtkWidget *label)

{

GdkColor color;

gtk_color_button_get_color (button, &color);

gtk_widget_modify_fg (label, GTK_STATE_NORMAL, &color);

}

In most cases, you will want to create a GtkColorButton with an initial color value, which is

done by specifying a GdkColor object to gtk_color_button_new_with_color(). The default

color, if none is provided, is opaque black with the alpha option disabled.

7931ch04.fm Page 99 Monday, February 5, 2007 8:25 PM

100 CHAPTER 4 ■ BASIC WIDGETS

Storing Colors in GdkColor

GdkColor is a structure that stores red, green, and blue values for a color as shown in the follow￾ing code snippet. The pixel object automatically stores the index of the color when it is

allocated in a color map, so there is usually no need for you to alter this value.

struct GdkColor

{

guint32 pixel;

guint16 red;

guint16 green;

guint16 blue;

};

After creating a new GdkColor object, if you already know the red, green, and blue values of

the color, you can specify them in the following manner. Red, green, and blue values are stored

as unsigned integer values ranging from 0 to 65,535, where 65,535 indicates full color intensity.

For example, the following color refers to white:

color.red = 65535;

color.green = 65535;

color.blue = 65535;

In most cases, you will be more familiar with the six-digit hexadecimal value for the color,

such as #FFFFFF that refers to the color white. Therefore, GDK provides gdk_color_parse(),

which parses the hexadecimal color into the correct RGB values. This function was used in

Listing 4-9.

gboolean gdk_color_parse (const gchar *color_string,

GdkColor *color);

Using the Color Button

After setting your initial color, you can choose the title that will be given to the color selection

dialog with gtk_color_button_set_title(). By default, the title is “Pick a Color”, so it is not

necessary to set this value if you are content with this title.

void gtk_color_button_set_title (GtkColorButton *button,

const gchar *title);

The color selection dialog, covered in the next chapter in more detail, is shown when the

user clicks the button. It allows the user to change the selected color. You can view the color

selection dialog in Figure 4-9.

When the color value is changed, the color-set signal is emitted for the widget. In Listing 4-5,

the signal is caught and the foreground color of a GtkLabel changed with gtk_widget_modify_fg()

as follows:

gtk_color_button_get_color (button, &color);

gtk_widget_modify_fg (label, GTK_STATE_NORMAL, &color);

7931ch04.fm Page 100 Monday, February 5, 2007 8:25 PM

CHAPTER 4 ■ BASIC WIDGETS 101

In Listing 4-9, the foreground color was set in the normal widget state, which is what state

all labels will be in, by and large, unless they are selectable. There are five options for the

GtkStateType enumeration that can be used in gtk_widget_modify_fg(), which were presented

in the “Widget Styles” section. You can reset the widget’s foreground color to the default value

by passing a NULL color.

File Chooser Buttons

The GtkFileChooserButton widget provides an easy method for you to ask users to choose a file

or a folder. It implements the functionality of the GtkFileChooser interface, the file selection

framework provided by GTK+. Figure 4-10 shows a file chooser button set to select a folder and

a button set to select a file.

Figure 4-10. File chooser buttons

When the user clicks a GtkFileChooserButton, an instance of GtkFileChooserDialog is

opened that allows the user to browse and select one file or one folder, depending on the type

of button you created.

■Note You will not learn how to use the GtkFileChooserDialog widget until Chapter 5, but you do not

need to directly interface with it at this point, because GtkFileChooserButton will handle all interactions

with the dialog.

A GtkFileChooserButton Example

You are able to change basic settings such as the currently selected file, the current folder, and

the title of the file selection window. Listing 4-10 shows you how to use both types of file

chooser buttons.

7931ch04.fm Page 101 Monday, February 5, 2007 8:25 PM

102 CHAPTER 4 ■ BASIC WIDGETS

Listing 4-10. Using the File Chooser Button (filechooserbuttons.c)

#include <gtk/gtk.h>

static void folder_changed (GtkFileChooser*, GtkFileChooser*);

static void file_changed (GtkFileChooser*, GtkLabel*);

int main (int argc,

char *argv[])

{

GtkWidget *window, *chooser1, *chooser2, label, *vbox;

GtkFileFilter *filter;

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

gtk_window_set_title (GTK_WINDOW (window), "File Chooser Button");

gtk_container_set_border_width (GTK_CONTAINER (window), 10);

label = gtk_label_new ("");

/* Create two buttons, one to select a folder and one to select a file. */

chooser1 = gtk_file_chooser_button_new ("Chooser a Folder",

GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);

chooser2 = gtk_file_chooser_button_new ("Chooser a Folder",

GTK_FILE_CHOOSER_ACTION_OPEN);

/* Monitor when the selected folder or file are changed. */

g_signal_connect (G_OBJECT (chooser1), "selection_changed",

G_CALLBACK (folder_changed),

(gpointer) chooser2);

g_signal_connect (G_OBJECT (chooser2), "selection_changed",

G_CALLBACK (file_changed),

(gpointer) label);

/* Set both file chooser buttons to the location of the user's home directory. */

gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser1),

g_get_home_dir());

gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser2),

g_get_home_dir());

7931ch04.fm Page 102 Monday, February 5, 2007 8:25 PM

Tải ngay đi em, còn do dự, trời tối mất!