]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/scrolwin.cpp
Honour wxSB_WRAP in wxMotif spin button
[wxWidgets.git] / src / gtk1 / 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 );
316
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
566d84a7 352 m_targetWindow->SetVirtualSizeHints( noUnitsX * pixelsPerUnitX, noUnitsY * pixelsPerUnitY );
30486297 353
ec7482df
RR
354 if (!noRefresh)
355 {
356 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
357 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
30486297 358
566d84a7 359 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
ec7482df 360 }
30954328
RR
361}
362
363void wxScrolledWindow::AdjustScrollbars()
364{
365 int w, h;
566d84a7
RL
366 int vw, vh;
367
30954328 368 m_targetWindow->GetClientSize( &w, &h );
566d84a7 369 m_targetWindow->GetVirtualSize( &vw, &vh );
39c869a6 370
30954328 371 if (m_xScrollPixelsPerLine == 0)
4ca24f18
RR
372 {
373 m_hAdjust->upper = 1.0;
30954328 374 m_hAdjust->page_size = 1.0;
4ca24f18 375 }
30954328 376 else
4ca24f18 377 {
566d84a7 378 m_hAdjust->upper = vw / m_xScrollPixelsPerLine;
30954328 379 m_hAdjust->page_size = (w / m_xScrollPixelsPerLine);
39c869a6 380
566d84a7
RL
381 // If the scrollbar hits the right side, move the window
382 // right to keep it from over extending.
4ca24f18 383
566d84a7
RL
384 if( m_hAdjust->value + m_hAdjust->page_size > m_hAdjust->upper )
385 {
b03f81b7
RR
386 if (GetChildren().GetCount() == 0)
387 {
388 m_hAdjust->value = m_hAdjust->upper - m_hAdjust->page_size;
389 m_xScrollPosition = (int)m_hAdjust->value;
390 }
566d84a7 391 }
4ca24f18
RR
392 }
393
30954328 394 if (m_yScrollPixelsPerLine == 0)
4ca24f18
RR
395 {
396 m_vAdjust->upper = 1.0;
30954328 397 m_vAdjust->page_size = 1.0;
4ca24f18 398 }
30954328 399 else
4ca24f18 400 {
566d84a7 401 m_vAdjust->upper = vh / m_yScrollPixelsPerLine;
30954328 402 m_vAdjust->page_size = (h / m_yScrollPixelsPerLine);
39c869a6 403
566d84a7
RL
404 if( m_vAdjust->value + m_vAdjust->page_size > m_vAdjust->upper )
405 {
b03f81b7
RR
406 if (GetChildren().GetCount() == 0)
407 {
408 m_vAdjust->value = m_vAdjust->upper - m_vAdjust->page_size;
409 m_yScrollPosition = (int)m_vAdjust->value;
410 }
566d84a7 411 }
4ca24f18
RR
412 }
413
0bc0cd5d
RR
414 m_xScrollLinesPerPage = (int)(m_hAdjust->page_size + 0.5);
415 m_yScrollLinesPerPage = (int)(m_vAdjust->page_size + 0.5);
39c869a6 416
30954328
RR
417 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
418 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
419}
420
566d84a7 421
30954328
RR
422// ----------------------------------------------------------------------------
423// target window handling
424// ----------------------------------------------------------------------------
425
13ff9344 426void wxScrolledWindow::SetTargetWindow( wxWindow *target, bool WXUNUSED(pushEventHandler) )
30954328
RR
427{
428 wxASSERT_MSG( target, wxT("target window must not be NULL") );
429 m_targetWindow = target;
430}
431
432wxWindow *wxScrolledWindow::GetTargetWindow()
433{
434 return m_targetWindow;
435}
436
437// Override this function if you don't want to have wxScrolledWindow
438// automatically change the origin according to the scroll position.
439void wxScrolledWindow::PrepareDC(wxDC& dc)
440{
441 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
442 -m_yScrollPosition * m_yScrollPixelsPerLine );
443}
444
566d84a7
RL
445void wxScrolledWindow::SetScrollRate( int xstep, int ystep )
446{
447 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
448 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
449
450 m_xScrollPixelsPerLine = xstep;
451 m_yScrollPixelsPerLine = ystep;
452
453 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
454 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
455
456 m_targetWindow->ScrollWindow( old_x - new_x, old_y - new_y );
457
458 AdjustScrollbars();
459}
460
30954328
RR
461void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
462{
463 if ( x_unit )
464 *x_unit = m_xScrollPixelsPerLine;
465 if ( y_unit )
466 *y_unit = m_yScrollPixelsPerLine;
467}
468
469int wxScrolledWindow::GetScrollPageSize(int orient) const
470{
471 if ( orient == wxHORIZONTAL )
472 return m_xScrollLinesPerPage;
473 else
474 return m_yScrollLinesPerPage;
475}
476
477void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
478{
479 if ( orient == wxHORIZONTAL )
480 m_xScrollLinesPerPage = pageSize;
481 else
482 m_yScrollLinesPerPage = pageSize;
483}
484
d9a4f620
RR
485void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
486{
487 int orient = event.GetOrientation();
0bc0cd5d 488
d9a4f620
RR
489 int nScrollInc = CalcScrollInc(event);
490 if (nScrollInc == 0) return;
491
492 if (orient == wxHORIZONTAL)
493 {
494 int newPos = m_xScrollPosition + nScrollInc;
495 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
496 }
497 else
498 {
499 int newPos = m_yScrollPosition + nScrollInc;
500 SetScrollPos(wxVERTICAL, newPos, TRUE );
501 }
502
503 if (orient == wxHORIZONTAL)
504 {
505 m_xScrollPosition += nScrollInc;
506 }
507 else
508 {
509 m_yScrollPosition += nScrollInc;
510 }
511
512 if (orient == wxHORIZONTAL)
513 {
514 if (m_xScrollingEnabled)
515 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
516 else
517 m_targetWindow->Refresh();
518 }
519 else
520 {
521 if (m_yScrollingEnabled)
522 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
523 else
524 m_targetWindow->Refresh();
525 }
526}
527
30954328
RR
528void wxScrolledWindow::Scroll( int x_pos, int y_pos )
529{
566d84a7 530 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
30954328
RR
531
532 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
533 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
534
30954328
RR
535 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
536 {
830ed6d9
RR
537 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
538 if (max < 0) max = 0;
539 if (x_pos > max) x_pos = max;
540 if (x_pos < 0) x_pos = 0;
39c869a6 541
30954328
RR
542 int old_x = m_xScrollPosition;
543 m_xScrollPosition = x_pos;
aafe4488 544 m_hAdjust->value = x_pos;
39c869a6 545
aafe4488 546 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
39c869a6 547
830ed6d9
RR
548 // Just update the scrollbar, don't send any wxWindows event
549 GtkHDisconnectEvent();
aafe4488 550 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
830ed6d9 551 GtkHConnectEvent();
30954328 552 }
39c869a6 553
30954328
RR
554 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
555 {
830ed6d9
RR
556 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
557 if (max < 0) max = 0;
558 if (y_pos > max) y_pos = max;
559 if (y_pos < 0) y_pos = 0;
39c869a6 560
30954328
RR
561 int old_y = m_yScrollPosition;
562 m_yScrollPosition = y_pos;
aafe4488 563 m_vAdjust->value = y_pos;
39c869a6 564
aafe4488 565 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
39c869a6 566
830ed6d9
RR
567 // Just update the scrollbar, don't send any wxWindows event
568 GtkVDisconnectEvent();
aafe4488 569 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
830ed6d9 570 GtkVConnectEvent();
30954328
RR
571 }
572}
573
9e691f46
VZ
574// TODO: [VH]Scroll functions should be combined
575
576void wxScrolledWindow::GtkVScroll( float value, unsigned int scroll_type )
30954328 577{
566d84a7 578 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
aafe4488
RR
579
580 if (m_yScrollPixelsPerLine == 0)
581 return;
39c869a6 582
aafe4488 583 int y_pos = (int)(value+0.5);
39c869a6 584
aafe4488
RR
585 if (y_pos == m_yScrollPosition)
586 return;
39c869a6 587
9e691f46 588 wxEventType command = GtkScrollWinTypeToWx(scroll_type);
d9a4f620 589
0bc0cd5d 590 wxScrollWinEvent event( command, y_pos, wxVERTICAL );
d9a4f620
RR
591 event.SetEventObject( this );
592 GetEventHandler()->ProcessEvent( event );
30954328
RR
593}
594
9e691f46 595void wxScrolledWindow::GtkHScroll( float value, unsigned int scroll_type )
30954328 596{
566d84a7 597 wxASSERT_MSG( m_targetWindow != 0, _T("No target window") );
39c869a6 598
aafe4488
RR
599 if (m_xScrollPixelsPerLine == 0)
600 return;
39c869a6 601
aafe4488 602 int x_pos = (int)(value+0.5);
0bc0cd5d 603
aafe4488
RR
604 if (x_pos == m_xScrollPosition)
605 return;
aafe4488 606
9e691f46 607 wxEventType command = GtkScrollWinTypeToWx(scroll_type);
d9a4f620 608
0bc0cd5d 609 wxScrollWinEvent event( command, x_pos, wxHORIZONTAL );
d9a4f620
RR
610 event.SetEventObject( this );
611 GetEventHandler()->ProcessEvent( event );
30954328
RR
612}
613
614void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
615{
616 m_xScrollingEnabled = x_scroll;
617 m_yScrollingEnabled = y_scroll;
618}
619
30954328
RR
620// Where the current view starts from
621void wxScrolledWindow::GetViewStart (int *x, int *y) const
622{
623 if ( x )
624 *x = m_xScrollPosition;
625 if ( y )
626 *y = m_yScrollPosition;
627}
628
8c2f3797 629void wxScrolledWindow::DoCalcScrolledPosition(int x, int y, int *xx, int *yy) const
30954328
RR
630{
631 if ( xx )
632 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
633 if ( yy )
634 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
635}
636
8c2f3797 637void wxScrolledWindow::DoCalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
30954328
RR
638{
639 if ( xx )
640 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
641 if ( yy )
642 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
643}
644
d9a4f620
RR
645int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
646{
647 int pos = event.GetPosition();
648 int orient = event.GetOrientation();
649
650 int nScrollInc = 0;
651 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
652 {
653 if (orient == wxHORIZONTAL)
654 nScrollInc = - m_xScrollPosition;
655 else
656 nScrollInc = - m_yScrollPosition;
657 } else
658 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
659 {
660 if (orient == wxHORIZONTAL)
566d84a7 661 nScrollInc = GetVirtualSize().GetWidth() / m_xScrollPixelsPerLine - m_xScrollPosition;
d9a4f620 662 else
566d84a7 663 nScrollInc = GetVirtualSize().GetHeight() / m_yScrollPixelsPerLine - m_yScrollPosition;
d9a4f620
RR
664 } else
665 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
666 {
667 nScrollInc = -1;
668 } else
669 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
670 {
671 nScrollInc = 1;
672 } else
673 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
674 {
675 if (orient == wxHORIZONTAL)
676 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
677 else
678 nScrollInc = -GetScrollPageSize(wxVERTICAL);
679 } else
680 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
681 {
682 if (orient == wxHORIZONTAL)
683 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
684 else
685 nScrollInc = GetScrollPageSize(wxVERTICAL);
686 } else
687 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
688 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
689 {
690 if (orient == wxHORIZONTAL)
691 nScrollInc = pos - m_xScrollPosition;
692 else
693 nScrollInc = pos - m_yScrollPosition;
694 }
695
696 if (orient == wxHORIZONTAL)
697 {
698 if (m_xScrollPixelsPerLine > 0)
699 {
830ed6d9
RR
700 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
701 if (max < 0) max = 0;
d9a4f620
RR
702
703 if ( (m_xScrollPosition + nScrollInc) < 0 )
704 nScrollInc = -m_xScrollPosition; // As -ve as we can go
830ed6d9
RR
705 else if ( (m_xScrollPosition + nScrollInc) > max )
706 nScrollInc = max - m_xScrollPosition; // As +ve as we can go
d9a4f620
RR
707 }
708 else
709 m_targetWindow->Refresh();
710 }
711 else
712 {
713 if (m_yScrollPixelsPerLine > 0)
714 {
830ed6d9
RR
715 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
716 if (max < 0) max = 0;
d9a4f620
RR
717
718 if ( (m_yScrollPosition + nScrollInc) < 0 )
719 nScrollInc = -m_yScrollPosition; // As -ve as we can go
830ed6d9
RR
720 else if ( (m_yScrollPosition + nScrollInc) > max )
721 nScrollInc = max - m_yScrollPosition; // As +ve as we can go
d9a4f620
RR
722 }
723 else
724 m_targetWindow->Refresh();
725 }
726
727 return nScrollInc;
728}
729
f47ae6e7 730void wxScrolledWindow::SetScrollPos( int orient, int pos, bool refresh )
0bc0cd5d
RR
731{
732 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
733
734 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
735
736 if (orient == wxHORIZONTAL)
737 {
830ed6d9
RR
738 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
739 if (max < 0) max = 0;
39c869a6 740
830ed6d9
RR
741 if (pos > max) pos = 0;
742 if (pos < 0) pos = 0;
39c869a6 743
830ed6d9
RR
744 if (pos == (int)(m_hAdjust->value+0.5)) return;
745 m_hAdjust->value = pos;
0bc0cd5d
RR
746 }
747 else
748 {
830ed6d9
RR
749 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
750 if (max < 0) max = 0;
39c869a6 751
830ed6d9
RR
752 if (pos > max) pos = 0;
753 if (pos < 0) pos = 0;
39c869a6 754
830ed6d9
RR
755 if (pos == (int)(m_vAdjust->value+0.5)) return;
756 m_vAdjust->value = pos;
0bc0cd5d
RR
757 }
758
759 if (m_wxwindow->window)
760 {
761 if (orient == wxHORIZONTAL)
762 {
830ed6d9
RR
763 // Just update the scrollbar, don't send any wxWindows event
764 GtkHDisconnectEvent();
0bc0cd5d 765 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
830ed6d9 766 GtkHConnectEvent();
0bc0cd5d
RR
767 }
768 else
769 {
830ed6d9
RR
770 // Just update the scrollbar, don't send any wxWindows event
771 GtkVDisconnectEvent();
0bc0cd5d 772 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
830ed6d9 773 GtkVConnectEvent();
0bc0cd5d
RR
774 }
775 }
776}
777
830ed6d9
RR
778void wxScrolledWindow::GtkVConnectEvent()
779{
780 gtk_signal_connect( GTK_OBJECT(m_vAdjust), "value_changed",
781 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
782}
783
784void wxScrolledWindow::GtkHConnectEvent()
785{
786 gtk_signal_connect( GTK_OBJECT(m_hAdjust), "value_changed",
787 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
788}
789
790void wxScrolledWindow::GtkHDisconnectEvent()
791{
792 gtk_signal_disconnect_by_func( GTK_OBJECT(m_hAdjust),
793 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
794}
795
796void wxScrolledWindow::GtkVDisconnectEvent()
797{
798 gtk_signal_disconnect_by_func( GTK_OBJECT(m_vAdjust),
799 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
800}
801
30486297
RD
802bool wxScrolledWindow::Layout()
803{
566d84a7 804 if (GetSizer() && m_targetWindow == this)
30486297 805 {
566d84a7
RL
806 // If we're the scroll target, take into account the
807 // virtual size and scrolled position of the window.
808
30486297
RD
809 int x, y, w, h;
810 CalcScrolledPosition(0,0, &x,&y);
811 GetVirtualSize(&w, &h);
812 GetSizer()->SetDimension(x, y, w, h);
813 return TRUE;
814 }
815 else
816 return wxPanel::Layout(); // fall back to default for LayoutConstraints
817}
818
30954328
RR
819// ----------------------------------------------------------------------------
820// event handlers
821// ----------------------------------------------------------------------------
822
823// Default OnSize resets scrollbars, if any
824void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
825{
566d84a7
RL
826 if( m_targetWindow != this )
827 m_targetWindow->SetVirtualSize( m_targetWindow->GetClientSize() );
828
829 SetVirtualSize( GetClientSize() );
30954328
RR
830}
831
832// This calls OnDraw, having adjusted the origin according to the current
833// scroll position
834void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
835{
836 wxPaintDC dc(this);
837 PrepareDC(dc);
838
839 OnDraw(dc);
840}
841
842// kbd handling: notice that we use OnChar() and not OnKeyDown() for
843// compatibility here - if we used OnKeyDown(), the programs which process
844// arrows themselves in their OnChar() would never get the message and like
845// this they always have the priority
846void wxScrolledWindow::OnChar(wxKeyEvent& event)
847{
848 int stx, sty, // view origin
849 szx, szy, // view size (total)
850 clix, cliy; // view size (on screen)
851
852 ViewStart(&stx, &sty);
853 GetClientSize(&clix, &cliy);
854 GetVirtualSize(&szx, &szy);
855
856 if( m_xScrollPixelsPerLine )
857 {
858 clix /= m_xScrollPixelsPerLine;
859 szx /= m_xScrollPixelsPerLine;
860 }
861 else
862 {
863 clix = 0;
864 szx = -1;
865 }
866 if( m_yScrollPixelsPerLine )
867 {
868 cliy /= m_yScrollPixelsPerLine;
869 szy /= m_yScrollPixelsPerLine;
870 }
871 else
872 {
873 cliy = 0;
874 szy = -1;
875 }
876
39c869a6
VZ
877 int xScrollOld = GetScrollPos(wxHORIZONTAL),
878 yScrollOld = GetScrollPos(wxVERTICAL);
879
30954328
RR
880 int dsty;
881 switch ( event.KeyCode() )
882 {
883 case WXK_PAGEUP:
884 case WXK_PRIOR:
885 dsty = sty - (5 * cliy / 6);
886 Scroll(-1, (dsty == -1) ? 0 : dsty);
887 break;
888
889 case WXK_PAGEDOWN:
890 case WXK_NEXT:
891 Scroll(-1, sty + (5 * cliy / 6));
892 break;
893
894 case WXK_HOME:
895 Scroll(0, event.ControlDown() ? 0 : -1);
896 break;
897
898 case WXK_END:
899 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
900 break;
901
902 case WXK_UP:
903 Scroll(-1, sty - 1);
904 break;
905
906 case WXK_DOWN:
907 Scroll(-1, sty + 1);
908 break;
909
910 case WXK_LEFT:
911 Scroll(stx - 1, -1);
912 break;
913
914 case WXK_RIGHT:
915 Scroll(stx + 1, -1);
916 break;
917
918 default:
919 // not for us
920 event.Skip();
39c869a6
VZ
921 return;
922 }
923
924 int xScroll = GetScrollPos(wxHORIZONTAL);
925 if ( xScroll != xScrollOld )
926 {
927 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, xScroll,
928 wxHORIZONTAL);
929 event.SetEventObject(this);
930 GetEventHandler()->ProcessEvent(event);
931 }
932
933 int yScroll = GetScrollPos(wxVERTICAL);
934 if ( yScroll != yScrollOld )
935 {
936 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, yScroll,
937 wxVERTICAL);
938 event.SetEventObject(this);
939 GetEventHandler()->ProcessEvent(event);
30954328
RR
940 }
941}
3379ed37 942
566d84a7
RL
943
944// vi:sts=4:sw=4:et