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