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