diff --git a/docs/latex/wx/category.tex b/docs/latex/wx/category.tex index ae4ec05096..c9710911c9 100644 --- a/docs/latex/wx/category.tex +++ b/docs/latex/wx/category.tex @@ -482,6 +482,7 @@ used internally by the HTML classes. \begin{twocollist}\itemsep=0pt \twocolitem{\helpref{wxApp}{wxapp}}{Application class} +\twocolitem{\helpref{wxCaret}{wxcaret}}{A caret (cursor) object} \twocolitem{\helpref{wxCmdLineParser}{wxcmdlineparser}}{Command line parser class} \twocolitem{\helpref{wxConfig}{wxconfigbase}}{Classes for configuration reading/writing (using either INI files or registry)} \twocolitem{\helpref{wxDllLoader}{wxdllloader}}{Class to work with shared libraries.} diff --git a/docs/latex/wx/classes.tex b/docs/latex/wx/classes.tex index 63dcd27006..3c4b90fc51 100644 --- a/docs/latex/wx/classes.tex +++ b/docs/latex/wx/classes.tex @@ -21,6 +21,7 @@ \input strmbfrd.tex \input calclevt.tex \input calctrl.tex +\input caret.tex \input checkbox.tex \input checklst.tex \input choice.tex diff --git a/docs/latex/wx/window.tex b/docs/latex/wx/window.tex index e9647edbbc..594ec21503 100644 --- a/docs/latex/wx/window.tex +++ b/docs/latex/wx/window.tex @@ -481,6 +481,12 @@ control label is not truncated. For windows containing subwindows (typically same as the size the window would have had after calling \helpref{Fit}{wxwindowfit}. +\membersection{wxWindow::GetCaret}\label{wxwindowgetcaret} + +\constfunc{wxCaret *}{GetCaret}{\void} + +Returns the \helpref{caret}{wxcaret} associated with the window. + \membersection{wxWindow::GetCharHeight} \constfunc{virtual int}{GetCharHeight}{\void} @@ -1807,6 +1813,12 @@ look as the user wishes with run-time loadable modules. \helpref{wxWindow::Refresh}{wxwindowrefresh},\rtfsp \helpref{wxWindow::OnEraseBackground}{wxwindowonerasebackground} +\membersection{wxWindow::SetCaret}\label{wxwindowsetcaret} + +\constfunc{void}{SetCaret}{\param{wxCaret *}{caret}} + +Sets the \helpref{caret}{wxcaret} associated with the window. + \membersection{wxWindow::SetClientSize}\label{wxwindowsetclientsize} \func{virtual void}{SetClientSize}{\param{int}{ width}, \param{int}{ height}} @@ -1854,36 +1866,6 @@ be reset back to default. \helpref{::wxSetCursor}{wxsetcursor}, \helpref{wxCursor}{wxcursor} -\membersection{wxWindow::SetEventHandler}\label{wxwindowseteventhandler} - -\func{void}{SetEventHandler}{\param{wxEvtHandler* }{handler}} - -Sets the event handler for this window. - -\wxheading{Parameters} - -\docparam{handler}{Specifies the handler to be set.} - -\wxheading{Remarks} - -An event handler is an object that is capable of processing the events -sent to a window. By default, the window is its own event handler, but -an application may wish to substitute another, for example to allow -central implementation of event-handling for a variety of different -window classes. - -It is usually better to use \helpref{wxWindow::PushEventHandler}{wxwindowpusheventhandler} since -this sets up a chain of event handlers, where an event not handled by one event handler is -handed to the next one in the chain. - -\wxheading{See also} - -\helpref{wxWindow::GetEventHandler}{wxwindowgeteventhandler},\rtfsp -\helpref{wxWindow::PushEventHandler}{wxwindowpusheventhandler},\rtfsp -\helpref{wxWindow::PopEventHandler}{wxwindowpusheventhandler},\rtfsp -\helpref{wxEvtHandler::ProcessEvent}{wxevthandlerprocessevent},\rtfsp -\helpref{wxEvtHandler}{wxevthandler} - \membersection{wxWindow::SetConstraints}\label{wxwindowsetconstraints} \func{void}{SetConstraints}{\param{wxLayoutConstraints* }{constraints}} @@ -1918,6 +1900,36 @@ If the window already has a drop target, it is deleted. \helpref{wxWindow::GetDropTarget}{wxwindowgetdroptarget}, \helpref{Drag and drop overview}{wxdndoverview} +\membersection{wxWindow::SetEventHandler}\label{wxwindowseteventhandler} + +\func{void}{SetEventHandler}{\param{wxEvtHandler* }{handler}} + +Sets the event handler for this window. + +\wxheading{Parameters} + +\docparam{handler}{Specifies the handler to be set.} + +\wxheading{Remarks} + +An event handler is an object that is capable of processing the events +sent to a window. By default, the window is its own event handler, but +an application may wish to substitute another, for example to allow +central implementation of event-handling for a variety of different +window classes. + +It is usually better to use \helpref{wxWindow::PushEventHandler}{wxwindowpusheventhandler} since +this sets up a chain of event handlers, where an event not handled by one event handler is +handed to the next one in the chain. + +\wxheading{See also} + +\helpref{wxWindow::GetEventHandler}{wxwindowgeteventhandler},\rtfsp +\helpref{wxWindow::PushEventHandler}{wxwindowpusheventhandler},\rtfsp +\helpref{wxWindow::PopEventHandler}{wxwindowpusheventhandler},\rtfsp +\helpref{wxEvtHandler::ProcessEvent}{wxevthandlerprocessevent},\rtfsp +\helpref{wxEvtHandler}{wxevthandler} + \membersection{wxWindow::SetExtraStyle}\label{wxwindowsetextrastyle} \func{void}{SetExtraStyle}{\param{long }{exStyle}} diff --git a/include/wx/generic/caret.h b/include/wx/generic/caret.h index ffb95c3422..9b2340de44 100644 --- a/include/wx/generic/caret.h +++ b/include/wx/generic/caret.h @@ -48,14 +48,24 @@ public: // implementation // -------------- - // blink the caret once - void Blink(); + // called by wxWindow (not using the event tables) + virtual void OnSetFocus(); + virtual void OnKillFocus(); + + // called by wxCaretTimer + void OnTimer(); protected: virtual void DoShow(); virtual void DoHide(); virtual void DoMove(); + // blink the caret once + void Blink(); + + // refresh the caret + void Refresh(); + // draw the caret on the given DC void DoDraw(wxDC *dc); @@ -64,7 +74,8 @@ private: void InitGeneric(); wxCaretTimer m_timer; - bool m_blinkedOut; // TRUE => caret hidden right now + bool m_blinkedOut, // TRUE => caret hidden right now + m_hasFocus; // TRUE => our window has focus }; #endif // _WX_CARET_H_ diff --git a/samples/caret/caret.cpp b/samples/caret/caret.cpp index 36801972fc..b3f8ca5fe7 100644 --- a/samples/caret/caret.cpp +++ b/samples/caret/caret.cpp @@ -61,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 @@ -124,8 +125,7 @@ enum // menu items Caret_Quit = 1, Caret_About, - Caret_Test1, - Caret_Test2, + Caret_SetBlinkTime, // controls start here (the numbers are, of course, arbitrary) Caret_Text = 1000 @@ -141,6 +141,7 @@ enum BEGIN_EVENT_TABLE(MyFrame, wxFrame) 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 @@ -190,6 +191,8 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) // create a menu bar wxMenu *menuFile = new wxMenu; + menuFile->Append(Caret_SetBlinkTime, "&Blink time...\tCtrl-B"); + menuFile->AppendSeparator(); menuFile->Append(Caret_About, "&About...\tCtrl-A", "Show about dialog"); menuFile->AppendSeparator(); menuFile->Append(Caret_Quit, "E&xit\tAlt-X", "Quit this program"); @@ -219,9 +222,26 @@ void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { - wxMessageBox(_T("This is the about dialog of caret sample."), "About Caret", 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 @@ -292,10 +312,15 @@ void MyCanvas::OnSize( wxSizeEvent &event ) 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 ); diff --git a/src/generic/caret.cpp b/src/generic/caret.cpp index 2620630941..3371a270cd 100644 --- a/src/generic/caret.cpp +++ b/src/generic/caret.cpp @@ -1,6 +1,6 @@ /////////////////////////////////////////////////////////////////////////////// -// Name: generic/caret.h -// Purpose: generic wxCaret class +// Name: generic/caret.cpp +// Purpose: generic wxCaret class implementation // Author: Vadim Zeitlin (original code by Robert Roebling) // Modified by: // Created: 25.05.99 @@ -18,7 +18,7 @@ // ---------------------------------------------------------------------------- #ifdef __GNUG__ -#pragma implementation "caret.h" + #pragma implementation "caret.h" #endif // For compilers that support precompilation, includes "wx.h". @@ -46,6 +46,10 @@ static int gs_blinkTime = 500; // in milliseconds // implementation // ============================================================================ +// ---------------------------------------------------------------------------- +// timer stuff +// ---------------------------------------------------------------------------- + wxCaretTimer::wxCaretTimer(wxCaret *caret) { m_caret = caret; @@ -53,14 +57,20 @@ wxCaretTimer::wxCaretTimer(wxCaret *caret) void wxCaretTimer::Notify() { - m_caret->Blink(); + m_caret->OnTimer(); +} + +void wxCaret::OnTimer() +{ + // don't blink the caret when we don't have the focus + if ( m_hasFocus ) + Blink(); } // ---------------------------------------------------------------------------- // wxCaret static functions and data // ---------------------------------------------------------------------------- - int wxCaretBase::GetBlinkTime() { return gs_blinkTime; @@ -77,6 +87,8 @@ void wxCaretBase::SetBlinkTime(int milliseconds) void wxCaret::InitGeneric() { + m_hasFocus = TRUE; + m_blinkedOut = FALSE; } wxCaret::~wxCaret() @@ -119,6 +131,32 @@ void wxCaret::DoMove() //else: will be shown at the correct location next time it blinks } +// ---------------------------------------------------------------------------- +// handling the focus +// ---------------------------------------------------------------------------- + +void wxCaret::OnSetFocus() +{ + m_hasFocus = TRUE; + + Refresh(); +} + +void wxCaret::OnKillFocus() +{ + m_hasFocus = FALSE; + + if ( IsVisible() ) + { + // the caret must be shown - otherwise, if it is hidden now, it will + // stay so until the focus doesn't return because it won't blink any + // more + m_blinkedOut = FALSE; + } + + Refresh(); +} + // ---------------------------------------------------------------------------- // drawing the caret // ---------------------------------------------------------------------------- @@ -127,14 +165,23 @@ void wxCaret::Blink() { m_blinkedOut = !m_blinkedOut; - wxClientDC dc(GetWindow()); + Refresh(); +} + +void wxCaret::Refresh() +{ if ( !m_blinkedOut ) { + wxClientDC dc(GetWindow()); DoDraw(&dc); } else { - // FIXME can't be less efficient than this... (+1 needed!) + // FIXME can't be less efficient than this... we probably should use + // backing store for the caret instead of leaving all the burden + // of correct refresh logic handling to the user code + + // NB: +1 is needed! wxRect rect(m_x, m_y, m_width + 1, m_height + 1); GetWindow()->Refresh(FALSE, &rect); } @@ -143,8 +190,18 @@ void wxCaret::Blink() void wxCaret::DoDraw(wxDC *dc) { dc->SetPen( *wxBLACK_PEN ); + + // VZ: Robert's code for I-shaped caret - this is nice but doesn't look + // at all the same as the MSW version and I don't know how to indicate + // that the window has focus or not with such caret +#if 0 dc->DrawLine( m_x, m_y, m_x+m_width, m_y ); dc->DrawLine( m_x, m_y+m_height, m_x+m_width, m_y+m_height ); dc->DrawLine( m_x+(m_width/2), m_y, m_x+(m_width/2), m_y+m_height ); // dc->DrawLine( m_x+(m_width/2)+1, m_y, m_x+(m_width/2)+1, m_y+m_height ); +#else // 1 + if ( m_hasFocus ) + dc->SetBrush( *wxBLACK_BRUSH ); + dc->DrawRectangle( m_x, m_y, m_width, m_height ); +#endif // 0/1 } diff --git a/src/gtk/window.cpp b/src/gtk/window.cpp index 31056af60b..ce99feabb6 100644 --- a/src/gtk/window.cpp +++ b/src/gtk/window.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: window.cpp +// Name: gtk/window.cpp // Purpose: // Author: Robert Roebling // Id: $Id$ @@ -30,6 +30,10 @@ #include "wx/tooltip.h" #endif +#if wxUSE_CARET + #include "wx/caret.h" +#endif // wxUSE_CARET + #include "wx/menu.h" #include "wx/statusbr.h" #include "wx/intl.h" @@ -298,6 +302,28 @@ extern bool g_isIdle; // local code (see below) //----------------------------------------------------------------------------- +// returns the child of win which currently has focus or NULL if not found +static wxWindow *FindFocusedChild(wxWindow *win) +{ + wxWindow *winFocus = wxWindow::FindFocus(); + if ( !winFocus ) + return (wxWindow *)NULL; + + if ( winFocus == win ) + return win; + + for ( wxWindowList::Node *node = win->GetChildren().GetFirst(); + node; + node = node->GetNext() ) + { + wxWindow *child = FindFocusedChild(node->GetData()); + if ( child ) + return child; + } + + return (wxWindow *)NULL; +} + static void draw_frame( GtkWidget *widget, wxWindow *win ) { if (!win->m_hasVMT) @@ -1486,6 +1512,15 @@ static gint gtk_window_focus_in_callback( GtkWidget *widget, GdkEvent *WXUNUSED( gdk_im_begin(win->m_ic, win->m_wxwindow->window); #endif +#ifdef wxUSE_CARET + // caret needs to be informed about focus change + wxCaret *caret = win->GetCaret(); + if ( caret ) + { + caret->OnSetFocus(); + } +#endif // wxUSE_CARET + wxFocusEvent event( wxEVT_SET_FOCUS, win->GetId() ); event.SetEventObject( win ); @@ -1517,6 +1552,10 @@ static gint gtk_window_focus_out_callback( GtkWidget *widget, GdkEvent *WXUNUSED // g_sendActivateEvent to -1 g_sendActivateEvent = 0; + wxWindow *winFocus = FindFocusedChild(win); + if ( winFocus ) + win = winFocus; + g_focusWindow = (wxWindow *)NULL; /* @@ -1531,6 +1570,15 @@ static gint gtk_window_focus_out_callback( GtkWidget *widget, GdkEvent *WXUNUSED gdk_im_end(); #endif +#ifdef wxUSE_CARET + // caret needs to be informed about focus change + wxCaret *caret = win->GetCaret(); + if ( caret ) + { + caret->OnKillFocus(); + } +#endif // wxUSE_CARET + wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() ); event.SetEventObject( win ); diff --git a/src/gtk1/window.cpp b/src/gtk1/window.cpp index 31056af60b..ce99feabb6 100644 --- a/src/gtk1/window.cpp +++ b/src/gtk1/window.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: window.cpp +// Name: gtk/window.cpp // Purpose: // Author: Robert Roebling // Id: $Id$ @@ -30,6 +30,10 @@ #include "wx/tooltip.h" #endif +#if wxUSE_CARET + #include "wx/caret.h" +#endif // wxUSE_CARET + #include "wx/menu.h" #include "wx/statusbr.h" #include "wx/intl.h" @@ -298,6 +302,28 @@ extern bool g_isIdle; // local code (see below) //----------------------------------------------------------------------------- +// returns the child of win which currently has focus or NULL if not found +static wxWindow *FindFocusedChild(wxWindow *win) +{ + wxWindow *winFocus = wxWindow::FindFocus(); + if ( !winFocus ) + return (wxWindow *)NULL; + + if ( winFocus == win ) + return win; + + for ( wxWindowList::Node *node = win->GetChildren().GetFirst(); + node; + node = node->GetNext() ) + { + wxWindow *child = FindFocusedChild(node->GetData()); + if ( child ) + return child; + } + + return (wxWindow *)NULL; +} + static void draw_frame( GtkWidget *widget, wxWindow *win ) { if (!win->m_hasVMT) @@ -1486,6 +1512,15 @@ static gint gtk_window_focus_in_callback( GtkWidget *widget, GdkEvent *WXUNUSED( gdk_im_begin(win->m_ic, win->m_wxwindow->window); #endif +#ifdef wxUSE_CARET + // caret needs to be informed about focus change + wxCaret *caret = win->GetCaret(); + if ( caret ) + { + caret->OnSetFocus(); + } +#endif // wxUSE_CARET + wxFocusEvent event( wxEVT_SET_FOCUS, win->GetId() ); event.SetEventObject( win ); @@ -1517,6 +1552,10 @@ static gint gtk_window_focus_out_callback( GtkWidget *widget, GdkEvent *WXUNUSED // g_sendActivateEvent to -1 g_sendActivateEvent = 0; + wxWindow *winFocus = FindFocusedChild(win); + if ( winFocus ) + win = winFocus; + g_focusWindow = (wxWindow *)NULL; /* @@ -1531,6 +1570,15 @@ static gint gtk_window_focus_out_callback( GtkWidget *widget, GdkEvent *WXUNUSED gdk_im_end(); #endif +#ifdef wxUSE_CARET + // caret needs to be informed about focus change + wxCaret *caret = win->GetCaret(); + if ( caret ) + { + caret->OnKillFocus(); + } +#endif // wxUSE_CARET + wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() ); event.SetEventObject( win );