Release 2.1.96-6
[hildon] / examples / hildon-stackable-window-example.c
index aabeda0..5991d97 100644 (file)
@@ -3,8 +3,6 @@
  *
  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
  *
- * Author: Karl Lattimer <karl.lattimer@nokia.com>
- *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
  * as published by the Free Software Foundation; version 2.1 of
  *
  */
 
-#include                                        <stdio.h>
-#include                                        <stdlib.h>
-#include                                        <glib.h>
-#include                                        <gtk/gtk.h>
-#include                                        "hildon.h"
+#include                                        <hildon/hildon.h>
+
+static gint                                     global_stack_count = 1;
+
+static void
+add_window                                      (GtkWidget             *button,
+                                                 HildonStackableWindow *parent);
 
-#include                                        <X11/X.h>
-#include                                        <X11/Xlib.h>
-#include                                        <X11/Xatom.h>
+static void
+push_windows                                    (GtkWidget     *button,
+                                                 GtkSpinButton *spin);
 
 static void
-add_window                                      (GtkWidget* w);
+pop_windows                                     (GtkWidget     *button,
+                                                 GtkSpinButton *spin);
 
 static GtkWidget*
-new_window                                      (gboolean ismain)
+new_window                                      (HildonStackableWindow *parent)
 {
-    GtkWidget *window, *hbbox, *add;
-    static int count = 0;
-    gchar* title;
+    GtkWidget *window, *hbbox, *vbox, *label, *add, *new;
+    GtkWidget *spin1hbox, *spin1label1, *spin1, *spin1label2, *pushbtn, *align1;
+    GtkWidget *spin2hbox, *spin2label1, *spin2, *spin2label2, *popbtn, *align2;
+    gint stack_number, win_number;
+    gchar *text;
 
     window = hildon_stackable_window_new ();
 
-    if (count == 0)
-        title = g_strdup ("main window");
-    else
-        title = g_strdup_printf ("win%d", count);
+    if (parent) {
+        stack_number = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (parent), "stack-number"));
+        win_number = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (parent), "win-number")) + 1;
+    } else {
+        stack_number = global_stack_count++;
+        win_number = 1;
+    }
+    g_object_set_data (G_OBJECT (window), "stack-number", GINT_TO_POINTER (stack_number));
+    g_object_set_data (G_OBJECT (window), "win-number", GINT_TO_POINTER (win_number));
 
-    gtk_window_set_title (GTK_WINDOW (window), title);
-    g_free (title);
+    /* Window title */
+    text = g_strdup_printf ("Stack number %d - window %d", stack_number, win_number);
+    gtk_window_set_title (GTK_WINDOW (window), text);
+    g_free (text);
 
-    count++;
+    /* Marked up window title */
+    text = g_strdup_printf ("Stack number <i>%d</i> - window <i>%d</i>", stack_number, win_number);
+    hildon_window_set_markup (HILDON_WINDOW (window), text);
+    g_free (text);
 
-    gtk_container_set_border_width (GTK_CONTAINER (window), 6);
+    /* Main label */
+    text = g_strdup_printf ("Stack number %d\nWindow number %d", stack_number, win_number);
+    label = gtk_label_new (text);
+    g_free (text);
 
     hbbox = gtk_hbutton_box_new ();
-    gtk_container_add (GTK_CONTAINER (window), hbbox);
 
-    add = gtk_button_new_with_label ("Add a window");
+    /* Button to push a window to the current stack */
+    add = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
+    gtk_button_set_label (GTK_BUTTON (add), "Add a window to this stack");
     gtk_box_pack_start (GTK_BOX (hbbox), add, FALSE, FALSE, 0);
+    g_signal_connect (G_OBJECT (add), "clicked", G_CALLBACK (add_window), window);
+
+    /* Button to create a new stack */
+    new = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
+    gtk_button_set_label (GTK_BUTTON (new), "Add a window to a new stack");
+    gtk_box_pack_start (GTK_BOX (hbbox), new, FALSE, FALSE, 0);
+    g_signal_connect (G_OBJECT (new), "clicked", G_CALLBACK (add_window), NULL);
+
+    /* Spinbox and button to push many windows */
+    spin1hbox = gtk_hbox_new (FALSE, 0);
+    spin1label1 = gtk_label_new ("Push");
+    spin1 = gtk_spin_button_new (GTK_ADJUSTMENT (gtk_adjustment_new (2, 2, 5, 1, 1, 1)), 1, 0);
+    gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin1), TRUE);
+    spin1label2 = gtk_label_new ("windows");
+    pushbtn = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
+    gtk_button_set_label (GTK_BUTTON (pushbtn), "Push windows");
+    gtk_box_pack_start (GTK_BOX (spin1hbox), spin1label1, FALSE, FALSE, 0);
+    gtk_box_pack_start (GTK_BOX (spin1hbox), spin1, FALSE, FALSE, 0);
+    gtk_box_pack_start (GTK_BOX (spin1hbox), spin1label2, FALSE, FALSE, 0);
+    gtk_box_pack_start (GTK_BOX (spin1hbox), pushbtn, FALSE, FALSE, 10);
+    align1 = gtk_alignment_new (0.5, 0.5, 0, 0);
+    gtk_container_add (GTK_CONTAINER (align1), spin1hbox);
+    g_signal_connect (G_OBJECT (pushbtn), "clicked", G_CALLBACK (push_windows), spin1);
+
+    /* Spinbox and button to pop many windows */
+    spin2hbox = gtk_hbox_new (FALSE, 0);
+    spin2label1 = gtk_label_new ("Pop");
+    spin2 = gtk_spin_button_new (GTK_ADJUSTMENT (gtk_adjustment_new (2, 2, 5, 1, 1, 1)), 1, 0);
+    gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spin2), TRUE);
+    spin2label2 = gtk_label_new ("windows");
+    popbtn = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
+    gtk_button_set_label (GTK_BUTTON (popbtn), "Pop windows");
+    gtk_box_pack_start (GTK_BOX (spin2hbox), spin2label1, FALSE, FALSE, 0);
+    gtk_box_pack_start (GTK_BOX (spin2hbox), spin2, FALSE, FALSE, 0);
+    gtk_box_pack_start (GTK_BOX (spin2hbox), spin2label2, FALSE, FALSE, 0);
+    gtk_box_pack_start (GTK_BOX (spin2hbox), popbtn, FALSE, FALSE, 10);
+    align2 = gtk_alignment_new (0.5, 0.5, 0, 0);
+    gtk_container_add (GTK_CONTAINER (align2), spin2hbox);
+    g_signal_connect (G_OBJECT (popbtn), "clicked", G_CALLBACK (pop_windows), spin2);
+
+    vbox = gtk_vbox_new (FALSE, 0);
+    gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
+    gtk_box_pack_start (GTK_BOX (vbox), hbbox, FALSE, FALSE, 10);
+    gtk_box_pack_start (GTK_BOX (vbox), align1, FALSE, FALSE, 10);
+    gtk_box_pack_start (GTK_BOX (vbox), align2, FALSE, FALSE, 10);
 
