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