diff options
author | Kristian Høgsberg <krh@redhat.com> | 2006-05-20 00:53:02 -0400 |
---|---|---|
committer | Kristian Høgsberg <krh@dinky.bitplanet.net> | 2006-05-20 00:53:02 -0400 |
commit | f7a5c852f4b6af4a3b1f489c333e338799d6cb00 (patch) | |
tree | 1ec358798816cbf05bb6637c002eb9b75d7b1c97 | |
parent | 199d00194b17db8dfdad622b33f659a55b07b076 (diff) |
Add combo box to swithc models on the fly; remove a few 'sproing' names.
-rw-r--r-- | akamaru.c | 138 |
1 files changed, 94 insertions, 44 deletions
@@ -187,6 +187,20 @@ model_init_curtain (Model *model) } static void +model_fini (Model *model) +{ + g_free (model->objects); + model->objects = NULL; + model->num_objects = 0; + g_free (model->sticks); + model->sticks = NULL; + model->sticks = 0; + g_free (model->offsets); + model->offsets = NULL; + model->num_offsets = 0; +} + +static void model_accumulate_forces (Model *model) { int i; @@ -444,9 +458,9 @@ static Color black = { 0, 0, 0 }; static Color white = { 1, 1, 1 }; static gboolean -sproing_expose_event (GtkWidget *widget, - GdkEventExpose *event, - gpointer data) +expose_event (GtkWidget *widget, + GdkEventExpose *event, + gpointer data) { Model *model = data; cairo_t *cr; @@ -467,9 +481,9 @@ sproing_expose_event (GtkWidget *widget, } static gboolean -sproing_button_press_event (GtkWidget *widget, - GdkEventButton *event, - gpointer data) +button_press_event (GtkWidget *widget, + GdkEventButton *event, + gpointer data) { Model *model = data; @@ -484,9 +498,9 @@ sproing_button_press_event (GtkWidget *widget, } static gboolean -sproing_button_release_event (GtkWidget *widget, - GdkEventButton *event, - gpointer data) +button_release_event (GtkWidget *widget, + GdkEventButton *event, + gpointer data) { Model *model = data; @@ -499,9 +513,9 @@ sproing_button_release_event (GtkWidget *widget, } static gboolean -sproing_motion_notify_event (GtkWidget *widget, - GdkEventMotion *event, - gpointer data) +motion_notify_event (GtkWidget *widget, + GdkEventMotion *event, + gpointer data) { Model *model = data; int x, y; @@ -515,47 +529,83 @@ sproing_motion_notify_event (GtkWidget *widget, return TRUE; } +typedef void (*ModelInitFunc) (Model *model); + static void -spring_constant_changed (GtkSpinButton *spinbutton, gpointer user_data) +model_changed (GtkComboBox *combo, gpointer user_data) { Model *model = user_data; + GtkTreeIter iter; + GtkTreeModel *tree_model; + ModelInitFunc init; + char *name; - model->k = gtk_spin_button_get_value (spinbutton); + tree_model = gtk_combo_box_get_model (combo); + if (!gtk_combo_box_get_active_iter (combo, &iter)) + return; + + gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1); + + model_fini (model); + (*init) (model); } -static void -friction_changed (GtkSpinButton *spinbutton, gpointer user_data) +static GtkTreeModel * +create_model_store (void) { - Model *model = user_data; + static struct { + const char *name; + ModelInitFunc init; + } models[] = { + { "Rope", model_init_rope }, + { "Snake", model_init_snake }, + { "Curtain", model_init_curtain } + }; + + GtkTreeIter iter; + GtkTreeStore *store; + gint i; + + store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER); + + for (i = 0; i < G_N_ELEMENTS(models); i++) { + gtk_tree_store_append (store, &iter, NULL); + gtk_tree_store_set (store, &iter, + 0, models[i].name, 1, models[i].init, -1); + } + + return GTK_TREE_MODEL (store); - model->friction = gtk_spin_button_get_value (spinbutton); } static GtkWidget * -create_spinners (Model *model) +create_model_combo (Model *model) { GtkWidget *hbox; - GtkWidget *spinner, *label; + GtkWidget *combo, *label; + GtkTreeModel *store; + GtkCellRenderer *renderer; hbox = gtk_hbox_new (FALSE, 8); - label = gtk_label_new_with_mnemonic ("_Spring constant:"); + label = gtk_label_new_with_mnemonic ("_Model:"); gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); - spinner = gtk_spin_button_new_with_range (0.05, 30.00, 0.05); - gtk_label_set_mnemonic_widget (GTK_LABEL (label), spinner); - gtk_box_pack_start (GTK_BOX (hbox), spinner, FALSE, FALSE, 0); - gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinner), model->k); - g_signal_connect (spinner, "value-changed", - G_CALLBACK (spring_constant_changed), model); - - label = gtk_label_new_with_mnemonic ("_Friction:"); - gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); - spinner = gtk_spin_button_new_with_range (0.05, 15.00, 0.05); - gtk_label_set_mnemonic_widget (GTK_LABEL (label), spinner); - gtk_box_pack_start (GTK_BOX (hbox), spinner, FALSE, FALSE, 0); - gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinner), model->friction); - g_signal_connect (spinner, "value-changed", - G_CALLBACK (friction_changed), model); + + store = create_model_store (); + combo = gtk_combo_box_new_with_model (store); + gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0); + g_object_unref (store); + + renderer = gtk_cell_renderer_text_new (); + gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE); + gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer, + "text", 0, + NULL); + + gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo); + gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0); + g_signal_connect (combo, "changed", + G_CALLBACK (model_changed), model); return hbox; } @@ -567,7 +617,7 @@ create_window (Model *model) GtkWidget *frame; GtkWidget *vbox; GtkWidget *da; - GtkWidget *spinners; + GtkWidget *model_combo; window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW (window), "Drawing Area"); @@ -598,16 +648,16 @@ create_window (Model *model) /* Signals used to handle backing pixmap */ g_signal_connect (da, "expose_event", - G_CALLBACK (sproing_expose_event), model); + G_CALLBACK (expose_event), model); /* Event signals */ g_signal_connect (da, "motion_notify_event", - G_CALLBACK (sproing_motion_notify_event), model); + G_CALLBACK (motion_notify_event), model); g_signal_connect (da, "button_press_event", - G_CALLBACK (sproing_button_press_event), model); + G_CALLBACK (button_press_event), model); g_signal_connect (da, "button_release_event", - G_CALLBACK (sproing_button_release_event), model); + G_CALLBACK (button_release_event), model); /* Ask to receive events the drawing area doesn't normally * subscribe to @@ -619,8 +669,8 @@ create_window (Model *model) | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK); - spinners = create_spinners (model); - gtk_box_pack_start (GTK_BOX (vbox), spinners, FALSE, FALSE, 0); + model_combo = create_model_combo (model); + gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0); return da; } @@ -657,7 +707,7 @@ main (int argc, char *argv[]) Model model; gtk_init (&argc, &argv); - model_init_curtain (&model); + model_init_rope (&model); closure.drawing_area = create_window (&model); closure.i = 0; gtk_widget_show_all (gtk_widget_get_toplevel (closure.drawing_area)); |