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