]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/filedlg.cpp
4d314208c57be7c5479a1fff2e7ed4265be846a5
[wxWidgets.git] / src / gtk / 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 // "delete_event"
23 //-----------------------------------------------------------------------------
24
25 static
26 bool gtk_filedialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
27 {
28 /*
29 printf( "OnDelete from " );
30 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
31 printf( win->GetClassInfo()->GetClassName() );
32 printf( ".\n" );
33 */
34
35 win->Close();
36
37 return TRUE;
38 }
39
40 //-----------------------------------------------------------------------------
41 // "clicked" for OK-button
42 //-----------------------------------------------------------------------------
43
44 static
45 void gtk_filedialog_ok_callback( GtkWidget *WXUNUSED(widget), gpointer data )
46 {
47 wxFileDialog *dialog = (wxFileDialog*)data;
48 wxCommandEvent event(wxEVT_NULL);
49 int style;
50
51 style = dialog->GetStyle();
52
53 if( (style & wxSAVE ) && ( style&wxOVERWRITE_PROMPT ) )
54 {
55 char *filename = gtk_file_selection_get_filename(
56 GTK_FILE_SELECTION(dialog->m_widget)
57 );
58
59 if(wxFileExists( filename ))
60 {
61 wxString msg;
62 msg.Printf(_("File '%s' already exists, do you really want to "
63 "overwrite it?"), filename);
64
65 if( wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
66 return;
67 }
68 }
69
70 dialog->OnOK( event );
71 }
72
73 //-----------------------------------------------------------------------------
74 // "clicked" for Cancel-button
75 //-----------------------------------------------------------------------------
76
77 static
78 void gtk_filedialog_cancel_callback( GtkWidget *WXUNUSED(widget), gpointer data )
79 {
80 wxFileDialog *dialog = (wxFileDialog*)data;
81 wxCommandEvent event(wxEVT_NULL);
82 dialog->OnCancel( event );
83 }
84
85 //-----------------------------------------------------------------------------
86 // wxFileDialog
87 //-----------------------------------------------------------------------------
88
89 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
90
91 wxFileDialog::wxFileDialog( wxWindow *parent, const wxString& message,
92 const wxString& defaultDir, const wxString& defaultFileName,
93 const wxString& wildCard,
94 long style, const wxPoint& pos )
95 {
96 m_needParent = FALSE;
97
98 PreCreation( parent, -1, pos, wxDefaultSize, style | wxDIALOG_MODAL, "filedialog" );
99 m_message = message;
100 m_path = "";
101 m_fileName = defaultFileName;
102 m_dir = defaultDir;
103 m_wildCard = wildCard;
104 m_dialogStyle = style;
105 m_filterIndex = 1;
106
107 m_widget = gtk_file_selection_new( m_message );
108
109 int x = (gdk_screen_width () - 400) / 2;
110 int y = (gdk_screen_height () - 400) / 2;
111 gtk_widget_set_uposition( m_widget, x, y );
112
113 GtkFileSelection *sel = GTK_FILE_SELECTION(m_widget);
114
115 m_path.Append(m_dir);
116 if(! m_path.IsEmpty() && m_path.Last()!='/') m_path.Append('/');
117 m_path.Append(m_fileName);
118
119 if(m_path.Length()>1) gtk_file_selection_set_filename(sel,m_path);
120
121 gtk_signal_connect( GTK_OBJECT(sel->ok_button), "clicked",
122 GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback), (gpointer*)this );
123
124 gtk_signal_connect( GTK_OBJECT(sel->cancel_button), "clicked",
125 GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback), (gpointer*)this );
126
127 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
128 GTK_SIGNAL_FUNC(gtk_filedialog_delete_callback), (gpointer)this );
129
130 }
131
132 int wxFileDialog::ShowModal(void)
133 {
134 int ret = wxDialog::ShowModal();
135
136 if (ret == wxID_OK)
137 {
138 m_fileName = gtk_file_selection_get_filename( GTK_FILE_SELECTION(m_widget) );
139 m_path = gtk_file_selection_get_filename( GTK_FILE_SELECTION(m_widget) );
140 }
141 return ret;
142 }
143
144
145 wxString wxFileSelector( const char *title,
146 const char *defaultDir, const char *defaultFileName,
147 const char *defaultExtension, const char *filter, int flags,
148 wxWindow *parent, int x, int y )
149 {
150 wxString filter2("");
151 if ( defaultExtension && !filter )
152 filter2 = wxString("*.") + wxString(defaultExtension) ;
153 else if ( filter )
154 filter2 = filter;
155
156 wxString defaultDirString;
157 if (defaultDir)
158 defaultDirString = defaultDir;
159 else
160 defaultDirString = "";
161
162 wxString defaultFilenameString;
163 if (defaultFileName)
164 defaultFilenameString = defaultFileName;
165 else
166 defaultFilenameString = "";
167
168 wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
169
170 if ( fileDialog.ShowModal() == wxID_OK )
171 {
172 return fileDialog.GetPath();
173 }
174 else
175 {
176 return wxEmptyString;
177 }
178 }
179
180 wxString wxLoadFileSelector( const char *what, const char *extension, const char *default_name, wxWindow *parent )
181 {
182 char *ext = (char *)extension;
183
184 char prompt[50];
185 wxString str = _("Load %s file");
186 sprintf(prompt, str, what);
187
188 if (*ext == '.') ext++;
189 char wild[60];
190 sprintf(wild, "*.%s", ext);
191
192 return wxFileSelector (prompt, (const char *) NULL, default_name, ext, wild, 0, parent);
193 }
194
195 wxString wxSaveFileSelector(const char *what, const char *extension, const char *default_name,
196 wxWindow *parent )
197 {
198 char *ext = (char *)extension;
199
200 char prompt[50];
201 wxString str = _("Save %s file");
202 sprintf(prompt, str, what);
203
204 if (*ext == '.') ext++;
205 char wild[60];
206 sprintf(wild, "*.%s", ext);
207
208 return wxFileSelector (prompt, (const char *) NULL, default_name, ext, wild, 0, parent);
209 }
210