]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/scrolwin.cpp
regenerated after version.bkl changes fixing -compatibility_version for Darwin
[wxWidgets.git] / src / gtk1 / scrolwin.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/scrolwin.cpp
3 // Purpose: wxScrolledWindow implementation
4 // Author: Robert Roebling
5 // Modified by: Ron Lee
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/scrolwin.h"
28 #include "wx/utils.h"
29 #include "wx/dcclient.h"
30 #include "wx/panel.h"
31 #include "wx/sizer.h"
32 #include "wx/math.h"
33
34 #include "wx/gtk/private.h"
35 #include "wx/gtk/win_gtk.h"
36
37 // ----------------------------------------------------------------------------
38 // event tables
39 // ----------------------------------------------------------------------------
40
41 BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel)
42 EVT_SCROLLWIN(wxScrolledWindow::OnScroll)
43 EVT_SIZE(wxScrolledWindow::OnSize)
44 EVT_PAINT(wxScrolledWindow::OnPaint)
45 EVT_CHAR(wxScrolledWindow::OnChar)
46 END_EVENT_TABLE()
47
48 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)
49
50 // ============================================================================
51 // implementation
52 // ============================================================================
53
54 //-----------------------------------------------------------------------------
55 // data
56 //-----------------------------------------------------------------------------
57
58 extern bool g_blockEventsOnDrag;
59 extern bool g_blockEventsOnScroll;
60
61 //-----------------------------------------------------------------------------
62 // idle system
63 //-----------------------------------------------------------------------------
64
65 extern void wxapp_install_idle_handler();
66 extern bool g_isIdle;
67
68 //-----------------------------------------------------------------------------
69 // "value_changed" from m_vAdjust
70 //-----------------------------------------------------------------------------
71
72 extern "C" {
73 static void gtk_scrolled_window_vscroll_callback( GtkAdjustment *adjust,
74 SCROLLBAR_CBACK_ARG
75 wxScrolledWindow *win )
76 {
77 if (g_isIdle)
78 wxapp_install_idle_handler();
79
80 if (g_blockEventsOnDrag) return;
81
82 if (!win->m_hasVMT) return;
83
84 win->GtkVScroll( adjust->value,
85 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win->m_widget)->vscrollbar) );
86 }
87 }
88
89 //-----------------------------------------------------------------------------
90 // "value_changed" from m_hAdjust
91 //-----------------------------------------------------------------------------
92
93 extern "C" {
94 static void gtk_scrolled_window_hscroll_callback( GtkAdjustment *adjust,
95 SCROLLBAR_CBACK_ARG
96 wxScrolledWindow *win )
97 {
98 if (g_isIdle)
99 wxapp_install_idle_handler();
100
101 if (g_blockEventsOnDrag) return;
102 if (!win->m_hasVMT) return;
103
104 win->GtkHScroll( adjust->value,
105 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win->m_widget)->hscrollbar) );
106 }
107 }
108
109 //-----------------------------------------------------------------------------
110 // "button_press_event" from scrollbar
111 //-----------------------------------------------------------------------------
112
113 extern "C" {
114 static gint gtk_scrollbar_button_press_callback( GtkRange *widget,
115 GdkEventButton *gdk_event,
116 wxWindowGTK *win)
117 {
118 if (g_isIdle)
119 wxapp_install_idle_handler();
120
121 g_blockEventsOnScroll = TRUE;
122
123 // FIXME: there is no slider field any more, what was meant here?
124 #ifndef __WXGTK20__
125 win->m_isScrolling = (gdk_event->window == widget->slider);
126 #endif
127
128 return FALSE;
129 }
130 }
131
132 //-----------------------------------------------------------------------------
133 // "button_release_event" from scrollbar
134 //-----------------------------------------------------------------------------
135
136 extern "C" {
137 static gint gtk_scrollbar_button_release_callback( GtkRange *widget,
138 GdkEventButton *WXUNUSED(gdk_event),
139 wxWindowGTK *win)
140 {
141 // don't test here as we can release the mouse while being over
142 // a different window than the slider
143 //
144 // if (gdk_event->window != widget->slider) return FALSE;
145
146 g_blockEventsOnScroll = FALSE;
147
148 if (win->m_isScrolling)
149 {
150 wxEventType command = wxEVT_SCROLLWIN_THUMBRELEASE;
151 int value = -1;
152 int dir = -1;
153
154 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(win->m_widget);
155 if (widget == GTK_RANGE(scrolledWindow->hscrollbar))
156 {
157 value = (int)(win->m_hAdjust->value+0.5);
158 dir = wxHORIZONTAL;
159 }
160 if (widget == GTK_RANGE(scrolledWindow->vscrollbar))
161 {
162 value = (int)(win->m_vAdjust->value+0.5);
163 dir = wxVERTICAL;
164 }
165
166 wxScrollWinEvent event( command, value, dir );
167 event.SetEventObject( win );
168 win->GetEventHandler()->ProcessEvent( event );
169 }
170
171 win->m_isScrolling = FALSE;
172
173 return FALSE;
174 }
175 }
176
177 //-----------------------------------------------------------------------------
178 // InsertChild for wxScrolledWindow
179 //-----------------------------------------------------------------------------
180
181 static void wxInsertChildInScrolledWindow( wxWindow* parent, wxWindow* child )
182 {
183 // The window might have been scrolled already, do we
184 // have to adapt the position.
185 GtkPizza *pizza = GTK_PIZZA(parent->m_wxwindow);
186 child->m_x += pizza->xoffset;
187 child->m_y += pizza->yoffset;
188
189 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
190 GTK_WIDGET(child->m_widget),
191 child->m_x,
192 child->m_y,
193 child->m_width,
194 child->m_height );
195 }
196
197 // ----------------------------------------------------------------------------
198 // wxScrolledWindow creation
199 // ----------------------------------------------------------------------------
200
201 void wxScrolledWindow::Init()
202 {
203 m_xScrollPixelsPerLine = 0;
204 m_yScrollPixelsPerLine = 0;
205 m_xScrollingEnabled = TRUE;
206 m_yScrollingEnabled = TRUE;
207 m_xScrollPosition = 0;
208 m_yScrollPosition = 0;
209 m_xScrollLinesPerPage = 0;
210 m_yScrollLinesPerPage = 0;
211 m_targetWindow = (wxWindow*) NULL;
212 m_scaleX = 1.0;
213 m_scaleY = 1.0;
214 m_hasScrolling = TRUE;
215 }
216
217 bool wxScrolledWindow::Create(wxWindow *parent,
218 wxWindowID id,
219 const wxPoint& pos,
220 const wxSize& size,
221 long style,
222 const wxString& name)
223 {
224 Init();
225
226 if (!PreCreation( parent, pos, size ) ||
227 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
228 {
229 wxFAIL_MSG( wxT("wxWindow creation failed") );
230 return FALSE;
231 }
232
233 m_insertCallback = wxInsertChildInScrolledWindow;
234
235 m_targetWindow = this;
236
237 m_widget = gtk_scrolled_window_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
238 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
239
240 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(m_widget);
241
242 GtkScrolledWindowClass *scroll_class = GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget) );
243 scroll_class->scrollbar_spacing = 0;
244
245 gtk_scrolled_window_set_policy( scrolledWindow, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
246
247 m_hAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->hscrollbar) );
248 m_vAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->vscrollbar) );
249
250 m_wxwindow = gtk_pizza_new();
251
252 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
253
254 GtkPizza *pizza = GTK_PIZZA(m_wxwindow);
255
256 if (HasFlag(wxRAISED_BORDER))
257 {
258 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_OUT );
259 }
260 else if (HasFlag(wxSUNKEN_BORDER))
261 {
262 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_IN );
263 }
264 else if (HasFlag(wxSIMPLE_BORDER))
265 {
266 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_THIN );
267 }
268 else
269 {
270 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_NONE );
271 }
272
273 GTK_WIDGET_SET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
274 m_acceptsFocus = TRUE;
275
276 // I _really_ don't want scrollbars in the beginning
277 m_vAdjust->lower = 0.0;
278 m_vAdjust->upper = 1.0;
279 m_vAdjust->value = 0.0;
280 m_vAdjust->step_increment = 1.0;
281 m_vAdjust->page_increment = 2.0;
282 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
283 m_hAdjust->lower = 0.0;
284 m_hAdjust->upper = 1.0;
285 m_hAdjust->value = 0.0;
286 m_hAdjust->step_increment = 1.0;
287 m_hAdjust->page_increment = 2.0;
288 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
289
290 // Handlers for new scrollbar values
291 GtkVConnectEvent();
292 GtkHConnectEvent();
293
294 // these handlers block mouse events to any window during scrolling such as
295 // motion events and prevent GTK and wxWidgets from fighting over where the
296 // slider should be
297
298 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_press_event",
299 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
300
301 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_press_event",
302 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
303
304 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_release_event",
305 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
306
307 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_release_event",
308 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
309
310 gtk_widget_show( m_wxwindow );
311
312 if (m_parent)
313 m_parent->DoAddChild( this );
314
315 m_focusWidget = m_wxwindow;
316
317 PostCreation();
318
319 Show( TRUE );
320
321 return TRUE;
322 }
323
324 // ----------------------------------------------------------------------------
325 // setting scrolling parameters
326 // ----------------------------------------------------------------------------
327
328 void wxScrolledWindow::DoSetVirtualSize( int x, int y )
329 {
330 wxPanel::DoSetVirtualSize( x, y );
331 AdjustScrollbars();
332
333 if (GetAutoLayout())
334 Layout();
335 }
336
337 // wxWindow's GetBestVirtualSize returns the actual window size,
338 // whereas we want to return the virtual size
339 wxSize wxScrolledWindow::GetBestVirtualSize() const
340 {
341 wxSize clientSize( GetClientSize() );
342 if (GetSizer())
343 {
344 wxSize minSize( GetSizer()->CalcMin() );
345
346 return wxSize( wxMax( clientSize.x, minSize.x ), wxMax( clientSize.y, minSize.y ) );
347 }
348 else
349 return clientSize;
350 }
351
352 // return the size best suited for the current window
353 // (this isn't a virtual size, this is a sensible size for the window)
354 wxSize wxScrolledWindow::DoGetBestSize() const
355 {
356 wxSize best;
357
358 if ( GetSizer() )
359 {
360 wxSize b = GetSizer()->GetMinSize();
361
362 // Only use the content to set the window size in the direction
363 // where there's no scrolling; otherwise we're going to get a huge
364 // window in the direction in which scrolling is enabled
365 int ppuX, ppuY;
366 GetScrollPixelsPerUnit(& ppuX, & ppuY);
367
368 wxSize minSize;
369 if ( GetMinSize().IsFullySpecified() )
370 minSize = GetMinSize();
371 else
372 minSize = GetSize();
373
374 if (ppuX > 0)
375 b.x = minSize.x;
376 if (ppuY > 0)
377 b.y = minSize.y;
378 best = b;
379 }
380 else
381 return wxWindow::DoGetBestSize();
382
383 // Add any difference between size and client size
384 wxSize diff = GetSize() - GetClientSize();
385 best.x += wxMax(0, diff.x);
386 best.y += wxMax(0, diff.y);
387
388 return best;
389 }
390
391 /*
392 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
393 * noUnitsX/noUnitsY: : no. units per scrollbar
394 */
395 void wxScrolledWindow::SetScrollbars( int pixelsPerUnitX, int pixelsPerUnitY,
396 int noUnitsX, int noUnitsY,
397 int xPos, int yPos, bool noRefresh )
398 {
399 int xs, ys;
400 GetViewStart (& xs, & ys);
401
402 int old_x = m_xScrollPixelsPerLine * xs;
403 int old_y = m_yScrollPixelsPerLine * ys;
404
405 m_xScrollPixelsPerLine = pixelsPerUnitX;
406 m_yScrollPixelsPerLine = pixelsPerUnitY;
407
408 m_hAdjust->value = m_xScrollPosition = xPos;
409 m_vAdjust->value = m_yScrollPosition = yPos;
410
411 // Setting hints here should arguably be deprecated, but without it
412 // a sizer might override this manual scrollbar setting in old code.
413 // m_targetWindow->SetVirtualSizeHints( noUnitsX * pixelsPerUnitX, noUnitsY * pixelsPerUnitY );
414
415 int w = noUnitsX * pixelsPerUnitX;
416 int h = noUnitsY * pixelsPerUnitY;
417 m_targetWindow->SetVirtualSize( w ? w : wxDefaultCoord,
418 h ? h : wxDefaultCoord);
419
420 if (!noRefresh)
421 {
422 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
423 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
424
425 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
426 }
427 }
428
429 void wxScrolledWindow::AdjustScrollbars()
430 {
431 int w, h;
432 int vw, vh;
433
434 m_targetWindow->GetClientSize( &w, &h );
435 m_targetWindow->GetVirtualSize( &vw, &vh );
436
437 if (m_xScrollPixelsPerLine == 0)
438 {
439 m_hAdjust->upper = 1.0;
440 m_hAdjust->page_increment = 1.0;
441 m_hAdjust->page_size = 1.0;
442 }
443 else
444 {
445 m_hAdjust->upper = (vw+m_xScrollPixelsPerLine-1) / m_xScrollPixelsPerLine;
446 m_hAdjust->page_size = w / m_xScrollPixelsPerLine;
447 m_hAdjust->page_increment = w / m_xScrollPixelsPerLine;
448
449 // Special case. When client and virtual size are very close but
450 // the client is big enough, kill scrollbar.
451
452 if ((m_hAdjust->page_size < m_hAdjust->upper) && (w >= vw))
453 m_hAdjust->page_size += 1.0;
454
455 // If the scrollbar hits the right side, move the window
456 // right to keep it from over extending.
457
458 if ( !wxIsNullDouble(m_hAdjust->value) &&
459 (m_hAdjust->value + m_hAdjust->page_size > m_hAdjust->upper) )
460 {
461 m_hAdjust->value = m_hAdjust->upper - m_hAdjust->page_size;
462 if (m_hAdjust->value < 0.0)
463 m_hAdjust->value = 0.0;
464
465 if (GetChildren().GetCount() == 0)
466 m_xScrollPosition = (int)m_hAdjust->value; // This is enough without child windows
467 else
468 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" ); // Actually scroll window
469 }
470 }
471
472 if (m_yScrollPixelsPerLine == 0)
473 {
474 m_vAdjust->upper = 1.0;
475 m_vAdjust->page_increment = 1.0;
476 m_vAdjust->page_size = 1.0;
477 }
478 else
479 {
480 m_vAdjust->upper = (vh+m_yScrollPixelsPerLine-1) / m_yScrollPixelsPerLine;
481 m_vAdjust->page_size = h / m_yScrollPixelsPerLine;
482 m_vAdjust->page_increment = h / m_yScrollPixelsPerLine;
483
484 if ((m_vAdjust->page_size < m_vAdjust->upper) && (h >= vh))
485 m_vAdjust->page_size += 1.0;
486
487 if ( !wxIsNullDouble(m_vAdjust->value) &&
488 (m_vAdjust->value + m_vAdjust->page_size > m_vAdjust->upper) )
489 {
490 m_vAdjust->value = m_vAdjust->upper - m_vAdjust->page_size;
491 if (m_vAdjust->value < 0.0)
492 m_vAdjust->value = 0.0;
493
494 if (GetChildren().GetCount() == 0)
495 m_yScrollPosition = (int)m_vAdjust->value;
496 else
497 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
498 }
499 }
500
501 m_xScrollLinesPerPage = (int)(m_hAdjust->page_increment + 0.5);
502 m_yScrollLinesPerPage = (int)(m_vAdjust->page_increment + 0.5);
503
504 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
505 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
506 }
507
508
509 // ----------------------------------------------------------------------------
510 // target window handling
511 // ----------------------------------------------------------------------------
512
513 void wxScrolledWindow::SetTargetWindow( wxWindow *target, bool WXUNUSED(pushEventHandler) )
514 {
515 wxASSERT_MSG( target, wxT("target window must not be NULL") );
516 m_targetWindow = target;
517 }
518
519 wxWindow *wxScrolledWindow::GetTargetWindow() const
520 {
521 return m_targetWindow;
522 }
523
524 // Override this function if you don't want to have wxScrolledWindow
525 // automatically change the origin according to the scroll position.
526 void wxScrolledWindow::DoPrepareDC(wxDC& dc)
527 {
528 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
529 -m_yScrollPosition * m_yScrollPixelsPerLine );
530 }
531
532 void wxScrolledWindow::SetScrollRate( int xstep, int ystep )
533 {
534 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
535 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
536
537 m_xScrollPixelsPerLine = xstep;
538 m_yScrollPixelsPerLine = ystep;
539
540 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
541 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
542
543 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
544
545 AdjustScrollbars();
546 }
547
548 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
549 {
550 if ( x_unit )
551 *x_unit = m_xScrollPixelsPerLine;
552 if ( y_unit )
553 *y_unit = m_yScrollPixelsPerLine;
554 }
555
556 int wxScrolledWindow::GetScrollPageSize(int orient) const
557 {
558 if ( orient == wxHORIZONTAL )
559 return m_xScrollLinesPerPage;
560 else
561 return m_yScrollLinesPerPage;
562 }
563
564 void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
565 {
566 if ( orient == wxHORIZONTAL )
567 m_xScrollLinesPerPage = pageSize;
568 else
569 m_yScrollLinesPerPage = pageSize;
570 }
571
572 void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
573 {
574 int orient = event.GetOrientation();
575
576 int nScrollInc = CalcScrollInc(event);
577 if (nScrollInc == 0) return;
578
579 if (orient == wxHORIZONTAL)
580 {
581 int newPos = m_xScrollPosition + nScrollInc;
582 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
583 }
584 else
585 {
586 int newPos = m_yScrollPosition + nScrollInc;
587 SetScrollPos(wxVERTICAL, newPos, TRUE );
588 }
589
590 if (orient == wxHORIZONTAL)
591 {
592 m_xScrollPosition += nScrollInc;
593 }
594 else
595 {
596 m_yScrollPosition += nScrollInc;
597 }
598
599 if (orient == wxHORIZONTAL)
600 {
601 if (m_xScrollingEnabled)
602 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
603 else
604 m_targetWindow->Refresh();
605 }
606 else
607 {
608 if (m_yScrollingEnabled)
609 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
610 else
611 m_targetWindow->Refresh();
612 }
613 }
614
615 void wxScrolledWindow::Scroll( int x_pos, int y_pos )
616 {
617 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
618
619 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
620 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
621
622 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
623 {
624 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
625 if (max < 0) max = 0;
626 if (x_pos > max) x_pos = max;
627 if (x_pos < 0) x_pos = 0;
628
629 int old_x = m_xScrollPosition;
630 m_xScrollPosition = x_pos;
631 m_hAdjust->value = x_pos;
632
633 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
634
635 // Just update the scrollbar, don't send any wxWidgets event
636 GtkHDisconnectEvent();
637 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
638 GtkHConnectEvent();
639 }
640
641 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
642 {
643 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
644 if (max < 0) max = 0;
645 if (y_pos > max) y_pos = max;
646 if (y_pos < 0) y_pos = 0;
647
648 int old_y = m_yScrollPosition;
649 m_yScrollPosition = y_pos;
650 m_vAdjust->value = y_pos;
651
652 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
653
654 // Just update the scrollbar, don't send any wxWidgets event
655 GtkVDisconnectEvent();
656 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
657 GtkVConnectEvent();
658 }
659 }
660
661 // TODO: [VH]Scroll functions should be combined
662
663 void wxScrolledWindow::GtkVScroll( float value, unsigned int scroll_type )
664 {
665 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
666
667 if (m_yScrollPixelsPerLine == 0)
668 return;
669
670 int y_pos = (int)(value+0.5);
671
672 if (y_pos == m_yScrollPosition)
673 return;
674
675 wxEventType command = GtkScrollWinTypeToWx(scroll_type);
676
677 wxScrollWinEvent event( command, y_pos, wxVERTICAL );
678 event.SetEventObject( this );
679 GetEventHandler()->ProcessEvent( event );
680 }
681
682 void wxScrolledWindow::GtkHScroll( float value, unsigned int scroll_type )
683 {
684 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
685
686 if (m_xScrollPixelsPerLine == 0)
687 return;
688
689 int x_pos = (int)(value+0.5);
690
691 if (x_pos == m_xScrollPosition)
692 return;
693
694 wxEventType command = GtkScrollWinTypeToWx(scroll_type);
695
696 wxScrollWinEvent event( command, x_pos, wxHORIZONTAL );
697 event.SetEventObject( this );
698 GetEventHandler()->ProcessEvent( event );
699 }
700
701 void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
702 {
703 m_xScrollingEnabled = x_scroll;
704 m_yScrollingEnabled = y_scroll;
705 }
706
707 // Where the current view starts from
708 void wxScrolledWindow::GetViewStart (int *x, int *y) const
709 {
710 if ( x )
711 *x = m_xScrollPosition;
712 if ( y )
713 *y = m_yScrollPosition;
714 }
715
716 void wxScrolledWindow::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const
717 {
718 int xs, ys;
719 GetViewStart (& xs, & ys);
720
721 if ( xx )
722 *xx = x - xs * m_xScrollPixelsPerLine;
723 if ( yy )
724 *yy = y - ys * m_yScrollPixelsPerLine;
725 }
726
727 void wxScrolledWindow::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
728 {
729 int xs, ys;
730 GetViewStart (& xs, & ys);
731
732 if ( xx )
733 *xx = x + xs * m_xScrollPixelsPerLine;
734 if ( yy )
735 *yy = y + ys * m_yScrollPixelsPerLine;
736 }
737
738 int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
739 {
740 int pos = event.GetPosition();
741 int orient = event.GetOrientation();
742
743 int nScrollInc = 0;
744 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
745 {
746 if (orient == wxHORIZONTAL)
747 nScrollInc = - m_xScrollPosition;
748 else
749 nScrollInc = - m_yScrollPosition;
750 } else
751 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
752 {
753 if (orient == wxHORIZONTAL)
754 nScrollInc = GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine - m_xScrollPosition;
755 else
756 nScrollInc = GetVirtualSize().GetHeight() / m_yScrollPixelsPerLine - m_yScrollPosition;
757 } else
758 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
759 {
760 nScrollInc = -1;
761 } else
762 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
763 {
764 nScrollInc = 1;
765 } else
766 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
767 {
768 if (orient == wxHORIZONTAL)
769 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
770 else
771 nScrollInc = -GetScrollPageSize(wxVERTICAL);
772 } else
773 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
774 {
775 if (orient == wxHORIZONTAL)
776 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
777 else
778 nScrollInc = GetScrollPageSize(wxVERTICAL);
779 } else
780 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
781 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
782 {
783 if (orient == wxHORIZONTAL)
784 nScrollInc = pos - m_xScrollPosition;
785 else
786 nScrollInc = pos - m_yScrollPosition;
787 }
788
789 if (orient == wxHORIZONTAL)
790 {
791 if (m_xScrollPixelsPerLine > 0)
792 {
793 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
794 if (max < 0) max = 0;
795
796 if ( (m_xScrollPosition + nScrollInc) < 0 )
797 nScrollInc = -m_xScrollPosition; // As -ve as we can go
798 else if ( (m_xScrollPosition + nScrollInc) > max )
799 nScrollInc = max - m_xScrollPosition; // As +ve as we can go
800 }
801 else
802 m_targetWindow->Refresh();
803 }
804 else
805 {
806 if (m_yScrollPixelsPerLine > 0)
807 {
808 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
809 if (max < 0) max = 0;
810
811 if ( (m_yScrollPosition + nScrollInc) < 0 )
812 nScrollInc = -m_yScrollPosition; // As -ve as we can go
813 else if ( (m_yScrollPosition + nScrollInc) > max )
814 nScrollInc = max - m_yScrollPosition; // As +ve as we can go
815 }
816 else
817 m_targetWindow->Refresh();
818 }
819
820 return nScrollInc;
821 }
822
823 void wxScrolledWindow::SetScrollPos( int orient, int pos, bool refresh )
824 {
825 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
826
827 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
828
829 if (orient == wxHORIZONTAL)
830 {
831 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
832 if (max < 0) max = 0;
833
834 if (pos > max) pos = 0;
835 if (pos < 0) pos = 0;
836
837 if (pos == (int)(m_hAdjust->value+0.5)) return;
838 m_hAdjust->value = pos;
839 }
840 else
841 {
842 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
843 if (max < 0) max = 0;
844
845 if (pos > max) pos = 0;
846 if (pos < 0) pos = 0;
847
848 if (pos == (int)(m_vAdjust->value+0.5)) return;
849 m_vAdjust->value = pos;
850 }
851
852 if (m_wxwindow->window)
853 {
854 if (orient == wxHORIZONTAL)
855 {
856 // Just update the scrollbar, don't send any wxWidgets event
857 GtkHDisconnectEvent();
858 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
859 GtkHConnectEvent();
860 }
861 else
862 {
863 // Just update the scrollbar, don't send any wxWidgets event
864 GtkVDisconnectEvent();
865 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
866 GtkVConnectEvent();
867 }
868 }
869 }
870
871 void wxScrolledWindow::GtkVConnectEvent()
872 {
873 gtk_signal_connect( GTK_OBJECT(m_vAdjust), "value_changed",
874 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
875 }
876
877 void wxScrolledWindow::GtkHConnectEvent()
878 {
879 gtk_signal_connect( GTK_OBJECT(m_hAdjust), "value_changed",
880 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
881 }
882
883 void wxScrolledWindow::GtkHDisconnectEvent()
884 {
885 gtk_signal_disconnect_by_func( GTK_OBJECT(m_hAdjust),
886 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
887 }
888
889 void wxScrolledWindow::GtkVDisconnectEvent()
890 {
891 gtk_signal_disconnect_by_func( GTK_OBJECT(m_vAdjust),
892 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
893 }
894
895 bool wxScrolledWindow::Layout()
896 {
897 if (GetSizer() && m_targetWindow == this)
898 {
899 // If we're the scroll target, take into account the
900 // virtual size and scrolled position of the window.
901
902 int x, y, w, h;
903 CalcScrolledPosition(0,0, &x,&y);
904 GetVirtualSize(&w, &h);
905 GetSizer()->SetDimension(x, y, w, h);
906 return TRUE;
907 }
908 else
909 return wxPanel::Layout(); // fall back to default for LayoutConstraints
910 }
911
912 // ----------------------------------------------------------------------------
913 // event handlers
914 // ----------------------------------------------------------------------------
915
916 // Default OnSize resets scrollbars, if any
917 void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
918 {
919 if ( m_targetWindow->GetAutoLayout() )
920 {
921 wxSize size = m_targetWindow->GetBestVirtualSize();
922
923 // This will call ::Layout() and ::AdjustScrollbars()
924 SetVirtualSize( size );
925 }
926 else
927 {
928 AdjustScrollbars();
929 }
930 }
931
932 // This calls OnDraw, having adjusted the origin according to the current
933 // scroll position
934 void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
935 {
936 wxPaintDC dc(this);
937 PrepareDC(dc);
938
939 OnDraw(dc);
940 }
941
942 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
943 // compatibility here - if we used OnKeyDown(), the programs which process
944 // arrows themselves in their OnChar() would never get the message and like
945 // this they always have the priority
946 void wxScrolledWindow::OnChar(wxKeyEvent& event)
947 {
948 int stx, sty, // view origin
949 szx, szy, // view size (total)
950 clix, cliy; // view size (on screen)
951
952 GetViewStart(&stx, &sty);
953 GetClientSize(&clix, &cliy);
954 GetVirtualSize(&szx, &szy);
955
956 if( m_xScrollPixelsPerLine )
957 {
958 clix /= m_xScrollPixelsPerLine;
959 szx /= m_xScrollPixelsPerLine;
960 }
961 else
962 {
963 clix = 0;
964 szx = -1;
965 }
966 if( m_yScrollPixelsPerLine )
967 {
968 cliy /= m_yScrollPixelsPerLine;
969 szy /= m_yScrollPixelsPerLine;
970 }
971 else
972 {
973 cliy = 0;
974 szy = -1;
975 }
976
977 int xScrollOld = GetScrollPos(wxHORIZONTAL),
978 yScrollOld = GetScrollPos(wxVERTICAL);
979
980 int dsty;
981 switch ( event.GetKeyCode() )
982 {
983 case WXK_PAGEUP:
984 case WXK_PRIOR:
985 dsty = sty - (5 * cliy / 6);
986 Scroll(-1, (dsty == -1) ? 0 : dsty);
987 break;
988
989 case WXK_PAGEDOWN:
990 case WXK_NEXT:
991 Scroll(-1, sty + (5 * cliy / 6));
992 break;
993
994 case WXK_HOME:
995 Scroll(0, event.ControlDown() ? 0 : -1);
996 break;
997
998 case WXK_END:
999 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
1000 break;
1001
1002 case WXK_UP:
1003 Scroll(-1, sty - 1);
1004 break;
1005
1006 case WXK_DOWN:
1007 Scroll(-1, sty + 1);
1008 break;
1009
1010 case WXK_LEFT:
1011 Scroll(stx - 1, -1);
1012 break;
1013
1014 case WXK_RIGHT:
1015 Scroll(stx + 1, -1);
1016 break;
1017
1018 default:
1019 // not for us
1020 event.Skip();
1021 return;
1022 }
1023
1024 int xScroll = GetScrollPos(wxHORIZONTAL);
1025 if ( xScroll != xScrollOld )
1026 {
1027 wxScrollWinEvent eventS(wxEVT_SCROLLWIN_THUMBTRACK, xScroll,
1028 wxHORIZONTAL);
1029 eventS.SetEventObject(this);
1030 GetEventHandler()->ProcessEvent(eventS);
1031 }
1032
1033 int yScroll = GetScrollPos(wxVERTICAL);
1034 if ( yScroll != yScrollOld )
1035 {
1036 wxScrollWinEvent eventS(wxEVT_SCROLLWIN_THUMBTRACK, yScroll,
1037 wxVERTICAL);
1038 eventS.SetEventObject(this);
1039 GetEventHandler()->ProcessEvent(eventS);
1040 }
1041 }
1042
1043
1044 // vi:sts=4:sw=4:et