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