1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/scrolwin.cpp
3 // Purpose: wxScrolledWindow implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "scrolwin.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/dcclient.h"
34 #include "wx/gtk/scrolwin.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
;
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 extern void wxapp_install_idle_handler();
70 //-----------------------------------------------------------------------------
71 // "value_changed" from m_vAdjust
72 //-----------------------------------------------------------------------------
74 static void gtk_scrolled_window_vscroll_callback( GtkAdjustment
*adjust
, wxScrolledWindow
*win
)
77 wxapp_install_idle_handler();
79 if (g_blockEventsOnDrag
) return;
81 if (!win
->m_hasVMT
) return;
83 win
->GtkVScroll( adjust
->value
);
86 //-----------------------------------------------------------------------------
87 // "value_changed" from m_hAdjust
88 //-----------------------------------------------------------------------------
90 static void gtk_scrolled_window_hscroll_callback( GtkAdjustment
*adjust
, wxScrolledWindow
*win
)
93 wxapp_install_idle_handler();
95 if (g_blockEventsOnDrag
) return;
96 if (!win
->m_hasVMT
) return;
98 win
->GtkHScroll( adjust
->value
);
101 //-----------------------------------------------------------------------------
102 // InsertChild for wxScrolledWindow
103 //-----------------------------------------------------------------------------
105 static void wxInsertChildInScrolledWindow( wxWindow
* parent
, wxWindow
* child
)
107 /* the window might have been scrolled already, do we
108 have to adapt the position */
109 GtkPizza
*pizza
= GTK_PIZZA(parent
->m_wxwindow
);
110 child
->m_x
+= pizza
->xoffset
;
111 child
->m_y
+= pizza
->yoffset
;
113 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
114 GTK_WIDGET(child
->m_widget
),
121 // ----------------------------------------------------------------------------
122 // wxScrolledWindow creation
123 // ----------------------------------------------------------------------------
125 wxScrolledWindow::wxScrolledWindow()
127 m_xScrollPixelsPerLine
= 0;
128 m_yScrollPixelsPerLine
= 0;
129 m_xScrollingEnabled
= TRUE
;
130 m_yScrollingEnabled
= TRUE
;
131 m_xScrollPosition
= 0;
132 m_yScrollPosition
= 0;
135 m_xScrollLinesPerPage
= 0;
136 m_yScrollLinesPerPage
= 0;
137 m_targetWindow
= (wxWindow
*) NULL
;
142 bool wxScrolledWindow::Create(wxWindow
*parent
,
147 const wxString
& name
)
149 if (!PreCreation( parent
, pos
, size
) ||
150 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
152 wxFAIL_MSG( wxT("wxWindow creation failed") );
156 m_insertCallback
= wxInsertChildInScrolledWindow
;
158 m_xScrollPixelsPerLine
= 0;
159 m_yScrollPixelsPerLine
= 0;
160 m_xScrollingEnabled
= TRUE
;
161 m_yScrollingEnabled
= TRUE
;
162 m_xScrollPosition
= 0;
163 m_yScrollPosition
= 0;
166 m_xScrollLinesPerPage
= 0;
167 m_yScrollLinesPerPage
= 0;
169 m_targetWindow
= this;
171 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
172 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
174 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(m_widget
);
176 GtkScrolledWindowClass
*scroll_class
= GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
177 scroll_class
->scrollbar_spacing
= 0;
179 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
181 m_hAdjust
= gtk_range_get_adjustment( GTK_RANGE(scrolledWindow
->hscrollbar
) );
182 m_vAdjust
= gtk_range_get_adjustment( GTK_RANGE(scrolledWindow
->vscrollbar
) );
184 m_wxwindow
= gtk_pizza_new();
186 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
188 GtkPizza
*pizza
= GTK_PIZZA(m_wxwindow
);
190 if (HasFlag(wxRAISED_BORDER
))
192 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_OUT
);
194 else if (HasFlag(wxSUNKEN_BORDER
))
196 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_IN
);
198 else if (HasFlag(wxSIMPLE_BORDER
))
200 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_THIN
);
204 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_NONE
);
207 GTK_WIDGET_SET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
208 m_acceptsFocus
= TRUE
;
210 // I _really_ don't want scrollbars in the beginning
211 m_vAdjust
->lower
= 0.0;
212 m_vAdjust
->upper
= 1.0;
213 m_vAdjust
->value
= 0.0;
214 m_vAdjust
->step_increment
= 1.0;
215 m_vAdjust
->page_increment
= 1.0;
216 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "changed" );
217 m_hAdjust
->lower
= 0.0;
218 m_hAdjust
->upper
= 1.0;
219 m_hAdjust
->value
= 0.0;
220 m_hAdjust
->step_increment
= 1.0;
221 m_hAdjust
->page_increment
= 1.0;
222 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "changed" );
224 // these handlers get notified when screen updates are required either when
225 // scrolling or when the window size (and therefore scrollbar configuration)
227 gtk_signal_connect( GTK_OBJECT(m_hAdjust
), "value_changed",
228 (GtkSignalFunc
) gtk_scrolled_window_hscroll_callback
, (gpointer
) this );
229 gtk_signal_connect( GTK_OBJECT(m_vAdjust
), "value_changed",
230 (GtkSignalFunc
) gtk_scrolled_window_vscroll_callback
, (gpointer
) this );
232 gtk_widget_show( m_wxwindow
);
235 m_parent
->DoAddChild( this );
244 wxScrolledWindow::~wxScrolledWindow()
248 // ----------------------------------------------------------------------------
249 // setting scrolling parameters
250 // ----------------------------------------------------------------------------
253 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
254 * noUnitsX/noUnitsY: : no. units per scrollbar
256 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
257 int noUnitsX
, int noUnitsY
,
258 int xPos
, int yPos
, bool noRefresh
)
260 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
261 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
262 m_xScrollPosition
= xPos
;
263 m_yScrollPosition
= yPos
;
264 m_xScrollLines
= noUnitsX
;
265 m_yScrollLines
= noUnitsY
;
267 m_hAdjust
->lower
= 0.0;
268 m_hAdjust
->upper
= noUnitsX
;
269 m_hAdjust
->value
= xPos
;
270 m_hAdjust
->step_increment
= 1.0;
271 m_hAdjust
->page_increment
= 1.0;
273 m_vAdjust
->lower
= 0.0;
274 m_vAdjust
->upper
= noUnitsY
;
275 m_vAdjust
->value
= yPos
;
276 m_vAdjust
->step_increment
= 1.0;
277 m_vAdjust
->page_increment
= 1.0;
282 void wxScrolledWindow::AdjustScrollbars()
285 m_targetWindow
->GetClientSize( &w
, &h
);
287 if (m_xScrollPixelsPerLine
== 0)
288 m_hAdjust
->page_size
= 1.0;
290 m_hAdjust
->page_size
= (w
/ m_xScrollPixelsPerLine
);
292 if (m_yScrollPixelsPerLine
== 0)
293 m_vAdjust
->page_size
= 1.0;
295 m_vAdjust
->page_size
= (h
/ m_yScrollPixelsPerLine
);
297 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "changed" );
298 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "changed" );
301 // ----------------------------------------------------------------------------
302 // target window handling
303 // ----------------------------------------------------------------------------
305 void wxScrolledWindow::SetTargetWindow( wxWindow
*target
)
307 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
308 m_targetWindow
= target
;
311 wxWindow
*wxScrolledWindow::GetTargetWindow()
313 return m_targetWindow
;
316 // Override this function if you don't want to have wxScrolledWindow
317 // automatically change the origin according to the scroll position.
318 void wxScrolledWindow::PrepareDC(wxDC
& dc
)
320 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
321 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
324 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
327 *x_unit
= m_xScrollPixelsPerLine
;
329 *y_unit
= m_yScrollPixelsPerLine
;
332 int wxScrolledWindow::GetScrollPageSize(int orient
) const
334 if ( orient
== wxHORIZONTAL
)
335 return m_xScrollLinesPerPage
;
337 return m_yScrollLinesPerPage
;
340 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
342 if ( orient
== wxHORIZONTAL
)
343 m_xScrollLinesPerPage
= pageSize
;
345 m_yScrollLinesPerPage
= pageSize
;
348 void wxScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
350 int orient
= event
.GetOrientation();
352 int nScrollInc
= CalcScrollInc(event
);
353 if (nScrollInc
== 0) return;
355 if (orient
== wxHORIZONTAL
)
357 int newPos
= m_xScrollPosition
+ nScrollInc
;
358 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
362 int newPos
= m_yScrollPosition
+ nScrollInc
;
363 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
366 if (orient
== wxHORIZONTAL
)
368 m_xScrollPosition
+= nScrollInc
;
372 m_yScrollPosition
+= nScrollInc
;
375 if (orient
== wxHORIZONTAL
)
377 if (m_xScrollingEnabled
)
378 m_targetWindow
->ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, (const wxRect
*) NULL
);
380 m_targetWindow
->Refresh();
384 if (m_yScrollingEnabled
)
385 m_targetWindow
->ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, (const wxRect
*) NULL
);
387 m_targetWindow
->Refresh();
391 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
396 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
397 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
399 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
401 int old_x
= m_xScrollPosition
;
402 m_xScrollPosition
= x_pos
;
403 m_hAdjust
->value
= x_pos
;
405 wxEventType command
= wxEVT_SCROLLWIN_THUMBTRACK
;
406 wxScrollWinEvent
event( command
, m_xScrollPosition
, wxHORIZONTAL
);
407 event
.SetEventObject( this );
408 GetEventHandler()->ProcessEvent( event
);
410 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
412 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" );
415 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
417 int old_y
= m_yScrollPosition
;
418 m_yScrollPosition
= y_pos
;
419 m_vAdjust
->value
= y_pos
;
421 wxEventType command
= wxEVT_SCROLLWIN_THUMBTRACK
;
422 wxScrollWinEvent
event( command
, m_yScrollPosition
, wxVERTICAL
);
423 event
.SetEventObject( this );
424 GetEventHandler()->ProcessEvent( event
);
426 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
428 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
432 void wxScrolledWindow::GtkVScroll( float value
)
437 if (m_yScrollPixelsPerLine
== 0)
440 int y_pos
= (int)(value
+0.5);
442 if (y_pos
== m_yScrollPosition
)
445 int old_y
= m_yScrollPosition
;
446 m_yScrollPosition
= y_pos
;
448 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(m_widget
);
449 GtkRange
*range
= GTK_RANGE(scrolledWindow
->hscrollbar
);
451 wxEventType command
= wxEVT_SCROLLWIN_THUMBTRACK
;
452 if (range
->scroll_type
== GTK_SCROLL_STEP_BACKWARD
) command
= wxEVT_SCROLLWIN_LINEUP
;
453 else if (range
->scroll_type
== GTK_SCROLL_STEP_FORWARD
) command
= wxEVT_SCROLLWIN_LINEDOWN
;
454 else if (range
->scroll_type
== GTK_SCROLL_PAGE_BACKWARD
) command
= wxEVT_SCROLLWIN_PAGEUP
;
455 else if (range
->scroll_type
== GTK_SCROLL_PAGE_FORWARD
) command
= wxEVT_SCROLLWIN_PAGEDOWN
;
457 wxScrollWinEvent
event( command
, m_yScrollPosition
, wxVERTICAL
);
458 event
.SetEventObject( this );
459 GetEventHandler()->ProcessEvent( event
);
461 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
464 void wxScrolledWindow::GtkHScroll( float value
)
469 if (m_xScrollPixelsPerLine
== 0)
472 int x_pos
= (int)(value
+0.5);
474 if (x_pos
== m_xScrollPosition
)
477 int old_x
= m_xScrollPosition
;
478 m_xScrollPosition
= x_pos
;
480 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(m_widget
);
481 GtkRange
*range
= GTK_RANGE(scrolledWindow
->hscrollbar
);
483 wxEventType command
= wxEVT_SCROLLWIN_THUMBTRACK
;
484 if (range
->scroll_type
== GTK_SCROLL_STEP_BACKWARD
) command
= wxEVT_SCROLLWIN_LINEUP
;
485 else if (range
->scroll_type
== GTK_SCROLL_STEP_FORWARD
) command
= wxEVT_SCROLLWIN_LINEDOWN
;
486 else if (range
->scroll_type
== GTK_SCROLL_PAGE_BACKWARD
) command
= wxEVT_SCROLLWIN_PAGEUP
;
487 else if (range
->scroll_type
== GTK_SCROLL_PAGE_FORWARD
) command
= wxEVT_SCROLLWIN_PAGEDOWN
;
489 wxScrollWinEvent
event( command
, m_xScrollPosition
, wxHORIZONTAL
);
490 event
.SetEventObject( this );
491 GetEventHandler()->ProcessEvent( event
);
493 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
496 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
498 m_xScrollingEnabled
= x_scroll
;
499 m_yScrollingEnabled
= y_scroll
;
502 void wxScrolledWindow::GetVirtualSize (int *x
, int *y
) const
505 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
507 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
510 // Where the current view starts from
511 void wxScrolledWindow::GetViewStart (int *x
, int *y
) const
514 *x
= m_xScrollPosition
;
516 *y
= m_yScrollPosition
;
519 void wxScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
522 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
524 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
527 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
530 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
532 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
535 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent
& event
)
537 int pos
= event
.GetPosition();
538 int orient
= event
.GetOrientation();
541 if (event
.GetEventType() == wxEVT_SCROLLWIN_TOP
)
543 if (orient
== wxHORIZONTAL
)
544 nScrollInc
= - m_xScrollPosition
;
546 nScrollInc
= - m_yScrollPosition
;
548 if (event
.GetEventType() == wxEVT_SCROLLWIN_BOTTOM
)
550 if (orient
== wxHORIZONTAL
)
551 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
553 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
555 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
)
559 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
)
563 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEUP
)
565 if (orient
== wxHORIZONTAL
)
566 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
568 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
570 if (event
.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN
)
572 if (orient
== wxHORIZONTAL
)
573 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
575 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
577 if ((event
.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK
) ||
578 (event
.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE
))
580 if (orient
== wxHORIZONTAL
)
581 nScrollInc
= pos
- m_xScrollPosition
;
583 nScrollInc
= pos
- m_yScrollPosition
;
586 if (orient
== wxHORIZONTAL
)
588 if (m_xScrollPixelsPerLine
> 0)
591 m_targetWindow
->GetClientSize(&w
, &h
);
593 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
594 int noPositions
= (int) ( ((nMaxWidth
- w
)/(double)m_xScrollPixelsPerLine
) + 0.5 );
598 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
599 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
600 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
601 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
604 m_targetWindow
->Refresh();
608 if (m_yScrollPixelsPerLine
> 0)
611 m_targetWindow
->GetClientSize(&w
, &h
);
613 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
614 int noPositions
= (int) ( ((nMaxHeight
- h
)/(double)m_yScrollPixelsPerLine
) + 0.5 );
618 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
619 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
620 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
621 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
624 m_targetWindow
->Refresh();
630 // ----------------------------------------------------------------------------
632 // ----------------------------------------------------------------------------
634 // Default OnSize resets scrollbars, if any
635 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
637 #if wxUSE_CONSTRAINTS
645 // This calls OnDraw, having adjusted the origin according to the current
647 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
655 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
656 // compatibility here - if we used OnKeyDown(), the programs which process
657 // arrows themselves in their OnChar() would never get the message and like
658 // this they always have the priority
659 void wxScrolledWindow::OnChar(wxKeyEvent
& event
)
661 int stx
, sty
, // view origin
662 szx
, szy
, // view size (total)
663 clix
, cliy
; // view size (on screen)
665 ViewStart(&stx
, &sty
);
666 GetClientSize(&clix
, &cliy
);
667 GetVirtualSize(&szx
, &szy
);
669 if( m_xScrollPixelsPerLine
)
671 clix
/= m_xScrollPixelsPerLine
;
672 szx
/= m_xScrollPixelsPerLine
;
679 if( m_yScrollPixelsPerLine
)
681 cliy
/= m_yScrollPixelsPerLine
;
682 szy
/= m_yScrollPixelsPerLine
;
691 switch ( event
.KeyCode() )
695 dsty
= sty
- (5 * cliy
/ 6);
696 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
701 Scroll(-1, sty
+ (5 * cliy
/ 6));
705 Scroll(0, event
.ControlDown() ? 0 : -1);
709 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);