]> git.saurik.com Git - wxWidgets.git/blame - samples/mobile/wxedit/wxedit.cpp
wxBase 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
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);
6a4e5f03 62 SetStatusText( "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.
0106a226 67 m_text = new wxTextCtrl( this, -1, "", 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;
73 conf->Read( "/History/Count", &entries );
74
75 for (int i = 0; i < entries; i++)
76 {
77 wxString tmp;
78 tmp.Printf( "/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
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;
126 file_menu->Append( ID_ABOUT, "About...", "Program info" );
127 file_menu->AppendSeparator();
128 file_menu->Append( ID_NEW, "New...", "New text" );
129 file_menu->Append( ID_OPEN, "Open...", "Open text" );
130 file_menu->Append( ID_SAVE, "Save", "Save text" );
131 file_menu->Append( ID_SAVEAS, "Save as...", "Save text as..." );
132 file_menu->AppendSeparator();
133 file_menu->Append( ID_QUIT, "Quit...", "Quit program" );
6a4e5f03
RR
134
135 wxMenu *edit_menu = new wxMenu;
136 edit_menu->Append( ID_COPY, "Copy" );
137 edit_menu->Append( ID_CUT, "Cut" );
138 edit_menu->Append( ID_PASTE, "Paste" );
139 edit_menu->AppendSeparator();
140 edit_menu->Append( ID_DELETE, "Delete" );
141
142 wxMenu *history_menu = new wxMenu;
143 history_menu->Append( ID_LAST_1, "No file." );
144 history_menu->Append( ID_LAST_2, "No file." );
145 history_menu->Append( ID_LAST_3, "No file." );
0106a226
RR
146
147 wxMenuBar *menu_bar = new wxMenuBar();
6a4e5f03
RR
148 menu_bar->Append( file_menu, "&File" );
149 menu_bar->Append( edit_menu, "&Edit" );
150 menu_bar->Append( history_menu, "&History" );
0106a226
RR
151
152 SetMenuBar( menu_bar );
153}
154
a0760bd3
RR
155void MyFrame::OnCopy( wxCommandEvent &event )
156{
157}
158
159void MyFrame::OnCut( wxCommandEvent &event )
160{
161}
162
163void MyFrame::OnPaste( wxCommandEvent &event )
164{
165}
166
167void MyFrame::OnDelete( wxCommandEvent &event )
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
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 );
a0760bd3
RR
188}
189
0106a226
RR
190void MyFrame::OnNew( wxCommandEvent &event )
191{
a0760bd3
RR
192 if (!Discard()) return;
193
0106a226 194 m_text->Clear();
6a4e5f03
RR
195
196 if (!m_filename.empty())
197 AddToHistory( m_filename );
198
199 m_filename = wxEmptyString;
200
201 SetStatusText( "" );
0106a226
RR
202}
203
204void MyFrame::OnOpen( wxCommandEvent &event )
205{
6a4e5f03
RR
206 if (!Discard()) return;
207
0106a226
RR
208 wxFileDialog dialog( this, "Open text", "", "",
209 "Text file (*.txt)|*.txt|"
210 "Any file (*)|*",
211 wxOPEN|wxFILE_MUST_EXIST );
40de795f
RR
212 if (dialog.ShowModal() == wxID_OK)
213 {
214 m_text->Clear();
215
216#ifdef __WXX11__
217 wxFileName fname( dialog.GetPath() );
218 if ((fname.GetExt() == "cpp") ||
219 (fname.GetExt() == "c") ||
220 (fname.GetExt() == "h") ||
221 (fname.GetExt() == "cxx") ||
222 (fname.GetExt() == "hxx"))
223 {
224 m_text->SetLanguage( wxSOURCE_LANG_CPP );
225 }
226 else
227 if (fname.GetExt() == "py")
228 {
229 m_text->SetLanguage( wxSOURCE_LANG_PYTHON );
230 }
231 else
232 if ((fname.GetExt() == "pl") ||
233 (fname.GetExt() == "pm"))
234 {
235 m_text->SetLanguage( wxSOURCE_LANG_PYTHON );
236 }
237 else
238 {
239 m_text->SetLanguage( wxSOURCE_LANG_NONE );
240 }
241#endif
242
6a4e5f03
RR
243 m_filename = dialog.GetPath();
244 m_text->LoadFile( m_filename );
245
246 SetStatusText( m_filename );
40de795f 247 }
0106a226
RR
248}
249
250void MyFrame::OnSave( wxCommandEvent &event )
251{
a7150197 252 Save();
0106a226
RR
253}
254
255void MyFrame::OnSaveAs( wxCommandEvent &event )
256{
6a4e5f03
RR
257 wxFileDialog dialog( this, "Open text", "", "",
258 "Text file (*.txt)|*.txt|"
259 "Any file (*)|*",
260 wxSAVE|wxOVERWRITE_PROMPT );
261 if (dialog.ShowModal() == wxID_OK)
262 {
263 m_filename = dialog.GetPath();
264 m_text->SaveFile( m_filename );
265
266 SetStatusText( m_filename );
267 }
0106a226
RR
268}
269
270void MyFrame::OnAbout( wxCommandEvent &event )
271{
a0760bd3
RR
272 wxMessageDialog dialog( this, "Welcome to wxEdit\n(C)opyright Robert Roebling",
273 "About wxEdit", wxOK|wxICON_INFORMATION );
0106a226
RR
274 dialog.ShowModal();
275}
276
277void MyFrame::OnQuit( wxCommandEvent &event )
278{
279 Close( TRUE );
280}
281
a0760bd3
RR
282bool MyFrame::Save()
283{
a7150197
JS
284 wxCommandEvent event;
285
286 if (m_filename.empty())
287 OnSaveAs( event );
288 else
289 m_text->SaveFile( m_filename );
a0760bd3
RR
290
291 return TRUE;
292}
293
294bool MyFrame::Discard()
0106a226 295{
a0760bd3
RR
296 if (m_text->IsModified())
297 {
6a4e5f03 298 wxMessageDialog dialog( this, "Text has been\nmodified! Save?",
a0760bd3
RR
299 "wxEdit", wxYES_NO|wxCANCEL|wxICON_EXCLAMATION );
300
301 int ret = dialog.ShowModal();
302
303 if (ret == wxID_CANCEL)
304 return FALSE;
305
306 if (ret == wxID_YES)
307 {
308 if (!Save())
309 return FALSE;
310 }
311 }
0106a226 312
a0760bd3
RR
313 return TRUE;
314}
315
6a4e5f03
RR
316void MyFrame::OnUpdateUI( wxUpdateUIEvent &event )
317{
318 switch (event.GetId())
319 {
320 case ID_COPY:
321 event.Enable( FALSE );
322 break;
323 case ID_CUT:
324 event.Enable( FALSE );
325 break;
326 case ID_PASTE:
327 event.Enable( FALSE );
328 break;
329 case ID_DELETE:
3f2b72b2 330#ifdef __WXUNIVERSAL__
6a4e5f03 331 event.Enable( m_text->HasSelection() );
3f2b72b2
JS
332#else
333 {
334 long selFrom, selTo;
335 m_text->GetSelection(& selFrom, & selTo);
336 event.Enable( selFrom != selTo );
337 }
338#endif
6a4e5f03
RR
339 break;
340 default:
341 break;
342 }
343}
344
a0760bd3
RR
345void MyFrame::OnCloseWindow( wxCloseEvent &event )
346{
6a4e5f03 347 // Save changes?
a0760bd3 348 if (!Discard()) return;
6a4e5f03
RR
349
350 // Add current to history
351 if (!m_filename.empty())
352 AddToHistory( m_filename );
a0760bd3 353
6a4e5f03
RR
354 // Write .ini file
355 wxConfig *conf = (wxConfig*) wxConfig::Get();
356
357 int max = HISTORY_ENTRIES;
358 if (m_history.GetCount() < (size_t)max)
359 max = m_history.GetCount();
360
361 conf->Write( "/History/Count", max );
362
363 for (int i = 0; i < max; i++)
364 {
365 wxString tmp;
366 tmp.Printf( "/History/File%d", (int)i );
367
368 conf->Write( tmp, m_history[(size_t)i] );
369 }
370
371 // Flush and delete config
372 delete wxConfig::Set( NULL );
373
374 // Finally destroy window and quit
0106a226
RR
375 Destroy();
376}
377
378//------------------------------------------------------------------------------
379// MyApp
380//------------------------------------------------------------------------------
381
382IMPLEMENT_APP(MyApp)
383
384MyApp::MyApp()
385{
386}
387
388bool MyApp::OnInit()
389{
6a4e5f03
RR
390 SetVendorName("Free world");
391 SetAppName("wxEdit");
392
a0760bd3 393 MyFrame *frame = new MyFrame( NULL, -1, "wxEdit", wxPoint(20,20), wxSize(500,340) );
0106a226
RR
394 frame->Show( TRUE );
395
396 return TRUE;
397}
398
399int MyApp::OnExit()
400{
401 return 0;
402}
403