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