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"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 extern void wxapp_install_idle_handler();
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
33 bool gtk_filedialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
35 if (g_isIdle
) wxapp_install_idle_handler();
38 printf( "OnDelete from " );
39 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
40 printf( win->GetClassInfo()->GetClassName() );
49 //-----------------------------------------------------------------------------
50 // "clicked" for OK-button
51 //-----------------------------------------------------------------------------
54 void gtk_filedialog_ok_callback( GtkWidget
*WXUNUSED(widget
), wxFileDialog
*dialog
)
56 if (g_isIdle
) wxapp_install_idle_handler();
58 int style
= dialog
->GetStyle();
60 GtkFileSelection
*filedlg
= GTK_FILE_SELECTION(dialog
->m_widget
);
61 char *filename
= gtk_file_selection_get_filename(filedlg
);
63 if ( (style
& wxSAVE
) && ( style
& wxOVERWRITE_PROMPT
) )
65 if (wxFileExists( filename
))
68 msg
.Printf( _("File '%s' already exists, do you really want to "
69 "overwrite it?"), filename
);
71 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
75 else if ( (style
& wxOPEN
) && ( style
& wxFILE_MUST_EXIST
) )
77 if ( !wxFileExists( filename
) )
79 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
);
85 dialog
->SetPath( filename
);
87 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
88 event
.SetEventObject( dialog
);
89 dialog
->GetEventHandler()->ProcessEvent( event
);
92 //-----------------------------------------------------------------------------
93 // "clicked" for Cancel-button
94 //-----------------------------------------------------------------------------
97 void gtk_filedialog_cancel_callback( GtkWidget
*WXUNUSED(w
), wxFileDialog
*dialog
)
99 if (g_isIdle
) wxapp_install_idle_handler();
101 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
102 event
.SetEventObject( dialog
);
103 dialog
->GetEventHandler()->ProcessEvent( event
);
106 //-----------------------------------------------------------------------------
108 //-----------------------------------------------------------------------------
110 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
112 wxFileDialog::wxFileDialog( wxWindow
*parent
, const wxString
& message
,
113 const wxString
& defaultDir
, const wxString
& defaultFileName
,
114 const wxString
& wildCard
,
115 long style
, const wxPoint
& pos
)
117 m_needParent
= FALSE
;
119 PreCreation( parent
, -1, pos
, wxDefaultSize
, style
| wxDIALOG_MODAL
, "filedialog" );
122 m_fileName
= defaultFileName
;
124 m_wildCard
= wildCard
;
125 m_dialogStyle
= style
;
128 m_widget
= gtk_file_selection_new( m_message
.mbc_str() );
130 int x
= (gdk_screen_width () - 400) / 2;
131 int y
= (gdk_screen_height () - 400) / 2;
132 gtk_widget_set_uposition( m_widget
, x
, y
);
134 GtkFileSelection
*sel
= GTK_FILE_SELECTION(m_widget
);
135 gtk_file_selection_hide_fileop_buttons( sel
); // they don't work anyway
137 m_path
.Append(m_dir
);
138 if( ! m_path
.IsEmpty() && m_path
.Last()!=_T('/') )
140 m_path
.Append(m_fileName
);
142 if(m_path
.Length()>1) gtk_file_selection_set_filename(sel
,m_path
.mbc_str());
144 gtk_signal_connect( GTK_OBJECT(sel
->ok_button
), "clicked",
145 GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback
), (gpointer
*)this );
147 // strange way to internationalize
148 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel
->ok_button
)->child
), wxConvCurrent
->cWX2MB(_("OK")) );
150 gtk_signal_connect( GTK_OBJECT(sel
->cancel_button
), "clicked",
151 GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback
), (gpointer
*)this );
153 // strange way to internationalize
154 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel
->cancel_button
)->child
), wxConvCurrent
->cWX2MB(_("Cancel")) );
156 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
157 GTK_SIGNAL_FUNC(gtk_filedialog_delete_callback
), (gpointer
)this );
160 void wxFileDialog::SetPath(const wxString
& path
)
162 // not only set the full path but also update filename and dir
167 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
170 m_fileName
+= _T(".");
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
181 wxFileSelectorEx(const wxChar
*message
,
182 const wxChar
*default_path
,
183 const wxChar
*default_filename
,
184 int *indexDefaultExtension
,
185 const wxChar
*wildcard
,
190 // TODO: implement this somehow
191 return wxFileSelector(message
, default_path
, default_filename
, _T(""),
192 wildcard
, flags
, parent
, x
, y
);
195 wxString
wxFileSelector( const wxChar
*title
,
196 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
197 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
198 wxWindow
*parent
, int x
, int y
)
201 if ( defaultExtension
&& !filter
)
202 filter2
= wxString(_T("*.")) + wxString(defaultExtension
) ;
206 wxString defaultDirString
;
208 defaultDirString
= defaultDir
;
210 wxString defaultFilenameString
;
212 defaultFilenameString
= defaultFileName
;
214 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
216 if ( fileDialog
.ShowModal() == wxID_OK
)
218 return fileDialog
.GetPath();
222 return wxEmptyString
;
226 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
228 wxChar
*ext
= (wxChar
*)extension
;
231 wxString str
= _("Load %s file");
232 wxSprintf(prompt
, str
, what
);
234 if (*ext
== _T('.')) ext
++;
236 wxSprintf(wild
, _T("*.%s"), ext
);
238 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
241 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
244 wxChar
*ext
= (wxChar
*)extension
;
247 wxString str
= _("Save %s file");
248 wxSprintf(prompt
, str
, what
);
250 if (*ext
== _T('.')) ext
++;
252 wxSprintf(wild
, _T("*.%s"), ext
);
254 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);