Commit a282480c8d0e4367d75afb32d965c9ee378de393

Authored by perry.werneck@gmail.com
1 parent e87b2ac8

Melhorando dialogo "save", incluindo código na ação "savecopy"

src/gtk/dialog.c
... ... @@ -34,55 +34,104 @@
34 34  
35 35 /*--[ Implement ]------------------------------------------------------------------------------------*/
36 36  
  37 + static void save_text(GtkWindow *toplevel,const gchar *filename, const gchar *text, const gchar *errmsg)
  38 + {
  39 + GError *error = NULL;
  40 +
  41 + if(!g_file_set_contents(filename,text,-1,&error))
  42 + {
  43 + GtkWidget *popup = gtk_message_dialog_new_with_markup(
  44 + toplevel,
  45 + GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
  46 + GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE,
  47 + gettext(errmsg),filename);
  48 +
  49 + gtk_window_set_title(GTK_WINDOW(popup),_("Can´t save file"));
  50 +
  51 + gtk_message_dialog_format_secondary_markup(GTK_MESSAGE_DIALOG(popup),"%s",error->message);
  52 + g_error_free(error);
  53 +
  54 + gtk_dialog_run(GTK_DIALOG(popup));
  55 + gtk_widget_destroy(popup);
  56 +
  57 + }
  58 +
  59 + }
  60 +
  61 + static GtkFileChooserConfirmation confirm_overwrite(GtkFileChooser *chooser, GtkAction *action)
  62 + {
  63 + gchar * filename = gtk_file_chooser_get_filename(chooser);
  64 + const gchar * attr = g_object_get_data(G_OBJECT(action),"overwrite");
  65 + GtkFileChooserConfirmation ret = GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME;
  66 + GtkWidget * dialog;
  67 +
  68 + if(attr && !g_strcasecmp(attr,"yes"))
  69 + return ret;
  70 +
  71 + dialog = gtk_message_dialog_new_with_markup( GTK_WINDOW(chooser),
  72 + GTK_DIALOG_DESTROY_WITH_PARENT,
  73 + GTK_MESSAGE_QUESTION,GTK_BUTTONS_OK_CANCEL,
  74 + "%s",_("The file already exists. Replace it?"));
  75 +
  76 +
  77 + if(gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_OK)
  78 + ret = GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN;
  79 +
  80 + gtk_widget_destroy(dialog);
  81 +
  82 + return ret;
  83 +
  84 + }
  85 +
