GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 02, 2014 11:50 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Open file dialog help
PostPosted: Tue Jul 07, 2009 11:58 pm 
Offline
Familiar Face

Joined: Tue Jul 07, 2009 11:31 pm
Posts: 13
I am trying to open a file by using the open button in the file menu. However it gives this error.
Quote:
/tmp/cckuxvSx.o: In function `file_open':
gui_v_1.c:(.text+0xda): undefined reference to `open_file'
collect2: ld returned 1 exit status

The usage of the file_open function is:
"/File/_Open", "<control>O", file_open, 1, "<StockItem>", GTK_STOCK_OPEN }

For the definition of the file_open fuction, I used the one provided in the gnome library for the GtkFileChooserDialog:
Code:
void file_open(GtkWidget *widget)
{
  GtkWidget *dialog;
  dialog = gtk_file_chooser_dialog_new ("Open File",
                                        NULL,
                                        GTK_FILE_CHOOSER_ACTION_OPEN,
                                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                        GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                        NULL);

  if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
  {
    char *filename;
    filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
    open_file (filename);
    g_free (filename);
  }

  gtk_widget_destroy (dialog);
}

Can anyone please help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 08, 2009 8:17 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello and welcome to the GTK+ forums.

I cannot completely understand your problem, but from the compiler output, I would say that linker cannot find "open_file" function on line 14. Did you properly declare/define this function?

I would also suggest you to avoid naming functions like you did, since "file_open" and "open_file" are too ambiguous and they can easily make one hell of a mess out of your code.

Code:
void file_open(GtkWidget *widget){
GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Open File",
NULL,
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);

if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
{
char *filename;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
open_file (filename);
g_free (filename);
}

gtk_widget_destroy (dialog);
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 08, 2009 3:09 pm 
Offline
Familiar Face

Joined: Tue Jul 07, 2009 11:31 pm
Posts: 13
Thanks for the reply.
I had thought that the "open_file" function was predefined.
So what do you suggest that I use instead of that.
Can I use fopen() instead.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 08, 2009 3:31 pm 
Offline
Never Seen the Sunlight

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

What function will you use will depend on many factors, most important being:
  1. How large opened file is?
  2. Is it binary or text file?
  3. What do you intend to do with it after opening?
But you can use any standard libc function to manipulate files when coding with GTK+. But glib has some convenience functions that you may find useful in certain situations: http://library.gnome.org/devel/glib/stable/glib-File-Utilities.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 08, 2009 3:40 pm 
Offline
Familiar Face

Joined: Tue Jul 07, 2009 11:31 pm
Posts: 13
Hey,
The file to be opened is in KB.
It will be a text file and it can be used to read and write.

I replaced the open_file function with g_open(filename, O_RDWR)
It opened up the dialog box but when I select the file, there is nothing that opens up.
What kind of modification do you suggest that I should do so that I see that file on the screen.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 15, 2009 8:09 am 
Offline
Never Seen the Sunlight

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

For files that are less than 1MB in size, I usually use g_file_get_contents, which slurps the whole file into a single string, which can be later manipulated.

To display file contents of the file, you'll also need some kind of display widget. For small amounts of text, GtkEntry may do, but in your situation, GtkTreeView is probably what you're looking for.

Below you'll find a minimalistic sample application that loads file into text view and is capable of saving it back to file.

Code:
/*
   Compile me with:
     gcc -Wall -o text text.c $(pkg-config --cflags --libs gtk+-2.0)
*/
#include <gtk/gtk.h>

typedef struct _Data Data;
struct _Data
{
   GtkTextBuffer *buffer;
   GtkWindow     *parent;
};


static void
cb_load_file( GtkButton *button,
           Data      *data )
{
   static GtkWidget *dialog = NULL;

   if( ! dialog )
   {
      dialog = gtk_file_chooser_dialog_new( "Open file", data->parent,
                                   GTK_FILE_CHOOSER_ACTION_OPEN,
                                   GTK_STOCK_OPEN,
                                   GTK_RESPONSE_ACCEPT,
                                   GTK_STOCK_CANCEL,
                                   GTK_RESPONSE_REJECT,
                                   NULL );
   }

   if( gtk_dialog_run( GTK_DIALOG( dialog ) ) == GTK_RESPONSE_ACCEPT )
   {
      gchar *filename;
      gchar *text;

      filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) );
      g_file_get_contents( filename, &text, NULL, NULL );
      gtk_text_buffer_set_text( data->buffer, text, -1 );
      g_free( filename );
      g_free( text );
   }
   gtk_widget_hide( dialog );
}

static void
cb_save_file( GtkButton *button,
           Data      *data )
{
   static GtkWidget *dialog = NULL;

   if( ! dialog )
   {
      dialog = gtk_file_chooser_dialog_new( "Save file", data->parent,
                                   GTK_FILE_CHOOSER_ACTION_SAVE,
                                   GTK_STOCK_SAVE,
                                   GTK_RESPONSE_ACCEPT,
                                   GTK_STOCK_CANCEL,
                                   GTK_RESPONSE_REJECT,
                                   NULL );
   }

   if( gtk_dialog_run( GTK_DIALOG( dialog ) ) == GTK_RESPONSE_ACCEPT )
   {
      gchar *filename;
      gchar *text;

      g_object_get( G_OBJECT( data->buffer ), "text", &text, NULL );
      filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) );
      g_file_set_contents( filename, text, -1, NULL );
      g_free( filename );
      g_free( text );
   }
   gtk_widget_hide( dialog );
}

int
main( int    argc,
     char **argv )
{
   GtkWidget     *window,
              *vbox,
              *swindow,
              *text,
              *save,
              *load;
   GtkTextBuffer *buffer;
   Data          *data;

   gtk_init( &argc, &argv );

   data = g_slice_new( Data );

   window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
   g_signal_connect( G_OBJECT( window ), "destroy",
                 G_CALLBACK( gtk_main_quit ), NULL );

   vbox = gtk_vbox_new( FALSE, 6 );
   gtk_container_add( GTK_CONTAINER( window ), vbox );

   swindow = gtk_scrolled_window_new( NULL, NULL );
   gtk_box_pack_start( GTK_BOX( vbox ), swindow, TRUE, TRUE, 0 );

   text = gtk_text_view_new();
   buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW( text ) );
   gtk_container_add( GTK_CONTAINER( swindow ), text );

   save = gtk_button_new_from_stock( GTK_STOCK_SAVE );
   g_signal_connect( G_OBJECT( save ), "clicked",
                 G_CALLBACK( cb_save_file ), data );
   gtk_box_pack_start( GTK_BOX( vbox ), save, FALSE, FALSE, 0 );

   load = gtk_button_new_from_stock( GTK_STOCK_OPEN );
   g_signal_connect( G_OBJECT( load ), "clicked",
                 G_CALLBACK( cb_load_file ), data );
   gtk_box_pack_start( GTK_BOX( vbox ), load, FALSE, FALSE, 0 );

   data->buffer = buffer;
   data->parent = GTK_WINDOW( window );

   gtk_widget_show_all( window );

   gtk_main();

   g_slice_free( Data, data );

   return( 0 );
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 15, 2009 10:41 pm 
Offline
Familiar Face

Joined: Tue Jul 07, 2009 11:31 pm
Posts: 13
Thanks a lot for all the help


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 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