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 //-----------------------------------------------------------------------------
26 bool gtk_filedialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
29 printf( "OnDelete from " );
30 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
31 printf( win->GetClassInfo()->GetClassName() );
40 //-----------------------------------------------------------------------------
41 // "clicked" for OK-button
42 //-----------------------------------------------------------------------------
45 void gtk_filedialog_ok_callback( GtkWidget
*WXUNUSED(widget
), wxFileDialog
*dialog
)
47 int style
= dialog
->GetStyle();
49 GtkFileSelection
*filedlg
= GTK_FILE_SELECTION(dialog
->m_widget
);
50 char *filename
= gtk_file_selection_get_filename(filedlg
);
52 if ( (style
& wxSAVE
) && ( style
& wxOVERWRITE_PROMPT
) )
54 if (wxFileExists( filename
))
57 msg
.Printf( _("File '%s' already exists, do you really want to "
58 "overwrite it?"), filename
);
60 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
64 else if ( (style
& wxOPEN
) && ( style
& wxFILE_MUST_EXIST
) )
66 if ( !wxFileExists( filename
) )
68 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
);
74 dialog
->SetPath( filename
);
76 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
77 event
.SetEventObject( dialog
);
78 dialog
->GetEventHandler()->ProcessEvent( event
);
81 //-----------------------------------------------------------------------------
82 // "clicked" for Cancel-button
83 //-----------------------------------------------------------------------------
86 void gtk_filedialog_cancel_callback( GtkWidget
*WXUNUSED(w
), wxFileDialog
*dialog
)
88 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
89 event
.SetEventObject( dialog
);
90 dialog
->GetEventHandler()->ProcessEvent( event
);
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
97 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
99 wxFileDialog::wxFileDialog( wxWindow
*parent
, const wxString
& message
,
100 const wxString
& defaultDir
, const wxString
& defaultFileName
,
101 const wxString
& wildCard
,
102 long style
, const wxPoint
& pos
)
104 m_needParent
= FALSE
;
106 PreCreation( parent
, -1, pos
, wxDefaultSize
, style
| wxDIALOG_MODAL
, "filedialog" );
109 m_fileName
= defaultFileName
;
111 m_wildCard
= wildCard
;
112 m_dialogStyle
= style
;
115 m_widget
= gtk_file_selection_new( m_message
.mbc_str() );
117 int x
= (gdk_screen_width () - 400) / 2;
118 int y
= (gdk_screen_height () - 400) / 2;
119 gtk_widget_set_uposition( m_widget
, x
, y
);
121 GtkFileSelection
*sel
= GTK_FILE_SELECTION(m_widget
);
122 gtk_file_selection_hide_fileop_buttons( sel
); // they don't work anyway
124 m_path
.Append(m_dir
);
125 if( ! m_path
.IsEmpty() && m_path
.Last()!=_T('/') )
127 m_path
.Append(m_fileName
);
129 if(m_path
.Length()>1) gtk_file_selection_set_filename(sel
,m_path
.mbc_str());
131 gtk_signal_connect( GTK_OBJECT(sel
->ok_button
), "clicked",
132 GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback
), (gpointer
*)this );
134 // strange way to internationalize
135 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel
->ok_button
)->child
), wxConv_current
->cWX2MB(_("OK")) );
137 gtk_signal_connect( GTK_OBJECT(sel
->cancel_button
), "clicked",
138 GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback
), (gpointer
*)this );
140 // strange way to internationalize
141 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel
->cancel_button
)->child
), wxConv_current
->cWX2MB(_("Cancel")) );
143 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
144 GTK_SIGNAL_FUNC(gtk_filedialog_delete_callback
), (gpointer
)this );
147 void wxFileDialog::SetPath(const wxString
& path
)
149 // not only set the full path but also update filename and dir
154 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 wxString
wxFileSelector( const wxChar
*title
,
164 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
165 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
166 wxWindow
*parent
, int x
, int y
)
169 if ( defaultExtension
&& !filter
)
170 filter2
= wxString(_T("*.")) + wxString(defaultExtension
) ;
174 wxString defaultDirString
;
176 defaultDirString
= defaultDir
;
178 wxString defaultFilenameString
;
180 defaultFilenameString
= defaultFileName
;
182 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
184 if ( fileDialog
.ShowModal() == wxID_OK
)
186 return fileDialog
.GetPath();
190 return wxEmptyString
;
194 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
196 wxChar
*ext
= (wxChar
*)extension
;
199 wxString str
= _("Load %s file");
200 wxSprintf(prompt
, str
, what
);
202 if (*ext
== _T('.')) ext
++;
204 wxSprintf(wild
, _T("*.%s"), ext
);
206 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
209 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
212 wxChar
*ext
= (wxChar
*)extension
;
215 wxString str
= _("Save %s file");
216 wxSprintf(prompt
, str
, what
);
218 if (*ext
== _T('.')) ext
++;
220 wxSprintf(wild
, _T("*.%s"), ext
);
222 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);