1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/scrolwin.cpp
3 // Purpose: wxScrolledWindow implementation
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "scrolwin.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/dcclient.h"
34 #include "wx/gtk/scrolwin.h"
38 #include "wx/gtk/win_gtk.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 BEGIN_EVENT_TABLE(wxScrolledWindow
, wxPanel
)
45 EVT_SIZE(wxScrolledWindow::OnSize
)
46 EVT_PAINT(wxScrolledWindow::OnPaint
)
47 EVT_CHAR(wxScrolledWindow::OnChar
)
50 IMPLEMENT_DYNAMIC_CLASS(wxScrolledWindow
, wxPanel
)
52 // ============================================================================
54 // ============================================================================
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 extern bool g_blockEventsOnDrag
;
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 extern void wxapp_install_idle_handler();
69 //-----------------------------------------------------------------------------
70 // "value_changed" from m_vAdjust
71 //-----------------------------------------------------------------------------
73 static void gtk_scrolled_window_vscroll_callback( GtkAdjustment
*adjust
, wxScrolledWindow
*win
)
76 wxapp_install_idle_handler();
78 if (g_blockEventsOnDrag
) return;
80 if (!win
->m_hasVMT
) return;
82 win
->GtkVScroll( adjust
->value
);
85 //-----------------------------------------------------------------------------
86 // "value_changed" from m_hAdjust
87 //-----------------------------------------------------------------------------
89 static void gtk_scrolled_window_hscroll_callback( GtkAdjustment
*adjust
, wxScrolledWindow
*win
)
92 wxapp_install_idle_handler();
94 if (g_blockEventsOnDrag
) return;
95 if (!win
->m_hasVMT
) return;
97 win
->GtkHScroll( adjust
->value
);
100 //-----------------------------------------------------------------------------
101 // InsertChild for wxScrolledWindow
102 //-----------------------------------------------------------------------------
104 static void wxInsertChildInScrolledWindow( wxWindow
* parent
, wxWindow
* child
)
106 /* the window might have been scrolled already, do we
107 have to adapt the position */
108 GtkPizza
*pizza
= GTK_PIZZA(parent
->m_wxwindow
);
109 child
->m_x
+= pizza
->xoffset
;
110 child
->m_y
+= pizza
->yoffset
;
112 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
113 GTK_WIDGET(child
->m_widget
),
120 // ----------------------------------------------------------------------------
121 // wxScrolledWindow creation
122 // ----------------------------------------------------------------------------
124 wxScrolledWindow::wxScrolledWindow()
126 m_xScrollPixelsPerLine
= 0;
127 m_yScrollPixelsPerLine
= 0;
128 m_xScrollingEnabled
= TRUE
;
129 m_yScrollingEnabled
= TRUE
;
130 m_xScrollPosition
= 0;
131 m_yScrollPosition
= 0;
134 m_xScrollLinesPerPage
= 0;
135 m_yScrollLinesPerPage
= 0;
136 m_targetWindow
= (wxWindow
*) NULL
;
139 bool wxScrolledWindow::Create(wxWindow
*parent
,
144 const wxString
& name
)
146 if (!PreCreation( parent
, pos
, size
) ||
147 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
149 wxFAIL_MSG( wxT("wxWindow creation failed") );
153 m_insertCallback
= wxInsertChildInScrolledWindow
;
155 m_xScrollPixelsPerLine
= 0;
156 m_yScrollPixelsPerLine
= 0;
157 m_xScrollingEnabled
= TRUE
;
158 m_yScrollingEnabled
= TRUE
;
159 m_xScrollPosition
= 0;
160 m_yScrollPosition
= 0;
163 m_xScrollLinesPerPage
= 0;
164 m_yScrollLinesPerPage
= 0;
166 m_targetWindow
= this;
168 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
169 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
171 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(m_widget
);
173 GtkScrolledWindowClass
*scroll_class
= GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
174 scroll_class
->scrollbar_spacing
= 0;
176 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
178 m_hAdjust
= gtk_range_get_adjustment( GTK_RANGE(scrolledWindow
->hscrollbar
) );
179 m_vAdjust
= gtk_range_get_adjustment( GTK_RANGE(scrolledWindow
->vscrollbar
) );
181 m_wxwindow
= gtk_pizza_new();
183 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
185 GtkPizza
*pizza
= GTK_PIZZA(m_wxwindow
);
187 if (HasFlag(wxRAISED_BORDER
))
189 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_OUT
);
191 else if (HasFlag(wxSUNKEN_BORDER
))
193 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_IN
);
195 else if (HasFlag(wxSIMPLE_BORDER
))
197 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_THIN
);
201 gtk_pizza_set_shadow_type( pizza
, GTK_MYSHADOW_NONE
);
204 GTK_WIDGET_SET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
205 m_acceptsFocus
= TRUE
;
207 // I _really_ don't want scrollbars in the beginning
208 m_vAdjust
->lower
= 0.0;
209 m_vAdjust
->upper
= 1.0;
210 m_vAdjust
->value
= 0.0;
211 m_vAdjust
->step_increment
= 1.0;
212 m_vAdjust
->page_increment
= 1.0;
213 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "changed" );
214 m_hAdjust
->lower
= 0.0;
215 m_hAdjust
->upper
= 1.0;
216 m_hAdjust
->value
= 0.0;
217 m_hAdjust
->step_increment
= 1.0;
218 m_hAdjust
->page_increment
= 1.0;
219 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "changed" );
221 // these handlers get notified when screen updates are required either when
222 // scrolling or when the window size (and therefore scrollbar configuration)
224 gtk_signal_connect( GTK_OBJECT(m_hAdjust
), "value_changed",
225 (GtkSignalFunc
) gtk_scrolled_window_hscroll_callback
, (gpointer
) this );
226 gtk_signal_connect( GTK_OBJECT(m_vAdjust
), "value_changed",
227 (GtkSignalFunc
) gtk_scrolled_window_vscroll_callback
, (gpointer
) this );
229 gtk_widget_show( m_wxwindow
);
232 m_parent
->DoAddChild( this );
241 wxScrolledWindow::~wxScrolledWindow()
245 // ----------------------------------------------------------------------------
246 // setting scrolling parameters
247 // ----------------------------------------------------------------------------
250 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
251 * noUnitsX/noUnitsY: : no. units per scrollbar
253 void wxScrolledWindow::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
254 int noUnitsX
, int noUnitsY
,
255 int xPos
, int yPos
, bool noRefresh
)
257 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
258 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
259 m_xScrollPosition
= xPos
;
260 m_yScrollPosition
= yPos
;
261 m_xScrollLines
= noUnitsX
;
262 m_yScrollLines
= noUnitsY
;
264 m_hAdjust
->lower
= 0.0;
265 m_hAdjust
->upper
= noUnitsX
;
266 m_hAdjust
->value
= xPos
;
267 m_hAdjust
->step_increment
= 1.0;
268 m_hAdjust
->page_increment
= 1.0;
270 m_vAdjust
->lower
= 0.0;
271 m_vAdjust
->upper
= noUnitsY
;
272 m_vAdjust
->value
= yPos
;
273 m_vAdjust
->step_increment
= 1.0;
274 m_vAdjust
->page_increment
= 1.0;
279 void wxScrolledWindow::AdjustScrollbars()
282 m_targetWindow
->GetClientSize( &w
, &h
);
284 if (m_xScrollPixelsPerLine
== 0)
285 m_hAdjust
->page_size
= 1.0;
287 m_hAdjust
->page_size
= (w
/ m_xScrollPixelsPerLine
);
289 if (m_yScrollPixelsPerLine
== 0)
290 m_vAdjust
->page_size
= 1.0;
292 m_vAdjust
->page_size
= (h
/ m_yScrollPixelsPerLine
);
294 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "changed" );
295 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "changed" );
298 // ----------------------------------------------------------------------------
299 // target window handling
300 // ----------------------------------------------------------------------------
302 void wxScrolledWindow::SetTargetWindow( wxWindow
*target
)
304 wxASSERT_MSG( target
, wxT("target window must not be NULL") );
305 m_targetWindow
= target
;
308 wxWindow
*wxScrolledWindow::GetTargetWindow()
310 return m_targetWindow
;
313 // Override this function if you don't want to have wxScrolledWindow
314 // automatically change the origin according to the scroll position.
315 void wxScrolledWindow::PrepareDC(wxDC
& dc
)
317 dc
.SetDeviceOrigin( -m_xScrollPosition
* m_xScrollPixelsPerLine
,
318 -m_yScrollPosition
* m_yScrollPixelsPerLine
);
321 void wxScrolledWindow::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
324 *x_unit
= m_xScrollPixelsPerLine
;
326 *y_unit
= m_yScrollPixelsPerLine
;
329 int wxScrolledWindow::GetScrollPageSize(int orient
) const
331 if ( orient
== wxHORIZONTAL
)
332 return m_xScrollLinesPerPage
;
334 return m_yScrollLinesPerPage
;
337 void wxScrolledWindow::SetScrollPageSize(int orient
, int pageSize
)
339 if ( orient
== wxHORIZONTAL
)
340 m_xScrollLinesPerPage
= pageSize
;
342 m_yScrollLinesPerPage
= pageSize
;
346 * Scroll to given position (scroll position, not pixel position)
348 void wxScrolledWindow::Scroll( int x_pos
, int y_pos
)
353 if (((x_pos
== -1) || (x_pos
== m_xScrollPosition
)) &&
354 ((y_pos
== -1) || (y_pos
== m_yScrollPosition
))) return;
356 if ((x_pos
!= -1) && (m_xScrollPixelsPerLine
))
358 int old_x
= m_xScrollPosition
;
359 m_xScrollPosition
= x_pos
;
360 m_hAdjust
->value
= x_pos
;
362 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
364 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust
), "value_changed" );
367 if ((y_pos
!= -1) && (m_yScrollPixelsPerLine
))
369 int old_y
= m_yScrollPosition
;
370 m_yScrollPosition
= y_pos
;
371 m_vAdjust
->value
= y_pos
;
373 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
375 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust
), "value_changed" );
379 void wxScrolledWindow::GtkVScroll( float value
)
384 if (m_yScrollPixelsPerLine
== 0)
387 int y_pos
= (int)(value
+0.5);
389 if (y_pos
== m_yScrollPosition
)
392 int old_y
= m_yScrollPosition
;
393 m_yScrollPosition
= y_pos
;
395 m_targetWindow
->ScrollWindow( 0, (old_y
-m_yScrollPosition
)*m_yScrollPixelsPerLine
);
398 void wxScrolledWindow::GtkHScroll( float value
)
403 if (m_xScrollPixelsPerLine
== 0)
406 int x_pos
= (int)(value
+0.5);
408 if (x_pos
== m_xScrollPosition
)
411 int old_x
= m_xScrollPosition
;
412 m_xScrollPosition
= x_pos
;
414 m_targetWindow
->ScrollWindow( (old_x
-m_xScrollPosition
)*m_xScrollPixelsPerLine
, 0 );
417 void wxScrolledWindow::EnableScrolling (bool x_scroll
, bool y_scroll
)
419 m_xScrollingEnabled
= x_scroll
;
420 m_yScrollingEnabled
= y_scroll
;
423 void wxScrolledWindow::GetVirtualSize (int *x
, int *y
) const
426 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
428 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
431 // Where the current view starts from
432 void wxScrolledWindow::GetViewStart (int *x
, int *y
) const
435 *x
= m_xScrollPosition
;
437 *y
= m_yScrollPosition
;
440 void wxScrolledWindow::CalcScrolledPosition(int x
, int y
, int *xx
, int *yy
) const
443 *xx
= x
- m_xScrollPosition
* m_xScrollPixelsPerLine
;
445 *yy
= y
- m_yScrollPosition
* m_yScrollPixelsPerLine
;
448 void wxScrolledWindow::CalcUnscrolledPosition(int x
, int y
, int *xx
, int *yy
) const
451 *xx
= x
+ m_xScrollPosition
* m_xScrollPixelsPerLine
;
453 *yy
= y
+ m_yScrollPosition
* m_yScrollPixelsPerLine
;
456 // ----------------------------------------------------------------------------
458 // ----------------------------------------------------------------------------
460 // Default OnSize resets scrollbars, if any
461 void wxScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
463 #if wxUSE_CONSTRAINTS
471 // This calls OnDraw, having adjusted the origin according to the current
473 void wxScrolledWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
481 // kbd handling: notice that we use OnChar() and not OnKeyDown() for
482 // compatibility here - if we used OnKeyDown(), the programs which process
483 // arrows themselves in their OnChar() would never get the message and like
484 // this they always have the priority
485 void wxScrolledWindow::OnChar(wxKeyEvent
& event
)
487 int stx
, sty
, // view origin
488 szx
, szy
, // view size (total)
489 clix
, cliy
; // view size (on screen)
491 ViewStart(&stx
, &sty
);
492 GetClientSize(&clix
, &cliy
);
493 GetVirtualSize(&szx
, &szy
);
495 if( m_xScrollPixelsPerLine
)
497 clix
/= m_xScrollPixelsPerLine
;
498 szx
/= m_xScrollPixelsPerLine
;
505 if( m_yScrollPixelsPerLine
)
507 cliy
/= m_yScrollPixelsPerLine
;
508 szy
/= m_yScrollPixelsPerLine
;
517 switch ( event
.KeyCode() )
521 dsty
= sty
- (5 * cliy
/ 6);
522 Scroll(-1, (dsty
== -1) ? 0 : dsty
);
527 Scroll(-1, sty
+ (5 * cliy
/ 6));
531 Scroll(0, event
.ControlDown() ? 0 : -1);
535 Scroll(szx
- clix
, event
.ControlDown() ? szy
- cliy
: -1);