-    g_signal_connect (G_OBJECT (add), "clicked", G_CALLBACK (add_window), NULL);
+    gtk_container_set_border_width (GTK_CONTAINER (window), 6);
+    gtk_container_add (GTK_CONTAINER (window), vbox);
+    gtk_widget_show_all (vbox);
 
-    if (!ismain)
-    {
-        GtkWidget *detach, *back;
-        detach = GTK_WIDGET (gtk_button_new_with_label ("Destroy"));
-        gtk_box_pack_end (GTK_BOX (hbbox), detach, FALSE, FALSE, 0);
+    return window;
+}
 
-        g_signal_connect_swapped (G_OBJECT (detach), "clicked",
-                                  G_CALLBACK (gtk_widget_destroy),
-                                  HILDON_STACKABLE_WINDOW (window));
+static void
+add_window                                      (GtkWidget             *button,
+                                                 HildonStackableWindow *parent)
+{
+    HildonWindowStack *stack = NULL;
+    GtkWidget *window;
 
-        back = GTK_WIDGET (gtk_button_new_with_label ("Back to root"));
-        gtk_box_pack_end (GTK_BOX (hbbox), back, FALSE, FALSE, 0);
+    if (parent) {
+        stack = hildon_stackable_window_get_stack (parent);
+    } else {
+        stack = hildon_window_stack_new ();
+    }
 
-        g_signal_connect_swapped (G_OBJECT (back), "clicked",
-                                  G_CALLBACK (hildon_program_go_to_root_window),
-                                  hildon_program_get_instance ());
+    window = new_window (parent);
+
+    if (!stack) {
+        stack = hildon_window_stack_get_default ();
     }
+    hildon_window_stack_push_1 (stack, HILDON_STACKABLE_WINDOW (window));
+}
 
-    return window;
+static void
+push_windows                                    (GtkWidget     *button,
+                                                 GtkSpinButton *spin)
+{
+    GList *l = NULL;
+    HildonWindowStack *stack = NULL;
+    HildonStackableWindow *parent;
+    gint nwindows = gtk_spin_button_get_value_as_int (spin);
+
+    parent = HILDON_STACKABLE_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (spin)));
+    stack = hildon_stackable_window_get_stack (parent);
+
+    while (nwindows > 0) {
+        parent = HILDON_STACKABLE_WINDOW (new_window (parent));
+        l = g_list_append (l, parent);
+        nwindows--;
+    }
+    hildon_window_stack_push_list (stack, l);
+    g_list_free (l);
 }
 
 static void
-add_window                                      (GtkWidget *w)
+pop_windows                                     (GtkWidget     *button,
+                                                 GtkSpinButton *spin)
 {
-    GtkWidget *window = new_window (FALSE);
-    gtk_widget_show_all (window);
+    HildonWindowStack *stack = NULL;
+    HildonStackableWindow *win;
+    gint nwindows = gtk_spin_button_get_value_as_int (spin);
+
+    win = HILDON_STACKABLE_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (spin)));
+    stack = hildon_stackable_window_get_stack (win);
 
-    return;
+    hildon_window_stack_pop (stack, nwindows, NULL);
 }
 
 int
-main                                            (int argc,
-                                                 char **args)
+main                                            (int    argc,
+                                                 char **argv)
 {
     GtkWidget *window;
 
-    gtk_init (&argc, &args);
+    hildon_gtk_init (&argc, &argv);
 
-    g_set_application_name ("stack");
+    g_set_application_name ("hildon-stackable-window-example");
 
-    window = new_window (TRUE);
+    window = new_window (NULL);
 
-    g_signal_connect (G_OBJECT (window), "delete_event",
+    g_signal_connect (G_OBJECT (window), "destroy",
                       G_CALLBACK (gtk_main_quit), NULL);
 
-    gtk_widget_show_all (GTK_WIDGET (window));
+    gtk_widget_show (window);
 
     gtk_main ();