]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolwin.cpp
Removed erroneous copyright names and corrected licence spelling
[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
566d84a7 5// Modified by: Ron Lee
30954328
RR
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;
30954328
RR
205 m_xScrollLinesPerPage = 0;
206 m_yScrollLinesPerPage = 0;
207 m_targetWindow = (wxWindow*) NULL;
d9a4f620
RR
208 m_scaleX = 1.0;
209 m_scaleY = 1.0;
e1437456 210 m_hasScrolling = TRUE;
30954328
RR
211}
212
213bool wxScrolledWindow::Create(wxWindow *parent,
214 wxWindowID id,
215 const wxPoint& pos,
216 const wxSize& size,
217 long style,
218 const wxString& name)
219{
830ed6d9
RR
220 Init();
221
30954328
RR
222 if (!PreCreation( parent, pos, size ) ||
223 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
224 {
225 wxFAIL_MSG( wxT("wxWindow creation failed") );
226 return FALSE;
227 }
228
229 m_insertCallback = wxInsertChildInScrolledWindow;
39c869a6 230
30954328
RR
231 m_targetWindow = this;
232
233 m_widget = gtk_scrolled_window_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
234 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
235
236 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(m_widget);
237
238 GtkScrolledWindowClass *scroll_class = GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget) );
239 scroll_class->scrollbar_spacing = 0;
240
241 gtk_scrolled_window_set_policy( scrolledWindow, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
242
243 m_hAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->hscrollbar) );
244 m_vAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->vscrollbar) );
245
246 m_wxwindow = gtk_pizza_new();
247
248 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
249
250 GtkPizza *pizza = GTK_PIZZA(m_wxwindow);
251
252 if (HasFlag(wxRAISED_BORDER))
253 {
254 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_OUT );
255 }
256 else if (HasFlag(wxSUNKEN_BORDER))
257 {
258 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_IN );
259 }
260 else if (HasFlag(wxSIMPLE_BORDER))
261 {
262 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_THIN );
263 }
264 else
265 {
266 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_NONE );
267 }
268
269 GTK_WIDGET_SET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
270 m_acceptsFocus = TRUE;
271
272 // I _really_ don't want scrollbars in the beginning
273 m_vAdjust->lower = 0.0;
274 m_vAdjust->upper = 1.0;
275 m_vAdjust->value = 0.0;
276 m_vAdjust->step_increment = 1.0;
4ca24f18 277 m_vAdjust->page_increment = 2.0;
30954328
RR
278 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
279 m_hAdjust->lower = 0.0;
280 m_hAdjust->upper = 1.0;
281 m_hAdjust->value = 0.0;
282 m_hAdjust->step_increment = 1.0;
4ca24f18 283 m_hAdjust->page_increment = 2.0;
30954328
RR
284 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
285
830ed6d9
RR
286 // Handlers for new scrollbar values
287 GtkVConnectEvent();
288 GtkHConnectEvent();
30954328 289
cf19b0bd
RR
290 // these handlers block mouse events to any window during scrolling such as
291 // motion events and prevent GTK and wxWindows from fighting over where the
292 // slider should be
293
294 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_press_event",
295 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
296
297 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_press_event",
298 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
299
300 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_release_event",
301 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
302
303 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_release_event",
304 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
305
30954328
RR
306 gtk_widget_show( m_wxwindow );
307
308 if (m_parent)
309 m_parent->DoAddChild( this );
30486297 310
c9c4b3a0 311 m_focusWidget = m_wxwindow;
30954328
RR
312
313 PostCreation();
314
315 Show( TRUE );
2b5f62a0 316
30954328
RR
317 return TRUE;
318}
319
30954328
RR
320// ----------------------------------------------------------------------------
321// setting scrolling parameters
322// ----------------------------------------------------------------------------
323
566d84a7
RL
324void wxScrolledWindow::DoSetVirtualSize( int x, int y )
325{
326 wxPanel::DoSetVirtualSize( x, y );
327 AdjustScrollbars();
92aff599
RL
328
329#if wxUSE_CONSTRAINTS
330 if (GetAutoLayout())
331 Layout();
332#endif
566d84a7
RL
333}
334
30954328
RR
335/*
336 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
337 * noUnitsX/noUnitsY: : no. units per scrollbar
338 */
566d84a7
RL
339void wxScrolledWindow::SetScrollbars( int pixelsPerUnitX, int pixelsPerUnitY,
340 int noUnitsX, int noUnitsY,
341 int xPos, int yPos, bool noRefresh )
30954328 342{
ec7482df
RR
343 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
344 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
30486297 345
30954328
RR
346 m_xScrollPixelsPerLine = pixelsPerUnitX;
347 m_yScrollPixelsPerLine = pixelsPerUnitY;
39c869a6 348
566d84a7
RL
349 m_hAdjust->value = m_xScrollPosition = xPos;
350 m_vAdjust->value = m_yScrollPosition = yPos;
30954328 351
2b5f62a0
VZ
352 // Setting hints here should arguably be deprecated, but without it
353 // a sizer might override this manual scrollbar setting in old code.
566d84a7 354 m_targetWindow->SetVirtualSizeHints( noUnitsX * pixelsPerUnitX, noUnitsY * pixelsPerUnitY );
30486297 355
2b5f62a0
VZ
356 m_targetWindow->SetVirtualSize( noUnitsX * pixelsPerUnitX, noUnitsY * pixelsPerUnitY );
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
566d84a7 363 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
ec7482df 364 }
30954328
RR
365}
366
367void wxScrolledWindow::AdjustScrollbars()
368{
369 int w, h;
566d84a7
RL
370 int vw, vh;
371
30954328 372 m_targetWindow->GetClientSize( &w, &h );
566d84a7 373 m_targetWindow->GetVirtualSize( &vw, &vh );
39c869a6 374
30954328 375 if (m_xScrollPixelsPerLine == 0)
4ca24f18
RR
376 {
377 m_hAdjust->upper = 1.0;
30954328 378 m_hAdjust->page_size = 1.0;
4ca24f18 379 }
30954328 380 else
4ca24f18 381 {
566d84a7 382 m_hAdjust->upper = vw / m_xScrollPixelsPerLine;
30954328 383 m_hAdjust->page_size = (w / m_xScrollPixelsPerLine);
39c869a6 384
566d84a7
RL
385 // If the scrollbar hits the right side, move the window
386 // right to keep it from over extending.
4ca24f18 387
2b5f62a0 388 if ((m_hAdjust->value != 0.0) && (m_hAdjust->value + m_hAdjust->page_size > m_hAdjust->upper))
566d84a7 389 {
2b5f62a0
VZ
390 m_hAdjust->value = m_hAdjust->upper - m_hAdjust->page_size;
391 if (m_hAdjust->value < 0.0)
392 m_hAdjust->value = 0.0;
393
b03f81b7 394 if (GetChildren().GetCount() == 0)
2b5f62a0
VZ
395 m_xScrollPosition = (int)m_hAdjust->value; // This is enough without child windows
396 else
397 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" ); // Actually scroll window
566d84a7 398 }
4ca24f18
RR
399 }
400
30954328 401 if (m_yScrollPixelsPerLine == 0)
4ca24f18
RR
402 {
403 m_vAdjust->upper = 1.0;
30954328 404 m_vAdjust->page_size = 1.0;
4ca24f18 405 }
30954328 406 else
4ca24f18 407 {
566d84a7 408 m_vAdjust->upper = vh / m_yScrollPixelsPerLine;
30954328 409 m_vAdjust->page_size = (h / m_yScrollPixelsPerLine);
39c869a6 410
2b5f62a0 411 if ((m_vAdjust->value != 0.0) && (m_vAdjust->value + m_vAdjust->page_size > m_vAdjust->upper))
566d84a7 412 {
2b5f62a0
VZ
413 m_vAdjust->value = m_vAdjust->upper - m_vAdjust->page_size;
414 if (m_vAdjust->value < 0.0)
415 m_vAdjust->value = 0.0;
416
b03f81b7 417 if (GetChildren().GetCount() == 0)
2b5f62a0
VZ
418 m_yScrollPosition = (int)m_vAdjust->value;
419 else
420 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
566d84a7 421 }
4ca24f18
RR
422 }
423
0bc0cd5d
RR
424 m_xScrollLinesPerPage = (int)(m_hAdjust->page_size + 0.5);
425 m_yScrollLinesPerPage = (int)(m_vAdjust->page_size + 0.5);
39c869a6 426
30954328
RR
427 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
428 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
429}
430
566d84a7 431
30954328
RR
432// ----------------------------------------------------------------------------
433// target window handling
434// ----------------------------------------------------------------------------
435
13ff9344 436void wxScrolledWindow::SetTargetWindow( wxWindow *target, bool WXUNUSED(pushEventHandler) )
30954328
RR
437{
438 wxASSERT_MSG( target, wxT("target window must not be NULL") );
439 m_targetWindow = target;
440}
441
442wxWindow *wxScrolledWindow::GetTargetWindow()
443{
444 return m_targetWindow;
445}
446
447// Override this function if you don't want to have wxScrolledWindow
448// automatically change the origin according to the scroll position.
449void wxScrolledWindow::PrepareDC(wxDC& dc)
450{
451 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
452 -m_yScrollPosition * m_yScrollPixelsPerLine );
453}
454
566d84a7
RL
455void wxScrolledWindow::SetScrollRate( int xstep, int ystep )
456{
457 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
458 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
459
460 m_xScrollPixelsPerLine = xstep;
461 m_yScrollPixelsPerLine = ystep;
462
463 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
464 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
465
466 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
467
468 AdjustScrollbars();
469}
470
30954328
RR
471void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
472{
473 if ( x_unit )
474 *x_unit = m_xScrollPixelsPerLine;
475 if ( y_unit )
476 *y_unit = m_yScrollPixelsPerLine;
477}
478
479int wxScrolledWindow::GetScrollPageSize(int orient) const
480{
481 if ( orient == wxHORIZONTAL )
482 return m_xScrollLinesPerPage;
483 else
484 return m_yScrollLinesPerPage;
485}
486
487void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
488{
489 if ( orient == wxHORIZONTAL )
490 m_xScrollLinesPerPage = pageSize;
491 else
492 m_yScrollLinesPerPage = pageSize;
493}
494
d9a4f620
RR
495void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
496{
497 int orient = event.GetOrientation();
0bc0cd5d 498
d9a4f620
RR
499 int nScrollInc = CalcScrollInc(event);
500 if (nScrollInc == 0) return;
501
502 if (orient == wxHORIZONTAL)
503 {
504 int newPos = m_xScrollPosition + nScrollInc;
505 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
506 }
507 else
508 {
509 int newPos = m_yScrollPosition + nScrollInc;
510 SetScrollPos(wxVERTICAL, newPos, TRUE );
511 }
512
513 if (orient == wxHORIZONTAL)
514 {
515 m_xScrollPosition += nScrollInc;
516 }
517 else
518 {
519 m_yScrollPosition += nScrollInc;
520 }
521
522 if (orient == wxHORIZONTAL)
523 {
524 if (m_xScrollingEnabled)
525 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
526 else
527 m_targetWindow->Refresh();
528 }
529 else
530 {
531 if (m_yScrollingEnabled)
532 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
533 else
534 m_targetWindow->Refresh();
535 }
536}
537
30954328
RR
538void wxScrolledWindow::Scroll( int x_pos, int y_pos )
539{
566d84a7 540 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
30954328
RR
541
542 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
543 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
544
30954328
RR
545 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
546 {
830ed6d9
RR
547 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
548 if (max < 0) max = 0;
549 if (x_pos > max) x_pos = max;
550 if (x_pos < 0) x_pos = 0;
39c869a6 551
30954328
RR
552 int old_x = m_xScrollPosition;
553 m_xScrollPosition = x_pos;
aafe4488 554 m_hAdjust->value = x_pos;
39c869a6 555
aafe4488 556 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
39c869a6 557
830ed6d9
RR
558 // Just update the scrollbar, don't send any wxWindows event
559 GtkHDisconnectEvent();
aafe4488 560 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
830ed6d9 561 GtkHConnectEvent();
30954328 562 }
39c869a6 563
30954328
RR
564 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
565 {
830ed6d9
RR
566 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
567 if (max < 0) max = 0;
568 if (y_pos > max) y_pos = max;
569 if (y_pos < 0) y_pos = 0;
39c869a6 570
30954328
RR
571 int old_y = m_yScrollPosition;
572 m_yScrollPosition = y_pos;
aafe4488 573 m_vAdjust->value = y_pos;
39c869a6 574
aafe4488 575 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
39c869a6 576
830ed6d9
RR
577 // Just update the scrollbar, don't send any wxWindows event
578 GtkVDisconnectEvent();
aafe4488 579 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
830ed6d9 580 GtkVConnectEvent();
30954328
RR
581 }
582}
583
9e691f46
VZ
584// TODO: [VH]Scroll functions should be combined
585
586void wxScrolledWindow::GtkVScroll( float value, unsigned int scroll_type )
30954328 587{
566d84a7 588 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
aafe4488
RR
589
590 if (m_yScrollPixelsPerLine == 0)
591 return;
39c869a6 592
aafe4488 593 int y_pos = (int)(value+0.5);
39c869a6 594
aafe4488
RR
595 if (y_pos == m_yScrollPosition)
596 return;
39c869a6 597
9e691f46 598 wxEventType command = GtkScrollWinTypeToWx(scroll_type);
d9a4f620 599
0bc0cd5d 600 wxScrollWinEvent event( command, y_pos, wxVERTICAL );
d9a4f620
RR
601 event.SetEventObject( this );
602 GetEventHandler()->ProcessEvent( event );
30954328
RR
603}
604
9e691f46 605void wxScrolledWindow::GtkHScroll( float value, unsigned int scroll_type )
30954328 606{
566d84a7 607 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
39c869a6 608
aafe4488
RR
609 if (m_xScrollPixelsPerLine == 0)
610 return;
39c869a6 611
aafe4488 612 int x_pos = (int)(value+0.5);
0bc0cd5d 613
aafe4488
RR
614 if (x_pos == m_xScrollPosition)
615 return;
aafe4488 616
9e691f46 617 wxEventType command = GtkScrollWinTypeToWx(scroll_type);
d9a4f620 618
0bc0cd5d 619 wxScrollWinEvent event( command, x_pos, wxHORIZONTAL );
d9a4f620
RR
620 event.SetEventObject( this );
621 GetEventHandler()->ProcessEvent( event );
30954328
RR
622}
623
624void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
625{
626 m_xScrollingEnabled = x_scroll;
627 m_yScrollingEnabled = y_scroll;
628}
629
30954328
RR
630// Where the current view starts from
631void wxScrolledWindow::GetViewStart (int *x, int *y) const
632{
633 if ( x )
634 *x = m_xScrollPosition;
635 if ( y )
636 *y = m_yScrollPosition;
637}
638
8c2f3797 639void wxScrolledWindow::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const
30954328
RR
640{
641 if ( xx )
642 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
643 if ( yy )
644 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
645}
646
8c2f3797 647void wxScrolledWindow::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
30954328
RR
648{
649 if ( xx )
650 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
651 if ( yy )
652 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
653}
654
d9a4f620
RR
655int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
656{
657 int pos = event.GetPosition();
658 int orient = event.GetOrientation();
659
660 int nScrollInc = 0;
661 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
662 {
663 if (orient == wxHORIZONTAL)
664 nScrollInc = - m_xScrollPosition;
665 else
666 nScrollInc = - m_yScrollPosition;
667 } else
668 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
669 {
670 if (orient == wxHORIZONTAL)
566d84a7 671 nScrollInc = GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine - m_xScrollPosition;
d9a4f620 672 else
566d84a7 673 nScrollInc = GetVirtualSize().GetHeight() / m_yScrollPixelsPerLine - m_yScrollPosition;
d9a4f620
RR
674 } else
675 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
676 {
677 nScrollInc = -1;
678 } else
679 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
680 {
681 nScrollInc = 1;
682 } else
683 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
684 {
685 if (orient == wxHORIZONTAL)
686 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
687 else
688 nScrollInc = -GetScrollPageSize(wxVERTICAL);
689 } else
690 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
691 {
692 if (orient == wxHORIZONTAL)
693 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
694 else
695 nScrollInc = GetScrollPageSize(wxVERTICAL);
696 } else
697 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
698 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
699 {
700 if (orient == wxHORIZONTAL)
701 nScrollInc = pos - m_xScrollPosition;
702 else
703 nScrollInc = pos - m_yScrollPosition;
704 }
705
706 if (orient == wxHORIZONTAL)
707 {
708 if (m_xScrollPixelsPerLine > 0)
709 {
830ed6d9
RR
710 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
711 if (max < 0) max = 0;
d9a4f620
RR
712
713 if ( (m_xScrollPosition + nScrollInc) < 0 )
714 nScrollInc = -m_xScrollPosition; // As -ve as we can go
830ed6d9
RR
715 else if ( (m_xScrollPosition + nScrollInc) > max )
716 nScrollInc = max - m_xScrollPosition; // As +ve as we can go
d9a4f620
RR
717 }
718 else
719 m_targetWindow->Refresh();
720 }
721 else
722 {
723 if (m_yScrollPixelsPerLine > 0)
724 {
830ed6d9
RR
725 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
726 if (max < 0) max = 0;
d9a4f620
RR
727
728 if ( (m_yScrollPosition + nScrollInc) < 0 )
729 nScrollInc = -m_yScrollPosition; // As -ve as we can go
830ed6d9
RR
730 else if ( (m_yScrollPosition + nScrollInc) > max )
731 nScrollInc = max - m_yScrollPosition; // As +ve as we can go
d9a4f620
RR
732 }
733 else
734 m_targetWindow->Refresh();
735 }
736
737 return nScrollInc;
738}
739
f47ae6e7 740void wxScrolledWindow::SetScrollPos( int orient, int pos, bool refresh )
0bc0cd5d
RR
741{
742 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
743
744 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
745
746 if (orient == wxHORIZONTAL)
747 {
830ed6d9
RR
748 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
749 if (max < 0) max = 0;
39c869a6 750
830ed6d9
RR
751 if (pos > max) pos = 0;
752 if (pos < 0) pos = 0;
39c869a6 753
830ed6d9
RR
754 if (pos == (int)(m_hAdjust->value+0.5)) return;
755 m_hAdjust->value = pos;
0bc0cd5d
RR
756 }
757 else
758 {
830ed6d9
RR
759 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
760 if (max < 0) max = 0;
39c869a6 761
830ed6d9
RR
762 if (pos > max) pos = 0;
763 if (pos < 0) pos = 0;
39c869a6 764
830ed6d9
RR
765 if (pos == (int)(m_vAdjust->value+0.5)) return;
766 m_vAdjust->value = pos;
0bc0cd5d
RR
767 }
768
769 if (m_wxwindow->window)
770 {
771 if (orient == wxHORIZONTAL)
772 {
830ed6d9
RR
773 // Just update the scrollbar, don't send any wxWindows event
774 GtkHDisconnectEvent();
0bc0cd5d 775 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
830ed6d9 776 GtkHConnectEvent();
0bc0cd5d
RR
777 }
778 else
779 {
830ed6d9
RR
780 // Just update the scrollbar, don't send any wxWindows event
781 GtkVDisconnectEvent();
0bc0cd5d 782 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
830ed6d9 783 GtkVConnectEvent();
0bc0cd5d
RR
784 }
785 }
786}
787
830ed6d9
RR
788void wxScrolledWindow::GtkVConnectEvent()
789{
790 gtk_signal_connect( GTK_OBJECT(m_vAdjust), "value_changed",
791 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
792}
793
794void wxScrolledWindow::GtkHConnectEvent()
795{
796 gtk_signal_connect( GTK_OBJECT(m_hAdjust), "value_changed",
797 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
798}
799
800void wxScrolledWindow::GtkHDisconnectEvent()
801{
802 gtk_signal_disconnect_by_func( GTK_OBJECT(m_hAdjust),
803 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
804}
805
806void wxScrolledWindow::GtkVDisconnectEvent()
807{
808 gtk_signal_disconnect_by_func( GTK_OBJECT(m_vAdjust),
809 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
810}
811
30486297
RD
812bool wxScrolledWindow::Layout()
813{
566d84a7 814 if (GetSizer() && m_targetWindow == this)
30486297 815 {
566d84a7
RL
816 // If we're the scroll target, take into account the
817 // virtual size and scrolled position of the window.
818
30486297
RD
819 int x, y, w, h;
820 CalcScrolledPosition(0,0, &x,&y);
821 GetVirtualSize(&w, &h);
822 GetSizer()->SetDimension(x, y, w, h);
823 return TRUE;
824 }
825 else
826 return wxPanel::Layout(); // fall back to default for LayoutConstraints
827}
828
30954328
RR
829// ----------------------------------------------------------------------------
830// event handlers
831// ----------------------------------------------------------------------------
832
833// Default OnSize resets scrollbars, if any
834void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
835{
150c8d89 836 if( GetAutoLayout() || m_targetWindow->GetAutoLayout() )
2b5f62a0
VZ
837 {
838 if( m_targetWindow != this )
839 m_targetWindow->FitInside();
566d84a7 840
2b5f62a0 841 FitInside();
150c8d89
RL
842
843 // FIXME: Something is really weird here... This should be
844 // called by FitInside above (and apparently is), yet the
845 // scrollsub sample will get the scrollbar wrong if resized
846 // quickly. This masks the bug, but is surely not the right
847 // answer at all.
848 AdjustScrollbars();
2b5f62a0
VZ
849 }
850 else
851 {
852 AdjustScrollbars();
853 }
30954328
RR
854}
855
856// This calls OnDraw, having adjusted the origin according to the current
857// scroll position
858void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
859{
860 wxPaintDC dc(this);
861 PrepareDC(dc);
862
863 OnDraw(dc);
864}
865
866// kbd handling: notice that we use OnChar() and not OnKeyDown() for
867// compatibility here - if we used OnKeyDown(), the programs which process
868// arrows themselves in their OnChar() would never get the message and like
869// this they always have the priority
870void wxScrolledWindow::OnChar(wxKeyEvent& event)
871{
872 int stx, sty, // view origin
873 szx, szy, // view size (total)
874 clix, cliy; // view size (on screen)
875
2b5f62a0 876 GetViewStart(&stx, &sty);
30954328
RR
877 GetClientSize(&clix, &cliy);
878 GetVirtualSize(&szx, &szy);
879
880 if( m_xScrollPixelsPerLine )
881 {
882 clix /= m_xScrollPixelsPerLine;
883 szx /= m_xScrollPixelsPerLine;
884 }
885 else
886 {
887 clix = 0;
888 szx = -1;
889 }
890 if( m_yScrollPixelsPerLine )
891 {
892 cliy /= m_yScrollPixelsPerLine;
893 szy /= m_yScrollPixelsPerLine;
894 }
895 else
896 {
897 cliy = 0;
898 szy = -1;
899 }
900
39c869a6
VZ
901 int xScrollOld = GetScrollPos(wxHORIZONTAL),
902 yScrollOld = GetScrollPos(wxVERTICAL);
903
30954328 904 int dsty;
12a3f227 905 switch ( event.GetKeyCode() )
30954328
RR
906 {
907 case WXK_PAGEUP:
908 case WXK_PRIOR:
909 dsty = sty - (5 * cliy / 6);
910 Scroll(-1, (dsty == -1) ? 0 : dsty);
911 break;
912
913 case WXK_PAGEDOWN:
914 case WXK_NEXT:
915 Scroll(-1, sty + (5 * cliy / 6));
916 break;
917
918 case WXK_HOME:
919 Scroll(0, event.ControlDown() ? 0 : -1);
920 break;
921
922 case WXK_END:
923 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
924 break;
925
926 case WXK_UP:
927 Scroll(-1, sty - 1);
928 break;
929
930 case WXK_DOWN:
931 Scroll(-1, sty + 1);
932 break;
933
934 case WXK_LEFT:
935 Scroll(stx - 1, -1);
936 break;
937
938 case WXK_RIGHT:
939 Scroll(stx + 1, -1);
940 break;
941
942 default:
943 // not for us
944 event.Skip();
39c869a6
VZ
945 return;
946 }
947
948 int xScroll = GetScrollPos(wxHORIZONTAL);
949 if ( xScroll != xScrollOld )
950 {
951 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, xScroll,
952 wxHORIZONTAL);
953 event.SetEventObject(this);
954 GetEventHandler()->ProcessEvent(event);
955 }
956
957 int yScroll = GetScrollPos(wxVERTICAL);
958 if ( yScroll != yScrollOld )
959 {
960 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, yScroll,
961 wxVERTICAL);
962 event.SetEventObject(this);
963 GetEventHandler()->ProcessEvent(event);
30954328
RR
964 }
965}
3379ed37 966
566d84a7
RL
967
968// vi:sts=4:sw=4:et