File: Canvas.hs

package info (click to toggle)
bustle 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 720 kB
  • sloc: haskell: 3,938; ansic: 939; makefile: 110; sh: 8
file content (289 lines) | stat: -rw-r--r-- 9,245 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
{-
Bustle.UI.Canvas: displays diagrams
Copyright © 2008–2012 Collabora Ltd.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-}
{-# LANGUAGE OverloadedStrings #-}
module Bustle.UI.Canvas
  (
    Canvas
  , canvasNew

  , canvasGetShapes
  , canvasSetShapes

  , canvasFocus
  , canvasScrollToBottom
  )
where

import Data.Maybe (isNothing)
import Data.IORef
import Control.Monad (forM_, when, void)

import Graphics.UI.Gtk
import Graphics.Rendering.Cairo (Render, translate)

import Bustle.Diagram
import Bustle.Regions
import Bustle.Util

data Canvas a =
    Canvas { canvasLayout :: Layout
           , canvasClampIdleId :: IORef (Maybe HandlerId)

           , canvasShapes :: IORef Diagram
           , canvasWidth :: IORef Double

           , canvasSelection :: IORef (RegionSelection a)
           , canvasSelectionChangedCb :: Maybe a -> IO ()

           , canvasShowBounds :: Bool
           }

canvasNew :: Eq a
          => Builder
          -> Bool
          -> (Maybe a -> IO ())
          -> IO (Canvas a)
canvasNew builder showBounds selectionChangedCb = do
    layout <- builderGetObject builder castToLayout ("diagramLayout" :: String)
    idRef <- newIORef Nothing
    shapesRef <- newIORef []
    widthRef <- newIORef 0
    rsRef <- newIORef $ regionSelectionNew []

    let canvas = Canvas layout idRef shapesRef widthRef rsRef selectionChangedCb showBounds
    setupCanvas canvas
    return canvas

-- Add/remove one step/page increment from an Adjustment, limited to the top of
-- the last page.
incStep, decStep, incPage{-, decPage -} :: Adjustment -> IO ()
incStep = incdec (+) adjustmentGetStepIncrement
decStep = incdec (-) adjustmentGetStepIncrement
incPage = incdec (+) adjustmentGetPageIncrement
--decPage = incdec (-) adjustmentGetPageIncrement

incdec :: (Double -> Double -> Double) -- How to combine the increment
       -> (Adjustment -> IO Double)    -- Action to discover the increment
       -> Adjustment
       -> IO ()
incdec (+-) f adj = do
    pos <- adjustmentGetValue adj
    step <- f adj
    page <- adjustmentGetPageSize adj
    lim <- adjustmentGetUpper adj
    adjustmentSetValue adj $ min (pos +- step) (lim - page)

setupCanvas :: Eq a
            => Canvas a
            -> IO ()
setupCanvas canvas = void $ do
    let layout = canvasLayout canvas

    -- Scrolling
    hadj <- layoutGetHAdjustment layout
    vadj <- layoutGetVAdjustment layout

    adjustmentSetStepIncrement hadj eventHeight
    adjustmentSetStepIncrement vadj eventHeight

    layout `on` keyPressEvent $ tryEvent $ do
      [] <- eventModifier
      key <- eventKeyName
      case key of
        "Left"      -> io $ decStep hadj
        "Right"     -> io $ incStep hadj
        "space"     -> io $ incPage vadj
        _           -> stopEvent

    let updateWith f = io $ canvasUpdateSelection canvas f

    -- Clicking
    layout `on` buttonPressEvent $ tryEvent $ do
      io $ layout `set` [ widgetIsFocus := True ]
      LeftButton <- eventButton
      (_, y) <- eventCoordinates

      updateWith (regionSelectionUpdate y)

    -- Keyboard navigation
    layout `on` keyPressEvent $ tryEvent $ do
      [] <- eventModifier
      key <- eventKeyName
      case key of
        "Up"        -> updateWith regionSelectionUp
        "Down"      -> updateWith regionSelectionDown
        "Home"      -> updateWith regionSelectionFirst
        "End"       -> updateWith regionSelectionLast
        _           -> stopEvent

    layout `on` draw $ canvasDraw canvas

canvasInvalidateArea :: Canvas a
                     -> Int
                     -> Int
                     -> Int
                     -> Int
                     -> IO ()
canvasInvalidateArea canvas x1 y1 x2 y2 = do
    let layout = canvasLayout canvas
    realized <- widgetGetRealized layout

    when realized $ do
        win <- layoutGetDrawWindow layout
        let pangoRectangle = Rectangle x1 y1 x2 y2
        drawWindowInvalidateRect win pangoRectangle False

canvasInvalidateStripe :: Canvas a
                       -> Stripe
                       -> IO ()
canvasInvalidateStripe canvas (Stripe y1 y2) = do
    let layout = canvasLayout canvas
    realized <- widgetGetRealized layout

    -- We only need to invalidate ourself if we're actually on the screen
    when realized $ do
        win <- layoutGetDrawWindow layout
        (width, _height) <- layoutGetSize layout
        let pangoRectangle = Rectangle 0 (floor y1) width (ceiling y2)

        drawWindowInvalidateRect win pangoRectangle False

canvasClampAroundSelection :: Canvas a
                           -> IO ()
canvasClampAroundSelection canvas = do
    let idRef = canvasClampIdleId canvas

    id_ <- readIORef idRef
    when (isNothing id_) $ do
        id' <- flip idleAdd priorityDefaultIdle $ do
            rs <- readIORef $ canvasSelection canvas
            forM_ (rsCurrent rs) $ \(Stripe top bottom, _) -> do
                vadj <- layoutGetVAdjustment $ canvasLayout canvas
                let padding = (bottom - top) / 2
                adjustmentClampPage vadj (top - padding) (bottom + padding)

            writeIORef idRef Nothing
            return False

        writeIORef idRef (Just id')

canvasGetSelection :: Canvas a
                   -> IO (Maybe (Stripe, a))
canvasGetSelection canvas = do
    rs <- readIORef $ canvasSelection canvas

    return $ rsCurrent rs

canvasUpdateSelection :: Eq a
                      => Canvas a
                      -> (RegionSelection a -> RegionSelection a)
                      -> IO ()
canvasUpdateSelection canvas f = do
    let regionSelectionRef = canvasSelection canvas
    rs <- readIORef regionSelectionRef
    let currentMessage = rsCurrent rs
        rs' = f rs
        newMessage = rsCurrent rs'
    writeIORef regionSelectionRef rs'

    when (newMessage /= currentMessage) $ do
        forM_ currentMessage $ \(r, _) ->
            canvasInvalidateStripe canvas r

        forM_ newMessage $ \(r, _) -> do
            canvasInvalidateStripe canvas r
            canvasClampAroundSelection canvas

        canvasSelectionChangedCb canvas (fmap snd newMessage)

canvasSetShapes :: Eq a
                => Canvas a
                -> Diagram
                -> Regions a
                -> Double -- Yuck. These shouldn't be here.
                -> Int    -- No no no!
                -> IO ()
canvasSetShapes canvas shapes regions centreOffset windowWidth = do
    let (width, height) = diagramDimensions shapes
        layout = canvasLayout canvas

    writeIORef (canvasShapes canvas) shapes
    writeIORef (canvasWidth canvas) width

    canvasUpdateSelection canvas $ \rs ->
      let
        rs' = regionSelectionNew regions
      in
        case rsCurrent rs of
            Just (_, x) -> regionSelectionSelect x rs'
            Nothing     -> rs'

    layoutSetSize layout (floor width) (floor height)
    canvasInvalidateArea canvas 0 0 (floor width) (floor height)

    -- FIXME: only do this the first time maybe?
    -- Shift to make the timestamp column visible
    hadj <- layoutGetHAdjustment layout
    -- Roughly centre the timestamp-and-member column
    adjustmentSetValue hadj
        (centreOffset -
            (fromIntegral windowWidth - timestampAndMemberWidth) / 2
        )

canvasGetShapes :: Canvas a
                -> IO Diagram
canvasGetShapes = readIORef . canvasShapes

-- | Redraws the currently-visible area of the canvas
canvasDraw :: Canvas a
           -> Render ()
canvasDraw canvas = do
    current <- io $ canvasGetSelection canvas
    shapes <- io $ canvasGetShapes canvas
    width <- io $ readIORef $ canvasWidth canvas
    let shapes' = case current of
            Nothing     -> shapes
            Just (Stripe y1 y2, _) -> Highlight (0, y1, width, y2):shapes

    let layout = canvasLayout canvas

    hadj <- io $ layoutGetHAdjustment layout
    hpos <- io $ adjustmentGetValue hadj
    hpage <- io $ adjustmentGetPageSize hadj

    vadj <- io $ layoutGetVAdjustment layout
    vpos <- io $ adjustmentGetValue vadj
    vpage <- io $ adjustmentGetPageSize vadj

    let r = (hpos, vpos, hpos + hpage, vpos + vpage)

    translate (-hpos) (-vpos)
    drawRegion r (canvasShowBounds canvas) shapes'

canvasFocus :: Canvas a
            -> IO ()
canvasFocus canvas = canvasLayout canvas `set` [ widgetIsFocus := True ]

canvasScrollToBottom :: Canvas a
                     -> IO ()
canvasScrollToBottom canvas = do
    vadj <- layoutGetVAdjustment (canvasLayout canvas)
    page <- adjustmentGetPageSize vadj
    lim <- adjustmentGetUpper vadj
    adjustmentSetValue vadj (max 0 (lim - page))