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 Oct 31, 2014 12:07 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Contents of a folder in a gtk_tree_view
PostPosted: Fri Apr 20, 2007 12:06 pm 
Offline

Joined: Fri Apr 20, 2007 11:07 am
Posts: 4
Hi!

I'm writing a program which uses "gtk_tree_view" to display the contents
of a folder. This folder is selected by the user through a
"gtk_file_chooser_dialog". Now while I can get my app to display the
files of any folder I want when I tell it which folder to scan by
'hardcoding' in the path (so the model is filled and created when the app is first started), I can't figure out how to call the appropriate
functions when 'user-selecting' this path. To clarify:
Code:
static GtkTreeModel *create_and_fill_model (void)
{
   GtkListStore    *store;
   ...
   scanFiles(folder);
         printf("Files found: %d\n",count);
         for (cnt = 0; cnt < count; ++cnt)
         {
             file = files[cnt]->d_name;
             printf ("%s\n",file);
             gtk_list_store_append (store, &iter);
             gtk_list_store_set (store, &iter,COL_TYPE,                      
"none",COL_NAME, file,-1);
         }
   return GTK_TREE_MODEL (store);
}
static GtkWidget *create_view_and_model (void)
{
   GtkWidget       *view;
   GtkTreeModel    *model;
   ...
   model = create_and_fill_model ();
   ...
   return view;
}
static void openDialog( GtkWidget *widget,GtkWidget *window)
{
   GtkWidget *dialogOPEN;
   ...
         folder = gtk_file_chooser_get_current_folder(...)
         create_view_and_model();
         ...   
     }
}
int main( int argc,char *argv[] )
{
   GtkWidget *list;
   GtkWidget *buttonOPEN;   
   ...
   list = create_view_and_model();
   buttonOPEN = gtk_button_new_from_stock(GTK_STOCK_OPEN);
g_signal_connect(G_OBJECT(buttonOPEN),"clicked",
G_CALLBACK(openDialog),(gpointer) window);
   ...
}

This is the basic structure of my program. I'm guessing that because I
am 'recalling' "create_view_and_model" from within "openDIALOG" the list
is not getting drawn inside the widget. I know that the
'data'(folder) is getting to my function, as I can dump them with
printf();
How do I solve this?

_________________
"...and also because he is one of those people who find reasons to blame Microsoft for almost all their problems in life."


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 6:27 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
I would recommend a few things. First, why not use GtkFileChooserButton instead of your own button? You can use the file chooser button's "selection-changed" signal to check when it was changed.

You can use g_get_home_dir() to check what the user's home directory is. Connect the selection-changed signal, and then set the file chooser button's folder to the home directory or whatever hardcoded folder you want. Then, that will do the initial population. Even if it doesn't, you can just call the callback function explicitly.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 21, 2007 10:35 am 
Offline

Joined: Fri Apr 20, 2007 11:07 am
Posts: 4
First let me say thank you for your reply! :)
However, I don't think that the type of button that calls the file_chooser_dialog makes any difference regarding my problem.
This is the function 'my' button calls when clicked:
Code:
char *folder;
static void openDialog( GtkWidget *widget,GtkWidget *window)
{
    GtkWidget *dialogOPEN;


    dialogOPEN = gtk_file_chooser_dialog_new ("Select a folder", GTK_WINDOW (window),
                      GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
                      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                      GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                      NULL);

    if (gtk_dialog_run (GTK_DIALOG (dialogOPEN)) == GTK_RESPONSE_ACCEPT)
    {
        folder = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER (dialogOPEN));
        create_view_and_model();
        g_free (folder);
    }
    gtk_widget_destroy (dialogOPEN);
}

This pops up the file_choose_dialog and the user can select what he wants.
On 'clicking' OK, "folder" is mapped to the selected path, and "create_view_and_model" is called. Now as far as I can work out, the TreeView-model is getting populated with the correct data, but it is not (re-)connected to the actual view.
(When "folder" is mapped at start-up, the "model" is populated and the view 'shows' its data accordingly)
This is the function that gets called from the file_chooser:
Code:
static GtkWidget *create_view_and_model (void)
{
    GtkCellRenderer *renderer;
    GtkTreeModel    *model;
    GtkWidget       *view;

    view = gtk_tree_view_new ();
    renderer = gtk_cell_renderer_text_new ();

    gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),0,"Type",renderer,"text", COL_TYPE,NULL);
    gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),1,"Name",renderer,"text", COL_NAME,NULL);

    gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (view),TRUE);
    gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW (view),FALSE);
    gtk_tree_view_set_enable_search(GTK_TREE_VIEW(view),FALSE);
    gtk_tree_view_columns_autosize (GTK_TREE_VIEW (view));

    model = create_and_fill_model ();
    gtk_tree_view_set_model (GTK_TREE_VIEW (view), model);
    g_object_unref (model);

    return view;
}

What am I doing wrong?

_________________
"...and also because he is one of those people who find reasons to blame Microsoft for almost all their problems in life."


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

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group