]>
git.saurik.com Git - wxWidgets.git/blob - samples/mobile/wxedit/wxedit.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Robert Roebling
6 /////////////////////////////////////////////////////////////////////////////
9 #pragma implementation "wxedit.h"
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.
63 SetStatusText( _T("Welcome to wxEdit!") );
64 #endif // wxUSE_STATUSBAR
66 // Create edit control. Since it is the only
67 // control in the frame, it will be resized
69 m_text
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
71 // Read .ini file for file history etc.
72 wxConfig
*conf
= (wxConfig
*) wxConfig::Get();
75 conf
->Read( _T("/History/Count"), &entries
);
77 for (int i
= 0; i
< entries
; i
++)
80 tmp
.Printf( _T("/History/File%d"), (int)i
);
83 conf
->Read( tmp
, &res
);
90 void MyFrame::MakeHistory()
92 wxMenuBar
*mb
= GetMenuBar();
96 int max
= m_history
.GetCount();
97 if (max
> HISTORY_ENTRIES
)
98 max
= HISTORY_ENTRIES
;
100 for (int i
= 0; i
< max
; i
++)
103 mb
->FindItem( ID_LAST_1
+ i
, &menu
);
106 wxFileName
fname( m_history
[(size_t)i
] );
107 menu
->SetLabel( ID_LAST_1
+ i
, fname
.GetFullName() );
111 void MyFrame::AddToHistory( const wxString
&fname
)
113 // Fill menu with history index
114 int index
= m_history
.Index( fname
);
116 if (index
!= wxNOT_FOUND
)
117 m_history
.RemoveAt( (size_t) index
);
119 m_history
.Insert( fname
, 0 );
125 void MyFrame::CreateMyMenuBar()
127 wxMenu
*file_menu
= new wxMenu
;
128 file_menu
->Append( ID_ABOUT
, _T("About..."), _T("Program info") );
129 file_menu
->AppendSeparator();
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...") );
134 file_menu
->AppendSeparator();
135 file_menu
->Append( ID_QUIT
, _T("Quit..."), _T("Quit program") );
137 wxMenu
*edit_menu
= new wxMenu
;
138 edit_menu
->Append( ID_COPY
, _T("Copy") );
139 edit_menu
->Append( ID_CUT
, _T("Cut") );
140 edit_menu
->Append( ID_PASTE
, _T("Paste") );
141 edit_menu
->AppendSeparator();
142 edit_menu
->Append( ID_DELETE
, _T("Delete") );
144 wxMenu
*history_menu
= new wxMenu
;
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.") );
149 wxMenuBar
*menu_bar
= new wxMenuBar();
150 menu_bar
->Append( file_menu
, _T("&File") );
151 menu_bar
->Append( edit_menu
, _T("&Edit") );
152 menu_bar
->Append( history_menu
, _T("&History") );
154 SetMenuBar( menu_bar
);
157 void MyFrame::OnCopy( wxCommandEvent
& WXUNUSED(event
) )
161 void MyFrame::OnCut( wxCommandEvent
& WXUNUSED(event
) )
165 void MyFrame::OnPaste( wxCommandEvent
& WXUNUSED(event
) )
169 void MyFrame::OnDelete( wxCommandEvent
& WXUNUSED(event
) )
173 void MyFrame::OnLastFiles( wxCommandEvent
&event
)
175 if (!Discard()) return;
177 if (!m_filename
.empty())
178 AddToHistory( m_filename
);
180 size_t index
= event
.GetId() - ID_LAST_1
;
182 if( index
< m_history
.GetCount() )
184 m_filename
= m_history
[index
];
187 m_text
->LoadFile( m_filename
);
190 SetStatusText( m_filename
);
191 #endif // wxUSE_STATUSBAR
196 _T("This entry is empty. It should be filled once you will start opening."),
198 wxOK
| wxICON_INFORMATION
,
204 void MyFrame::OnNew( wxCommandEvent
& WXUNUSED(event
) )
206 if (!Discard()) return;
210 if (!m_filename
.empty())
211 AddToHistory( m_filename
);
213 m_filename
= wxEmptyString
;
216 SetStatusText( wxEmptyString
);
217 #endif // wxUSE_STATUSBAR
220 void MyFrame::OnOpen( wxCommandEvent
& WXUNUSED(event
) )
223 if (!Discard()) return;
225 wxFileDialog
dialog( this, _T("Open text"), wxEmptyString
, wxEmptyString
,
226 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
227 wxOPEN
|wxFILE_MUST_EXIST
);
228 if (dialog
.ShowModal() == wxID_OK
)
233 // requires wxUSE_UNIV_TEXTCTRL to be set to 0
235 wxFileName
fname( dialog
.GetPath() );
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")))
242 m_text
->SetLanguage( wxSOURCE_LANG_CPP
);
245 if (fname
.GetExt() == _T("py"))
247 m_text
->SetLanguage( wxSOURCE_LANG_PYTHON
);
250 if ((fname
.GetExt() == _T("pl")) ||
251 (fname
.GetExt() == _T("pm")))
253 m_text
->SetLanguage( wxSOURCE_LANG_PYTHON
);
257 m_text
->SetLanguage( wxSOURCE_LANG_NONE
);
262 m_filename
= dialog
.GetPath();
263 m_text
->LoadFile( m_filename
);
266 SetStatusText( m_filename
);
267 #endif // wxUSE_STATUSBAR
269 #endif // wxUSE_FILEDLG
272 void MyFrame::OnSave( wxCommandEvent
& WXUNUSED(event
) )
277 void MyFrame::OnSaveAs( wxCommandEvent
& WXUNUSED(event
) )
280 wxFileDialog
dialog( this, _T("Open text"), wxEmptyString
, wxEmptyString
,
281 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
282 wxSAVE
|wxOVERWRITE_PROMPT
);
283 if (dialog
.ShowModal() == wxID_OK
)
285 m_filename
= dialog
.GetPath();
286 m_text
->SaveFile( m_filename
);
289 SetStatusText( m_filename
);
290 #endif // wxUSE_STATUSBAR
292 #endif // wxUSE_FILEDLG
295 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
297 wxMessageDialog
dialog( this, _T("Welcome to wxEdit\n(C)opyright Robert Roebling"),
298 _T("About wxEdit"), wxOK
|wxICON_INFORMATION
);
302 void MyFrame::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
309 wxCommandEvent event
;
311 if (m_filename
.empty())
314 m_text
->SaveFile( m_filename
);
319 bool MyFrame::Discard()
321 if (m_text
->IsModified())
323 wxMessageDialog
dialog( this, _T("Text has been\nmodified! Save?"),
324 _T("wxEdit"), wxYES_NO
|wxCANCEL
|wxICON_EXCLAMATION
);
326 int ret
= dialog
.ShowModal();
328 if (ret
== wxID_CANCEL
)
341 void MyFrame::OnUpdateUI( wxUpdateUIEvent
&event
)
343 switch (event
.GetId())
346 event
.Enable( false );
349 event
.Enable( false );
352 event
.Enable( false );
355 #ifdef __WXUNIVERSAL__
356 event
.Enable( m_text
->HasSelection() );
360 m_text
->GetSelection(& selFrom
, & selTo
);
361 event
.Enable( selFrom
!= selTo
);
370 void MyFrame::OnCloseWindow( wxCloseEvent
& WXUNUSED(event
) )
373 if (!Discard()) return;
375 // Add current to history
376 if (!m_filename
.empty())
377 AddToHistory( m_filename
);
380 wxConfig
*conf
= (wxConfig
*) wxConfig::Get();
382 int max
= HISTORY_ENTRIES
;
383 if (m_history
.GetCount() < (size_t)max
)
384 max
= m_history
.GetCount();
386 conf
->Write( _T("/History/Count"), max
);
388 for (int i
= 0; i
< max
; i
++)
391 tmp
.Printf( _T("/History/File%d"), (int)i
);
393 conf
->Write( tmp
, m_history
[(size_t)i
] );
396 // Flush and delete config
397 delete wxConfig::Set( NULL
);
399 // Finally destroy window and quit
403 //------------------------------------------------------------------------------
405 //------------------------------------------------------------------------------
411 SetVendorName(_T("Free world"));
412 SetAppName(_T("wxEdit"));
414 MyFrame
*frame
= new MyFrame( NULL
, wxID_ANY
, _T("wxEdit"), wxPoint(20,20), wxSize(500,340) );