filefind.c

Go to the documentation of this file.
00001 /*
00002  * File Name: filefind.c
00003  */
00004 
00005 /*
00006  * This file is part of ctb.
00007  *
00008  * ctb is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * ctb is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021  
00022 /**
00023  * Copyright (C) 2009 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 #include <gtk/gtk.h>
00032 
00033 #include "liberkeyb/ergtk-keyb.h"
00034 
00035 #include "i18n.h"
00036 #include "log.h"
00037 #include "filefind.h"
00038 #include "measures.h"
00039 
00040 
00041 //----------------------------------------------------------------------------
00042 // Static Variables
00043 //----------------------------------------------------------------------------
00044 
00045 static GtkWidget* g_dialog = NULL;
00046 static GtkWidget* g_search_label  = NULL;
00047 static GtkWidget* g_input_entry   = NULL;
00048 static GtkWidget* g_search_button = NULL;
00049 static GtkWidget* g_cancel_button = NULL; 
00050 static gchar*     g_lastsearch = NULL;
00051 
00052 //============================================================================
00053 // Local Function Definitions
00054 //============================================================================
00055 
00056 static void filefind_on_hbox_size_allocate(GtkWidget * widget, GtkAllocation * allocation, gpointer user_data);
00057 static void filefind_on_search_button_clicked(GtkWidget * widget, gpointer data);
00058 static void filefind_on_cancel_button_clicked(GtkWidget * widget, gpointer data);
00059 
00060 
00061 #define UNUSED(x) (void)(x)
00062 
00063 //============================================================================
00064 // Functions Implementation
00065 //============================================================================
00066 
00067 GtkWidget* filefind_dialog_create(GtkWidget* parent)
00068 {
00069     // dialog
00070     GtkWidget* dialog = gtk_dialog_new ();
00071     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
00072     gtk_window_set_title (GTK_WINDOW(dialog), "");
00073     gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
00074     gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
00075     gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
00076     g_dialog = dialog;
00077 
00078     // alignment
00079     GtkWidget* alignment = gtk_alignment_new(0.0, 0.0, 1.0, 0);
00080     gtk_alignment_set_padding(GTK_ALIGNMENT(alignment), 
00081                               POPUP_DIALOG_V_PADDING, 
00082                               POPUP_DIALOG_V_PADDING, 
00083                               POPUP_DIALOG_H_PADDING, 
00084                               POPUP_DIALOG_H_PADDING);
00085     gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), alignment);
00086 
00087     // vbox
00088     GtkWidget* vbox = gtk_vbox_new(FALSE, POPUP_DIALOG_V_SPACING);
00089     gtk_container_add(GTK_CONTAINER(alignment), vbox);
00090 
00091     // search_label_widget
00092     GtkWidget * search_label_widget = gtk_label_new("");
00093     gtk_widget_set_name(search_label_widget, "irex-uds-search-label");
00094     gtk_widget_set_size_request(search_label_widget, 
00095             POPUP_DIALOG_WIDTH - 2 * POPUP_DIALOG_H_PADDING, -1);
00096     gtk_misc_set_alignment(GTK_MISC(search_label_widget), 0.0, 0.0);
00097     gtk_label_set_justify(GTK_LABEL(search_label_widget), GTK_JUSTIFY_LEFT);
00098     gtk_label_set_single_line_mode(GTK_LABEL(search_label_widget), FALSE);
00099     gtk_label_set_line_wrap(GTK_LABEL(search_label_widget), TRUE);
00100     gtk_label_set_ellipsize(GTK_LABEL(search_label_widget), PANGO_ELLIPSIZE_NONE);
00101     gtk_box_pack_start(GTK_BOX(vbox), search_label_widget, TRUE, TRUE, 0);
00102     g_search_label = search_label_widget;
00103 
00104     // hbox
00105     GtkWidget* hbox = gtk_hbox_new(FALSE, POPUP_DIALOG_H_SPACING);
00106     gtk_widget_set_size_request(hbox, -1, -1);
00107     gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
00108 
00109     // input_entry
00110     GtkWidget* entry_widget = gtk_entry_new();
00111     gtk_widget_set_size_request(entry_widget, TEXT_ENTRY_WIDTH, TEXT_ENTRY_HEIGHT);
00112     if (g_lastsearch)
00113         gtk_entry_set_text(GTK_ENTRY(entry_widget), g_lastsearch);
00114     else 
00115         gtk_entry_set_text(GTK_ENTRY(entry_widget), "");
00116     gtk_box_pack_start(GTK_BOX(hbox), entry_widget, TRUE, TRUE, 0); 
00117     g_object_set(G_OBJECT(entry_widget), "im-context-inhibit", TRUE, NULL );
00118     g_object_set(G_OBJECT(entry_widget), "activates-default", TRUE, NULL );
00119     g_input_entry = entry_widget;
00120 
00121     // sub_hbox
00122     GtkWidget* sub_hbox = gtk_hbox_new(FALSE, POPUP_DIALOG_H_SPACING);
00123     gtk_box_pack_start(GTK_BOX(hbox), sub_hbox, FALSE, FALSE, 0); 
00124 
00125     // search_button
00126     GtkWidget* button_widget = gtk_button_new();
00127     gtk_widget_set_size_request(button_widget, BUTTON_WIDTH, BUTTON_HEIGHT);
00128     gtk_box_pack_start(GTK_BOX(sub_hbox), button_widget, FALSE, FALSE, 0); 
00129     g_search_button = button_widget;
00130     GTK_WIDGET_SET_FLAGS(g_search_button, GTK_CAN_DEFAULT);
00131     gtk_window_set_default(GTK_WINDOW(dialog), g_search_button);
00132     
00133     // cancel_button
00134     GtkWidget* cancel_widget = gtk_button_new();
00135     gtk_widget_set_size_request(cancel_widget, BUTTON_WIDTH, BUTTON_HEIGHT);
00136     gtk_box_pack_start(GTK_BOX(sub_hbox), cancel_widget, FALSE, FALSE, 0); 
00137     g_cancel_button = cancel_widget;
00138 
00139     // keyboard_widget
00140     GtkWidget* keyboard_widget = ergtk_keyb_new();
00141     gtk_box_pack_start(GTK_BOX(vbox), keyboard_widget, TRUE, TRUE, 10);
00142 
00143     // install signal hanlders.
00144     g_signal_connect(G_OBJECT(sub_hbox), "size-allocate",  
00145             G_CALLBACK(filefind_on_hbox_size_allocate), sub_hbox); 
00146     g_signal_connect(G_OBJECT(button_widget), "clicked", 
00147             G_CALLBACK(filefind_on_search_button_clicked), (gpointer) dialog);
00148     g_signal_connect(G_OBJECT(cancel_widget), "clicked", 
00149             G_CALLBACK(filefind_on_cancel_button_clicked), (gpointer) dialog);
00150 
00151 
00152     // Focus on the entry in order to show keyboard automatically.
00153     gtk_widget_grab_focus(entry_widget);
00154     
00155     filefind_locale_changed();
00156 
00157     gtk_widget_show_all(dialog);
00158     return dialog;
00159 }
00160 
00161 
00162 void filefind_locale_changed()
00163 {
00164     gtk_label_set_text(GTK_LABEL(g_search_label), 
00165                        _("Type the name or author of the item you are looking for. Tap the Find button; the device will begin searching."));
00166 
00167     gtk_button_set_label(GTK_BUTTON(g_search_button), _("Find"));
00168     gtk_button_set_label(GTK_BUTTON(g_cancel_button), _("Cancel"));
00169 }
00170 
00171 
00172 const gchar* filefind_get_text()
00173 {
00174     return g_lastsearch;
00175 }
00176 
00177 
00178 static void filefind_on_search_button_clicked(GtkWidget *widget, gpointer data)
00179 {
00180     UNUSED(widget);
00181     UNUSED(data);
00182     const gchar* text = gtk_entry_get_text(GTK_ENTRY(g_input_entry));
00183     g_free(g_lastsearch);
00184     g_lastsearch = g_strdup(text);
00185     if (strcmp(text, "") == 0) {
00186         gtk_dialog_response(GTK_DIALOG(g_dialog), GTK_RESPONSE_CLOSE);
00187     } else {
00188         gtk_dialog_response(GTK_DIALOG(g_dialog), GTK_RESPONSE_ACCEPT);
00189     }
00190 }
00191 
00192 
00193 static void filefind_on_cancel_button_clicked(GtkWidget * widget, gpointer data)
00194 {
00195     UNUSED(widget);
00196     UNUSED(data);
00197     const gchar* text = gtk_entry_get_text(GTK_ENTRY(g_input_entry));
00198     g_free(g_lastsearch);
00199     g_lastsearch = g_strdup(text);
00200     gtk_dialog_response(GTK_DIALOG(g_dialog), GTK_RESPONSE_CLOSE);
00201 }
00202 
00203 static void filefind_on_hbox_size_allocate(GtkWidget * widget,
00204         GtkAllocation * allocation,
00205         gpointer user_data)
00206 {
00207     UNUSED(widget);
00208     UNUSED(allocation);
00209     if (!user_data || !GTK_IS_HBOX(user_data))
00210     {
00211         ERRORPRINTF("Can't adjust for this widget because it's not a HBOX!");
00212         return;
00213     }
00214 
00215     // Get child widgets.
00216     GList * children = gtk_container_get_children(GTK_CONTAINER(user_data));
00217     if (!children)
00218     {
00219         ERRORPRINTF("The hbox hasn't any children!");
00220         return;
00221     }
00222     int n_children = g_list_length(children);
00223 
00224     // Calculate the button's width.
00225     int request_width = BUTTON_WIDTH;
00226     int index = 0;
00227     while (index < n_children)
00228     {
00229         gpointer gp = g_list_nth_data(children, index);
00230         GtkWidget * child = GTK_WIDGET(gp);
00231         GtkRequisition requisition;
00232         gtk_widget_size_request(child, &requisition);
00233         if (request_width < requisition.width)
00234         {
00235             request_width = requisition.width;
00236         }
00237 
00238         LOGPRINTF("index [%d] width [%d]", index, requisition.width);
00239         index++;
00240     }
00241 
00242     // Set the buttons's width to be the request_width.
00243     index = 0;
00244     while (index < n_children)
00245     {
00246         gpointer gp = g_list_nth_data(children, index);
00247         GtkWidget * child = GTK_WIDGET(gp);
00248         GtkRequisition requisition;
00249         gtk_widget_size_request(child, &requisition);
00250         if (requisition.width != request_width)
00251         {   
00252             gtk_widget_set_size_request(child,
00253                     request_width,
00254                     requisition.height);
00255         }
00256         index++;
00257     }
00258 }
00259 
Generated by  doxygen 1.6.2-20100208