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