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