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