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