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