GtkFileChooser problem

classic Classic list List threaded Threaded
4 messages Options
rbd
Reply | Threaded
Open this post in threaded view
|

GtkFileChooser problem

rbd

Hi all,

I am having a problem with GtkFileChooser when the chooser action is set to
GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER, i.e., I want to allow the user to
specify any existing or as-yet-nonexistent directory. The documentation says
that use of GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:

"... Indicates a mode for creating a new folder. The file chooser will let the
user name an existing or new folder."

This certainly implies that if the user specifies an existing directory the
dialog will not complain that it cannot create the specified directory because
it already exists. Unfortunately, this is exactly what is happening to me! I
get a popup message that says:

         The folder could not be created
    Error creating /yadayadayada...: File exists

The chooser dialog remains visible while this message is displayed and after it
is dismissed, waiting for me to select something else.

Curiously, this seems to happen only when I specify the current working
directory (CWD) of the process. If I specify any other pre-existing directory
this error does not occur. If I run this program from a different directory and
specify my original CWD where it failed before, the error does not occur. It
seems perfectly reasonable to me to make the CWD the default directory of
choice for the user and I do not want to change this.

Can anyone explain this bizarre behavior? I am using gtk 3.22.24 (MacPorts
release) under MacOS 10.12.6.

The relevant code follows, somewhat simplified from its original state to make
it more intelligible here. Please advise if you see something stupid.

         GtkWidget *d;
         GtkFileChooser *fc;
         char cwdbuf[BIGBUF];
         int rspID;

         if ((d= gtk_file_chooser_dialog_new("Dir Select", NULL,
(GtkFileChooserAction) GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER, "Cancel",
GTK_RESPONSE_CANCEL, "Continue", GTK_RESPONSE_ACCEPT, NULL)) == (GtkWidget *)
0)
                 return -1;

         fc= GTK_FILE_CHOOSER(d);

         if (getcwd(cwdbuf, (size_t) MAXPATHLEN) == (char *) 0)
                 return -1;
         gtk_file_chooser_set_current_name(fc, cwdbuf);

         rspID= gtk_dialog_run(GTK_DIALOG(d));

The error occurs within gtk_dialog_run() of course, as the dialog remains
displayed and I can change the selection after I dismiss the error message.

I have also tried doing

         gtk_file_chooser_set_create_folders(fc, FALSE);

prior to calling gtk_dialog_run() but that makes no difference.

Thanks in advance for any ideas!

Roger Davis
Univ. of Hawaii
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
rbd
Reply | Threaded
Open this post in threaded view
|

Re: GtkFileChooser problem

rbd


Hi all,

I have done some further experimentation with regard to my GtkFileChooser
problem and discovered that the piece of my code that seems to be
triggering this problem is the call to gtk_file_chooser_set_current_name()
-- if I remove that then I do not get the error described in my last
message.

Looking closer at the documentation I see a statement that says this
function should not be used when an existing file is being specified, so I
will take responsibility for ignoring that warning. I have to say,
however, that this seems to me to be an unacceptable restriction -- why
shouldn't I be able to initialize the text entry field at the top of the
dialog to a completely clear and unambiguous presentation of the default
name that will be returned by the dialog? And, why does a
GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER dialog which is supposedly designed
to work with either pre-existing or yet-to-be-created filesystem objects
(i) try to create something that already exists and then (ii) complain
when that creation attempt fails, whether or not I call
gtk_file_chooser_set_current_name() to set the text entry field?

I have noticed other inconsistent and weird behavior in the UI of this
widget through my further experimentation today:

(1) When I do use gtk_file_chooser_set_current_name() to initialize the
text entry field as shown in my original code, when I click the dialog's
OK response button I get the error I have described. But if instead of
clicking that button I activate the cursor in the text entry field and hit
RETURN (without making any change to that field), everything works fine.
Why is only clicking the OK button problematic?

(2) Independently of whether or not I use
gtk_file_chooser_set_current_name() to initialize the text entry field to
the name of a pre-existing current working directory (CWD), I have noticed
that the OK response button will often be grayed out (and hence
unclickable) even when the final path component of that CWD is selected in
the dialog's large directory contents subwindow when the dialog is first
mapped. Often I can fix this only by clicking on different directory
contents subwindow objects (i.e., other subdirectories), perhaps several
times, and then re-clicking on the subdirectory object which corresponds
to my CWD (and which was originally selected). Only then does the OK
response button get activated and become clickable.

I can appreciate that this widget may require a complicated
implementation, and that in particular it may be difficult to properly
correlate the text entry field with the directory contents subwindow. I
have to say, however, that I think it would be worthwhile to get rid of
the confusing limitations and behaviors noted above.

If there is further documentation on these issues somewhere that would
help me to understand and avoid them better I would very much like to see
it.

Thanks!

Roger Davis
Univ. of Hawaii

_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: GtkFileChooser problem

tito-wolit
On 10/25/2017 12:43 AM, rbd wrote:

