]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/filedlg.cpp
Version 0.4 of wxPython for MSW.
[wxWidgets.git] / src / gtk1 / 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?"),
35 _("Confirm"), wxYES_NO) != wxYES)
36 return;
37 }
38
39 dialog->OnOK( event );
40 }
41
42 void gtk_filedialog_cancel_callback( GtkWidget *WXUNUSED(widget), gpointer data )
43 {
44 wxFileDialog *dialog = (wxFileDialog*)data;
45 wxCommandEvent event(wxEVT_NULL);
46 dialog->OnCancel( event );
47 }
48
49 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
50
51 wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
52 const wxString& defaultDir, const wxString& defaultFileName,
53 const wxString& wildCard,
54 long style, const wxPoint& pos )
55 {
56 m_needParent = FALSE;
57
58 PreCreation( parent, -1, pos, wxDefaultSize, style | wxDIALOG_MODAL, "filedialog" );
59 m_message = message;
60 m_path = "";
61 m_fileName = defaultFileName;
62 m_dir = defaultDir;
63 m_wildCard = wildCard;
64 m_dialogStyle = style;
65 m_filterIndex = 1;
66
67 m_widget = gtk_file_selection_new( m_message );
68
69 int x = (gdk_screen_width () - 400) / 2;
70 int y = (gdk_screen_height () - 400) / 2;
71 gtk_widget_set_uposition( m_widget, x, y );
72
73 GtkFileSelection *sel = GTK_FILE_SELECTION(m_widget);
74
75 m_path.Append(m_dir);
76 if(! m_path.IsEmpty() && m_path.Last()!='/') m_path.Append('/');
77 m_path.Append(m_fileName);
78
79 if(m_path.Length()>1) gtk_file_selection_set_filename(sel,m_path);
80
81 gtk_signal_connect( GTK_OBJECT(sel->ok_button), "clicked",
82 GTK_SIGNAL_FUNC(gtk_filedialog_ok_callback), (gpointer*)this );
83
84 gtk_signal_connect( GTK_OBJECT(sel->cancel_button), "clicked",
85 GTK_SIGNAL_FUNC(gtk_filedialog_cancel_callback), (gpointer*)this );
86 }
87
88 int wxFileDialog::ShowModal(void)
89 {
90 int ret = wxDialog::ShowModal();
91
92 if (ret == wxID_OK)
93 {
94 m_fileName = gtk_file_selection_get_filename( GTK_FILE_SELECTION(m_widget) );
95 m_path = gtk_file_selection_get_filename( GTK_FILE_SELECTION(m_widget) );
96 }
97 return ret;
98 }
99
100
101 char *wxFileSelector(const char *title,
102 const char *defaultDir, const char *defaultFileName,
103 const char *defaultExtension, const char *filter, int flags,
104 wxWindow *parent, int x, int y)
105 {
106 wxString filter2("");
107 if ( defaultExtension && !filter )
108 filter2 = wxString("*.") + wxString(defaultExtension) ;
109 else if ( filter )
110 filter2 = filter;
111
112 wxString defaultDirString;
113 if (defaultDir)
114 defaultDirString = defaultDir;
115 else
116 defaultDirString = "";
117
118 wxString defaultFilenameString;
119 if (defaultFileName)
120 defaultFilenameString = defaultFileName;
121 else
122 defaultFilenameString = "";
123
124 wxFileDialog fileDialog(parent, title, defaultDirString, defaultFilenameString,
125 filter2, flags, wxPoint(x, y));
126
127 if ( fileDialog.ShowModal() == wxID_OK )
128 {
129 strcpy(wxBuffer, (const char *)fileDialog.GetPath());
130 return wxBuffer;
131 }
132 else
133 return (char *) NULL;
134 }
135
136 char* wxLoadFileSelector(const char *what, const char *extension, const char *default_name,
137 wxWindow *parent )
138 {
139 char *ext = (char *)extension;
140
141 char prompt[50];
142 wxString str = _("Load %s file");
143 sprintf(prompt, str, what);
144
145 if (*ext == '.') ext++;
146 char wild[60];
147 sprintf(wild, "*.%s", ext);
148
149 return wxFileSelector (prompt, (const char *) NULL, default_name, ext, wild, 0, parent);
150 }
151
152 char* wxSaveFileSelector(const char *what, const char *extension, const char *default_name,
153 wxWindow *parent )
154 {
155 char *ext = (char *)extension;
156
157 char prompt[50];
158 wxString str = _("Save %s file");
159 sprintf(prompt, str, what);
160
161 if (*ext == '.') ext++;
162 char wild[60];
163 sprintf(wild, "*.%s", ext);
164
165 return wxFileSelector (prompt, (const char *) NULL, default_name, ext, wild, 0, parent);
166 }
167