Debian Codegraph @

demo/inputmethod/Layout.hs

raw
1 {-# LANGUAGE OverloadedStrings #-}
2 -- Example of using a PangoLayout
3
4 import Data.IORef
5 import Data.Monoid ((<>))
6 import qualified Data.Text as T
7
8 import Graphics.UI.Gtk
9 import Graphics.Rendering.Cairo
10
11 loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipisicing elit,\
12 \ sed do eiusmod tempor incididunt ut labore et dolore magna\
13 \ aliqua. Ut enim ad minim veniam, quis nostrud exercitation\
14 \ ullamco laboris nisi ut aliquip ex ea commodo consequat.\
15 \ Duis aute irure dolor in reprehenderit in voluptate\
16 \ velit esse cillum dolore eu fugiat nulla pariatur.\
17 \ Excepteur sint occaecat cupidatat non proident, sunt in culpa\
18 \ qui officia deserunt mollit anim id est laborum."
19
20 data Buffer = Buffer T.Text Int
21
22 defaultBuffer = Buffer loremIpsum (T.length loremIpsum)
23
24 displayBuffer (Buffer str pos) =
25 before <> "<CURSOR>" <> after
26 where (before,after) = T.splitAt pos str
27
28 displayBufferPreedit (Buffer str pos) preeditStr preeditPos =
29 before <> "[" <> prebefore <> "<CURSOR>" <> preafter <> "]" <> after
30 where (before,after) = T.splitAt pos str
31 (prebefore, preafter) = T.splitAt preeditPos preeditStr
32
33 insertStr new (Buffer str pos) = Buffer (before<>new<>after) (pos+T.length new)
34 where (before,after) = T.splitAt pos str
35
36 deleteChar b@(Buffer str 0) = b
37 deleteChar (Buffer str pos) = Buffer (T.init before <> after) (pos-1)
38 where (before,after) = T.splitAt pos str
39
40 moveLeft b@(Buffer str pos) | pos==0 = b
41 | otherwise = Buffer str (pos-1)
42
43 moveRight b@(Buffer str pos) | pos==T.length str = b
44 | otherwise = Buffer str (pos+1)
45
46 main = do
47 initGUI
48
49 -- Create the main window.
50 win <- windowNew
51 on win objectDestroy mainQuit
52 -- Create a drawing area in which we can render text.
53 area <- drawingAreaNew
54 containerAdd win area
55 widgetSetSizeRequest area 100 100
56
57 -- Our widget's data
58 buffer <- newIORef defaultBuffer
59
60 preeditRef <- newIORef Nothing
61
62 -- Create a Cairo Context that contains information about the current font,
63 -- etc.
64 ctxt <- cairoCreateContext Nothing
65 lay <- layoutEmpty ctxt
66 layoutSetWrap lay WrapWholeWords
67
68 let relayout = do
69 buffer@(Buffer _ cursor) <- readIORef buffer
70 preedit <- readIORef preeditRef
71 case preedit of
72 Nothing -> do
73 layoutSetText lay (displayBuffer buffer)
74 layoutSetAttributes lay []
75 Just (str,attrs,pos) -> do
76 layoutSetText lay (displayBufferPreedit buffer str pos)
77 layoutSetAttributes lay (map (shiftAttribute (cursor + 1))
78 (concat attrs))
79 widgetQueueDraw area
80
81 relayout
82
83 -- Wrap the layout to a different width each time the window is resized.
84 on area sizeAllocate $ \(Rectangle _ _ w _) ->
85 layoutSetWidth lay (Just (fromIntegral w))
86
87 -- Setup the handler to draw the layout.
88 on area draw $ updateArea area lay
89
90 -- Set up input method
91 im <- imMulticontextNew
92
93 on im imContextPreeditStart $ do
94 writeIORef preeditRef (Just ("",[],0))
95 relayout
96 on im imContextPreeditEnd $ do
97 writeIORef preeditRef Nothing
98 relayout
99 on im imContextPreeditChanged $ do
100 writeIORef preeditRef . Just =<< imContextGetPreeditString im
101 relayout
102 on im imContextCommit $ \str -> do
103 modifyIORef buffer (insertStr str)
104 relayout
105 on im imContextRetrieveSurrounding $ do
106 Buffer text pos <- readIORef buffer
107 imContextSetSurrounding im text pos
108 return True
109 on im imContextDeleteSurrounding' $ \off nchars -> do
110 putStrLn $ "delete-surrounding("++show off++","++show nchars++")"
111 return False
112
113 on win realize $ do
114 imContextSetClientWindow im =<< widgetGetWindow win
115 on win focusInEvent $ liftIO (imContextFocusIn im) >> return False
116 on win focusOutEvent $ liftIO (imContextFocusOut im) >> return False
117 on win keyReleaseEvent $ imContextFilterKeypress im
118 on win keyPressEvent $ do
119 imHandled <- imContextFilterKeypress im
120 if imHandled then return True else do
121 mod <- interpretKeyPress
122 case mod of
123 Just f -> liftIO $ modifyIORef buffer f >> relayout >> return True
124 Nothing -> return False
125
126 widgetShowAll win
127 mainGUI
128
129 updateArea :: DrawingArea -> PangoLayout -> Render ()
130 updateArea area lay = do
131 moveTo 0 0
132 showLayout lay
133
134 interpretKeyPress :: EventM EKey (Maybe (Buffer -> Buffer))
135 interpretKeyPress = do
136 modifiers <- eventModifier
137 if modifiers /= [] then return Nothing else do
138 keyName <- eventKeyName
139 keyChar <- fmap keyToChar eventKeyVal
140 case keyChar of
141 Just ch -> do
142 -- This does not appear to get called; the IM handles
143 -- unmodified keypresses.
144 liftIO $ putStrLn "Literal character not handled by IM"
145 returnJust (insertStr $ T.singleton ch)
146 Nothing -> do
147 case keyName of
148 "Left" -> returnJust moveLeft
149 "Right" -> returnJust moveRight
150 "BackSpace" -> returnJust deleteChar
151 _ -> return Nothing
152 where returnJust = return . Just
153
154 shiftAttribute :: Int -> PangoAttribute -> PangoAttribute
155 shiftAttribute x attr = attr { paStart = x + paStart attr,
156 paEnd = x + paEnd attr }