1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dirdlg.cpp
3 // Purpose: native implementation of wxDirDialog
4 // Author: Robert Roebling, Zbigniew Zagorski, Mart Raudsepp, Francesco Montorsi
6 // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
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 !
23 #if wxUSE_DIRDLG && defined( __WXGTK24__ )
25 #include "wx/dirdlg.h"
29 #include "wx/filedlg.h"
33 #include "wx/gtk/private.h"
35 #include <unistd.h> // chdir
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 extern void wxapp_install_idle_handler();
44 //-----------------------------------------------------------------------------
45 // "clicked" for OK-button
46 //-----------------------------------------------------------------------------
49 static void gtk_dirdialog_ok_callback(GtkWidget
*widget
, wxDirDialog
*dialog
)
51 gchar
* filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
53 // change to the directory where the user went if asked
54 if (dialog
->HasFlag(wxDD_CHANGE_DIR
))
59 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
60 event
.SetEventObject(dialog
);
61 dialog
->GetEventHandler()->ProcessEvent(event
);
65 //-----------------------------------------------------------------------------
66 // "clicked" for Cancel-button
67 //-----------------------------------------------------------------------------
70 static void gtk_dirdialog_cancel_callback(GtkWidget
*WXUNUSED(w
),
73 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
74 event
.SetEventObject(dialog
);
75 dialog
->GetEventHandler()->ProcessEvent(event
);
80 static void gtk_dirdialog_response_callback(GtkWidget
*w
,
84 wxapp_install_idle_handler();
86 if (response
== GTK_RESPONSE_ACCEPT
)
87 gtk_dirdialog_ok_callback(w
, dialog
);
88 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
89 gtk_dirdialog_cancel_callback(w
, dialog
);
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
97 IMPLEMENT_DYNAMIC_CLASS(wxDirDialog
,wxGenericDirDialog
)
99 BEGIN_EVENT_TABLE(wxDirDialog
,wxGenericDirDialog
)
100 EVT_BUTTON(wxID_OK
, wxDirDialog::OnFakeOk
)
103 wxDirDialog::wxDirDialog(wxWindow
* parent
, const wxString
& title
,
104 const wxString
& defaultPath
, long style
,
105 const wxPoint
& pos
, const wxSize
& sz
,
106 const wxString
& name
)
108 if (!gtk_check_version(2,4,0))
111 m_needParent
= false;
113 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
114 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
115 wxDefaultValidator
, wxT("dirdialog")))
117 wxFAIL_MSG( wxT("wxDirDialog creation failed") );
121 GtkFileChooserAction gtk_action
;
122 GtkWindow
* gtk_parent
= NULL
;
124 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
126 if (HasFlag(wxDD_DIR_MUST_EXIST
))
127 gtk_action
= GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
;
129 gtk_action
= GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
;
131 m_widget
= gtk_file_chooser_dialog_new(
132 wxGTK_CONV(m_message
),
135 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
136 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
139 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
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
),
146 G_CALLBACK (gtk_widget_hide_on_delete
),
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);
156 g_signal_connect (m_widget
, "response",
157 G_CALLBACK (gtk_dirdialog_response_callback
), this);
159 if ( !defaultPath
.empty() )
160 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget
),
161 wxConvFileName
->cWX2MB(defaultPath
) );
164 wxGenericDirDialog::Create(parent
, title
, defaultPath
, style
, pos
, sz
, name
);
167 void wxDirDialog::OnFakeOk( wxCommandEvent
&event
)
169 if (!gtk_check_version(2,4,0))
172 wxGenericDirDialog::OnOK( event
);
175 int wxDirDialog::ShowModal()
177 if (!gtk_check_version(2,4,0))
178 return wxDialog::ShowModal();
180 return wxGenericDirDialog::ShowModal();
183 bool wxDirDialog::Show( bool show
)
185 if (!gtk_check_version(2,4,0))
186 return wxDialog::Show( show
);
188 return wxGenericDirDialog::Show( show
);
191 void wxDirDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
196 wxGenericDirDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
199 void wxDirDialog::SetPath(const wxString
& dir
)
201 if (!gtk_check_version(2,4,0))
203 if (wxDirExists(dir
))
205 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
209 wxGenericDirDialog::SetPath( dir
);
212 wxString
wxDirDialog::GetPath() const
214 if (!gtk_check_version(2,4,0))
216 gchar
*str
= gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(m_widget
) );
217 wxString ret
= wxConvFileName
->cMB2WX(str
);
218 if (str
) g_free(str
);
223 return wxGenericDirDialog::GetPath();
226 #endif // wxUSE_DIRDLG