Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#10917 closed defect (fixed)

wxGTK wxFileDialog::SetDirectory and ::SetFilename problems

Reported by: dghart Owned by:
Priority: normal Milestone: 2.9.1
Component: wxGTK Version: stable-latest
Keywords: wxFileDialog Cc:
Blocked By: Blocking:
Patch: yes

Description

A recent wxForum post, http://wxforum.shadonet.com/viewtopic.php?t=24490, revealed that the wxGTK wxFileDialog (which uses the native control) has problems with setting and getting the default directory and file. There are three issues:

Supplying default values via the ctor get passed to the gtk control, and also stored in m_dir and m_fileName. Calls to SetDirectory() and SetFilename() do only the former.

GetDirectory() and GetFilename() don't try to use m_dir and m_fileName.

gtk_file_chooser_get_current_folder() doesn't seem to return the value set by gtk_file_chooser_set_current_folder() until after the dialog has been shown.

So GetDirectory() and GetFilename() will return "" until the dialog has been shown. Also, SetFilename() calls SetPath(wxFileName(GetDirectory(), name).GetFullPath()). Since the dialog won't have been shown yet, GetDirectory() returns "", so SetPath() receives just a filename; which causes gtk_file_chooser_set_filename() to fail with the assert:
Gtk-CRITICAL : gtk_file_system_unix_get_parent: assertion `g_path_is_absolute (filename)' failed.

Passing a default dir and filename to the wxFileDialog ctor does work: the file is selected within the default dir, and both GetDirectory() and GetFilename() return the correct values once the dialog has closed.

The patches (for trunk and WX_2_8_BRANCH, which has the same problem) make the setters set m_dir and m_fileName; the getters now use these values if the gtk2 calls fail. The trunk one includes a doc patch. Also attached is a diff to the minimal sample that demonstrates the problem; the first dialog has defaults set in its ctor, the second using the setters.

Attachments (3)

trunk.diff download (2.5 KB) - added by dghart 5 years ago.
2_8.diff download (2.9 KB) - added by dghart 5 years ago.
minimal-sample.diff download (1.8 KB) - added by dghart 5 years ago.

Download all attachments as: .zip

Change History (9)

Changed 5 years ago by dghart

Changed 5 years ago by dghart

Changed 5 years ago by dghart

comment:1 Changed 5 years ago by vadz

  • Status changed from new to confirmed

Thanks for fixing this problem!

I have just one question: why does SetFilename() call SetPath(wxFileName(path, name).GetFullPath()), shouldn't it just use GetPath() here?

comment:2 follow-up: Changed 5 years ago by dghart

I have just one question: why does SetFilename() call SetPath(wxFileName(path, name).GetFullPath()), shouldn't it just use GetPath() here?

Well, GetPath() calls gtk_file_chooser_get_filename() which returns a filepath (I think; the gtk docs tend not to distinguish clearly between filenames and filepaths). So it would have to be stripped of its filename first, before it can be used as the path of the new name. Whereas GetDirectory() uses gtk_file_chooser_get_current_folder(), which gives the path direct (except they'll both usually return null as the dialog won't yet have been shown).

comment:3 in reply to: ↑ 2 Changed 5 years ago by vadz

Replying to dghart:

I have just one question: why does SetFilename() call SetPath(wxFileName(path, name).GetFullPath()), shouldn't it just use GetPath() here?

Well, GetPath() calls gtk_file_chooser_get_filename()

Sorry, I wasn't precise enough: I meant to ask whether it shouldn't use wxFileName::GetPath(). I.e. shouldn't the code read SetPath(wxFileName(path, name).GetPath())?

Thanks!

comment:4 Changed 5 years ago by dghart

Ah, I see what you mean. Apologies.

wxFileDialog::SetFilename calls wxFileDialog::SetPath which calls wxGtkFileChooser::SetPath which uses gtk_file_chooser_set_filename. Despite the misleading names, gtk_file_chooser_set_filename actually sets a filepath, as do both SetPath()s.

So GetFullPath() is correct afaict. Indeed if it isn't used, the name parameter wouldn't be sent to the gtk widget.

comment:5 Changed 5 years ago by juliansmart

  • Resolution set to fixed
  • Status changed from confirmed to closed

Applied, many thanks.

comment:6 Changed 4 years ago by VZ

(In [62769]) Don't assert if the path is not absolute in wxFileDialog::SetPath().

Remove the assert added in r62101 (see #10917), it was wrong as the other
ports do not assert in this case. Instead, just ensure that the path we use
with the GTK+ native chooser is absolute.

Note: See TracTickets for help on using tickets.