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