1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/dirdlg.cpp
3 // Purpose: native implementation of wxDirDialogGTK
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 wxDirDialogGTK.
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 !
25 #include "wx/dirdlg.h"
29 #include "wx/filedlg.h"
32 #ifdef __WXGTK24__ // only for GTK+ > 2.4 there is GtkFileChooserDialog
35 #include "wx/gtk/private.h"
37 #include <unistd.h> // chdir
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 extern void wxapp_install_idle_handler();
46 //-----------------------------------------------------------------------------
47 // "clicked" for OK-button
48 //-----------------------------------------------------------------------------
51 static void gtk_dirdialog_ok_callback(GtkWidget
*widget
, wxDirDialogGTK
*dialog
)
53 gchar
* filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
55 // change to the directory where the user went if asked
56 if (dialog
->HasFlag(wxDD_CHANGE_DIR
))
61 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
62 event
.SetEventObject(dialog
);
63 dialog
->GetEventHandler()->ProcessEvent(event
);
67 //-----------------------------------------------------------------------------
68 // "clicked" for Cancel-button
69 //-----------------------------------------------------------------------------
72 static void gtk_dirdialog_cancel_callback(GtkWidget
*WXUNUSED(w
),
73 wxDirDialogGTK
*dialog
)
75 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
76 event
.SetEventObject(dialog
);
77 dialog
->GetEventHandler()->ProcessEvent(event
);
82 static void gtk_dirdialog_response_callback(GtkWidget
*w
,
84 wxDirDialogGTK
*dialog
)
86 wxapp_install_idle_handler();
88 if (response
== GTK_RESPONSE_ACCEPT
)
89 gtk_dirdialog_ok_callback(w
, dialog
);
90 else if (response
== GTK_RESPONSE_CANCEL
)
91 gtk_dirdialog_cancel_callback(w
, dialog
);
94 gtk_dirdialog_cancel_callback(w
, dialog
);
95 dialog
->m_destroyed_by_delete
= true;
100 #endif // __WXGTK24__
102 //-----------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------
106 IMPLEMENT_DYNAMIC_CLASS(wxDirDialogGTK
,wxGenericDirDialog
)
108 BEGIN_EVENT_TABLE(wxDirDialogGTK
,wxGenericDirDialog
)
109 EVT_BUTTON(wxID_OK
, wxDirDialogGTK::OnFakeOk
)
112 wxDirDialogGTK::wxDirDialogGTK(wxWindow
* parent
, const wxString
& title
,
113 const wxString
& defaultPath
, long style
,
114 const wxPoint
& pos
, const wxSize
& sz
,
115 const wxString
& name
)
118 if (!gtk_check_version(2,4,0))
121 m_needParent
= false;
122 m_destroyed_by_delete
= false;
124 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
125 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
126 wxDefaultValidator
, wxT("dirdialog")))
128 wxFAIL_MSG( wxT("wxDirDialogGTK creation failed") );
132 GtkFileChooserAction gtk_action
;
133 GtkWindow
* gtk_parent
= NULL
;
135 gtk_parent
= GTK_WINDOW( gtk_widget_get_toplevel(parent
->m_widget
) );
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
;
141 m_widget
= gtk_file_chooser_dialog_new(
142 wxGTK_CONV(m_message
),
145 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
146 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
149 gtk_dialog_set_default_response(GTK_DIALOG(m_widget
), GTK_RESPONSE_ACCEPT
);
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);
158 g_signal_connect (m_widget
, "response",
159 G_CALLBACK (gtk_dirdialog_response_callback
), this);
161 if ( !defaultPath
.empty() )
162 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(m_widget
),
163 wxConvFileName
->cWX2MB(defaultPath
) );
167 wxGenericDirDialog::Create(parent
, title
, defaultPath
, style
, pos
, sz
, name
);
170 wxDirDialogGTK::~wxDirDialogGTK()
173 if (!gtk_check_version(2,4,0))
175 if (m_destroyed_by_delete
)
181 void wxDirDialogGTK::OnFakeOk( wxCommandEvent
&event
)
184 if (!gtk_check_version(2,4,0))
185 wxDialog::OnOK( event
);
188 wxGenericDirDialog::OnOK( event
);
191 int wxDirDialogGTK::ShowModal()
194 if (!gtk_check_version(2,4,0))
195 return wxDialog::ShowModal();
198 return wxGenericDirDialog::ShowModal();
201 bool wxDirDialogGTK::Show( bool show
)
204 if (!gtk_check_version(2,4,0))
205 return wxDialog::Show( show
);
208 return wxGenericDirDialog::Show( show
);
211 void wxDirDialogGTK::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
216 wxGenericDirDialog::DoSetSize( x
, y
, width
, height
, sizeFlags
);
219 void wxDirDialogGTK::SetPath(const wxString
& dir
)
222 if (!gtk_check_version(2,4,0))
224 if (wxDirExists(dir
))
226 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
), wxConvFileName
->cWX2MB(dir
));
231 wxGenericDirDialog::SetPath( dir
);
234 wxString
wxDirDialogGTK::GetPath() const
237 if (!gtk_check_version(2,4,0))
238 return wxConvFileName
->cMB2WX( gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(m_widget
) ) );
241 return wxGenericDirDialog::GetPath();
244 #endif // wxUSE_DIRDLG