fix the bug which prevented the same wxFile/DirDialog object from being used twice...
[wxWidgets.git] / src / gtk / dirdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dirdlg.cpp
3 // Purpose: native implementation of wxDirDialogGTK
4 // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp, Francesco Montorsi
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13
14
15 /*
16 NOTE: the GtkFileChooser interface can be used both for wxFileDialog and for wxDirDialogGTK.
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 !
19 */
20
21
22
23 #if wxUSE_DIRDLG
24
25 #include "wx/dirdlg.h"
26
27 #ifndef WX_PRECOMP
28 #include "wx/intl.h"
29 #include "wx/filedlg.h"
30 #endif
31
32 #ifdef __WXGTK24__ // only for GTK+ > 2.4 there is GtkFileChooserDialog
33
34 #include <gtk/gtk.h>
35 #include "wx/gtk/private.h"
36
37 #include <unistd.h> // chdir
38
39
40 //-----------------------------------------------------------------------------
41 // idle system
42 //-----------------------------------------------------------------------------
43
44 extern void wxapp_install_idle_handler();
45
46 //-----------------------------------------------------------------------------
47 // "clicked" for OK-button
48 //-----------------------------------------------------------------------------
49
50 extern "C" {
51 static void gtk_dirdialog_ok_callback(GtkWidget *widget, wxDirDialogGTK *dialog)
52 {
53 gchar* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
54
55 // change to the directory where the user went if asked
56 if (dialog->HasFlag(wxDD_CHANGE_DIR))
57 chdir(filename);
58
59 g_free(filename);
60
61 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
62 event.SetEventObject(dialog);
63 dialog->GetEventHandler()->ProcessEvent(event);
64 }
65 }
66
67 //-----------------------------------------------------------------------------
68 // "clicked" for Cancel-button
69 //-----------------------------------------------------------------------------
70
71 extern "C" {
72 static void gtk_dirdialog_cancel_callback(GtkWidget *WXUNUSED(w),
73 wxDirDialogGTK *dialog)
74 {
75 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
76 event.SetEventObject(dialog);
77 dialog->GetEventHandler()->ProcessEvent(event);
78 }
79 }
80
81 extern "C" {
82 static void gtk_dirdialog_response_callback(GtkWidget *w,
83 gint response,
84 wxDirDialogGTK *dialog)
85 {
86 wxapp_install_idle_handler();
87
88 if (response == GTK_RESPONSE_ACCEPT)
89 gtk_dirdialog_ok_callback(w, dialog);
90 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
91 gtk_dirdialog_cancel_callback(w, dialog);
92 }
93 }
94
95 #endif // __WXGTK24__
96
97 //-----------------------------------------------------------------------------
98 // wxDirDialogGTK
99 //-----------------------------------------------------------------------------
100
101 IMPLEMENT_DYNAMIC_CLASS(wxDirDialogGTK,wxGenericDirDialog)
102
103 BEGIN_EVENT_TABLE(wxDirDialogGTK,wxGenericDirDialog)
104 EVT_BUTTON(wxID_OK, wxDirDialogGTK::OnFakeOk)
105 END_EVENT_TABLE()
106
107 wxDirDialogGTK::wxDirDialogGTK(wxWindow* parent, const wxString& title,
108 const wxString& defaultPath, long style,
109 const wxPoint& pos, const wxSize& sz,
110 const wxString& name)
111 {
112 #ifdef __WXGTK24__
113 if (!gtk_check_version(2,4,0))
114 {
115 m_message = title;
116 m_needParent = false;
117
118 if (!PreCreation(parent, pos, wxDefaultSize) ||
119 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
120 wxDefaultValidator, wxT("dirdialog")))
121 {
122 wxFAIL_MSG( wxT("wxDirDialogGTK creation failed") );
123 return;
124 }
125
126 GtkFileChooserAction gtk_action;
127 GtkWindow* gtk_parent = NULL;
128 if (parent)
129 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
130
131 gtk_action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
132 if (style & wxDD_NEW_DIR_BUTTON)
133 gtk_action = GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
134
135 m_widget = gtk_file_chooser_dialog_new(
136 wxGTK_CONV(m_message),
137 gtk_parent,
138 gtk_action,
139 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
140 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
141 NULL);
142
143 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
144
145 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically destroys
146 // the dialog when the user press ESC on the dialog: in that case a second call to
147 // ShowModal() would result in a bunch of Gtk-CRITICAL errors...
148 g_signal_connect (G_OBJECT(m_widget),
149 "delete_event",
150 G_CALLBACK (gtk_widget_hide_on_delete),
151 (gpointer)this);
152
153 // local-only property could be set to false to allow non-local files to be loaded.
154 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
155 // and the GtkFileChooserDialog should probably also be created with a backend,
156 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
157 // Currently local-only is kept as the default - true:
158 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
159
160 g_signal_connect (m_widget, "response",
161 G_CALLBACK (gtk_dirdialog_response_callback), this);
162
163 if ( !defaultPath.empty() )
164 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget),
165 wxConvFileName->cWX2MB(defaultPath) );
166 }
167 else
168 #endif
169 wxGenericDirDialog::Create(parent, title, defaultPath, style, pos, sz, name);
170 }
171
172 void wxDirDialogGTK::OnFakeOk( wxCommandEvent &event )
173 {
174 #ifdef __WXGTK24__
175 if (!gtk_check_version(2,4,0))
176 wxDialog::OnOK( event );
177 else
178 #endif
179 wxGenericDirDialog::OnOK( event );
180 }
181
182 int wxDirDialogGTK::ShowModal()
183 {
184 #ifdef __WXGTK24__
185 if (!gtk_check_version(2,4,0))
186 return wxDialog::ShowModal();
187 else
188 #endif
189 return wxGenericDirDialog::ShowModal();
190 }
191
192 bool wxDirDialogGTK::Show( bool show )
193 {
194 #ifdef __WXGTK24__
195 if (!gtk_check_version(2,4,0))
196 return wxDialog::Show( show );
197 else
198 #endif
199 return wxGenericDirDialog::Show( show );
200 }
201
202 void wxDirDialogGTK::DoSetSize(int x, int y, int width, int height, int sizeFlags )
203 {
204 if (!m_wxwindow)
205 return;
206 else
207 wxGenericDirDialog::DoSetSize( x, y, width, height, sizeFlags );
208 }
209
210 void wxDirDialogGTK::SetPath(const wxString& dir)
211 {
212 #ifdef __WXGTK24__
213 if (!gtk_check_version(2,4,0))
214 {
215 if (wxDirExists(dir))
216 {
217 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(dir));
218 }
219 }
220 else
221 #endif
222 wxGenericDirDialog::SetPath( dir );
223 }
224
225 wxString wxDirDialogGTK::GetPath() const
226 {
227 #ifdef __WXGTK24__
228 if (!gtk_check_version(2,4,0))
229 return wxConvFileName->cMB2WX( gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(m_widget) ) );
230 else
231 #endif
232 return wxGenericDirDialog::GetPath();
233 }
234
235 #endif // wxUSE_DIRDLG