scandir & gtk

This is a discussion on scandir & gtk within the C Programming forums, part of the General Programming Boards category; Hello I'm writing an app in c using the gtk-api. I have a "gtk_file_chooser_dialog" which is configured to 'open' folders. ...

  1. #1
    Registered User haifisch's Avatar
    Join Date
    Jun 2006
    Posts
    6

    Question scandir & gtk

    Hello
    I'm writing an app in c using the gtk-api.
    I have a "gtk_file_chooser_dialog" which is configured to 'open' folders.
    More specifically, a function "scanFiles(folder)" is called, folder being of type "char *folder".
    Code:
    scanFiles(char *folder)
    {
        int count,cnt;
        char *file;
        struct dirent **files;
        extern int alphasort();
    
        int fileSelect(struct dirent *files){...}
    
        printf("Selected folder: %s\n",folder);
        count = scandir64(folder, &files, fileSelect, alphasort);
        printf("Files found: %d\n",count);
    
        for (cnt = 0; cnt < count; ++cnt)
        {
            printf ("%s\n",files[cnt]->d_name);
        }
    }
    Is basically this function. "fileSelect "just specifies 'which' files to display.
    But what I really want the function "scanFiles" to do is to "return files;", not "printf();" them, so that I can then add them to a "gtk_list_store" in another function.
    how would I need to define the function scanFiles, what type must it be, as "files" is of "struct dirent **files"?

    Or is there perhaps another way of getting the contents from a directory selected by gtk_file_chooser flushed to a gtk_list_store using 'just' the gtk-api?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You have several options. You can pass files as a parameter to the function, but that would be messy because you'd have a struct dirent ***. Or you could return files from the function, but then the return value could possibly be ignored. (Perhaps you want this.) Anyway, those two options would give you one of these:
    Code:
    void scanFiles(char *folder, struct dirent ***files) {
        /* ... */
    }
    or
    Code:
    struct dirent **scanFiles(char *folder) {
        /* ... */
        return files;
    }
    I recommend the second, just because of how easy it is to code.

    Also, whatever method you use, you should free() that data if you can.

    Or is there perhaps another way of getting the contents from a directory selected by gtk_file_chooser flushed to a gtk_list_store using 'just' the gtk-api?
    I don't know about that, you'll have to check the documentation.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User haifisch's Avatar
    Join Date
    Jun 2006
    Posts
    6

    Lightbulb

    Thank you for your fast reply!
    OK, I used the second method and it works at least 'forwarding' the data does...now I'm back to battling the gtk-api...I did check the documentation by the way, but couldn't find anything fitting, so if somebody happens to know a little about gtk I'd appreciate a few hints.

    At current I'm trying to get my now 'forwarded' data crammed into a "gtk_list_store".
    I was thinking of using "gtk_list_store_append" & "gtk_list_store_set" to do this, but haven't figured out how to actually draw my 'finished' list once the data is in side, as this happens after the initial drawing of the gui-widgets. 'Adding data by hardcoding it works, but not adding it during runtime, when the user selects a folder which content is to be listed.
    I would post code, but a) I am shy and b) I don't really know which part to post...

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well, I have never programmed with GTK+ before, so I can't really help you there. The only thing I can suggest is looking around for similar code, because I'm sure it's a relatively common problem. This was one google hit I found: http://members.tripod.com/Yarick/FileBrowser.html

    I suspect that that code would probably be far too complicated to learn anything from, however. I'll keep looking.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User haifisch's Avatar
    Join Date
    Jun 2006
    Posts
    6

    Lightbulb Good tutorial for gtk_tree_view

    I've been digging around and I've found a very good tutorial regarding "gtk_list_store" and other things related to "gtk_tree_view".
    Just in case somebody else is looking for similar info, here's the link:
    http://scentric.net/tutorial/

  6. #6
    Registered User haifisch's Avatar
    Join Date
    Jun 2006
    Posts
    6

    Unhappy ...but it hasn't solved my problem.

    Now I must be missing something obvious here...This is the code that takes care of building and displaying my list...or it should do that
    Code:
    static GtkTreeModel *create_and_fill_model (void)
    {
        GtkListStore    *store;
        GtkTreeIter     iter;
        store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_STRING);
        /* folder = ("/home/user/path"); */
        if (folder != NULL)
        {
            gchar *file;
            scanFiles(folder);
            printf("Files found: &#37;d\n",count);
            gtk_list_store_clear(store);
            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)
    {...model = create_and_fill_model ();...}
    When not commenting out the 'red-bit', the files in that path are displayed in my list. Normally the folder-variable is NULL to beginn with, as the path should not be coded in like that, but selected by the user during runtime. I set the variable 'folder' from the following function and then I call "create_view_and_model" from there.
    Code:
    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);
    }
    As mentioned above, the 'purple-bit' works when called from main(),but this only happens when I set the folder-variable the 'red-way'. The files in the user-selected folder still get dumped via printf(); though, but the actual list is not getting drawn to the screen.
    So to summarise, when using the coded in path, the ui and the list as part of that get built from within main() and the list gets displayed. I suspect that the list not being updated has something to do with the fact that I'm not calling the "create_view_and_model" function from main() when user-selecting a path.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MinGW32 and gtk setup in Windows
    By Joelito in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-05-2009, 11:39 AM
  2. Replies: 1
    Last Post: 09-22-2008, 05:49 AM
  3. Scandir ()
    By gurvinder in forum C Programming
    Replies: 1
    Last Post: 08-11-2008, 04:19 PM
  4. GTK troubles...again...
    By cornholio in forum Linux Programming
    Replies: 4
    Last Post: 01-16-2006, 01:37 AM
  5. scandir select function
    By dsl24 in forum C Programming
    Replies: 3
    Last Post: 04-12-2002, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21