notepad_rename.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: notepad_rename.cpp
00003  */
00004 
00005 /*
00006  * This file is part of notepad.
00007  *
00008  * notepad 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  * notepad 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) 2010 IREX Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 #include <gtk/gtk.h>
00032 
00033 #include <libergtk/ergtk.h>
00034 #include <liberkeyb/ergtk-keyb.h>
00035 
00036 #include "i18n.h"
00037 #include "log.h"
00038 #include "notepad_rename.h"
00039 
00040 
00041 //============================================================================
00042 // Local defines
00043 //============================================================================
00044 
00045 #define WINDOW_TOP_PADDING         46
00046 #define WINDOW_BOTTOM_PADDING      10
00047 #define WINDOW_H_PADDING           40
00048 #define PREVNEXT_HEIGHT            20
00049 #define PREVNEXT_ARROW_WIDTH      140
00050 #define TITLE_V_PADDING            10  // vertical padding around title
00051 #define TITLE_SPACING              20  // space between path and number of items in title
00052 #define TITLE_PATH_SPACING         10  // space between path elements in title
00053 #define FILEGRID_V_PADDING         10  // vertical padding around the file grid
00054 #define LISTVIEW_COL_SPACING       20  // space between columns in listview
00055 #define LISTVIEW_ICON_WIDTH        30
00056 #define LISTVIEW_ICON_HEIGHT       30
00057 
00058 #define POPUP_DIALOG_WIDTH         600
00059 #define POPUP_DIALOG_V_PADDING     10
00060 #define POPUP_DIALOG_H_PADDING     10
00061 #define POPUP_DIALOG_V_SPACING     10
00062 #define POPUP_DIALOG_H_SPACING     10
00063 
00064 #define BUTTON_WIDTH               -1
00065 #define BUTTON_HEIGHT              27
00066 
00067 #define TEXT_ENTRY_WIDTH           300
00068 #define TEXT_ENTRY_HEIGHT          27
00069 
00070 #define TITLE_HEIGHT               44   // total height of titlebar
00071 
00072 #define MAX_FILENAME_LEN           200  // 200 chars filename allowed
00073 
00074 //----------------------------------------------------------------------------
00075 // Static Variables
00076 //----------------------------------------------------------------------------
00077 
00078 static GtkWidget* g_dialog          = NULL;
00079 static GtkWidget* g_rename_label    = NULL;
00080 static GtkWidget* g_input_entry     = NULL;
00081 static GtkWidget* g_rename_button   = NULL;
00082 static GtkWidget* g_cancel_button   = NULL; 
00083 static gchar*     g_lastfilename    = NULL;
00084 
00085 //============================================================================
00086 // Local Function Definitions
00087 //============================================================================
00088 
00089 static void rename_on_hbox_size_allocate(GtkWidget* widget, GtkAllocation* allocation, gpointer user_data);
00090 static void rename_on_rename_button_clicked(GtkWidget* widget, gpointer data);
00091 static void rename_on_cancel_button_clicked(GtkWidget* widget, gpointer data);
00092 
00093 
00094 //============================================================================
00095 // Functions Implementation
00096 //============================================================================
00097 
00098 GtkWidget* rename_dialog_create(const GtkWidget* parent, const gchar* current_filename)
00099 {
00100 
00101     // dialog
00102     GtkWidget* dialog = gtk_dialog_new ();
00103     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
00104     gtk_window_set_title (GTK_WINDOW(dialog), "");
00105     gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
00106     gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(parent));
00107     gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
00108     g_dialog = dialog;
00109 
00110     // alignment
00111     GtkWidget* alignment = gtk_alignment_new(0.0, 0.0, 1.0, 0);
00112     gtk_alignment_set_padding(GTK_ALIGNMENT(alignment), 
00113                               POPUP_DIALOG_V_PADDING, 
00114                               POPUP_DIALOG_V_PADDING, 
00115                               POPUP_DIALOG_H_PADDING, 
00116                               POPUP_DIALOG_H_PADDING);
00117     gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), alignment);
00118 
00119     // vbox
00120     GtkWidget* vbox = gtk_vbox_new(FALSE, POPUP_DIALOG_V_SPACING);
00121     gtk_container_add(GTK_CONTAINER(alignment), vbox);
00122 
00123     // rename_label_widget
00124     GtkWidget * rename_label_widget = gtk_label_new("");
00125     gtk_widget_set_name(rename_label_widget, "irex-normal-text");
00126     gtk_widget_set_size_request(rename_label_widget, 
00127             POPUP_DIALOG_WIDTH - 2 * POPUP_DIALOG_H_PADDING, -1);
00128     gtk_misc_set_alignment(GTK_MISC(rename_label_widget), 0.0, 0.0);
00129     gtk_label_set_justify(GTK_LABEL(rename_label_widget), GTK_JUSTIFY_LEFT);
00130     gtk_label_set_single_line_mode(GTK_LABEL(rename_label_widget), FALSE);
00131     gtk_label_set_line_wrap(GTK_LABEL(rename_label_widget), TRUE);
00132     gtk_label_set_ellipsize(GTK_LABEL(rename_label_widget), PANGO_ELLIPSIZE_NONE);
00133     gtk_box_pack_start(GTK_BOX(vbox), rename_label_widget, TRUE, TRUE, 0);
00134     g_rename_label = rename_label_widget;
00135 
00136     // hbox
00137     GtkWidget* hbox = gtk_hbox_new(FALSE, POPUP_DIALOG_H_SPACING);
00138     gtk_widget_set_size_request(hbox, -1, -1);
00139     gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
00140 
00141     // input_entry
00142     GtkWidget* entry_widget = gtk_entry_new();
00143     gtk_widget_set_size_request(entry_widget, TEXT_ENTRY_WIDTH, TEXT_ENTRY_HEIGHT);
00144     gtk_entry_set_max_length(GTK_ENTRY(entry_widget), MAX_FILENAME_LEN);
00145     g_free(g_lastfilename);
00146     g_lastfilename = g_strdup(current_filename);
00147 
00148     if (g_lastfilename)
00149         gtk_entry_set_text(GTK_ENTRY(entry_widget), g_lastfilename);
00150     else 
00151         gtk_entry_set_text(GTK_ENTRY(entry_widget), "");
00152 
00153     gtk_box_pack_start(GTK_BOX(hbox), entry_widget, TRUE, TRUE, 0); 
00154     g_object_set(G_OBJECT(entry_widget), "im-context-inhibit", TRUE, NULL );
00155     g_object_set(G_OBJECT(entry_widget), "activates-default", TRUE, NULL );
00156     g_input_entry = entry_widget;
00157 
00158     // sub_hbox
00159     GtkWidget* sub_hbox = gtk_hbox_new(FALSE, POPUP_DIALOG_H_SPACING);
00160     gtk_box_pack_start(GTK_BOX(hbox), sub_hbox, FALSE, FALSE, 0); 
00161 
00162     // rename_button
00163     GtkWidget* button_widget = gtk_button_new();
00164     gtk_widget_set_size_request(button_widget, BUTTON_WIDTH, BUTTON_HEIGHT);
00165     gtk_box_pack_start(GTK_BOX(sub_hbox), button_widget, FALSE, FALSE, 0); 
00166     g_rename_button = button_widget;
00167     GTK_WIDGET_SET_FLAGS(g_rename_button, GTK_CAN_DEFAULT);
00168     gtk_window_set_default(GTK_WINDOW(dialog), g_rename_button);
00169     
00170     // cancel_button
00171     GtkWidget* cancel_widget = gtk_button_new();
00172     gtk_widget_set_size_request(cancel_widget, BUTTON_WIDTH, BUTTON_HEIGHT);
00173     gtk_box_pack_start(GTK_BOX(sub_hbox), cancel_widget, FALSE, FALSE, 0); 
00174     g_cancel_button = cancel_widget;
00175 
00176     // keyboard_widget
00177     GtkWidget* keyboard_widget = ergtk_keyb_new();
00178     gtk_box_pack_start(GTK_BOX(vbox), keyboard_widget, TRUE, TRUE, 10);
00179 
00180     // install signal hanlders.
00181     g_signal_connect(G_OBJECT(sub_hbox), "size-allocate",  
00182             G_CALLBACK(rename_on_hbox_size_allocate), sub_hbox); 
00183     g_signal_connect(G_OBJECT(button_widget), "clicked", 
00184             G_CALLBACK(rename_on_rename_button_clicked), (gpointer) dialog);
00185     g_signal_connect(G_OBJECT(cancel_widget), "clicked", 
00186             G_CALLBACK(rename_on_cancel_button_clicked), (gpointer) dialog);
00187 
00188     // Focus on the entry in order to show keyboard automatically.
00189     gtk_widget_grab_focus(g_input_entry);
00190     
00191     rename_locale_changed();
00192 
00193     gtk_widget_show_all(dialog);
00194     return dialog;
00195 }
00196 
00197 
00198 void rename_locale_changed()
00199 {
00200     gtk_label_set_text(GTK_LABEL(g_rename_label), 
00201                        _("Type the new name for this note and then tap the Rename button to save it. \n"
00202                          " / \\ |: * ? < or > cannot be used in the name and it cannot begin with a space or period."));
00203 
00204     gtk_button_set_label(GTK_BUTTON(g_rename_button), _("Rename"));
00205     gtk_button_set_label(GTK_BUTTON(g_cancel_button), _("Cancel"));
00206 }
00207 
00208 
00209 const gchar* rename_get_text()
00210 {
00211     LOGPRINTF("%s", g_lastfilename);
00212     return g_lastfilename;
00213 }
00214 
00215 
00216 void rename_set_focus()
00217 {
00218     gtk_widget_grab_focus(g_input_entry);
00219 }
00220 
00221 
00222 static void rename_on_rename_button_clicked(GtkWidget * widget, gpointer data)
00223 {
00224     const gchar* text = gtk_entry_get_text(GTK_ENTRY(g_input_entry));
00225     LOGPRINTF("%s", text);
00226     g_free(g_lastfilename);
00227     g_lastfilename = g_strdup(text);
00228     if (g_strcmp0(text, "") == 0) 
00229     {
00230         gtk_dialog_response(GTK_DIALOG(g_dialog), GTK_RESPONSE_CLOSE);
00231     } else 
00232     {
00233         gtk_dialog_response(GTK_DIALOG(g_dialog), GTK_RESPONSE_ACCEPT);
00234     }
00235 }
00236 
00237 static void rename_on_cancel_button_clicked(GtkWidget * widget, gpointer data)
00238 {
00239     const gchar* text = gtk_entry_get_text(GTK_ENTRY(g_input_entry));
00240     LOGPRINTF("%s", text);
00241     g_free(g_lastfilename);
00242     g_lastfilename = g_strdup(text);
00243     gtk_dialog_response(GTK_DIALOG(g_dialog), GTK_RESPONSE_CLOSE);
00244 }
00245 
00246 static void rename_on_hbox_size_allocate(GtkWidget * widget,
00247         GtkAllocation * allocation,
00248         gpointer user_data)
00249 {
00250     if (!user_data || !GTK_IS_HBOX(user_data))
00251     {
00252         ERRORPRINTF("Can't adjust for this widget because it's not a HBOX!");
00253         return;
00254     }
00255 
00256     // Get child widgets.
00257     GList * children = gtk_container_get_children(GTK_CONTAINER(user_data));
00258     if (!children)
00259     {
00260         ERRORPRINTF("The hbox hasn't any children!");
00261         return;
00262     }
00263     int n_children = g_list_length(children);
00264 
00265     // Calculate the button's width.
00266     int request_width = BUTTON_WIDTH;
00267     int index = 0;
00268     while (index < n_children)
00269     {
00270         gpointer gp = g_list_nth_data(children, index);
00271         GtkWidget * child = GTK_WIDGET(gp);
00272         GtkRequisition requisition;
00273         gtk_widget_size_request(child, &requisition);
00274         if (request_width < requisition.width)
00275         {
00276             request_width = requisition.width;
00277         }
00278 
00279         LOGPRINTF("index [%d] width [%d]", index, requisition.width);
00280         index++;
00281     }
00282 
00283     // Set the buttons's width to be the request_width.
00284     index = 0;
00285     while (index < n_children)
00286     {
00287         gpointer gp = g_list_nth_data(children, index);
00288         GtkWidget * child = GTK_WIDGET(gp);
00289         GtkRequisition requisition;
00290         gtk_widget_size_request(child, &requisition);
00291         if (requisition.width != request_width)
00292         {   
00293             gtk_widget_set_size_request(child,
00294                     request_width,
00295                     requisition.height);
00296         }
00297         index++;
00298     }
00299 }
00300 
Generated by  doxygen 1.6.2-20100208