extracted common code into a single wxfileDialogBase class (patch 758901)
[wxWidgets.git] / src / gtk / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/filedlg.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "filedlg.h"
12 #endif
13
14 #include "wx/filedlg.h"
15 #include "wx/utils.h"
16 #include "wx/intl.h"
17 #include "wx/generic/msgdlgg.h"
18
19 #include <gtk/gtk.h>
20
21 //-----------------------------------------------------------------------------
22 // idle system
23 //-----------------------------------------------------------------------------
24
25 extern void wxapp_install_idle_handler();
26 extern bool g_isIdle;
27
28 //-----------------------------------------------------------------------------
29 // "delete_event"
30 //-----------------------------------------------------------------------------
31
32 static
33 bool gtk_filedialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
34 {
35 if (g_isIdle) wxapp_install_idle_handler();
36
37 /*
38 printf( "OnDelete from " );
39 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
40 printf( win->GetClassInfo()->GetClassName() );
41 printf( ".\n" );
42 */
43
44 win->Close();
45
46 return TRUE;
47 }
48
49 //-----------------------------------------------------------------------------
50 // "clicked" for OK-button
51 //-----------------------------------------------------------------------------
52
53 static
54 void gtk_filedialog_ok_callback( GtkWidget *WXUNUSED(widget), wxFileDialog *dialog )
55 {
56 if (g_isIdle) wxapp_install_idle_handler();
57
58 int style = dialog->GetStyle();
59
60 GtkFileSelection *filedlg = GTK_FILE_SELECTION(dialog->m_widget);
61 char *filename = gtk_file_selection_get_filename(filedlg);
62
63 if ( (style & wxSAVE) && ( style & wxOVERWRITE_PROMPT ) )
64 {
65 if (wxFileExists( filename ))
66 {
67 wxString msg;
68 msg.Printf( _("File '%s' already exists, do you really want to "
69 "overwrite it?"), filename);
70
71 if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
72 return;
73 }
74 }
75 else if ( (style & wxOPEN) && ( style & wxFILE_MUST_EXIST) )
76 {
77 if ( !wxFileExists( filename ) )
78 {
79 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK);
80
81 return;
82 }
83 }
84
85 // change to the directory where the user went if asked
86 if ( style & wxCHANGE_DIR )
87 {
88 wxString cwd;
89 wxSplitPath(filename, &cwd, NULL, NULL);
90
91 if ( cwd != wxGetWorkingDirectory() )
92 {
93 wxSetWorkingDirectory(cwd);
94 }
95 }
96
97 dialog->SetPath( filename );
98
99 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
100 event.SetEventObject( dialog );
101 dialog->GetEventHandler()->ProcessEvent( event );
102 }
103
104 //-----------------------------------------------------------------------------
105 // "clicked" for Cancel-button
106 //-----------------------------------------------------------------------------
107
108 static
109 void gtk_filedialog_cancel_callback( GtkWidget *WXUNUSED(w), wxFileDialog *dialog )
110 {
111 if (g_isIdle) wxapp_install_idle_handler();
112
113 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
114 event.SetEventObject( dialog );
115 dialog->GetEventHandler()->ProcessEvent( event );
116 }
117
118 //-----------------------------------------------------------------------------
119 // wxFileDialog
120 //-----------------------------------------------------------------------------
121
122 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxFileDialogBase)
123
124 wxFileDialog::wxFileDialog( wxWindow *parent, const wxString& message,
125 const wxString& defaultDir, const wxString& defaultFileName,
126 const wxString& wildCard,
127 long style, const wxPoint& pos )
128 :wxFileDialogBase(parent, message, defaultDir, defaultFileName, wildCard, style, pos)
129 {
130 m_needParent = FALSE;
131
132 if (!PreCreation( parent, pos, wxDefaultSize ) ||
133 !CreateBase( parent, -1, pos, wxDefaultSize, style | wxDIALOG_MODAL, wxDefaultValidator, wxT("filedialog") ))
134 {
135 wxFAIL_MSG( wxT("wxXX creation failed") );
136 return;
137 }
138
139 m_widget = gtk_file_selection_new( m_message.mbc_str() );
140
141 int x = (gdk_screen_width () - 400) / 2;
142 int y = (gdk_screen_height () - 400) / 2;
143 gtk_widget_set_uposition( m_widget, x, y );
144
145 GtkFileSelection *sel = GTK_FILE_SELECTION(m_widget);
146 gtk_file_selection_hide_fileop_buttons( sel ); // they don't work anyway
147
148 m_path.Append(m_dir);
149 if( ! m_path.IsEmpty() && m_path.Last()!=wxT('/') )
150 m_path.Append('/');
151 m_path.Append(m_fileName);
152
153 if(m_path.Length()>1) gtk_file_selection_set_filename(sel,m_path.mbc_str());
154
155 gtk_signal_connect( GTK_OBJECT(sel->ok_button), "clicked",
156 GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback), (gpointer*)this );
157
158 // strange way to internationalize
159 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->ok_button)->child ), wxConvCurrent->cWX2MB(_("OK")) );
160
161 gtk_signal_connect( GTK_OBJECT(sel->cancel_button), "clicked",
162 GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback), (gpointer*)this );
163
164 // strange way to internationalize
165 gtk_label_set( GTK_LABEL( GTK_BUTTON(sel->cancel_button)->child ), wxConvCurrent->cWX2MB(_("Cancel")) );
166
167 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
168 GTK_SIGNAL_FUNC(gtk_filedialog_delete_callback), (gpointer)this );
169 }
170
171 void wxFileDialog::SetPath(const wxString& path)
172 {
173 // not only set the full path but also update filename and dir
174 m_path = path;
175 if ( !!path )
176 {
177 wxString ext;
178 wxSplitPath(path, &m_dir, &m_fileName, &ext);
179 if (!ext.IsEmpty())
180 {
181 m_fileName += wxT(".");
182 m_fileName += ext;
183 }
184 }
185 }
186