]>
Commit | Line | Data |
---|---|---|
0106a226 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wxedit.cpp | |
3 | // Author: Robert Roebling | |
4 | // Created: 04/07/02 | |
925e9792 | 5 | // Copyright: |
0106a226 RR |
6 | ///////////////////////////////////////////////////////////////////////////// |
7 | ||
0106a226 RR |
8 | // For compilers that support precompilation |
9 | #include "wx/wxprec.h" | |
10 | ||
11 | #ifdef __BORLANDC__ | |
12 | #pragma hdrstop | |
13 | #endif | |
14 | ||
40de795f | 15 | #include "wx/filename.h" |
6a4e5f03 | 16 | #include "wx/config.h" |
40de795f | 17 | |
0106a226 RR |
18 | // Include private headers |
19 | #include "wxedit.h" | |
20 | ||
6a4e5f03 RR |
21 | //------------------------------------------------------------------------------ |
22 | // constants | |
23 | //------------------------------------------------------------------------------ | |
24 | ||
25 | #define HISTORY_ENTRIES 3 | |
26 | ||
0106a226 RR |
27 | //------------------------------------------------------------------------------ |
28 | // MyFrame | |
29 | //------------------------------------------------------------------------------ | |
30 | ||
31 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
32 | EVT_MENU(ID_ABOUT, MyFrame::OnAbout) | |
925e9792 | 33 | |
0106a226 RR |
34 | EVT_MENU(ID_NEW, MyFrame::OnNew) |
35 | EVT_MENU(ID_OPEN, MyFrame::OnOpen) | |
36 | EVT_MENU(ID_SAVE, MyFrame::OnSave) | |
37 | EVT_MENU(ID_SAVEAS, MyFrame::OnSaveAs) | |
38 | EVT_MENU(ID_QUIT, MyFrame::OnQuit) | |
925e9792 | 39 | |
a0760bd3 RR |
40 | EVT_MENU(ID_COPY, MyFrame::OnCopy) |
41 | EVT_MENU(ID_CUT, MyFrame::OnCut) | |
42 | EVT_MENU(ID_PASTE, MyFrame::OnPaste) | |
43 | EVT_MENU(ID_DELETE, MyFrame::OnDelete) | |
925e9792 | 44 | |
6a4e5f03 | 45 | EVT_MENU_RANGE(ID_LAST_1, ID_LAST_3, MyFrame::OnLastFiles) |
925e9792 | 46 | |
0106a226 | 47 | EVT_CLOSE(MyFrame::OnCloseWindow) |
af870ed8 | 48 | EVT_UPDATE_UI(wxID_ANY,MyFrame::OnUpdateUI) |
0106a226 RR |
49 | END_EVENT_TABLE() |
50 | ||
51 | MyFrame::MyFrame( wxWindow *parent, wxWindowID id, const wxString &title, | |
52 | const wxPoint &position, const wxSize& size, long style ) : | |
53 | wxFrame( parent, id, title, position, size, style ) | |
54 | { | |
6a4e5f03 | 55 | // Create menu and status bar. |
0106a226 | 56 | CreateMyMenuBar(); |
8520f137 | 57 | #if wxUSE_STATUSBAR |
0106a226 | 58 | CreateStatusBar(1); |
9687fdee | 59 | SetStatusText( _T("Welcome to wxEdit!") ); |
8520f137 | 60 | #endif // wxUSE_STATUSBAR |
925e9792 | 61 | |
6a4e5f03 RR |
62 | // Create edit control. Since it is the only |
63 | // control in the frame, it will be resized | |
64 | // to file it out. | |
71307412 | 65 | m_text = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ); |
925e9792 | 66 | |
6a4e5f03 RR |
67 | // Read .ini file for file history etc. |
68 | wxConfig *conf = (wxConfig*) wxConfig::Get(); | |
69 | ||
925e9792 | 70 | int entries = 0; |
9687fdee | 71 | conf->Read( _T("/History/Count"), &entries ); |
925e9792 | 72 | |
6a4e5f03 RR |
73 | for (int i = 0; i < entries; i++) |
74 | { | |
75 | wxString tmp; | |
9687fdee | 76 | tmp.Printf( _T("/History/File%d"), (int)i ); |
925e9792 | 77 | |
6a4e5f03 RR |
78 | wxString res; |
79 | conf->Read( tmp, &res ); | |
925e9792 | 80 | |
6a4e5f03 RR |
81 | if (!res.empty()) |
82 | AddToHistory( res ); | |
83 | } | |
84 | } | |
85 | ||
86 | void MyFrame::MakeHistory() | |
87 | { | |
88 | wxMenuBar *mb = GetMenuBar(); | |
925e9792 | 89 | |
6a4e5f03 RR |
90 | wxASSERT( mb ); |
91 | ||
92 | int max = m_history.GetCount(); | |
93 | if (max > HISTORY_ENTRIES) | |
94 | max = HISTORY_ENTRIES; | |
925e9792 | 95 | |
6a4e5f03 RR |
96 | for (int i = 0; i < max; i++) |
97 | { | |
98 | wxMenu *menu = NULL; | |
99 | mb->FindItem( ID_LAST_1 + i, &menu ); | |
100 | wxASSERT( menu ); | |
925e9792 | 101 | |
6a4e5f03 RR |
102 | wxFileName fname( m_history[(size_t)i] ); |
103 | menu->SetLabel( ID_LAST_1 + i, fname.GetFullName() ); | |
104 | } | |
105 | } | |
106 | ||
107 | void MyFrame::AddToHistory( const wxString &fname ) | |
108 | { | |
109 | // Fill menu with history index | |
110 | int index = m_history.Index( fname ); | |
925e9792 | 111 | |
6a4e5f03 | 112 | if (index != wxNOT_FOUND) |
af870ed8 | 113 | m_history.RemoveAt( (size_t) index ); |
925e9792 | 114 | |
6a4e5f03 | 115 | m_history.Insert( fname, 0 ); |
925e9792 | 116 | |
6a4e5f03 RR |
117 | // Update menu |
118 | MakeHistory(); | |
0106a226 RR |
119 | } |
120 | ||
121 | void MyFrame::CreateMyMenuBar() | |
122 | { | |
123 | wxMenu *file_menu = new wxMenu; | |
9687fdee | 124 | file_menu->Append( ID_ABOUT, _T("About..."), _T("Program info") ); |
0106a226 | 125 | file_menu->AppendSeparator(); |
9687fdee JS |
126 | file_menu->Append( ID_NEW, _T("New..."), _T("New text") ); |
127 | file_menu->Append( ID_OPEN, _T("Open..."), _T("Open text") ); | |
128 | file_menu->Append( ID_SAVE, _T("Save"), _T("Save text") ); | |
129 | file_menu->Append( ID_SAVEAS, _T("Save as..."), _T("Save text as...") ); | |
0106a226 | 130 | file_menu->AppendSeparator(); |
9687fdee | 131 | file_menu->Append( ID_QUIT, _T("Quit..."), _T("Quit program") ); |
6a4e5f03 RR |
132 | |
133 | wxMenu *edit_menu = new wxMenu; | |
9687fdee JS |
134 | edit_menu->Append( ID_COPY, _T("Copy") ); |
135 | edit_menu->Append( ID_CUT, _T("Cut") ); | |
136 | edit_menu->Append( ID_PASTE, _T("Paste") ); | |
6a4e5f03 | 137 | edit_menu->AppendSeparator(); |
9687fdee | 138 | edit_menu->Append( ID_DELETE, _T("Delete") ); |
925e9792 | 139 | |
6a4e5f03 | 140 | wxMenu *history_menu = new wxMenu; |
9687fdee JS |
141 | history_menu->Append( ID_LAST_1, _T("No file.") ); |
142 | history_menu->Append( ID_LAST_2, _T("No file.") ); | |
143 | history_menu->Append( ID_LAST_3, _T("No file.") ); | |
925e9792 | 144 | |
0106a226 | 145 | wxMenuBar *menu_bar = new wxMenuBar(); |
9687fdee JS |
146 | menu_bar->Append( file_menu, _T("&File") ); |
147 | menu_bar->Append( edit_menu, _T("&Edit") ); | |
148 | menu_bar->Append( history_menu, _T("&History") ); | |
925e9792 | 149 | |
0106a226 RR |
150 | SetMenuBar( menu_bar ); |
151 | } | |
152 | ||
dacaa6f1 | 153 | void MyFrame::OnCopy( wxCommandEvent& WXUNUSED(event) ) |
a0760bd3 RR |
154 | { |
155 | } | |
156 | ||
dacaa6f1 | 157 | void MyFrame::OnCut( wxCommandEvent& WXUNUSED(event) ) |
a0760bd3 RR |
158 | { |
159 | } | |
160 | ||
dacaa6f1 | 161 | void MyFrame::OnPaste( wxCommandEvent& WXUNUSED(event) ) |
a0760bd3 RR |
162 | { |
163 | } | |
164 | ||
dacaa6f1 | 165 | void MyFrame::OnDelete( wxCommandEvent& WXUNUSED(event) ) |
a0760bd3 RR |
166 | { |
167 | } | |
168 | ||
6a4e5f03 | 169 | void MyFrame::OnLastFiles( wxCommandEvent &event ) |
a0760bd3 | 170 | { |
6a4e5f03 | 171 | if (!Discard()) return; |
a0760bd3 | 172 | |
6a4e5f03 RR |
173 | if (!m_filename.empty()) |
174 | AddToHistory( m_filename ); | |
925e9792 | 175 | |
6a4e5f03 | 176 | size_t index = event.GetId() - ID_LAST_1; |
925e9792 | 177 | |
dacaa6f1 JS |
178 | if( index < m_history.GetCount() ) |
179 | { | |
8520f137 | 180 | m_filename = m_history[index]; |
925e9792 | 181 | |
8520f137 WS |
182 | m_text->Clear(); |
183 | m_text->LoadFile( m_filename ); | |
925e9792 | 184 | |
8520f137 WS |
185 | #if wxUSE_STATUSBAR |
186 | SetStatusText( m_filename ); | |
187 | #endif // wxUSE_STATUSBAR | |
dacaa6f1 JS |
188 | } |
189 | else | |
190 | { | |
191 | wxMessageBox( | |
192 | _T("This entry is empty. It should be filled once you will start opening."), | |
193 | _T("Empty entry"), | |
194 | wxOK | wxICON_INFORMATION, | |
195 | this | |
196 | ); | |
197 | } | |
a0760bd3 RR |
198 | } |
199 | ||
dacaa6f1 | 200 | void MyFrame::OnNew( wxCommandEvent& WXUNUSED(event) ) |
0106a226 | 201 | { |
a0760bd3 RR |
202 | if (!Discard()) return; |
203 | ||
0106a226 | 204 | m_text->Clear(); |
925e9792 | 205 | |
6a4e5f03 RR |
206 | if (!m_filename.empty()) |
207 | AddToHistory( m_filename ); | |
925e9792 | 208 | |
6a4e5f03 | 209 | m_filename = wxEmptyString; |
925e9792 | 210 | |
8520f137 | 211 | #if wxUSE_STATUSBAR |
71307412 | 212 | SetStatusText( wxEmptyString ); |
8520f137 | 213 | #endif // wxUSE_STATUSBAR |
0106a226 RR |
214 | } |
215 | ||
dacaa6f1 | 216 | void MyFrame::OnOpen( wxCommandEvent& WXUNUSED(event) ) |
0106a226 | 217 | { |
71307412 | 218 | #if wxUSE_FILEDLG |
6a4e5f03 RR |
219 | if (!Discard()) return; |
220 | ||
71307412 | 221 | wxFileDialog dialog( this, _T("Open text"), wxEmptyString, wxEmptyString, |
9687fdee | 222 | _T("Text file (*.txt)|*.txt|Any file (*)|*"), |
ff3e84ff | 223 | wxFD_OPEN|wxFD_FILE_MUST_EXIST ); |
40de795f RR |
224 | if (dialog.ShowModal() == wxID_OK) |
225 | { | |
226 | m_text->Clear(); | |
227 | ||
228 | #ifdef __WXX11__ | |
95a16b12 MW |
229 | // requires wxUSE_UNIV_TEXTCTRL to be set to 0 |
230 | #if 0 | |
40de795f | 231 | wxFileName fname( dialog.GetPath() ); |
9687fdee JS |
232 | if ((fname.GetExt() == _T("cpp")) || |
233 | (fname.GetExt() == _T("c")) || | |
234 | (fname.GetExt() == _T("h")) || | |
235 | (fname.GetExt() == _T("cxx")) || | |
236 | (fname.GetExt() == _T("hxx"))) | |
40de795f RR |
237 | { |
238 | m_text->SetLanguage( wxSOURCE_LANG_CPP ); | |
239 | } | |
240 | else | |
9687fdee | 241 | if (fname.GetExt() == _T("py")) |
40de795f RR |
242 | { |
243 | m_text->SetLanguage( wxSOURCE_LANG_PYTHON ); | |
244 | } | |
245 | else | |
9687fdee JS |
246 | if ((fname.GetExt() == _T("pl")) || |
247 | (fname.GetExt() == _T("pm"))) | |
40de795f RR |
248 | { |
249 | m_text->SetLanguage( wxSOURCE_LANG_PYTHON ); | |
250 | } | |
251 | else | |
252 | { | |
253 | m_text->SetLanguage( wxSOURCE_LANG_NONE ); | |
254 | } | |
95a16b12 | 255 | #endif |
40de795f RR |
256 | #endif |
257 | ||
6a4e5f03 RR |
258 | m_filename = dialog.GetPath(); |
259 | m_text->LoadFile( m_filename ); | |
925e9792 | 260 | |
8520f137 | 261 | #if wxUSE_STATUSBAR |
6a4e5f03 | 262 | SetStatusText( m_filename ); |
8520f137 | 263 | #endif // wxUSE_STATUSBAR |
40de795f | 264 | } |
71307412 | 265 | #endif // wxUSE_FILEDLG |
0106a226 RR |
266 | } |
267 | ||
dacaa6f1 | 268 | void MyFrame::OnSave( wxCommandEvent& WXUNUSED(event) ) |
0106a226 | 269 | { |
a7150197 | 270 | Save(); |
0106a226 RR |
271 | } |
272 | ||
dacaa6f1 | 273 | void MyFrame::OnSaveAs( wxCommandEvent& WXUNUSED(event) ) |
0106a226 | 274 | { |
71307412 WS |
275 | #if wxUSE_FILEDLG |
276 | wxFileDialog dialog( this, _T("Open text"), wxEmptyString, wxEmptyString, | |
9687fdee | 277 | _T("Text file (*.txt)|*.txt|Any file (*)|*"), |
ff3e84ff | 278 | wxFD_SAVE|wxFD_OVERWRITE_PROMPT ); |
6a4e5f03 RR |
279 | if (dialog.ShowModal() == wxID_OK) |
280 | { | |
281 | m_filename = dialog.GetPath(); | |
282 | m_text->SaveFile( m_filename ); | |
925e9792 | 283 | |
8520f137 | 284 | #if wxUSE_STATUSBAR |
6a4e5f03 | 285 | SetStatusText( m_filename ); |
8520f137 | 286 | #endif // wxUSE_STATUSBAR |
6a4e5f03 | 287 | } |
71307412 | 288 | #endif // wxUSE_FILEDLG |
0106a226 RR |
289 | } |
290 | ||
dacaa6f1 | 291 | void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) ) |
0106a226 | 292 | { |
9687fdee JS |
293 | wxMessageDialog dialog( this, _T("Welcome to wxEdit\n(C)opyright Robert Roebling"), |
294 | _T("About wxEdit"), wxOK|wxICON_INFORMATION ); | |
0106a226 RR |
295 | dialog.ShowModal(); |
296 | } | |
297 | ||
dacaa6f1 | 298 | void MyFrame::OnQuit( wxCommandEvent& WXUNUSED(event) ) |
0106a226 | 299 | { |
af870ed8 | 300 | Close( true ); |
0106a226 RR |
301 | } |
302 | ||
a0760bd3 RR |
303 | bool MyFrame::Save() |
304 | { | |
a7150197 | 305 | wxCommandEvent event; |
925e9792 | 306 | |
a7150197 JS |
307 | if (m_filename.empty()) |
308 | OnSaveAs( event ); | |
309 | else | |
310 | m_text->SaveFile( m_filename ); | |
925e9792 | 311 | |
af870ed8 | 312 | return true; |
a0760bd3 RR |
313 | } |
314 | ||
315 | bool MyFrame::Discard() | |
0106a226 | 316 | { |
a0760bd3 RR |
317 | if (m_text->IsModified()) |
318 | { | |
9687fdee JS |
319 | wxMessageDialog dialog( this, _T("Text has been\nmodified! Save?"), |
320 | _T("wxEdit"), wxYES_NO|wxCANCEL|wxICON_EXCLAMATION ); | |
925e9792 | 321 | |
a0760bd3 | 322 | int ret = dialog.ShowModal(); |
925e9792 | 323 | |
a0760bd3 | 324 | if (ret == wxID_CANCEL) |
af870ed8 | 325 | return false; |
925e9792 | 326 | |
a0760bd3 RR |
327 | if (ret == wxID_YES) |
328 | { | |
329 | if (!Save()) | |
af870ed8 | 330 | return false; |
a0760bd3 RR |
331 | } |
332 | } | |
925e9792 | 333 | |
af870ed8 | 334 | return true; |
a0760bd3 RR |
335 | } |
336 | ||
6a4e5f03 RR |
337 | void MyFrame::OnUpdateUI( wxUpdateUIEvent &event ) |
338 | { | |
339 | switch (event.GetId()) | |
340 | { | |
341 | case ID_COPY: | |
af870ed8 | 342 | event.Enable( false ); |
6a4e5f03 RR |
343 | break; |
344 | case ID_CUT: | |
af870ed8 | 345 | event.Enable( false ); |
6a4e5f03 RR |
346 | break; |
347 | case ID_PASTE: | |
af870ed8 | 348 | event.Enable( false ); |
6a4e5f03 RR |
349 | break; |
350 | case ID_DELETE: | |
3f2b72b2 | 351 | #ifdef __WXUNIVERSAL__ |
6a4e5f03 | 352 | event.Enable( m_text->HasSelection() ); |
3f2b72b2 JS |
353 | #else |
354 | { | |
355 | long selFrom, selTo; | |
356 | m_text->GetSelection(& selFrom, & selTo); | |
357 | event.Enable( selFrom != selTo ); | |
358 | } | |
925e9792 | 359 | #endif |
6a4e5f03 RR |
360 | break; |
361 | default: | |
362 | break; | |
363 | } | |
364 | } | |
365 | ||
dacaa6f1 | 366 | void MyFrame::OnCloseWindow( wxCloseEvent& WXUNUSED(event) ) |
a0760bd3 | 367 | { |
6a4e5f03 | 368 | // Save changes? |
925e9792 WS |
369 | if (!Discard()) return; |
370 | ||
6a4e5f03 RR |
371 | // Add current to history |
372 | if (!m_filename.empty()) | |
373 | AddToHistory( m_filename ); | |
a0760bd3 | 374 | |
925e9792 | 375 | // Write .ini file |
6a4e5f03 | 376 | wxConfig *conf = (wxConfig*) wxConfig::Get(); |
925e9792 | 377 | |
6a4e5f03 RR |
378 | int max = HISTORY_ENTRIES; |
379 | if (m_history.GetCount() < (size_t)max) | |
380 | max = m_history.GetCount(); | |
925e9792 | 381 | |
9687fdee | 382 | conf->Write( _T("/History/Count"), max ); |
925e9792 | 383 | |
6a4e5f03 RR |
384 | for (int i = 0; i < max; i++) |
385 | { | |
386 | wxString tmp; | |
9687fdee | 387 | tmp.Printf( _T("/History/File%d"), (int)i ); |
925e9792 | 388 | |
6a4e5f03 RR |
389 | conf->Write( tmp, m_history[(size_t)i] ); |
390 | } | |
925e9792 | 391 | |
6a4e5f03 RR |
392 | // Flush and delete config |
393 | delete wxConfig::Set( NULL ); | |
394 | ||
395 | // Finally destroy window and quit | |
0106a226 RR |
396 | Destroy(); |
397 | } | |
398 | ||
399 | //------------------------------------------------------------------------------ | |
400 | // MyApp | |
401 | //------------------------------------------------------------------------------ | |
402 | ||
403 | IMPLEMENT_APP(MyApp) | |
404 | ||
0106a226 RR |
405 | bool MyApp::OnInit() |
406 | { | |
9687fdee JS |
407 | SetVendorName(_T("Free world")); |
408 | SetAppName(_T("wxEdit")); | |
925e9792 | 409 | |
af870ed8 WS |
410 | MyFrame *frame = new MyFrame( NULL, wxID_ANY, _T("wxEdit"), wxPoint(20,20), wxSize(500,340) ); |
411 | frame->Show( true ); | |
925e9792 | 412 | |
af870ed8 | 413 | return true; |
0106a226 RR |
414 | } |
415 | ||
416 | int MyApp::OnExit() | |
417 | { | |
418 | return 0; | |
419 | } |