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 !
23 #if wxUSE_DIRDLG && defined( __WXGTK24__ )
25 #include "wx/dirdlg.h"
29 #include "wx/filedlg.h"
32 #include "wx/gtk/private.h"
34 #include <unistd.h> // chdir
36 //-----------------------------------------------------------------------------
37 // "clicked" for OK-button
38 //-----------------------------------------------------------------------------
41 static void gtk_dirdialog_ok_callback(GtkWidget
*widget
, wxDirDialog
*dialog
)
43 // change to the directory where the user went if asked
44 if (dialog
->HasFlag(wxDD_CHANGE_DIR
))
46 wxGtkString
filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
)));
50 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
51 event
.SetEventObject(dialog
);
52 dialog
->GetEventHandler()->ProcessEvent(event
);
56 //-----------------------------------------------------------------------------
57 // "clicked" for Cancel-button
58 //-----------------------------------------------------------------------------
61 static void gtk_dirdialog_cancel_callback(GtkWidget
*WXUNUSED(w
),
64 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
65 event
.SetEventObject(dialog
);
66 dialog
->GetEventHandler()->ProcessEvent(event
);
71 static void gtk_dirdialog_response_callback(GtkWidget
*w
,
75 if (response
== GTK_RESPONSE_ACCEPT
)
76 gtk_dirdialog_ok_callback(w
, dialog
);
77 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
78 gtk_dirdialog_cancel_callback(w
, dialog
);
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
86 IMPLEMENT_DYNAMIC_CLASS(wxDirDialog
,wxGenericDirDialog
)
88 BEGIN_EVENT_TABLE(wxDirDialog
,wxGenericDirDialog
)
89 EVT_BUTTON(wxID_OK
, wxDirDialog::OnFakeOk
)
92 wxDirDialog::wxDirDialog(wxWindow
* parent
, const wxString
& title
,
93 const wxString
& defaultPath
, long style
,
94 const wxPoint
& pos
, const wxSize
& sz
,
97 if (!gtk_check_version(2,4,0))
101 parent
= GetParentForModalDialog(parent
);
103 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
104 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
105 wxDefaultValidator
, wxT("dirdialog")))
107 wxFAIL_MSG( wxT("wxDirDialog creation failed") );
111 GtkFileChooserAction gtk_action
;
112 GtkWindow
* gtk_parent
= NULL
;
114 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
116 if (HasFlag(wxDD_DIR_MUST_EXIST
))
117 gtk_action
= GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
;
119 gtk_action
= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
;
121 m_widget
= gtk_file_chooser_dialog_new(
122 wxGTK_CONV(m_message
),
125 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
126 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
129 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
131 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically destroys
132 // the dialog when the user press ESC on the dialog: in that case a second call to
133 // ShowModal() would result in a bunch of Gtk-CRITICAL errors...
134 g_signal_connect (G_OBJECT(m_widget
),
136 G_CALLBACK (gtk_widget_hide_on_delete
),
139 // local-only property could be set to false to allow non-local files to be loaded.
140 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
141 // and the GtkFileChooserDialog should probably also be created with a backend,
142 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
143 // Currently local-only is kept as the default - true:
144 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
146 g_signal_connect (m_widget
, "response",
147 G_CALLBACK (gtk_dirdialog_response_callback
), this);
149 if ( !defaultPath
.empty() )
150 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget
),
151 defaultPath
.fn_str() );
154 wxGenericDirDialog::Create(parent
, title
, defaultPath
, style
, pos
, sz
, name
);
157 void wxDirDialog::OnFakeOk( wxCommandEvent
&event
)
159 if (!gtk_check_version(2,4,0))
162 wxGenericDirDialog::OnOK( event
);
165 int wxDirDialog::ShowModal()
167 if (!gtk_check_version(2,4,0))
168 return wxDialog::ShowModal();
170 return wxGenericDirDialog::ShowModal();
173 bool wxDirDialog::Show( bool show
)
175 if (!gtk_check_version(2,4,0))
176 return wxDialog::Show( show
);
178 return wxGenericDirDialog::Show( show
);
181 void wxDirDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
186 wxGenericDirDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
189 void wxDirDialog::SetPath(const wxString
& dir
)
191 if (!gtk_check_version(2,4,0))
193 if (wxDirExists(dir
))
195 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
200 wxGenericDirDialog::SetPath( dir
);
203 wxString
wxDirDialog::GetPath() const
205 if (!gtk_check_version(2,4,0))
207 wxGtkString
str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget
)));
208 return wxString(str
, *wxConvFileName
);
211 return wxGenericDirDialog::GetPath();
214 #endif // wxUSE_DIRDLG