]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/scrolwin.cpp
changes to match listctrl.h changes (forgot to commit yesterday, sorry)
[wxWidgets.git] / src / gtk / scrolwin.cpp
CommitLineData
30954328 1/////////////////////////////////////////////////////////////////////////////
e1437456 2// Name: gtk/scrolwin.cpp
30954328
RR
3// Purpose: wxScrolledWindow implementation
4// Author: Julian Smart
5// Modified by:
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
34#include "wx/gtk/scrolwin.h"
35#include "wx/panel.h"
36
37#include <gtk/gtk.h>
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
75static void gtk_scrolled_window_vscroll_callback( GtkAdjustment *adjust, wxScrolledWindow *win )
76{
77 if (g_isIdle)
78 wxapp_install_idle_handler();
79
80 if (g_blockEventsOnDrag) return;
81
82 if (!win->m_hasVMT) return;
83
84 win->GtkVScroll( adjust->value );
85}
86
87//-----------------------------------------------------------------------------
88// "value_changed" from m_hAdjust
89//-----------------------------------------------------------------------------
90
91static void gtk_scrolled_window_hscroll_callback( GtkAdjustment *adjust, wxScrolledWindow *win )
92{
93 if (g_isIdle)
94 wxapp_install_idle_handler();
95
96 if (g_blockEventsOnDrag) return;
97 if (!win->m_hasVMT) return;
98
99 win->GtkHScroll( adjust->value );
100}
101
cf19b0bd
RR
102//-----------------------------------------------------------------------------
103// "button_press_event" from scrollbar
104//-----------------------------------------------------------------------------
105
106static gint gtk_scrollbar_button_press_callback( GtkRange *widget,
107 GdkEventButton *gdk_event,
108 wxWindowGTK *win)
109{
110 if (g_isIdle)
111 wxapp_install_idle_handler();
112
113 g_blockEventsOnScroll = TRUE;
114 win->m_isScrolling = (gdk_event->window == widget->slider);
115
116 return FALSE;
117}
118
119//-----------------------------------------------------------------------------
120// "button_release_event" from scrollbar
121//-----------------------------------------------------------------------------
122
123static gint gtk_scrollbar_button_release_callback( GtkRange *widget,
124 GdkEventButton *WXUNUSED(gdk_event),
125 wxWindowGTK *win)
126{
127// don't test here as we can release the mouse while being over
128// a different window than the slider
129//
130// if (gdk_event->window != widget->slider) return FALSE;
131
132 g_blockEventsOnScroll = FALSE;
133
134 if (win->m_isScrolling)
135 {
136 wxEventType command = wxEVT_SCROLLWIN_THUMBRELEASE;
137 int value = -1;
138 int dir = -1;
139
140 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(win->m_widget);
141 if (widget == GTK_RANGE(scrolledWindow->hscrollbar))
142 {
143 value = (int)(win->m_hAdjust->value+0.5);
144 dir = wxHORIZONTAL;
145 }
146 if (widget == GTK_RANGE(scrolledWindow->vscrollbar))
147 {
148 value = (int)(win->m_vAdjust->value+0.5);
149 dir = wxVERTICAL;
150 }
151
152 wxScrollWinEvent event( command, value, dir );
153 event.SetEventObject( win );
154 win->GetEventHandler()->ProcessEvent( event );
155 }
156
157 win->m_isScrolling = FALSE;
158
159 return FALSE;
160}
161
30954328
RR
162//-----------------------------------------------------------------------------
163// InsertChild for wxScrolledWindow
164//-----------------------------------------------------------------------------
165
166static void wxInsertChildInScrolledWindow( wxWindow* parent, wxWindow* child )
167{
830ed6d9
RR
168 // The window might have been scrolled already, do we
169 // have to adapt the position.
30954328
RR
170 GtkPizza *pizza = GTK_PIZZA(parent->m_wxwindow);
171 child->m_x += pizza->xoffset;
172 child->m_y += pizza->yoffset;
173
174 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
175 GTK_WIDGET(child->m_widget),
176 child->m_x,
177 child->m_y,
178 child->m_width,
179 child->m_height );
180}
181
182// ----------------------------------------------------------------------------
183// wxScrolledWindow creation
184// ----------------------------------------------------------------------------
185
830ed6d9 186void wxScrolledWindow::Init()
30954328
RR
187{
188 m_xScrollPixelsPerLine = 0;
189 m_yScrollPixelsPerLine = 0;
190 m_xScrollingEnabled = TRUE;
191 m_yScrollingEnabled = TRUE;
192 m_xScrollPosition = 0;
193 m_yScrollPosition = 0;
194 m_xScrollLines = 0;
195 m_yScrollLines = 0;
196 m_xScrollLinesPerPage = 0;
197 m_yScrollLinesPerPage = 0;
198 m_targetWindow = (wxWindow*) NULL;
d9a4f620
RR
199 m_scaleX = 1.0;
200 m_scaleY = 1.0;
e1437456 201 m_hasScrolling = TRUE;
30954328
RR
202}
203
204bool wxScrolledWindow::Create(wxWindow *parent,
205 wxWindowID id,
206 const wxPoint& pos,
207 const wxSize& size,
208 long style,
209 const wxString& name)
210{
830ed6d9
RR
211 Init();
212
30954328
RR
213 if (!PreCreation( parent, pos, size ) ||
214 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
215 {
216 wxFAIL_MSG( wxT("wxWindow creation failed") );
217 return FALSE;
218 }
219
220 m_insertCallback = wxInsertChildInScrolledWindow;
39c869a6 221
30954328
RR
222 m_targetWindow = this;
223
224 m_widget = gtk_scrolled_window_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
225 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
226
227 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(m_widget);
228
229 GtkScrolledWindowClass *scroll_class = GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget) );
230 scroll_class->scrollbar_spacing = 0;
231
232 gtk_scrolled_window_set_policy( scrolledWindow, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
233
234 m_hAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->hscrollbar) );
235 m_vAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->vscrollbar) );
236
237 m_wxwindow = gtk_pizza_new();
238
239 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
240
241 GtkPizza *pizza = GTK_PIZZA(m_wxwindow);
242
243 if (HasFlag(wxRAISED_BORDER))
244 {
245 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_OUT );
246 }
247 else if (HasFlag(wxSUNKEN_BORDER))
248 {
249 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_IN );
250 }
251 else if (HasFlag(wxSIMPLE_BORDER))
252 {
253 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_THIN );
254 }
255 else
256 {
257 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_NONE );
258 }
259
260 GTK_WIDGET_SET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
261 m_acceptsFocus = TRUE;
262
263 // I _really_ don't want scrollbars in the beginning
264 m_vAdjust->lower = 0.0;
265 m_vAdjust->upper = 1.0;
266 m_vAdjust->value = 0.0;
267 m_vAdjust->step_increment = 1.0;
268 m_vAdjust->page_increment = 1.0;
269 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
270 m_hAdjust->lower = 0.0;
271 m_hAdjust->upper = 1.0;
272 m_hAdjust->value = 0.0;
273 m_hAdjust->step_increment = 1.0;
274 m_hAdjust->page_increment = 1.0;
275 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
276
830ed6d9
RR
277 // Handlers for new scrollbar values
278 GtkVConnectEvent();
279 GtkHConnectEvent();
30954328 280
cf19b0bd
RR
281 // these handlers block mouse events to any window during scrolling such as
282 // motion events and prevent GTK and wxWindows from fighting over where the
283 // slider should be
284
285 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_press_event",
286 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
287
288 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_press_event",
289 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
290
291 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_release_event",
292 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
293
294 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_release_event",
295 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
296
30954328
RR
297 gtk_widget_show( m_wxwindow );
298
299 if (m_parent)
300 m_parent->DoAddChild( this );
c9c4b3a0
RR
301
302 m_focusWidget = m_wxwindow;
30954328
RR
303
304 PostCreation();
305
306 Show( TRUE );
307
308 return TRUE;
309}
310
30954328
RR
311// ----------------------------------------------------------------------------
312// setting scrolling parameters
313// ----------------------------------------------------------------------------
314
315/*
316 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
317 * noUnitsX/noUnitsY: : no. units per scrollbar
318 */
319void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX, int pixelsPerUnitY,
320 int noUnitsX, int noUnitsY,
321 int xPos, int yPos, bool noRefresh )
322{
ec7482df
RR
323 int old_x = m_xScrollPixelsPerLine * m_xScrollPosition;
324 int old_y = m_yScrollPixelsPerLine * m_yScrollPosition;
325
30954328
RR
326 m_xScrollPixelsPerLine = pixelsPerUnitX;
327 m_yScrollPixelsPerLine = pixelsPerUnitY;
30954328
RR
328 m_xScrollLines = noUnitsX;
329 m_yScrollLines = noUnitsY;
ec7482df
RR
330 m_xScrollPosition = xPos;
331 m_yScrollPosition = yPos;
39c869a6 332
30954328
RR
333 m_hAdjust->lower = 0.0;
334 m_hAdjust->upper = noUnitsX;
335 m_hAdjust->value = xPos;
336 m_hAdjust->step_increment = 1.0;
0bc0cd5d 337 m_hAdjust->page_increment = 2.0;
30954328
RR
338
339 m_vAdjust->lower = 0.0;
340 m_vAdjust->upper = noUnitsY;
341 m_vAdjust->value = yPos;
342 m_vAdjust->step_increment = 1.0;
0bc0cd5d 343 m_vAdjust->page_increment = 2.0;
ec7482df 344
30954328 345 AdjustScrollbars();
ec7482df
RR
346
347 if (!noRefresh)
348 {
349 int new_x = m_xScrollPixelsPerLine * m_xScrollPosition;
350 int new_y = m_yScrollPixelsPerLine * m_yScrollPosition;
351
352 m_targetWindow->ScrollWindow( old_x-new_x, old_y-new_y );
353 }
30954328
RR
354}
355
356void wxScrolledWindow::AdjustScrollbars()
357{
358 int w, h;
359 m_targetWindow->GetClientSize( &w, &h );
39c869a6 360
30954328
RR
361 if (m_xScrollPixelsPerLine == 0)
362 m_hAdjust->page_size = 1.0;
363 else
364 m_hAdjust->page_size = (w / m_xScrollPixelsPerLine);
39c869a6 365
30954328
RR
366 if (m_yScrollPixelsPerLine == 0)
367 m_vAdjust->page_size = 1.0;
368 else
369 m_vAdjust->page_size = (h / m_yScrollPixelsPerLine);
39c869a6 370
0bc0cd5d
RR
371 m_xScrollLinesPerPage = (int)(m_hAdjust->page_size + 0.5);
372 m_yScrollLinesPerPage = (int)(m_vAdjust->page_size + 0.5);
39c869a6 373
30954328
RR
374 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
375 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
376}
377
378// ----------------------------------------------------------------------------
379// target window handling
380// ----------------------------------------------------------------------------
381
382void wxScrolledWindow::SetTargetWindow( wxWindow *target )
383{
384 wxASSERT_MSG( target, wxT("target window must not be NULL") );
385 m_targetWindow = target;
386}
387
388wxWindow *wxScrolledWindow::GetTargetWindow()
389{
390 return m_targetWindow;
391}
392
393// Override this function if you don't want to have wxScrolledWindow
394// automatically change the origin according to the scroll position.
395void wxScrolledWindow::PrepareDC(wxDC& dc)
396{
397 dc.SetDeviceOrigin( -m_xScrollPosition * m_xScrollPixelsPerLine,
398 -m_yScrollPosition * m_yScrollPixelsPerLine );
399}
400
401void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit, int *y_unit) const
402{
403 if ( x_unit )
404 *x_unit = m_xScrollPixelsPerLine;
405 if ( y_unit )
406 *y_unit = m_yScrollPixelsPerLine;
407}
408
409int wxScrolledWindow::GetScrollPageSize(int orient) const
410{
411 if ( orient == wxHORIZONTAL )
412 return m_xScrollLinesPerPage;
413 else
414 return m_yScrollLinesPerPage;
415}
416
417void wxScrolledWindow::SetScrollPageSize(int orient, int pageSize)
418{
419 if ( orient == wxHORIZONTAL )
420 m_xScrollLinesPerPage = pageSize;
421 else
422 m_yScrollLinesPerPage = pageSize;
423}
424
d9a4f620
RR
425void wxScrolledWindow::OnScroll(wxScrollWinEvent& event)
426{
427 int orient = event.GetOrientation();
0bc0cd5d 428
d9a4f620
RR
429 int nScrollInc = CalcScrollInc(event);
430 if (nScrollInc == 0) return;
431
432 if (orient == wxHORIZONTAL)
433 {
434 int newPos = m_xScrollPosition + nScrollInc;
435 SetScrollPos(wxHORIZONTAL, newPos, TRUE );
436 }
437 else
438 {
439 int newPos = m_yScrollPosition + nScrollInc;
440 SetScrollPos(wxVERTICAL, newPos, TRUE );
441 }
442
443 if (orient == wxHORIZONTAL)
444 {
445 m_xScrollPosition += nScrollInc;
446 }
447 else
448 {
449 m_yScrollPosition += nScrollInc;
450 }
451
452 if (orient == wxHORIZONTAL)
453 {
454 if (m_xScrollingEnabled)
455 m_targetWindow->ScrollWindow(-m_xScrollPixelsPerLine * nScrollInc, 0, (const wxRect *) NULL);
456 else
457 m_targetWindow->Refresh();
458 }
459 else
460 {
461 if (m_yScrollingEnabled)
462 m_targetWindow->ScrollWindow(0, -m_yScrollPixelsPerLine * nScrollInc, (const wxRect *) NULL);
463 else
464 m_targetWindow->Refresh();
465 }
466}
467
30954328
RR
468void wxScrolledWindow::Scroll( int x_pos, int y_pos )
469{
470 if (!m_targetWindow)
471 return;
472
473 if (((x_pos == -1) || (x_pos == m_xScrollPosition)) &&
474 ((y_pos == -1) || (y_pos == m_yScrollPosition))) return;
475
30954328
RR
476 if ((x_pos != -1) && (m_xScrollPixelsPerLine))
477 {
830ed6d9
RR
478 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
479 if (max < 0) max = 0;
480 if (x_pos > max) x_pos = max;
481 if (x_pos < 0) x_pos = 0;
39c869a6 482
30954328
RR
483 int old_x = m_xScrollPosition;
484 m_xScrollPosition = x_pos;
aafe4488 485 m_hAdjust->value = x_pos;
39c869a6 486
aafe4488 487 m_targetWindow->ScrollWindow( (old_x-m_xScrollPosition)*m_xScrollPixelsPerLine, 0 );
39c869a6 488
830ed6d9
RR
489 // Just update the scrollbar, don't send any wxWindows event
490 GtkHDisconnectEvent();
aafe4488 491 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
830ed6d9 492 GtkHConnectEvent();
30954328 493 }
39c869a6 494
30954328
RR
495 if ((y_pos != -1) && (m_yScrollPixelsPerLine))
496 {
830ed6d9
RR
497 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
498 if (max < 0) max = 0;
499 if (y_pos > max) y_pos = max;
500 if (y_pos < 0) y_pos = 0;
39c869a6 501
30954328
RR
502 int old_y = m_yScrollPosition;
503 m_yScrollPosition = y_pos;
aafe4488 504 m_vAdjust->value = y_pos;
39c869a6 505
aafe4488 506 m_targetWindow->ScrollWindow( 0, (old_y-m_yScrollPosition)*m_yScrollPixelsPerLine );
39c869a6 507
830ed6d9
RR
508 // Just update the scrollbar, don't send any wxWindows event
509 GtkVDisconnectEvent();
aafe4488 510 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
830ed6d9 511 GtkVConnectEvent();
30954328
RR
512 }
513}
514
515void wxScrolledWindow::GtkVScroll( float value )
516{
aafe4488
RR
517 if (!m_targetWindow)
518 return;
519
520 if (m_yScrollPixelsPerLine == 0)
521 return;
39c869a6 522
aafe4488 523 int y_pos = (int)(value+0.5);
39c869a6 524
aafe4488
RR
525 if (y_pos == m_yScrollPosition)
526 return;
39c869a6 527
d9a4f620 528 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(m_widget);
0bc0cd5d 529 GtkRange *range = GTK_RANGE(scrolledWindow->vscrollbar);
39c869a6 530
d9a4f620
RR
531 wxEventType command = wxEVT_SCROLLWIN_THUMBTRACK;
532 if (range->scroll_type == GTK_SCROLL_STEP_BACKWARD) command = wxEVT_SCROLLWIN_LINEUP;
533 else if (range->scroll_type == GTK_SCROLL_STEP_FORWARD) command = wxEVT_SCROLLWIN_LINEDOWN;
534 else if (range->scroll_type == GTK_SCROLL_PAGE_BACKWARD) command = wxEVT_SCROLLWIN_PAGEUP;
535 else if (range->scroll_type == GTK_SCROLL_PAGE_FORWARD) command = wxEVT_SCROLLWIN_PAGEDOWN;
536
0bc0cd5d 537 wxScrollWinEvent event( command, y_pos, wxVERTICAL );
d9a4f620
RR
538 event.SetEventObject( this );
539 GetEventHandler()->ProcessEvent( event );
30954328
RR
540}
541
542void wxScrolledWindow::GtkHScroll( float value )
543{
aafe4488
RR
544 if (!m_targetWindow)
545 return;
39c869a6 546
aafe4488
RR
547 if (m_xScrollPixelsPerLine == 0)
548 return;
39c869a6 549
aafe4488 550 int x_pos = (int)(value+0.5);
0bc0cd5d 551
aafe4488
RR
552 if (x_pos == m_xScrollPosition)
553 return;
aafe4488 554
d9a4f620
RR
555 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(m_widget);
556 GtkRange *range = GTK_RANGE(scrolledWindow->hscrollbar);
39c869a6 557
d9a4f620
RR
558 wxEventType command = wxEVT_SCROLLWIN_THUMBTRACK;
559 if (range->scroll_type == GTK_SCROLL_STEP_BACKWARD) command = wxEVT_SCROLLWIN_LINEUP;
560 else if (range->scroll_type == GTK_SCROLL_STEP_FORWARD) command = wxEVT_SCROLLWIN_LINEDOWN;
561 else if (range->scroll_type == GTK_SCROLL_PAGE_BACKWARD) command = wxEVT_SCROLLWIN_PAGEUP;
562 else if (range->scroll_type == GTK_SCROLL_PAGE_FORWARD) command = wxEVT_SCROLLWIN_PAGEDOWN;
563
0bc0cd5d 564 wxScrollWinEvent event( command, x_pos, wxHORIZONTAL );
d9a4f620
RR
565 event.SetEventObject( this );
566 GetEventHandler()->ProcessEvent( event );
30954328
RR
567}
568
569void wxScrolledWindow::EnableScrolling (bool x_scroll, bool y_scroll)
570{
571 m_xScrollingEnabled = x_scroll;
572 m_yScrollingEnabled = y_scroll;
573}
574
575void wxScrolledWindow::GetVirtualSize (int *x, int *y) const
576{
577 if ( x )
578 *x = m_xScrollPixelsPerLine * m_xScrollLines;
579 if ( y )
580 *y = m_yScrollPixelsPerLine * m_yScrollLines;
581}
582
583// Where the current view starts from
584void wxScrolledWindow::GetViewStart (int *x, int *y) const
585{
586 if ( x )
587 *x = m_xScrollPosition;
588 if ( y )
589 *y = m_yScrollPosition;
590}
591
592void wxScrolledWindow::CalcScrolledPosition(int x, int y, int *xx, int *yy) const
593{
594 if ( xx )
595 *xx = x - m_xScrollPosition * m_xScrollPixelsPerLine;
596 if ( yy )
597 *yy = y - m_yScrollPosition * m_yScrollPixelsPerLine;
598}
599
600void wxScrolledWindow::CalcUnscrolledPosition(int x, int y, int *xx, int *yy) const
601{
602 if ( xx )
603 *xx = x + m_xScrollPosition * m_xScrollPixelsPerLine;
604 if ( yy )
605 *yy = y + m_yScrollPosition * m_yScrollPixelsPerLine;
606}
607
d9a4f620
RR
608int wxScrolledWindow::CalcScrollInc(wxScrollWinEvent& event)
609{
610 int pos = event.GetPosition();
611 int orient = event.GetOrientation();
612
613 int nScrollInc = 0;
614 if (event.GetEventType() == wxEVT_SCROLLWIN_TOP)
615 {
616 if (orient == wxHORIZONTAL)
617 nScrollInc = - m_xScrollPosition;
618 else
619 nScrollInc = - m_yScrollPosition;
620 } else
621 if (event.GetEventType() == wxEVT_SCROLLWIN_BOTTOM)
622 {
623 if (orient == wxHORIZONTAL)
624 nScrollInc = m_xScrollLines - m_xScrollPosition;
625 else
626 nScrollInc = m_yScrollLines - m_yScrollPosition;
627 } else
628 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP)
629 {
630 nScrollInc = -1;
631 } else
632 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN)
633 {
634 nScrollInc = 1;
635 } else
636 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEUP)
637 {
638 if (orient == wxHORIZONTAL)
639 nScrollInc = -GetScrollPageSize(wxHORIZONTAL);
640 else
641 nScrollInc = -GetScrollPageSize(wxVERTICAL);
642 } else
643 if (event.GetEventType() == wxEVT_SCROLLWIN_PAGEDOWN)
644 {
645 if (orient == wxHORIZONTAL)
646 nScrollInc = GetScrollPageSize(wxHORIZONTAL);
647 else
648 nScrollInc = GetScrollPageSize(wxVERTICAL);
649 } else
650 if ((event.GetEventType() == wxEVT_SCROLLWIN_THUMBTRACK) ||
651 (event.GetEventType() == wxEVT_SCROLLWIN_THUMBRELEASE))
652 {
653 if (orient == wxHORIZONTAL)
654 nScrollInc = pos - m_xScrollPosition;
655 else
656 nScrollInc = pos - m_yScrollPosition;
657 }
658
659 if (orient == wxHORIZONTAL)
660 {
661 if (m_xScrollPixelsPerLine > 0)
662 {
830ed6d9
RR
663 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
664 if (max < 0) max = 0;
d9a4f620
RR
665
666 if ( (m_xScrollPosition + nScrollInc) < 0 )
667 nScrollInc = -m_xScrollPosition; // As -ve as we can go
830ed6d9
RR
668 else if ( (m_xScrollPosition + nScrollInc) > max )
669 nScrollInc = max - m_xScrollPosition; // As +ve as we can go
d9a4f620
RR
670 }
671 else
672 m_targetWindow->Refresh();
673 }
674 else
675 {
676 if (m_yScrollPixelsPerLine > 0)
677 {
830ed6d9
RR
678 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
679 if (max < 0) max = 0;
d9a4f620
RR
680
681 if ( (m_yScrollPosition + nScrollInc) < 0 )
682 nScrollInc = -m_yScrollPosition; // As -ve as we can go
830ed6d9
RR
683 else if ( (m_yScrollPosition + nScrollInc) > max )
684 nScrollInc = max - m_yScrollPosition; // As +ve as we can go
d9a4f620
RR
685 }
686 else
687 m_targetWindow->Refresh();
688 }
689
690 return nScrollInc;
691}
692
0bc0cd5d
RR
693void wxScrolledWindow::SetScrollPos( int orient, int pos, bool WXUNUSED(refresh) )
694{
695 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
696
697 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
698
699 if (orient == wxHORIZONTAL)
700 {
830ed6d9
RR
701 int max = (int)(m_hAdjust->upper - m_hAdjust->page_size + 0.5);
702 if (max < 0) max = 0;
39c869a6 703
830ed6d9
RR
704 if (pos > max) pos = 0;
705 if (pos < 0) pos = 0;
39c869a6 706
830ed6d9
RR
707 if (pos == (int)(m_hAdjust->value+0.5)) return;
708 m_hAdjust->value = pos;
0bc0cd5d
RR
709 }
710 else
711 {
830ed6d9
RR
712 int max = (int)(m_vAdjust->upper - m_vAdjust->page_size + 0.5);
713 if (max < 0) max = 0;
39c869a6 714
830ed6d9
RR
715 if (pos > max) pos = 0;
716 if (pos < 0) pos = 0;
39c869a6 717
830ed6d9
RR
718 if (pos == (int)(m_vAdjust->value+0.5)) return;
719 m_vAdjust->value = pos;
0bc0cd5d
RR
720 }
721
722 if (m_wxwindow->window)
723 {
724 if (orient == wxHORIZONTAL)
725 {
830ed6d9
RR
726 // Just update the scrollbar, don't send any wxWindows event
727 GtkHDisconnectEvent();
0bc0cd5d 728 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "value_changed" );
830ed6d9 729 GtkHConnectEvent();
0bc0cd5d
RR
730 }
731 else
732 {
830ed6d9
RR
733 // Just update the scrollbar, don't send any wxWindows event
734 GtkVDisconnectEvent();
0bc0cd5d 735 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "value_changed" );
830ed6d9 736 GtkVConnectEvent();
0bc0cd5d
RR
737 }
738 }
739}
740
830ed6d9
RR
741void wxScrolledWindow::GtkVConnectEvent()
742{
743 gtk_signal_connect( GTK_OBJECT(m_vAdjust), "value_changed",
744 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
745}
746
747void wxScrolledWindow::GtkHConnectEvent()
748{
749 gtk_signal_connect( GTK_OBJECT(m_hAdjust), "value_changed",
750 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
751}
752
753void wxScrolledWindow::GtkHDisconnectEvent()
754{
755 gtk_signal_disconnect_by_func( GTK_OBJECT(m_hAdjust),
756 (GtkSignalFunc) gtk_scrolled_window_hscroll_callback, (gpointer) this );
757}
758
759void wxScrolledWindow::GtkVDisconnectEvent()
760{
761 gtk_signal_disconnect_by_func( GTK_OBJECT(m_vAdjust),
762 (GtkSignalFunc) gtk_scrolled_window_vscroll_callback, (gpointer) this );
763}
764
30954328
RR
765// ----------------------------------------------------------------------------
766// event handlers
767// ----------------------------------------------------------------------------
768
769// Default OnSize resets scrollbars, if any
770void wxScrolledWindow::OnSize(wxSizeEvent& WXUNUSED(event))
771{
772#if wxUSE_CONSTRAINTS
773 if (GetAutoLayout())
774 Layout();
775#endif
776
777 AdjustScrollbars();
778}
779
780// This calls OnDraw, having adjusted the origin according to the current
781// scroll position
782void wxScrolledWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
783{
784 wxPaintDC dc(this);
785 PrepareDC(dc);
786
787 OnDraw(dc);
788}
789
790// kbd handling: notice that we use OnChar() and not OnKeyDown() for
791// compatibility here - if we used OnKeyDown(), the programs which process
792// arrows themselves in their OnChar() would never get the message and like
793// this they always have the priority
794void wxScrolledWindow::OnChar(wxKeyEvent& event)
795{
796 int stx, sty, // view origin
797 szx, szy, // view size (total)
798 clix, cliy; // view size (on screen)
799
800 ViewStart(&stx, &sty);
801 GetClientSize(&clix, &cliy);
802 GetVirtualSize(&szx, &szy);
803
804 if( m_xScrollPixelsPerLine )
805 {
806 clix /= m_xScrollPixelsPerLine;
807 szx /= m_xScrollPixelsPerLine;
808 }
809 else
810 {
811 clix = 0;
812 szx = -1;
813 }
814 if( m_yScrollPixelsPerLine )
815 {
816 cliy /= m_yScrollPixelsPerLine;
817 szy /= m_yScrollPixelsPerLine;
818 }
819 else
820 {
821 cliy = 0;
822 szy = -1;
823 }
824
39c869a6
VZ
825 int xScrollOld = GetScrollPos(wxHORIZONTAL),
826 yScrollOld = GetScrollPos(wxVERTICAL);
827
30954328
RR
828 int dsty;
829 switch ( event.KeyCode() )
830 {
831 case WXK_PAGEUP:
832 case WXK_PRIOR:
833 dsty = sty - (5 * cliy / 6);
834 Scroll(-1, (dsty == -1) ? 0 : dsty);
835 break;
836
837 case WXK_PAGEDOWN:
838 case WXK_NEXT:
839 Scroll(-1, sty + (5 * cliy / 6));
840 break;
841
842 case WXK_HOME:
843 Scroll(0, event.ControlDown() ? 0 : -1);
844 break;
845
846 case WXK_END:
847 Scroll(szx - clix, event.ControlDown() ? szy - cliy : -1);
848 break;
849
850 case WXK_UP:
851 Scroll(-1, sty - 1);
852 break;
853
854 case WXK_DOWN:
855 Scroll(-1, sty + 1);
856 break;
857
858 case WXK_LEFT:
859 Scroll(stx - 1, -1);
860 break;
861
862 case WXK_RIGHT:
863 Scroll(stx + 1, -1);
864 break;
865
866 default:
867 // not for us
868 event.Skip();
39c869a6
VZ
869 return;
870 }
871
872 int xScroll = GetScrollPos(wxHORIZONTAL);
873 if ( xScroll != xScrollOld )
874 {
875 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, xScroll,
876 wxHORIZONTAL);
877 event.SetEventObject(this);
878 GetEventHandler()->ProcessEvent(event);
879 }
880
881 int yScroll = GetScrollPos(wxVERTICAL);
882 if ( yScroll != yScrollOld )
883 {
884 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBTRACK, yScroll,
885 wxVERTICAL);
886 event.SetEventObject(this);
887 GetEventHandler()->ProcessEvent(event);
30954328
RR
888 }
889}
3379ed37 890