From 9cd6d737d5adc5fe415cddb3ef0024b9da2b9e08 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 30 Jul 2001 15:22:38 +0000 Subject: [PATCH] 1. made ScrollLines/Pages return bool indicating if we scrolled till the end or not 2. implemented them for wxGTK text ctrl git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@11215 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/window.tex | 26 ++++++++++++++--- include/wx/gtk/textctrl.h | 11 +++++++ include/wx/gtk1/textctrl.h | 11 +++++++ include/wx/msw/window.h | 4 +-- include/wx/window.h | 17 ++++++----- src/gtk/textctrl.cpp | 60 ++++++++++++++++++++++++++++++++++++++ src/gtk1/textctrl.cpp | 60 ++++++++++++++++++++++++++++++++++++++ src/msw/window.cpp | 55 ++++++++++++++++++++++------------ 8 files changed, 212 insertions(+), 32 deletions(-) diff --git a/docs/latex/wx/window.tex b/docs/latex/wx/window.tex index 43697c7b0f..4332f54142 100644 --- a/docs/latex/wx/window.tex +++ b/docs/latex/wx/window.tex @@ -1807,12 +1807,21 @@ implements the following methods:\par \membersection{wxWindow::ScrollLines}\label{wxwindowscrolllines} -\func{virtual void}{ScrollLines}{\param{int }{lines}} +\func{virtual bool}{ScrollLines}{\param{int }{lines}} Scrolls the window by the given number of lines down (if {\it lines} is positive) or up. -This function is currently only implemented under MSW. +\wxheading{Return value} + +Returns {\tt TRUE} if the window was scrolled, {\tt FALSE} if it was already +on top/bottom and nothing was done. + +\wxheading{Remarks} + +This function is currently only implemented under MSW and wxTextCtrl under +wxGTK (it also works for wxScrolledWindow derived classes under all +platforms). \wxheading{See also} @@ -1820,12 +1829,21 @@ This function is currently only implemented under MSW. \membersection{wxWindow::ScrollPages}\label{wxwindowscrollpages} -\func{virtual void}{ScrollPages}{\param{int }{pages}} +\func{virtual bool}{ScrollPages}{\param{int }{pages}} Scrolls the window by the given number of pages down (if {\it pages} is positive) or up. -This function is currently only implemented under MSW. +\wxheading{Return value} + +Returns {\tt TRUE} if the window was scrolled, {\tt FALSE} if it was already +on top/bottom and nothing was done. + +\wxheading{Remarks} + +This function is currently only implemented under MSW and wxTextCtrl under +wxGTK (it also works for wxScrolledWindow derived classes under all +platforms). \wxheading{See also} diff --git a/include/wx/gtk/textctrl.h b/include/wx/gtk/textctrl.h index bc5d527d11..085d5ca4b6 100644 --- a/include/wx/gtk/textctrl.h +++ b/include/wx/gtk/textctrl.h @@ -148,6 +148,10 @@ public: virtual void Freeze(); virtual void Thaw(); + // textctrl specific scrolling + virtual bool ScrollLines(int lines); + virtual bool ScrollPages(int pages); + // wxGTK-specific: called recursively by Enable, // to give widgets an oppprtunity to correct their colours after they // have been changed by Enable @@ -159,6 +163,13 @@ protected: // common part of all ctors void Init(); + // get the vertical adjustment, if any, NULL otherwise + GtkAdjustment *GetVAdj() const; + + // scroll the control by the given number of pixels, return true if the + // scroll position changed + bool DoScroll(GtkAdjustment *adj, int diff); + private: // change the font for everything in this control void ChangeFontGlobally(); diff --git a/include/wx/gtk1/textctrl.h b/include/wx/gtk1/textctrl.h index bc5d527d11..085d5ca4b6 100644 --- a/include/wx/gtk1/textctrl.h +++ b/include/wx/gtk1/textctrl.h @@ -148,6 +148,10 @@ public: virtual void Freeze(); virtual void Thaw(); + // textctrl specific scrolling + virtual bool ScrollLines(int lines); + virtual bool ScrollPages(int pages); + // wxGTK-specific: called recursively by Enable, // to give widgets an oppprtunity to correct their colours after they // have been changed by Enable @@ -159,6 +163,13 @@ protected: // common part of all ctors void Init(); + // get the vertical adjustment, if any, NULL otherwise + GtkAdjustment *GetVAdj() const; + + // scroll the control by the given number of pixels, return true if the + // scroll position changed + bool DoScroll(GtkAdjustment *adj, int diff); + private: // change the font for everything in this control void ChangeFontGlobally(); diff --git a/include/wx/msw/window.h b/include/wx/msw/window.h index 6e654f53f1..27432a415d 100644 --- a/include/wx/msw/window.h +++ b/include/wx/msw/window.h @@ -118,8 +118,8 @@ public: virtual void ScrollWindow( int dx, int dy, const wxRect* rect = (wxRect *) NULL ); - virtual void ScrollLines(int lines); - virtual void ScrollPages(int pages); + virtual bool ScrollLines(int lines); + virtual bool ScrollPages(int pages); #if wxUSE_DRAG_AND_DROP virtual void SetDropTarget( wxDropTarget *dropTarget ); diff --git a/include/wx/window.h b/include/wx/window.h index f1534578e3..3f453930d6 100644 --- a/include/wx/window.h +++ b/include/wx/window.h @@ -664,13 +664,16 @@ public: const wxRect* rect = (wxRect *) NULL ) = 0; // scrolls window by line/page: note that not all controls support this - virtual void ScrollLines(int WXUNUSED(lines)) { } - virtual void ScrollPages(int WXUNUSED(pages)) { } - - void LineUp() { ScrollLines(-1); } - void LineDown() { ScrollLines(1); } - void PageUp() { ScrollPages(-1); } - void PageDown() { ScrollPages(1); } + // + // return TRUE if the position changed, FALSE otherwise + virtual bool ScrollLines(int WXUNUSED(lines)) { return FALSE; } + virtual bool ScrollPages(int WXUNUSED(pages)) { return FALSE; } + + // convenient wrappers for ScrollLines/Pages + bool LineUp() { return ScrollLines(-1); } + bool LineDown() { return ScrollLines(1); } + bool PageUp() { return ScrollPages(-1); } + bool PageDown() { return ScrollPages(1); } // context-sensitive help // ---------------------- diff --git a/src/gtk/textctrl.cpp b/src/gtk/textctrl.cpp index 23abc60f42..05ccf01a77 100644 --- a/src/gtk/textctrl.cpp +++ b/src/gtk/textctrl.cpp @@ -1296,6 +1296,10 @@ wxSize wxTextCtrl::DoGetBestSize() const return wxSize(80, ret.y); } +// ---------------------------------------------------------------------------- +// freeze/thaw +// ---------------------------------------------------------------------------- + void wxTextCtrl::Freeze() { if ( HasFlag(wxTE_MULTILINE) ) @@ -1311,3 +1315,59 @@ void wxTextCtrl::Thaw() gtk_text_thaw(GTK_TEXT(m_text)); } } + +// ---------------------------------------------------------------------------- +// scrolling +// ---------------------------------------------------------------------------- + +GtkAdjustment *wxTextCtrl::GetVAdj() const +{ + return HasFlag(wxTE_MULTILINE) ? GTK_TEXT(m_text)->vadj : NULL; +} + +bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff) +{ + float value = adj->value + diff; + + if ( value < 0 ) + value = 0; + + float upper = adj->upper - adj->page_size; + if ( value > upper ) + value = upper; + + // did we noticeably change the scroll position? + if ( fabs(adj->value - value) < 0.2 ) + { + // well, this is what Robert does in wxScrollBar, so it must be good... + return FALSE; + } + + adj->value = value; + + gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed"); + + return TRUE; +} + +bool wxTextCtrl::ScrollLines(int lines) +{ + GtkAdjustment *adj = GetVAdj(); + if ( !adj ) + return FALSE; + + // this is hardcoded to 10 in GTK+ 1.2 (great idea) + static const int KEY_SCROLL_PIXELS = 10; + + return DoScroll(adj, lines*KEY_SCROLL_PIXELS); +} + +bool wxTextCtrl::ScrollPages(int pages) +{ + GtkAdjustment *adj = GetVAdj(); + if ( !adj ) + return FALSE; + + return DoScroll(adj, pages*adj->page_increment); +} + diff --git a/src/gtk1/textctrl.cpp b/src/gtk1/textctrl.cpp index 23abc60f42..05ccf01a77 100644 --- a/src/gtk1/textctrl.cpp +++ b/src/gtk1/textctrl.cpp @@ -1296,6 +1296,10 @@ wxSize wxTextCtrl::DoGetBestSize() const return wxSize(80, ret.y); } +// ---------------------------------------------------------------------------- +// freeze/thaw +// ---------------------------------------------------------------------------- + void wxTextCtrl::Freeze() { if ( HasFlag(wxTE_MULTILINE) ) @@ -1311,3 +1315,59 @@ void wxTextCtrl::Thaw() gtk_text_thaw(GTK_TEXT(m_text)); } } + +// ---------------------------------------------------------------------------- +// scrolling +// ---------------------------------------------------------------------------- + +GtkAdjustment *wxTextCtrl::GetVAdj() const +{ + return HasFlag(wxTE_MULTILINE) ? GTK_TEXT(m_text)->vadj : NULL; +} + +bool wxTextCtrl::DoScroll(GtkAdjustment *adj, int diff) +{ + float value = adj->value + diff; + + if ( value < 0 ) + value = 0; + + float upper = adj->upper - adj->page_size; + if ( value > upper ) + value = upper; + + // did we noticeably change the scroll position? + if ( fabs(adj->value - value) < 0.2 ) + { + // well, this is what Robert does in wxScrollBar, so it must be good... + return FALSE; + } + + adj->value = value; + + gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed"); + + return TRUE; +} + +bool wxTextCtrl::ScrollLines(int lines) +{ + GtkAdjustment *adj = GetVAdj(); + if ( !adj ) + return FALSE; + + // this is hardcoded to 10 in GTK+ 1.2 (great idea) + static const int KEY_SCROLL_PIXELS = 10; + + return DoScroll(adj, lines*KEY_SCROLL_PIXELS); +} + +bool wxTextCtrl::ScrollPages(int pages) +{ + GtkAdjustment *adj = GetVAdj(); + if ( !adj ) + return FALSE; + + return DoScroll(adj, pages*adj->page_increment); +} + diff --git a/src/msw/window.cpp b/src/msw/window.cpp index b3a276a34a..989cf2021b 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -770,6 +770,15 @@ int wxWindowMSW::GetScrollPage(int orient) const #endif // WXWIN_COMPATIBILITY +inline int GetScrollPosition(HWND hWnd, int wOrient) +{ +#ifdef __WXMICROWIN__ + return ::GetScrollPosWX(hWnd, wOrient); +#else + return ::GetScrollPos(hWnd, wOrient); +#endif +} + int wxWindowMSW::GetScrollPos(int orient) const { int wOrient; @@ -777,17 +786,11 @@ int wxWindowMSW::GetScrollPos(int orient) const wOrient = SB_HORZ; else wOrient = SB_VERT; + HWND hWnd = GetHwnd(); - if ( hWnd ) - { -#ifdef __WXMICROWIN__ - return ::GetScrollPosWX(hWnd, wOrient); -#else - return ::GetScrollPos(hWnd, wOrient); -#endif - } - else - return 0; + wxCHECK_MSG( hWnd, 0, _T("no HWND in GetScrollPos") ); + + return GetScrollPosition(hWnd, wOrient); } // This now returns the whole range, not just the number @@ -940,30 +943,44 @@ void wxWindowMSW::ScrollWindow(int dx, int dy, const wxRect *prect) ::ScrollWindow(GetHwnd(), dx, dy, prect ? &rect : NULL, NULL); } -static void ScrollVertically(HWND hwnd, int kind, int count) +static bool ScrollVertically(HWND hwnd, int kind, int count) { + int posStart = GetScrollPosition(hwnd, SB_VERT); + + int pos = posStart; for ( int n = 0; n < count; n++ ) { ::SendMessage(hwnd, WM_VSCROLL, kind, 0); + + int posNew = GetScrollPosition(hwnd, SB_VERT); + if ( posNew == pos ) + { + // don't bother to continue, we're already at top/bottom + break; + } + + pos = posNew; } + + return pos != posStart; } -void wxWindowMSW::ScrollLines(int lines) +bool wxWindowMSW::ScrollLines(int lines) { bool down = lines > 0; - ScrollVertically(GetHwnd(), - down ? SB_LINEDOWN : SB_LINEUP, - down ? lines : -lines); + return ScrollVertically(GetHwnd(), + down ? SB_LINEDOWN : SB_LINEUP, + down ? lines : -lines); } -void wxWindowMSW::ScrollPages(int pages) +bool wxWindowMSW::ScrollPages(int pages) { bool down = pages > 0; - ScrollVertically(GetHwnd(), - down ? SB_PAGEDOWN : SB_PAGEUP, - down ? pages : -pages); + return ScrollVertically(GetHwnd(), + down ? SB_PAGEDOWN : SB_PAGEUP, + down ? pages : -pages); } // --------------------------------------------------------------------------- -- 2.45.2