GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Nov 01, 2014 9:08 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: save_as keyboard short cut
PostPosted: Sun Oct 14, 2012 9:02 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Dear friends,
In my project, I have created a SAVE AS file submenu.
I have two question:
1) This "save as" does not have any key board shortcut(Ctrl+S as standard) like open or new file submenu, which are also created in the same way, using accel group. Why this is so?
2) Though the file copying is working fine, I would love to receive some comment about if this is a dependable(in sense of failsafe and fast) process of copying file.
The code is as follows:

Code:
//In Main
accel_group = gtk_accel_group_new();
    gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
    save = gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE_AS, accel_group);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), save);
    g_signal_connect(G_OBJECT(save), "activate", G_CALLBACK(rename_file), NULL);

Code:
//the called function
static void rename_file(GtkWidget *widget, gpointer data)
{
   gchar *fname=NULL, *str;
   gchar *cts;
   GError *Err=NULL;
   GtkWidget *dialog;
   gsize length;
   dialog=gtk_file_chooser_dialog_new("Save File", GTK_WINDOW(window),
                                      GTK_FILE_CHOOSER_ACTION_SAVE,
                                      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                      GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
      gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);     
   if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
   {
      g_file_get_contents(filename, &cts, &length, &Err);
      filename=gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
      g_file_set_contents(filename, cts, -1, &Err);
      g_free(cts);
      if (Err)
      {
        caution("Failed to save in %s",filename);
         {g_free(str); g_error_free(Err);}
      }
   }
   gtk_widget_destroy(dialog);
}


Top
 Profile  
 
 Post subject: Re: save_as keyboard short cut
PostPosted: Mon Oct 15, 2012 1:48 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Is this too dumb a question?
I will be very happy if some one kindly give me some way.


Top
 Profile  
 
 Post subject: Re: save_as keyboard short cut
PostPosted: Mon Oct 15, 2012 7:12 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

First, save as stock item has no predefined accelerator and this is why nothing is shown on the right. But solution to this one is really simple: just call gtk_widget_add_accelerator() function on your menu item and things will work as expected. Code will look something like this:
Code:
gtk_widget_add_accelerator (save, "activate", accel_group,
                            GDK_KEY_s, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE);


As for the second question, your solution is fine as long as you don't have a large file coming in. Then you better copy it in chunks to avoid depleting your memory. Also, your function is named "rename_file". Why would you name your function like that if function actually copies the file?

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: save_as keyboard short cut
PostPosted: Mon Oct 15, 2012 8:21 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Tadej,
Thanks agian.
Yes, as you said, the name of the function is bad. I will change it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group