2016-09-29 3 views
0

"패널"을 클릭 한 후 "statusField"를 업데이트하는 방법에 대한 조언이 필요합니다.wxhaskell : 패널의 "클릭 할 때"를 사용하여 statusField 업데이트

다음 프로그램은 문제를 보여줍니다. 이 프로그램은 두 개의 프레임을 그립니다 ( ). 왼쪽 프레임이 그림 영역 일 것이라고 상상할 수 있으며 오른쪽 프레임에는 "빨강"과 "녹색"버튼이 있습니다. "빨간색"이라고 표시된 버튼을 클릭 한 후 statusField의 텍스트는 이 "현재 색상 : 빨간색"으로 업데이트되었습니다. "녹색"레이블이 붙은 단추는 텍스트를 "현재 색 : 녹색"으로 업데이트합니다.

사용자가 왼쪽 패널 을 클릭 한 후 statusField 텍스트를 변경하는 방법은 무엇입니까? 예 : " 드로잉 패널을 성공적으로 클릭했습니다."로 변경하십시오.

버튼의 "on 명령"과 같은 방식으로 "클릭"에서 수행 할 수없는 이유는 무엇입니까? (아래 소스의 주석을 참조하십시오.)

고맙습니다.

module Main where 

import Graphics.UI.WX 

-- | NOP (= No Operation) 
data Command = Nop 
      | Red 
      | Green 
       deriving (Eq) 

main :: IO() 
main 
    = start hello 


hello :: IO() 
hello 
    = do currentCommand <- varCreate $ Nop    -- current command performed on next click on "pDrawingarea" 

      status <- statusField [text := "Welcome."] 

      -- Frames and Panels 
      f   <- frame [ text := "Demo" 
            , bgcolor := lightgrey ] 

      pButtons  <- panel f [ bgcolor := lightgrey] 
      pDrawingarea <- panel f [ on paint := draw 
            , bgcolor := lightgrey 
            ] 

      set pDrawingarea [on click := do drawingAreaOnClick status currentCommand pDrawingarea 
              -- set status [text := "User clicked on the panel."] 
              -- Problem: uncommenting the line above shows the problem 
          ] 

      bRed <- button pButtons [text := "Red", on command := do varSet currentCommand Red 
                    set status [text := "Current color: Red"] 
           ] 

      bGreen <- button pButtons [text := "Green", on command := do varSet currentCommand Green 
                     set status [text := "Current color: Green"] 
            ] 

      set pButtons [ layout := column 1 [ hstretch.expand $ widget bRed 
              , hstretch.expand $ widget bGreen 
              ] 
         ] 

      set f [ statusBar := [status] 
       , layout := row 3 [ 
            minsize (sz 600 500) $ stretch.expand $ widget pDrawingarea 
            , vstretch.expand $ rule 3 500 
            , minsize (sz 200 500) $ vstretch.expand $ widget pButtons 
            ]  
       ] 

      return() 

draw :: DC a -> Rect -> IO() 
draw dc viewArea 
    = do putStrLn "Imagine some code to repaint the screen." 


drawingAreaOnClick :: statusField -> Var Command -> Panel() -> Point -> IO() 
drawingAreaOnClick sf command panel pt 
    = do c <- varGet command 
     case c of 
      Red -> do putStrLn "Imagine some code to do red painting" 
      Green -> do putStrLn "Imagine some code to do green painting" 

답변

0

이 문제에 많은 시간을 보낸 후에 해결책을 찾았습니다.

이 솔루션은 "statusField"자체가 클래스 "대한 텍스트가"나는이 문제를 이해하지의 일원이기 때문에

drawingAreaOnClick :: Textual x => x -> Var Command -> Panel() -> Point -> IO() 

drawingAreaOnClick :: statusField -> Var Command -> Panel() -> Point -> IO() 

의 정의를 변경하는 것입니다.

완성도를 위해 나는 또한 GHC verions.The 원래 문제는 GHC 7.8.4와 함께 발생했고 GHC 7.10.3에서 작동하는 해결책을 찾았습니다. 나는 GHC 버전이 문제에 영향을 미치는지 말할 수 없다. 참고로

전체 작업 코드 :

module Main where 

import Graphics.UI.WX 

-- | NOP (= No Operation) 
data Command = Nop 
      | Red 
      | Green 
       deriving (Eq) 

main :: IO() 
main 
    = start hello 


hello :: IO() 
hello 
    = do currentCommand <- varCreate Nop    -- current command performed on next click on "pDrawingarea" 


      status <- statusField [text := "Welcome."] 

      -- not needed:  currentStatus <- varCreate status 


      -- Frames and Panels 
      f   <- frame [ text := "Demo" 
            , bgcolor := lightgrey ] 

      pButtons  <- panel f [ bgcolor := lightgrey] 
      pDrawingarea <- panel f [ on paint := draw 
            , bgcolor := lightgrey 
            ] 

      set pDrawingarea [on click := do drawingAreaOnClick status currentCommand pDrawingarea 
              -- set status [text := "User clicked on the panel."] 
              -- Problem: uncommenting the line above shows the problem 
          ] 

      bRed <- button pButtons [text := "Red", on command := do varSet currentCommand Red 
                    set status [text := "Current color: Red"] 
           ] 

      bGreen <- button pButtons [text := "Green", on command := do varSet currentCommand Green 
                     set status [text := "Current color: Green"] 
                     --sf <- varGet currentStatus 
                     -- set sf [text := "yyy"] 

            ] 

      set pButtons [ layout := column 1 [ hstretch.expand $ widget bRed 
              , hstretch.expand $ widget bGreen 
              ] 
         ] 

      set f [ statusBar := [status] 
       , layout := row 3 [ 
            minsize (sz 600 500) $ stretch.expand $ widget pDrawingarea 
            , vstretch.expand $ rule 3 500 
            , minsize (sz 200 500) $ vstretch.expand $ widget pButtons 
            ]  
       ] 

      return() 

draw :: DC a -> Rect -> IO() 
draw dc viewArea 
    = do putStrLn "Imagine some code to repaint the screen." 


drawingAreaOnClick :: Textual x => x -> Var Command -> Panel() -> Point -> IO() 
drawingAreaOnClick sf command panel pt 
    = do c <- varGet command 
     set sf [text := "Drawing on the screen."] 
     case c of 
      Red -> do putStrLn "Imagine some code to do red painting" 
      Green -> do putStrLn "Imagine some code to do green painting"