|
Packit |
1470ea |
|
|
Packit |
1470ea |
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="combobox.c" xml:lang="gl">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">ComboBox (C)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="c#menu-combo-toolbar"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-08-20" status="draft"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Monica Kochofar</name>
|
|
Packit |
1470ea |
<email its:translate="no">monicakochofar@gmail.com</email>
|
|
Packit |
1470ea |
<years>2012</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
<credit type="editor">
|
|
Packit |
1470ea |
<name>Tiffany Antopolski</name>
|
|
Packit |
1470ea |
<email its:translate="no">tiffany.antopolski@gmail.com</email>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<desc>A widget used to choose from a list of items</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Fran Dieguez</mal:name>
|
|
Packit |
1470ea |
<mal:email>frandieguez@gnome.org</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012-2013.</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>ComboBox</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/combobox.png"/>
|
|
Packit |
1470ea |
This ComboBox prints to the terminal when you change your selection.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
#include <gtk/gtk.h>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* This is the callback function. It is a handler function which reacts to the
|
|
Packit |
1470ea |
* signal. In this case, if the row selected is not the first one of the
|
|
Packit |
1470ea |
* ComboBox, we write its value in the terminal for the user.
|
|
Packit |
1470ea |
*/
|
|
Packit |
1470ea |
static void
|
|
Packit |
1470ea |
on_changed (GtkComboBox *widget,
|
|
Packit |
1470ea |
gpointer user_data)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
GtkComboBox *combo_box = widget;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (gtk_combo_box_get_active (combo_box) != 0) {
|
|
Packit |
1470ea |
gchar *distro = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT(combo_box));
|
|
Packit |
1470ea |
g_print ("You chose %s\n", distro);
|
|
Packit |
1470ea |
g_free (distro);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
static void
|
|
Packit |
1470ea |
activate (GtkApplication *app,
|
|
Packit |
1470ea |
gpointer user_data)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
gint i;
|
|
Packit |
1470ea |
GtkWidget *view;
|
|
Packit |
1470ea |
GtkWidget *window;
|
|
Packit |
1470ea |
GtkWidget *combo_box;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Create a window with a title, border width, and a default size. Setting the
|
|
Packit |
1470ea |
* size to -1 means to use the "natural" default size.
|
|
Packit |
1470ea |
* (the size request of the window)
|
|
Packit |
1470ea |
*/
|
|
Packit |
1470ea |
window = gtk_application_window_new (app);
|
|
Packit |
1470ea |
gtk_window_set_title (GTK_WINDOW (window), "Welcome to GNOME");
|
|
Packit |
1470ea |
gtk_window_set_default_size (GTK_WINDOW (window), 200, -1);
|
|
Packit |
1470ea |
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Create the combo box and append your string values to it. */
|
|
Packit |
1470ea |
combo_box = gtk_combo_box_text_new ();
|
|
Packit |
1470ea |
const char *distros[] = {"Select distribution", "Fedora", "Mint", "Suse"};
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* G_N_ELEMENTS is a macro which determines the number of elements in an array.*/
|
|
Packit |
1470ea |
for (i = 0; i < G_N_ELEMENTS (distros); i++){
|
|
Packit |
1470ea |
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), distros[i]);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Choose to set the first row as the active one by default, from the beginning */
|
|
Packit |
1470ea |
gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Connect the signal emitted when a row is selected to the appropriate
|
|
Packit |
1470ea |
* callback function.
|
|
Packit |
1470ea |
*/
|
|
Packit |
1470ea |
g_signal_connect (combo_box,
|
|
Packit |
1470ea |
"changed",
|
|
Packit |
1470ea |
G_CALLBACK (on_changed),
|
|
Packit |
1470ea |
NULL);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Add it to the window */
|
|
Packit |
1470ea |
gtk_container_add (GTK_CONTAINER (window), combo_box);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
gtk_widget_show_all (window);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
int
|
|
Packit |
1470ea |
main (int argc, char **argv)
|
|
Packit |
1470ea |
{
|
|
Packit |
1470ea |
GtkApplication *app;
|
|
Packit |
1470ea |
int status;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
|
|
Packit |
1470ea |
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
|
|
Packit |
1470ea |
status = g_application_run (G_APPLICATION (app), argc, argv);
|
|
Packit |
1470ea |
g_object_unref (app);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
return status;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Neste exemplo empregaremos o seguinte:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/3.4/GtkApplication.html">GtkApplication</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/3.4/GtkWindow.html">GtkWindow</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkComboBoxText.html">GtkComboBoxText</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</page>
|