37 86 static int save_dialog(GtkAction *action, GtkWidget *widget, const gchar *title, const gchar *errmsg, const gchar *text)
38 87 {
39 88 GtkWindow * toplevel = GTK_WINDOW(gtk_widget_get_toplevel(widget));
40 89 const gchar * user_title = g_object_get_data(G_OBJECT(action),"title");
41   - GtkWidget * dialog;
42   -
  90 + const gchar * filename = g_object_get_data(G_OBJECT(action),"filename");
43 91  
44 92 if(!text)
45 93 return;
46 94  
47   - dialog = gtk_file_chooser_dialog_new( gettext(user_title ? user_title : title),
48   - toplevel,
49   - GTK_FILE_CHOOSER_ACTION_SAVE,
50   - GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
51   - GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
52   - NULL );
53   -
54   -
55   - if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
  95 + if(filename)
  96 + {
  97 + save_text(toplevel,filename,text,errmsg);
  98 + }
  99 + else
56 100 {
57   - GError *error = NULL;
58   - gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
  101 + GtkWidget * dialog;
  102 + gchar * ptr;
59 103  
60   - if(!g_file_set_contents(filename,text,-1,&error))
61   - {
62   - GtkWidget *popup = gtk_message_dialog_new_with_markup(
  104 + dialog = gtk_file_chooser_dialog_new( gettext(user_title ? user_title : title),
63 105 toplevel,
64   - GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
65   - GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE,
66   - gettext(errmsg),filename);
67   -
68   - gtk_window_set_title(GTK_WINDOW(popup),_("Can´t save file"));
  106 + GTK_FILE_CHOOSER_ACTION_SAVE,
  107 + GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
  108 + GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
  109 + NULL );
69 110  
70   - gtk_message_dialog_format_secondary_markup(GTK_MESSAGE_DIALOG(popup),"%s",error->message);
71   - g_error_free(error);
  111 + gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
  112 + g_signal_connect(GTK_FILE_CHOOSER(dialog), "confirm-overwrite", G_CALLBACK(confirm_overwrite), action);
72 113  
73   - gtk_dialog_run(GTK_DIALOG(popup));
74   - gtk_widget_destroy(popup);
  114 + ptr = get_string_from_config("save",gtk_action_get_name(action),"");
  115 + if(*ptr)
  116 + gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog),ptr);
  117 + g_free(ptr);
75 118  
  119 + if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
  120 + {
  121 + ptr = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
  122 + if(ptr)
  123 + {
  124 + trace("Saving \"%s\"",ptr);
  125 + set_string_to_config("save",gtk_action_get_name(action),"%s",ptr);
  126 + save_text(toplevel,ptr,text,errmsg);
  127 + g_free(ptr);
  128 + }
76 129 }
77 130  
78   - g_free(filename);
  131 + gtk_widget_destroy(dialog);
79 132 }
80   -
81   - gtk_widget_destroy(dialog);
82   - return 0;
83 133 }
84 134  
85   -
86 135 void paste_file_action(GtkAction *action, GtkWidget *widget)
87 136 {
88 137 trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
... ... @@ -204,7 +253,7 @@
204 253 save_dialog( action,
205 254 widget,
206 255 N_( "Save screen to file" ),
207   - N_( "Can't save screen to file \n%s" ),
  256 + N_( "Can't save screen to file\n%s" ),
208 257 text);
209 258  
210 259 g_free(text);
... ... @@ -218,7 +267,7 @@
218 267 save_dialog( action,
219 268 widget,
220 269 N_( "Save selection to file" ),
221   - N_( "Can't save selection to file \n%s" ),
  270 + N_( "Can't save selection to file\n%s" ),
222 271 v3270_get_selected_text(widget));
223 272 }
224 273  
... ... @@ -226,4 +275,9 @@
226 275 {
227 276 trace("Action %s activated on widget %p",gtk_action_get_name(action),widget);
228 277  
  278 + save_dialog( action,
  279 + widget,
  280 + N_( "Save copy to file" ),
  281 + N_( "Can't save copy to file\n%s" ),
  282 + v3270_get_copy(widget));
229 283 }
... ...
src/gtk/v3270/clipboard.c
... ... @@ -127,6 +127,17 @@ const gchar * v3270_get_selected_text(GtkWidget *widget)
127 127 return terminal->clipboard;
128 128 }
129 129  
  130 +const gchar * v3270_get_copy(GtkWidget *widget)
  131 +{
  132 + v3270 *terminal;
  133 +
  134 + g_return_val_if_fail(GTK_IS_V3270(widget),NULL);
  135 +
  136 + terminal = GTK_V3270(widget);
  137 +
  138 + return terminal->clipboard;
  139 +}
  140 +
130 141 const gchar * v3270_copy_append(GtkWidget *widget)
131 142 {
132 143 v3270 * terminal;
... ... @@ -161,7 +172,6 @@ const gchar * v3270_copy_append(GtkWidget *widget)
161 172  
162 173 g_signal_emit(widget,v3270_widget_signal[SIGNAL_CLIPBOARD], 0, TRUE);
163 174  
164   -
165 175 return terminal->clipboard;
166 176 }
167 177  
... ...
src/gtk/v3270/v3270.h
... ... @@ -202,6 +202,7 @@
202 202  
203 203 // Clipboard
204 204 const gchar * v3270_get_selected_text(GtkWidget *widget);
  205 + const gchar * v3270_get_copy(GtkWidget *widget);
205 206 gchar * v3270_get_text(GtkWidget *widget);
206 207 const gchar * v3270_copy(GtkWidget *widget);
207 208 const gchar * v3270_copy_append(GtkWidget *widget);
... ...