 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
hungerfish
Joined: 20 Apr 2007 Posts: 4
|
Posted: Fri Apr 20, 2007 12:06 pm Post subject: Contents of a folder in a gtk_tree_view |
|
|
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: 372 Location: State College, Pennsylvania
|
Posted: Fri Apr 20, 2007 6:27 pm Post subject: |
|
|
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
|
Posted: Sat Apr 21, 2007 10:35 am Post subject: |
|
|
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 |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|