]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/scrolwin.cpp
wxCocoa: Added preliminary Blit support
[wxWidgets.git] / src / gtk / scrolwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/scrolwin.cpp
3 // Purpose: wxScrolledWindow implementation
4 // Author: Julian Smart
5 // Modified by: Ron Lee
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "scrolwin.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #include "wx/utils.h"
32 #include "wx/dcclient.h"
33
34 #include "wx/scrolwin.h"
35 #include "wx/panel.h"
36 #include "wx/sizer.h"
37
38 #include "wx/gtk/private.h"
39 #include "wx/gtk/win_gtk.h"
40
41 // ----------------------------------------------------------------------------
42 // event tables
43 // ----------------------------------------------------------------------------
44
45 BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel)
46 EVT_SCROLLWIN(wxScrolledWindow::OnScroll)
47 EVT_SIZE(wxScrolledWindow::OnSize)
48 EVT_PAINT(wxScrolledWindow::OnPaint)
49 EVT_CHAR(wxScrolledWindow::OnChar)
50 END_EVENT_TABLE()
51
52 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57
58 //-----------------------------------------------------------------------------
59 // data
60 //-----------------------------------------------------------------------------
61
62 extern bool g_blockEventsOnDrag;
63 extern bool g_blockEventsOnScroll;
64
65 //-----------------------------------------------------------------------------
66 // idle system
67 //-----------------------------------------------------------------------------
68
69 extern void wxapp_install_idle_handler();
70 extern bool g_isIdle;
71
72 //-----------------------------------------------------------------------------
73 // "value_changed" from m_vAdjust
74 //-----------------------------------------------------------------------------
75
76 static void gtk_scrolled_window_vscroll_callback( GtkAdjustment *adjust,
77 SCROLLBAR_CBACK_ARG
78 wxScrolledWindow *win )
79 {
80 if (g_isIdle)
81 wxapp_install_idle_handler();
82
83 if (g_blockEventsOnDrag) return;
84
85 if (!win->m_hasVMT) return;
86
87 win->GtkVScroll( adjust->value,
88 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win->m_widget)->vscrollbar) );
89 }
90
91 //-----------------------------------------------------------------------------
92 // "value_changed" from m_hAdjust
93 //-----------------------------------------------------------------------------
94
95 static void gtk_scrolled_window_hscroll_callback( GtkAdjustment *adjust,
96 SCROLLBAR_CBACK_ARG
97 wxScrolledWindow *win )
98 {
99 if (g_isIdle)
100 wxapp_install_idle_handler();
101
102 if (g_blockEventsOnDrag) return;
103 if (!win->m_hasVMT) return;
104
105 win->GtkHScroll( adjust->value,
106 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win->m_widget)->hscrollbar) );
107 }
108
109 //-----------------------------------------------------------------------------
110 // "button_press_event" from scrollbar
111 //-----------------------------------------------------------------------------
112
113 static gint gtk_scrollbar_button_press_callback( GtkRange *widget,
114 GdkEventButton *gdk_event,
115 wxWindowGTK *win)
116 {
117 if (g_isIdle)
118 wxapp_install_idle_handler();
119
120 g_blockEventsOnScroll = TRUE;
121
122 // FIXME: there is no slider field any more, what was meant here?
123 #ifndef __WXGTK20__
124 win->m_isScrolling = (gdk_event->window == widget->slider);
125 #endif
126
127 return FALSE;
128 }
129
130 //-----------------------------------------------------------------------------
131 // "button_release_event" from scrollbar
132 //-----------------------------------------------------------------------------
133
134 static gint gtk_scrollbar_button_release_callback( GtkRange *widget,
135 GdkEventButton *WXUNUSED(gdk_event),
136 wxWindowGTK *win)
137 {
138 // don't test here as we can release the mouse while being over
139 // a different window than the slider
140 //
141 // if (gdk_event->window != widget->slider) return FALSE;
142
143 g_blockEventsOnScroll = FALSE;
144
145 if (win->m_isScrolling)
146 {
147 wxEventType command = wxEVT_SCROLLWIN_THUMBRELEASE;
148 int value = -1;
149 int dir = -1;
150
151 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(win->m_widget);
152 if (widget == GTK_RANGE(scrolledWindow->hscrollbar))
153 {
154 value = (int)(win->m_hAdjust->value+0.5);
155 dir = wxHORIZONTAL;
156 }
157 if (widget == GTK_RANGE(scrolledWindow->vscrollbar))
158 {
159 value = (int)(win->m_vAdjust->value+0.5);
160 dir = wxVERTICAL;
161 }
162
163 wxScrollWinEvent event( command, value, dir );
164 event.SetEventObject( win );
165 win->GetEventHandler()->ProcessEvent( event );
166 }
167
168 win->m_isScrolling = FALSE;
169
170 return FALSE;
171 }
172
173 //-----------------------------------------------------------------------------
174 // InsertChild for wxScrolledWindow
175 //-----------------------------------------------------------------------------
176
177 static void wxInsertChildInScrolledWindow( wxWindow* parent, wxWindow* child )
178 {
179 // The window might have been scrolled already, do we
180 // have to adapt the position.
181 GtkPizza *pizza = GTK_PIZZA(parent->m_wxwindow);
182 child->m_x += pizza->xoffset;
183 child->m_y += pizza->yoffset;
184
185 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
186 GTK_WIDGET(child->m_widget),
187 child->m_x,
188 child->m_y,
189 child->m_width,
190 child->m_height );
191 }
192
193 // ----------------------------------------------------------------------------
194 // wxScrolledWindow creation
195 // ----------------------------------------------------------------------------
196
197 void wxScrolledWindow::Init()
198 {
199 m_xScrollPixelsPerLine = 0;
200 m_yScrollPixelsPerLine = 0;
201 m_xScrollingEnabled = TRUE;
202 m_yScrollingEnabled = TRUE;
203 m_xScrollPosition = 0;
204 m_yScrollPosition = 0;
205 m_xScrollLinesPerPage = 0;
206 m_yScrollLinesPerPage = 0;
207 m_targetWindow = (wxWindow*) NULL;
208 m_scaleX = 1.0;
209 m_scaleY = 1.0;
210 m_hasScrolling = TRUE;
211 }
212
213 bool wxScrolledWindow::Create(wxWindow *parent,
214 wxWindowID id,
215 const wxPoint& pos,
216 const wxSize& size,
217 long style,
218 const wxString& name)
219 {
220 Init();
221
222 if (!PreCreation( parent, pos, size ) ||
223 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
224 {
225 wxFAIL_MSG( wxT("wxWindow creation failed") );
226 return FALSE;
227 }
228
229 m_insertCallback = wxInsertChildInScrolledWindow;
230
231 m_targetWindow = this;
232
233 m_widget = gtk_scrolled_window_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
234 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
235
236 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(m_widget);
237
238 GtkScrolledWindowClass *scroll_class = GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget) );
239 scroll_class->scrollbar_spacing = 0;
240
241 gtk_scrolled_window_set_policy( scrolledWindow, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
242
243 m_hAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->hscrollbar) );
244 m_vAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->vscrollbar) );
245
246 m_wxwindow = gtk_pizza_new();
247
248 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
249
250 GtkPizza *pizza = GTK_PIZZA(m_wxwindow);
251
252 if (HasFlag(wxRAISED_BORDER))
253 {
254 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_OUT );
255 }
256 else if (HasFlag(wxSUNKEN_BORDER))
257 {
258 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_IN );
259 }
260 else if (HasFlag(wxSIMPLE_BORDER))
261 {
262 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_THIN );
263 }
264 else
265 {
266 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_NONE );
267 }
268
269 GTK_WIDGET_SET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
270 m_acceptsFocus = TRUE;
271
272 // I _really_ don't want scrollbars in the beginning
273 m_vAdjust->lower = 0.0;
274 m_vAdjust->upper = 1.0;
275 m_vAdjust->value = 0.0;
276 m_vAdjust->step_increment = 1.0;
277 m_vAdjust->page_increment = 2.0;
278 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
279 m_hAdjust->lower = 0.0;
280 m_hAdjust->upper = 1.0;
281 m_hAdjust->value = 0.0;
282 m_hAdjust->step_increment = 1.0;
283 m_hAdjust->page_increment = 2.0;
284 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
285
286 // Handlers for new scrollbar values
287 GtkVConnectEvent();
288 GtkHConnectEvent();
289
290 // these handlers block mouse events to any window during scrolling such as
291 // motion events and prevent GTK and wxWindows from fighting over where the
292 // slider should be
293
294 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_press_event",
295 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
296
297 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_press_event",
298 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
299
300 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_release_event",
301 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
302
303 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_release_event",
304 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
305
306 gtk_widget_show( m_wxwindow );
307
308 if (m_parent)
309 m_parent->DoAddChild( this );
310
311 m_focusWidget = m_wxwindow;
312
313 PostCreation();
314
315 Show( TRUE );
316
317 return TRUE;
318 }
319
320 // ----------------------------------------------------------------------------
321 // setting scrolling parameters
322 // ----------------------------------------------------------------------------
323
324 void wxScrolledWindow::DoSetVirtualSize( int x, int y )
325 {
326 wxPanel::DoSetVirtualSize( x, y );
327 AdjustScrollbars();
328
329 #if wxUSE_CONSTRAINTS
330 if (GetAutoLayout())
331 Layout();
332 #endif
333 }
334
335 /*
336 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
337 * noUnitsX/noUnitsY: : no. units per scrollbar
338 */
339 void wxScrolledWindow::SetScrollbars( int pixelsPerUnitX, int pixelsPerUnitY,
340 int noUnitsX, int noUnitsY,
341 int xPos, int yPos, bool noRefresh )
342 {
343 int xs, ys;
344 GetViewStart (& xs, & ys);
345
346 int old_x = m_xScrollPixelsPerLine * xs;
347 int old_y = m_yScrollPixelsPerLine * ys;
348
349 m_xScrollPixelsPerLine = pixelsPerUnitX;
350 m_yScrollPixelsPerLine = pixelsPerUnitY;
351
352 m_hAdjust->value = m_xScrollPosition = xPos;
353 m_vAdjust->value = m_yScrollPosition = yPos;
354
355 // Setting hints here should arguably be deprecated, but without it
356 // a sizer might override this manual scrollbar setting in old code.
357 m_targetWindow->SetVirtualSizeHints( noUnitsX * pixelsPerUnitX, noUnitsY * pixelsPerUnitY );
358
359 m_targetWindow->SetVirtualSize( noUnitsX * pixelsPerUnitX, noUnitsY * pixelsPerUnitY );
360
361 if (!noRefresh)
362 {
363 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
364 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
365
366 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
367 }
368 }
369
370 void wxScrolledWindow::AdjustScrollbars()
371 {
372 int w, h;
373 int vw, vh;
374
375 m_targetWindow->GetClientSize( &w, &h );
376 m_targetWindow->GetVirtualSize( &vw, &vh );
377
378 if (m_xScrollPixelsPerLine == 0)
379 {
380 m_hAdjust->upper = 1.0;
381 m_hAdjust->page_size = 1.0;
382 }
383 else
384 {
385 m_hAdjust->upper = vw / m_xScrollPixelsPerLine;
386 m_hAdjust->page_size = (w / m_xScrollPixelsPerLine);
387
388 // If the scrollbar hits the right side, move the window
389 // right to keep it from over extending.
390
391 if ((m_hAdjust->value != 0.0) && (m_hAdjust->value + m_hAdjust->page_size > m_hAdjust->upper))
392 {
393 m_hAdjust->value = m_hAdjust->upper - m_hAdjust->page_size;
394 if (m_hAdjust->value < 0.0)
395 m_hAdjust->value = 0.0;
396
397 if (GetChildren().GetCount() == 0)
398 m_xScrollPosition = (int)m_hAdjust->value; // This is enough without child windows
399 else
400 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" ); // Actually scroll window
401 }
402 }
403
404 if (m_yScrollPixelsPerLine == 0)
405 {
406 m_vAdjust->upper = 1.0;
407 m_vAdjust->page_size = 1.0;
408 }
409 else
410 {
411 m_vAdjust->upper = vh / m_yScrollPixelsPerLine;
412 m_vAdjust->page_size = (h / m_yScrollPixelsPerLine);
413
414 if ((m_vAdjust->value != 0.0) && (m_vAdjust->value + m_vAdjust->page_size > m_vAdjust->upper))
415 {
416 m_vAdjust->value = m_vAdjust->upper - m_vAdjust->page_size;
417 if (m_vAdjust->value < 0.0)
418 m_vAdjust->value = 0.0;
419
420 if (GetChildren().GetCount() == 0)
421 m_yScrollPosition = (int)m_vAdjust->value;
422 else
423 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
424 }
425 }
426
427 m_xScrollLinesPerPage = (int)(m_hAdjust->page_size + 0.5);
428 m_yScrollLinesPerPage = (int)(m_vAdjust->page_size + 0.5);
429
430 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
431 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
432 }
433
434
435 // ----------------------------------------------------------------------------
436 // target window handling
437 // ----------------------------------------------------------------------------
438
439 void wxScrolledWindow::SetTargetWindow( wxWindow *target, bool WXUNUSED(pushEventHandler) )
440 {
441 wxASSERT_MSG( target, wxT("target window must not be NULL") );
442 m_targetWindow = target;
443 }
444
445 wxWindow *wxScrolledWindow::GetTargetWindow()
446 {
447 return m_targetWindow;
448 }
449
450 // Override this function if you don't want to have wxScrolledWindow
451 // automatically change the origin according to the scroll position.
452 void wxScrolledWindow::PrepareDC(wxDC& dc)
453 {
454 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
455 -m_yScrollPosition * m_yScrollPixelsPerLine );
456 }
457
458 void wxScrolledWindow::SetScrollRate( int xstep, int ystep )
459 {
460 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
461 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
462
463 m_xScrollPixelsPerLine = xstep;
464 m_yScrollPixelsPerLine = ystep;
465
466 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
467 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
468
469 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
470
471 AdjustScrollbars();
472 }
473
474 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
475 {
476 if ( x_unit )
477 *x_unit = m_xScrollPixelsPerLine;
478 if ( y_unit )
479 *y_unit = m_yScrollPixelsPerLine;
480 }
481
482 int wxScrolledWindow::GetScrollPageSize(int orient) const
483 {
484 if ( orient == wxHORIZONTAL )
485 return m_xScrollLinesPerPage;
486 else
487 return m_yScrollLinesPerPage;
488 }
489
490 void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
491 {
492 if ( orient == wxHORIZONTAL )
493 m_xScrollLinesPerPage = pageSize;
494 else
495 m_yScrollLinesPerPage = pageSize;
496 }
497
498 void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
499 {
500 int orient = event.GetOrientation();
501
502 int nScrollInc = CalcScrollInc(event);
503 if (nScrollInc == 0) return;
504
505 if (orient == wxHORIZONTAL)
506 {
507 int newPos = m_xScrollPosition + nScrollInc;
508 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
509 }
510 else
511 {
512 int newPos = m_yScrollPosition + nScrollInc;
513 SetScrollPos(wxVERTICAL, newPos, TRUE );
514 }
515
516 if (orient == wxHORIZONTAL)
517 {
518 m_xScrollPosition += nScrollInc;
519 }
520 else
521 {
522 m_yScrollPosition += nScrollInc;
523 }
524
525 if (orient == wxHORIZONTAL)
526 {
527 if (m_xScrollingEnabled)
528 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
529 else
530 m_targetWindow->Refresh();
531 }
532 else
533 {
534 if (m_yScrollingEnabled)
535 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
536 else
537 m_targetWindow->Refresh();
538 }
539 }
540
541 void wxScrolledWindow::Scroll( int x_pos, int y_pos )
542 {
543 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
544
545 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
546 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
547
548 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
549 {
550 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
551 if (max < 0) max = 0;
552 if (x_pos > max) x_pos = max;
553 if (x_pos < 0) x_pos = 0;
554
555 int old_x = m_xScrollPosition;
556 m_xScrollPosition = x_pos;
557 m_hAdjust->value = x_pos;
558
559 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
560
561 // Just update the scrollbar, don't send any wxWindows event
562 GtkHDisconnectEvent();
563 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
564 GtkHConnectEvent();
565 }
566
567 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
568 {
569 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
570 if (max < 0) max = 0;
571 if (y_pos > max) y_pos = max;
572 if (y_pos < 0) y_pos = 0;
573
574 int old_y = m_yScrollPosition;
575 m_yScrollPosition = y_pos;
576 m_vAdjust->value = y_pos;
577
578 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
579
580 // Just update the scrollbar, don't send any wxWindows event
581 GtkVDisconnectEvent();
582 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
583 GtkVConnectEvent();
584 }
585 }
586
587 // TODO: [VH]Scroll functions should be combined
588
589 void wxScrolledWindow::GtkVScroll( float value, unsigned int scroll_type )
590 {
591 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
592
593 if (m_yScrollPixelsPerLine == 0)
594 return;
595
596 int y_pos = (int)(value+0.5);
597
598 if (y_pos == m_yScrollPosition)
599 return;
600
601 wxEventType command = GtkScrollWinTypeToWx(scroll_type);
602
603 wxScrollWinEvent event( command, y_pos, wxVERTICAL );
604 event.SetEventObject( this );
605 GetEventHandler()->ProcessEvent( event );
606 }
607
608 void wxScrolledWindow::GtkHScroll( float value, unsigned int scroll_type )
609 {
610 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
611
612 if (m_xScrollPixelsPerLine == 0)
613 return;
614
615 int x_pos = (int)(value+0.5);
616
617 if (x_pos == m_xScrollPosition)
618 return;
619
620 wxEventType command = GtkScrollWinTypeToWx(scroll_type);
621
622 wxScrollWinEvent event( command, x_pos, wxHORIZONTAL );
623 event.SetEventObject( this );
624 GetEventHandler()->ProcessEvent( event );
625 }
626
627 void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
628 {
629 m_xScrollingEnabled = x_scroll;
630 m_yScrollingEnabled = y_scroll;
631 }
632
633 // Where the current view starts from
634 void wxScrolledWindow::GetViewStart (int *x, int *y) const
635 {
636 if ( x )
637 *x = m_xScrollPosition;
638 if ( y )
639 *y = m_yScrollPosition;
640 }
641
642 void wxScrolledWindow::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const
643 {
644 int xs, ys;
645 GetViewStart (& xs, & ys);
646
647 if ( xx )
648 *xx = x - xs * m_xScrollPixelsPerLine;
649 if ( yy )
650 *yy = y - ys * m_yScrollPixelsPerLine;
651 }
652
653 void wxScrolledWindow::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
654 {
655 int xs, ys;
656 GetViewStart (& xs, & ys);
657
658 if ( xx )
659 *xx = x + xs * m_xScrollPixelsPerLine;
660 if ( yy )
661 *yy = y + ys * m_yScrollPixelsPerLine;
662 }
663
664 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
665 {
666 int pos = event.GetPosition();
667 int orient = event.GetOrientation();
668
669 int nScrollInc = 0;
670 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
671 {
672 if (orient == wxHORIZONTAL)
673 nScrollInc = - m_xScrollPosition;
674 else
675 nScrollInc = - m_yScrollPosition;
676 } else
677 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
678 {
679 if (orient == wxHORIZONTAL)
680 nScrollInc = GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine - m_xScrollPosition;
681 else
682 nScrollInc = GetVirtualSize().GetHeight() / m_yScrollPixelsPerLine - m_yScrollPosition;
683 } else
684 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
685 {
686 nScrollInc = -1;
687 } else
688 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
689 {
690 nScrollInc = 1;
691 } else
692 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
693 {
694 if (orient == wxHORIZONTAL)
695 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
696 else
697 nScrollInc = -GetScrollPageSize(wxVERTICAL);
698 } else
699 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
700 {
701 if (orient == wxHORIZONTAL)
702 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
703 else
704 nScrollInc = GetScrollPageSize(wxVERTICAL);
705 } else
706 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
707 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
708 {
709 if (orient == wxHORIZONTAL)
710 nScrollInc = pos - m_xScrollPosition;
711 else
712 nScrollInc = pos - m_yScrollPosition;
713 }
714
715 if (orient == wxHORIZONTAL)
716 {
717 if (m_xScrollPixelsPerLine > 0)
718 {
719 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
720 if (max < 0) max = 0;
721
722 if ( (m_xScrollPosition + nScrollInc) < 0 )
723 nScrollInc = -m_xScrollPosition; // As -ve as we can go
724 else if ( (m_xScrollPosition + nScrollInc) > max )
725 nScrollInc = max - m_xScrollPosition; // As +ve as we can go
726 }
727 else
728 m_targetWindow->Refresh();
729 }
730 else
731 {
732 if (m_yScrollPixelsPerLine > 0)
733 {
734 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
735 if (max < 0) max = 0;
736
737 if ( (m_yScrollPosition + nScrollInc) < 0 )
738 nScrollInc = -m_yScrollPosition; // As -ve as we can go
739 else if ( (m_yScrollPosition + nScrollInc) > max )
740 nScrollInc = max - m_yScrollPosition; // As +ve as we can go
741 }
742 else
743 m_targetWindow->Refresh();
744 }
745
746 return nScrollInc;
747 }
748
749 void wxScrolledWindow::SetScrollPos( int orient, int pos, bool refresh )
750 {
751 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
752
753 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
754
755 if (orient == wxHORIZONTAL)
756 {
757 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
758 if (max < 0) max = 0;
759
760 if (pos > max) pos = 0;
761 if (pos < 0) pos = 0;
762
763 if (pos == (int)(m_hAdjust->value+0.5)) return;
764 m_hAdjust->value = pos;
765 }
766 else
767 {
768 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
769 if (max < 0) max = 0;
770
771 if (pos > max) pos = 0;
772 if (pos < 0) pos = 0;
773
774 if (pos == (int)(m_vAdjust->value+0.5)) return;
775 m_vAdjust->value = pos;
776 }
777
778 if (m_wxwindow->window)
779 {
780 if (orient == wxHORIZONTAL)
781 {
782 // Just update the scrollbar, don't send any wxWindows event
783 GtkHDisconnectEvent();
784 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
785 GtkHConnectEvent();
786 }
787 else
788 {
789 // Just update the scrollbar, don't send any wxWindows event
790 GtkVDisconnectEvent();
791 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
792 GtkVConnectEvent();
793 }
794 }
795 }
796
797 void wxScrolledWindow::GtkVConnectEvent()
798 {
799 gtk_signal_connect( GTK_OBJECT(m_vAdjust), "value_changed",
800 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
801 }
802
803 void wxScrolledWindow::GtkHConnectEvent()
804 {
805 gtk_signal_connect( GTK_OBJECT(m_hAdjust), "value_changed",
806 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
807 }
808
809 void wxScrolledWindow::GtkHDisconnectEvent()
810 {
811 gtk_signal_disconnect_by_func( GTK_OBJECT(m_hAdjust),
812 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
813 }
814
815 void wxScrolledWindow::GtkVDisconnectEvent()
816 {
817 gtk_signal_disconnect_by_func( GTK_OBJECT(m_vAdjust),
818 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
819 }
820
821 bool wxScrolledWindow::Layout()
822 {
823 if (GetSizer() && m_targetWindow == this)
824 {
825 // If we're the scroll target, take into account the
826 // virtual size and scrolled position of the window.
827
828 int x, y, w, h;
829 CalcScrolledPosition(0,0, &x,&y);
830 GetVirtualSize(&w, &h);
831 GetSizer()->SetDimension(x, y, w, h);
832 return TRUE;
833 }
834 else
835 return wxPanel::Layout(); // fall back to default for LayoutConstraints
836 }
837
838 // ----------------------------------------------------------------------------
839 // event handlers
840 // ----------------------------------------------------------------------------
841
842 // Default OnSize resets scrollbars, if any
843 void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
844 {
845 if( GetAutoLayout() || m_targetWindow->GetAutoLayout() )
846 {
847 if( m_targetWindow != this )
848 m_targetWindow->FitInside();
849
850 FitInside();
851
852 // FIXME: Something is really weird here... This should be
853 // called by FitInside above (and apparently is), yet the
854 // scrollsub sample will get the scrollbar wrong if resized
855 // quickly. This masks the bug, but is surely not the right
856 // answer at all.
857 AdjustScrollbars();
858 }
859 else
860 {
861 AdjustScrollbars();
862 }
863 }
864
865 // This calls OnDraw, having adjusted the origin according to the current
866 // scroll position
867 void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
868 {
869 wxPaintDC dc(this);
870 PrepareDC(dc);
871
872 OnDraw(dc);
873 }
874
875 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
876 // compatibility here - if we used OnKeyDown(), the programs which process
877 // arrows themselves in their OnChar() would never get the message and like
878 // this they always have the priority
879 void wxScrolledWindow::OnChar(wxKeyEvent& event)
880 {
881 int stx, sty, // view origin
882 szx, szy, // view size (total)
883 clix, cliy; // view size (on screen)
884
885 GetViewStart(&stx, &sty);
886 GetClientSize(&clix, &cliy);
887 GetVirtualSize(&szx, &szy);
888
889 if( m_xScrollPixelsPerLine )
890 {
891 clix /= m_xScrollPixelsPerLine;
892 szx /= m_xScrollPixelsPerLine;
893 }
894 else
895 {
896 clix = 0;
897 szx = -1;
898 }
899 if( m_yScrollPixelsPerLine )
900 {
901 cliy /= m_yScrollPixelsPerLine;
902 szy /= m_yScrollPixelsPerLine;
903 }
904 else
905 {
906 cliy = 0;
907 szy = -1;
908 }
909
910 int xScrollOld = GetScrollPos(wxHORIZONTAL),
911 yScrollOld = GetScrollPos(wxVERTICAL);
912
913 int dsty;
914 switch ( event.GetKeyCode() )
915 {
916 case WXK_PAGEUP:
917 case WXK_PRIOR:
918 dsty = sty - (5 * cliy / 6);
919 Scroll(-1, (dsty == -1) ? 0 : dsty);
920 break;
921
922 case WXK_PAGEDOWN:
923 case WXK_NEXT:
924 Scroll(-1, sty + (5 * cliy / 6));
925 break;
926
927 case WXK_HOME:
928 Scroll(0, event.ControlDown() ? 0 : -1);
929 break;
930
931 case WXK_END:
932 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
933 break;
934
935 case WXK_UP:
936 Scroll(-1, sty - 1);
937 break;
938
939 case WXK_DOWN:
940 Scroll(-1, sty + 1);
941 break;
942
943 case WXK_LEFT:
944 Scroll(stx - 1, -1);
945 break;
946
947 case WXK_RIGHT:
948 Scroll(stx + 1, -1);
949 break;
950
951 default:
952 // not for us
953 event.Skip();
954 return;
955 }
956
957 int xScroll = GetScrollPos(wxHORIZONTAL);
958 if ( xScroll != xScrollOld )
959 {
960 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, xScroll,
961 wxHORIZONTAL);
962 event.SetEventObject(this);
963 GetEventHandler()->ProcessEvent(event);
964 }
965
966 int yScroll = GetScrollPos(wxVERTICAL);
967 if ( yScroll != yScrollOld )
968 {
969 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, yScroll,
970 wxVERTICAL);
971 event.SetEventObject(this);
972 GetEventHandler()->ProcessEvent(event);
973 }
974 }
975
976
977 // vi:sts=4:sw=4:et