1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "filedlg.h"
14 #include "wx/filedlg.h"
17 #include "wx/generic/msgdlgg.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
34 bool gtk_filedialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
36 if (g_isIdle
) wxapp_install_idle_handler();
39 printf( "OnDelete from " );
40 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
41 printf( win->GetClassInfo()->GetClassName() );
50 //-----------------------------------------------------------------------------
51 // "clicked" for OK-button
52 //-----------------------------------------------------------------------------
55 void gtk_filedialog_ok_callback( GtkWidget
*WXUNUSED(widget
), wxFileDialog
*dialog
)
57 if (g_isIdle
) wxapp_install_idle_handler();
59 int style
= dialog
->GetStyle();
61 GtkFileSelection
*filedlg
= GTK_FILE_SELECTION(dialog
->m_widget
);
62 char *filename
= gtk_file_selection_get_filename(filedlg
);
64 if ( (style
& wxSAVE
) && ( style
& wxOVERWRITE_PROMPT
) )
66 if (wxFileExists( filename
))
69 msg
.Printf( _("File '%s' already exists, do you really want to "
70 "overwrite it?"), filename
);
72 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
76 else if ( (style
& wxOPEN
) && ( style
& wxFILE_MUST_EXIST
) )
78 if ( !wxFileExists( filename
) )
80 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
);
86 dialog
->SetPath( filename
);
88 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
89 event
.SetEventObject( dialog
);
90 dialog
->GetEventHandler()->ProcessEvent( event
);
93 //-----------------------------------------------------------------------------
94 // "clicked" for Cancel-button
95 //-----------------------------------------------------------------------------
98 void gtk_filedialog_cancel_callback( GtkWidget
*WXUNUSED(w
), wxFileDialog
*dialog
)
100 if (g_isIdle
) wxapp_install_idle_handler();
102 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
103 event
.SetEventObject( dialog
);
104 dialog
->GetEventHandler()->ProcessEvent( event
);
107 //-----------------------------------------------------------------------------
109 //-----------------------------------------------------------------------------
111 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
113 wxFileDialog::wxFileDialog( wxWindow
*parent
, const wxString
& message
,
114 const wxString
& defaultDir
, const wxString
& defaultFileName
,
115 const wxString
& wildCard
,
116 long style
, const wxPoint
& pos
)
118 m_needParent
= FALSE
;
120 if (!PreCreation( parent
, pos
, wxDefaultSize
) ||
121 !CreateBase( parent
, -1, pos
, wxDefaultSize
, style
| wxDIALOG_MODAL
, wxDefaultValidator
, wxT("filedialog") ))
123 wxFAIL_MSG( wxT("wxXX creation failed") );
129 m_fileName
= defaultFileName
;
131 m_wildCard
= wildCard
;
132 m_dialogStyle
= style
;
135 m_widget
= gtk_file_selection_new( m_message
.mbc_str() );
137 int x
= (gdk_screen_width () - 400) / 2;
138 int y
= (gdk_screen_height () - 400) / 2;
139 gtk_widget_set_uposition( m_widget
, x
, y
);
141 GtkFileSelection
*sel
= GTK_FILE_SELECTION(m_widget
);
142 gtk_file_selection_hide_fileop_buttons( sel
); // they don't work anyway
144 m_path
.Append(m_dir
);
145 if( ! m_path
.IsEmpty() && m_path
.Last()!=wxT('/') )
147 m_path
.Append(m_fileName
);
149 if(m_path
.Length()>1) gtk_file_selection_set_filename(sel
,m_path
.mbc_str());
151 gtk_signal_connect( GTK_OBJECT(sel
->ok_button
), "clicked",
152 GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback
), (gpointer
*)this );
154 // strange way to internationalize
155 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel
->ok_button
)->child
), wxConvCurrent
->cWX2MB(_("OK")) );
157 gtk_signal_connect( GTK_OBJECT(sel
->cancel_button
), "clicked",
158 GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback
), (gpointer
*)this );
160 // strange way to internationalize
161 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel
->cancel_button
)->child
), wxConvCurrent
->cWX2MB(_("Cancel")) );
163 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
164 GTK_SIGNAL_FUNC(gtk_filedialog_delete_callback
), (gpointer
)this );
167 void wxFileDialog::SetPath(const wxString
& path
)
169 // not only set the full path but also update filename and dir
174 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
177 m_fileName
+= wxT(".");
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
188 wxFileSelectorEx(const wxChar
*message
,
189 const wxChar
*default_path
,
190 const wxChar
*default_filename
,
191 int *indexDefaultExtension
,
192 const wxChar
*wildcard
,
197 // TODO: implement this somehow
198 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
199 wildcard
, flags
, parent
, x
, y
);
202 wxString
wxFileSelector( const wxChar
*title
,
203 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
204 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
205 wxWindow
*parent
, int x
, int y
)
208 if ( defaultExtension
&& !filter
)
209 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
213 wxString defaultDirString
;
215 defaultDirString
= defaultDir
;
217 wxString defaultFilenameString
;
219 defaultFilenameString
= defaultFileName
;
221 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
223 if ( fileDialog
.ShowModal() == wxID_OK
)
225 return fileDialog
.GetPath();
229 return wxEmptyString
;
233 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
235 wxChar
*ext
= (wxChar
*)extension
;
238 wxString str
= _("Load %s file");
239 wxSprintf(prompt
, str
, what
);
241 if (*ext
== wxT('.')) ext
++;
243 wxSprintf(wild
, wxT("*.%s"), ext
);
245 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
248 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
251 wxChar
*ext
= (wxChar
*)extension
;
254 wxString str
= _("Save %s file");
255 wxSprintf(prompt
, str
, what
);
257 if (*ext
== wxT('.')) ext
++;
259 wxSprintf(wild
, wxT("*.%s"), ext
);
261 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);