1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dirdlg.cpp
3 // Purpose: native implementation of wxDirDialog
4 // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp, Francesco Montorsi
6 // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
16 NOTE: the GtkFileChooser interface can be used both for wxFileDialog and for wxDirDialog.
17 Thus following code is very similar (even if not identic) to src/gtk/filedlg.cpp
18 If you find a problem in this code, remember to check also that file !
25 #include "wx/dirdlg.h"
29 #include "wx/filedlg.h"
32 #include "wx/gtk/private.h"
35 static void gtk_dirdialog_response_callback(GtkWidget
* WXUNUSED(w
),
39 if (response
== GTK_RESPONSE_ACCEPT
)
40 dialog
->GTKOnAccept();
41 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
42 dialog
->GTKOnCancel();
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxDirDialog
, wxDialog
)
52 wxDirDialog::wxDirDialog(wxWindow
* parent
,
53 const wxString
& title
,
54 const wxString
& defaultPath
,
57 const wxSize
& WXUNUSED(sz
),
58 const wxString
& WXUNUSED(name
))
60 Create(parent
, title
, defaultPath
, style
, pos
);
63 bool wxDirDialog::Create(wxWindow
* parent
,
64 const wxString
& title
,
65 const wxString
& defaultPath
,
68 const wxSize
& WXUNUSED(sz
),
69 const wxString
& WXUNUSED(name
))
73 parent
= GetParentForModalDialog(parent
, style
);
75 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
76 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
77 wxDefaultValidator
, wxT("dirdialog")))
79 wxFAIL_MSG( wxT("wxDirDialog creation failed") );
83 GtkWindow
* gtk_parent
= NULL
;
85 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
87 m_widget
= gtk_file_chooser_dialog_new(
88 wxGTK_CONV(m_message
),
90 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
,
91 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
92 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
94 g_object_ref(m_widget
);
96 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
98 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically destroys
99 // the dialog when the user press ESC on the dialog: in that case a second call to
100 // ShowModal() would result in a bunch of Gtk-CRITICAL errors...
101 g_signal_connect (m_widget
,
103 G_CALLBACK (gtk_widget_hide_on_delete
),
106 // local-only property could be set to false to allow non-local files to be loaded.
107 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
108 // and the GtkFileChooserDialog should probably also be created with a backend,
109 // e.g. "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
110 // Currently local-only is kept as the default - true:
111 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
113 g_signal_connect (m_widget
, "response",
114 G_CALLBACK (gtk_dirdialog_response_callback
), this);
116 if ( !defaultPath
.empty() )
117 SetPath(defaultPath
);
122 void wxDirDialog::GTKOnAccept()
124 wxGtkString
str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
)));
125 m_selectedDirectory
= wxString::FromUTF8(str
);
127 // change to the directory where the user went if asked
128 if (HasFlag(wxDD_CHANGE_DIR
))
130 wxSetWorkingDirectory(m_selectedDirectory
);
136 void wxDirDialog::GTKOnCancel()
138 EndDialog(wxID_CANCEL
);
141 void wxDirDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
146 wxDirDialogBase::DoSetSize( x
, y
, width
, height
, sizeFlags
);
149 void wxDirDialog::SetPath(const wxString
& dir
)
151 if (wxDirExists(dir
))
153 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
158 wxString
wxDirDialog::GetPath() const
160 return m_selectedDirectory
;
163 #endif // wxUSE_DIRDLG