]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolwin.cpp
applying OSAF patch for "toolbar selection not correct on Mac after calling Realize...
[wxWidgets.git] / src / gtk / scrolwin.cpp
CommitLineData
30954328 1/////////////////////////////////////////////////////////////////////////////
e1437456 2// Name: gtk/scrolwin.cpp
30954328 3// Purpose: wxScrolledWindow implementation
47a3ff38 4// Author: Robert Roebling
566d84a7 5// Modified by: Ron Lee
30954328
RR
6// Created: 01/02/97
7// RCS-ID: $Id$
47a3ff38 8// Copyright: (c) Robert Roebling
65571936 9// Licence: wxWindows licence
30954328
RR
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
30954328
RR
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
e1bf3ad3 31#include "wx/scrolwin.h"
30954328
RR
32#include "wx/utils.h"
33#include "wx/dcclient.h"
30954328 34#include "wx/panel.h"
30486297 35#include "wx/sizer.h"
30954328 36
9e691f46 37#include "wx/gtk/private.h"
30954328
RR
38#include "wx/gtk/win_gtk.h"
39
40// ----------------------------------------------------------------------------
41// event tables
42// ----------------------------------------------------------------------------
43
44BEGIN_EVENT_TABLE(wxScrolledWindow, wxPanel)
d9a4f620 45 EVT_SCROLLWIN(wxScrolledWindow::OnScroll)
30954328
RR
46 EVT_SIZE(wxScrolledWindow::OnSize)
47 EVT_PAINT(wxScrolledWindow::OnPaint)
48 EVT_CHAR(wxScrolledWindow::OnChar)
49END_EVENT_TABLE()
50
51IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow, wxPanel)
52
53// ============================================================================
54// implementation
55// ============================================================================
56
57//-----------------------------------------------------------------------------
58// data
59//-----------------------------------------------------------------------------
60
61extern bool g_blockEventsOnDrag;
cf19b0bd 62extern bool g_blockEventsOnScroll;
30954328
RR
63
64//-----------------------------------------------------------------------------
65// idle system
66//-----------------------------------------------------------------------------
67
68extern void wxapp_install_idle_handler();
69extern bool g_isIdle;
70
71//-----------------------------------------------------------------------------
72// "value_changed" from m_vAdjust
73//-----------------------------------------------------------------------------
74
865bb325 75extern "C" {
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;
47a3ff38 86
9e691f46
VZ
87 win->GtkVScroll( adjust->value,
88 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win->m_widget)->vscrollbar) );
30954328 89}
865bb325 90}
30954328
RR
91
92//-----------------------------------------------------------------------------
93// "value_changed" from m_hAdjust
94//-----------------------------------------------------------------------------
95
865bb325 96extern "C" {
9e691f46
VZ
97static void gtk_scrolled_window_hscroll_callback( GtkAdjustment *adjust,
98 SCROLLBAR_CBACK_ARG
99 wxScrolledWindow *win )
30954328
RR
100{
101 if (g_isIdle)
102 wxapp_install_idle_handler();
103
104 if (g_blockEventsOnDrag) return;
105 if (!win->m_hasVMT) return;
106
9e691f46
VZ
107 win->GtkHScroll( adjust->value,
108 GET_SCROLL_TYPE(GTK_SCROLLED_WINDOW(win->m_widget)->hscrollbar) );
30954328 109}
865bb325 110}
30954328 111
cf19b0bd
RR
112//-----------------------------------------------------------------------------
113// "button_press_event" from scrollbar
114//-----------------------------------------------------------------------------
115
865bb325 116extern "C" {
cf19b0bd
RR
117static 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;
9e691f46
VZ
125
126 // FIXME: there is no slider field any more, what was meant here?
127#ifndef __WXGTK20__
cf19b0bd 128 win->m_isScrolling = (gdk_event->window == widget->slider);
9e691f46 129#endif
cf19b0bd
RR
130
131 return FALSE;
132}
865bb325 133}
cf19b0bd
RR
134
135//-----------------------------------------------------------------------------
136// "button_release_event" from scrollbar
137//-----------------------------------------------------------------------------
138
865bb325 139extern "C" {
cf19b0bd
RR
140static 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}
865bb325 178}
cf19b0bd 179
30954328
RR
180//-----------------------------------------------------------------------------
181// InsertChild for wxScrolledWindow
182//-----------------------------------------------------------------------------
183
184static void wxInsertChildInScrolledWindow( wxWindow* parent, wxWindow* child )
185{
830ed6d9
RR
186 // The window might have been scrolled already, do we
187 // have to adapt the position.
30954328
RR
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
830ed6d9 204void wxScrolledWindow::Init()
30954328
RR
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;
30954328
RR
212 m_xScrollLinesPerPage = 0;
213 m_yScrollLinesPerPage = 0;
214 m_targetWindow = (wxWindow*) NULL;
d9a4f620
RR
215 m_scaleX = 1.0;
216 m_scaleY = 1.0;
e1437456 217 m_hasScrolling = TRUE;
30954328
RR
218}
219
220bool wxScrolledWindow::Create(wxWindow *parent,
221 wxWindowID id,
222 const wxPoint& pos,
223 const wxSize& size,
224 long style,
225 const wxString& name)
226{
830ed6d9
RR
227 Init();
228
30954328
RR
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;
39c869a6 237
30954328
RR
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;
4ca24f18 284 m_vAdjust->page_increment = 2.0;
30954328
RR
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;
4ca24f18 290 m_hAdjust->page_increment = 2.0;
30954328
RR
291 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
292
830ed6d9
RR
293 // Handlers for new scrollbar values
294 GtkVConnectEvent();
295 GtkHConnectEvent();
30954328 296
cf19b0bd 297 // these handlers block mouse events to any window during scrolling such as
77ffb593 298 // motion events and prevent GTK and wxWidgets from fighting over where the
cf19b0bd
RR
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
30954328
RR
313 gtk_widget_show( m_wxwindow );
314
315 if (m_parent)
316 m_parent->DoAddChild( this );
30486297 317
c9c4b3a0 318 m_focusWidget = m_wxwindow;
30954328
RR
319
320 PostCreation();
321
322 Show( TRUE );
2b5f62a0 323
30954328
RR
324 return TRUE;
325}
326
30954328
RR
327// ----------------------------------------------------------------------------
328// setting scrolling parameters
329// ----------------------------------------------------------------------------
330
566d84a7
RL
331void wxScrolledWindow::DoSetVirtualSize( int x, int y )
332{
333 wxPanel::DoSetVirtualSize( x, y );
334 AdjustScrollbars();
92aff599 335
92aff599
RL
336 if (GetAutoLayout())
337 Layout();
566d84a7
RL
338}
339
844adaa4
JS
340// wxWindow's GetBestVirtualSize returns the actual window size,
341// whereas we want to return the virtual size
342wxSize 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)
357wxSize 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
30954328
RR
394/*
395 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
396 * noUnitsX/noUnitsY: : no. units per scrollbar
397 */
566d84a7
RL
398void wxScrolledWindow::SetScrollbars( int pixelsPerUnitX, int pixelsPerUnitY,
399 int noUnitsX, int noUnitsY,
400 int xPos, int yPos, bool noRefresh )
30954328 401{
1adff2f5
JS
402 int xs, ys;
403 GetViewStart (& xs, & ys);
404
405 int old_x = m_xScrollPixelsPerLine * xs;
406 int old_y = m_yScrollPixelsPerLine * ys;
30486297 407
30954328
RR
408 m_xScrollPixelsPerLine = pixelsPerUnitX;
409 m_yScrollPixelsPerLine = pixelsPerUnitY;
39c869a6 410
566d84a7
RL
411 m_hAdjust->value = m_xScrollPosition = xPos;
412 m_vAdjust->value = m_yScrollPosition = yPos;
30954328 413
2b5f62a0
VZ
414 // Setting hints here should arguably be deprecated, but without it
415 // a sizer might override this manual scrollbar setting in old code.
878ddad5 416 // m_targetWindow->SetVirtualSizeHints( noUnitsX * pixelsPerUnitX, noUnitsY * pixelsPerUnitY );
30486297 417
f18f464c
VZ
418 int w = noUnitsX * pixelsPerUnitX;
419 int h = noUnitsY * pixelsPerUnitY;
420 m_targetWindow->SetVirtualSize( w ? w : wxDefaultCoord,
421 h ? h : wxDefaultCoord);
2b5f62a0 422
ec7482df
RR
423 if (!noRefresh)
424 {
425 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
426 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
30486297 427
566d84a7 428 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
ec7482df 429 }
30954328
RR
430}
431
432void wxScrolledWindow::AdjustScrollbars()
433{
434 int w, h;
566d84a7
RL
435 int vw, vh;
436
30954328 437 m_targetWindow->GetClientSize( &w, &h );
566d84a7 438 m_targetWindow->GetVirtualSize( &vw, &vh );
878ddad5 439
30954328 440 if (m_xScrollPixelsPerLine == 0)
4ca24f18
RR
441 {
442 m_hAdjust->upper = 1.0;
47a3ff38 443 m_hAdjust->page_increment = 1.0;
30954328 444 m_hAdjust->page_size = 1.0;
4ca24f18 445 }
30954328 446 else
4ca24f18 447 {
878ddad5
RR
448 m_hAdjust->upper = (vw+m_xScrollPixelsPerLine-1) / m_xScrollPixelsPerLine;
449 m_hAdjust->page_size = w / m_xScrollPixelsPerLine;
bf67b135 450 m_hAdjust->page_increment = w / m_xScrollPixelsPerLine;
39c869a6 451
878ddad5
RR
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
566d84a7
RL
458 // If the scrollbar hits the right side, move the window
459 // right to keep it from over extending.
4ca24f18 460
2b5f62a0 461 if ((m_hAdjust->value != 0.0) && (m_hAdjust->value + m_hAdjust->page_size > m_hAdjust->upper))
566d84a7 462 {
2b5f62a0
VZ
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
b03f81b7 467 if (GetChildren().GetCount() == 0)
2b5f62a0
VZ
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
566d84a7 471 }
4ca24f18
RR
472 }
473
30954328 474 if (m_yScrollPixelsPerLine == 0)
4ca24f18
RR
475 {
476 m_vAdjust->upper = 1.0;
47a3ff38 477 m_vAdjust->page_increment = 1.0;
30954328 478 m_vAdjust->page_size = 1.0;
4ca24f18 479 }
30954328 480 else
4ca24f18 481 {
878ddad5
RR
482 m_vAdjust->upper = (vh+m_yScrollPixelsPerLine-1) / m_yScrollPixelsPerLine;
483 m_vAdjust->page_size = h / m_yScrollPixelsPerLine;
bf67b135 484 m_vAdjust->page_increment = h / m_yScrollPixelsPerLine;
878ddad5
RR
485
486 if ((m_vAdjust->page_size < m_vAdjust->upper) && (h >= vh))
487 m_vAdjust->page_size += 1.0;
39c869a6 488
2b5f62a0 489 if ((m_vAdjust->value != 0.0) && (m_vAdjust->value + m_vAdjust->page_size > m_vAdjust->upper))
566d84a7 490 {
2b5f62a0
VZ
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
b03f81b7 495 if (GetChildren().GetCount() == 0)
2b5f62a0
VZ
496 m_yScrollPosition = (int)m_vAdjust->value;
497 else
498 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
566d84a7 499 }
4ca24f18
RR
500 }
501
47a3ff38
RR
502 m_xScrollLinesPerPage = (int)(m_hAdjust->page_increment + 0.5);
503 m_yScrollLinesPerPage = (int)(m_vAdjust->page_increment + 0.5);
39c869a6 504
30954328
RR
505 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
506 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
507}
508
566d84a7 509
30954328
RR
510// ----------------------------------------------------------------------------
511// target window handling
512// ----------------------------------------------------------------------------
513
13ff9344 514void wxScrolledWindow::SetTargetWindow( wxWindow *target, bool WXUNUSED(pushEventHandler) )
30954328
RR
515{
516 wxASSERT_MSG( target, wxT("target window must not be NULL") );
517 m_targetWindow = target;
518}
519
bfe35776 520wxWindow *wxScrolledWindow::GetTargetWindow() const
30954328
RR
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.
5de9f492 527void wxScrolledWindow::DoPrepareDC(wxDC& dc)
30954328
RR
528{
529 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
530 -m_yScrollPosition * m_yScrollPixelsPerLine );
531}
532
566d84a7
RL
533void 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
30954328
RR
549void 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
557int wxScrolledWindow::GetScrollPageSize(int orient) const
558{
559 if ( orient == wxHORIZONTAL )
560 return m_xScrollLinesPerPage;
561 else
562 return m_yScrollLinesPerPage;
563}
564
565void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
566{
567 if ( orient == wxHORIZONTAL )
568 m_xScrollLinesPerPage = pageSize;
569 else
570 m_yScrollLinesPerPage = pageSize;
571}
572
d9a4f620
RR
573void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
574{
575 int orient = event.GetOrientation();
0bc0cd5d 576
d9a4f620
RR
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
30954328
RR
616void wxScrolledWindow::Scroll( int x_pos, int y_pos )
617{
566d84a7 618 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
30954328
RR
619
620 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
621 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
622
30954328
RR
623 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
624 {
830ed6d9
RR
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;
39c869a6 629
30954328
RR
630 int old_x = m_xScrollPosition;
631 m_xScrollPosition = x_pos;
aafe4488 632 m_hAdjust->value = x_pos;
39c869a6 633
aafe4488 634 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
39c869a6 635
77ffb593 636 // Just update the scrollbar, don't send any wxWidgets event
830ed6d9 637 GtkHDisconnectEvent();
aafe4488 638 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
830ed6d9 639 GtkHConnectEvent();
30954328 640 }
39c869a6 641
30954328
RR
642 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
643 {
830ed6d9
RR
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;
39c869a6 648
30954328
RR
649 int old_y = m_yScrollPosition;
650 m_yScrollPosition = y_pos;
aafe4488 651 m_vAdjust->value = y_pos;
39c869a6 652
aafe4488 653 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
39c869a6 654
77ffb593 655 // Just update the scrollbar, don't send any wxWidgets event
830ed6d9 656 GtkVDisconnectEvent();
aafe4488 657 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
830ed6d9 658 GtkVConnectEvent();
30954328
RR
659 }
660}
661
9e691f46
VZ
662// TODO: [VH]Scroll functions should be combined
663
664void wxScrolledWindow::GtkVScroll( float value, unsigned int scroll_type )
30954328 665{
566d84a7 666 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
aafe4488
RR
667
668 if (m_yScrollPixelsPerLine == 0)
669 return;
39c869a6 670
aafe4488 671 int y_pos = (int)(value+0.5);
39c869a6 672
aafe4488
RR
673 if (y_pos == m_yScrollPosition)
674 return;
39c869a6 675
9e691f46 676 wxEventType command = GtkScrollWinTypeToWx(scroll_type);
d9a4f620 677
0bc0cd5d 678 wxScrollWinEvent event( command, y_pos, wxVERTICAL );
d9a4f620
RR
679 event.SetEventObject( this );
680 GetEventHandler()->ProcessEvent( event );
30954328
RR
681}
682
9e691f46 683void wxScrolledWindow::GtkHScroll( float value, unsigned int scroll_type )
30954328 684{
566d84a7 685 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
39c869a6 686
aafe4488
RR
687 if (m_xScrollPixelsPerLine == 0)
688 return;
39c869a6 689
aafe4488 690 int x_pos = (int)(value+0.5);
0bc0cd5d 691
aafe4488
RR
692 if (x_pos == m_xScrollPosition)
693 return;
aafe4488 694
9e691f46 695 wxEventType command = GtkScrollWinTypeToWx(scroll_type);
d9a4f620 696
0bc0cd5d 697 wxScrollWinEvent event( command, x_pos, wxHORIZONTAL );
d9a4f620
RR
698 event.SetEventObject( this );
699 GetEventHandler()->ProcessEvent( event );
30954328
RR
700}
701
702void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
703{
704 m_xScrollingEnabled = x_scroll;
705 m_yScrollingEnabled = y_scroll;
706}
707
30954328
RR
708// Where the current view starts from
709void wxScrolledWindow::GetViewStart (int *x, int *y) const
710{
711 if ( x )
712 *x = m_xScrollPosition;
713 if ( y )
714 *y = m_yScrollPosition;
715}
716
8c2f3797 717void wxScrolledWindow::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const
30954328 718{
1adff2f5
JS
719 int xs, ys;
720 GetViewStart (& xs, & ys);
721
30954328 722 if ( xx )
1adff2f5 723 *xx = x - xs * m_xScrollPixelsPerLine;
30954328 724 if ( yy )
1adff2f5 725 *yy = y - ys * m_yScrollPixelsPerLine;
30954328
RR
726}
727
8c2f3797 728void wxScrolledWindow::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
30954328 729{
1adff2f5
JS
730 int xs, ys;
731 GetViewStart (& xs, & ys);
732
30954328 733 if ( xx )
1adff2f5 734 *xx = x + xs * m_xScrollPixelsPerLine;
30954328 735 if ( yy )
1adff2f5 736 *yy = y + ys * m_yScrollPixelsPerLine;
30954328
RR
737}
738
d9a4f620
RR
739int 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)
566d84a7 755 nScrollInc = GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine - m_xScrollPosition;
d9a4f620 756 else
566d84a7 757 nScrollInc = GetVirtualSize().GetHeight() / m_yScrollPixelsPerLine - m_yScrollPosition;
d9a4f620
RR
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 {
830ed6d9
RR
794 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
795 if (max < 0) max = 0;
d9a4f620
RR
796
797 if ( (m_xScrollPosition + nScrollInc) < 0 )
798 nScrollInc = -m_xScrollPosition; // As -ve as we can go
830ed6d9
RR
799 else if ( (m_xScrollPosition + nScrollInc) > max )
800 nScrollInc = max - m_xScrollPosition; // As +ve as we can go
d9a4f620
RR
801 }
802 else
803 m_targetWindow->Refresh();
804 }
805 else
806 {
807 if (m_yScrollPixelsPerLine > 0)
808 {
830ed6d9
RR
809 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
810 if (max < 0) max = 0;
d9a4f620
RR
811
812 if ( (m_yScrollPosition + nScrollInc) < 0 )
813 nScrollInc = -m_yScrollPosition; // As -ve as we can go
830ed6d9
RR
814 else if ( (m_yScrollPosition + nScrollInc) > max )
815 nScrollInc = max - m_yScrollPosition; // As +ve as we can go
d9a4f620
RR
816 }
817 else
818 m_targetWindow->Refresh();
819 }
820
821 return nScrollInc;
822}
823
f47ae6e7 824void wxScrolledWindow::SetScrollPos( int orient, int pos, bool refresh )
0bc0cd5d
RR
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 {
830ed6d9
RR
832 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
833 if (max < 0) max = 0;
39c869a6 834
830ed6d9
RR
835 if (pos > max) pos = 0;
836 if (pos < 0) pos = 0;
39c869a6 837
830ed6d9
RR
838 if (pos == (int)(m_hAdjust->value+0.5)) return;
839 m_hAdjust->value = pos;
0bc0cd5d
RR
840 }
841 else
842 {
830ed6d9
RR
843 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
844 if (max < 0) max = 0;
39c869a6 845
830ed6d9
RR
846 if (pos > max) pos = 0;
847 if (pos < 0) pos = 0;
39c869a6 848
830ed6d9
RR
849 if (pos == (int)(m_vAdjust->value+0.5)) return;
850 m_vAdjust->value = pos;
0bc0cd5d
RR
851 }
852
853 if (m_wxwindow->window)
854 {
855 if (orient == wxHORIZONTAL)
856 {
77ffb593 857 // Just update the scrollbar, don't send any wxWidgets event
830ed6d9 858 GtkHDisconnectEvent();
0bc0cd5d 859 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
830ed6d9 860 GtkHConnectEvent();
0bc0cd5d
RR
861 }
862 else
863 {
77ffb593 864 // Just update the scrollbar, don't send any wxWidgets event
830ed6d9 865 GtkVDisconnectEvent();
0bc0cd5d 866 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
830ed6d9 867 GtkVConnectEvent();
0bc0cd5d
RR
868 }
869 }
870}
871
830ed6d9
RR
872void wxScrolledWindow::GtkVConnectEvent()
873{
874 gtk_signal_connect( GTK_OBJECT(m_vAdjust), "value_changed",
875 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
876}
877
878void wxScrolledWindow::GtkHConnectEvent()
879{
880 gtk_signal_connect( GTK_OBJECT(m_hAdjust), "value_changed",
881 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
882}
883
884void wxScrolledWindow::GtkHDisconnectEvent()
885{
886 gtk_signal_disconnect_by_func( GTK_OBJECT(m_hAdjust),
887 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
888}
889
890void wxScrolledWindow::GtkVDisconnectEvent()
891{
892 gtk_signal_disconnect_by_func( GTK_OBJECT(m_vAdjust),
893 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
894}
895
30486297
RD
896bool wxScrolledWindow::Layout()
897{
566d84a7 898 if (GetSizer() && m_targetWindow == this)
30486297 899 {
566d84a7
RL
900 // If we're the scroll target, take into account the
901 // virtual size and scrolled position of the window.
902
30486297
RD
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
30954328
RR
913// ----------------------------------------------------------------------------
914// event handlers
915// ----------------------------------------------------------------------------
916
917// Default OnSize resets scrollbars, if any
918void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
919{
c376d80f 920 if ( m_targetWindow->GetAutoLayout() )
2b5f62a0 921 {
c376d80f
RR
922 wxSize size = m_targetWindow->GetBestVirtualSize();
923
924 // This will call ::Layout() and ::AdjustScrollbars()
925 SetVirtualSize( size );
2b5f62a0
VZ
926 }
927 else
928 {
929 AdjustScrollbars();
930 }
30954328
RR
931}
932
933// This calls OnDraw, having adjusted the origin according to the current
934// scroll position
935void 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
947void 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
2b5f62a0 953 GetViewStart(&stx, &sty);
30954328
RR
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
39c869a6
VZ
978 int xScrollOld = GetScrollPos(wxHORIZONTAL),
979 yScrollOld = GetScrollPos(wxVERTICAL);
980
30954328 981 int dsty;
12a3f227 982 switch ( event.GetKeyCode() )
30954328
RR
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();
39c869a6
VZ
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);
30954328
RR
1041 }
1042}
3379ed37 1043
566d84a7
RL
1044
1045// vi:sts=4:sw=4:et