]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/filedlg.cpp
allow dialog to be resized smaller with extra control
[wxWidgets.git] / src / gtk / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/filedlg.cpp
3 // Purpose: native implementation of wxFileDialog
4 // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp
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 #if wxUSE_FILEDLG
14
15 #include "wx/filedlg.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #include "wx/msgdlg.h"
20 #endif
21
22 #include <gtk/gtk.h>
23 #include "wx/gtk/private.h"
24
25 #include <unistd.h> // chdir
26
27 #include "wx/filename.h" // wxFilename
28 #include "wx/tokenzr.h" // wxStringTokenizer
29 #include "wx/filefn.h" // ::wxGetCwd
30
31 //-----------------------------------------------------------------------------
32 // "clicked" for OK-button
33 //-----------------------------------------------------------------------------
34
35 extern "C" {
36 static void gtk_filedialog_ok_callback(GtkWidget *widget, wxFileDialog *dialog)
37 {
38 int style = dialog->GetWindowStyle();
39 wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)));
40
41 // gtk version numbers must be identical with the one in ctor (that calls set_do_overwrite_confirmation)
42 #if GTK_CHECK_VERSION(2,7,3)
43 if(gtk_check_version(2,7,3) != NULL)
44 #endif
45 if ((style & wxFD_SAVE) && (style & wxFD_OVERWRITE_PROMPT))
46 {
47 if ( g_file_test(filename, G_FILE_TEST_EXISTS) )
48 {
49 wxString msg;
50
51 msg.Printf(
52 _("File '%s' already exists, do you really want to overwrite it?"),
53 wxString(filename, *wxConvFileName));
54
55 wxMessageDialog dlg(dialog, msg, _("Confirm"),
56 wxYES_NO | wxICON_QUESTION);
57 if (dlg.ShowModal() != wxID_YES)
58 return;
59 }
60 }
61
62 if (style & wxFD_FILE_MUST_EXIST)
63 {
64 if ( !g_file_test(filename, G_FILE_TEST_EXISTS) )
65 {
66 wxMessageDialog dlg( dialog, _("Please choose an existing file."),
67 _("Error"), wxOK| wxICON_ERROR);
68 dlg.ShowModal();
69 return;
70 }
71 }
72
73 // change to the directory where the user went if asked
74 if (style & wxFD_CHANGE_DIR)
75 {
76 // Use chdir to not care about filename encodings
77 wxGtkString folder(g_path_get_dirname(filename));
78 chdir(folder);
79 }
80
81 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
82 event.SetEventObject(dialog);
83 dialog->HandleWindowEvent(event);
84 }
85 }
86
87 //-----------------------------------------------------------------------------
88 // "clicked" for Cancel-button
89 //-----------------------------------------------------------------------------
90
91 extern "C"
92 {
93
94 static void
95 gtk_filedialog_cancel_callback(GtkWidget * WXUNUSED(w), wxFileDialog *dialog)
96 {
97 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
98 event.SetEventObject(dialog);
99 dialog->HandleWindowEvent(event);
100 }
101
102 static void gtk_filedialog_response_callback(GtkWidget *w,
103 gint response,
104 wxFileDialog *dialog)
105 {
106 if (response == GTK_RESPONSE_ACCEPT)
107 gtk_filedialog_ok_callback(w, dialog);
108 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
109 gtk_filedialog_cancel_callback(w, dialog);
110 }
111
112 static void gtk_filedialog_update_preview_callback(GtkFileChooser *chooser,
113 gpointer user_data)
114 {
115 GtkWidget *preview = GTK_WIDGET(user_data);
116
117 wxGtkString filename(gtk_file_chooser_get_preview_filename(chooser));
118
119 if ( !filename )
120 return;
121
122 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size(filename, 128, 128, NULL);
123 gboolean have_preview = pixbuf != NULL;
124
125 gtk_image_set_from_pixbuf(GTK_IMAGE(preview), pixbuf);
126 if ( pixbuf )
127 g_object_unref (pixbuf);
128
129 gtk_file_chooser_set_preview_widget_active(chooser, have_preview);
130 }
131
132 } // extern "C"
133
134 //-----------------------------------------------------------------------------
135 // "size_request" from m_extraControl
136 //-----------------------------------------------------------------------------
137
138 extern "C" {
139 static void extra_widget_size_request(GtkWidget*, GtkRequisition* req, void*)
140 {
141 // allow dialog to be resized smaller horizontally
142 req->width = 1;
143 }
144 }
145
146 static void wxInsertChildInFileDialog(wxWindow* WXUNUSED(parent),
147 wxWindow* WXUNUSED(child))
148 {
149 }
150
151
152 //-----------------------------------------------------------------------------
153 // wxFileDialog
154 //-----------------------------------------------------------------------------
155
156 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase)
157
158 BEGIN_EVENT_TABLE(wxFileDialog,wxFileDialogBase)
159 EVT_BUTTON(wxID_OK, wxFileDialog::OnFakeOk)
160 EVT_SIZE(wxFileDialog::OnSize)
161 END_EVENT_TABLE()
162
163 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
164 const wxString& defaultDir,
165 const wxString& defaultFileName,
166 const wxString& wildCard,
167 long style, const wxPoint& pos,
168 const wxSize& sz,
169 const wxString& name)
170 : wxFileDialogBase()
171 {
172 m_insertCallback = wxInsertChildInFileDialog;
173 parent = GetParentForModalDialog(parent);
174
175 if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName,
176 wildCard, style, pos, sz, name))
177 {
178 return;
179 }
180
181 if (!PreCreation(parent, pos, wxDefaultSize) ||
182 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
183 wxDefaultValidator, wxT("filedialog")))
184 {
185 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
186 return;
187 }
188
189 GtkFileChooserAction gtk_action;
190 GtkWindow* gtk_parent = NULL;
191 if (parent)
192 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
193
194 const gchar* ok_btn_stock;
195 if ( style & wxFD_SAVE )
196 {
197 gtk_action = GTK_FILE_CHOOSER_ACTION_SAVE;
198 ok_btn_stock = GTK_STOCK_SAVE;
199 }
200 else
201 {
202 gtk_action = GTK_FILE_CHOOSER_ACTION_OPEN;
203 ok_btn_stock = GTK_STOCK_OPEN;
204 }
205
206 m_widget = gtk_file_chooser_dialog_new(
207 wxGTK_CONV(m_message),
208 gtk_parent,
209 gtk_action,
210 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
211 ok_btn_stock, GTK_RESPONSE_ACCEPT,
212 NULL);
213
214 m_fc.SetWidget( GTK_FILE_CHOOSER(m_widget) );
215
216 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
217
218 if ( style & wxFD_MULTIPLE )
219 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget), true);
220
221 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically
222 // destroys the dialog when the user press ESC on the dialog: in that case
223 // a second call to ShowModal() would result in a bunch of Gtk-CRITICAL
224 // errors...
225 g_signal_connect (G_OBJECT(m_widget),
226 "delete_event",
227 G_CALLBACK (gtk_widget_hide_on_delete),
228 (gpointer)this);
229
230 // local-only property could be set to false to allow non-local files to be
231 // loaded. In that case get/set_uri(s) should be used instead of
232 // get/set_filename(s) everywhere and the GtkFileChooserDialog should
233 // probably also be created with a backend, e.g "gnome-vfs", "default", ...
234 // (gtk_file_chooser_dialog_new_with_backend). Currently local-only is kept
235 // as the default - true:
236 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
237
238 g_signal_connect (m_widget, "response",
239 G_CALLBACK (gtk_filedialog_response_callback), this);
240
241 SetWildcard(wildCard);
242
243 // if defaultDir is specified it should contain the directory and
244 // defaultFileName should contain the default name of the file, however if
245 // directory is not given, defaultFileName contains both
246 wxFileName fn;
247 if ( defaultDir.empty() )
248 fn.Assign(defaultFileName);
249 else if ( !defaultFileName.empty() )
250 fn.Assign(defaultDir, defaultFileName);
251 else
252 fn.AssignDir(defaultDir);
253
254 // set the initial file name and/or directory
255 fn.MakeAbsolute(); // GTK+ needs absolute path
256 const wxString dir = fn.GetPath();
257 if ( !dir.empty() )
258 {
259 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
260 dir.fn_str());
261 }
262
263 const wxString fname = fn.GetFullName();
264 if ( style & wxFD_SAVE )
265 {
266 if ( !fname.empty() )
267 {
268 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget),
269 fname.fn_str());
270 }
271
272 #if GTK_CHECK_VERSION(2,7,3)
273 if ((style & wxFD_OVERWRITE_PROMPT) && !gtk_check_version(2,7,3))
274 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(m_widget), TRUE);
275 #endif
276 }
277 else // wxFD_OPEN
278 {
279 if ( !fname.empty() )
280 {
281 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget),
282 fn.GetFullPath().fn_str());
283 }
284 }
285
286 if ( style & wxFD_PREVIEW )
287 {
288 GtkWidget *previewImage = gtk_image_new();
289
290 gtk_file_chooser_set_preview_widget(GTK_FILE_CHOOSER(m_widget),
291 previewImage);
292 g_signal_connect(m_widget, "update-preview",
293 G_CALLBACK(gtk_filedialog_update_preview_callback),
294 previewImage);
295 }
296 }
297
298
299 void wxFileDialog::OnFakeOk(wxCommandEvent& WXUNUSED(event))
300 {
301 EndDialog(wxID_OK);
302 }
303
304 int wxFileDialog::ShowModal()
305 {
306 if (CreateExtraControl())
307 {
308 GtkWidget *control = m_extraControl->m_widget;
309
310 wxASSERT(control->parent == NULL);
311
312 gtk_widget_show(control);
313 g_signal_connect_after(control, "size_request",
314 G_CALLBACK(extra_widget_size_request), NULL);
315 gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(m_widget), control);
316 }
317
318 return wxDialog::ShowModal();
319 }
320
321 void wxFileDialog::DoSetSize(int WXUNUSED(x), int WXUNUSED(y),
322 int WXUNUSED(width), int WXUNUSED(height),
323 int WXUNUSED(sizeFlags))
324 {
325 }
326
327 void wxFileDialog::OnSize(wxSizeEvent&)
328 {
329 // avoid calling DoLayout(), which will set the (wrong) size of
330 // m_extraControl, its size is managed by GtkFileChooser
331 }
332
333 wxString wxFileDialog::GetPath() const
334 {
335 return m_fc.GetPath();
336 }
337
338 void wxFileDialog::GetFilenames(wxArrayString& files) const
339 {
340 m_fc.GetFilenames( files );
341 }
342
343 void wxFileDialog::GetPaths(wxArrayString& paths) const
344 {
345 m_fc.GetPaths( paths );
346 }
347
348 void wxFileDialog::SetMessage(const wxString& message)
349 {
350 m_message = message;
351 SetTitle(message);
352 }
353
354 void wxFileDialog::SetPath(const wxString& path)
355 {
356 m_fc.SetPath( path );
357 }
358
359 void wxFileDialog::SetDirectory(const wxString& dir)
360 {
361 m_fc.SetDirectory( dir );
362 }
363
364 wxString wxFileDialog::GetDirectory() const
365 {
366 return m_fc.GetDirectory();
367 }
368
369 void wxFileDialog::SetFilename(const wxString& name)
370 {
371 if (HasFdFlag(wxFD_SAVE))
372 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget), wxGTK_CONV(name));
373 else
374 SetPath(wxFileName(GetDirectory(), name).GetFullPath());
375 }
376
377 wxString wxFileDialog::GetFilename() const
378 {
379 return m_fc.GetFilename();
380 }
381
382 void wxFileDialog::SetWildcard(const wxString& wildCard)
383 {
384 m_fc.SetWildcard( wildCard );
385 }
386
387 void wxFileDialog::SetFilterIndex(int filterIndex)
388 {
389
390 m_fc.SetFilterIndex( filterIndex);
391 }
392
393 int wxFileDialog::GetFilterIndex() const
394 {
395 return m_fc.GetFilterIndex();
396 }
397
398 #endif // wxUSE_FILEDLG