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