(1) Denis Pershin's patch for wxGTK (memory leaks corrections)
[wxWidgets.git] / src / gtk / filedlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: filedlg.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "filedlg.h"
13 #endif
14
15 #include "wx/filedlg.h"
16 #include "wx/utils.h"
17 #include "wx/intl.h"
18 #include "wx/generic/msgdlgg.h"
19
20 //-----------------------------------------------------------------------------
21 // wxFileDialog
22 //-----------------------------------------------------------------------------
23
24 void gtk_filedialog_ok_callback( GtkWidget *WXUNUSED(widget), gpointer data )
25 {
26 wxFileDialog *dialog = (wxFileDialog*)data;
27 wxCommandEvent event(wxEVT_NULL);
28 int style;
29
30 style=dialog->GetStyle();
31
32 if((style&wxSAVE)&&(style&wxOVERWRITE_PROMPT))
33 if(wxFileExists(gtk_file_selection_get_filename(GTK_FILE_SELECTION(dialog->m_widget) ))) {
34 if(wxMessageBox("File exists. Overwrite?","Confirm",wxYES_NO)!=wxYES)
35 return;
36 }
37
38 dialog->OnOk( event );
39 };
40
41 void gtk_filedialog_cancel_callback( GtkWidget *WXUNUSED(widget), gpointer data )
42 {
43 wxFileDialog *dialog = (wxFileDialog*)data;
44 wxCommandEvent event(wxEVT_NULL);
45 dialog->OnCancel( event );
46 };
47
48 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
49
50 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
51 const wxString& defaultDir, const wxString& defaultFileName,
52 const wxString& wildCard,
53 long style, const wxPoint& pos )
54 {
55 m_needParent = FALSE;
56
57 PreCreation( parent, -1, pos, wxDefaultSize, style | wxDIALOG_MODAL, "filedialog" );
58 m_message = message;
59 m_path = "";
60 m_fileName = defaultFileName;
61 m_dir = defaultDir;
62 m_wildCard = wildCard;
63 m_dialogStyle = style;
64 m_filterIndex = 1;
65
66 m_widget = gtk_file_selection_new( "File selection" );
67
68 int x = (gdk_screen_width () - 400) / 2;
69 int y = (gdk_screen_height () - 400) / 2;
70 gtk_widget_set_uposition( m_widget, x, y );
71
72 GtkFileSelection *sel = GTK_FILE_SELECTION(m_widget);
73
74 m_path.Append(m_dir);
75 if(! m_path.IsEmpty() && m_path.Last()!='/') m_path.Append('/');
76 m_path.Append(m_fileName);
77
78 if(m_path.Length()>1) gtk_file_selection_set_filename(sel,m_path);
79
80 gtk_signal_connect( GTK_OBJECT(sel->ok_button), "clicked",
81 GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback), (gpointer*)this );
82
83 gtk_signal_connect( GTK_OBJECT(sel->cancel_button), "clicked",
84 GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback), (gpointer*)this );
85 };
86
87 int wxFileDialog::ShowModal(void)
88 {
89 int ret = wxDialog::ShowModal();
90
91 if (ret == wxID_OK)
92 {
93 m_fileName = gtk_file_selection_get_filename( GTK_FILE_SELECTION(m_widget) );
94 m_path = gtk_file_selection_get_filename( GTK_FILE_SELECTION(m_widget) );
95 };
96 return ret;
97 };
98
99
100 char *wxFileSelector(const char *title,
101 const char *defaultDir, const char *defaultFileName,
102 const char *defaultExtension, const char *filter, int flags,
103 wxWindow *parent, int x, int y)
104 {
105 wxString filter2("");
106 if ( defaultExtension && !filter )
107 filter2 = wxString("*.") + wxString(defaultExtension) ;
108 else if ( filter )
109 filter2 = filter;
110
111 wxString defaultDirString;
112 if (defaultDir)
113 defaultDirString = defaultDir;
114 else
115 defaultDirString = "";
116
117 wxString defaultFilenameString;
118 if (defaultFileName)
119 defaultFilenameString = defaultFileName;
120 else
121 defaultFilenameString = "";
122
123 wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString,
124 filter2, flags, wxPoint(x, y));
125
126 if ( fileDialog.ShowModal() == wxID_OK )
127 {
128 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
129 return wxBuffer;
130 }
131 else
132 return NULL;
133 };
134
135 char* wxLoadFileSelector(const char *what, const char *extension, const char *default_name,
136 wxWindow *parent )
137 {
138 char *ext = (char *)extension;
139
140 char prompt[50];
141 wxString str = _("Load %s file");
142 sprintf(prompt, str, what);
143
144 if (*ext == '.') ext++;
145 char wild[60];
146 sprintf(wild, "*.%s", ext);
147
148 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
149 };
150
151 char* wxSaveFileSelector(const char *what, const char *extension, const char *default_name,
152 wxWindow *parent )
153 {
154 char *ext = (char *)extension;
155
156 char prompt[50];
157 wxString str = _("Save %s file");
158 sprintf(prompt, str, what);
159
160 if (*ext == '.') ext++;
161 char wild[60];
162 sprintf(wild, "*.%s", ext);
163
164 return wxFileSelector (prompt, NULL, default_name, ext, wild, 0, parent);
165 };
166
167
168
169