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