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