Contribution:
wxHaskell
Headline
GUI programming in Language:Haskell with Technology:wxHaskell
Motivation
The implementation demonstrates GUI programming in Language:Haskell with Technology:wxHaskell. A simple GUI for companies is provided. The user can navigate the hierarchical company structure, cut salaries, and edit attributes such as names, addresses, and salaries. The implementation uses a zipper-inspired focus concept to enable modification of the company tree along editing in the GUI. The focus keeps track of a location in the company tree and enables read and write operations for the location.
Illustration
Focus datatype
We are using a datastructure inspired by the concept of Zippers to specify positions of components within the company. We provide an algebraic datatype
Focus
data Focus =
CompanyFocus
| DeptFocus [Int]
| ManagerFocus [Int]
| EmployeeFocus [Int] Int
deriving (Show, Read)
We define one constructor per company datatype and one for managers. For example to construct a focus for an employee one needs to pass:
- A list of indexes: Starting from the company root this list is used to navigate through the departments and subdepartments to the employee's department.
- An index: The index of this employee in the employee's department's employees list.
The views
For each company datatype, that is
Company
Department
Employee
Frame () -> Focus -> Company -> IO ()
Viewing an employee
showEmployee
viewEmployee :: Frame () -> Focus -> Company -> IO ()
viewEmployee f focus c = do
-- reading employee
let e = readEM focus c
-- setting up frames and panels
set f [ text := "Employee \"" ++ ename e ++ "\""]
p <- panel f [textColor := textBlue]
-- boxes for name, address and salary
nameBox <- entry p [text := ename e]
addressBox <- entry p [text := address e]
salaryBox <- entry p [text := show $ salary e]
-- cut button
cButton <- cutButton p f focus c
-- back button
bButton <- backButton p f focus c
-- save button
sButton <- button p
[ text := "Save"
, size := Size 50 22
, on command := do {
newName <- get nameBox text;
newAddress <- get addressBox text;
newSalary <- get salaryBox text;
objectDelete p;
viewEmployee f focus $
writeEM focus c $
Employee newName newAddress $
read newSalary; }]
-- compose layout
setEmployeeLayout f p sButton bButton nameBox addressBox salaryBox cButton
In line 4 we read the employee in question using the Focus module's function
readEM
setEmployeeLayout
Cutting button
On each view the GUI provides a button to cut all salaries:
cutButton :: Panel () -> Frame () -> Focus -> Company -> IO (Button ())
cutButton p f focus c =
button p [ text := "Cut"
, size := Size 50 22
, on command := do {
objectDelete p;
view f focus $ readCutWrite focus c;}]
When the user clicks the button the
command
readCutWrite
command
view
view
view:: Frame () -> Focus -> Company -> IO ()
view f focus = view' f focus
where
view' = case focus of
CompanyFocus -> viewCompany
(DeptFocus ) -> viewDept
(EmployeeFocus ) -> viewEmployee
(ManagerFocus ) -> viewEmployee
Starting the GUI
We use xwHaskell's
start :: IO a -> IO ()
IO a
a
IO ()
gui :: IO ()
gui = do
f <- frame [ textBgcolor := colorRGB 112 128 144
, resizeable := False
, fontWeight := WeightBold
, fontUnderline := False
, position := Point 50 50]
showCompany f CompanyFocus company
main :: IO ()
main = start gui
In lines 3-7
gui
(:=)
showCompany
Architecture
this!!Views.hs provides one view per company datatype. this!!Main.hs starts the GUI. The algebraic datatype for companies can be found in this!!Company.hs, a sample company in this!!SampleCompany.hs. this!!Focus.hs provides a focus datatype and functions on top of it. this!!Total.hs and this!!Cut.hs provide functionality to total and cut salaries.
Usage
- this!!Main.hs has to be compiled using GHC.
- The output is executed.
There are no revisions for this page.
User contributions
User edits
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.
Syntax for editing wiki
For you are available next options:will make text bold.
will make text italic.
will make text underlined.
will make text striked.
will allow you to paste code headline into the page.
will allow you to link into the page.
will allow you to paste code with syntax highlight into the page. You will need to define used programming language.
will allow you to paste image into the page.
is list with bullets.
is list with numbers.
will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.
will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.
will allow your to insert code snippets from @worker.