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