1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/scrolwin.cpp
3 // Purpose: wxScrolledWindow implementation
4 // Author: Robert Roebling
5 // Modified by: Ron Lee
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "scrolwin.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 #include "wx/scrolwin.h"
33 #include "wx/dcclient.h"
37 #include "wx/gtk/private.h"
38 #include "wx/gtk/win_gtk.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
45 EVT_SCROLLWIN(wxScrolledWindow::OnScroll
)
46 EVT_SIZE(wxScrolledWindow::OnSize
)
47 EVT_PAINT(wxScrolledWindow::OnPaint
)
48 EVT_CHAR(wxScrolledWindow::OnChar
)
51 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
53 // ============================================================================
55 // ============================================================================
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 extern bool g_blockEventsOnDrag
;
62 extern bool g_blockEventsOnScroll
;
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
68 extern void wxapp_install_idle_handler();
71 //-----------------------------------------------------------------------------
72 // "value_changed" from m_vAdjust
73 //-----------------------------------------------------------------------------
76 static void gtk_scrolled_window_vscroll_callback( GtkAdjustment
*adjust
,
78 wxScrolledWindow
*win
)
81 wxapp_install_idle_handler();
83 if (g_blockEventsOnDrag
) return;
85 if (!win
->m_hasVMT
) return;
87 win
->GtkVScroll( adjust
->value
,
88 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win
->m_widget
)->vscrollbar
) );
92 //-----------------------------------------------------------------------------
93 // "value_changed" from m_hAdjust
94 //-----------------------------------------------------------------------------
97 static void gtk_scrolled_window_hscroll_callback( GtkAdjustment
*adjust
,
99 wxScrolledWindow
*win
)
102 wxapp_install_idle_handler();
104 if (g_blockEventsOnDrag
) return;
105 if (!win
->m_hasVMT
) return;
107 win
->GtkHScroll( adjust
->value
,
108 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win
->m_widget
)->hscrollbar
) );
112 //-----------------------------------------------------------------------------
113 // "button_press_event" from scrollbar
114 //-----------------------------------------------------------------------------
117 static gint
gtk_scrollbar_button_press_callback( GtkRange
*widget
,
118 GdkEventButton
*gdk_event
,
122 wxapp_install_idle_handler();
124 g_blockEventsOnScroll
= TRUE
;
126 // FIXME: there is no slider field any more, what was meant here?
128 win
->m_isScrolling
= (gdk_event
->window
== widget
->slider
);
135 //-----------------------------------------------------------------------------
136 // "button_release_event" from scrollbar
137 //-----------------------------------------------------------------------------
140 static gint
gtk_scrollbar_button_release_callback( GtkRange
*widget
,
141 GdkEventButton
*WXUNUSED(gdk_event
),
144 // don't test here as we can release the mouse while being over
145 // a different window than the slider
147 // if (gdk_event->window != widget->slider) return FALSE;
149 g_blockEventsOnScroll
= FALSE
;
151 if (win
->m_isScrolling
)
153 wxEventType command
= wxEVT_SCROLLWIN_THUMBRELEASE
;
157 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(win
->m_widget
);
158 if (widget
== GTK_RANGE(scrolledWindow
->hscrollbar
))
160 value
= (int)(win
->m_hAdjust
->value
+0.5);
163 if (widget
== GTK_RANGE(scrolledWindow
->vscrollbar
))
165 value
= (int)(win
->m_vAdjust
->value
+0.5);
169 wxScrollWinEvent
event( command
, value
, dir
);
170 event
.SetEventObject( win
);
171 win
->GetEventHandler()->ProcessEvent( event
);
174 win
->m_isScrolling
= FALSE
;
180 //-----------------------------------------------------------------------------
181 // InsertChild for wxScrolledWindow
182 //-----------------------------------------------------------------------------
184 static void wxInsertChildInScrolledWindow( wxWindow
* parent
, wxWindow
* child
)
186 // The window might have been scrolled already, do we
187 // have to adapt the position.
188 GtkPizza
*pizza
= GTK_PIZZA(parent
->m_wxwindow
);
189 child
->m_x
+= pizza
->xoffset
;
190 child
->m_y
+= pizza
->yoffset
;
192 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
193 GTK_WIDGET(child
->m_widget
),
200 // ----------------------------------------------------------------------------
201 // wxScrolledWindow creation
202 // ----------------------------------------------------------------------------
204 void wxScrolledWindow::Init()
206 m_xScrollPixelsPerLine
= 0;
207 m_yScrollPixelsPerLine
= 0;
208 m_xScrollingEnabled
= TRUE
;
209 m_yScrollingEnabled
= TRUE
;
210 m_xScrollPosition
= 0;
211 m_yScrollPosition
= 0;
212 m_xScrollLinesPerPage
= 0;
213 m_yScrollLinesPerPage
= 0;
214 m_targetWindow
= (wxWindow
*) NULL
;
217 m_hasScrolling
= TRUE
;
220 bool wxScrolledWindow::Create(wxWindow
*parent
,
225 const wxString
& name
)
229 if (!PreCreation( parent
, pos
, size
) ||
230 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
232 wxFAIL_MSG( wxT("wxWindow creation failed") );
236 m_insertCallback
= wxInsertChildInScrolledWindow
;
238 m_targetWindow
= this;
240 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
241 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
243 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(m_widget
);
245 GtkScrolledWindowClass
*scroll_class
= GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
246 scroll_class
->scrollbar_spacing
= 0;
248 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
250 m_hAdjust
= gtk_range_get_adjustment( GTK_RANGE(scrolledWindow
->hscrollbar
) );
251 m_vAdjust
= gtk_range_get_adjustment( GTK_RANGE(scrolledWindow
->vscrollbar
) );
253 m_wxwindow
= gtk_pizza_new();
255 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
257 GtkPizza
*pizza
= GTK_PIZZA(m_wxwindow
);
259 if (HasFlag(wxRAISED_BORDER
))
261 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_OUT
);
263 else if (HasFlag(wxSUNKEN_BORDER
))
265 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_IN
);
267 else if (HasFlag(wxSIMPLE_BORDER
))
269 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_THIN
);
273 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_NONE
);
276 GTK_WIDGET_SET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
277 m_acceptsFocus
= TRUE
;
279 // I _really_ don't want scrollbars in the beginning
280 m_vAdjust
->lower
= 0.0;
281 m_vAdjust
->upper
= 1.0;
282 m_vAdjust
->value
= 0.0;
283 m_vAdjust
->step_increment
= 1.0;
284 m_vAdjust
->page_increment
= 2.0;
285 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "changed" );
286 m_hAdjust
->lower
= 0.0;
287 m_hAdjust
->upper
= 1.0;
288 m_hAdjust
->value
= 0.0;
289 m_hAdjust
->step_increment
= 1.0;
290 m_hAdjust
->page_increment
= 2.0;
291 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "changed" );
293 // Handlers for new scrollbar values
297 // these handlers block mouse events to any window during scrolling such as
298 // motion events and prevent GTK and wxWidgets from fighting over where the
301 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->vscrollbar
), "button_press_event",
302 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
, (gpointer
) this );
304 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->hscrollbar
), "button_press_event",
305 (GtkSignalFunc
)gtk_scrollbar_button_press_callback
, (gpointer
) this );
307 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->vscrollbar
), "button_release_event",
308 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
, (gpointer
) this );
310 gtk_signal_connect( GTK_OBJECT(scrolledWindow
->hscrollbar
), "button_release_event",
311 (GtkSignalFunc
)gtk_scrollbar_button_release_callback
, (gpointer
) this );
313 gtk_widget_show( m_wxwindow
);
316 m_parent
->DoAddChild( this );
318 m_focusWidget
= m_wxwindow
;
327 // ----------------------------------------------------------------------------
328 // setting scrolling parameters
329 // ----------------------------------------------------------------------------
331 void wxScrolledWindow::DoSetVirtualSize( int x
, int y
)
333 wxPanel::DoSetVirtualSize( x
, y
);
340 // wxWindow's GetBestVirtualSize returns the actual window size,
341 // whereas we want to return the virtual size
342 wxSize
wxScrolledWindow::GetBestVirtualSize() const
344 wxSize
clientSize( GetClientSize() );
347 wxSize
minSize( GetSizer()->CalcMin() );
349 return wxSize( wxMax( clientSize
.x
, minSize
.x
), wxMax( clientSize
.y
, minSize
.y
) );
355 // return the size best suited for the current window
356 // (this isn't a virtual size, this is a sensible size for the window)
357 wxSize
wxScrolledWindow::DoGetBestSize() const
363 wxSize b
= GetSizer()->GetMinSize();
365 // Only use the content to set the window size in the direction
366 // where there's no scrolling; otherwise we're going to get a huge
367 // window in the direction in which scrolling is enabled
369 GetScrollPixelsPerUnit(& ppuX
, & ppuY
);
372 if ( GetMinSize().IsFullySpecified() )
373 minSize
= GetMinSize();
384 return wxWindow::DoGetBestSize();
386 // Add any difference between size and client size
387 wxSize diff
= GetSize() - GetClientSize();
388 best
.x
+= wxMax(0, diff
.x
);
389 best
.y
+= wxMax(0, diff
.y
);
395 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
396 * noUnitsX/noUnitsY: : no. units per scrollbar
398 void wxScrolledWindow::SetScrollbars( int pixelsPerUnitX
, int pixelsPerUnitY
,
399 int noUnitsX
, int noUnitsY
,
400 int xPos
, int yPos
, bool noRefresh
)
403 GetViewStart (& xs
, & ys
);
405 int old_x
= m_xScrollPixelsPerLine
* xs
;
406 int old_y
= m_yScrollPixelsPerLine
* ys
;
408 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
409 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
411 m_hAdjust
->value
= m_xScrollPosition
= xPos
;
412 m_vAdjust
->value
= m_yScrollPosition
= yPos
;
414 // Setting hints here should arguably be deprecated, but without it
415 // a sizer might override this manual scrollbar setting in old code.
416 // m_targetWindow->SetVirtualSizeHints( noUnitsX * pixelsPerUnitX, noUnitsY * pixelsPerUnitY );
418 int w
= noUnitsX
* pixelsPerUnitX
;
419 int h
= noUnitsY
* pixelsPerUnitY
;
420 m_targetWindow
->SetVirtualSize( w
? w
: wxDefaultCoord
,
421 h
? h
: wxDefaultCoord
);
425 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
426 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
428 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
432 void wxScrolledWindow::AdjustScrollbars()
437 m_targetWindow
->GetClientSize( &w
, &h
);
438 m_targetWindow
->GetVirtualSize( &vw
, &vh
);
440 if (m_xScrollPixelsPerLine
== 0)
442 m_hAdjust
->upper
= 1.0;
443 m_hAdjust
->page_increment
= 1.0;
444 m_hAdjust
->page_size
= 1.0;
448 m_hAdjust
->upper
= (vw
+m_xScrollPixelsPerLine
-1) / m_xScrollPixelsPerLine
;
449 m_hAdjust
->page_size
= w
/ m_xScrollPixelsPerLine
;
450 m_hAdjust
->page_increment
= w
/ m_xScrollPixelsPerLine
;
452 // Special case. When client and virtual size are very close but
453 // the client is big enough, kill scrollbar.
455 if ((m_hAdjust
->page_size
< m_hAdjust
->upper
) && (w
>= vw
))
456 m_hAdjust
->page_size
+= 1.0;
458 // If the scrollbar hits the right side, move the window
459 // right to keep it from over extending.
461 if ((m_hAdjust
->value
!= 0.0) && (m_hAdjust
->value
+ m_hAdjust
->page_size
> m_hAdjust
->upper
))
463 m_hAdjust
->value
= m_hAdjust
->upper
- m_hAdjust
->page_size
;
464 if (m_hAdjust
->value
< 0.0)
465 m_hAdjust
->value
= 0.0;
467 if (GetChildren().GetCount() == 0)
468 m_xScrollPosition
= (int)m_hAdjust
->value
; // This is enough without child windows
470 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" ); // Actually scroll window
474 if (m_yScrollPixelsPerLine
== 0)
476 m_vAdjust
->upper
= 1.0;
477 m_vAdjust
->page_increment
= 1.0;
478 m_vAdjust
->page_size
= 1.0;
482 m_vAdjust
->upper
= (vh
+m_yScrollPixelsPerLine
-1) / m_yScrollPixelsPerLine
;
483 m_vAdjust
->page_size
= h
/ m_yScrollPixelsPerLine
;
484 m_vAdjust
->page_increment
= h
/ m_yScrollPixelsPerLine
;
486 if ((m_vAdjust
->page_size
< m_vAdjust
->upper
) && (h
>= vh
))
487 m_vAdjust
->page_size
+= 1.0;
489 if ((m_vAdjust
->value
!= 0.0) && (m_vAdjust
->value
+ m_vAdjust
->page_size
> m_vAdjust
->upper
))
491 m_vAdjust
->value
= m_vAdjust
->upper
- m_vAdjust
->page_size
;
492 if (m_vAdjust
->value
< 0.0)
493 m_vAdjust
->value
= 0.0;
495 if (GetChildren().GetCount() == 0)
496 m_yScrollPosition
= (int)m_vAdjust
->value
;
498 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
502 m_xScrollLinesPerPage
= (int)(m_hAdjust
->page_increment
+ 0.5);
503 m_yScrollLinesPerPage
= (int)(m_vAdjust
->page_increment
+ 0.5);
505 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "changed" );
506 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "changed" );
510 // ----------------------------------------------------------------------------
511 // target window handling
512 // ----------------------------------------------------------------------------
514 void wxScrolledWindow::SetTargetWindow( wxWindow
*target
, bool WXUNUSED(pushEventHandler
) )
516 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
517 m_targetWindow
= target
;
520 wxWindow
*wxScrolledWindow::GetTargetWindow() const
522 return m_targetWindow
;
525 // Override this function if you don't want to have wxScrolledWindow
526 // automatically change the origin according to the scroll position.
527 void wxScrolledWindow::DoPrepareDC(wxDC
& dc
)
529 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
530 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
533 void wxScrolledWindow::SetScrollRate( int xstep
, int ystep
)
535 int old_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
536 int old_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
538 m_xScrollPixelsPerLine
= xstep
;
539 m_yScrollPixelsPerLine
= ystep
;
541 int new_x
= m_xScrollPixelsPerLine
* m_xScrollPosition
;
542 int new_y
= m_yScrollPixelsPerLine
* m_yScrollPosition
;
544 m_targetWindow
->ScrollWindow( old_x
- new_x
, old_y
- new_y
);
549 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
552 *x_unit
= m_xScrollPixelsPerLine
;
554 *y_unit
= m_yScrollPixelsPerLine
;
557 int wxScrolledWindow::GetScrollPageSize(int orient
) const
559 if ( orient
== wxHORIZONTAL
)
560 return m_xScrollLinesPerPage
;
562 return m_yScrollLinesPerPage
;
565 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
567 if ( orient
== wxHORIZONTAL
)
568 m_xScrollLinesPerPage
= pageSize
;
570 m_yScrollLinesPerPage
= pageSize
;
573 void wxScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
575 int orient
= event
.GetOrientation();
577 int nScrollInc
= CalcScrollInc(event
);
578 if (nScrollInc
== 0) return;
580 if (orient
== wxHORIZONTAL
)
582 int newPos
= m_xScrollPosition
+ nScrollInc
;
583 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
587 int newPos
= m_yScrollPosition
+ nScrollInc
;
588 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
591 if (orient
== wxHORIZONTAL
)
593 m_xScrollPosition
+= nScrollInc
;
597 m_yScrollPosition
+= nScrollInc
;
600 if (orient
== wxHORIZONTAL
)
602 if (m_xScrollingEnabled
)
603 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
605 m_targetWindow
->Refresh();
609 if (m_yScrollingEnabled
)
610 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
612 m_targetWindow
->Refresh();
616 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
618 wxASSERT_MSG( m_targetWindow
!= 0, _T("No target window") );
620 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
621 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
623 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
625 int max
= (int)(m_hAdjust
->upper
- m_hAdjust
->page_size
+ 0.5);
626 if (max
< 0) max
= 0;
627 if (x_pos
> max
) x_pos
= max
;
628 if (x_pos
< 0) x_pos
= 0;
630 int old_x
= m_xScrollPosition
;
631 m_xScrollPosition
= x_pos
;
632 m_hAdjust
->value
= x_pos
;
634 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
636 // Just update the scrollbar, don't send any wxWidgets event
637 GtkHDisconnectEvent();
638 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" );
642 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
644 int max
= (int)(m_vAdjust
->upper
- m_vAdjust
->page_size
+ 0.5);
645 if (max
< 0) max
= 0;
646 if (y_pos
> max
) y_pos
= max
;
647 if (y_pos
< 0) y_pos
= 0;
649 int old_y
= m_yScrollPosition
;
650 m_yScrollPosition
= y_pos
;
651 m_vAdjust
->value
= y_pos
;
653 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
655 // Just update the scrollbar, don't send any wxWidgets event
656 GtkVDisconnectEvent();
657 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
662 // TODO: [VH]Scroll functions should be combined
664 void wxScrolledWindow::GtkVScroll( float value
, unsigned int scroll_type
)
666 wxASSERT_MSG( m_targetWindow
!= 0, _T("No target window") );
668 if (m_yScrollPixelsPerLine
== 0)
671 int y_pos
= (int)(value
+0.5);
673 if (y_pos
== m_yScrollPosition
)
676 wxEventType command
= GtkScrollWinTypeToWx(scroll_type
);
678 wxScrollWinEvent
event( command
, y_pos
, wxVERTICAL
);
679 event
.SetEventObject( this );
680 GetEventHandler()->ProcessEvent( event
);
683 void wxScrolledWindow::GtkHScroll( float value
, unsigned int scroll_type
)
685 wxASSERT_MSG( m_targetWindow
!= 0, _T("No target window") );
687 if (m_xScrollPixelsPerLine
== 0)
690 int x_pos
= (int)(value
+0.5);
692 if (x_pos
== m_xScrollPosition
)
695 wxEventType command
= GtkScrollWinTypeToWx(scroll_type
);
697 wxScrollWinEvent
event( command
, x_pos
, wxHORIZONTAL
);
698 event
.SetEventObject( this );
699 GetEventHandler()->ProcessEvent( event
);
702 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
704 m_xScrollingEnabled
= x_scroll
;
705 m_yScrollingEnabled
= y_scroll
;
708 // Where the current view starts from
709 void wxScrolledWindow::GetViewStart (int *x
, int *y
) const
712 *x
= m_xScrollPosition
;
714 *y
= m_yScrollPosition
;
717 void wxScrolledWindow::DoCalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
720 GetViewStart (& xs
, & ys
);
723 *xx
= x
- xs
* m_xScrollPixelsPerLine
;
725 *yy
= y
- ys
* m_yScrollPixelsPerLine
;
728 void wxScrolledWindow::DoCalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
731 GetViewStart (& xs
, & ys
);
734 *xx
= x
+ xs
* m_xScrollPixelsPerLine
;
736 *yy
= y
+ ys
* m_yScrollPixelsPerLine
;
739 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
741 int pos
= event
.GetPosition();
742 int orient
= event
.GetOrientation();
745 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
747 if (orient
== wxHORIZONTAL
)
748 nScrollInc
= - m_xScrollPosition
;
750 nScrollInc
= - m_yScrollPosition
;
752 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
754 if (orient
== wxHORIZONTAL
)
755 nScrollInc
= GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine
- m_xScrollPosition
;
757 nScrollInc
= GetVirtualSize().GetHeight() / m_yScrollPixelsPerLine
- m_yScrollPosition
;
759 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
763 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
767 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
769 if (orient
== wxHORIZONTAL
)
770 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
772 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
774 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
776 if (orient
== wxHORIZONTAL
)
777 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
779 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
781 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
782 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
784 if (orient
== wxHORIZONTAL
)
785 nScrollInc
= pos
- m_xScrollPosition
;
787 nScrollInc
= pos
- m_yScrollPosition
;
790 if (orient
== wxHORIZONTAL
)
792 if (m_xScrollPixelsPerLine
> 0)
794 int max
= (int)(m_hAdjust
->upper
- m_hAdjust
->page_size
+ 0.5);
795 if (max
< 0) max
= 0;
797 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
798 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
799 else if ( (m_xScrollPosition
+ nScrollInc
) > max
)
800 nScrollInc
= max
- m_xScrollPosition
; // As +ve as we can go
803 m_targetWindow
->Refresh();
807 if (m_yScrollPixelsPerLine
> 0)
809 int max
= (int)(m_vAdjust
->upper
- m_vAdjust
->page_size
+ 0.5);
810 if (max
< 0) max
= 0;
812 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
813 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
814 else if ( (m_yScrollPosition
+ nScrollInc
) > max
)
815 nScrollInc
= max
- m_yScrollPosition
; // As +ve as we can go
818 m_targetWindow
->Refresh();
824 void wxScrolledWindow::SetScrollPos( int orient
, int pos
, bool refresh
)
826 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
828 wxCHECK_RET( m_wxwindow
!= NULL
, wxT("window needs client area for scrolling") );
830 if (orient
== wxHORIZONTAL
)
832 int max
= (int)(m_hAdjust
->upper
- m_hAdjust
->page_size
+ 0.5);
833 if (max
< 0) max
= 0;
835 if (pos
> max
) pos
= 0;
836 if (pos
< 0) pos
= 0;
838 if (pos
== (int)(m_hAdjust
->value
+0.5)) return;
839 m_hAdjust
->value
= pos
;
843 int max
= (int)(m_vAdjust
->upper
- m_vAdjust
->page_size
+ 0.5);
844 if (max
< 0) max
= 0;
846 if (pos
> max
) pos
= 0;
847 if (pos
< 0) pos
= 0;
849 if (pos
== (int)(m_vAdjust
->value
+0.5)) return;
850 m_vAdjust
->value
= pos
;
853 if (m_wxwindow
->window
)
855 if (orient
== wxHORIZONTAL
)
857 // Just update the scrollbar, don't send any wxWidgets event
858 GtkHDisconnectEvent();
859 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" );
864 // Just update the scrollbar, don't send any wxWidgets event
865 GtkVDisconnectEvent();
866 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
872 void wxScrolledWindow::GtkVConnectEvent()
874 gtk_signal_connect( GTK_OBJECT(m_vAdjust
), "value_changed",
875 (GtkSignalFunc
) gtk_scrolled_window_vscroll_callback
, (gpointer
) this );
878 void wxScrolledWindow::GtkHConnectEvent()
880 gtk_signal_connect( GTK_OBJECT(m_hAdjust
), "value_changed",
881 (GtkSignalFunc
) gtk_scrolled_window_hscroll_callback
, (gpointer
) this );
884 void wxScrolledWindow::GtkHDisconnectEvent()
886 gtk_signal_disconnect_by_func( GTK_OBJECT(m_hAdjust
),
887 (GtkSignalFunc
) gtk_scrolled_window_hscroll_callback
, (gpointer
) this );
890 void wxScrolledWindow::GtkVDisconnectEvent()
892 gtk_signal_disconnect_by_func( GTK_OBJECT(m_vAdjust
),
893 (GtkSignalFunc
) gtk_scrolled_window_vscroll_callback
, (gpointer
) this );
896 bool wxScrolledWindow::Layout()
898 if (GetSizer() && m_targetWindow
== this)
900 // If we're the scroll target, take into account the
901 // virtual size and scrolled position of the window.
904 CalcScrolledPosition(0,0, &x
,&y
);
905 GetVirtualSize(&w
, &h
);
906 GetSizer()->SetDimension(x
, y
, w
, h
);
910 return wxPanel::Layout(); // fall back to default for LayoutConstraints
913 // ----------------------------------------------------------------------------
915 // ----------------------------------------------------------------------------
917 // Default OnSize resets scrollbars, if any
918 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
920 if ( m_targetWindow
->GetAutoLayout() )
922 wxSize size
= m_targetWindow
->GetBestVirtualSize();
924 // This will call ::Layout() and ::AdjustScrollbars()
925 SetVirtualSize( size
);
933 // This calls OnDraw, having adjusted the origin according to the current
935 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
943 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
944 // compatibility here - if we used OnKeyDown(), the programs which process
945 // arrows themselves in their OnChar() would never get the message and like
946 // this they always have the priority
947 void wxScrolledWindow::OnChar(wxKeyEvent
& event
)
949 int stx
, sty
, // view origin
950 szx
, szy
, // view size (total)
951 clix
, cliy
; // view size (on screen)
953 GetViewStart(&stx
, &sty
);
954 GetClientSize(&clix
, &cliy
);
955 GetVirtualSize(&szx
, &szy
);
957 if( m_xScrollPixelsPerLine
)
959 clix
/= m_xScrollPixelsPerLine
;
960 szx
/= m_xScrollPixelsPerLine
;
967 if( m_yScrollPixelsPerLine
)
969 cliy
/= m_yScrollPixelsPerLine
;
970 szy
/= m_yScrollPixelsPerLine
;
978 int xScrollOld
= GetScrollPos(wxHORIZONTAL
),
979 yScrollOld
= GetScrollPos(wxVERTICAL
);
982 switch ( event
.GetKeyCode() )
986 dsty
= sty
- (5 * cliy
/ 6);
987 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
992 Scroll(-1, sty
+ (5 * cliy
/ 6));
996 Scroll(0, event
.ControlDown() ? 0 : -1);
1000 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);
1004 Scroll(-1, sty
- 1);
1008 Scroll(-1, sty
+ 1);
1012 Scroll(stx
- 1, -1);
1016 Scroll(stx
+ 1, -1);
1025 int xScroll
= GetScrollPos(wxHORIZONTAL
);
1026 if ( xScroll
!= xScrollOld
)
1028 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, xScroll
,
1030 event
.SetEventObject(this);
1031 GetEventHandler()->ProcessEvent(event
);
1034 int yScroll
= GetScrollPos(wxVERTICAL
);
1035 if ( yScroll
!= yScrollOld
)
1037 wxScrollWinEvent
event(wxEVT_SCROLLWIN_THUMBTRACK
, yScroll
,
1039 event
.SetEventObject(this);
1040 GetEventHandler()->ProcessEvent(event
);