]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/dirdlg.cpp
simplify code to return from the end of the function
[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
ff654490 23#if wxUSE_DIRDLG
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
f04f570f 34#ifdef __UNIX__
b50747ea 35#include <unistd.h> // chdir
f04f570f 36#endif
b50747ea 37
b50747ea 38extern "C" {
5a7c1881 39static void gtk_dirdialog_response_callback(GtkWidget * WXUNUSED(w),
b50747ea 40 gint response,
ff3e84ff 41 wxDirDialog *dialog)
b50747ea 42{
b50747ea 43 if (response == GTK_RESPONSE_ACCEPT)
5a7c1881 44 dialog->GTKOnAccept();
a552d120 45 else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
5a7c1881 46 dialog->GTKOnCancel();
b50747ea
RR
47}
48}
49
b50747ea 50//-----------------------------------------------------------------------------
ff3e84ff 51// wxDirDialog
b50747ea
RR
52//-----------------------------------------------------------------------------
53
ff654490 54IMPLEMENT_DYNAMIC_CLASS(wxDirDialog, wxDialog)
b50747ea 55
ff654490
VZ
56wxDirDialog::wxDirDialog(wxWindow* parent,
57 const wxString& title,
58 const wxString& defaultPath,
59 long style,
60 const wxPoint& pos,
61 const wxSize& WXUNUSED(sz),
62 const wxString& WXUNUSED(name))
e3f54c8f
VZ
63{
64 Create(parent, title, defaultPath, style, pos);
65}
66
67bool wxDirDialog::Create(wxWindow* parent,
68 const wxString& title,
69 const wxString& defaultPath,
70 long style,
71 const wxPoint& pos,
72 const wxSize& WXUNUSED(sz),
73 const wxString& WXUNUSED(name))
b50747ea 74{
ff654490
VZ
75 m_message = title;
76
cdc48273 77 parent = GetParentForModalDialog(parent, style);
ff654490
VZ
78
79 if (!PreCreation(parent, pos, wxDefaultSize) ||
80 !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
81 wxDefaultValidator, wxT("dirdialog")))
b50747ea 82 {
ff654490 83 wxFAIL_MSG( wxT("wxDirDialog creation failed") );
e3f54c8f 84 return false;
b50747ea 85 }
b50747ea 86
ff654490
VZ
87 GtkWindow* gtk_parent = NULL;
88 if (parent)
89 gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
90
91 m_widget = gtk_file_chooser_dialog_new(
92 wxGTK_CONV(m_message),
93 gtk_parent,
94 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
95 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
96 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
97 NULL);
9ff9d30c 98 g_object_ref(m_widget);
ff654490
VZ
99
100 gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
101
102 // gtk_widget_hide_on_delete is used here to avoid that Gtk automatically destroys
103 // the dialog when the user press ESC on the dialog: in that case a second call to
104 // ShowModal() would result in a bunch of Gtk-CRITICAL errors...
84f623f8 105 g_signal_connect (m_widget,
ff654490
VZ
106 "delete_event",
107 G_CALLBACK (gtk_widget_hide_on_delete),
108 (gpointer)this);
109
110 // local-only property could be set to false to allow non-local files to be loaded.
111 // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
112 // and the GtkFileChooserDialog should probably also be created with a backend,
d13b34d3 113 // e.g. "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
ff654490
VZ
114 // Currently local-only is kept as the default - true:
115 // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
116
117 g_signal_connect (m_widget, "response",
118 G_CALLBACK (gtk_dirdialog_response_callback), this);
119
120 if ( !defaultPath.empty() )
5a7c1881 121 SetPath(defaultPath);
c282e47d 122
e3f54c8f 123 return true;
b50747ea
RR
124}
125
5a7c1881 126void wxDirDialog::GTKOnAccept()
b50747ea 127{
5a7c1881
VZ
128 wxGtkString str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget)));
129 m_selectedDirectory = wxString::FromUTF8(str);
130
131 // change to the directory where the user went if asked
132 if (HasFlag(wxDD_CHANGE_DIR))
133 {
9823d1c5 134 chdir(m_selectedDirectory.fn_str());
5a7c1881
VZ
135 }
136
ff654490 137 EndDialog(wxID_OK);
b50747ea
RR
138}
139
5a7c1881
VZ
140void wxDirDialog::GTKOnCancel()
141{
142 EndDialog(wxID_CANCEL);
143}
144
ff3e84ff 145void wxDirDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
b50747ea
RR
146{
147 if (!m_wxwindow)
148 return;
ff654490
VZ
149
150 wxDirDialogBase::DoSetSize( x, y, width, height, sizeFlags );
b50747ea
RR
151}
152
ff3e84ff 153void wxDirDialog::SetPath(const wxString& dir)
b50747ea 154{
ff654490 155 if (wxDirExists(dir))
b50747ea 156 {
ff654490 157 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
c282e47d 158 wxGTK_CONV_FN(dir));
b50747ea 159 }
b50747ea
RR
160}
161
ff3e84ff 162wxString wxDirDialog::GetPath() const
b50747ea 163{
5a7c1881 164 return m_selectedDirectory;
b50747ea
RR
165}
166
167#endif // wxUSE_DIRDLG