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