test_pagebar.c

Go to the documentation of this file.
00001 /**
00002  * \file test_pagebar.c
00003  * \brief Test pagebar functions
00004  */
00005  
00006 /*
00007  * This file is part of libergtk.
00008  *
00009  * libergtk is free software: you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation, either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * libergtk is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00021  */
00022 
00023 /**
00024  * Copyright (C) 2008 iRex Technologies B.V.
00025  * All rights reserved.
00026  */
00027 
00028 #include <stdlib.h>
00029 #include <gtk/gtk.h>
00030 #include <gdk/gdkkeysyms.h>
00031 #include "erGtkPageBar.h"
00032 
00033 static GtkWidget *thePagebar;
00034 static gint g_current_page = 0;
00035 static gint g_number_of_pages = 1;
00036 
00037 static void on_destroy(GtkWidget * widget, gpointer data)
00038 {
00039     gtk_main_quit();
00040 }
00041 
00042 static gboolean on_configured(GtkWidget * widget,
00043                               GdkEventConfigure * event, 
00044                               gpointer user_data)
00045 {
00046     return FALSE;
00047 }
00048 
00049 static void on_page_selected(GtkWidget * widget, gint page, gpointer data)
00050 {
00051     g_print("page %d has been selected!\n", page);
00052     g_current_page = page;
00053 }
00054 
00055 static gboolean key_release_callback(GtkWidget *widget, GdkEventKey *event, gpointer data)
00056 {
00057     switch (event->keyval)
00058     {
00059         case GDK_KP_Left:
00060         case GDK_Left:
00061             g_current_page--;
00062             break;
00063 
00064         case GDK_KP_Right:
00065         case GDK_Right:
00066             g_current_page++;
00067             break;
00068         
00069         case GDK_Page_Up:
00070             g_current_page = g_current_page + 5;
00071             break;
00072 
00073         case GDK_Page_Down:
00074             g_current_page = g_current_page - 5;
00075             break;
00076 
00077         default:
00078             return FALSE;
00079     }
00080     if (g_current_page > g_number_of_pages)
00081     {
00082         g_current_page = g_number_of_pages;
00083     }
00084     else if (g_current_page < 0)
00085     {
00086         g_current_page = 0;
00087     }
00088     gtk_pagebar_set_current_page(GTK_PAGEBAR(data), g_current_page);
00089     return TRUE;
00090 }
00091 
00092 void test_assertions(GtkPagebar * pagebar)
00093 {
00094     g_print("--- Test assertions ---\n");
00095     g_print("** The assertions below are expected behavior:");
00096     gtk_pagebar_set_number_of_pages(NULL, -1);
00097     gtk_pagebar_set_number_of_pages(pagebar, -1);
00098     gtk_pagebar_get_number_of_pages(NULL);
00099     gtk_pagebar_set_current_page(NULL, -1);
00100     gtk_pagebar_set_current_page(pagebar, -1);
00101     gtk_pagebar_get_current_page(NULL);
00102     gtk_pagebar_set_bookmark(NULL, 0);
00103     gtk_pagebar_set_bookmark(pagebar, 0);
00104     gtk_pagebar_get_bookmarks(NULL);
00105     gtk_pagebar_reset(NULL);
00106     gtk_pagebar_get_minimal_height(NULL);
00107     g_print("--- End test assertions ---\n\n");
00108 }
00109 
00110 int main(int argc, char *argv[])
00111 {
00112     GtkWidget *window;
00113     GtkWidget *layout_box;
00114 
00115     unsigned int num_pages = 300;
00116     unsigned int selected_page = 10;
00117 
00118     if (argc == 3)
00119     {
00120         num_pages = atoi(argv[1]);
00121         selected_page = atoi(argv[2]);
00122     }
00123     
00124     g_current_page = selected_page;
00125     g_number_of_pages = num_pages;
00126 
00127     /* init threads */
00128     g_thread_init(NULL);
00129     gdk_threads_init();
00130     gdk_threads_enter();
00131     gtk_init(&argc, &argv);
00132 
00133     // create the main, top level, window 
00134     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00135 
00136     gtk_window_set_title(GTK_WINDOW(window), "pagebar_test");
00137     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);
00138     gtk_container_set_border_width(GTK_CONTAINER(window), 0);
00139     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
00140     gtk_widget_set_size_request(GTK_WIDGET(window), 1024, 60);
00141 
00142     // Construct layout.
00143     layout_box = gtk_vbox_new(FALSE, 0);
00144     thePagebar = gtk_pagebar_new();
00145 
00146     // Test erGtkPageBar assertions
00147     test_assertions(GTK_PAGEBAR(thePagebar));
00148 
00149     g_signal_connect(G_OBJECT(window), "destroy",
00150                      G_CALLBACK(on_destroy), thePagebar);
00151     g_signal_connect(G_OBJECT (window), "key_release_event", 
00152                      G_CALLBACK (key_release_callback), thePagebar);
00153 
00154     gtk_pagebar_set_number_of_pages(GTK_PAGEBAR(thePagebar), num_pages);
00155     gtk_pagebar_set_current_page(GTK_PAGEBAR(thePagebar), selected_page);
00156 
00157     // TODO Yehong
00158     gtk_pagebar_set_bookmark(GTK_PAGEBAR(thePagebar), selected_page);
00159     gtk_pagebar_set_bookmark(GTK_PAGEBAR(thePagebar), (int)(selected_page/2)+1);
00160     gtk_pagebar_set_bookmark(GTK_PAGEBAR(thePagebar), (int)(selected_page/4)+1);
00161 
00162     int idx = 0;
00163     for (; idx < (g_number_of_pages >> 1); ++idx)
00164     {
00165         gtk_pagebar_set_bookmark(GTK_PAGEBAR(thePagebar), idx);
00166     }
00167 
00168 
00169     g_signal_connect(G_OBJECT(thePagebar), "pagebar_page_selected",
00170                      G_CALLBACK(on_page_selected), NULL);
00171 
00172     g_signal_connect(G_OBJECT(window), 
00173                      "configure-event",
00174                      G_CALLBACK(on_configured), 
00175                      thePagebar);
00176 
00177     // gtk_container_add(GTK_CONTAINER(fixedContainer), thePagebar);
00178     gtk_box_pack_start(GTK_BOX(layout_box), thePagebar, TRUE, TRUE, 0);
00179     gtk_container_add(GTK_CONTAINER(window), layout_box);
00180 
00181     gtk_widget_show_all(layout_box);
00182     gtk_widget_show(window);
00183 
00184     // test get bookmarks
00185     const GArray * bookmarks = gtk_pagebar_get_bookmarks(GTK_PAGEBAR(thePagebar));
00186     if (bookmarks != NULL)
00187     {
00188         g_print("bookmarks number:%d\n", bookmarks->len);
00189         int i = 0;
00190         for (; i < bookmarks->len; ++i)
00191         {
00192             int mark_page = g_array_index(bookmarks, guint, i);
00193             g_print("current bookmark:%d\n", mark_page);
00194         }
00195     }
00196 
00197     gtk_main();
00198     gdk_threads_leave();
00199 
00200     return 0;
00201 }
Generated by  doxygen 1.6.2-20100208