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