]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dirdlg.cpp
corrected off by 1 error in cMB2WC() call (thanks valgrind)
[wxWidgets.git] / src / gtk / dirdlg.cpp
CommitLineData
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
40extern "C" {
ff3e84ff 41static 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
60extern "C" {
f1e1e695 61static 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
70extern "C" {
f1e1e695 71static void gtk_dirdialog_response_callback(GtkWidget *w,
b50747ea 72 gint response,
ff3e84ff 73 wxDirDialog *dialog)
b50747ea 74{
b50747ea 75 if (response == GTK_RESPONSE_ACCEPT)
f1e1e695 76 gtk_dirdialog_ok_callback(w, dialog);
a552d120 77 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
f1e1e695 78 gtk_dirdialog_cancel_callback(w, dialog);
b50747ea
RR
79}
80}
81
b50747ea 82//-----------------------------------------------------------------------------
ff3e84ff 83// wxDirDialog
b50747ea
RR
84//-----------------------------------------------------------------------------
85
ff3e84ff 86IMPLEMENT_DYNAMIC_CLASS(wxDirDialog,wxGenericDirDialog)
b50747ea 87
ff3e84ff
VZ
88BEGIN_EVENT_TABLE(wxDirDialog,wxGenericDirDialog)
89 EVT_BUTTON(wxID_OK, wxDirDialog::OnFakeOk)
b50747ea
RR
90END_EVENT_TABLE()
91
ff3e84ff 92wxDirDialog::wxDirDialog(wxWindow* parent, const wxString& title,
b50747ea
RR
93 const wxString& defaultPath, long style,
94 const wxPoint& pos, const wxSize& sz,
95 const wxString& name)
96{
b50747ea
RR
97 if (!gtk_check_version(2,4,0))
98 {
99 m_message = title;
b50747ea 100
2229243b
VZ
101 parent = GetParentForModalDialog(parent);
102
b50747ea
RR
103 if (!PreCreation(parent, pos, wxDefaultSize) ||
104 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
f1e1e695 105 wxDefaultValidator, wxT("dirdialog")))
b50747ea 106 {
ff3e84ff 107 wxFAIL_MSG( wxT("wxDirDialog creation failed") );
b50747ea
RR
108 return;
109 }
110
b50747ea
RR
111 GtkWindow* gtk_parent = NULL;
112 if (parent)
113 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
114
b50747ea
RR
115 m_widget = gtk_file_chooser_dialog_new(
116 wxGTK_CONV(m_message),
117 gtk_parent,
38a14fe9 118 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
b50747ea
RR
119 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
120 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
121 NULL);
122
6905dfe9
MR
123 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
124
a552d120
VZ
125 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically destroys
126 // the dialog when the user press ESC on the dialog: in that case a second call to
127 // ShowModal() would result in a bunch of Gtk-CRITICAL errors...
128 g_signal_connect (G_OBJECT(m_widget),
129 "delete_event",
130 G_CALLBACK (gtk_widget_hide_on_delete),
131 (gpointer)this);
132
b50747ea
RR
133 // local-only property could be set to false to allow non-local files to be loaded.
134 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
135 // and the GtkFileChooserDialog should probably also be created with a backend,
136 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
137 // Currently local-only is kept as the default - true:
138 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
139
3fe39b0c 140 g_signal_connect (m_widget, "response",
f1e1e695 141 G_CALLBACK (gtk_dirdialog_response_callback), this);
b50747ea
RR
142
143 if ( !defaultPath.empty() )
144 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget),
86501081 145 defaultPath.fn_str() );
b50747ea
RR
146 }
147 else
b50747ea
RR
148 wxGenericDirDialog::Create(parent, title, defaultPath, style, pos, sz, name);
149}
150
ff3e84ff 151void wxDirDialog::OnFakeOk( wxCommandEvent &event )
b50747ea 152{
b50747ea 153 if (!gtk_check_version(2,4,0))
86508681 154 EndDialog(wxID_OK);
b50747ea 155 else
b50747ea
RR
156 wxGenericDirDialog::OnOK( event );
157}
158
ff3e84ff 159int wxDirDialog::ShowModal()
b50747ea 160{
b50747ea
RR
161 if (!gtk_check_version(2,4,0))
162 return wxDialog::ShowModal();
163 else
b50747ea
RR
164 return wxGenericDirDialog::ShowModal();
165}
166
ff3e84ff 167bool wxDirDialog::Show( bool show )
b50747ea 168{
b50747ea
RR
169 if (!gtk_check_version(2,4,0))
170 return wxDialog::Show( show );
171 else
b50747ea
RR
172 return wxGenericDirDialog::Show( show );
173}
174
ff3e84ff 175void wxDirDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
b50747ea
RR
176{
177 if (!m_wxwindow)
178 return;
179 else
180 wxGenericDirDialog::DoSetSize( x, y, width, height, sizeFlags );
181}
182
ff3e84ff 183void wxDirDialog::SetPath(const wxString& dir)
b50747ea 184{
b50747ea
RR
185 if (!gtk_check_version(2,4,0))
186 {
187 if (wxDirExists(dir))
188 {
86501081
VS
189 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
190 dir.fn_str());
b50747ea
RR
191 }
192 }
193 else
b50747ea
RR
194 wxGenericDirDialog::SetPath( dir );
195}
196
ff3e84ff 197wxString wxDirDialog::GetPath() const
b50747ea 198{
b50747ea 199 if (!gtk_check_version(2,4,0))
3ab296d9 200 {
e808cf8a 201 wxGtkString str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget)));
86501081 202 return wxString(str, *wxConvFileName);
3ab296d9 203 }
e808cf8a
PC
204
205 return wxGenericDirDialog::GetPath();
b50747ea
RR
206}
207
208#endif // wxUSE_DIRDLG