]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dirdlg.cpp
Missing header.
[wxWidgets.git] / src / gtk / dirdlg.cpp
CommitLineData
b50747ea
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/dirdlg.cpp
141d782d 3// Purpose: native implementation of wxDirDialogGTK
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/*
141d782d 16 NOTE: the GtkFileChooser interface can be used both for wxFileDialog and for wxDirDialogGTK.
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
23#if wxUSE_DIRDLG
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
141d782d 32#ifdef __WXGTK24__ // only for GTK+ > 2.4 there is GtkFileChooserDialog
b50747ea
RR
33
34#include <gtk/gtk.h>
35#include "wx/gtk/private.h"
36
37#include <unistd.h> // chdir
38
b50747ea
RR
39
40//-----------------------------------------------------------------------------
41// idle system
42//-----------------------------------------------------------------------------
43
44extern void wxapp_install_idle_handler();
45
46//-----------------------------------------------------------------------------
47// "clicked" for OK-button
48//-----------------------------------------------------------------------------
49
50extern "C" {
f1e1e695 51static void gtk_dirdialog_ok_callback(GtkWidget *widget, wxDirDialogGTK *dialog)
b50747ea 52{
b50747ea
RR
53 gchar* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
54
b50747ea 55 // change to the directory where the user went if asked
141d782d
VZ
56 if (dialog->HasFlag(wxDD_CHANGE_DIR))
57 chdir(filename);
b50747ea
RR
58
59 g_free(filename);
60
61 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
62 event.SetEventObject(dialog);
63 dialog->GetEventHandler()->ProcessEvent(event);
64}
65}
66
67//-----------------------------------------------------------------------------
68// "clicked" for Cancel-button
69//-----------------------------------------------------------------------------
70
71extern "C" {
f1e1e695 72static void gtk_dirdialog_cancel_callback(GtkWidget *WXUNUSED(w),
141d782d 73 wxDirDialogGTK *dialog)
b50747ea
RR
74{
75 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
76 event.SetEventObject(dialog);
77 dialog->GetEventHandler()->ProcessEvent(event);
78}
79}
80
81extern "C" {
f1e1e695 82static void gtk_dirdialog_response_callback(GtkWidget *w,
b50747ea 83 gint response,
141d782d 84 wxDirDialogGTK *dialog)
b50747ea
RR
85{
86 wxapp_install_idle_handler();
87
88 if (response == GTK_RESPONSE_ACCEPT)
f1e1e695 89 gtk_dirdialog_ok_callback(w, dialog);
b50747ea 90 else if (response == GTK_RESPONSE_CANCEL)
f1e1e695 91 gtk_dirdialog_cancel_callback(w, dialog);
b50747ea
RR
92 else // "delete"
93 {
f1e1e695 94 gtk_dirdialog_cancel_callback(w, dialog);
b50747ea
RR
95 dialog->m_destroyed_by_delete = true;
96 }
97}
98}
99
100#endif // __WXGTK24__
101
102//-----------------------------------------------------------------------------
141d782d 103// wxDirDialogGTK
b50747ea
RR
104//-----------------------------------------------------------------------------
105
141d782d 106IMPLEMENT_DYNAMIC_CLASS(wxDirDialogGTK,wxGenericDirDialog)
b50747ea 107
141d782d
VZ
108BEGIN_EVENT_TABLE(wxDirDialogGTK,wxGenericDirDialog)
109 EVT_BUTTON(wxID_OK, wxDirDialogGTK::OnFakeOk)
b50747ea
RR
110END_EVENT_TABLE()
111
141d782d 112wxDirDialogGTK::wxDirDialogGTK(wxWindow* parent, const wxString& title,
b50747ea
RR
113 const wxString& defaultPath, long style,
114 const wxPoint& pos, const wxSize& sz,
115 const wxString& name)
116{
117#ifdef __WXGTK24__
118 if (!gtk_check_version(2,4,0))
119 {
120 m_message = title;
121 m_needParent = false;
122 m_destroyed_by_delete = false;
123
124 if (!PreCreation(parent, pos, wxDefaultSize) ||
125 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
f1e1e695 126 wxDefaultValidator, wxT("dirdialog")))
b50747ea 127 {
141d782d 128 wxFAIL_MSG( wxT("wxDirDialogGTK creation failed") );
b50747ea
RR
129 return;
130 }
131
132 GtkFileChooserAction gtk_action;
133 GtkWindow* gtk_parent = NULL;
134 if (parent)
135 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
136
137 gtk_action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
138 if (style & wxDD_NEW_DIR_BUTTON)
139 gtk_action = GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
140
141 m_widget = gtk_file_chooser_dialog_new(
142 wxGTK_CONV(m_message),
143 gtk_parent,
144 gtk_action,
145 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
146 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
147 NULL);
148
6905dfe9
MR
149 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
150
b50747ea
RR
151 // local-only property could be set to false to allow non-local files to be loaded.
152 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
153 // and the GtkFileChooserDialog should probably also be created with a backend,
154 // e.g "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
155 // Currently local-only is kept as the default - true:
156 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
157
3fe39b0c 158 g_signal_connect (m_widget, "response",
f1e1e695 159 G_CALLBACK (gtk_dirdialog_response_callback), this);
b50747ea
RR
160
161 if ( !defaultPath.empty() )
162 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget),
163 wxConvFileName->cWX2MB(defaultPath) );
164 }
165 else
166#endif
167 wxGenericDirDialog::Create(parent, title, defaultPath, style, pos, sz, name);
168}
169
141d782d 170wxDirDialogGTK::~wxDirDialogGTK()
b50747ea
RR
171{
172#ifdef __WXGTK24__
173 if (!gtk_check_version(2,4,0))
174 {
175 if (m_destroyed_by_delete)
176 m_widget = NULL;
177 }
178#endif
179}
180
141d782d 181void wxDirDialogGTK::OnFakeOk( wxCommandEvent &event )
b50747ea
RR
182{
183#ifdef __WXGTK24__
184 if (!gtk_check_version(2,4,0))
185 wxDialog::OnOK( event );
186 else
187#endif
188 wxGenericDirDialog::OnOK( event );
189}
190
141d782d 191int wxDirDialogGTK::ShowModal()
b50747ea
RR
192{
193#ifdef __WXGTK24__
194 if (!gtk_check_version(2,4,0))
195 return wxDialog::ShowModal();
196 else
197#endif
198 return wxGenericDirDialog::ShowModal();
199}
200
141d782d 201bool wxDirDialogGTK::Show( bool show )
b50747ea
RR
202{
203#ifdef __WXGTK24__
204 if (!gtk_check_version(2,4,0))
205 return wxDialog::Show( show );
206 else
207#endif
208 return wxGenericDirDialog::Show( show );
209}
210
141d782d 211void wxDirDialogGTK::DoSetSize(int x, int y, int width, int height, int sizeFlags )
b50747ea
RR
212{
213 if (!m_wxwindow)
214 return;
215 else
216 wxGenericDirDialog::DoSetSize( x, y, width, height, sizeFlags );
217}
218
141d782d 219void wxDirDialogGTK::SetPath(const wxString& dir)
b50747ea
RR
220{
221#ifdef __WXGTK24__
222 if (!gtk_check_version(2,4,0))
223 {
224 if (wxDirExists(dir))
225 {
226 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget), wxConvFileName->cWX2MB(dir));
227 }
228 }
229 else
230#endif
231 wxGenericDirDialog::SetPath( dir );
232}
233
141d782d 234wxString wxDirDialogGTK::GetPath() const
b50747ea
RR
235{
236#ifdef __WXGTK24__
237 if (!gtk_check_version(2,4,0))
238 return wxConvFileName->cMB2WX( gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(m_widget) ) );
239 else
240#endif
241 return wxGenericDirDialog::GetPath();
242}
243
244#endif // wxUSE_DIRDLG