GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Nov 28, 2014 1:23 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Save_as Not saving
PostPosted: Wed Oct 03, 2012 9:35 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
This is my code for save_as utility
Code:
static  void rename_file(GtkWidget *widget, gpointer data) {
    GtkWidget *dialog = gtk_file_chooser_dialog_new("Save File", GTK_WINDOW(window),
        GTK_FILE_CHOOSER_ACTION_SAVE,
        GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
        NULL);
     if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
     filename=gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog),"U.bib");
     gtk_widget_destroy(dialog);
     return;
     }
    gtk_widget_destroy(dialog);

}

But it is not creating any file. where I am going wromg?


Top
 Profile  
 
 Post subject: Re: Save_as Not saving
PostPosted: Thu Oct 04, 2012 5:47 am 
Offline
Never Seen the Sunlight

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

Where is the code to save your file on disk? All you posted is code that is responsible for selecting new filename. After you get that file, you need to save your data. GTK+ will not (and cannot) save data for you, since it doesn't know ow your file is formatted, etc.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: Save_as Not saving
PostPosted: Fri Oct 05, 2012 1:21 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
gtk_file_chooser_set_filename does not save but only sets the filename field on the dialog window (of no practical use placed after the dialog_run call). It also returns a boolean so I'm surprised you haven't picked up on a warning at least when compiling.
To get your code to save, modify to the following (where statusbar and window are setup in main -- I have also changed the button order to put save in the bottom right which is the more commonly seen amongst GTK apps):
Code:
GtkWidget *statusbar, *window;

static void rename_file(GtkWidget *widget, gpointer data)
{
   gchar *fname=NULL, *str;
   GError *Err=NULL;
   GtkWidget *dialog;

   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);
   if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
   {
      fname=gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
      /*insert code to put data to be saved into cts*/
      g_file_set_contents(fname, cts, -1, &Err);
      g_free(cts);
      if (Err)
      {
         str=g_strdup_printf(_("Error Saving file: %s."), (Err->message));
         gtk_statusbar_push(GTK_STATUSBAR(statusbar), gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), str), str);
         {g_free(str); g_error_free(Err);}
      }
      g_free(fname);
   }
   gtk_widget_destroy(dialog);
}


Top
 Profile  
 
 Post subject: Re: Save_as Not saving
PostPosted: Fri Oct 05, 2012 7:55 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Paul,
Thanks for your code.
I was doing it as(to open a new file):
Code:
static  void new_file(GtkWidget *widget, gpointer data) {
    GtkWidget *dialog = gtk_file_chooser_dialog_new("Save File", GTK_WINDOW(window),
        GTK_FILE_CHOOSER_ACTION_SAVE,
        GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
        NULL);
     gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
     gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog),"Untitled.bib");
     if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
     filename=gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
     if(TRUE){
       remove(filename);
     }
     FILE *fn=g_fopen(filename,"a");
     g_fprintf(fn,"#This File is Created by mkBIB\n");
     fclose(fn);
     gtk_widget_destroy(dialog);
     return;
     }
    gtk_widget_destroy(dialog);
}

Of course yours is better with error checking etc. I will try to implement that in mine


Top
 Profile  
 
 Post subject: Re: Save_as Not saving
PostPosted: Fri Oct 05, 2012 11:19 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Sorry just noticed that the _() macro slipped in in the error checking section. Don't bother copying this unless you want to setup gettext for doing translations.
btw the
Code:
     gtk_widget_destroy(dialog);
     return;
chunk is superfluous and can be left out to naturally flow into the final gtk_widget_destroy(dialog);
Not really sure why you aren't getting a file for the second case.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 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