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"
34 #include "wx/gtk/private.h"
36 #include <unistd.h> // chdir
38 #include "wx/filename.h" // wxFilename
39 #include "wx/tokenzr.h" // wxStringTokenizer
40 #include "wx/filefn.h" // ::wxGetCwd
41 #include "wx/msgdlg.h" // wxMessageDialog
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 extern void wxapp_install_idle_handler();
49 //-----------------------------------------------------------------------------
50 // "clicked" for OK-button
51 //-----------------------------------------------------------------------------
54 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxDirDialog
*dialog
)
56 int style
= dialog
->GetStyle();
57 gchar
* filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
59 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
60 #if GTK_CHECK_VERSION(2,7,3)
61 if(gtk_check_version(2,7,3) != NULL
)
63 if ((style
& wxSAVE
) && (style
& wxOVERWRITE_PROMPT
))
65 if ( g_file_test(filename
, G_FILE_TEST_EXISTS
) )
70 _("File '%s' already exists, do you really want to overwrite it?"),
71 wxString(wxConvFileName
->cMB2WX(filename
)).c_str());
73 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
74 wxYES_NO
| wxICON_QUESTION
);
75 if (dlg
.ShowModal() != wxID_YES
)
80 // change to the directory where the user went if asked
81 if (style
& wxCHANGE_DIR
)
83 // Use chdir to not care about filename encodings
84 gchar
* folder
= g_path_get_dirname(filename
);
91 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
92 event
.SetEventObject(dialog
);
93 dialog
->GetEventHandler()->ProcessEvent(event
);
97 //-----------------------------------------------------------------------------
98 // "clicked" for Cancel-button
99 //-----------------------------------------------------------------------------
102 static void gtk_filedialog_cancel_callback(GtkWidget
*WXUNUSED(w
),
105 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
106 event
.SetEventObject(dialog
);
107 dialog
->GetEventHandler()->ProcessEvent(event
);
112 static void gtk_filedialog_response_callback(GtkWidget
*w
,
116 wxapp_install_idle_handler();
118 if (response
== GTK_RESPONSE_ACCEPT
)
119 gtk_filedialog_ok_callback(w
, dialog
);
120 else if (response
== GTK_RESPONSE_CANCEL
)
121 gtk_filedialog_cancel_callback(w
, dialog
);
124 gtk_filedialog_cancel_callback(w
, dialog
);
125 dialog
->m_destroyed_by_delete
= true;
130 #endif // __WXGTK24__
132 //-----------------------------------------------------------------------------
134 //-----------------------------------------------------------------------------
136 IMPLEMENT_DYNAMIC_CLASS(wxDirDialog
,wxGenericDirDialog
)
138 BEGIN_EVENT_TABLE(wxDirDialog
,wxGenericDirDialog
)
139 EVT_BUTTON(wxID_OK
, wxDirDialog
::OnFakeOk
)
142 wxDirDialog
::wxDirDialog(wxWindow
* parent
, const wxString
& title
,
143 const wxString
& defaultPath
, long style
,
144 const wxPoint
& pos
, const wxSize
& sz
,
145 const wxString
& name
)
148 if (!gtk_check_version(2,4,0))
151 m_needParent
= false;
152 m_destroyed_by_delete
= false;
154 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
155 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
156 wxDefaultValidator
, wxT("filedialog")))
158 wxFAIL_MSG( wxT("wxDirDialog creation failed") );
162 GtkFileChooserAction gtk_action
;
163 GtkWindow
* gtk_parent
= NULL
;
165 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
167 gtk_action
= GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
;
168 if (style
& wxDD_NEW_DIR_BUTTON
)
169 gtk_action
= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
;
171 m_widget
= gtk_file_chooser_dialog_new(
172 wxGTK_CONV(m_message
),
175 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
176 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
179 // local-only property could be set to false to allow non-local files to be loaded.
180 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
181 // and the GtkFileChooserDialog should probably also be created with a backend,
182 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
183 // Currently local-only is kept as the default - true:
184 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
186 g_signal_connect(G_OBJECT(m_widget
), "response",
187 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback
), (gpointer
)this);
189 if ( !defaultPath
.empty() )
190 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget
),
191 wxConvFileName
->cWX2MB(defaultPath
) );
195 wxGenericDirDialog
::Create(parent
, title
, defaultPath
, style
, pos
, sz
, name
);
198 wxDirDialog
::~wxDirDialog()
201 if (!gtk_check_version(2,4,0))
203 if (m_destroyed_by_delete
)
209 void wxDirDialog
::OnFakeOk( wxCommandEvent
&event
)
212 if (!gtk_check_version(2,4,0))
213 wxDialog
::OnOK( event
);
216 wxGenericDirDialog
::OnOK( event
);
219 int wxDirDialog
::ShowModal()
222 if (!gtk_check_version(2,4,0))
223 return wxDialog
::ShowModal();
226 return wxGenericDirDialog
::ShowModal();
229 bool wxDirDialog
::Show( bool show
)
232 if (!gtk_check_version(2,4,0))
233 return wxDialog
::Show( show
);
236 return wxGenericDirDialog
::Show( show
);
239 void wxDirDialog
::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
244 wxGenericDirDialog
::DoSetSize( x
, y
, width
, height
, sizeFlags
);
247 void wxDirDialog
::SetPath(const wxString
& dir
)
250 if (!gtk_check_version(2,4,0))
252 if (wxDirExists(dir
))
254 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
259 wxGenericDirDialog
::SetPath( dir
);
262 wxString wxDirDialog
::GetPath() const
265 if (!gtk_check_version(2,4,0))
266 return wxConvFileName
->cMB2WX( gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(m_widget
) ) );
269 return wxGenericDirDialog
::GetPath();
272 #endif // wxUSE_DIRDLG