erGtkDialogJumpToPage.c

Go to the documentation of this file.
00001 /**
00002  * \file erGtkDialogJumpToPage.c
00003  * \brief ereader gtk library - GtkDialog used to jump to a specific page
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 
00031 #include "eri18n.h"
00032 #include "ergtk_log.h"
00033 #include "erGtkDialogJumpToPage.h"
00034 
00035 #define UNUSED(x) (void)(x)
00036 
00037 
00038 G_DEFINE_TYPE (erGtkJumpToPageDialog, ergtk_jump_to_page_dialog, GTK_TYPE_DIALOG)
00039 
00040 static void ergtk_jump_to_page_dialog_class_init (erGtkJumpToPageDialogClass *klass)
00041 {
00042     UNUSED(klass);
00043 }
00044 
00045 static void on_number_clicked(GtkWidget *widget, gpointer data)
00046 {
00047     gchar buf[3];
00048     gint len, pos;
00049     gint i = 0;
00050 
00051     g_return_if_fail(ERGTK_IS_JUMP_TO_PAGE_DIALOG(data));
00052     erGtkJumpToPageDialog* dialog = ERGTK_JUMP_TO_PAGE_DIALOG(data);
00053 
00054     for (i = 0; i < TOTAL_NUMERIC_BUTTONS; i++)
00055     {
00056         if (dialog->numeric_btns[i] == widget)
00057         {
00058             break;
00059         }
00060     }
00061 
00062     g_assert(i != TOTAL_NUMERIC_BUTTONS);
00063 
00064     len = g_snprintf(buf, sizeof(buf), "%d", i);
00065     pos = gtk_editable_get_position(GTK_EDITABLE(dialog->entry));
00066     gtk_editable_insert_text(GTK_EDITABLE(dialog->entry), buf, len, &pos);
00067     gtk_editable_set_position(GTK_EDITABLE(dialog->entry), pos);
00068 }
00069 
00070 static void on_backspace_clicked(GtkWidget *widget, gpointer data)
00071 {
00072     UNUSED(widget);
00073     
00074     g_return_if_fail(ERGTK_IS_JUMP_TO_PAGE_DIALOG(data));
00075     erGtkJumpToPageDialog* dialog = ERGTK_JUMP_TO_PAGE_DIALOG(data);
00076 
00077     gint pos = gtk_editable_get_position(GTK_EDITABLE(dialog->entry));
00078     gtk_editable_delete_text(GTK_EDITABLE(dialog->entry), pos - 1, pos);
00079 }
00080 
00081 static void on_enter_clicked(GtkWidget *widget, gpointer data)
00082 {
00083     UNUSED(widget);
00084     LOGPRINTF("entry");
00085     
00086     g_return_if_fail(ERGTK_IS_JUMP_TO_PAGE_DIALOG(data));
00087     erGtkJumpToPageDialog* dialog = ERGTK_JUMP_TO_PAGE_DIALOG(data);
00088 
00089     gint page = atoi(gtk_entry_get_text(GTK_ENTRY(dialog->entry)));
00090     if (page > 0)
00091     {
00092         /* Return the page number in the entry field */
00093         gtk_dialog_response(GTK_DIALOG(dialog), page);
00094     }
00095     else
00096     {
00097         /* Return CANCEL if no page in entry field */
00098         gtk_dialog_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
00099     }
00100 }
00101 
00102 static void on_focus_out(GtkWidget *widget, gpointer data)
00103 {
00104     UNUSED(data);
00105     LOGPRINTF("entry");
00106 
00107     /* Return CANCEL when dialog looses focus */
00108     gtk_dialog_response(GTK_DIALOG(widget), GTK_RESPONSE_CANCEL);
00109 }
00110 
00111 // init instance
00112 static void ergtk_jump_to_page_dialog_init (erGtkJumpToPageDialog *dialog)
00113 {
00114     GtkWidget *button;
00115     GtkWidget *table;
00116     GtkWidget *vbox;
00117     gint i, row, col;
00118     gchar buf[4];
00119 
00120     LOGPRINTF("entry");
00121 
00122     g_return_if_fail(ERGTK_IS_JUMP_TO_PAGE_DIALOG(dialog));
00123 
00124     /* Create a new dialog */
00125     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
00126     gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
00127     gtk_widget_hide (GTK_DIALOG(dialog)->action_area);
00128 
00129     /* Set the window title */
00130     gtk_window_set_title (GTK_WINDOW (dialog), "");
00131 
00132     /* Set the window properties */
00133     gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
00134     gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
00135 
00136     /* Sets the border width of the window. */
00137     //gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
00138 
00139     /* Create a 4x3 table */
00140     table = gtk_table_new (4, 3, TRUE);
00141     gtk_table_set_row_spacings(GTK_TABLE(table), 2);
00142     gtk_table_set_col_spacings(GTK_TABLE(table), 2);
00143 
00144     /* Put the table in the main window */
00145     vbox = gtk_vbox_new(FALSE, 0);
00146     gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), vbox);
00147     gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
00148     
00149     row = 0;
00150     
00151     /* Create and add the entry field into the table */
00152     dialog->entry = gtk_entry_new_with_max_length(6);
00153     g_object_set(G_OBJECT(dialog->entry), "im-context-inhibit", TRUE, NULL); // disable input method
00154     g_object_set(G_OBJECT(dialog->entry), "activates-default", TRUE, NULL );
00155     gtk_widget_set_size_request(dialog->entry, 50, -1);
00156     gtk_table_attach_defaults (GTK_TABLE (table), dialog->entry, 0, 1, row, row + 1);
00157     gtk_widget_show(dialog->entry);
00158 
00159     /* Create and add the 'Go to page' button */
00160     button = gtk_button_new_with_label(_("Go to Page"));
00161     g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK (on_enter_clicked), (gpointer) dialog);
00162     gtk_table_attach_defaults (GTK_TABLE (table), button, 1, 3, row, row + 1);
00163     gtk_widget_show(button);
00164 
00165     /* Make the 'Go to page' button the default action */
00166     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
00167     gtk_window_set_default(GTK_WINDOW(dialog), button);
00168 
00169     /* Loop through all numbers */
00170     for (i = 0; i <= 9; i++)
00171     {
00172         g_snprintf(buf, sizeof(buf), " %d ", i);
00173         button = gtk_button_new_with_label(buf);
00174         g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK (on_number_clicked), (gpointer) dialog);
00175 
00176         if (i == 0)
00177         {
00178             /* Number 0 is in the last row, first column */
00179             row = 4;
00180             col = 0;
00181         }
00182         else
00183         {
00184             /* Determine row and column for numbers 1 through 9 */
00185             row = (i - 1) / 3;
00186             col = (i - 1) % 3;
00187             /* Offset one row: the input field and Goto button are above */
00188             row += 1;
00189         }
00190 
00191         /* Insert numbered button into the table */
00192         gtk_table_attach_defaults (GTK_TABLE (table), button, col, col + 1, row, row + 1);
00193         gtk_widget_show (button);
00194         dialog->numeric_btns[i] = button;
00195     }
00196     
00197     row = 4;
00198 
00199     /* Add a backspace button to remove a char */
00200     button = gtk_button_new_with_label(_("Backspace"));
00201     g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK (on_backspace_clicked), (gpointer) dialog);
00202     gtk_table_attach_defaults (GTK_TABLE (table), button, 1, 3, row, row + 1);
00203     gtk_widget_show (button);
00204 
00205     gtk_widget_show (table);
00206     gtk_widget_show (vbox);
00207     
00208     g_signal_connect(G_OBJECT(dialog), "focus-out-event", G_CALLBACK (on_focus_out), NULL);
00209 }
00210 
00211 GtkWidget* ergtk_jump_to_page_dialog_new ()
00212 {
00213     erGtkJumpToPageDialog* dialog;
00214 
00215     LOGPRINTF("entry");
00216 
00217     dialog = g_object_new (ERGTK_TYPE_JUMP_TO_PAGE_DIALOG, NULL);
00218 
00219     return GTK_WIDGET (dialog);
00220 }
Generated by  doxygen 1.6.2-20100208