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