X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b1d4dd7add411ebff1ce4a8d1d992e0e43ee26b1..c663fbea6851207b4e0ac3e362868c23b1494a46:/samples/caret/caret.cpp diff --git a/samples/caret/caret.cpp b/samples/caret/caret.cpp index 4a2b1bf40d..9180908c65 100644 --- a/samples/caret/caret.cpp +++ b/samples/caret/caret.cpp @@ -20,11 +20,11 @@ // need because it includes almost all 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, _T("&About...\tCtrl-A"), _T("Show about dialog")); @@ -260,6 +268,23 @@ void MyFrame::OnSetBlinkTime(wxCommandEvent& WXUNUSED(event)) } } +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 // ---------------------------------------------------------------------------- @@ -281,8 +306,7 @@ MyCanvas::MyCanvas( wxWindow *parent ) SetBackgroundColour(*wxWHITE); - m_font = wxFont(12, wxFONTFAMILY_TELETYPE, - wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL); + SetFontSize(12); m_xCaret = m_yCaret = m_xChars = m_yChars = 0; @@ -299,16 +323,30 @@ MyCanvas::~MyCanvas() 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) @@ -327,10 +365,18 @@ void MyCanvas::DoMoveCaret() 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 ) @@ -348,8 +394,6 @@ void MyCanvas::OnSize( wxSizeEvent &event ) frame->SetStatusText(msg, 1); } - - event.Skip(); } // NB: this method is horrible inefficient especially because the caret