Reorganize idle system code.
[wxWidgets.git] / src / gtk / dirdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dirdlg.cpp
3 // Purpose: native implementation of wxDirDialog
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 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 !
19 */
20
21
22
23 #if wxUSE_DIRDLG && defined( __WXGTK24__ )
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 #include "wx/gtk/private.h"
33
34 #include <unistd.h> // chdir
35
36 //-----------------------------------------------------------------------------
37 // "clicked" for OK-button
38 //-----------------------------------------------------------------------------
39
40 extern "C" {
41 static void gtk_dirdialog_ok_callback(GtkWidget *widget, wxDirDialog *dialog)
42 {
43 // change to the directory where the user went if asked
44 if (dialog->HasFlag(wxDD_CHANGE_DIR))
45 {
46 wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget)));
47 chdir(filename);
48 }
49
50 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
51 event.SetEventObject(dialog);
52 dialog->GetEventHandler()->ProcessEvent(event);
53 }
54 }
55
56 //-----------------------------------------------------------------------------
57 // "clicked" for Cancel-button
58 //-----------------------------------------------------------------------------
59
60 extern "C" {
61 static void gtk_dirdialog_cancel_callback(GtkWidget *WXUNUSED(w),
62 wxDirDialog *dialog)
63 {
64 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
65 event.SetEventObject(dialog);
66 dialog->GetEventHandler()->ProcessEvent(event);
67 }
68 }
69
70 extern "C" {
71 static void gtk_dirdialog_response_callback(GtkWidget *w,
72 gint response,
73 wxDirDialog *dialog)
74 {
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);
79 }
80 }
81
82 //-----------------------------------------------------------------------------
83 // wxDirDialog
84 //-----------------------------------------------------------------------------
85
86 IMPLEMENT_DYNAMIC_CLASS(wxDirDialog,wxGenericDirDialog)
87
88 BEGIN_EVENT_TABLE(wxDirDialog,wxGenericDirDialog)
89 EVT_BUTTON(wxID_OK, wxDirDialog::OnFakeOk)
90 END_EVENT_TABLE()
91
92 wxDirDialog::wxDirDialog(wxWindow* parent, const wxString& title,
93 const wxString& defaultPath, long style,
94 const wxPoint& pos, const wxSize& sz,
95 const wxString& name)
96 {
97 if (!gtk_check_version(2,4,0))
98 {
99 m_message = title;
100 m_needParent = false;
101
102 if (!PreCreation(parent, pos, wxDefaultSize) ||
103 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
104 wxDefaultValidator, wxT("dirdialog")))
105 {
106 wxFAIL_MSG( wxT("wxDirDialog creation failed") );
107 return;
108 }
109
110 GtkFileChooserAction gtk_action;
111 GtkWindow* gtk_parent = NULL;
112 if (parent)
113 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
114
115 if (HasFlag(wxDD_DIR_MUST_EXIST))
116 gtk_action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
117 else
118 gtk_action = GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
119
120 m_widget = gtk_file_chooser_dialog_new(
121 wxGTK_CONV(m_message),
122 gtk_parent,
123 gtk_action,
124 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
125 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
126 NULL);
127
128 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
129
130 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically destroys
131 // the dialog when the user press ESC on the dialog: in that case a second call to
132 // ShowModal() would result in a bunch of Gtk-CRITICAL errors...
133 g_signal_connect (G_OBJECT(m_widget),
134 "delete_event",
135 G_CALLBACK (gtk_widget_hide_on_delete),
136 (gpointer)this);
137
138 // local-only property could be set to false to allow non-local files to be loaded.
139 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
140 // and the GtkFileChooserDialog should probably also be created with a backend,
141 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
142 // Currently local-only is kept as the default - true:
143 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
144
145 g_signal_connect (m_widget, "response",
146 G_CALLBACK (gtk_dirdialog_response_callback), this);
147
148 if ( !defaultPath.empty() )
149 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget),
150 wxConvFileName->cWX2MB(defaultPath) );
151 }
152 else
153 wxGenericDirDialog::Create(parent, title, defaultPath, style, pos, sz, name);
154 }
155
156 void wxDirDialog::OnFakeOk( wxCommandEvent &event )
157 {
158 if (!gtk_check_version(2,4,0))
159 EndDialog(wxID_OK);
160 else
161 wxGenericDirDialog::OnOK( event );
162 }
163
164 int wxDirDialog::ShowModal()
165 {
166 if (!gtk_check_version(2,4,0))
167 return wxDialog::ShowModal();
168 else
169 return wxGenericDirDialog::ShowModal();
170 }
171
172 bool wxDirDialog::Show( bool show )
173 {
174 if (!gtk_check_version(2,4,0))
175 return wxDialog::Show( show );
176 else
177 return wxGenericDirDialog::Show( show );
178 }
179
180 void wxDirDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
181 {
182 if (!m_wxwindow)
183 return;
184 else
185 wxGenericDirDialog::DoSetSize( x, y, width, height, sizeFlags );
186 }
187
188 void wxDirDialog::SetPath(const wxString& dir)
189 {
190 if (!gtk_check_version(2,4,0))
191 {
192 if (wxDirExists(dir))
193 {
194 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(dir));
195 }
196 }
197 else
198 wxGenericDirDialog::SetPath( dir );
199 }
200
201 wxString wxDirDialog::GetPath() const
202 {
203 if (!gtk_check_version(2,4,0))
204 {
205 wxGtkString str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget)));
206 return wxConvFileName->cMB2WX(str);
207 }
208
209 return wxGenericDirDialog::GetPath();
210 }
211
212 #endif // wxUSE_DIRDLG