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