| 1 |
-- Demo to show off entry completion. |
| 2 |
|
| 3 |
import Graphics.UI.Gtk |
| 4 |
import Data.Char |
| 5 |
import Data.List |
| 6 |
|
| 7 |
data ColorDesc = ColorDesc { |
| 8 |
cdColor :: Color, |
| 9 |
cdName :: String |
| 10 |
} deriving Show |
| 11 |
|
| 12 |
compareCol :: ColumnId ColorDesc String |
| 13 |
compareCol = makeColumnIdString 0 |
| 14 |
|
| 15 |
invertColor (Color r g b) = Color (32767+r) (32767+g) (32767+b) |
| 16 |
--Color (65535-r) (65535-g) (65535-b) |
| 17 |
|
| 18 |
parseColor s = ColorDesc c (dropWhile isSpace (upperToSpace name)) |
| 19 |
where |
| 20 |
s1 = dropWhile isSpace s |
| 21 |
(s2,s3) = span isDigit s1 |
| 22 |
s4 = dropWhile isSpace s3 |
| 23 |
(s5,s6) = span isDigit s4 |
| 24 |
s7 = dropWhile isSpace s6 |
| 25 |
(s8,s9) = span isDigit s7 |
| 26 |
n1 = read ('0':s2) |
| 27 |
n2 = read ('0':s5) |
| 28 |
n3 = read ('0':s8) |
| 29 |
c = Color (n1*256+n1) (n2*256+n2) (n3*256+n3) |
| 30 |
name = dropWhile isSpace s9 |
| 31 |
upperToSpace [] = [] |
| 32 |
upperToSpace (x:xs) | isUpper x = ' ':toLower x:upperToSpace xs |
| 33 |
| otherwise = x:upperToSpace xs |
| 34 |
|
| 35 |
main = |
| 36 |
do |
| 37 |
initGUI |
| 38 |
window <- windowNew |
| 39 |
|
| 40 |
contents <- readFile "rgb.txt" |
| 41 |
let killDups [] = [] |
| 42 |
killDups [x] = [x] |
| 43 |
killDups (x:y:xs) | cdName x==cdName y = killDups (y:xs) |
| 44 |
| otherwise = x:killDups (y:xs) |
| 45 |
cols = killDups $ map parseColor (drop 1 (lines contents)) |
| 46 |
store <- listStoreNew cols |
| 47 |
customStoreSetColumn store compareCol cdName |
| 48 |
|
| 49 |
entry <- entryNew |
| 50 |
completion <- entryCompletionNew |
| 51 |
entrySetCompletion entry completion |
| 52 |
|
| 53 |
set completion [entryCompletionModel := Just store] |
| 54 |
cell <- cellRendererTextNew |
| 55 |
set cell [cellTextBackgroundSet := True, |
| 56 |
cellTextForegroundSet := True] |
| 57 |
cellLayoutPackStart completion cell True |
| 58 |
cellLayoutSetAttributes completion cell store |
| 59 |
(\cd -> [cellText := cdName cd, |
| 60 |
cellTextBackgroundColor := cdColor cd, |
| 61 |
cellTextForegroundColor := invertColor (cdColor cd)]) |
| 62 |
entryCompletionSetMatchFunc completion (matchFunc store) |
| 63 |
on completion matchSelected $ \model iter -> do |
| 64 |
color <- treeModelGetValue model iter compareCol |
| 65 |
entrySetText entry color |
| 66 |
return True |
| 67 |
set window [containerChild := entry] |
| 68 |
widgetShowAll window |
| 69 |
on window objectDestroy mainQuit |
| 70 |
mainGUI |
| 71 |
|
| 72 |
matchFunc :: ListStore ColorDesc -> String -> TreeIter -> IO Bool |
| 73 |
matchFunc model str iter = do |
| 74 |
--putStrLn ("iter is "++show iter) |
| 75 |
tp <- treeModelGetPath model iter |
| 76 |
r <- case tp of |
| 77 |
(i:_) -> do row <- listStoreGetValue model i |
| 78 |
return $ any (isPrefixOf (map toLower str)) |
| 79 |
(words (map toLower (cdName row))) |
| 80 |
otherwise -> return False |
| 81 |
return r |