>
>
> Hi all,
>
> I have done some further experimentation with regard to my
> GtkFileChooser problem and discovered that the piece of my code that
> seems to be triggering this problem is the call to
> gtk_file_chooser_set_current_name() -- if I remove that then I do not
> get the error described in my last message.
>
> Looking closer at the documentation I see a statement that says this
> function should not be used when an existing file is being specified, so
> I will take responsibility for ignoring that warning. I have to say,
> however, that this seems to me to be an unacceptable restriction -- why
> shouldn't I be able to initialize the text entry field at the top of the
> dialog to a completely clear and unambiguous presentation of the default
> name that will be returned by the dialog? And, why does a
> GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER dialog which is supposedly
> designed to work with either pre-existing or yet-to-be-created
> filesystem objects (i) try to create something that already exists and
> then (ii) complain when that creation attempt fails, whether or not I
> call gtk_file_chooser_set_current_name() to set the text entry field?
>
> I have noticed other inconsistent and weird behavior in the UI of this
> widget through my further experimentation today:
>
> (1) When I do use gtk_file_chooser_set_current_name() to initialize the
> text entry field as shown in my original code, when I click the dialog's
> OK response button I get the error I have described. But if instead of
> clicking that button I activate the cursor in the text entry field and
> hit RETURN (without making any change to that field), everything works
> fine. Why is only clicking the OK button problematic?
>
> (2) Independently of whether or not I use
> gtk_file_chooser_set_current_name() to initialize the text entry field
> to the name of a pre-existing current working directory (CWD), I have
> noticed that the OK response button will often be grayed out (and hence
> unclickable) even when the final path component of that CWD is selected
> in the dialog's large directory contents subwindow when the dialog is
> first mapped. Often I can fix this only by clicking on different
> directory contents subwindow objects (i.e., other subdirectories),
> perhaps several times, and then re-clicking on the subdirectory object
> which corresponds to my CWD (and which was originally selected). Only
> then does the OK response button get activated and become clickable.
>
> I can appreciate that this widget may require a complicated
> implementation, and that in particular it may be difficult to properly
> correlate the text entry field with the directory contents subwindow. I
> have to say, however, that I think it would be worthwhile to get rid of
> the confusing limitations and behaviors noted above.
>
> If there is further documentation on these issues somewhere that would
> help me to understand and avoid them better I would very much like to
> see it.
>
> Thanks!
>
> Roger Davis
> Univ. of Hawaii
>

Hi,
i recall having struggled with a file chooser some years ago,
but after some iterations got something that worked for me
for simple file operations. It was however impossible at that
time to tell the file chooser to open a file OR a dir so i ended
reinventing the wheel and wrote my own file chooser widget
( if you are interested in that drop a mail, but it is a lot
of code, some 1000+ lines).
Nonetheless attached inline you will find a copy of what did
work for me in the hope that it helps you. This is for gtk 2.0 tough
and maybe needs some adaptations.

Ciao,
Tito

/*
*
* Gtk+2.x  Utility routines: File selection widget.
* Copyright (C) 2002-2009 by <[hidden email]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Please read the COPYING and README file!!!
*
* Report bugs to <[hidden email]>
*
*/

/*#################################### File Selection Widget #####################################################*/

#include <stdio.h>
#include <gtk/gtk.h>
#include "mylib.h"
#include "mygtk.h"

/*
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
*/

char *file_chooser(const char *title, GtkFileChooserAction fs_action, gboolean local_only, gboolean show_hidden,
                                        const char *name, const char *folder, const char *shortcut_folder)
{
        GtkWidget *dialog;
        char *tmp;
        char *filename = NULL;;

        tmp = g_locale_to_utf8((title) ? title : "", -1,NULL,NULL,NULL);

        dialog = gtk_file_chooser_dialog_new (tmp,
                                                NULL, /*parent_window*/
                                                fs_action,
                                                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                                GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                                NULL);
        XFREE(tmp);
        gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(dialog), local_only);
        gtk_file_chooser_set_show_hidden(GTK_FILE_CHOOSER(dialog), show_hidden);
        /* Returns a GsList *, not a char * */
        /*gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), select_multiple);*/

        if (name) {
                tmp = g_locale_to_utf8((name), -1, NULL, NULL, NULL);
                gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), tmp);
                XFREE(tmp);
        }
       
        if (folder) {
                tmp = g_locale_to_utf8((folder), -1, NULL, NULL, NULL);
                gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), tmp);
                XFREE(tmp);
        }

        if (shortcut_folder) {
                tmp = g_locale_to_utf8((shortcut_folder), -1, NULL, NULL, NULL);
                gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog), tmp, NULL);
                XFREE(tmp);
        }

        if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
                filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (dialog));
        }
        if (G_IS_OBJECT (dialog)) {
                gtk_widget_destroy (dialog);
        }
        return filename;
}

char *default_file_chooser(const char *title, GtkFileChooserAction fs_action)
{
        return file_chooser(title, fs_action, FALSE, TRUE, NULL, NULL, NULL);
}

/*############################### End of File Selection Widget #####################################################*/
 
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
rbd
Reply | Threaded
Open this post in threaded view
|

Re: GtkFileChooser problem

rbd
In reply to this post by rbd

> Hi,
> i recall having struggled with a file chooser some years ago,
> but after some iterations got something that worked for me
> for simple file operations. It was however impossible at that
> time to tell the file chooser to open a file OR a dir so i ended
> reinventing the wheel and wrote my own file chooser widget
> ( if you are interested in that drop a mail, but it is a lot
> of code, some 1000+ lines).
> Nonetheless attached inline you will find a copy of what did
> work for me in the hope that it helps you. This is for gtk 2.0 tough
> and maybe needs some adaptations.

Thanks very much for the example code, Tito! I have something that works
well enough for me to be usable despite the oddities of the existing API
and UI behavior, but I will inspect your code to see if there is anything
there that I can use to improve my situation.

Grazie,

Roger
_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list