]>
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
22 #include "../../sample.xpm"
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 SetIcon(wxICON(sample
));
61 // Create menu and status bar.
65 SetStatusText( _T("Welcome to wxEdit!") );
66 #endif // wxUSE_STATUSBAR
68 // Create edit control. Since it is the only
69 // control in the frame, it will be resized
71 m_text
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
73 // Read .ini file for file history etc.
74 wxConfig
*conf
= (wxConfig
*) wxConfig::Get();
77 conf
->Read( _T("/History/Count"), &entries
);
79 for (int i
= 0; i
< entries
; i
++)
82 tmp
.Printf( _T("/History/File%d"), (int)i
);
85 conf
->Read( tmp
, &res
);
92 void MyFrame::MakeHistory()
94 wxMenuBar
*mb
= GetMenuBar();
98 int max
= m_history
.GetCount();
99 if (max
> HISTORY_ENTRIES
)
100 max
= HISTORY_ENTRIES
;
102 for (int i
= 0; i
< max
; i
++)
105 mb
->FindItem( ID_LAST_1
+ i
, &menu
);
108 wxFileName
fname( m_history
[(size_t)i
] );
109 menu
->SetLabel( ID_LAST_1
+ i
, fname
.GetFullName() );
113 void MyFrame::AddToHistory( const wxString
&fname
)
115 // Fill menu with history index
116 int index
= m_history
.Index( fname
);
118 if (index
!= wxNOT_FOUND
)
119 m_history
.RemoveAt( (size_t) index
);
121 m_history
.Insert( fname
, 0 );
127 void MyFrame::CreateMyMenuBar()
129 wxMenu
*file_menu
= new wxMenu
;
130 file_menu
->Append( ID_ABOUT
, _T("About..."), _T("Program info") );
131 file_menu
->AppendSeparator();
132 file_menu
->Append( ID_NEW
, _T("New..."), _T("New text") );
133 file_menu
->Append( ID_OPEN
, _T("Open..."), _T("Open text") );
134 file_menu
->Append( ID_SAVE
, _T("Save"), _T("Save text") );
135 file_menu
->Append( ID_SAVEAS
, _T("Save as..."), _T("Save text as...") );
136 file_menu
->AppendSeparator();
137 file_menu
->Append( ID_QUIT
, _T("Quit..."), _T("Quit program") );
139 wxMenu
*edit_menu
= new wxMenu
;
140 edit_menu
->Append( ID_COPY
, _T("Copy") );
141 edit_menu
->Append( ID_CUT
, _T("Cut") );
142 edit_menu
->Append( ID_PASTE
, _T("Paste") );
143 edit_menu
->AppendSeparator();
144 edit_menu
->Append( ID_DELETE
, _T("Delete") );
146 wxMenu
*history_menu
= new wxMenu
;
147 history_menu
->Append( ID_LAST_1
, _T("No file.") );
148 history_menu
->Append( ID_LAST_2
, _T("No file.") );
149 history_menu
->Append( ID_LAST_3
, _T("No file.") );
151 wxMenuBar
*menu_bar
= new wxMenuBar();
152 menu_bar
->Append( file_menu
, _T("&File") );
153 menu_bar
->Append( edit_menu
, _T("&Edit") );
154 menu_bar
->Append( history_menu
, _T("&History") );
156 SetMenuBar( menu_bar
);
159 void MyFrame::OnCopy( wxCommandEvent
& WXUNUSED(event
) )
163 void MyFrame::OnCut( wxCommandEvent
& WXUNUSED(event
) )
167 void MyFrame::OnPaste( wxCommandEvent
& WXUNUSED(event
) )
171 void MyFrame::OnDelete( wxCommandEvent
& WXUNUSED(event
) )
175 void MyFrame::OnLastFiles( wxCommandEvent
&event
)
177 if (!Discard()) return;
179 if (!m_filename
.empty())
180 AddToHistory( m_filename
);
182 size_t index
= event
.GetId() - ID_LAST_1
;
184 if( index
< m_history
.GetCount() )
186 m_filename
= m_history
[index
];
189 m_text
->LoadFile( m_filename
);
192 SetStatusText( m_filename
);
193 #endif // wxUSE_STATUSBAR
198 _T("This entry is empty. It should be filled once you will start opening."),
200 wxOK
| wxICON_INFORMATION
,
206 void MyFrame::OnNew( wxCommandEvent
& WXUNUSED(event
) )
208 if (!Discard()) return;
212 if (!m_filename
.empty())
213 AddToHistory( m_filename
);
215 m_filename
= wxEmptyString
;
218 SetStatusText( wxEmptyString
);
219 #endif // wxUSE_STATUSBAR
222 void MyFrame::OnOpen( wxCommandEvent
& WXUNUSED(event
) )
225 if (!Discard()) return;
227 wxFileDialog
dialog( this, _T("Open text"), wxEmptyString
, wxEmptyString
,
228 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
229 wxFD_OPEN
|wxFD_FILE_MUST_EXIST
);
230 if (dialog
.ShowModal() == wxID_OK
)
235 // requires wxUSE_UNIV_TEXTCTRL to be set to 0
237 wxFileName
fname( dialog
.GetPath() );
238 if ((fname
.GetExt() == _T("cpp")) ||
239 (fname
.GetExt() == _T("c")) ||
240 (fname
.GetExt() == _T("h")) ||
241 (fname
.GetExt() == _T("cxx")) ||
242 (fname
.GetExt() == _T("hxx")))
244 m_text
->SetLanguage( wxSOURCE_LANG_CPP
);
247 if (fname
.GetExt() == _T("py"))
249 m_text
->SetLanguage( wxSOURCE_LANG_PYTHON
);
252 if ((fname
.GetExt() == _T("pl")) ||
253 (fname
.GetExt() == _T("pm")))
255 m_text
->SetLanguage( wxSOURCE_LANG_PYTHON
);
259 m_text
->SetLanguage( wxSOURCE_LANG_NONE
);
264 m_filename
= dialog
.GetPath();
265 m_text
->LoadFile( m_filename
);
268 SetStatusText( m_filename
);
269 #endif // wxUSE_STATUSBAR
271 #endif // wxUSE_FILEDLG
274 void MyFrame::OnSave( wxCommandEvent
& WXUNUSED(event
) )
279 void MyFrame::OnSaveAs( wxCommandEvent
& WXUNUSED(event
) )
282 wxFileDialog
dialog( this, _T("Open text"), wxEmptyString
, wxEmptyString
,
283 _T("Text file (*.txt)|*.txt|Any file (*)|*"),
284 wxFD_SAVE
|wxFD_OVERWRITE_PROMPT
);
285 if (dialog
.ShowModal() == wxID_OK
)
287 m_filename
= dialog
.GetPath();
288 m_text
->SaveFile( m_filename
);
291 SetStatusText( m_filename
);
292 #endif // wxUSE_STATUSBAR
294 #endif // wxUSE_FILEDLG
297 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
299 wxMessageDialog
dialog( this, _T("Welcome to wxEdit\n(C)opyright Robert Roebling"),
300 _T("About wxEdit"), wxOK
|wxICON_INFORMATION
);
304 void MyFrame::OnQuit( wxCommandEvent
& WXUNUSED(event
) )
311 wxCommandEvent event
;
313 if (m_filename
.empty())
316 m_text
->SaveFile( m_filename
);
321 bool MyFrame::Discard()
323 if (m_text
->IsModified())
325 wxMessageDialog
dialog( this, _T("Text has been\nmodified! Save?"),
326 _T("wxEdit"), wxYES_NO
|wxCANCEL
|wxICON_EXCLAMATION
);
328 int ret
= dialog
.ShowModal();
330 if (ret
== wxID_CANCEL
)
343 void MyFrame::OnUpdateUI( wxUpdateUIEvent
&event
)
345 switch (event
.GetId())
348 event
.Enable( false );
351 event
.Enable( false );
354 event
.Enable( false );
357 #ifdef __WXUNIVERSAL__
358 event
.Enable( m_text
->HasSelection() );
362 m_text
->GetSelection(& selFrom
, & selTo
);
363 event
.Enable( selFrom
!= selTo
);
372 void MyFrame::OnCloseWindow( wxCloseEvent
& WXUNUSED(event
) )
375 if (!Discard()) return;
377 // Add current to history
378 if (!m_filename
.empty())
379 AddToHistory( m_filename
);
382 wxConfig
*conf
= (wxConfig
*) wxConfig::Get();
384 int max
= HISTORY_ENTRIES
;
385 if (m_history
.GetCount() < (size_t)max
)
386 max
= m_history
.GetCount();
388 conf
->Write( _T("/History/Count"), max
);
390 for (int i
= 0; i
< max
; i
++)
393 tmp
.Printf( _T("/History/File%d"), (int)i
);
395 conf
->Write( tmp
, m_history
[(size_t)i
] );
398 // Flush and delete config
399 delete wxConfig::Set( NULL
);
401 // Finally destroy window and quit
405 //------------------------------------------------------------------------------
407 //------------------------------------------------------------------------------
413 if ( !wxApp::OnInit() )
416 SetVendorName(_T("Free world"));
417 SetAppName(_T("wxEdit"));
419 MyFrame
*frame
= new MyFrame( NULL
, wxID_ANY
, _T("wxEdit"), wxPoint(20,20), wxSize(500,340) );