1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/filedlg.cpp
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 // change to the directory where the user went if asked
87 if ( style
& wxCHANGE_DIR
)
90 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
92 if ( cwd
!= wxGetWorkingDirectory() )
94 wxSetWorkingDirectory(cwd
);
98 dialog
->SetPath( filename
);
100 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
101 event
.SetEventObject( dialog
);
102 dialog
->GetEventHandler()->ProcessEvent( event
);
105 //-----------------------------------------------------------------------------
106 // "clicked" for Cancel-button
107 //-----------------------------------------------------------------------------
110 void gtk_filedialog_cancel_callback( GtkWidget
*WXUNUSED(w
), wxFileDialog
*dialog
)
112 if (g_isIdle
) wxapp_install_idle_handler();
114 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
115 event
.SetEventObject( dialog
);
116 dialog
->GetEventHandler()->ProcessEvent( event
);
119 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
123 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
125 wxFileDialog::wxFileDialog( wxWindow
*parent
, const wxString
& message
,
126 const wxString
& defaultDir
, const wxString
& defaultFileName
,
127 const wxString
& wildCard
,
128 long style
, const wxPoint
& pos
)
130 m_needParent
= FALSE
;
132 if (!PreCreation( parent
, pos
, wxDefaultSize
) ||
133 !CreateBase( parent
, -1, pos
, wxDefaultSize
, style
| wxDIALOG_MODAL
, wxDefaultValidator
, wxT("filedialog") ))
135 wxFAIL_MSG( wxT("wxXX creation failed") );
141 m_fileName
= defaultFileName
;
143 m_wildCard
= wildCard
;
144 m_dialogStyle
= style
;
147 m_widget
= gtk_file_selection_new( m_message
.mbc_str() );
149 int x
= (gdk_screen_width () - 400) / 2;
150 int y
= (gdk_screen_height () - 400) / 2;
151 gtk_widget_set_uposition( m_widget
, x
, y
);
153 GtkFileSelection
*sel
= GTK_FILE_SELECTION(m_widget
);
154 gtk_file_selection_hide_fileop_buttons( sel
); // they don't work anyway
156 m_path
.Append(m_dir
);
157 if( ! m_path
.IsEmpty() && m_path
.Last()!=wxT('/') )
159 m_path
.Append(m_fileName
);
161 if(m_path
.Length()>1) gtk_file_selection_set_filename(sel
,m_path
.mbc_str());
163 gtk_signal_connect( GTK_OBJECT(sel
->ok_button
), "clicked",
164 GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback
), (gpointer
*)this );
166 // strange way to internationalize
167 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel
->ok_button
)->child
), wxConvCurrent
->cWX2MB(_("OK")) );
169 gtk_signal_connect( GTK_OBJECT(sel
->cancel_button
), "clicked",
170 GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback
), (gpointer
*)this );
172 // strange way to internationalize
173 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel
->cancel_button
)->child
), wxConvCurrent
->cWX2MB(_("Cancel")) );
175 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
176 GTK_SIGNAL_FUNC(gtk_filedialog_delete_callback
), (gpointer
)this );
179 void wxFileDialog::SetPath(const wxString
& path
)
181 // not only set the full path but also update filename and dir
186 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
189 m_fileName
+= wxT(".");
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
200 wxFileSelectorEx(const wxChar
*message
,
201 const wxChar
*default_path
,
202 const wxChar
*default_filename
,
203 int *indexDefaultExtension
,
204 const wxChar
*wildcard
,
209 // TODO: implement this somehow
210 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
211 wildcard
, flags
, parent
, x
, y
);
214 wxString
wxFileSelector( const wxChar
*title
,
215 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
216 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
217 wxWindow
*parent
, int x
, int y
)
220 if ( defaultExtension
&& !filter
)
221 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
225 wxString defaultDirString
;
227 defaultDirString
= defaultDir
;
229 wxString defaultFilenameString
;
231 defaultFilenameString
= defaultFileName
;
233 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
235 if ( fileDialog
.ShowModal() == wxID_OK
)
237 return fileDialog
.GetPath();
241 return wxEmptyString
;
245 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
247 wxChar
*ext
= (wxChar
*)extension
;
249 wxString prompt
= wxString::Format(_("Load %s file"), what
);
251 if (*ext
== wxT('.'))
254 wxString wild
= wxString::Format(_T("*.%s"), ext
);
256 return wxFileSelector(prompt
, (const wxChar
*) NULL
, default_name
,
257 ext
, wild
, 0, parent
);
260 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
263 wxChar
*ext
= (wxChar
*)extension
;
265 wxString prompt
= wxString::Format(_("Save %s file"), what
);
267 if (*ext
== wxT('.'))
270 wxString wild
= wxString::Format(_T("*.%s"), ext
);
272 return wxFileSelector(prompt
, (const wxChar
*) NULL
, default_name
,
273 ext
, wild
, 0, parent
);