1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/gtk/dirdlg.cpp
3 // Purpose:     native implementation of wxDirDialog
4 // Author:      Robert Roebling, Zbigniew Zagorski, Mart Raudsepp, Francesco Montorsi
5 // Copyright:   (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski, 2005 Mart Raudsepp
6 // Licence:     wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11 
12 
13 
14 /*
15   NOTE: the GtkFileChooser interface can be used both for wxFileDialog and for wxDirDialog.
16         Thus following code is very similar (even if not identic) to src/gtk/filedlg.cpp
17         If you find a problem in this code, remember to check also that file !
18 */
19 
20 
21 
22 #if wxUSE_DIRDLG
23 
24 #include "wx/dirdlg.h"
25 
26 #ifndef WX_PRECOMP
27     #include "wx/intl.h"
28     #include "wx/filedlg.h"
29 #endif
30 
31 #include "wx/gtk/private.h"
32 
33 extern "C" {
gtk_dirdialog_response_callback(GtkWidget * WXUNUSED (w),gint response,wxDirDialog * dialog)34 static void gtk_dirdialog_response_callback(GtkWidget * WXUNUSED(w),
35                                              gint response,
36                                              wxDirDialog *dialog)
37 {
38     if (response == GTK_RESPONSE_ACCEPT)
39         dialog->GTKOnAccept();
40     else // GTK_RESPONSE_CANCEL or GTK_RESPONSE_NONE
41         dialog->GTKOnCancel();
42 }
43 }
44 
45 //-----------------------------------------------------------------------------
46 // wxDirDialog
47 //-----------------------------------------------------------------------------
48 
IMPLEMENT_DYNAMIC_CLASS(wxDirDialog,wxDialog)49 IMPLEMENT_DYNAMIC_CLASS(wxDirDialog, wxDialog)
50 
51 wxDirDialog::wxDirDialog(wxWindow* parent,
52                          const wxString& title,
53                          const wxString& defaultPath,
54                          long style,
55                          const wxPoint& pos,
56                          const wxSize& WXUNUSED(sz),
57                          const wxString& WXUNUSED(name))
58 {
59     Create(parent, title, defaultPath, style, pos);
60 }
61 
Create(wxWindow * parent,const wxString & title,const wxString & defaultPath,long style,const wxPoint & pos,const wxSize & WXUNUSED (sz),const wxString & WXUNUSED (name))62 bool wxDirDialog::Create(wxWindow* parent,
63                          const wxString& title,
64                          const wxString& defaultPath,
65                          long style,
66                          const wxPoint& pos,
67                          const wxSize& WXUNUSED(sz),
68                          const wxString& WXUNUSED(name))
69 {
70     m_message = title;
71 
72     parent = GetParentForModalDialog(parent, style);
73 
74     if (!PreCreation(parent, pos, wxDefaultSize) ||
75         !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
76                 wxDefaultValidator, wxT("dirdialog")))
77     {
78         wxFAIL_MSG( wxT("wxDirDialog creation failed") );
79         return false;
80     }
81 
82     GtkWindow* gtk_parent = NULL;
83     if (parent)
84         gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
85 
86     m_widget = gtk_file_chooser_dialog_new(
87                    wxGTK_CONV(m_message),
88                    gtk_parent,
89                    GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
90                    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
91                    GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
92                    NULL);
93     g_object_ref(m_widget);
94 
95     gtk_dialog_set_default_response(GTK_DIALOG(m_widget), GTK_RESPONSE_ACCEPT);
96 #if GTK_CHECK_VERSION(2,18,0)
97 #ifndef __WXGTK3__
98     if (gtk_check_version(2,18,0) == NULL)
99 #endif
100     {
101         gtk_file_chooser_set_create_folders(
102             GTK_FILE_CHOOSER(m_widget), (style & wxDD_DIR_MUST_EXIST) == 0);
103     }
104 #endif
105 
106     // local-only property could be set to false to allow non-local files to be loaded.
107     // In that case get/set_uri(s) should be used instead of get/set_filename(s) everywhere
108     // and the GtkFileChooserDialog should probably also be created with a backend,
109     // e.g. "gnome-vfs", "default", ... (gtk_file_chooser_dialog_new_with_backend).
110     // Currently local-only is kept as the default - true:
111     // gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(m_widget), true);
112 
113     g_signal_connect (m_widget, "response",
114         G_CALLBACK (gtk_dirdialog_response_callback), this);
115 
116     if ( !defaultPath.empty() )
117         SetPath(defaultPath);
118 
119     return true;
120 }
121 
GTKOnAccept()122 void wxDirDialog::GTKOnAccept()
123 {
124     wxGtkString str(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(m_widget)));
125     m_selectedDirectory = wxString::FromUTF8(str);
126 
127     // change to the directory where the user went if asked
128     if (HasFlag(wxDD_CHANGE_DIR))
129     {
130         wxSetWorkingDirectory(m_selectedDirectory);
131     }
132 
133     EndDialog(wxID_OK);
134 }
135 
GTKOnCancel()136 void wxDirDialog::GTKOnCancel()
137 {
138     EndDialog(wxID_CANCEL);
139 }
140 
DoSetSize(int x,int y,int width,int height,int sizeFlags)141 void wxDirDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
142 {
143     if (!m_wxwindow)
144         return;
145 
146     wxDirDialogBase::DoSetSize( x, y, width, height, sizeFlags );
147 }
148 
SetPath(const wxString & dir)149 void wxDirDialog::SetPath(const wxString& dir)
150 {
151     if (wxDirExists(dir))
152     {
153         gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget),
154                                             wxGTK_CONV_FN(dir));
155     }
156 }
157 
GetPath() const158 wxString wxDirDialog::GetPath() const
159 {
160     return m_selectedDirectory;
161 }
162 
163 #endif // wxUSE_DIRDLG
164