]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: filedlg.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
a81258be | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
a3622daa | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "filedlg.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/filedlg.h" | |
15 | #include "wx/utils.h" | |
16 | #include "wx/intl.h" | |
035b704a | 17 | #include "wx/generic/msgdlgg.h" |
c801d85f | 18 | |
83624f79 RR |
19 | #include "gtk/gtk.h" |
20 | ||
acfd422a RR |
21 | //----------------------------------------------------------------------------- |
22 | // idle system | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | extern void wxapp_install_idle_handler(); | |
26 | extern bool g_isIdle; | |
27 | ||
c801d85f | 28 | //----------------------------------------------------------------------------- |
291a8f20 RR |
29 | // "delete_event" |
30 | //----------------------------------------------------------------------------- | |
31 | ||
0e1399b3 | 32 | static |
291a8f20 | 33 | bool gtk_filedialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win ) |
0e1399b3 | 34 | { |
acfd422a RR |
35 | if (g_isIdle) wxapp_install_idle_handler(); |
36 | ||
291a8f20 RR |
37 | /* |
38 | printf( "OnDelete from " ); | |
39 | if (win->GetClassInfo() && win->GetClassInfo()->GetClassName()) | |
40 | printf( win->GetClassInfo()->GetClassName() ); | |
41 | printf( ".\n" ); | |
42 | */ | |
0e1399b3 | 43 | |
291a8f20 RR |
44 | win->Close(); |
45 | ||
46 | return TRUE; | |
47 | } | |
48 | ||
49 | //----------------------------------------------------------------------------- | |
50 | // "clicked" for OK-button | |
c801d85f KB |
51 | //----------------------------------------------------------------------------- |
52 | ||
0e1399b3 | 53 | static |
2748d251 | 54 | void gtk_filedialog_ok_callback( GtkWidget *WXUNUSED(widget), wxFileDialog *dialog ) |
c801d85f | 55 | { |
acfd422a RR |
56 | if (g_isIdle) wxapp_install_idle_handler(); |
57 | ||
2748d251 | 58 | int style = dialog->GetStyle(); |
035b704a | 59 | |
a2053b27 | 60 | GtkFileSelection *filedlg = GTK_FILE_SELECTION(dialog->m_widget); |
bfc6fde4 | 61 | char *filename = gtk_file_selection_get_filename(filedlg); |
0e1399b3 | 62 | |
bfc6fde4 VZ |
63 | if ( (style & wxSAVE) && ( style & wxOVERWRITE_PROMPT ) ) |
64 | { | |
2748d251 | 65 | if (wxFileExists( filename )) |
0e1399b3 VZ |
66 | { |
67 | wxString msg; | |
2748d251 | 68 | msg.Printf( _("File '%s' already exists, do you really want to " |
0e1399b3 VZ |
69 | "overwrite it?"), filename); |
70 | ||
2748d251 | 71 | if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES) |
0e1399b3 VZ |
72 | return; |
73 | } | |
83624f79 | 74 | } |
bfc6fde4 VZ |
75 | else if ( (style & wxOPEN) && ( style & wxFILE_MUST_EXIST) ) |
76 | { | |
77 | if ( !wxFileExists( filename ) ) | |
78 | { | |
79 | wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK); | |
80 | ||
81 | return; | |
82 | } | |
83 | } | |
035b704a | 84 | |
bfc6fde4 | 85 | dialog->SetPath( filename ); |
b7f4714a | 86 | |
bfc6fde4 | 87 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK); |
2748d251 RR |
88 | event.SetEventObject( dialog ); |
89 | dialog->GetEventHandler()->ProcessEvent( event ); | |
ff7b1510 | 90 | } |
c801d85f | 91 | |
291a8f20 RR |
92 | //----------------------------------------------------------------------------- |
93 | // "clicked" for Cancel-button | |
94 | //----------------------------------------------------------------------------- | |
95 | ||
0e1399b3 | 96 | static |
b7f4714a | 97 | void gtk_filedialog_cancel_callback( GtkWidget *WXUNUSED(w), wxFileDialog *dialog ) |
c801d85f | 98 | { |
acfd422a RR |
99 | if (g_isIdle) wxapp_install_idle_handler(); |
100 | ||
bfc6fde4 | 101 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
2748d251 RR |
102 | event.SetEventObject( dialog ); |
103 | dialog->GetEventHandler()->ProcessEvent( event ); | |
ff7b1510 | 104 | } |
c801d85f | 105 | |
291a8f20 RR |
106 | //----------------------------------------------------------------------------- |
107 | // wxFileDialog | |
108 | //----------------------------------------------------------------------------- | |
109 | ||
c801d85f KB |
110 | IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog) |
111 | ||
83624f79 RR |
112 | wxFileDialog::wxFileDialog( wxWindow *parent, const wxString& message, |
113 | const wxString& defaultDir, const wxString& defaultFileName, | |
114 | const wxString& wildCard, | |
115 | long style, const wxPoint& pos ) | |
c801d85f | 116 | { |
83624f79 RR |
117 | m_needParent = FALSE; |
118 | ||
4dcaf11a RR |
119 | if (!PreCreation( parent, pos, wxDefaultSize ) || |
120 | !CreateBase( parent, -1, pos, wxDefaultSize, style | wxDIALOG_MODAL, wxDefaultValidator, _T("filedialog") )) | |
121 | { | |
122 | wxFAIL_MSG( _T("wxXX creation failed") ); | |
123 | return; | |
124 | } | |
125 | ||
83624f79 | 126 | m_message = message; |
ed9b9841 | 127 | m_path = _T(""); |
83624f79 RR |
128 | m_fileName = defaultFileName; |
129 | m_dir = defaultDir; | |
130 | m_wildCard = wildCard; | |
131 | m_dialogStyle = style; | |
132 | m_filterIndex = 1; | |
133 | ||
ed9b9841 | 134 | m_widget = gtk_file_selection_new( m_message.mbc_str() ); |
0e1399b3 | 135 | |
83624f79 RR |
136 | int x = (gdk_screen_width () - 400) / 2; |
137 | int y = (gdk_screen_height () - 400) / 2; | |
138 | gtk_widget_set_uposition( m_widget, x, y ); | |
0e1399b3 | 139 | |
83624f79 | 140 | GtkFileSelection *sel = GTK_FILE_SELECTION(m_widget); |
3502e687 | 141 | gtk_file_selection_hide_fileop_buttons( sel ); // they don't work anyway |
035b704a | 142 | |
83624f79 | 143 | m_path.Append(m_dir); |
ed9b9841 | 144 | if( ! m_path.IsEmpty() && m_path.Last()!=_T('/') ) |
3218cf58 | 145 | m_path.Append('/'); |
83624f79 | 146 | m_path.Append(m_fileName); |
035b704a | 147 | |
ed9b9841 | 148 | if(m_path.Length()>1) gtk_file_selection_set_filename(sel,m_path.mbc_str()); |
a3622daa | 149 | |
83624f79 RR |
150 | gtk_signal_connect( GTK_OBJECT(sel->ok_button), "clicked", |
151 | GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback), (gpointer*)this ); | |
c801d85f | 152 | |
3502e687 | 153 | // strange way to internationalize |
dcf924a3 | 154 | gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->ok_button)->child ), wxConvCurrent->cWX2MB(_("OK")) ); |
3502e687 | 155 | |
83624f79 RR |
156 | gtk_signal_connect( GTK_OBJECT(sel->cancel_button), "clicked", |
157 | GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback), (gpointer*)this ); | |
3502e687 RR |
158 | |
159 | // strange way to internationalize | |
dcf924a3 | 160 | gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->cancel_button)->child ), wxConvCurrent->cWX2MB(_("Cancel")) ); |
3502e687 | 161 | |
0e1399b3 | 162 | gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event", |
291a8f20 | 163 | GTK_SIGNAL_FUNC(gtk_filedialog_delete_callback), (gpointer)this ); |
ff7b1510 | 164 | } |
c801d85f | 165 | |
3218cf58 VZ |
166 | void wxFileDialog::SetPath(const wxString& path) |
167 | { | |
168 | // not only set the full path but also update filename and dir | |
169 | m_path = path; | |
170 | if ( !!path ) | |
171 | { | |
172 | wxString ext; | |
173 | wxSplitPath(path, &m_dir, &m_fileName, &ext); | |
53daeada RR |
174 | if (!ext.IsEmpty()) |
175 | { | |
176 | m_fileName += _T("."); | |
177 | m_fileName += ext; | |
178 | } | |
3218cf58 VZ |
179 | } |
180 | } | |
181 | ||
182 | // ---------------------------------------------------------------------------- | |
183 | // global functions | |
184 | // ---------------------------------------------------------------------------- | |
185 | ||
36453d0f VZ |
186 | wxString |
187 | wxFileSelectorEx(const wxChar *message, | |
188 | const wxChar *default_path, | |
189 | const wxChar *default_filename, | |
190 | int *indexDefaultExtension, | |
191 | const wxChar *wildcard, | |
192 | int flags, | |
193 | wxWindow *parent, | |
194 | int x, int y) | |
195 | { | |
196 | // TODO: implement this somehow | |
197 | return wxFileSelector(message, default_path, default_filename, _T(""), | |
198 | wildcard, flags, parent, x, y); | |
199 | } | |
200 | ||
ed9b9841 OK |
201 | wxString wxFileSelector( const wxChar *title, |
202 | const wxChar *defaultDir, const wxChar *defaultFileName, | |
203 | const wxChar *defaultExtension, const wxChar *filter, int flags, | |
83624f79 | 204 | wxWindow *parent, int x, int y ) |
c801d85f | 205 | { |
3218cf58 | 206 | wxString filter2; |
83624f79 | 207 | if ( defaultExtension && !filter ) |
ed9b9841 | 208 | filter2 = wxString(_T("*.")) + wxString(defaultExtension) ; |
83624f79 RR |
209 | else if ( filter ) |
210 | filter2 = filter; | |
211 | ||
212 | wxString defaultDirString; | |
213 | if (defaultDir) | |
214 | defaultDirString = defaultDir; | |
83624f79 RR |
215 | |
216 | wxString defaultFilenameString; | |
217 | if (defaultFileName) | |
218 | defaultFilenameString = defaultFileName; | |
0e1399b3 | 219 | |
83624f79 RR |
220 | wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) ); |
221 | ||
222 | if ( fileDialog.ShowModal() == wxID_OK ) | |
223 | { | |
7482b220 | 224 | return fileDialog.GetPath(); |
83624f79 RR |
225 | } |
226 | else | |
227 | { | |
7482b220 | 228 | return wxEmptyString; |
83624f79 | 229 | } |
ff7b1510 | 230 | } |
c801d85f | 231 | |
ed9b9841 | 232 | wxString wxLoadFileSelector( const wxChar *what, const wxChar *extension, const wxChar *default_name, wxWindow *parent ) |
c801d85f | 233 | { |
ed9b9841 | 234 | wxChar *ext = (wxChar *)extension; |
a3622daa | 235 | |
ed9b9841 | 236 | wxChar prompt[50]; |
83624f79 | 237 | wxString str = _("Load %s file"); |
ed9b9841 | 238 | wxSprintf(prompt, str, what); |
c801d85f | 239 | |
ed9b9841 OK |
240 | if (*ext == _T('.')) ext++; |
241 | wxChar wild[60]; | |
242 | wxSprintf(wild, _T("*.%s"), ext); | |
c801d85f | 243 | |
ed9b9841 | 244 | return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent); |
ff7b1510 | 245 | } |
c801d85f | 246 | |
ed9b9841 | 247 | wxString wxSaveFileSelector(const wxChar *what, const wxChar *extension, const wxChar *default_name, |
c801d85f KB |
248 | wxWindow *parent ) |
249 | { | |
ed9b9841 | 250 | wxChar *ext = (wxChar *)extension; |
a3622daa | 251 | |
ed9b9841 | 252 | wxChar prompt[50]; |
83624f79 | 253 | wxString str = _("Save %s file"); |
ed9b9841 | 254 | wxSprintf(prompt, str, what); |
c801d85f | 255 | |
ed9b9841 OK |
256 | if (*ext == _T('.')) ext++; |
257 | wxChar wild[60]; | |
258 | wxSprintf(wild, _T("*.%s"), ext); | |
c801d85f | 259 | |
ed9b9841 | 260 | return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent); |
ff7b1510 | 261 | } |
c801d85f | 262 |