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