]>
git.saurik.com Git - wxWidgets.git/blob - samples/mobile/wxedit/wxedit.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Robert Roebling
6 /////////////////////////////////////////////////////////////////////////////
8 // For compilers that support precompilation
15 #include "wx/filename.h"
16 #include "wx/config.h"
18 // Include private headers
21 //------------------------------------------------------------------------------
23 //------------------------------------------------------------------------------
25 #define HISTORY_ENTRIES 3
27 //------------------------------------------------------------------------------
29 //------------------------------------------------------------------------------
31 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
32 EVT_MENU(ID_ABOUT
, MyFrame::OnAbout
)
34 EVT_MENU(ID_NEW
, MyFrame::OnNew
)
35 EVT_MENU(ID_OPEN
, MyFrame::OnOpen
)
36 EVT_MENU(ID_SAVE
, MyFrame::OnSave
)
37 EVT_MENU(ID_SAVEAS
, MyFrame::OnSaveAs
)
38 EVT_MENU(ID_QUIT
, MyFrame::OnQuit
)
40 EVT_MENU(ID_COPY
, MyFrame::OnCopy
)
41 EVT_MENU(ID_CUT
, MyFrame::OnCut
)
42 EVT_MENU(ID_PASTE
, MyFrame::OnPaste
)
43 EVT_MENU(ID_DELETE
, MyFrame::OnDelete
)
45 EVT_MENU_RANGE(ID_LAST_1
, ID_LAST_3
, MyFrame::OnLastFiles
)
47 EVT_CLOSE(MyFrame::OnCloseWindow
)
48 EVT_UPDATE_UI(wxID_ANY
,MyFrame::OnUpdateUI
)
51 MyFrame::MyFrame( wxWindow
*parent
, wxWindowID id
, const wxString
&title
,
52 const wxPoint
&position
, const wxSize
& size
, long style
) :
53 wxFrame( parent
, id
, title
, position
, size
, style
)
55 // Create menu and status bar.
59 SetStatusText( _T("Welcome to wxEdit!") );
60 #endif // wxUSE_STATUSBAR
62 // Create edit control. Since it is the only
63 // control in the frame, it will be resized
65 m_text
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
67 // Read .ini file for file history etc.
68 wxConfig
*conf
= (wxConfig
*) wxConfig::Get();
71 conf
->Read( _T("/History/Count"), &entries
);
73 for (int i
= 0; i
< entries
; i
++)
76 tmp
.Printf( _T("/History/File%d"), (int)i
);
79 conf
->Read( tmp
, &res
);
86 void MyFrame::MakeHistory()
88 wxMenuBar
*mb
= GetMenuBar();
92 int max
= m_history
.GetCount();
93 if (max
> HISTORY_ENTRIES
)
94 max
= HISTORY_ENTRIES
;
96 for (int i
= 0; i
< max
; i
++)
99 mb
->FindItem( ID_LAST_1
+ i
, &menu
);
102 wxFileName
fname( m_history
[(size_t)i
] );
103 menu
->SetLabel( ID_LAST_1
+ i
, fname
.GetFullName() );
107 void MyFrame::AddToHistory( const wxString
&fname
)
109 // Fill menu with history index
110 int index
= m_history
.Index( fname
);
112 if (index
!= wxNOT_FOUND
)
113 m_history
.RemoveAt( (size_t) index
);
115 m_history
.Insert( fname
, 0 );
121 void MyFrame::CreateMyMenuBar()
123 wxMenu
*file_menu
= new wxMenu
;
124 file_menu
->Append( ID_ABOUT
, _T("About..."), _T("Program info") );
125 file_menu
->AppendSeparator();
126 file_menu
->Append( ID_NEW
, _T("New..."), _T("New text") );
127 file_menu
->Append( ID_OPEN
, _T("Open..."), _T("Open text") );
128 file_menu
->Append( ID_SAVE
, _T("Save"), _T("Save text") );
129 file_menu
->Append( ID_SAVEAS
, _T("Save as..."), _T("Save text as...") );
130 file_menu
->AppendSeparator();
131 file_menu
->Append( ID_QUIT
, _T("Quit..."), _T("Quit program") );
133 wxMenu
*edit_menu
= new wxMenu
;
134 edit_menu
->Append( ID_COPY
, _T("Copy") );
135 edit_menu
->Append( ID_CUT
, _T("Cut") );
136 edit_menu
->Append( ID_PASTE
, _T("Paste") );
137 edit_menu
->AppendSeparator();
138 edit_menu
->Append( ID_DELETE
, _T("Delete") );
140 wxMenu
*history_menu
= new wxMenu
;
141 history_menu
->Append( ID_LAST_1
, _T("No file.") );
142 history_menu
->Append( ID_LAST_2
, _T("No file.") );
143 history_menu
->Append( ID_LAST_3
, _T("No file.") );
145 wxMenuBar
*menu_bar
= new wxMenuBar();
146 menu_bar
->Append( file_menu
, _T("&File") );
147 menu_bar
->Append( edit_menu
, _T("&Edit") );
148 menu_bar
->Append( history_menu
, _T("&History") );
150 SetMenuBar( menu_bar
);
153 void MyFrame::OnCopy( wxCommandEvent
& WXUNUSED(event
) )
157 void MyFrame::OnCut( wxCommandEvent
& WXUNUSED(event
) )
161 void MyFrame::OnPaste( wxCommandEvent
& WXUNUSED(event
) )
165 void MyFrame::OnDelete( wxCommandEvent
& WXUNUSED(event
) )
169 void MyFrame::OnLastFiles( wxCommandEvent
&event
)
171 if (!Discard()) return;
173 if (!m_filename
.empty())
174 AddToHistory( m_filename
);
176 size_t index
= event
.GetId() - ID_LAST_1
;
178 if( index
< m_history
.GetCount() )
180 m_filename
= m_history
[index
];
183 m_text
->LoadFile( m_filename
);
186 SetStatusText( m_filename
);
187 #endif // wxUSE_STATUSBAR
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
;
212 SetStatusText( wxEmptyString
);
213 #endif // wxUSE_STATUSBAR
216 void MyFrame::OnOpen( wxCommandEvent
& WXUNUSED(event
) )
219 if (!Discard()) return;
221 wxFileDialog
dialog( this, _T("Open text"), wxEmptyString
, wxEmptyString
,
222 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
223 wxOPEN
|wxFILE_MUST_EXIST
);
224 if (dialog
.ShowModal() == wxID_OK
)
229 // requires wxUSE_UNIV_TEXTCTRL to be set to 0
231 wxFileName
fname( dialog
.GetPath() );
232 if ((fname
.GetExt() == _T("cpp")) ||
233 (fname
.GetExt() == _T("c")) ||
234 (fname
.GetExt() == _T("h")) ||
235 (fname
.GetExt() == _T("cxx")) ||
236 (fname
.GetExt() == _T("hxx")))
238 m_text
->SetLanguage( wxSOURCE_LANG_CPP
);
241 if (fname
.GetExt() == _T("py"))
243 m_text
->SetLanguage( wxSOURCE_LANG_PYTHON
);
246 if ((fname
.GetExt() == _T("pl")) ||
247 (fname
.GetExt() == _T("pm")))
249 m_text
->SetLanguage( wxSOURCE_LANG_PYTHON
);
253 m_text
->SetLanguage( wxSOURCE_LANG_NONE
);
258 m_filename
= dialog
.GetPath();
259 m_text
->LoadFile( m_filename
);
262 SetStatusText( m_filename
);
263 #endif // wxUSE_STATUSBAR
265 #endif // wxUSE_FILEDLG
268 void MyFrame::OnSave( wxCommandEvent
& WXUNUSED(event
) )
273 void MyFrame::OnSaveAs( wxCommandEvent
& WXUNUSED(event
) )
276 wxFileDialog
dialog( this, _T("Open text"), wxEmptyString
, wxEmptyString
,
277 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
278 wxSAVE
|wxOVERWRITE_PROMPT
);
279 if (dialog
.ShowModal() == wxID_OK
)
281 m_filename
= dialog
.GetPath();
282 m_text
->SaveFile( m_filename
);
285 SetStatusText( m_filename
);
286 #endif // wxUSE_STATUSBAR
288 #endif // wxUSE_FILEDLG
291 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
293 wxMessageDialog
dialog( this, _T("Welcome to wxEdit\n(C)opyright Robert Roebling"),
294 _T("About wxEdit"), wxOK
|wxICON_INFORMATION
);
298 void MyFrame::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
305 wxCommandEvent event
;
307 if (m_filename
.empty())
310 m_text
->SaveFile( m_filename
);
315 bool MyFrame::Discard()
317 if (m_text
->IsModified())
319 wxMessageDialog
dialog( this, _T("Text has been\nmodified! Save?"),
320 _T("wxEdit"), wxYES_NO
|wxCANCEL
|wxICON_EXCLAMATION
);
322 int ret
= dialog
.ShowModal();
324 if (ret
== wxID_CANCEL
)
337 void MyFrame::OnUpdateUI( wxUpdateUIEvent
&event
)
339 switch (event
.GetId())
342 event
.Enable( false );
345 event
.Enable( false );
348 event
.Enable( false );
351 #ifdef __WXUNIVERSAL__
352 event
.Enable( m_text
->HasSelection() );
356 m_text
->GetSelection(& selFrom
, & selTo
);
357 event
.Enable( selFrom
!= selTo
);
366 void MyFrame::OnCloseWindow( wxCloseEvent
& WXUNUSED(event
) )
369 if (!Discard()) return;
371 // Add current to history
372 if (!m_filename
.empty())
373 AddToHistory( m_filename
);
376 wxConfig
*conf
= (wxConfig
*) wxConfig::Get();
378 int max
= HISTORY_ENTRIES
;
379 if (m_history
.GetCount() < (size_t)max
)
380 max
= m_history
.GetCount();
382 conf
->Write( _T("/History/Count"), max
);
384 for (int i
= 0; i
< max
; i
++)
387 tmp
.Printf( _T("/History/File%d"), (int)i
);
389 conf
->Write( tmp
, m_history
[(size_t)i
] );
392 // Flush and delete config
393 delete wxConfig::Set( NULL
);
395 // Finally destroy window and quit
399 //------------------------------------------------------------------------------
401 //------------------------------------------------------------------------------
407 SetVendorName(_T("Free world"));
408 SetAppName(_T("wxEdit"));
410 MyFrame
*frame
= new MyFrame( NULL
, wxID_ANY
, _T("wxEdit"), wxPoint(20,20), wxSize(500,340) );