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