Debian Codegraph @

Graphics/UI/Gtk/Scrolling/ScrolledWindow.chs

raw
1 {-# LANGUAGE CPP #-}
2 -- -*-haskell-*-
3 -- GIMP Toolkit (GTK) Widget ScrolledWindow
4 --
5 -- Author : Axel Simon
6 --
7 -- Created: 23 May 2001
8 --
9 -- Copyright (C) 1999-2005 Axel Simon
10 --
11 -- This library is free software; you can redistribute it and/or
12 -- modify it under the terms of the GNU Lesser General Public
13 -- License as published by the Free Software Foundation; either
14 -- version 2.1 of the License, or (at your option) any later version.
15 --
16 -- This library is distributed in the hope that it will be useful,
17 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
18 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 -- Lesser General Public License for more details.
20 --
21 -- |
22 -- Maintainer : [email protected]
23 -- Stability : provisional
24 -- Portability : portable (depends on GHC)
25 --
26 -- Adds scrollbars to its child widget
27 --
28 module Graphics.UI.Gtk.Scrolling.ScrolledWindow (
29 -- * Detail
30 --
31 -- | 'ScrolledWindow' is a 'Bin' subclass: it's a container the accepts a
32 -- single child widget. 'ScrolledWindow' adds scrollbars to the child widget
33 -- and optionally draws a beveled frame around the child widget.
34 --
35 -- The scrolled window can work in two ways. Some widgets have native
36 -- scrolling support; these widgets have \"slots\" for 'Adjustment' objects.
37 -- Widgets with native scroll support include 'TreeView', 'TextView', and
38 -- 'Layout'.
39 --
40 -- For widgets that lack native scrolling support, the 'Viewport' widget
41 -- acts as an adaptor class, implementing scrollability for child widgets that
42 -- lack their own scrolling capabilities. Use 'Viewport' to scroll child
43 -- widgets such as 'Table', 'Box', and so on.
44 --
45 -- If a widget has native scrolling abilities, it can be added to the
46 -- 'ScrolledWindow' with 'Graphics.UI.Gtk.Abstract.Container.containerAdd'.
47 -- If a widget does not, you must first add the widget to a 'Viewport', then
48 -- add the 'Viewport' to the scrolled window. The convenience function
49 -- 'scrolledWindowAddWithViewport' does exactly this, so you can ignore the
50 -- presence of the viewport.
51 --
52 -- The position of the scrollbars is controlled by the scroll adjustments.
53 -- See 'Adjustment' for the fields in an adjustment - for 'Scrollbar', used by
54 -- 'ScrolledWindow', the \"value\" field represents the position of the
55 -- scrollbar, which must be between the \"lower\" field and \"upper -
56 -- page_size.\" The \"page_size\" field represents the size of the visible
57 -- scrollable area. The \"step_increment\" and \"page_increment\" fields are
58 -- used when the user asks to step down (using the small stepper arrows) or
59 -- page down (using for example the PageDown key).
60 --
61 -- If a 'ScrolledWindow' doesn't behave quite as you would like, or doesn't
62 -- have exactly the right layout, it's very possible to set up your own
63 -- scrolling with 'Scrollbar' and for example a 'Table'.
64
65 -- * Class Hierarchy
66 -- |
67 -- @
68 -- | 'GObject'
69 -- | +----'Object'
70 -- | +----'Widget'
71 -- | +----'Container'
72 -- | +----'Bin'
73 -- | +----ScrolledWindow
74 -- @
75
76 -- * Types
77 ScrolledWindow,
78 ScrolledWindowClass,
79 castToScrolledWindow, gTypeScrolledWindow,
80 toScrolledWindow,
81
82 -- * Constructors
83 scrolledWindowNew,
84
85 -- * Methods
86 scrolledWindowGetHAdjustment,
87 scrolledWindowGetVAdjustment,
88 PolicyType(..),
89 scrolledWindowSetPolicy,
90 scrolledWindowGetPolicy,
91 scrolledWindowAddWithViewport,
92 CornerType(..),
93 scrolledWindowSetPlacement,
94 scrolledWindowGetPlacement,
95 ShadowType(..),
96 scrolledWindowSetShadowType,
97 scrolledWindowGetShadowType,
98 #if GTK_MAJOR_VERSION >= 3
99 scrolledWindowSetMinContentWidth,
100 scrolledWindowGetMinContentWidth,
101 scrolledWindowSetMinContentHeight,
102 scrolledWindowGetMinContentHeight,
103 #endif
104 scrolledWindowSetHAdjustment,
105 scrolledWindowSetVAdjustment,
106 #if GTK_CHECK_VERSION(2,8,0)
107 scrolledWindowGetHScrollbar,
108 scrolledWindowGetVScrollbar,
109 #endif
110 #if GTK_CHECK_VERSION(3,4,0)
111 scrolledWindowSetKineticScrolling,
112 scrolledWindowGetKineticScrolling,
113 scrolledWindowSetCaptureButtonPress,
114 scrolledWindowGetCaptureButtonPress,
115 #endif
116
117 -- * Attributes
118 scrolledWindowHAdjustment,
119 scrolledWindowVAdjustment,
120 scrolledWindowHscrollbarPolicy,
121 scrolledWindowVscrollbarPolicy,
122 scrolledWindowWindowPlacement,
123 scrolledWindowShadowType,
124 #if GTK_MAJOR_VERSION >= 3
125 scrolledWindowMinContentWidth,
126 scrolledWindowMinContentHeight,
127 #endif
128 scrolledWindowPlacement,
129 #if GTK_CHECK_VERSION(3,4,0)
130 scrolledWindowKineticScrolling,
131 #endif
132 ) where
133
134 import Control.Monad (liftM)
135 import Data.Maybe (fromMaybe)
136
137 import System.Glib.FFI
138 import System.Glib.Attributes
139 import System.Glib.Properties
140 import Graphics.UI.Gtk.Abstract.Object (makeNewObject)
141 {#import Graphics.UI.Gtk.Types#}
142 import Graphics.UI.Gtk.General.Enums (PolicyType(..), CornerType(..), ShadowType(..))
143
144 {# context lib="gtk" prefix="gtk" #}
145
146 --------------------
147 -- Constructors
148
149 -- | Creates a new scrolled window. The two arguments are the scrolled
150 -- window's adjustments; these will be shared with the scrollbars and the child
151 -- widget to keep the bars in sync with the child. Usually you want to pass
152 -- @Nothing@ for the adjustments, which will cause the scrolled window to
153 -- create them for you.
154 --
155 scrolledWindowNew ::
156 Maybe Adjustment -- ^ @hadjustment@ - Horizontal adjustment.
157 -> Maybe Adjustment -- ^ @vadjustment@ - Vertical adjustment.
158 -> IO ScrolledWindow
159 scrolledWindowNew hadjustment vadjustment =
160 makeNewObject mkScrolledWindow $
161 liftM (castPtr :: Ptr Widget -> Ptr ScrolledWindow) $
162 {# call unsafe scrolled_window_new #}
163 (fromMaybe (Adjustment nullForeignPtr) hadjustment)
164 (fromMaybe (Adjustment nullForeignPtr) vadjustment)
165
166 --------------------
167 -- Methods
168
169 -- | Returns the horizontal scrollbar's adjustment, used to connect the
170 -- horizontal scrollbar to the child widget's horizontal scroll functionality.
171 --
172 scrolledWindowGetHAdjustment :: ScrolledWindowClass self => self -> IO Adjustment
173 scrolledWindowGetHAdjustment self =
174 makeNewObject mkAdjustment $
175 {# call unsafe scrolled_window_get_hadjustment #}
176 (toScrolledWindow self)
177
178 -- | Returns the vertical scrollbar's adjustment, used to connect the vertical
179 -- scrollbar to the child widget's vertical scroll functionality.
180 --
181 scrolledWindowGetVAdjustment :: ScrolledWindowClass self => self -> IO Adjustment
182 scrolledWindowGetVAdjustment self =
183 makeNewObject mkAdjustment $
184 {# call unsafe scrolled_window_get_vadjustment #}
185 (toScrolledWindow self)
186
187 -- | Sets the scrollbar policy for the horizontal and vertical scrollbars. The
188 -- policy determines when the scrollbar should appear; it is a value from the
189 -- 'PolicyType' enumeration. If 'PolicyAlways', the scrollbar is always
190 -- present; if 'PolicyNever', the scrollbar is never present; if
191 -- 'PolicyAutomatic', the scrollbar is present only if needed (that is, if the
192 -- slider part of the bar would be smaller than the through - the display is
193 -- larger than the page size).
194 --
195 scrolledWindowSetPolicy :: ScrolledWindowClass self => self
196 -> PolicyType -- ^ @hscrollbarPolicy@ - Policy for horizontal bar.
197 -> PolicyType -- ^ @vscrollbarPolicy@ - Policy for vertical bar.
198 -> IO ()
199 scrolledWindowSetPolicy self hscrollbarPolicy vscrollbarPolicy =
200 {# call scrolled_window_set_policy #}
201 (toScrolledWindow self)
202 ((fromIntegral . fromEnum) hscrollbarPolicy)
203 ((fromIntegral . fromEnum) vscrollbarPolicy)
204
205 -- | Retrieves the current policy values for the horizontal and vertical
206 -- scrollbars. See 'scrolledWindowSetPolicy'.
207 --
208 scrolledWindowGetPolicy :: ScrolledWindowClass self => self
209 -> IO (PolicyType, PolicyType) -- ^ @(hscrollbarPolicy, vscrollbarPolicy)@
210 scrolledWindowGetPolicy self =
211 alloca $ \hPolPtr ->
212 alloca $ \vPolPtr -> do
213 {# call unsafe scrolled_window_get_policy #}
214 (toScrolledWindow self)
215 hPolPtr vPolPtr
216 hPol <- liftM (toEnum.fromIntegral) $ peek hPolPtr
217 vPol <- liftM (toEnum.fromIntegral) $ peek vPolPtr
218 return (hPol, vPol)
219
220 -- | Used to add children without native scrolling capabilities. This is
221 -- simply a convenience function; it is equivalent to adding the unscrollable
222 -- child to a viewport, then adding the viewport to the scrolled window. If a
223 -- child has native scrolling, use
224 -- 'Graphics.UI.Gtk.Abstract.Container.containerAdd' instead of this function.
225 --
226 -- The viewport scrolls the child by moving its 'DrawWindow', and takes the
227 -- size of the child to be the size of its toplevel 'DrawWindow'. This will be
228 -- very wrong for most widgets that support native scrolling; for example, if
229 -- you add a widget such as 'TreeView' with a viewport, the whole widget will
230 -- scroll, including the column headings. Thus, widgets with native scrolling
231 -- support should not be used with the 'Viewport' proxy.
232 --
233 scrolledWindowAddWithViewport :: (ScrolledWindowClass self, WidgetClass child) => self
234 -> child -- ^ @child@ - Widget you want to scroll.
235 -> IO ()
236 scrolledWindowAddWithViewport self child =
237 {# call scrolled_window_add_with_viewport #}
238 (toScrolledWindow self)
239 (toWidget child)
240
241 -- | Determines the location of the child widget with respect to the
242 -- scrollbars. The default is 'CornerTopLeft', meaning the child is in the top
243 -- left, with the scrollbars underneath and to the right. Other values in
244 -- 'CornerType' are 'CornerTopRight', 'CornerBottomLeft', and
245 -- 'CornerBottomRight'.
246 --
247 scrolledWindowSetPlacement :: ScrolledWindowClass self => self
248 -> CornerType -- ^ @windowPlacement@ - Position of the child window.
249 -> IO ()
250 scrolledWindowSetPlacement self windowPlacement =
251 {# call scrolled_window_set_placement #}
252 (toScrolledWindow self)
253 ((fromIntegral . fromEnum) windowPlacement)
254
255 -- | Gets the placement of the scrollbars for the scrolled window. See
256 -- 'scrolledWindowSetPlacement'.
257 --
258 scrolledWindowGetPlacement :: ScrolledWindowClass self => self -> IO CornerType
259 scrolledWindowGetPlacement self =
260 liftM (toEnum . fromIntegral) $
261 {# call unsafe scrolled_window_get_placement #}
262 (toScrolledWindow self)
263
264 -- | Changes the type of shadow drawn around the contents of @scrolledWindow@.
265 --
266 scrolledWindowSetShadowType :: ScrolledWindowClass self => self -> ShadowType -> IO ()
267 scrolledWindowSetShadowType self type_ =
268 {# call scrolled_window_set_shadow_type #}
269 (toScrolledWindow self)
270 ((fromIntegral . fromEnum) type_)
271
272 -- | Gets the shadow type of the scrolled window. See
273 -- 'scrolledWindowSetShadowType'.
274 --
275 scrolledWindowGetShadowType :: ScrolledWindowClass self => self -> IO ShadowType
276 scrolledWindowGetShadowType self =
277 liftM (toEnum . fromIntegral) $
278 {# call unsafe scrolled_window_get_shadow_type #}
279 (toScrolledWindow self)
280
281 #if GTK_MAJOR_VERSION >= 3
282 -- | Sets the minimum width that @scrolledWindow@ should keep visible.
283 -- Note that this can and (usually will) be smaller than the minimum size of the content.
284 --
285 scrolledWindowSetMinContentWidth :: ScrolledWindowClass self => self -> Int -> IO ()
286 scrolledWindowSetMinContentWidth self width =
287 {# call gtk_scrolled_window_set_min_content_width #}
288 (toScrolledWindow self)
289 (fromIntegral width)
290
291 -- | Gets the minimum content width of @scrolledWindow@, or -1 if not set.
292 --
293 scrolledWindowGetMinContentWidth :: ScrolledWindowClass self => self -> IO Int
294 scrolledWindowGetMinContentWidth self =
295 liftM fromIntegral $
296 {# call unsafe scrolled_window_get_min_content_width #}
297 (toScrolledWindow self)
298
299 -- | Sets the minimum height that @scrolledWindow@ should keep visible.
300 -- Note that this can and (usually will) be smaller than the minimum size of the content.
301 --
302 scrolledWindowSetMinContentHeight :: ScrolledWindowClass self => self -> Int -> IO ()
303 scrolledWindowSetMinContentHeight self height =
304 {# call gtk_scrolled_window_set_min_content_height #}
305 (toScrolledWindow self)
306 (fromIntegral height)
307
308 -- | Gets the minimum content height of @scrolledWindow@, or -1 if not set.
309 --
310 scrolledWindowGetMinContentHeight :: ScrolledWindowClass self => self -> IO Int
311 scrolledWindowGetMinContentHeight self =
312 liftM fromIntegral $
313 {# call unsafe scrolled_window_get_min_content_height #}
314 (toScrolledWindow self)
315 #endif
316
317 -- | Sets the 'Adjustment' for the horizontal scrollbar.
318 --
319 scrolledWindowSetHAdjustment :: ScrolledWindowClass self => self -> Adjustment -> IO ()
320 scrolledWindowSetHAdjustment self hadjustment =
321 {# call scrolled_window_set_hadjustment #}
322 (toScrolledWindow self)
323 hadjustment
324
325 -- | Sets the 'Adjustment' for the vertical scrollbar.
326 --
327 scrolledWindowSetVAdjustment :: ScrolledWindowClass self => self
328 -> Adjustment -- ^ @vadjustment@ - Vertical scroll adjustment.
329 -> IO ()
330 scrolledWindowSetVAdjustment self vadjustment =
331 {# call scrolled_window_set_vadjustment #}
332 (toScrolledWindow self)
333 vadjustment
334
335 #if GTK_CHECK_VERSION(2,8,0)
336 -- | Returns the horizontal scrollbar of @scrolledWindow@.
337 --
338 -- * Available since Gtk+ version 2.8
339 --
340 scrolledWindowGetHScrollbar :: ScrolledWindowClass self => self
341 -> IO (Maybe HScrollbar) -- ^ returns the horizontal scrollbar of the scrolled
342 -- window, or @Nothing@ if it does not have one.
343 scrolledWindowGetHScrollbar self =
344 maybeNull (makeNewObject mkHScrollbar) $
345 liftM (castPtr :: Ptr Widget -> Ptr HScrollbar) $
346 {# call gtk_scrolled_window_get_hscrollbar #}
347 (toScrolledWindow self)
348
349 -- | Returns the vertical scrollbar of @scrolledWindow@.
350 --
351 -- * Available since Gtk+ version 2.8
352 --
353 scrolledWindowGetVScrollbar :: ScrolledWindowClass self => self
354 -> IO (Maybe VScrollbar) -- ^ returns the vertical scrollbar of the scrolled
355 -- window, or @Nothing@ if it does not have one.
356 scrolledWindowGetVScrollbar self =
357 maybeNull (makeNewObject mkVScrollbar) $
358 liftM (castPtr :: Ptr Widget -> Ptr VScrollbar) $
359 {# call gtk_scrolled_window_get_vscrollbar #}
360 (toScrolledWindow self)
361 #endif
362
363 #if GTK_CHECK_VERSION(3,4,0)
364 -- | Turns kinetic scrolling on or off. Kinetic scrolling only applies to
365 -- devices with source GDK_SOURCE_TOUCHSCREEN.
366 --
367 scrolledWindowSetKineticScrolling :: ScrolledWindowClass self => self -> Bool -> IO ()
368 scrolledWindowSetKineticScrolling self kineticScrolling =
369 {# call scrolled_window_set_kinetic_scrolling #}
370 (toScrolledWindow self)
371 (fromBool kineticScrolling)
372
373 -- | Returns the specified kinetic scrolling behavior.
374 --
375 scrolledWindowGetKineticScrolling :: ScrolledWindowClass self => self -> IO Bool
376 scrolledWindowGetKineticScrolling self =
377 liftM toBool $
378 {# call scrolled_window_get_kinetic_scrolling #}
379 (toScrolledWindow self)
380
381 -- | Changes the behaviour of @scrolledWindow@ wrt. to the initial event that
382 -- possibly starts kinetic scrolling. When @captureButtonPress@ is set to
383 -- True, the event is captured by the scrolled window, and then later replayed
384 -- if it is meant to go to the child widget.
385 --
386 -- This should be enabled if any child widgets perform non-reversible actions
387 -- on "button-press-event". If they don't, and handle additionally handle
388 -- "grab-broken-event", it might be better to set captureButtonPress to False.
389 --
390 -- This setting only has an effect if kinetic scrolling is enabled.
391 --
392 scrolledWindowSetCaptureButtonPress :: ScrolledWindowClass self => self -> Bool -> IO ()
393 scrolledWindowSetCaptureButtonPress self captureButtonPress =
394 {# call gtk_scrolled_window_set_capture_button_press #}
395 (toScrolledWindow self)
396 (fromBool captureButtonPress)
397
398 -- | Return whether button presses are captured during kinetic scrolling.
399 -- See @scrolledWindowSetCaptureButtonPress@.
400 --
401 scrolledWindowGetCaptureButtonPress :: ScrolledWindowClass self => self -> IO Bool
402 scrolledWindowGetCaptureButtonPress self =
403 liftM toBool $
404 {# call gtk_scrolled_window_get_capture_button_press #}
405 (toScrolledWindow self)
406 #endif
407
408 --------------------
409 -- Attributes
410
411 -- | The 'Adjustment' for the horizontal position.
412 --
413 scrolledWindowHAdjustment :: ScrolledWindowClass self => Attr self Adjustment
414 scrolledWindowHAdjustment = newAttr
415 scrolledWindowGetHAdjustment
416 scrolledWindowSetHAdjustment
417
418 -- | The 'Adjustment' for the vertical position.
419 --
420 scrolledWindowVAdjustment :: ScrolledWindowClass self => Attr self Adjustment
421 scrolledWindowVAdjustment = newAttr
422 scrolledWindowGetVAdjustment
423 scrolledWindowSetVAdjustment
424
425 -- | When the horizontal scrollbar is displayed.
426 --
427 -- Default value: 'PolicyAlways'
428 --
429 scrolledWindowHscrollbarPolicy :: ScrolledWindowClass self => Attr self PolicyType
430 scrolledWindowHscrollbarPolicy = newAttrFromEnumProperty "hscrollbar-policy"
431 {# call pure unsafe gtk_policy_type_get_type #}
432
433 -- | When the vertical scrollbar is displayed.
434 --
435 -- Default value: 'PolicyAlways'
436 --
437 scrolledWindowVscrollbarPolicy :: ScrolledWindowClass self => Attr self PolicyType
438 scrolledWindowVscrollbarPolicy = newAttrFromEnumProperty "vscrollbar-policy"
439 {# call pure unsafe gtk_policy_type_get_type #}
440
441 -- | Where the contents are located with respect to the scrollbars.
442 --
443 -- Default value: 'CornerTopLeft'
444 --
445 scrolledWindowWindowPlacement :: ScrolledWindowClass self => Attr self CornerType
446 scrolledWindowWindowPlacement = newAttrFromEnumProperty "window-placement"
447 {# call pure unsafe gtk_corner_type_get_type #}
448
449 -- | Style of bevel around the contents.
450 --
451 -- Default value: 'ShadowNone'
452 --
453 scrolledWindowShadowType :: ScrolledWindowClass self => Attr self ShadowType
454 scrolledWindowShadowType = newAttr
455 scrolledWindowGetShadowType
456 scrolledWindowSetShadowType
457
458 #if GTK_MAJOR_VERSION >= 3
459 -- | Minimum width that @scrolledWindow@ should keep visible.
460 --
461 -- Default value: -1
462 --
463 scrolledWindowMinContentWidth :: ScrolledWindowClass self => Attr self Int
464 scrolledWindowMinContentWidth = newAttr
465 scrolledWindowGetMinContentWidth
466 scrolledWindowSetMinContentWidth
467
468 -- | Minimum height that @scrolledWindow@ should keep visible.
469 --
470 -- Default value: -1
471 --
472 scrolledWindowMinContentHeight :: ScrolledWindowClass self => Attr self Int
473 scrolledWindowMinContentHeight = newAttr
474 scrolledWindowGetMinContentHeight
475 scrolledWindowSetMinContentHeight
476 #endif
477
478 -- | \'placement\' property. See 'scrolledWindowGetPlacement' and
479 -- 'scrolledWindowSetPlacement'
480 --
481 scrolledWindowPlacement :: ScrolledWindowClass self => Attr self CornerType
482 scrolledWindowPlacement = newAttr
483 scrolledWindowGetPlacement
484 scrolledWindowSetPlacement
485
486 #if GTK_CHECK_VERSION(3,4,0)
487 -- | The kinetic scrolling behavior flags. Kinetic scrolling only applies to
488 -- devices with source GDK_SOURCE_TOUCHSCREEN
489 --
490 scrolledWindowKineticScrolling :: ScrolledWindowClass self => Attr self Bool
491 scrolledWindowKineticScrolling = newAttr
492 scrolledWindowGetKineticScrolling
493 scrolledWindowSetKineticScrolling
494 #endif
495