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