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