GTK FAQ Widgets

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
(Новая: In this part of the GTK+ programming tutorial, we will introduce some GTK+ widgets. Widgets are basic building blocks of a GUI application. Over the years, several widgets became a stan...)
(Удалено по требованию автора...)
 
Строка 1: Строка 1:
-
In this part of the GTK+ programming tutorial, we will introduce some GTK+ widgets.
 
-
Widgets are basic building blocks of a GUI application. Over the years, several widgets became a standard in all toolkits on all OS platforms. For example a button, a check box or a scroll bar.
 
-
The GTK+ toolkit's philosophy is to keep the number of widgets at a minimum level. More specialized widgets are created as custom GTK+ widgets.
 
-
 
-
== GtkButton ==
 
-
 
-
GtkButton is a simple widget, that is used to trigger an action.
 
-
 
-
<source lang="cpp">
 
-
#include &lt;gtk/gtk.h&gt;
 
-
 
-
 
-
int main( int argc, char *argv[])
 
-
{
 
-
  GtkWidget *window;
 
-
  GtkWidget *fixed;
 
-
  GtkWidget *button;
 
-
 
-
  gtk_init(&argc, &argv);
 
-
 
-
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
-
  gtk_window_set_title(GTK_WINDOW(window), "GtkButton");
 
-
  gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
 
-
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
 
-
 
-
  fixed = gtk_fixed_new();
 
-
  gtk_container_add(GTK_CONTAINER(window), fixed);
 
-
 
-
  button = gtk_button_new_with_label("Quit");
 
-
 
-
  gtk_fixed_put(GTK_FIXED(fixed), button, 50, 50);
 
-
  gtk_widget_set_size_request(button, 80, 35);
 
-
 
-
  g_signal_connect(G_OBJECT(button), "clicked",
 
-
      G_CALLBACK(gtk_main_quit), G_OBJECT(window));
 
-
 
-
  g_signal_connect_swapped(G_OBJECT(window), "destroy",
 
-
      G_CALLBACK(gtk_main_quit), NULL);
 
-
 
-
  gtk_widget_show_all(window);
 
-
 
-
  gtk_main();
 
-
 
-
  return 0;
 
-
}
 
-
</source>
 
-
 
-
The example shows a button that is positioned in a fixed container. The application quits, when we click on the  button.
 
-
 
-
<source lang="cpp">
 
-
button = gtk_button_new_with_label("Quit");
 
-
</source>
 
-
 
-
This code line creates a <b>GtkButton</b> with a label.
 
-
 
-
<source lang="cpp">
 
-
  g_signal_connect(G_OBJECT(button), "clicked",
 
-
      G_CALLBACK(gtk_main_quit), G_OBJECT(window));
 
-
 
-
</source>
 
-
 
-
Here we connect a <b>clicked</b> signal to the button. The signal will launch the <b>gtk_main_quit()</b> function, which terminates the application.
 
-
 
-
[[image: gtk_faq_gtkbutton.png | center]]
 
-
 
-
== GtkCheckButton ==
 
-
 
-
GtkCheckButton is a widget, that has two states. On and Off. The On state is visualised by a check mark.
 
-
 
-
<source lang="cpp">
 
-
#include &lt;gtk/gtk.h&gt;
 
-
 
-
 
-
void toggle_title(GtkWidget *widget, gpointer window)
 
-
{
 
-
  if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
 
-
      gtk_window_set_title(window, "GtkCheckButton");
 
-
  } else {
 
-
      gtk_window_set_title(window, "");
 
-
  }
 
-
}
 
-
 
-
int main(int argc, char** argv) {
 
-
 
-
  GtkWidget *window;
 
-
  GtkWidget *frame;
 
-
  GtkWidget *check;
 
-
 
-
  gtk_init(&argc, &argv);
 
-
 
-
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
-
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
 
-
  gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
 
-
  gtk_window_set_title(GTK_WINDOW(window), "GtkCheckButton");
 
-
 
-
 
-
  frame = gtk_fixed_new();
 
-
  gtk_container_add(GTK_CONTAINER(window), frame);
 
-
 
-
 
-
  check = gtk_check_button_new_with_label("Show title");
 
-
  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), TRUE);
 
-
  GTK_WIDGET_UNSET_FLAGS(check, GTK_CAN_FOCUS);
 
-
  gtk_fixed_put(GTK_FIXED(frame), check, 50, 50);
 
-
 
-
  g_signal_connect_swapped(window, "destroy",
 
-
          G_CALLBACK(gtk_main_quit), NULL);
 
-
 
-
  g_signal_connect(check, "clicked",
 
-
          G_CALLBACK(toggle_title), (gpointer) window);
 
