]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/filedlg.cpp
A bit of scrolling works under GTK 2.0
[wxWidgets.git] / src / gtk / filedlg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
3f6638b8 2// Name: gtk/filedlg.cpp
c801d85f
KB
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
3f6638b8
VZ
86 // change to the directory where the user went if asked
87 if ( style & wxCHANGE_DIR )
88 {
89 wxString cwd;
90 wxSplitPath(filename, &cwd, NULL, NULL);
91
92 if ( cwd != wxGetWorkingDirectory() )
93 {
94 wxSetWorkingDirectory(cwd);
95 }
96 }
97
bfc6fde4 98 dialog->SetPath( filename );
b7f4714a 99
bfc6fde4 100 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
2748d251
RR
101 event.SetEventObject( dialog );
102 dialog->GetEventHandler()->ProcessEvent( event );
ff7b1510 103}
c801d85f 104
291a8f20
RR
105//-----------------------------------------------------------------------------
106// "clicked" for Cancel-button
107//-----------------------------------------------------------------------------
108
0e1399b3 109static
b7f4714a 110void gtk_filedialog_cancel_callback( GtkWidget *WXUNUSED(w), wxFileDialog *dialog )
c801d85f 111{
acfd422a
RR
112 if (g_isIdle) wxapp_install_idle_handler();
113
bfc6fde4 114 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
2748d251
RR
115 event.SetEventObject( dialog );
116 dialog->GetEventHandler()->ProcessEvent( event );
ff7b1510 117}
c801d85f 118
291a8f20
RR
119//-----------------------------------------------------------------------------
120// wxFileDialog
121//-----------------------------------------------------------------------------
122
c801d85f
KB
123IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
124
83624f79
RR
125wxFileDialog::wxFileDialog( wxWindow *parent, const wxString& message,
126 const wxString& defaultDir, const wxString& defaultFileName,
127 const wxString& wildCard,
128 long style, const wxPoint& pos )
c801d85f 129{
83624f79
RR
130 m_needParent = FALSE;
131
4dcaf11a 132 if (!PreCreation( parent, pos, wxDefaultSize ) ||
223d09f6 133 !CreateBase( parent, -1, pos, wxDefaultSize, style | wxDIALOG_MODAL, wxDefaultValidator, wxT("filedialog") ))
4dcaf11a 134 {
223d09f6 135 wxFAIL_MSG( wxT("wxXX creation failed") );
3f6638b8 136 return;
4dcaf11a 137 }
3f6638b8 138
83624f79 139 m_message = message;
223d09f6 140 m_path = wxT("");
83624f79
RR
141 m_fileName = defaultFileName;
142 m_dir = defaultDir;
143 m_wildCard = wildCard;
144 m_dialogStyle = style;
145 m_filterIndex = 1;
146
ed9b9841 147 m_widget = gtk_file_selection_new( m_message.mbc_str() );
0e1399b3 148
83624f79
RR
149 int x = (gdk_screen_width () - 400) / 2;
150 int y = (gdk_screen_height () - 400) / 2;
151 gtk_widget_set_uposition( m_widget, x, y );
0e1399b3 152
83624f79 153 GtkFileSelection *sel = GTK_FILE_SELECTION(m_widget);
3502e687 154 gtk_file_selection_hide_fileop_buttons( sel ); // they don't work anyway
035b704a 155
83624f79 156 m_path.Append(m_dir);
223d09f6 157 if( ! m_path.IsEmpty() && m_path.Last()!=wxT('/') )
3218cf58 158 m_path.Append('/');
83624f79 159 m_path.Append(m_fileName);
035b704a 160
ed9b9841 161 if(m_path.Length()>1) gtk_file_selection_set_filename(sel,m_path.mbc_str());
a3622daa 162
83624f79
RR
163 gtk_signal_connect( GTK_OBJECT(sel->ok_button), "clicked",
164 GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback), (gpointer*)this );
c801d85f 165
3502e687 166 // strange way to internationalize
dcf924a3 167 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->ok_button)->child ), wxConvCurrent->cWX2MB(_("OK")) );
3502e687 168
83624f79
RR
169 gtk_signal_connect( GTK_OBJECT(sel->cancel_button), "clicked",
170 GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback), (gpointer*)this );
3f6638b8 171
3502e687 172 // strange way to internationalize
dcf924a3 173 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->cancel_button)->child ), wxConvCurrent->cWX2MB(_("Cancel")) );
3f6638b8 174
0e1399b3 175 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
291a8f20 176 GTK_SIGNAL_FUNC(gtk_filedialog_delete_callback), (gpointer)this );
ff7b1510 177}
c801d85f 178
3218cf58
VZ
179void wxFileDialog::SetPath(const wxString& path)
180{
181 // not only set the full path but also update filename and dir
182 m_path = path;
183 if ( !!path )
184 {
185 wxString ext;
186 wxSplitPath(path, &m_dir, &m_fileName, &ext);
3f6638b8
VZ
187 if (!ext.IsEmpty())
188 {
189 m_fileName += wxT(".");
53daeada 190 m_fileName += ext;
3f6638b8 191 }
3218cf58
VZ
192 }
193}
194
195// ----------------------------------------------------------------------------
196// global functions
197// ----------------------------------------------------------------------------
198
36453d0f
VZ
199wxString
200wxFileSelectorEx(const wxChar *message,
201 const wxChar *default_path,
202 const wxChar *default_filename,
203 int *indexDefaultExtension,
204 const wxChar *wildcard,
205 int flags,
206 wxWindow *parent,
207 int x, int y)
208{
209 // TODO: implement this somehow
223d09f6 210 return wxFileSelector(message, default_path, default_filename, wxT(""),
36453d0f
VZ
211 wildcard, flags, parent, x, y);
212}
213
ed9b9841
OK
214wxString wxFileSelector( const wxChar *title,
215 const wxChar *defaultDir, const wxChar *defaultFileName,
216 const wxChar *defaultExtension, const wxChar *filter, int flags,
83624f79 217 wxWindow *parent, int x, int y )
c801d85f 218{
3218cf58 219 wxString filter2;
83624f79 220 if ( defaultExtension && !filter )
223d09f6 221 filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ;
83624f79
RR
222 else if ( filter )
223 filter2 = filter;
224
225 wxString defaultDirString;
226 if (defaultDir)
227 defaultDirString = defaultDir;
83624f79
RR
228
229 wxString defaultFilenameString;
230 if (defaultFileName)
231 defaultFilenameString = defaultFileName;
0e1399b3 232
83624f79
RR
233 wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
234
235 if ( fileDialog.ShowModal() == wxID_OK )
236 {
7482b220 237 return fileDialog.GetPath();
83624f79
RR
238 }
239 else
240 {
7482b220 241 return wxEmptyString;
83624f79 242 }
ff7b1510 243}
c801d85f 244
ed9b9841 245wxString wxLoadFileSelector( const wxChar *what, const wxChar *extension, const wxChar *default_name, wxWindow *parent )
c801d85f 246{
ed9b9841 247 wxChar *ext = (wxChar *)extension;
a3622daa 248
3f6638b8
VZ
249 wxString prompt = wxString::Format(_("Load %s file"), what);
250
251 if (*ext == wxT('.'))
252 ext++;
c801d85f 253
3f6638b8 254 wxString wild = wxString::Format(_T("*.%s"), ext);
c801d85f 255
3f6638b8
VZ
256 return wxFileSelector(prompt, (const wxChar *) NULL, default_name,
257 ext, wild, 0, parent);
ff7b1510 258}
c801d85f 259
ed9b9841 260wxString wxSaveFileSelector(const wxChar *what, const wxChar *extension, const wxChar *default_name,
c801d85f
KB
261 wxWindow *parent )
262{
ed9b9841 263 wxChar *ext = (wxChar *)extension;
a3622daa 264
3f6638b8
VZ
265 wxString prompt = wxString::Format(_("Save %s file"), what);
266
267 if (*ext == wxT('.'))
268 ext++;
c801d85f 269
3f6638b8 270 wxString wild = wxString::Format(_T("*.%s"), ext);
c801d85f 271
3f6638b8
VZ
272 return wxFileSelector(prompt, (const wxChar *) NULL, default_name,
273 ext, wild, 0, parent);
ff7b1510 274}
c801d85f 275