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