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 #define gtk_file_selection_hide_fileop_buttons gtk_file_selection_hide_fileop_
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
37 bool gtk_filedialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
39 if (g_isIdle
) wxapp_install_idle_handler();
42 printf( "OnDelete from " );
43 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
44 printf( win->GetClassInfo()->GetClassName() );
53 //-----------------------------------------------------------------------------
54 // "clicked" for OK-button
55 //-----------------------------------------------------------------------------
58 void gtk_filedialog_ok_callback( GtkWidget
*WXUNUSED(widget
), wxFileDialog
*dialog
)
60 if (g_isIdle
) wxapp_install_idle_handler();
62 int style
= dialog
->GetStyle();
64 GtkFileSelection
*filedlg
= GTK_FILE_SELECTION(dialog
->m_widget
);
65 char *filename
= gtk_file_selection_get_filename(filedlg
);
67 if ( (style
& wxSAVE
) && ( style
& wxOVERWRITE_PROMPT
) )
69 if (wxFileExists( filename
))
72 msg
.Printf( _("File '%s' already exists, do you really want to "
73 "overwrite it?"), filename
);
75 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
79 else if ( (style
& wxOPEN
) && ( style
& wxFILE_MUST_EXIST
) )
81 if ( !wxFileExists( filename
) )
83 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
);
89 dialog
->SetPath( filename
);
91 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
92 event
.SetEventObject( dialog
);
93 dialog
->GetEventHandler()->ProcessEvent( event
);
96 //-----------------------------------------------------------------------------
97 // "clicked" for Cancel-button
98 //-----------------------------------------------------------------------------
101 void gtk_filedialog_cancel_callback( GtkWidget
*WXUNUSED(w
), wxFileDialog
*dialog
)
103 if (g_isIdle
) wxapp_install_idle_handler();
105 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
106 event
.SetEventObject( dialog
);
107 dialog
->GetEventHandler()->ProcessEvent( event
);
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
114 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
116 wxFileDialog::wxFileDialog( wxWindow
*parent
, const wxString
& message
,
117 const wxString
& defaultDir
, const wxString
& defaultFileName
,
118 const wxString
& wildCard
,
119 long style
, const wxPoint
& pos
)
121 m_needParent
= FALSE
;
123 if (!PreCreation( parent
, pos
, wxDefaultSize
) ||
124 !CreateBase( parent
, -1, pos
, wxDefaultSize
, style
| wxDIALOG_MODAL
, wxDefaultValidator
, wxT("filedialog") ))
126 wxFAIL_MSG( wxT("wxXX creation failed") );
132 m_fileName
= defaultFileName
;
134 m_wildCard
= wildCard
;
135 m_dialogStyle
= style
;
138 m_widget
= gtk_file_selection_new( m_message
.mbc_str() );
140 int x
= (gdk_screen_width () - 400) / 2;
141 int y
= (gdk_screen_height () - 400) / 2;
142 gtk_widget_set_uposition( m_widget
, x
, y
);
144 GtkFileSelection
*sel
= GTK_FILE_SELECTION(m_widget
);
145 gtk_file_selection_hide_fileop_buttons( sel
); // they don't work anyway
147 m_path
.Append(m_dir
);
148 if( ! m_path
.IsEmpty() && m_path
.Last()!=wxT('/') )
150 m_path
.Append(m_fileName
);
152 if(m_path
.Length()>1) gtk_file_selection_set_filename(sel
,m_path
.mbc_str());
154 gtk_signal_connect( GTK_OBJECT(sel
->ok_button
), "clicked",
155 GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback
), (gpointer
*)this );
157 // strange way to internationalize
158 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel
->ok_button
)->child
), wxConvCurrent
->cWX2MB(_("OK")) );
160 gtk_signal_connect( GTK_OBJECT(sel
->cancel_button
), "clicked",
161 GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback
), (gpointer
*)this );
163 // strange way to internationalize
164 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel
->cancel_button
)->child
), wxConvCurrent
->cWX2MB(_("Cancel")) );
166 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
167 GTK_SIGNAL_FUNC(gtk_filedialog_delete_callback
), (gpointer
)this );
170 void wxFileDialog::SetPath(const wxString
& path
)
172 // not only set the full path but also update filename and dir
177 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
180 m_fileName
+= wxT(".");
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
191 wxFileSelectorEx(const wxChar
*message
,
192 const wxChar
*default_path
,
193 const wxChar
*default_filename
,
194 int *indexDefaultExtension
,
195 const wxChar
*wildcard
,
200 // TODO: implement this somehow
201 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
202 wildcard
, flags
, parent
, x
, y
);
205 wxString
wxFileSelector( const wxChar
*title
,
206 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
207 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
208 wxWindow
*parent
, int x
, int y
)
211 if ( defaultExtension
&& !filter
)
212 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
216 wxString defaultDirString
;
218 defaultDirString
= defaultDir
;
220 wxString defaultFilenameString
;
222 defaultFilenameString
= defaultFileName
;
224 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
226 if ( fileDialog
.ShowModal() == wxID_OK
)
228 return fileDialog
.GetPath();
232 return wxEmptyString
;
236 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
238 wxChar
*ext
= (wxChar
*)extension
;
241 wxString str
= _("Load %s file");
242 wxSprintf(prompt
, str
, what
);
244 if (*ext
== wxT('.')) ext
++;
246 wxSprintf(wild
, wxT("*.%s"), ext
);
248 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
251 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
254 wxChar
*ext
= (wxChar
*)extension
;
257 wxString str
= _("Save %s file");
258 wxSprintf(prompt
, str
, what
);
260 if (*ext
== wxT('.')) ext
++;
262 wxSprintf(wild
, wxT("*.%s"), ext
);
264 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);