Commit ced4124e authored by Kristian Rietveld's avatar Kristian Rietveld Committed by Kristian Rietveld

add editable_cells.c, list_store.c and tree_store.c

Tue Oct  2 20:18:32 2001  Kristian Rietveld  <kristian@planet.nl>

        * demos/gtk-demo/Makefile.am: add editable_cells.c,
        list_store.c and tree_store.c

        * demos/gtk-demo/appwindow.c: remove tearoff item from
        File menu, put menubar and toolbar in handle boxes.

        * demos/gtk-demo/genincude.pl: various changes to support trees

        * demos/gtk-demo/main.c: various changes to support trees

        * demos/gtk-demo/stock_browser.c: changed name of demo
        to Stock Item and Icon Browser, so geninclude.pl doesn't see
        it as child

        * demos/gtk-demo/editable_cells.c: new file/demo
        * demos/gtk-demo/list_store.c: new file/demo
        * demos/gtk-demo/tree_store.c: new file/demo
parent ace5a03b
Tue Oct 2 20:18:32 2001 Kristian Rietveld <kristian@planet.nl>
* demos/gtk-demo/Makefile.am: add editable_cells.c,
list_store.c and tree_store.c
* demos/gtk-demo/appwindow.c: remove tearoff item from
File menu, put menubar and toolbar in handle boxes.
* demos/gtk-demo/genincude.pl: various changes to support trees
* demos/gtk-demo/main.c: various changes to support trees
* demos/gtk-demo/stock_browser.c: changed name of demo
to Stock Item and Icon Browser, so geninclude.pl doesn't see
it as child
* demos/gtk-demo/editable_cells.c: new file/demo
* demos/gtk-demo/list_store.c: new file/demo
* demos/gtk-demo/tree_store.c: new file/demo
Mon Oct 1 16:05:44 2001 Owen Taylor <otaylor@redhat.com>
* gtk/gtkrc.c gtk/gtksettings.c:
......
Tue Oct 2 20:18:32 2001 Kristian Rietveld <kristian@planet.nl>
* demos/gtk-demo/Makefile.am: add editable_cells.c,
list_store.c and tree_store.c
* demos/gtk-demo/appwindow.c: remove tearoff item from
File menu, put menubar and toolbar in handle boxes.
* demos/gtk-demo/genincude.pl: various changes to support trees
* demos/gtk-demo/main.c: various changes to support trees
* demos/gtk-demo/stock_browser.c: changed name of demo
to Stock Item and Icon Browser, so geninclude.pl doesn't see
it as child
* demos/gtk-demo/editable_cells.c: new file/demo
* demos/gtk-demo/list_store.c: new file/demo
* demos/gtk-demo/tree_store.c: new file/demo
Mon Oct 1 16:05:44 2001 Owen Taylor <otaylor@redhat.com>
* gtk/gtkrc.c gtk/gtksettings.c:
......
Tue Oct 2 20:18:32 2001 Kristian Rietveld <kristian@planet.nl>
* demos/gtk-demo/Makefile.am: add editable_cells.c,
list_store.c and tree_store.c
* demos/gtk-demo/appwindow.c: remove tearoff item from
File menu, put menubar and toolbar in handle boxes.
* demos/gtk-demo/genincude.pl: various changes to support trees
* demos/gtk-demo/main.c: various changes to support trees
* demos/gtk-demo/stock_browser.c: changed name of demo
to Stock Item and Icon Browser, so geninclude.pl doesn't see
it as child
* demos/gtk-demo/editable_cells.c: new file/demo
* demos/gtk-demo/list_store.c: new file/demo
* demos/gtk-demo/tree_store.c: new file/demo
Mon Oct 1 16:05:44 2001 Owen Taylor <otaylor@redhat.com>
* gtk/gtkrc.c gtk/gtksettings.c:
......
Tue Oct 2 20:18:32 2001 Kristian Rietveld <kristian@planet.nl>
* demos/gtk-demo/Makefile.am: add editable_cells.c,
list_store.c and tree_store.c
* demos/gtk-demo/appwindow.c: remove tearoff item from
File menu, put menubar and toolbar in handle boxes.
* demos/gtk-demo/genincude.pl: various changes to support trees
* demos/gtk-demo/main.c: various changes to support trees
* demos/gtk-demo/stock_browser.c: changed name of demo
to Stock Item and Icon Browser, so geninclude.pl doesn't see
it as child
* demos/gtk-demo/editable_cells.c: new file/demo
* demos/gtk-demo/list_store.c: new file/demo
* demos/gtk-demo/tree_store.c: new file/demo
Mon Oct 1 16:05:44 2001 Owen Taylor <otaylor@redhat.com>
* gtk/gtkrc.c gtk/gtksettings.c:
......
Tue Oct 2 20:18:32 2001 Kristian Rietveld <kristian@planet.nl>
* demos/gtk-demo/Makefile.am: add editable_cells.c,
list_store.c and tree_store.c
* demos/gtk-demo/appwindow.c: remove tearoff item from
File menu, put menubar and toolbar in handle boxes.
* demos/gtk-demo/genincude.pl: various changes to support trees
* demos/gtk-demo/main.c: various changes to support trees
* demos/gtk-demo/stock_browser.c: changed name of demo
to Stock Item and Icon Browser, so geninclude.pl doesn't see
it as child
* demos/gtk-demo/editable_cells.c: new file/demo
* demos/gtk-demo/list_store.c: new file/demo
* demos/gtk-demo/tree_store.c: new file/demo
Mon Oct 1 16:05:44 2001 Owen Taylor <otaylor@redhat.com>
* gtk/gtkrc.c gtk/gtksettings.c:
......
Tue Oct 2 20:18:32 2001 Kristian Rietveld <kristian@planet.nl>
* demos/gtk-demo/Makefile.am: add editable_cells.c,
list_store.c and tree_store.c
* demos/gtk-demo/appwindow.c: remove tearoff item from
File menu, put menubar and toolbar in handle boxes.
* demos/gtk-demo/genincude.pl: various changes to support trees
* demos/gtk-demo/main.c: various changes to support trees
* demos/gtk-demo/stock_browser.c: changed name of demo
to Stock Item and Icon Browser, so geninclude.pl doesn't see
it as child
* demos/gtk-demo/editable_cells.c: new file/demo
* demos/gtk-demo/list_store.c: new file/demo
* demos/gtk-demo/tree_store.c: new file/demo
Mon Oct 1 16:05:44 2001 Owen Taylor <otaylor@redhat.com>
* gtk/gtkrc.c gtk/gtksettings.c:
......
Tue Oct 2 20:18:32 2001 Kristian Rietveld <kristian@planet.nl>
* demos/gtk-demo/Makefile.am: add editable_cells.c,
list_store.c and tree_store.c
* demos/gtk-demo/appwindow.c: remove tearoff item from
File menu, put menubar and toolbar in handle boxes.
* demos/gtk-demo/genincude.pl: various changes to support trees
* demos/gtk-demo/main.c: various changes to support trees
* demos/gtk-demo/stock_browser.c: changed name of demo
to Stock Item and Icon Browser, so geninclude.pl doesn't see
it as child
* demos/gtk-demo/editable_cells.c: new file/demo
* demos/gtk-demo/list_store.c: new file/demo
* demos/gtk-demo/tree_store.c: new file/demo
Mon Oct 1 16:05:44 2001 Owen Taylor <otaylor@redhat.com>
* gtk/gtkrc.c gtk/gtksettings.c:
......
......@@ -10,14 +10,17 @@ demos = @STRIP_BEGIN@ \
colorsel.c \
dialog.c \
drawingarea.c \
editable_cells.c \
images.c \
item_factory.c \
list_store.c \
menus.c \
panes.c \
pixbufs.c \
sizegroup.c \
stock_browser.c \
textview.c \
tree_store.c \
@STRIP_END@
INCLUDES = @STRIP_BEGIN@ \
......
......@@ -35,7 +35,6 @@ menuitem_cb (gpointer callback_data,
static GtkItemFactoryEntry menu_items[] =
{
{ "/_File", NULL, 0, 0, "<Branch>" },
{ "/File/tearoff1", NULL, menuitem_cb, 0, "<Tearoff>" },
{ "/File/_New", "<control>N", menuitem_cb, 0, "<StockItem>", GTK_STOCK_NEW },
{ "/File/_Open", "<control>O", menuitem_cb, 0, "<StockItem>", GTK_STOCK_OPEN },
{ "/File/_Save", "<control>S", menuitem_cb, 0, "<StockItem>", GTK_STOCK_SAVE },
......@@ -182,6 +181,8 @@ do_appwindow (void)
if (!window)
{
GtkWidget *table;
GtkWidget *menubar_handle;
GtkWidget *toolbar_handle;
GtkWidget *toolbar;
GtkWidget *statusbar;
GtkWidget *contents;
......@@ -214,6 +215,8 @@ do_appwindow (void)
gtk_accel_group_attach (accel_group, G_OBJECT (window));
gtk_accel_group_unref (accel_group);
menubar_handle = gtk_handle_box_new ();
item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR, "<main>", accel_group);
/* Set up item factory to go away with the window */
......@@ -228,8 +231,11 @@ do_appwindow (void)
gtk_item_factory_create_items (item_factory, G_N_ELEMENTS (menu_items),
menu_items, window);
gtk_container_add (GTK_CONTAINER (menubar_handle),
gtk_item_factory_get_widget (item_factory, "<main>"));
gtk_table_attach (GTK_TABLE (table),
gtk_item_factory_get_widget (item_factory, "<main>"),
menubar_handle,
/* X direction */ /* Y direction */
0, 1, 0, 1,
GTK_EXPAND | GTK_FILL, 0,
......@@ -237,6 +243,8 @@ do_appwindow (void)
/* Create the toolbar
*/
toolbar_handle = gtk_handle_box_new ();
toolbar = gtk_toolbar_new ();
gtk_toolbar_insert_stock (GTK_TOOLBAR (toolbar),
......@@ -265,8 +273,10 @@ do_appwindow (void)
window, /* user data for callback */
-1); /* -1 means "append" */
gtk_container_add (GTK_CONTAINER (toolbar_handle), toolbar);
gtk_table_attach (GTK_TABLE (table),
toolbar,
toolbar_handle,
/* X direction */ /* Y direction */
0, 1, 1, 2,
GTK_EXPAND | GTK_FILL, 0,
......
/* Tree View/Editable Cells
*
* This demo demostrates the use of editable cells in a GtkTreeView. If
* you're new to the GtkTreeView widgets and associates, look into
* the GtkListStore example first.
*
*/
#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>
static GtkWidget *window = NULL;
typedef struct
{
gint number;
gchar *product;
gboolean editable;
}
Item;
enum
{
COLUMN_NUMBER,
COLUMN_PRODUCT,
COLUMN_EDITABLE,
NUM_COLUMNS
};
static GArray *articles = NULL;
static void
add_items (void)
{
Item foo;
g_return_if_fail (articles != NULL);
foo.number = 3;
foo.product = g_strdup ("bottles of coke");
foo.editable = TRUE;
g_array_append_vals (articles, &foo, 1);
foo.number = 5;
foo.product = g_strdup ("packages of noodles");
foo.editable = TRUE;
g_array_append_vals (articles, &foo, 1);
foo.number = 2;
foo.product = g_strdup ("packages of chocolate chip cookies");
foo.editable = TRUE;
g_array_append_vals (articles, &foo, 1);
foo.number = 1;
foo.product = g_strdup ("can vanilla ice cream");
foo.editable = TRUE;
g_array_append_vals (articles, &foo, 1);
foo.number = 6;
foo.product = g_strdup ("eggs");
foo.editable = TRUE;
g_array_append_vals (articles, &foo, 1);
}
static GtkTreeModel *
create_model (void)
{
gint i = 0;
GtkListStore *model;
GtkTreeIter iter;
/* create array */
articles = g_array_sized_new (FALSE, FALSE, sizeof (Item), 1);
add_items ();
/* create list store */
model = gtk_list_store_new (NUM_COLUMNS, G_TYPE_INT, G_TYPE_STRING,
G_TYPE_BOOLEAN);
/* add items */
for (i = 0; i < articles->len; i++)
{
gtk_list_store_append (model, &iter);
gtk_list_store_set (model, &iter,
COLUMN_NUMBER,
g_array_index (articles, Item, i).number,
COLUMN_PRODUCT,
g_array_index (articles, Item, i).product,
COLUMN_EDITABLE,
g_array_index (articles, Item, i).editable,
-1);
}
return GTK_TREE_MODEL (model);
}
static void
add_item (GtkWidget *button, gpointer data)
{
Item foo;
GtkTreeIter iter;
GtkTreeModel *model = (GtkTreeModel *)data;
g_return_if_fail (articles != NULL);
foo.number = 0;
foo.product = g_strdup ("Description here");
foo.editable = TRUE;
g_array_append_vals (articles, &foo, 1);
gtk_list_store_append (GTK_LIST_STORE (model), &iter);
gtk_list_store_set (GTK_LIST_STORE (model), &iter,
COLUMN_NUMBER, foo.number,
COLUMN_PRODUCT, foo.product,
COLUMN_EDITABLE, foo.editable,
-1);
}
static void
remove_item (GtkWidget *widget, gpointer data)
{
GtkTreeIter iter;
GtkTreeView *treeview = (GtkTreeView *)data;
GtkTreeModel *model = gtk_tree_view_get_model (treeview);
GtkTreeSelection *selection = gtk_tree_view_get_selection (treeview);
if (gtk_tree_selection_get_selected (selection, NULL, &iter))
{
gint i;
GtkTreePath *path;
path = gtk_tree_model_get_path (model, &iter);
i = gtk_tree_path_get_indices (path)[0];
gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
g_array_remove_index (articles, i);
gtk_tree_path_free (path);
}
}
static void
cell_edited (GtkCellRendererText *cell,
gchar *path_string,
gchar *new_text,
gpointer data)
{
GtkTreeModel *model = (GtkTreeModel *)data;
GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
GtkTreeIter iter;
gchar old_text[256]; /* ugly? */
gint *column;
column = g_object_get_data (G_OBJECT (cell), "column");
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_model_get (model, &iter, column, old_text, -1);
switch ((gint) column)
{
case COLUMN_NUMBER:
{
gint i;
i = gtk_tree_path_get_indices (path)[0];
g_array_index (articles, Item, i).number = atoi (new_text);
gtk_list_store_set (GTK_LIST_STORE (model), &iter, column,
atoi (new_text), -1);
}
break;
case COLUMN_PRODUCT:
{
gint i;
i = gtk_tree_path_get_indices (path)[0];
g_free (g_array_index (articles, Item, i).product);
g_array_index (articles, Item, i).product = g_strdup (new_text);
gtk_list_store_set (GTK_LIST_STORE (model), &iter, column,
new_text, -1);
}
break;
}
gtk_tree_path_free (path);
}
static void
add_columns (GtkTreeView *treeview)
{
GtkCellRenderer *renderer;
GtkTreeModel *model = gtk_tree_view_get_model (treeview);
/* number column */
renderer = gtk_cell_renderer_text_new ();
g_signal_connect (G_OBJECT (renderer), "edited",
G_CALLBACK (cell_edited), model);
g_object_set_data (G_OBJECT (renderer), "column", (gint *)COLUMN_NUMBER);
gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
-1, "Number", renderer,
"text", COLUMN_NUMBER,
"editable", COLUMN_EDITABLE,
NULL);
/* product column */
renderer = gtk_cell_renderer_text_new ();
g_signal_connect (G_OBJECT (renderer), "edited",
G_CALLBACK (cell_edited), model);
g_object_set_data (G_OBJECT (renderer), "column", (gint *)COLUMN_PRODUCT);
gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
-1, "Product", renderer,
"text", COLUMN_PRODUCT,
"editable", COLUMN_EDITABLE,
NULL);
}
GtkWidget *
do_editable_cells (void)
{
if (!window)
{
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *sw;
GtkWidget *treeview;
GtkWidget *button;
GtkTreeModel *model;
/* create window, etc */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Shopping list");
gtk_container_set_border_width (GTK_CONTAINER (window), 5);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);
vbox = gtk_vbox_new (FALSE, 5);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_box_pack_start (GTK_BOX (vbox),
gtk_label_new ("Shopping list (you can edit the cells!)"),
FALSE, FALSE, 0);
sw = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
GTK_SHADOW_ETCHED_IN);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0);
/* create model */
model = create_model ();
/* create tree view */
treeview = gtk_tree_view_new_with_model (model);
gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview), TRUE);
gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)),
GTK_SELECTION_SINGLE);
add_columns (GTK_TREE_VIEW (treeview));
gtk_container_add (GTK_CONTAINER (sw), treeview);
/* some buttons */
hbox = gtk_hbox_new (TRUE, 4);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
button = gtk_button_new_with_label ("Add item");
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (add_item), model);
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
button = gtk_button_new_with_label ("Remove item");
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (remove_item), treeview);
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
gtk_window_set_default_size (GTK_WINDOW (window), 320, 200);
}
if (!GTK_WIDGET_VISIBLE (window))
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
return window;
}
......@@ -10,22 +10,16 @@ struct _Demo
gchar *title;
gchar *filename;
GDoDemoFunc func;
Demo *children;
};
EOT
$array = "";
$first = 1;
for $file (@ARGV) {
my %demo;
($basename = $file) =~ s/\.c$//;
if ($first) {
$first = 0;
} else {
$array .= ",\n";
}
open INFO_FILE, $file or die "Cannot open '$file'\n";
$title = <INFO_FILE>;
$title =~ s@^\s*/\*\s*@@;
......@@ -34,9 +28,136 @@ for $file (@ARGV) {
close INFO_FILE;
print "GtkWidget *do_$basename (void);\n";
$array .= qq( { "$title", "$file", do_$basename });
push @demos, {"title" => $title, "file" => $file,
"func" => "do_$basename"};
}
# generate a list of 'parent names'
foreach $href (@demos) {
if ($href->{"title"} =~ m|^([\w\s]+)/[\w\s]+$|) {
my $parent_name = $1;
my $do_next = 0;
# parent detected
if (defined @parents) {
foreach $foo (@parents) {
if ($foo eq $parent_name) {
$do_next = 1;
}
}
if ($do_next) {
next;
}
}
push @parents, $parent_name;
$tmp = (defined @child_arrays)?($#child_arrays + 1):0;
push @child_arrays, "child$tmp";
push @demos, {"title" => $parent_name, "file" => "NULL",
"func" => "NULL"};
}
}
if (defined @parents) {
$i = 0;
for ($i = 0; $i <= $#parents; $i++) {
$first = 1;
print "\nDemo ", $child_arrays[$i], "[] = {\n";
$j = 0;
for ($j = 0; $j <= $#demos; $j++) {
$href = $demos[$j];
if (!defined $demos[$j]) {
next;
}
if ($demos[$j]{"title"} =~ m|^$parents[$i]/([\w\s]+)$|) {
if ($first) {
$first = 0;
} else {
print ",\n";
}
print qq ( { "$1", "$demos[$j]{file}", $demos[$j]{func}, NULL });
# hack ... ugly
$demos[$j]{"title"} = "foo";
}
}
print ",\n";
print qq ( { NULL } );
print "\n};\n";
}
}
# sort @demos
@demos_old = @demos;
@demos = sort {
$a->{"title"} cmp $b->{"title"};
} @demos_old;
# sort the child arrays
if (defined @child_arrays) {
for ($i = 0; $i <= $#child_arrays; $i++) {
@foo_old = @{