-
 
-
  gtk_widget_show_all(window);
 
-
 
-
  gtk_main();
 
-
 
-
  return 0;
 
-
}
 
-
</source>
 
-
 
-
We will show a title depending on the state of the <b>GtkCheckButton</b>.
 
-
 
-
<source lang="cpp">
 
-
check = gtk_check_button_new_with_label("Show title");
 
-
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), TRUE);
 
-
</source>
 
-
 
-
The <b>GtkCheckButton</b> is created and is marked by default. Because we show a title by default.
 
-
 
-
<source lang="cpp">
 
-
GTK_WIDGET_UNSET_FLAGS(check, GTK_CAN_FOCUS);
 
-
</source>
 
-
 
-
This code line disables the focus. I simply didn't like the rectangle over the check button. It does not look good.
 
-
 
-
<source lang="cpp">
 
-
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
 
-
    gtk_window_set_title(window, "GtkCheckButton");
 
-
} else {
 
-
    gtk_window_set_title(window, "");
 
-
}
 
-
</source>
 
-
 
-
We show the title of the window, depending on the state of the <b>GtkCheckButton</b>.
 
-
 
-
[[image: gtk_faq_gtkcheckbutton.png | center]]
 
-
 
-
== GtkFrame ==
 
-
 
-
GtkFrame is a bin with a decorative frame and optional label
 
-
 
-
<source lang="cpp">
 
-
#include &lt;gtk/gtk.h&gt;
 
-
 
-
 
-
int main( int argc, char *argv[])
 
-
{
 
-
 
-
  GtkWidget *window;
 
-
  GtkWidget *table;
 
-
 
-
  GtkWidget *frame1;
 
-
  GtkWidget *frame2;
 
-
  GtkWidget *frame3;
 
-
  GtkWidget *frame4;
 
-
 
-
  gtk_init(&argc, &argv);
 
-
 
-
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
-
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
 
-
  gtk_window_set_default_size(GTK_WINDOW(window), 250, 250);
 
-
  gtk_window_set_title(GTK_WINDOW(window), "GtkFrame");
 
-
 
-
  gtk_container_set_border_width(GTK_CONTAINER(window), 10);
 
-
 
-
  table = gtk_table_new(2, 2, TRUE);
 
-
  gtk_table_set_row_spacings(GTK_TABLE(table), 10);
 
-
  gtk_table_set_col_spacings(GTK_TABLE(table), 10);
 
-
  gtk_container_add(GTK_CONTAINER(window), table);
 
-
 
-
 
-
  frame1 = gtk_frame_new("Shadow In");
 
-
  gtk_frame_set_shadow_type(GTK_FRAME(frame1), GTK_SHADOW_IN);
 
-
  frame2 = gtk_frame_new("Shadow Out");
 
-
  gtk_frame_set_shadow_type(GTK_FRAME(frame2), GTK_SHADOW_OUT);
 
-
  frame3 = gtk_frame_new("Shadow Etched In");
 
-
  gtk_frame_set_shadow_type(GTK_FRAME(frame3), GTK_SHADOW_ETCHED_IN);
 
-
  frame4 = gtk_frame_new("Shadow Etched Out");
 
-
  gtk_frame_set_shadow_type(GTK_FRAME(frame4), GTK_SHADOW_ETCHED_OUT);
 
-
 
-
 
-
  gtk_table_attach_defaults(GTK_TABLE(table), frame1, 0, 1, 0, 1);
 
-
  gtk_table_attach_defaults(GTK_TABLE(table), frame2, 0, 1, 1, 2);
 
-
  gtk_table_attach_defaults(GTK_TABLE(table), frame3, 1, 2, 0, 1);
 
-
  gtk_table_attach_defaults(GTK_TABLE(table), frame4, 1, 2, 1, 2);
 
-
 
-
  g_signal_connect_swapped(G_OBJECT(window), "destroy",
 
-
        G_CALLBACK(gtk_main_quit), G_OBJECT(window));
 
-
 
-
  gtk_widget_show_all(window);
 
-
 
-
  gtk_main();
 
-
 
-
  return 0;
 
-
}
 
-
</source>
 
-
 
-
The example shows four different frame types. The frames are attached into a table container.
 
-
 
-
<source lang="cpp">
 
-
frame1 = gtk_frame_new("Shadow In");
 
-
gtk_frame_set_shadow_type(GTK_FRAME(frame1), GTK_SHADOW_IN);
 
-
</source>
 
-
 
-
We create a <b>GtkFrame</b> and set it's shadow type. 
 
-
 
-
[[image: gtk_faq_frames.png | center]]
 
-
 
-
== GtkLabel ==
 
-
 
