]> git.saurik.com Git - wxWidgets.git/blame - samples/mobile/wxedit/wxedit.cpp
reSWIGged
[wxWidgets.git] / samples / mobile / wxedit / wxedit.cpp
CommitLineData
0106a226
RR
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
40de795f 19#include "wx/filename.h"
6a4e5f03 20#include "wx/config.h"
40de795f 21
0106a226
RR
22// Include private headers
23#include "wxedit.h"
24
6a4e5f03
RR
25//------------------------------------------------------------------------------
26// constants
27//------------------------------------------------------------------------------
28
29#define HISTORY_ENTRIES 3
30
0106a226
RR
31//------------------------------------------------------------------------------
32// MyFrame
33//------------------------------------------------------------------------------
34
35BEGIN_EVENT_TABLE(MyFrame,wxFrame)
36 EVT_MENU(ID_ABOUT, MyFrame::OnAbout)
a0760bd3 37
0106a226
RR
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)
a0760bd3
RR
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
6a4e5f03 49 EVT_MENU_RANGE(ID_LAST_1, ID_LAST_3, MyFrame::OnLastFiles)
a0760bd3 50
0106a226 51 EVT_CLOSE(MyFrame::OnCloseWindow)
6a4e5f03 52 EVT_UPDATE_UI(-1,MyFrame::OnUpdateUI)
0106a226
RR
53END_EVENT_TABLE()
54
55MyFrame::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{
6a4e5f03 59 // Create menu and status bar.
0106a226 60 CreateMyMenuBar();
0106a226 61 CreateStatusBar(1);
9687fdee 62 SetStatusText( _T("Welcome to wxEdit!") );
0106a226 63
6a4e5f03
RR
64 // Create edit control. Since it is the only
65 // control in the frame, it will be resized
66 // to file it out.
9687fdee 67 m_text = new wxTextCtrl( this, -1, _T(""), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
6a4e5f03
RR
68
69 // Read .ini file for file history etc.
70 wxConfig *conf = (wxConfig*) wxConfig::Get();
71
72 int entries = 0;
9687fdee 73 conf->Read( _T("/History/Count"), &entries );
6a4e5f03
RR
74
75 for (int i = 0; i < entries; i++)
76 {
77 wxString tmp;
9687fdee 78 tmp.Printf( _T("/History/File%d"), (int)i );
6a4e5f03
RR
79
80 wxString res;
81 conf->Read( tmp, &res );
82
83 if (!res.empty())
84 AddToHistory( res );
85 }
86}
87
88void 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
109void 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();
0106a226
RR
121}
122
123void MyFrame::CreateMyMenuBar()
124{
125 wxMenu *file_menu = new wxMenu;
9687fdee 126 file_menu->Append( ID_ABOUT, _T("About..."), _T("Program info") );
0106a226 127 file_menu->AppendSeparator();
9687fdee
JS
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...") );
0106a226 132 file_menu->AppendSeparator();
9687fdee 133 file_menu->Append( ID_QUIT, _T("Quit..."), _T("Quit program") );
6a4e5f03
RR
134
135 wxMenu *edit_menu = new wxMenu;
9687fdee
JS
136 edit_menu->Append( ID_COPY, _T("Copy") );
137 edit_menu->Append( ID_CUT, _T("Cut") );
138 edit_menu->Append( ID_PASTE, _T("Paste") );
6a4e5f03 139 edit_menu->AppendSeparator();
9687fdee 140 edit_menu->Append( ID_DELETE, _T("Delete") );
6a4e5f03
RR
141
142 wxMenu *history_menu = new wxMenu;
9687fdee
JS
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.") );
0106a226
RR
146
147 wxMenuBar *menu_bar = new wxMenuBar();
9687fdee
JS
148 menu_bar->Append( file_menu, _T("&File") );
149 menu_bar->Append( edit_menu, _T("&Edit") );
150 menu_bar->Append( history_menu, _T("&History") );
0106a226
RR
151
152 SetMenuBar( menu_bar );
153}
154
dacaa6f1 155void MyFrame::OnCopy( wxCommandEvent& WXUNUSED(event) )
a0760bd3
RR
156{
157}
158
dacaa6f1 159void MyFrame::OnCut( wxCommandEvent& WXUNUSED(event) )
a0760bd3
RR
160{
161}
162
dacaa6f1 163void MyFrame::OnPaste( wxCommandEvent& WXUNUSED(event) )
a0760bd3
RR
164{
165}
166
dacaa6f1 167void MyFrame::OnDelete( wxCommandEvent& WXUNUSED(event) )
a0760bd3
RR
168{
169}
170
6a4e5f03 171void MyFrame::OnLastFiles( wxCommandEvent &event )
a0760bd3 172{
6a4e5f03 173 if (!Discard()) return;
a0760bd3 174
6a4e5f03
RR
175 if (!m_filename.empty())
176 AddToHistory( m_filename );
177
178 size_t index = event.GetId() - ID_LAST_1;
179
dacaa6f1
JS
180 if( index < m_history.GetCount() )
181 {
6a4e5f03
RR
182 m_filename = m_history[index];
183
184 m_text->Clear();
185 m_text->LoadFile( m_filename );
186
187 SetStatusText( m_filename );
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 200void MyFrame::OnNew( wxCommandEvent& WXUNUSED(event) )
0106a226 201{
a0760bd3
RR
202 if (!Discard()) return;
203
0106a226 204 m_text->Clear();
6a4e5f03
RR
205
206 if (!m_filename.empty())
207 AddToHistory( m_filename );
208
209 m_filename = wxEmptyString;
210
9687fdee 211 SetStatusText( _T("") );
0106a226
RR
212}
213
dacaa6f1 214void MyFrame::OnOpen( wxCommandEvent& WXUNUSED(event) )
0106a226 215{
6a4e5f03
RR
216 if (!Discard()) return;
217
9687fdee
JS
218 wxFileDialog dialog( this, _T("Open text"), _T(""), _T(""),
219 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
0106a226 220 wxOPEN|wxFILE_MUST_EXIST );
40de795f
RR
221 if (dialog.ShowModal() == wxID_OK)
222 {
223 m_text->Clear();
224
225#ifdef __WXX11__
226 wxFileName fname( dialog.GetPath() );
9687fdee
JS
227 if ((fname.GetExt() == _T("cpp")) ||
228 (fname.GetExt() == _T("c")) ||
229 (fname.GetExt() == _T("h")) ||
230 (fname.GetExt() == _T("cxx")) ||
231 (fname.GetExt() == _T("hxx")))
40de795f
RR
232 {
233 m_text->SetLanguage( wxSOURCE_LANG_CPP );
234 }
235 else
9687fdee 236 if (fname.GetExt() == _T("py"))
40de795f
RR
237 {
238 m_text->SetLanguage( wxSOURCE_LANG_PYTHON );
239 }
240 else
9687fdee
JS
241 if ((fname.GetExt() == _T("pl")) ||
242 (fname.GetExt() == _T("pm")))
40de795f
RR
243 {
244 m_text->SetLanguage( wxSOURCE_LANG_PYTHON );
245 }
246 else
247 {
248 m_text->SetLanguage( wxSOURCE_LANG_NONE );
249 }
250#endif
251
6a4e5f03
RR
252 m_filename = dialog.GetPath();
253 m_text->LoadFile( m_filename );
254
255 SetStatusText( m_filename );
40de795f 256 }
0106a226
RR
257}
258
dacaa6f1 259void MyFrame::OnSave( wxCommandEvent& WXUNUSED(event) )
0106a226 260{
a7150197 261 Save();
0106a226
RR
262}
263
dacaa6f1 264void MyFrame::OnSaveAs( wxCommandEvent& WXUNUSED(event) )
0106a226 265{
9687fdee
JS
266 wxFileDialog dialog( this, _T("Open text"), _T(""), _T(""),
267 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
6a4e5f03
RR
268 wxSAVE|wxOVERWRITE_PROMPT );
269 if (dialog.ShowModal() == wxID_OK)
270 {
271 m_filename = dialog.GetPath();
272 m_text->SaveFile( m_filename );
273
274 SetStatusText( m_filename );
275 }
0106a226
RR
276}
277
dacaa6f1 278void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
0106a226 279{
9687fdee
JS
280 wxMessageDialog dialog( this, _T("Welcome to wxEdit\n(C)opyright Robert Roebling"),
281 _T("About wxEdit"), wxOK|wxICON_INFORMATION );
0106a226
RR
282 dialog.ShowModal();
283}
284
dacaa6f1 285void MyFrame::OnQuit( wxCommandEvent& WXUNUSED(event) )
0106a226
RR
286{
287 Close( TRUE );
288}
289
a0760bd3
RR
290bool MyFrame::Save()
291{
a7150197
JS
292 wxCommandEvent event;
293
294 if (m_filename.empty())
295 OnSaveAs( event );
296 else
297 m_text->SaveFile( m_filename );
a0760bd3
RR
298
299 return TRUE;
300}
301
302bool MyFrame::Discard()
0106a226 303{
a0760bd3
RR
304 if (m_text->IsModified())
305 {
9687fdee
JS
306 wxMessageDialog dialog( this, _T("Text has been\nmodified! Save?"),
307 _T("wxEdit"), wxYES_NO|wxCANCEL|wxICON_EXCLAMATION );
a0760bd3
RR
308
309 int ret = dialog.ShowModal();
310
311 if (ret == wxID_CANCEL)
312 return FALSE;
313
314 if (ret == wxID_YES)
315 {
316 if (!Save())
317 return FALSE;
318 }
319 }
0106a226 320
a0760bd3
RR
321 return TRUE;
322}
323
6a4e5f03
RR
324void MyFrame::OnUpdateUI( wxUpdateUIEvent &event )
325{
326 switch (event.GetId())
327 {
328 case ID_COPY:
329 event.Enable( FALSE );
330 break;
331 case ID_CUT:
332 event.Enable( FALSE );
333 break;
334 case ID_PASTE:
335 event.Enable( FALSE );
336 break;
337 case ID_DELETE:
3f2b72b2 338#ifdef __WXUNIVERSAL__
6a4e5f03 339 event.Enable( m_text->HasSelection() );
3f2b72b2
JS
340#else
341 {
342 long selFrom, selTo;
343 m_text->GetSelection(& selFrom, & selTo);
344 event.Enable( selFrom != selTo );
345 }
346#endif
6a4e5f03
RR
347 break;
348 default:
349 break;
350 }
351}
352
dacaa6f1 353void MyFrame::OnCloseWindow( wxCloseEvent& WXUNUSED(event) )
a0760bd3 354{
6a4e5f03 355 // Save changes?
a0760bd3 356 if (!Discard()) return;
6a4e5f03
RR
357
358 // Add current to history
359 if (!m_filename.empty())
360 AddToHistory( m_filename );
a0760bd3 361
6a4e5f03
RR
362 // Write .ini file
363 wxConfig *conf = (wxConfig*) wxConfig::Get();
364
365 int max = HISTORY_ENTRIES;
366 if (m_history.GetCount() < (size_t)max)
367 max = m_history.GetCount();
368
9687fdee 369 conf->Write( _T("/History/Count"), max );
6a4e5f03
RR
370
371 for (int i = 0; i < max; i++)
372 {
373 wxString tmp;
9687fdee 374 tmp.Printf( _T("/History/File%d"), (int)i );
6a4e5f03
RR
375
376 conf->Write( tmp, m_history[(size_t)i] );
377 }
378
379 // Flush and delete config
380 delete wxConfig::Set( NULL );
381
382 // Finally destroy window and quit
0106a226
RR
383 Destroy();
384}
385
386//------------------------------------------------------------------------------
387// MyApp
388//------------------------------------------------------------------------------
389
390IMPLEMENT_APP(MyApp)
391
392MyApp::MyApp()
393{
394}
395
396bool MyApp::OnInit()
397{
9687fdee
JS
398 SetVendorName(_T("Free world"));
399 SetAppName(_T("wxEdit"));
6a4e5f03 400
9687fdee 401 MyFrame *frame = new MyFrame( NULL, -1, _T("wxEdit"), wxPoint(20,20), wxSize(500,340) );
0106a226
RR
402 frame->Show( TRUE );
403
404 return TRUE;
405}
406
407int MyApp::OnExit()
408{
409 return 0;
410}
411