• No results found

Countdown Alarm Clock. GUI Programming with wxhaskell. Widgets used. UI Creation Code. Wei Tan

N/A
N/A
Protected

Academic year: 2021

Share "Countdown Alarm Clock. GUI Programming with wxhaskell. Widgets used. UI Creation Code. Wei Tan"

Copied!
5
0
0

Loading.... (view fulltext now)

Full text

(1)

GUI Programming with

wxHaskell

Wei Tan

<[email protected]>

Countdown Alarm Clock

Widgets used

Frame Menu StaticText

UI Creation Code

-- create main window & container for widgets

f <-frame [text := “Countdown Watch”, clientSize := sz 300 200] panel <- panel f []

-- create menu

timerMenu <- menuPane [text := “&Timer”]

tstart <- menuItem timerMenu [text := “&Start”] quit <- menuQuit timerMenu [help := “Quit”]

-- labels

timeLabel <- staticText panel [text := “Time:”, fontWeight := WeightBold]

-- spin control

(2)

UI Creation Code (cont’d)

-- start/cancel button

startBtn <- button panel [text := “Start”]

set startBtn <- [on command := setAlarm f startBtn hr min sec]

-- layout

set f [layout := column 1 $

[hfill $ hrule 1 -- ruler to separate menu from panel ,fill $ container panel $

margin 10 $ column 10

[hfill $ row 1 -- current time [widget timeLabel, glue, widget timeStatic],

,hfill $ row 1 -- set alarm interval spinCtrl’s [widget intvLabel, glue, widget hr, label “:”, …. ,floatBottomRight $ widget startBtn]]] -- start button

What is

wxHaskell

and what’s so good

about it?

• Haskell binding for wxWidgets

wxWidgetsis a cross-platform GUI library written in C++. Mature, extensive, actively being developed.

• supports 75% of wxWidgets’ functionality

wxHaskellis a medium-level library – it offers simple functional bindings + higher level abstraction (really neat)

Why use wxHaskell?

• Rapid prototyping • Commercial applications

• Multi-platform support, native look-and-feel • Integrate with existing Haskell code • Because we can ☺

Tour of wxHaskell

• Packages

• Controls

• Types & Inheritance

• Events

• Attributes and Properties

• Layout

(3)

wxHaskell

Packages

• Graphics.UI.WXCore

– Lower level interface to wxWidgets library

– Almost one-to-one mapping between C++ and Haskell

• Graphics.UI.WX

– Built on top of WXCore

– Provides nice functional abstraction (attributes, layout combinators, etc.)

Controls

p <- panel []

txt <- textEntry p AlignLeft [text := “your name here”]

cb <- comboBox p true [“NSW”, “ACT”, “VIC”, “WA”] []

rd <- radioBox p Horizontal [“one”, “two”] [on select := logSelection]

Other widgets: Gauge, Choice, ListBox, Slider, TreeCtrl, SplitterWindow, Toolbar

Types & Inheritance

• Encodes inheritance relationship between different

widget types using ADT

Object (Ptr) |- .. . |- Window |- Frame |- Control |- Button |- RadioBox

Button a === Ptr (... (CWindow (CControl (CButton a))) ...)

Attributes I

We can control various attributes of widgets, e.g.

caption, colour, font, size, etc.

But what attributes can I use on which widget?

• Attributes are organized into Haskell classes • Types of widgets instantiate appropriate classes • Inherit instance definitions from “parent types”

(4)

Attributes II

Type Frame a = Window (CFrame a)

Frame a instantiatesHasImage, Form, Closable, and everything that Window instantiates

Window a instantiates Textual,Literate,Dimensions,...

The HasImageclass defines the ‘image’ attribute,

Textualclass defines the ‘text’ attribute.

So, we can:

f <- frame []

set f [text := “Window Title”, image := “/some/image.ico”]

Events

• Organized into Haskell classes (like Attr)

• A widget that instantiates an event class means it can receive events of that class.

• Event handlers can be defined by turning it into an attribute using the ‘on’ function:

paint :: (Paint w) => Event w (DC () -> Rect -> IO ())

Windowis an instance of Paint, so we can define our own paint routine for all window types (including buttons and text boxes).

set f [on paint := drawObjects]

Attributes and Properties

Attributes are turned in Properties with

(:=)

Prop (Button a) Prop (Button a) / \ / \ set btn [text := “Stop”, on command := doSomething]

| | | | Attr String Event IO ()

(Button a) (Button a) String (IO ())

set :: forall w. w -> [Prop w] -> IO () (:=) :: forall w a. Attr w a -> a -> Prop w on :: forall a w. Event w a -> Attr w a

Layout

• Manages the positioning and sizing of widgets within a container widget

• wxHaskell uses layout combinatorswhich allows a more declarative style of specifying layout

• The return type of a layout combinator is always Layout

• It may take other arguments, often another Layout

• Allows precise control of behavior when window is resized (or to prevent resizing)

• Types of layout combinators: layouts (widgets, containers, glue, spacers) and transformers (stretch, expand, margin, alignment)

(5)

Layout examples

set f [layout := column1 $

[hfill $ hrule 1 -- ruler to separate menu from panel ,fill $ container panel $

margin 10 $ column10

[hfill $ row 1 -- current time [widget timeLabel, glue, widgettimeStatic],

,hfill $ row 1 -- set alarm interval spinCtrl’s [widget intvLabel, glue, widgethr, label“:”, …. ,floatBottomRight $ widget startBtn]]] -- start button

HPView

• Assignment: Heap Profile Viewer for GHC profiling output. • Similar to hp2psutility, but interactive.

• Draws a lot of lines so the mathematical model of Haskell helps.

Screenshot

Round up

• wxHaskell is great!

– script-like GUI creation, speeds up development

– no need to declare variables in IO monad, types

deduced automatically (no need to keep track of

intermediate objects)

– uniform interface for getting/setting properties

– closure for passing vars to event handlers, no

References

Related documents

In more detail, the Service Studio calls the Service Center web method to download remote commands; the Service Center replies with two commands, with ids 34 and 35; due to

In the default configuration, CAN interfaces have attached pfifo fast queuing discipline which, when enqueueing the packet, checks whether the number of queued packets is greater

• The Web UI framework provides tools such as widgets, events, effects, and animations, for Web application development. • You can code Web applications using the Web UI,

Simulation results show that our proposed scheme has the potential of reducing the CSI feedback overhead and providing more accurate CSI at the transmitter than the

We came to the Capel-y-ffin road (Gospel Pass road) and soon rejoined the Path as it descended by lane past Hill Farm, Dan-y-Capel and Cilonw.. The Path departs from the road at

I wish to extend my sincere thanks to all those who participated/who took part in the competition I would also like to thank all those who contributed to the success of the IST

Then, with this background in mind, Chapter 3 shall address  the Press within the scope of the English language (in and out the United Kingdom), and shall review its main

The total cost of the optimum solution in scenario 2, shown in Table 5, is $13,784 (expected failure cost: $12,684; maintenance cost: $100; missed mission cost: $1000).The