2013年1月21日月曜日

Haskell GUI Programming >>= Windows環境のGTK+とGtk2Hsのインストール

必要のは勿論HaskellのGHCとcabal install. Haskellのインストールは簡単なので、ここ略します。

1.GTKの準備
  GTK+で、all in one bundleをダウンロード後、解凍して、好きなフォルダに移動してください。
  注意すべきのは、フォルダのPathにSpaceがあったら、だめです。
  そして、環境変数%PATH%に、GTKのサブフォルダbinのPATHを追加する。

2.以上終わったら、これでチェックする。
  pkg-config --cflags gtk+-2.0 
  もし間違いがなければ、binのPATHが中に入ってるはずです。
  もう一つの方法はconsoleでgtk-demoを実行できれば、OKです。

3.Gtk2Hsのインストール
  cabal install cabal-install
  cabal install gtk2hs-buildtools
  cabal install gtk
  エラーがなければ、これで終了です。

4.成功したかどうかのチェック
  下のコードをファイルhello.hsにコピーペーストして、
-- A simple program to demonstrate Gtk2Hs.
module Main (Main.main) where

import Graphics.UI.Gtk

main :: IO ()
main = do
  initGUI
  -- Create a new window
  window <- windowNew
  -- Here we connect the "destroy" event to a signal handler.
  -- This event occurs when we call widgetDestroy on the window
  -- or if the user closes the window.
  onDestroy window mainQuit
  -- Sets the border width and tile of the window. Note that border width
  -- attribute is in 'Container' from which 'Window' is derived.
  set window [ containerBorderWidth := 10, windowTitle := "Hello World" ]
  -- Creates a new button with the label "Hello World".
  button <- buttonNew
  set button [ buttonLabel := "Hello World" ]
  -- When the button receives the "clicked" signal, it will call the
  -- function given as the second argument.
  onClicked button (putStrLn "Hello World")
  -- Gtk+ allows several callbacks for the same event.
  -- This one will cause the window to be destroyed by calling
  -- widgetDestroy. The callbacks are called in the sequence they were added.
  onClicked button $ do
    putStrLn "A \"clicked\"-handler to say \"destroy\""
    widgetDestroy window
  -- Insert the hello-world button into the window.
  set window [ containerChild := button ]
  -- The final step is to display this newly created widget. Note that this
  -- also allocates the right amount of space to the windows and the button.
  widgetShowAll window
  -- All Gtk+ applications must have a main loop. Control ends here
  -- and waits for an event to occur (like a key press or mouse event).
  -- This function returns if the program should finish.
  mainGUI

  ghc --make Hello.hs -o hello
  でCompile成功したら、OKです。  

0 件のコメント:

コメントを投稿