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