]>
git.saurik.com Git - wxWidgets.git/blob - samples/mobile/wxedit/wxedit.cpp
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(-1,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( "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, -1, "", wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
69 // Read .ini file for file history etc.
70 wxConfig
*conf
= (wxConfig
*) wxConfig::Get();
73 conf
->Read( "/History/Count", &entries
);
75 for (int i
= 0; i
< entries
; i
++)
78 tmp
.Printf( "/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
.Remove( (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
, "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" );
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" );
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." );
147 wxMenuBar
*menu_bar
= new wxMenuBar();
148 menu_bar
->Append( file_menu
, "&File" );
149 menu_bar
->Append( edit_menu
, "&Edit" );
150 menu_bar
->Append( history_menu
, "&History" );
152 SetMenuBar( menu_bar
);
155 void MyFrame::OnCopy( wxCommandEvent
&event
)
159 void MyFrame::OnCut( wxCommandEvent
&event
)
163 void MyFrame::OnPaste( wxCommandEvent
&event
)
167 void MyFrame::OnDelete( wxCommandEvent
&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 wxASSERT( index
< m_history
.GetCount() );
182 m_filename
= m_history
[index
];
185 m_text
->LoadFile( m_filename
);
187 SetStatusText( m_filename
);
190 void MyFrame::OnNew( wxCommandEvent
&event
)
192 if (!Discard()) return;
196 if (!m_filename
.empty())
197 AddToHistory( m_filename
);
199 m_filename
= wxEmptyString
;
204 void MyFrame::OnOpen( wxCommandEvent
&event
)
206 if (!Discard()) return;
208 wxFileDialog
dialog( this, "Open text", "", "",
209 "Text file (*.txt)|*.txt|"
211 wxOPEN
|wxFILE_MUST_EXIST
);
212 if (dialog
.ShowModal() == wxID_OK
)
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"))
224 m_text
->SetLanguage( wxSOURCE_LANG_CPP
);
227 if (fname
.GetExt() == "py")
229 m_text
->SetLanguage( wxSOURCE_LANG_PYTHON
);
232 if ((fname
.GetExt() == "pl") ||
233 (fname
.GetExt() == "pm"))
235 m_text
->SetLanguage( wxSOURCE_LANG_PYTHON
);
239 m_text
->SetLanguage( wxSOURCE_LANG_NONE
);
243 m_filename
= dialog
.GetPath();
244 m_text
->LoadFile( m_filename
);
246 SetStatusText( m_filename
);
250 void MyFrame::OnSave( wxCommandEvent
&event
)
255 void MyFrame::OnSaveAs( wxCommandEvent
&event
)
257 wxFileDialog
dialog( this, "Open text", "", "",
258 "Text file (*.txt)|*.txt|"
260 wxSAVE
|wxOVERWRITE_PROMPT
);
261 if (dialog
.ShowModal() == wxID_OK
)
263 m_filename
= dialog
.GetPath();
264 m_text
->SaveFile( m_filename
);
266 SetStatusText( m_filename
);
270 void MyFrame::OnAbout( wxCommandEvent
&event
)
272 wxMessageDialog
dialog( this, "Welcome to wxEdit\n(C)opyright Robert Roebling",
273 "About wxEdit", wxOK
|wxICON_INFORMATION
);
277 void MyFrame::OnQuit( wxCommandEvent
&event
)
284 wxCommandEvent event
;
286 if (m_filename
.empty())
289 m_text
->SaveFile( m_filename
);
294 bool MyFrame::Discard()
296 if (m_text
->IsModified())
298 wxMessageDialog
dialog( this, "Text has been\nmodified! Save?",
299 "wxEdit", wxYES_NO
|wxCANCEL
|wxICON_EXCLAMATION
);
301 int ret
= dialog
.ShowModal();
303 if (ret
== wxID_CANCEL
)
316 void MyFrame::OnUpdateUI( wxUpdateUIEvent
&event
)
318 switch (event
.GetId())
321 event
.Enable( FALSE
);
324 event
.Enable( FALSE
);
327 event
.Enable( FALSE
);
330 #ifdef __WXUNIVERSAL__
331 event
.Enable( m_text
->HasSelection() );
335 m_text
->GetSelection(& selFrom
, & selTo
);
336 event
.Enable( selFrom
!= selTo
);
345 void MyFrame::OnCloseWindow( wxCloseEvent
&event
)
348 if (!Discard()) return;
350 // Add current to history
351 if (!m_filename
.empty())
352 AddToHistory( m_filename
);
355 wxConfig
*conf
= (wxConfig
*) wxConfig::Get();
357 int max
= HISTORY_ENTRIES
;
358 if (m_history
.GetCount() < (size_t)max
)
359 max
= m_history
.GetCount();
361 conf
->Write( "/History/Count", max
);
363 for (int i
= 0; i
< max
; i
++)
366 tmp
.Printf( "/History/File%d", (int)i
);
368 conf
->Write( tmp
, m_history
[(size_t)i
] );
371 // Flush and delete config
372 delete wxConfig::Set( NULL
);
374 // Finally destroy window and quit
378 //------------------------------------------------------------------------------
380 //------------------------------------------------------------------------------
390 SetVendorName("Free world");
391 SetAppName("wxEdit");
393 MyFrame
*frame
= new MyFrame( NULL
, -1, "wxEdit", wxPoint(20,20), wxSize(500,340) );