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