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