1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/filedlg.cpp
3 // Purpose: native implementation of wxFileDialog
4 // Author: Robert Roebling, Zbigniew Zagorski
6 // Copyright: (c) 1998 Robert Roebling, 2004 Zbigniew Zagorski
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "filedlg.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
17 #if wxUSE_FILEDLG && defined(__WXGTK24__)
19 #include "wx/filedlg.h"
22 #include "wx/filename.h"
23 #include "wx/msgdlg.h"
26 #include "wx/gtk/private.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern void wxapp_install_idle_handler();
35 //-----------------------------------------------------------------------------
36 // "clicked" for OK-button
37 //-----------------------------------------------------------------------------
39 static void gtk_filedialog_ok_callback(GtkWidget
*widget
, wxFileDialog
*dialog
)
41 int style
= dialog
->GetStyle();
42 gchar
* text
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget
));
43 wxString
filename(wxGTK_CONV_BACK(text
));
45 if ((style
& wxSAVE
) && (style
& wxOVERWRITE_PROMPT
))
47 if (wxFileExists(filename
))
51 _("File '%s' already exists, do you really want to overwrite it?"),
54 wxMessageDialog
dlg(dialog
, msg
, _("Confirm"),
55 wxYES_NO
| wxICON_QUESTION
);
56 if (dlg
.ShowModal() != wxID_YES
)
60 else if ((style
& wxOPEN
) && ( style
& wxFILE_MUST_EXIST
))
62 if (!wxFileExists( filename
))
64 wxMessageDialog
dlg(dialog
,
65 _("Please choose an existing file."),
66 _("Error"), wxOK
| wxICON_ERROR
);
73 // change to the directory where the user went if asked
74 if (style
& wxCHANGE_DIR
)
77 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
79 if (cwd
!= wxGetCwd())
81 wxSetWorkingDirectory(cwd
);
85 dialog
->SetPath(filename
);
86 dialog
->UpdateFromDialog();
88 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
89 event
.SetEventObject(dialog
);
90 dialog
->GetEventHandler()->ProcessEvent(event
);
93 //-----------------------------------------------------------------------------
94 // "clicked" for Cancel-button
95 //-----------------------------------------------------------------------------
97 static void gtk_filedialog_cancel_callback(GtkWidget
*WXUNUSED(w
),
100 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
101 event
.SetEventObject(dialog
);
102 dialog
->GetEventHandler()->ProcessEvent(event
);
105 static void gtk_filedialog_response_callback(GtkWidget
*w
,
107 wxFileDialog
*dialog
)
109 wxapp_install_idle_handler();
111 if (response
== GTK_RESPONSE_CANCEL
)
112 gtk_filedialog_cancel_callback(w
, dialog
);
114 gtk_filedialog_ok_callback(w
, dialog
);
117 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
121 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxFileDialogBase
)
123 wxFileDialog
::wxFileDialog(wxWindow
*parent
, const wxString
& message
,
124 const wxString
& defaultDir
,
125 const wxString
& defaultFileName
,
126 const wxString
& wildCard
,
127 long style
, const wxPoint
& pos
)
128 : wxFileDialogBase(parent
, message
, defaultDir
, defaultFileName
,
129 wildCard
, style
, pos
)
131 m_needParent
= FALSE
;
133 if (!PreCreation(parent
, pos
, wxDefaultSize
) ||
134 !CreateBase(parent
, wxID_ANY
, pos
, wxDefaultSize
, style
,
135 wxDefaultValidator
, wxT("filedialog")))
137 wxFAIL_MSG( wxT("wxFileDialog creation failed") );
141 bool multiple
= (style
& wxMULTIPLE
) == wxMULTIPLE
;
142 GtkFileChooserAction gtk_action
;
143 GtkWindow
* gtk_parent
= NULL
;
145 gtk_parent
= GTK_WINDOW(parent
->m_widget
);
148 if ((style
& wxSAVE
) == wxSAVE
)
150 gtk_action
= GTK_FILE_CHOOSER_ACTION_SAVE
;
151 ok_btn_stock
= GTK_STOCK_SAVE
;
155 gtk_action
= GTK_FILE_CHOOSER_ACTION_OPEN
;
156 ok_btn_stock
= GTK_STOCK_OPEN
;
158 m_widget
= gtk_file_chooser_dialog_new(
159 wxGTK_CONV(m_message
),
162 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
163 ok_btn_stock
, GTK_RESPONSE_ACCEPT
,
166 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(m_widget
), multiple
);
168 gtk_signal_connect(GTK_OBJECT(m_widget
),
170 GTK_SIGNAL_FUNC(gtk_filedialog_response_callback
),
174 if (!m_path
.empty() && m_path
.Last() != wxT('/'))
176 m_path
+= m_fileName
;
179 SetWildcard(wildCard
);
183 wxFileDialog
::~wxFileDialog()
188 void wxFileDialog
::GetFilenames(wxArrayString
& files
) const
191 for (size_t n
= 0; n
< files
.GetCount(); n
++ )
194 wxSplitPath(files
[n
], NULL
, &name
, &ext
);
204 void wxFileDialog
::GetPaths(wxArrayString
& paths
) const
207 if (GetWindowStyle() & wxMULTIPLE
)
210 gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(m_widget
));
211 GSList
*gpaths
= gpathsi
;
214 wxString file
= wxGTK_CONV_BACK((gchar
*) gpathsi
->data
);
216 g_free(gpathsi
->data
);
217 gpathsi
= gpathsi
->next
;
220 g_slist_free(gpaths
);
224 paths
.Add(m_fileName
);
228 void wxFileDialog
::SetMessage(const wxString
& message
)
234 void wxFileDialog
::SetPath(const wxString
& path
)
236 if (path
.empty()) return;
239 m_path
= fn
.GetFullPath();
240 m_dir
= fn
.GetPath();
241 m_fileName
= fn
.GetFullName();
243 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
246 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
),
249 // pre-fill the filename, too:
250 if (GetWindowStyle() & wxSAVE
) // Why only then??
252 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
253 wxGTK_CONV(m_fileName
));
257 void wxFileDialog
::SetDirectory(const wxString
& dir
)
259 if (wxDirExists(dir
))
262 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(m_widget
),
264 wxFileName
fn(m_dir
,m_fileName
);
265 m_path
= fn
.GetFullPath();
269 void wxFileDialog
::SetFilename(const wxString
& name
)
272 wxFileName
fn(m_dir
,m_fileName
);
273 m_path
= fn
.GetFullPath();
275 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(m_widget
),
278 // pre-fill the filename, too:
279 if (GetWindowStyle() & wxSAVE
) // Why only then??
281 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(m_widget
),
282 wxGTK_CONV(m_fileName
));
286 void wxFileDialog
::SetWildcard(const wxString
& wildCard
)
288 m_wildCard
= wildCard
;
290 GtkFileChooser
* chooser
= GTK_FILE_CHOOSER(m_widget
);
292 // empty current filter list:
293 GSList
* ifilters
= gtk_file_chooser_list_filters(chooser
);
294 GSList
* filters
= ifilters
;
297 gtk_file_chooser_remove_filter(chooser
,GTK_FILE_FILTER(ifilters
->data
));
298 ifilters
= ifilters
->next
;
300 g_slist_free(filters
);
303 wxArrayString wildDescriptions
, wildFilters
;
304 if (!wxParseCommonDialogsFilter(m_wildCard
, wildDescriptions
, wildFilters
))
306 wxFAIL_MSG( wxT("Wrong file type description") );
310 // add parsed to GtkChooser
311 for (size_t n
= 0; n
< wildFilters
.GetCount(); n
++)
313 GtkFileFilter
* filter
= gtk_file_filter_new();
314 gtk_file_filter_set_name(filter
,wxGTK_CONV(wildDescriptions
[n
]));
315 wxString after
= wildFilters
[n
];
318 wxString ext
= after
.BeforeFirst(wxT(';'));
319 gtk_file_filter_add_pattern(filter
,wxGTK_CONV(ext
));
320 if (after
.Find(wxT(';')) == wxNOT_FOUND
)
322 after
= after
.AfterLast(wxT(';'));
324 while (!after
.empty());
326 gtk_file_chooser_add_filter(chooser
, filter
);
331 void wxFileDialog
::SetFilterIndex(int filterIndex
)
333 m_filterIndex
= filterIndex
;
335 GtkFileChooser
*chooser
= GTK_FILE_CHOOSER(m_widget
);
336 GSList
*fnode
= gtk_file_chooser_list_filters(chooser
);
337 GSList
*filters
= fnode
;
341 if (i
== filterIndex
)
343 gtk_file_chooser_set_filter(chooser
, GTK_FILE_FILTER(fnode
->data
));
350 g_slist_free(filters
);
353 void wxFileDialog
::UpdateFromDialog()
355 // update filterIndex
356 GSList
*fnode
= gtk_file_chooser_list_filters(GTK_FILE_CHOOSER(m_widget
));
357 GSList
*filters
= fnode
;
358 GtkFileFilter
*current
=
359 gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(m_widget
));
365 if (fnode
->data
== (gpointer
)current
)
373 g_slist_free(filters
);
376 #endif // wxUSE_FILEDLG && defined(__WXGTK24__)