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