]>
git.saurik.com Git - wxWidgets.git/blob - samples/mobile/wxedit/wxedit.cpp
fa4a2d3ee3d6571522105b9df82bfd46e5fc28a2
1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Robert Roebling
6 /////////////////////////////////////////////////////////////////////////////
9 #pragma implementation "wxedit.cpp"
12 // For compilers that support precompilation
13 #include "wx/wxprec.h"
19 #include "wx/filename.h"
20 #include "wx/config.h"
22 // Include private headers
25 //------------------------------------------------------------------------------
27 //------------------------------------------------------------------------------
29 #define HISTORY_ENTRIES 3
31 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
35 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
36 EVT_MENU(ID_ABOUT
, MyFrame::OnAbout
)
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
)
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
)
49 EVT_MENU_RANGE(ID_LAST_1
, ID_LAST_3
, MyFrame::OnLastFiles
)
51 EVT_CLOSE(MyFrame::OnCloseWindow
)
52 EVT_UPDATE_UI(wxID_ANY
,MyFrame::OnUpdateUI
)
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
)
59 // Create menu and status bar.
62 SetStatusText( _T("Welcome to wxEdit!") );
64 // Create edit control. Since it is the only
65 // control in the frame, it will be resized
67 m_text
= new wxTextCtrl( this, wxID_ANY
, _T(""), wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
69 // Read .ini file for file history etc.
70 wxConfig
*conf
= (wxConfig
*) wxConfig::Get();
73 conf
->Read( _T("/History/Count"), &entries
);
75 for (int i
= 0; i
< entries
; i
++)
78 tmp
.Printf( _T("/History/File%d"), (int)i
);
81 conf
->Read( tmp
, &res
);
88 void MyFrame::MakeHistory()
90 wxMenuBar
*mb
= GetMenuBar();
94 int max
= m_history
.GetCount();
95 if (max
> HISTORY_ENTRIES
)
96 max
= HISTORY_ENTRIES
;
98 for (int i
= 0; i
< max
; i
++)
101 mb
->FindItem( ID_LAST_1
+ i
, &menu
);
104 wxFileName
fname( m_history
[(size_t)i
] );
105 menu
->SetLabel( ID_LAST_1
+ i
, fname
.GetFullName() );
109 void MyFrame::AddToHistory( const wxString
&fname
)
111 // Fill menu with history index
112 int index
= m_history
.Index( fname
);
114 if (index
!= wxNOT_FOUND
)
115 m_history
.RemoveAt( (size_t) index
);
117 m_history
.Insert( fname
, 0 );
123 void MyFrame::CreateMyMenuBar()
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") );
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") );
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.") );
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") );
152 SetMenuBar( menu_bar
);
155 void MyFrame::OnCopy( wxCommandEvent
& WXUNUSED(event
) )
159 void MyFrame::OnCut( wxCommandEvent
& WXUNUSED(event
) )
163 void MyFrame::OnPaste( wxCommandEvent
& WXUNUSED(event
) )
167 void MyFrame::OnDelete( wxCommandEvent
& WXUNUSED(event
) )
171 void MyFrame::OnLastFiles( wxCommandEvent
&event
)
173 if (!Discard()) return;
175 if (!m_filename
.empty())
176 AddToHistory( m_filename
);
178 size_t index
= event
.GetId() - ID_LAST_1
;
180 if( index
< m_history
.GetCount() )
182 m_filename
= m_history
[index
];
185 m_text
->LoadFile( m_filename
);
187 SetStatusText( m_filename
);
192 _T("This entry is empty. It should be filled once you will start opening."),
194 wxOK
| wxICON_INFORMATION
,
200 void MyFrame::OnNew( wxCommandEvent
& WXUNUSED(event
) )
202 if (!Discard()) return;
206 if (!m_filename
.empty())
207 AddToHistory( m_filename
);
209 m_filename
= wxEmptyString
;
211 SetStatusText( _T("") );
214 void MyFrame::OnOpen( wxCommandEvent
& WXUNUSED(event
) )
216 if (!Discard()) return;
218 wxFileDialog
dialog( this, _T("Open text"), _T(""), _T(""),
219 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
220 wxOPEN
|wxFILE_MUST_EXIST
);
221 if (dialog
.ShowModal() == wxID_OK
)
226 wxFileName
fname( dialog
.GetPath() );
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")))
233 m_text
->SetLanguage( wxSOURCE_LANG_CPP
);
236 if (fname
.GetExt() == _T("py"))
238 m_text
->SetLanguage( wxSOURCE_LANG_PYTHON
);
241 if ((fname
.GetExt() == _T("pl")) ||
242 (fname
.GetExt() == _T("pm")))
244 m_text
->SetLanguage( wxSOURCE_LANG_PYTHON
);
248 m_text
->SetLanguage( wxSOURCE_LANG_NONE
);
252 m_filename
= dialog
.GetPath();
253 m_text
->LoadFile( m_filename
);
255 SetStatusText( m_filename
);
259 void MyFrame::OnSave( wxCommandEvent
& WXUNUSED(event
) )
264 void MyFrame::OnSaveAs( wxCommandEvent
& WXUNUSED(event
) )
266 wxFileDialog
dialog( this, _T("Open text"), _T(""), _T(""),
267 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
268 wxSAVE
|wxOVERWRITE_PROMPT
);
269 if (dialog
.ShowModal() == wxID_OK
)
271 m_filename
= dialog
.GetPath();
272 m_text
->SaveFile( m_filename
);
274 SetStatusText( m_filename
);
278 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
280 wxMessageDialog
dialog( this, _T("Welcome to wxEdit\n(C)opyright Robert Roebling"),
281 _T("About wxEdit"), wxOK
|wxICON_INFORMATION
);
285 void MyFrame::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
292 wxCommandEvent event
;
294 if (m_filename
.empty())
297 m_text
->SaveFile( m_filename
);
302 bool MyFrame::Discard()
304 if (m_text
->IsModified())
306 wxMessageDialog
dialog( this, _T("Text has been\nmodified! Save?"),
307 _T("wxEdit"), wxYES_NO
|wxCANCEL
|wxICON_EXCLAMATION
);
309 int ret
= dialog
.ShowModal();
311 if (ret
== wxID_CANCEL
)
324 void MyFrame::OnUpdateUI( wxUpdateUIEvent
&event
)
326 switch (event
.GetId())
329 event
.Enable( false );
332 event
.Enable( false );
335 event
.Enable( false );
338 #ifdef __WXUNIVERSAL__
339 event
.Enable( m_text
->HasSelection() );
343 m_text
->GetSelection(& selFrom
, & selTo
);
344 event
.Enable( selFrom
!= selTo
);
353 void MyFrame::OnCloseWindow( wxCloseEvent
& WXUNUSED(event
) )
356 if (!Discard()) return;
358 // Add current to history
359 if (!m_filename
.empty())
360 AddToHistory( m_filename
);
363 wxConfig
*conf
= (wxConfig
*) wxConfig::Get();
365 int max
= HISTORY_ENTRIES
;
366 if (m_history
.GetCount() < (size_t)max
)
367 max
= m_history
.GetCount();
369 conf
->Write( _T("/History/Count"), max
);
371 for (int i
= 0; i
< max
; i
++)
374 tmp
.Printf( _T("/History/File%d"), (int)i
);
376 conf
->Write( tmp
, m_history
[(size_t)i
] );
379 // Flush and delete config
380 delete wxConfig::Set( NULL
);
382 // Finally destroy window and quit
386 //------------------------------------------------------------------------------
388 //------------------------------------------------------------------------------
398 SetVendorName(_T("Free world"));
399 SetAppName(_T("wxEdit"));
401 MyFrame
*frame
= new MyFrame( NULL
, wxID_ANY
, _T("wxEdit"), wxPoint(20,20), wxSize(500,340) );