]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/filedlg.cpp
wxSpinButton works ok with negative values
[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), wxFileDialog *dialog )
46 {
47 int style = dialog->GetStyle();
48
49 GtkFileSelection *filedlg = GTK_FILE_SELECTION(dialog->m_widget);
50 char *filename = gtk_file_selection_get_filename(filedlg);
51
52 if ( (style & wxSAVE) && ( style & wxOVERWRITE_PROMPT ) )
53 {
54 if (wxFileExists( filename ))
55 {
56 wxString msg;
57 msg.Printf( _("File '%s' already exists, do you really want to "
58 "overwrite it?"), filename);
59
60 if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
61 return;
62 }
63 }
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 }
73
74 dialog->SetPath( filename );
75
76 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
77 event.SetEventObject( dialog );
78 dialog->GetEventHandler()->ProcessEvent( event );
79 }
80
81 //-----------------------------------------------------------------------------
82 // "clicked" for Cancel-button
83 //-----------------------------------------------------------------------------
84
85 static
86 void gtk_filedialog_cancel_callback( GtkWidget *WXUNUSED(w), wxFileDialog *dialog )
87 {
88 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
89 event.SetEventObject( dialog );
90 dialog->GetEventHandler()->ProcessEvent( event );
91 }
92
93 //-----------------------------------------------------------------------------
94 // wxFileDialog
95 //-----------------------------------------------------------------------------
96
97 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
98
99 wxFileDialog::wxFileDialog( wxWindow *parent, const wxString& message,
100 const wxString& defaultDir, const wxString& defaultFileName,
101 const wxString& wildCard,
102 long style, const wxPoint& pos )
103 {
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 );
116
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 );
120
121 GtkFileSelection *sel = GTK_FILE_SELECTION(m_widget);
122 gtk_file_selection_hide_fileop_buttons( sel ); // they don't work anyway
123
124 m_path.Append(m_dir);
125 if(! m_path.IsEmpty() && m_path.Last()!='/') m_path.Append('/');
126 m_path.Append(m_fileName);
127
128 if(m_path.Length()>1) gtk_file_selection_set_filename(sel,m_path);
129
130 gtk_signal_connect( GTK_OBJECT(sel->ok_button), "clicked",
131 GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback), (gpointer*)this );
132
133 // strange way to internationalize
134 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->ok_button)->child ), _("OK") );
135
136 gtk_signal_connect( GTK_OBJECT(sel->cancel_button), "clicked",
137 GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback), (gpointer*)this );
138
139 // strange way to internationalize
140 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->cancel_button)->child ), _("Cancel") );
141
142 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
143 GTK_SIGNAL_FUNC(gtk_filedialog_delete_callback), (gpointer)this );
144 }
145
146 wxString wxFileSelector( const char *title,
147 const char *defaultDir, const char *defaultFileName,
148 const char *defaultExtension, const char *filter, int flags,
149 wxWindow *parent, int x, int y )
150 {
151 wxString filter2("");
152 if ( defaultExtension && !filter )
153 filter2 = wxString("*.") + wxString(defaultExtension) ;
154 else if ( filter )
155 filter2 = filter;
156
157 wxString defaultDirString;
158 if (defaultDir)
159 defaultDirString = defaultDir;
160 else
161 defaultDirString = "";
162
163 wxString defaultFilenameString;
164 if (defaultFileName)
165 defaultFilenameString = defaultFileName;
166 else
167 defaultFilenameString = "";
168
169 wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
170
171 if ( fileDialog.ShowModal() == wxID_OK )
172 {
173 return fileDialog.GetPath();
174 }
175 else
176 {
177 return wxEmptyString;
178 }
179 }
180
181 wxString wxLoadFileSelector( const char *what, const char *extension, const char *default_name, wxWindow *parent )
182 {
183 char *ext = (char *)extension;
184
185 char prompt[50];
186 wxString str = _("Load %s file");
187 sprintf(prompt, str, what);
188
189 if (*ext == '.') ext++;
190 char wild[60];
191 sprintf(wild, "*.%s", ext);
192
193 return wxFileSelector (prompt, (const char *) NULL, default_name, ext, wild, 0, parent);
194 }
195
196 wxString wxSaveFileSelector(const char *what, const char *extension, const char *default_name,
197 wxWindow *parent )
198 {
199 char *ext = (char *)extension;
200
201 char prompt[50];
202 wxString str = _("Save %s file");
203 sprintf(prompt, str, what);
204
205 if (*ext == '.') ext++;
206 char wild[60];
207 sprintf(wild, "*.%s", ext);
208
209 return wxFileSelector (prompt, (const char *) NULL, default_name, ext, wild, 0, parent);
210 }
211