]>
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 | ||
ff654490 | 23 | #if wxUSE_DIRDLG |
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 | ||
f04f570f | 34 | #ifdef __UNIX__ |
b50747ea | 35 | #include <unistd.h> // chdir |
f04f570f | 36 | #endif |
b50747ea | 37 | |
b50747ea RR |
38 | //----------------------------------------------------------------------------- |
39 | // "clicked" for OK-button | |
40 | //----------------------------------------------------------------------------- | |
41 | ||
42 | extern "C" { | |
ff3e84ff | 43 | static void gtk_dirdialog_ok_callback(GtkWidget *widget, wxDirDialog *dialog) |
b50747ea | 44 | { |
b50747ea | 45 | // change to the directory where the user went if asked |
141d782d | 46 | if (dialog->HasFlag(wxDD_CHANGE_DIR)) |
e808cf8a PC |
47 | { |
48 | wxGtkString filename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget))); | |
141d782d | 49 | chdir(filename); |
e808cf8a | 50 | } |
b50747ea RR |
51 | |
52 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK); | |
53 | event.SetEventObject(dialog); | |
937013e0 | 54 | dialog->HandleWindowEvent(event); |
b50747ea RR |
55 | } |
56 | } | |
57 | ||
58 | //----------------------------------------------------------------------------- | |
59 | // "clicked" for Cancel-button | |
60 | //----------------------------------------------------------------------------- | |
61 | ||
62 | extern "C" { | |
f1e1e695 | 63 | static void gtk_dirdialog_cancel_callback(GtkWidget *WXUNUSED(w), |
ff3e84ff | 64 | wxDirDialog *dialog) |
b50747ea RR |
65 | { |
66 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); | |
67 | event.SetEventObject(dialog); | |
937013e0 | 68 | dialog->HandleWindowEvent(event); |
b50747ea RR |
69 | } |
70 | } | |
71 | ||
72 | extern "C" { | |
f1e1e695 | 73 | static void gtk_dirdialog_response_callback(GtkWidget *w, |
b50747ea | 74 | gint response, |
ff3e84ff | 75 | wxDirDialog *dialog) |
b50747ea | 76 | { |
b50747ea | 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 | ||
ff654490 | 88 | IMPLEMENT_DYNAMIC_CLASS(wxDirDialog, wxDialog) |
b50747ea | 89 | |
ff654490 | 90 | BEGIN_EVENT_TABLE(wxDirDialog, wxDirDialogBase) |
ff3e84ff | 91 | EVT_BUTTON(wxID_OK, wxDirDialog::OnFakeOk) |
b50747ea RR |
92 | END_EVENT_TABLE() |
93 | ||
ff654490 VZ |
94 | wxDirDialog::wxDirDialog(wxWindow* parent, |
95 | const wxString& title, | |
96 | const wxString& defaultPath, | |
97 | long style, | |
98 | const wxPoint& pos, | |
99 | const wxSize& WXUNUSED(sz), | |
100 | const wxString& WXUNUSED(name)) | |
b50747ea | 101 | { |
ff654490 VZ |
102 | m_message = title; |
103 | ||
cdc48273 | 104 | parent = GetParentForModalDialog(parent, style); |
ff654490 VZ |
105 | |
106 | if (!PreCreation(parent, pos, wxDefaultSize) || | |
107 | !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style, | |
108 | wxDefaultValidator, wxT("dirdialog"))) | |
b50747ea | 109 | { |
ff654490 VZ |
110 | wxFAIL_MSG( wxT("wxDirDialog creation failed") ); |
111 | return; | |
b50747ea | 112 | } |
b50747ea | 113 | |
ff654490 VZ |
114 | GtkWindow* gtk_parent = NULL; |
115 | if (parent) | |
116 | gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) ); | |
117 | ||
118 | m_widget = gtk_file_chooser_dialog_new( | |
119 | wxGTK_CONV(m_message), | |
120 | gtk_parent, | |
121 | GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, | |
122 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, | |
123 | GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, | |
124 | NULL); | |
9ff9d30c | 125 | g_object_ref(m_widget); |
ff654490 VZ |
126 | |
127 | gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT); | |
128 | ||
129 | // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically destroys | |
130 | // the dialog when the user press ESC on the dialog: in that case a second call to | |
131 | // ShowModal() would result in a bunch of Gtk-CRITICAL errors... | |
132 | g_signal_connect (G_OBJECT(m_widget), | |
133 | "delete_event", | |
134 | G_CALLBACK (gtk_widget_hide_on_delete), | |
135 | (gpointer)this); | |
136 | ||
137 | // local-only property could be set to false to allow non-local files to be loaded. | |
138 | // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere | |
139 | // and the GtkFileChooserDialog should probably also be created with a backend, | |
d13b34d3 | 140 | // e.g. "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend). |
ff654490 VZ |
141 | // Currently local-only is kept as the default - true: |
142 | // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true); | |
143 | ||
144 | g_signal_connect (m_widget, "response", | |
145 | G_CALLBACK (gtk_dirdialog_response_callback), this); | |
146 | ||
147 | if ( !defaultPath.empty() ) | |
148 | gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget), | |
149 | defaultPath.fn_str() ); | |
b50747ea RR |
150 | } |
151 | ||
ff654490 | 152 | void wxDirDialog::OnFakeOk(wxCommandEvent& WXUNUSED(event)) |
b50747ea | 153 | { |
ff654490 | 154 | EndDialog(wxID_OK); |
b50747ea RR |
155 | } |
156 | ||
ff3e84ff | 157 | void wxDirDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
b50747ea RR |
158 | { |
159 | if (!m_wxwindow) | |
160 | return; | |
ff654490 VZ |
161 | |
162 | wxDirDialogBase::DoSetSize( x, y, width, height, sizeFlags ); | |
b50747ea RR |
163 | } |
164 | ||
ff3e84ff | 165 | void wxDirDialog::SetPath(const wxString& dir) |
b50747ea | 166 | { |
ff654490 | 167 | if (wxDirExists(dir)) |
b50747ea | 168 | { |
ff654490 VZ |
169 | gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget), |
170 | dir.fn_str()); | |
b50747ea | 171 | } |
b50747ea RR |
172 | } |
173 | ||
ff3e84ff | 174 | wxString wxDirDialog::GetPath() const |
b50747ea | 175 | { |
ff654490 | 176 | wxGtkString str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget))); |
b74d7c85 | 177 | return wxString::FromUTF8(str); |
b50747ea RR |
178 | } |
179 | ||
180 | #endif // wxUSE_DIRDLG |