-
The GtkLabel widget displays text.
 
-
 
-
<source lang="cpp">
 
-
#include &lt;gtk/gtk.h&gt;
 
-
 
-
 
-
int main( int argc, char *argv[])
 
-
{
 
-
  GtkWidget *window;
 
-
  GtkWidget *label;
 
-
 
-
  gtk_init(&argc, &argv);
 
-
 
-
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
-
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
 
-
  gtk_window_set_title(GTK_WINDOW(window), "Nymphetamine");
 
-
  gtk_window_set_default_size(GTK_WINDOW(window), 350, 400);
 
-
 
-
  label = gtk_label_new("Cold was my soul\n\
 
-
Untold was the pain\n\
 
-
I faced when you left me\n\
 
-
A rose in the rain....\n\
 
-
So I swore to the razor\n\
 
-
That never, enchained\n\
 
-
Would your dark nails of faith\n\
 
-
Be pushed through my veins again\n\
 
-
\n\
 
-
Bared on your tomb\n\
 
-
I'm a prayer for your loneliness\n\
 
-
And would you ever soon\n\
 
-
Come above onto me?\n\
 
-
For once upon a time\n\
 
-
On the binds of your lowliness\n\
 
-
I could always find the slot for your sacred key ");
 
-
 
-
  gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
 
-
  gtk_container_add(GTK_CONTAINER(window), label);
 
-
 
-
  g_signal_connect_swapped(window, "destroy",
 
-
      G_CALLBACK (gtk_main_quit), NULL);
 
-
 
-
  gtk_widget_show_all(window);
 
-
 
-
  gtk_main();
 
-
 
-
  return 0;
 
-
}
 
-
 
-
</source>
 
-
 
-
The example shows lyrics of a song.
 
-
 
-
 
-
<source lang="cpp">
 
-
  label = gtk_label_new("Cold was my soul\n\
 
-
Untold was the pain\n\
 
-
...
 
-
</source>
 
-
 
-
We create a <b>GtkLabel</b> widget. We can create multiline text label by using a new line character. Note the escape character. We use a rather long string and we don't want to put all the text into  one line. In such cases, we can use an escape character.
 
-
 
-
<source lang="cpp">
 
-
gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
 
-
</source>
 
-
 
-
We center our label.
 
-
 
-
[[image: gtk_faq_label.png | center]]
 
-
 
-
In GtkLabel we can also use markup language. The next example shows how we can accomplish this.
 
-
 
-
<source lang="cpp">
 
-
#include &lt;gtk/gtk.h&gt;
 
-
 
-
 
-
int main( int argc, char *argv[])
 
-
{
 
-
  GtkWidget *window;
 
-
  GtkWidget *label;
 
-
 
-
  gtk_init(&argc, &argv);
 
-
 
-
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
-
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
 
-
  gtk_window_set_title(GTK_WINDOW(window), "markup label");
 
-
 
-
  char *str = "&lt;b&gt;ZetCode&lt;/b&gt;, Knowledge only matters";
 
-
 
-
  label = gtk_label_new(NULL);
 
-
  gtk_label_set_markup(GTK_LABEL(label), str);
 
-
 
-
  gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
 
-
  gtk_container_add(GTK_CONTAINER(window), label);
 
-
  gtk_widget_show(label);
 
-
 
-
  gtk_window_set_default_size(GTK_WINDOW(window), 300, 100);
 
-
 
-
  g_signal_connect(window, "destroy",
 
-
      G_CALLBACK (gtk_main_quit), NULL);
 
-
 
-
  gtk_widget_show(window);
 
-
 
-
  gtk_main();
 
-
 
-
  return 0;
 
-
}
 
-
 
-
</source>
 
-
 
-
The example shows a portion of text in bold.
 
-
 
-
<source lang="cpp">
 
-
char *str = "&lt;b&gt;ZetCode&lt;/b&gt;, Knowledge only matters";
 
-
</source>
 
-
 
-
This is the string, we are going to display.
 
-
 
-
<source lang="cpp">
 
-
  label = gtk_label_new(NULL);
 
-
  gtk_label_set_markup(GTK_LABEL(label), str);
 
-
</source>
 
-
 
-
We create an empty label and set a markup text for it.
 
-
 
-
[[image: gtk_faq_markup.png | center]]
 
-
 
-
== GtkComboBox ==
 
-
 
-
<b>GtkComboBox</b> is a widget that allows the user to choose from a list of options.
 
-
 
-
<source lang="cpp">
 
-
#include &lt;gtk/gtk.h&gt;
 
-
 
-
void combo_selected(GtkWidget *widget, gpointer window)
 
-
{
 
-
  gchar *text =  gtk_combo_box_get_active_text(GTK_COMBO_BOX(widget));
 
-
  gtk_label_set_text(GTK_LABEL(window), text);
 
-
  g_free(text);
 
-
}
 
-
 
-
 
-
int main( int argc, char *argv[])
 
-
{
 
-
 
-
  GtkWidget *window;
 
-
  GtkWidget *fixed;
 
-
  GtkWidget *combo;
 
-
  GtkWidget *label;
 
-
 
-
  gtk_init(&argc, &argv);
 
-
 
-
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
-
  gtk_window_set_title(GTK_WINDOW(window), "GtkCombo");
 
-
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
 
-
  gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
 
-
 
 
-
  fixed = gtk_fixed_new();
 
-
 
-
  combo = gtk_combo_box_new_text();
 
-
  gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Ubuntu");
 
-
  gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Mandriva");
 
-
  gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Fedora");
 
-
  gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Mint");
 
-
  gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Gentoo");
 
-
  gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Debian");
 
-
 
-
  gtk_fixed_put(GTK_FIXED(fixed), combo, 50, 50);
 
-
  gtk_container_add(GTK_CONTAINER(window), fixed);
 
-
 
-
  label = gtk_label_new("-");
 
-
  gtk_fixed_put(GTK_FIXED(fixed), label, 50, 110);
 
-
 
-
  g_signal_connect_swapped(G_OBJECT(window), "destroy",
 
-
        G_CALLBACK(gtk_main_quit), G_OBJECT(window));
 
-
 
-
  g_signal_connect(G_OBJECT(combo), "changed",
 
-
        G_CALLBACK(combo_selected), (gpointer) label);
 
-
 
-
  gtk_widget_show_all(window);
 
-
 
-
  gtk_main();
 
-
 
-
  return 0;
 
-
}
 
-
</source>
 
-
 
-
The example shows a combo box and a label. The combo box has a list of six options. These are the names of Linux Distros.
 
-
The label widget shows the selected option from the combo box. 
 
-
 
-
<source lang="cpp">
 
-
combo = gtk_combo_box_new_text();
 
-
gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Ubuntu");
 
-
gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Mandriva");
 
-
gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Fedora");
 
-
gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Mint");
 
-
gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Gentoo");
 
-
gtk_combo_box_append_text(GTK_COMBO_BOX(combo), "Debian");
 
-
</source>
 
-
 
-
We create a <b>GtkComboBox</b> and fill it with a list of Linux Distribution names.
 
-
 
-
<source lang="cpp">
 
-
 
-
label = gtk_label_new("-");
 
-
</source>
 
-
 
-
We also create a label widget.
 
-
 
-
<source lang="cpp">
 
-
gchar *text =  gtk_combo_box_get_active_text(GTK_COMBO_BOX(widget));
 
-
gtk_label_set_text(GTK_LABEL(window), text);
 
-
g_free(text);
 
-
</source>
 
-
 
-
We get the selected text and set the label text to it. The documentation to the combo box says, that <b>gtk_combo_box_get_active_text()</b> returns a newly allocated string containing the currently active text.
 
-
This means, that we must free the memory.
 
-
 
-
[[image: gtk_faq_gtkcombobox.png | center]]
 
-
 
-
== GtkHSeparator ==
 
-
 
-
The <b>GtkHSeparator</b> is is a horizontal separator. It is a kind of an ornament widget.
 
-
This widgets serves some design purposes. There is also a sister <b>GtkVSeparator</b> widget.
 
-
 
-
<source lang="cpp">
 
-
 
-
#include &lt;gtk/gtk.h&gt;
 
-
 
-
int main( int argc, char *argv[])
 
-
{
 
-
 
-
  GtkWidget *window;
 
-
  GtkWidget *label1;
 
-
  GtkWidget *label2;
 
-
  GtkWidget *hseparator;
 
-
  GtkWidget *vbox;
 
-
 
-
  gtk_init(&argc, &argv);
 
-
 
-
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
-
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
 
-
  gtk_window_set_title(GTK_WINDOW(window), "GtkHSeparator");
 
-
  gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
 
-
 
-
  gtk_container_set_border_width(GTK_CONTAINER(window), 20);
 
-
 
-
  label1 = gtk_label_new("Zinc is a moderately reactive, blue gray metal \
 
-
that tarnishes in moist air and burns in air with a bright bluish-green flame,\
 
-
giving off fumes of zinc oxide. It reacts with acids, alkalis and other non-metals.\
 
-
If not completely pure, zinc reacts with dilute acids to release hydrogen.");
 
-
 
-
  gtk_label_set_line_wrap(GTK_LABEL(label1), TRUE);
 
-
 
-
  label2 = gtk_label_new("Copper is an essential trace nutrient to all high \
 
-
plants and animals. In animals, including humans, it is found primarily in \
 
-
the bloodstream, as a co-factor in various enzymes, and in copper-based pigments. \
 
-
However, in sufficient amounts, copper can be poisonous and even fatal to organisms.");
 
-
 
-
  gtk_label_set_line_wrap(GTK_LABEL(label2), TRUE);
 
-
 
-
  vbox = gtk_vbox_new(FALSE, 10);
 
-
  gtk_container_add(GTK_CONTAINER(window), vbox);
 
-
 
-
  hseparator = gtk_hseparator_new();
 
-
 
-
  gtk_box_pack_start(GTK_BOX(vbox), label1, FALSE, TRUE, 0);
 
-
  gtk_box_pack_start(GTK_BOX(vbox), hseparator, FALSE, TRUE, 10);
 
-
  gtk_box_pack_start(GTK_BOX(vbox), label2, FALSE, TRUE, 0);
 
-
 
-
 
-
  g_signal_connect_swapped(G_OBJECT(window), "destroy",
 
-
        G_CALLBACK(gtk_main_quit), G_OBJECT(window));
 
-
 
-
  gtk_widget_show_all(window);
 
-
 
-
  gtk_main();
 
-
 
-
  return 0;
 
-
}
 
-
</source>
 
-
 
-
The code example shows definitions of two chemical elements. They are separated by a horizontal separator.
 
-
This makes the example more visually appealing.
 
-
 
-
<source lang="cpp">
 
-
  label1 = gtk_label_new("Zinc is a moderately reactive, blue gray metal \
 
-
that tarnishes in moist air and burns in air with a bright bluish-green flame,\
 
-
giving off fumes of zinc oxide. It reacts with acids, alkalis and other non-metals.\
 
-
If not completely pure, zinc reacts with dilute acids to release hydrogen.");
 
-
</source>
 
-
 
-
We create the first label, the definition of the Zinc element.
 
-
 
-
<source lang="cpp">
 
-
gtk_label_set_line_wrap(GTK_LABEL(label2), TRUE);
 
-
</source>
 
-
 
-
We wrap the text.
 
-
 
-
<source lang="cpp">
 
-
hseparator = gtk_hseparator_new();
 
-
</source>
 
-
 
-
We create a horizontal separator.
 
-
 
-
<source lang="cpp">
 
-
gtk_box_pack_start(GTK_BOX(vbox), label1, FALSE, TRUE, 0);
 
-
gtk_box_pack_start(GTK_BOX(vbox), hseparator, FALSE, TRUE, 10);
 
-
gtk_box_pack_start(GTK_BOX(vbox), label2, FALSE, TRUE, 0);
 
-
</source>
 
-
 
-
We place the separator between the labels.
 
-
 
-
[[image: gtk_faq_separator.png | center]]
 
-
 
-
== GtkEntry ==
 
-
 
-
<b>GtkEntry</b> is a single line text entry field. This widget is used to enter textual data.
 
-
 
-
<source lang="cpp">
 
-
#include &lt;gtk/gtk.h&gt;
 
-
 
-
 
-
int main(int argc, char *argv[]) {
 
-
 
-
  GtkWidget *window;
 
-
  GtkWidget *table;
 
-
 
-
  GtkWidget *label1;
 
-
  GtkWidget *label2;
 
-
  GtkWidget *label3;
 
-
 
-
  GtkWidget *entry1;
 
-
  GtkWidget *entry2;
 
-
  GtkWidget *entry3;
 
-
 
-
  gtk_init(&argc, &argv);
 
-
 
-
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
-
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
 
-
  gtk_window_set_title(GTK_WINDOW(window), "GtkEntry");
 
-
  gtk_container_set_border_width(GTK_CONTAINER(window), 10);
 
-
 
-
  table = gtk_table_new(3, 2, FALSE);
 
-
  gtk_container_add(GTK_CONTAINER(window), table);
 
-
 
-
  label1 = gtk_label_new("Name");
 
-
  label2 = gtk_label_new("Age");
 
-
  label3 = gtk_label_new("Occupation");
 
-
 
-
 
-
  gtk_table_attach(GTK_TABLE(table), label1, 0, 1, 0, 1,
 
-
      GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
 
-
  gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 1, 2,
 
-
      GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
 
-
  gtk_table_attach(GTK_TABLE(table), label3, 0, 1, 2, 3,
 
-
      GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
 
-
 
-
  entry1 = gtk_entry_new();
 
-
  entry2 = gtk_entry_new();
 
-
  entry3 = gtk_entry_new();
 
-
 
-
  gtk_table_attach(GTK_TABLE(table), entry1, 1, 2, 0, 1,
 
-
      GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
 
-
  gtk_table_attach(GTK_TABLE(table), entry2, 1, 2, 1, 2,
 
-
      GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
 
-
  gtk_table_attach(GTK_TABLE(table), entry3, 1, 2, 2, 3,
 
-
      GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
 
-
 
-
  gtk_widget_show(table);
 
-
 
-
  gtk_widget_show(label1);
 
-
  gtk_widget_show(label2);
 
-
  gtk_widget_show(label3);
 
-
 
-
  gtk_widget_show(entry1);
 
-
  gtk_widget_show(entry2);
 
-
  gtk_widget_show(entry3);
 
-
 
-
  gtk_widget_show(window);
 
-
 
-
  g_signal_connect(window, "destroy",
 
-
      G_CALLBACK(gtk_main_quit), NULL);
 
-
 
-
  gtk_main();
 
-
 
-
  return 0;
 
-
}
 
-
</source>
 
-
 
-
In our example we show three text entries and three labels.
 
-
 
-
<source lang="cpp">
 
-
table = gtk_table_new(3, 2, FALSE);
 
-
gtk_container_add(GTK_CONTAINER(window), table);
 
-
</source>
 
-
 
-
To organize our widgets, we use the table container widget.
 
-
 
-
<source lang="cpp">
 
-
entry1 = gtk_entry_new();
 
-
entry2 = gtk_entry_new();
 
-
entry3 = gtk_entry_new();
 
-
</source>
 
-
 
-
Here we create three text entries.
 
-
 
-
<source lang="cpp">
 
-
gtk_table_attach(GTK_TABLE(table), entry1, 1, 2, 0, 1,
 
-
    GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
 
-
gtk_table_attach(GTK_TABLE(table), entry2, 1, 2, 1, 2,
 
-
    GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
 
-
gtk_table_attach(GTK_TABLE(table), entry3, 1, 2, 2, 3,
 
-
    GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
 
-
</source>
 
-
 
-
We attacht the widgets to the table widget.
 
-
 
-
[[image: gtk_faq_gtkentry.png | center]]
 
-
 
-
== GtkImage ==
 
-
 
-
<b>GtkImage</b> is a widget used to display an image.
 
-
 
-
<source lang="cpp">
 
-
#include &lt;gtk/gtk.h&gt;
 
-
 
-
int main( int argc, char *argv[])
 
-
{
 
-
 
-
  GtkWidget *window;
 
-
  GtkWidget *image;
 
-
 
-
  gtk_init(&argc, &argv);
 
-
 
-
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
-
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
 
-
  gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
 
-
  gtk_window_set_title(GTK_WINDOW(window), "Red Rock");
 
-
  gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
 
-
 
-
  gtk_container_set_border_width(GTK_CONTAINER(window), 2);
 
-
 
-
  image = gtk_image_new_from_file("redrock.png");
 
-
  gtk_container_add(GTK_CONTAINER(window), image);
 
-
 
-
 
-
  g_signal_connect_swapped(G_OBJECT(window), "destroy",
 
-
        G_CALLBACK(gtk_main_quit), G_OBJECT(window));
 
-
 
-
  gtk_widget_show_all(window);
 
-
 
-
  gtk_main();
 
-
 
-
  return 0;
 
-
}
 
-
</source>
 
-
 
-
In our example we show an image of a castle. The castle is called Red Rock and is situated in the western part of Slovakia.
 
-
 
-
<source lang="cpp">
 
-
gtk_container_set_border_width(GTK_CONTAINER(window), 2);
 
-
</source>
 
-
 
-
We put a small 2px border around the picture. 
 
-
 
-
<source lang="cpp">
 
-
image = gtk_image_new_from_file("redrock.png");
 
-
gtk_container_add(GTK_CONTAINER(window), image);
 
-
</source>
 
-
 
-
We load an image from the file and add it into the container.
 
-
 
-
[[image: gtk_faq_gtkimage.jpg | center]]
 
-
 
-
== GtkStatusbar ==
 
-
<b>GtkStatusbar</b> displays status information. It is placed at the bottom of the application window.
 
-
<source lang="cpp">
 
-
#include &lt;gtk/gtk.h&gt;
 
-
 
-
 
-
void button_pressed(GtkWidget *widget, gpointer window)
 
-
{
 
-
  gchar *str;
 
-
  str = g_strdup_printf("Button %s clicked",
 
-
        gtk_button_get_label(GTK_BUTTON(widget)));
 
-
 
-
  gtk_statusbar_push(GTK_STATUSBAR(window),
 
-
        gtk_statusbar_get_context_id(GTK_STATUSBAR(window), str), str);
 
-
  g_free(str);
 
-
}
 
-
 
-
int main( int argc, char *argv[])
 
-
{
 
-
 
-
  GtkWidget *window;
 
-
  GtkWidget *fixed;
 
-
  GtkWidget *button1;
 
-
  GtkWidget *button2;
 
-
  GtkWidget *statusbar;
 
-
  GtkWidget *vbox;
 
-
 
-
  gtk_init(&argc, &argv);
 
-
 
-
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
-
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
 
-
  gtk_window_set_default_size(GTK_WINDOW(window), 280, 150);
 
-
  gtk_window_set_title(GTK_WINDOW(window), "GtkStatusbar");
 
-
 
-
  vbox = gtk_vbox_new(FALSE, 2);
 
-
 
-
  fixed = gtk_fixed_new();
 
-
  gtk_container_add(GTK_CONTAINER(window), vbox);
 
-
 
-
  gtk_box_pack_start(GTK_BOX(vbox), fixed, TRUE, TRUE, 1);
 
-
 
-
  button1 = gtk_button_new_with_label("OK");
 
-
  gtk_widget_set_size_request(button1, 80, 30 );
 
-
  button2 = gtk_button_new_with_label("Apply");
 
-
  gtk_widget_set_size_request(button2, 80, 30 );
 
-
 
-
  gtk_fixed_put(GTK_FIXED(fixed), button1, 50, 50);
 
-
  gtk_fixed_put(GTK_FIXED(fixed), button2, 150, 50);
 
-
 
-
 
-
  statusbar = gtk_statusbar_new();
 
-
  gtk_box_pack_start(GTK_BOX(vbox), statusbar, FALSE, TRUE, 1);
 
-
 
-
  g_signal_connect(G_OBJECT(button1), "clicked",
 
-
          G_CALLBACK(button_pressed), G_OBJECT(statusbar));
 
-
 
-
  g_signal_connect(G_OBJECT(button2), "clicked",
 
-
          G_CALLBACK(button_pressed), G_OBJECT(statusbar));
 
-
 
-
  g_signal_connect_swapped(G_OBJECT(window), "destroy",
 
-
        G_CALLBACK(gtk_main_quit), G_OBJECT(window));
 
-
 
-
  gtk_widget_show_all(window);
 
-
 
-
  gtk_main();
 
-
 
-
  return 0;
 
-
}
 
-
</source>
 
-
 
-
In our code example we show two buttons and a statusbar. If we click on the button, a message is displayed in the statusbar.
 
-
It says, which button we have pressed.
 
-
 
-
<source lang="cpp">
 
-
gchar *str;
 
-
str = g_strdup_printf("Button %s clicked",
 
-
      gtk_button_get_label(GTK_BUTTON(widget)));
 
-
</source>
 
-
 
-
We create a message.
 
-
 
-
<source lang="cpp">
 
-
gtk_statusbar_push(GTK_STATUSBAR(window),
 
-
      gtk_statusbar_get_context_id(GTK_STATUSBAR(window), str), str);
 
-
</source>
 
-
 
-
We show the message in the statusbar.
 
-
 
-
[[image: gtk_faq_gtkstatusbar.png | center]]
 
-
 
-
== GtkIconView ==
 
-
 
-
The GtkIconView is a widget which displays a list of icons in a grid.
 
-
 
-
<source lang="cpp">
 
-
#include &lt;gtk/gtk.h&gt;
 
-
#include &lt;assert.h&gt;
 
-
 
-
enum
 
-
{
 
-
  COL_DISPLAY_NAME,
 
-
  COL_PIXBUF,
 
-
  NUM_COLS
 
-
};
 
-
 
-
 
-
GtkTreeModel * init_model(void)
 
-
{
 
-
  GtkListStore *list_store;
 
-
  GdkPixbuf *p1, *p2, *p3, *p4;
 
-
  GtkTreeIter iter;
 
-
  GError *err = NULL;
 
-
 
-
 
-
  p1 = gdk_pixbuf_new_from_file("ubuntu.png", &err);
 
-
  p2 = gdk_pixbuf_new_from_file("gnumeric.png", &err);
 
-
  p3 = gdk_pixbuf_new_from_file("blender.png", &err);
 
-
  p4 = gdk_pixbuf_new_from_file("inkscape.png", &err);
 
-
 
-
  assert(err==NULL);
 
-
 
-
  list_store = gtk_list_store_new(NUM_COLS,
 
-
      G_TYPE_STRING, GDK_TYPE_PIXBUF);
 
-
 
-
  int i = 0;
 
-
  for (i; i < 50; i++) {
 
-
    gtk_list_store_append(list_store, &iter);
 
-
    gtk_list_store_set(list_store, &iter, COL_DISPLAY_NAME,
 
-
        "ubuntu", COL_PIXBUF, p1, -1);
 
-
    gtk_list_store_append(list_store, &iter);
 
-
    gtk_list_store_set(list_store, &iter, COL_DISPLAY_NAME,
 
-
        "gnumeric", COL_PIXBUF, p2, -1);
 
-
    gtk_list_store_append(list_store, &iter);
 
-
    gtk_list_store_set(list_store, &iter, COL_DISPLAY_NAME,
 
-
        "blender", COL_PIXBUF, p3, -1);
 
-
    gtk_list_store_append(list_store, &iter);
 
-
    gtk_list_store_set(list_store, &iter, COL_DISPLAY_NAME,
 
-
        "inkscape", COL_PIXBUF, p4, -1);
 
-
  }
 
-
 
-
  return GTK_TREE_MODEL(list_store);
 
-
}
 
-
 
-
int main (int argc, char *argv[])
 
-
{
 
-
  GtkWidget *window;
 
-
  GtkWidget *icon_view;
 
-
  GtkWidget *sw;
 
-
 
-
  gtk_init (&argc, &argv);
 
-
 
-
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
-
 
-
  gtk_window_set_title(GTK_WINDOW (window), "Icon View");
 
-
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
 
-
  gtk_container_set_border_width(GTK_CONTAINER(window), 10);
 
-
  gtk_widget_set_size_request(window, 350, 330);
 
-
 
-
  sw = gtk_scrolled_window_new(NULL, NULL);
 
-
  gtk_container_add(GTK_CONTAINER (window), sw);
 
-
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
 
-
      GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
 
-
  gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw),
 
-
      GTK_SHADOW_IN);
 
-
 
-
  icon_view = gtk_icon_view_new_with_model(init_model());
 
-
  gtk_container_add(GTK_CONTAINER(sw), icon_view);
 
-
 
-
  gtk_icon_view_set_text_column(GTK_ICON_VIEW(icon_view),
 
-
      COL_DISPLAY_NAME);
 
-
  gtk_icon_view_set_pixbuf_column(GTK_ICON_VIEW(icon_view),
 
-
      COL_PIXBUF);
 
-
  gtk_icon_view_set_selection_mode(GTK_ICON_VIEW(icon_view),
 
-
      GTK_SELECTION_MULTIPLE);
 
-
 
-
  g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
 
-
 
-
  gtk_widget_show_all(window);
 
-
 
-
  gtk_main();
 
-
 
-
  return 0;
 
-
}
 
-
 
-
</source>
 
-
 
-
The example will display 200 icons. The icons represent four prominent open source projects.
 
-
 
-
<source lang="cpp">
 
-
p1 = gdk_pixbuf_new_from_file("ubuntu.png", &err);
 
-
p2 = gdk_pixbuf_new_from_file("gnumeric.png", &err);
 
-
p3 = gdk_pixbuf_new_from_file("blender.png", &err);
 
-
p4 = gdk_pixbuf_new_from_file("inkscape.png", &err);
 
-
</source>
 
-
 
-
We load 4 icons from the disk.
 
-
 
-
<source lang="cpp">
 
-
list_store = gtk_list_store_new(NUM_COLS,
 
-
    G_TYPE_STRING, GDK_TYPE_PIXBUF);
 
-
</source>
 
-
 
-
We will store textual and pixbuf data.
 
-
 
-
<source lang="cpp">
 
-
gtk_list_store_append(list_store, &iter);
 
-
gtk_list_store_set(list_store, &iter, COL_DISPLAY_NAME,
 
-
    "ubuntu", COL_PIXBUF, p1, -1);
 
-
</source>
 
-
 
-
This code adds a new icon to the icon view.
 
-
 
-
<source lang="cpp">
 
-
  icon_view = gtk_icon_view_new_with_model(init_model());
 
-
  gtk_container_add(GTK_CONTAINER(sw), icon_view);
 
-
 
-
  gtk_icon_view_set_text_column(GTK_ICON_VIEW(icon_view),
 
-
      COL_DISPLAY_NAME);
 
-
  gtk_icon_view_set_pixbuf_column(GTK_ICON_VIEW(icon_view),
 
-
      COL_PIXBUF);
 
-
</source>
 
-
 
-
We create a <b>GtkIconView</b> widget and set an icon and it's name to the icon view.
 
-
 
-
[[image: gtk_faq_iconview.png | center]]
 
-
 
-
[[Категория:GTK+]]
 

Текущая версия на 11:28, 7 апреля 2009