1998-06-25 08:57:29 +00:00
|
|
|
|
2004-03-06 03:38:59 +00:00
|
|
|
#include <config.h>
|
1999-02-22 08:51:02 +00:00
|
|
|
#include <gtk/gtk.h>
|
1998-06-25 08:57:29 +00:00
|
|
|
|
1999-02-22 08:51:02 +00:00
|
|
|
/* User clicked the "Add List" button. */
|
|
|
|
void button_add_clicked( gpointer data )
|
|
|
|
{
|
|
|
|
int indx;
|
|
|
|
|
|
|
|
/* Something silly to add to the list. 4 rows of 2 columns each */
|
|
|
|
gchar *drink[4][2] = { { "Milk", "3 Oz" },
|
|
|
|
{ "Water", "6 l" },
|
|
|
|
{ "Carrots", "2" },
|
|
|
|
{ "Snakes", "55" } };
|
|
|
|
|
|
|
|
/* Here we do the actual adding of the text. It's done once for
|
|
|
|
* each row.
|
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
for (indx = 0; indx < 4; indx++)
|
|
|
|
gtk_clist_append ((GtkCList *)data, drink[indx]);
|
1999-02-22 08:51:02 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* User clicked the "Clear List" button. */
|
|
|
|
void button_clear_clicked( gpointer data )
|
|
|
|
{
|
|
|
|
/* Clear the list using gtk_clist_clear. This is much faster than
|
|
|
|
* calling gtk_clist_remove once for each row.
|
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_clist_clear ((GtkCList *)data);
|
1999-02-22 08:51:02 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The user clicked the "Hide/Show titles" button. */
|
|
|
|
void button_hide_show_clicked( gpointer data )
|
|
|
|
{
|
|
|
|
/* Just a flag to remember the status. 0 = currently visible */
|
|
|
|
static short int flag = 0;
|
|
|
|
|
|
|
|
if (flag == 0)
|
|
|
|
{
|
|
|
|
/* Hide the titles and set the flag to 1 */
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_clist_column_titles_hide ((GtkCList *)data);
|
1999-02-22 08:51:02 +00:00
|
|
|
flag++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Show the titles and reset flag to 0 */
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_clist_column_titles_show ((GtkCList *)data);
|
1999-02-22 08:51:02 +00:00
|
|
|
flag--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we come here, then the user has selected a row in the list. */
|
|
|
|
void selection_made( GtkWidget *clist,
|
|
|
|
gint row,
|
|
|
|
gint column,
|
|
|
|
GdkEventButton *event,
|
|
|
|
gpointer data )
|
|
|
|
{
|
|
|
|
gchar *text;
|
1998-06-25 08:57:29 +00:00
|
|
|
|
1999-02-22 08:51:02 +00:00
|
|
|
/* Get the text that is stored in the selected row and column
|
|
|
|
* which was clicked in. We will receive it as a pointer in the
|
|
|
|
* argument text.
|
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_clist_get_text (GTK_CLIST (clist), row, column, &text);
|
1999-02-22 08:51:02 +00:00
|
|
|
|
|
|
|
/* Just prints some information about the selected row */
|
2002-02-19 01:25:26 +00:00
|
|
|
g_print ("You selected row %d. More specifically you clicked in "
|
|
|
|
"column %d, and the text in this cell is %s\n\n",
|
|
|
|
row, column, text);
|
1999-02-22 08:51:02 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc,
|
|
|
|
gchar *argv[] )
|
1998-06-25 08:57:29 +00:00
|
|
|
{
|
1999-02-22 08:51:02 +00:00
|
|
|
GtkWidget *window;
|
|
|
|
GtkWidget *vbox, *hbox;
|
1999-04-10 12:55:24 +00:00
|
|
|
GtkWidget *scrolled_window, *clist;
|
1999-02-22 08:51:02 +00:00
|
|
|
GtkWidget *button_add, *button_clear, *button_hide_show;
|
|
|
|
gchar *titles[2] = { "Ingredients", "Amount" };
|
1998-06-25 08:57:29 +00:00
|
|
|
|
|
|
|
gtk_init(&argc, &argv);
|
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
window=gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
2002-02-16 23:52:30 +00:00
|
|
|
gtk_widget_set_size_request (GTK_WIDGET (window), 300, 150);
|
1998-06-25 08:57:29 +00:00
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "GtkCList Example");
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_connect (G_OBJECT (window), "destroy",
|
|
|
|
G_CALLBACK (gtk_main_quit),
|
|
|
|
NULL);
|
1998-06-25 08:57:29 +00:00
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
vbox=gtk_vbox_new (FALSE, 5);
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), vbox);
|
|
|
|
gtk_widget_show (vbox);
|
1998-06-25 08:57:29 +00:00
|
|
|
|
1999-04-10 12:55:24 +00:00
|
|
|
/* Create a scrolled window to pack the CList widget into */
|
|
|
|
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
|
|
|
|
GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
|
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
|
1999-04-10 12:55:24 +00:00
|
|
|
gtk_widget_show (scrolled_window);
|
|
|
|
|
|
|
|
/* Create the CList. For this example we use 2 columns */
|
2002-02-19 01:25:26 +00:00
|
|
|
clist = gtk_clist_new_with_titles (2, titles);
|
1998-06-25 08:57:29 +00:00
|
|
|
|
|
|
|
/* When a selection is made, we want to know about it. The callback
|
1998-12-10 17:31:04 +00:00
|
|
|
* used is selection_made, and its code can be found further down */
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_connect (G_OBJECT (clist), "select_row",
|
|
|
|
G_CALLBACK (selection_made),
|
|
|
|
NULL);
|
1998-06-25 08:57:29 +00:00
|
|
|
|
|
|
|
/* It isn't necessary to shadow the border, but it looks nice :) */
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_clist_set_shadow_type (GTK_CLIST (clist), GTK_SHADOW_OUT);
|
1998-06-25 08:57:29 +00:00
|
|
|
|
|
|
|
/* What however is important, is that we set the column widths as
|
|
|
|
* they will never be right otherwise. Note that the columns are
|
|
|
|
* numbered from 0 and up (to 1 in this case).
|
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_clist_set_column_width (GTK_CLIST (clist), 0, 150);
|
1998-06-25 08:57:29 +00:00
|
|
|
|
1999-04-10 12:55:24 +00:00
|
|
|
/* Add the CList widget to the vertical box and show it. */
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (scrolled_window), clist);
|
|
|
|
gtk_widget_show (clist);
|
1998-06-25 08:57:29 +00:00
|
|
|
|
|
|
|
/* Create the buttons and add them to the window. See the button
|
|
|
|
* tutorial for more examples and comments on this.
|
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
hbox = gtk_hbox_new (FALSE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
|
|
|
|
gtk_widget_show (hbox);
|
1998-06-25 08:57:29 +00:00
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
button_add = gtk_button_new_with_label ("Add List");
|
|
|
|
button_clear = gtk_button_new_with_label ("Clear List");
|
|
|
|
button_hide_show = gtk_button_new_with_label ("Hide/Show titles");
|
1998-06-25 08:57:29 +00:00
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), button_add, TRUE, TRUE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), button_clear, TRUE, TRUE, 0);
|
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), button_hide_show, TRUE, TRUE, 0);
|
1998-06-25 08:57:29 +00:00
|
|
|
|
|
|
|
/* Connect our callbacks to the three buttons */
|
2002-02-19 19:47:16 +00:00
|
|
|
g_signal_connect_swapped (G_OBJECT (button_add), "clicked",
|
|
|
|
G_CALLBACK (button_add_clicked),
|
|
|
|
clist);
|
|
|
|
g_signal_connect_swapped (G_OBJECT (button_clear), "clicked",
|
|
|
|
G_CALLBACK (button_clear_clicked),
|
|
|
|
clist);
|
|
|
|
g_signal_connect_swapped (G_OBJECT (button_hide_show), "clicked",
|
|
|
|
G_CALLBACK (button_hide_show_clicked),
|
|
|
|
clist);
|
2002-02-19 01:25:26 +00:00
|
|
|
|
|
|
|
gtk_widget_show (button_add);
|
|
|
|
gtk_widget_show (button_clear);
|
|
|
|
gtk_widget_show (button_hide_show);
|
1998-06-25 08:57:29 +00:00
|
|
|
|
|
|
|
/* The interface is completely set up so we show the window and
|
|
|
|
* enter the gtk_main loop.
|
|
|
|
*/
|
2002-02-19 01:25:26 +00:00
|
|
|
gtk_widget_show (window);
|
|
|
|
|
1998-06-25 08:57:29 +00:00
|
|
|
gtk_main();
|
|
|
|
|
2002-02-19 01:25:26 +00:00
|
|
|
return 0;
|
1998-06-25 08:57:29 +00:00
|
|
|
}
|