GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Contents of a folder in a gtk_tree_view

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
hungerfish



Joined: 20 Apr 2007
Posts: 4

PostPosted: Fri Apr 20, 2007 12:06 pm    Post subject: Contents of a folder in a gtk_tree_view Reply with quote

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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

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?
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Fri Apr 20, 2007 6:27 pm    Post subject: Reply with quote

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.
Back to top
hungerfish



Joined: 20 Apr 2007
Posts: 4

PostPosted: Sat Apr 21, 2007 10:35 am    Post subject: Reply with quote

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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP