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