// need because it includes almost all <standard< wxWindows headers
#ifndef WX_PRECOMP
#include "wx/wx.h"
-
#include "wx/log.h"
#endif
#include "wx/caret.h"
+#include "wx/numdlg.h"
// ----------------------------------------------------------------------------
// ressources
wxChar& CharAt(int x, int y) { return *(m_text + x + m_xChars * y); }
// operations
+ void SetFontSize(int fontSize);
void CreateCaret();
void MoveCaret(int x, int y);
// move the caret to m_xCaret, m_yCaret
void DoMoveCaret();
+ // update the geometry
+ void ChangeSize();
+
wxFont m_font;
// the margin around the text (looks nicer)
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnSetBlinkTime(wxCommandEvent& event);
+ void OnSetFontSize(wxCommandEvent& event);
void OnCaretMove(wxCommandEvent& event);
private:
Caret_Quit = 1,
Caret_About,
Caret_SetBlinkTime,
+ Caret_SetFontSize,
Caret_Move,
// controls start here (the numbers are, of course, arbitrary)
EVT_MENU(Caret_Quit, MyFrame::OnQuit)
EVT_MENU(Caret_About, MyFrame::OnAbout)
EVT_MENU(Caret_SetBlinkTime, MyFrame::OnSetBlinkTime)
+ EVT_MENU(Caret_SetFontSize, MyFrame::OnSetFontSize)
EVT_MENU(Caret_Move, MyFrame::OnCaretMove)
END_EVENT_TABLE()
bool MyApp::OnInit()
{
// create and show the main application window
- MyFrame *frame = new MyFrame("Caret wxWindows sample",
+ MyFrame *frame = new MyFrame(_T("Caret wxWindows sample"),
wxPoint(50, 50), wxSize(450, 340));
frame->Show(TRUE);
// create a menu bar
wxMenu *menuFile = new wxMenu;
- menuFile->Append(Caret_SetBlinkTime, "&Blink time...\tCtrl-B");
- menuFile->Append(Caret_Move, "&Move caret\tCtrl-C");
+ menuFile->Append(Caret_SetBlinkTime, _T("&Blink time...\tCtrl-B"));
+ menuFile->Append(Caret_SetFontSize, _T("&Font size...\tCtrl-S"));
+ menuFile->Append(Caret_Move, _T("&Move caret\tCtrl-C"));
menuFile->AppendSeparator();
- menuFile->Append(Caret_About, "&About...\tCtrl-A", "Show about dialog");
+ menuFile->Append(Caret_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
menuFile->AppendSeparator();
- menuFile->Append(Caret_Quit, "E&xit\tAlt-X", "Quit this program");
+ menuFile->Append(Caret_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
// now append the freshly created menu to the menu bar...
wxMenuBar *menuBar = new wxMenuBar;
- menuBar->Append(menuFile, "&File");
+ menuBar->Append(menuFile, _T("&File"));
// ... and attach this menu bar to the frame
SetMenuBar(menuBar);
// create a status bar just for fun (by default with 1 pane only)
CreateStatusBar(2);
- SetStatusText("Welcome to wxWindows!");
+ SetStatusText(_T("Welcome to wxWindows!"));
}
}
}
+void MyFrame::OnSetFontSize(wxCommandEvent& WXUNUSED(event))
+{
+ long fontSize = wxGetNumberFromUser
+ (
+ _T("The font size also determines the caret size so\nthis demonstrates resizing the caret."),
+ _T("Font size (in points):"),
+ _T("wxCaret sample"),
+ 12, 1, 100,
+ this
+ );
+
+ if ( fontSize != -1 )
+ {
+ m_canvas->SetFontSize((int)fontSize);
+ }
+}
+
// ----------------------------------------------------------------------------
// MyCanvas
// ----------------------------------------------------------------------------
SetBackgroundColour(*wxWHITE);
- m_font = wxFont(12, wxFONTFAMILY_TELETYPE,
- wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
+ SetFontSize(12);
m_xCaret = m_yCaret =
m_xChars = m_yChars = 0;
void MyCanvas::CreateCaret()
{
+ wxCaret *caret = new wxCaret(this, m_widthChar, m_heightChar);
+ SetCaret(caret);
+
+ caret->Move(m_xMargin, m_yMargin);
+ caret->Show();
+}
+
+void MyCanvas::SetFontSize(int fontSize)
+{
+ m_font = wxFont(fontSize, wxFONTFAMILY_TELETYPE,
+ wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
+
wxClientDC dc(this);
dc.SetFont(m_font);
m_heightChar = dc.GetCharHeight();
m_widthChar = dc.GetCharWidth();
- wxCaret *caret = new wxCaret(this, m_widthChar, m_heightChar);
- SetCaret(caret);
+ wxCaret *caret = GetCaret();
+ if ( caret )
+ {
+ caret->SetSize(m_widthChar, m_heightChar);
- caret->Move(m_xMargin, m_yMargin);
- caret->Show();
+ ChangeSize();
+ }
}
void MyCanvas::MoveCaret(int x, int y)
m_yMargin + m_yCaret * m_heightChar);
}
-void MyCanvas::OnSize( wxSizeEvent &event )
+void MyCanvas::OnSize(wxSizeEvent& event)
{
- m_xChars = (event.GetSize().x - 2*m_xMargin) / m_widthChar;
- m_yChars = (event.GetSize().y - 2*m_yMargin) / m_heightChar;
+ ChangeSize();
+
+ event.Skip();
+}
+
+void MyCanvas::ChangeSize()
+{
+ wxSize size = GetClientSize();
+ m_xChars = (size.x - 2*m_xMargin) / m_widthChar;
+ m_yChars = (size.y - 2*m_yMargin) / m_heightChar;
if ( !m_xChars )
m_xChars = 1;
if ( !m_yChars )
frame->SetStatusText(msg, 1);
}
-
- event.Skip();
}
// NB: this method is horrible inefficient especially because the caret
void MyCanvas::OnChar( wxKeyEvent &event )
{
- switch ( event.KeyCode() )
+ switch ( event.GetKeyCode() )
{
case WXK_LEFT:
PrevChar();
break;
default:
- if ( !event.AltDown() && wxIsprint(event.KeyCode()) )
+ if ( !event.AltDown() && wxIsprint(event.GetKeyCode()) )
{
- wxChar ch = (wxChar)event.KeyCode();
+ wxChar ch = (wxChar)event.GetKeyCode();
CharAt(m_xCaret, m_yCaret) = ch;
wxCaretSuspend cs(this);