X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/926076161327c12d8535c72235f91cee6d316281..80d2803f703d1b238f42725504f08266ef02defe:/samples/caret/caret.cpp diff --git a/samples/caret/caret.cpp b/samples/caret/caret.cpp index 9142ef3094..b3f8ca5fe7 100644 --- a/samples/caret/caret.cpp +++ b/samples/caret/caret.cpp @@ -9,18 +9,6 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -// ============================================================================ -// declarations -// ============================================================================ - -// ---------------------------------------------------------------------------- -// headers -// ---------------------------------------------------------------------------- -#ifdef __GNUG__ - #pragma implementation "caret.cpp" - #pragma interface "caret.cpp" -#endif - // For compilers that support precompilation, includes "wx/wx.h". #include @@ -73,6 +61,7 @@ public: // event handlers (these functions should _not_ be virtual) void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); + void OnSetBlinkTime(wxCommandEvent& event); private: // any class wishing to process wxWindows events must use this macro @@ -105,7 +94,6 @@ public: void OnChar( wxKeyEvent &event ); private: - wxCaret m_caret; wxFont m_font; // the margin around the text (looks nicer) @@ -135,13 +123,12 @@ private: enum { // menu items - Minimal_Quit = 1, - Minimal_About, - Minimal_Test1, - Minimal_Test2, + Caret_Quit = 1, + Caret_About, + Caret_SetBlinkTime, // controls start here (the numbers are, of course, arbitrary) - Minimal_Text = 1000, + Caret_Text = 1000 }; // ---------------------------------------------------------------------------- @@ -152,8 +139,9 @@ enum // handlers) which process them. It can be also done at run-time, but for the // simple menu events like this the static method is much simpler. BEGIN_EVENT_TABLE(MyFrame, wxFrame) - EVT_MENU(Minimal_Quit, MyFrame::OnQuit) - EVT_MENU(Minimal_About, MyFrame::OnAbout) + EVT_MENU(Caret_Quit, MyFrame::OnQuit) + EVT_MENU(Caret_About, MyFrame::OnAbout) + EVT_MENU(Caret_SetBlinkTime, MyFrame::OnSetBlinkTime) END_EVENT_TABLE() // Create a new application object: this macro will allow wxWindows to create @@ -175,7 +163,7 @@ IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { // Create the main application window - MyFrame *frame = new MyFrame("Minimal wxWindows App", + MyFrame *frame = new MyFrame("Caret wxWindows sample", wxPoint(50, 50), wxSize(450, 340)); // Show it and tell the application that it's our main window @@ -203,9 +191,11 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) // create a menu bar wxMenu *menuFile = new wxMenu; - menuFile->Append(Minimal_About, "&About...\tCtrl-A", "Show about dialog"); + menuFile->Append(Caret_SetBlinkTime, "&Blink time...\tCtrl-B"); + menuFile->AppendSeparator(); + menuFile->Append(Caret_About, "&About...\tCtrl-A", "Show about dialog"); menuFile->AppendSeparator(); - menuFile->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program"); + menuFile->Append(Caret_Quit, "E&xit\tAlt-X", "Quit this program"); // now append the freshly created menu to the menu bar... wxMenuBar *menuBar = new wxMenuBar; @@ -232,21 +222,26 @@ void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { - wxString msg; - msg.Printf( _T("This is the about dialog of minimal sample.\n") - _T("Welcome to %s") -#ifdef wxBETA_NUMBER - _T(" (beta %d)!") -#endif // wxBETA_NUMBER - , wxVERSION_STRING -#ifdef wxBETA_NUMBER - , wxBETA_NUMBER -#endif // wxBETA_NUMBER - ); - - wxMessageBox(msg, "About Minimal", wxOK | wxICON_INFORMATION, this); + wxMessageBox(_T("The caret wxWindows sample.\n© 1999 Vadim Zeitlin"), + _T("About Caret"), wxOK | wxICON_INFORMATION, this); } +void MyFrame::OnSetBlinkTime(wxCommandEvent& WXUNUSED(event)) +{ + long blinkTime = wxGetNumberFromUser + ( + _T("The caret blink time is the time between two blinks"), + _T("Time in milliseconds:"), + _T("wxCaret sample"), + wxCaret::GetBlinkTime(), 0, 10000, + this + ); + if ( blinkTime != -1 ) + { + wxCaret::SetBlinkTime((int)blinkTime); + wxLogStatus(this, _T("Blink time set to %ld milliseconds."), blinkTime); + } +} // ---------------------------------------------------------------------------- // MyCanvas @@ -276,14 +271,15 @@ MyCanvas::MyCanvas( wxWindow *parent ) m_heightChar = dc.GetCharHeight(); m_widthChar = dc.GetCharWidth(); - m_caret.Create( this, m_widthChar, m_heightChar ); + wxCaret *caret = new wxCaret(this, m_widthChar, m_heightChar); + SetCaret(caret); m_xCaret = m_yCaret = m_xChars = m_yChars = 0; m_xMargin = m_yMargin = 5; - m_caret.Move(m_xMargin, m_yMargin); - m_caret.Show(); + caret->Move(m_xMargin, m_yMargin); + caret->Show(); } MyCanvas::~MyCanvas() @@ -303,18 +299,28 @@ void MyCanvas::OnSize( wxSizeEvent &event ) free(m_text); m_text = (wxChar *)calloc(m_xChars * m_yChars, sizeof(wxChar)); - wxString msg; - msg.Printf(_T("Panel size is (%d, %d)"), m_xChars, m_yChars); + wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); - ((wxFrame *)GetParent())->SetStatusText(msg, 1); + if ( frame && frame->GetStatusBar() ) + { + wxString msg; + msg.Printf(_T("Panel size is (%d, %d)"), m_xChars, m_yChars); + + frame->SetStatusText(msg, 1); + } event.Skip(); } +// NB: this method is horrible inefficient especially because the caret +// needs to be redrawn often and in this case we only have to redraw +// the caret location and not the entire window - in a real program we +// would use GetUpdateRegion() and iterate over rectangles it contains void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) { wxPaintDC dc( this ); PrepareDC( dc ); + dc.Clear(); dc.SetFont( m_font ); @@ -368,13 +374,15 @@ void MyCanvas::OnChar( wxKeyEvent &event ) break; default: - if ( wxIsprint(event.KeyCode()) ) + if ( !event.AltDown() && wxIsprint(event.KeyCode()) ) { CharAt(m_xCaret, m_yCaret) = (wxChar)event.KeyCode(); NextChar(); } else { + event.Skip(); + // don't refresh return; } @@ -382,8 +390,8 @@ void MyCanvas::OnChar( wxKeyEvent &event ) wxLogStatus(_T("Caret is at (%d, %d)"), m_xCaret, m_yCaret); - m_caret.Move(m_xMargin + m_xCaret * m_widthChar, - m_yMargin + m_yCaret * m_heightChar); + GetCaret()->Move(m_xMargin + m_xCaret * m_widthChar, + m_yMargin + m_yCaret * m_heightChar); Refresh(); }