2014-02-11 15:14:15 -06:00
// 11 february 2014
//package ui
package main
import (
"sync"
)
// TODO adorn errors in each stage with which stage failed?
// Window represents an on-screen window.
type Window struct {
// If this channel is non-nil, the event loop will receive on this when the user clicks the window's close button.
// This channel can only be set before initially opening the window.
2014-02-12 09:51:27 -06:00
Closing chan struct { }
2014-02-11 15:14:15 -06:00
2014-02-12 09:51:27 -06:00
lock sync . Mutex
created bool
sysData * sysData
initTitle string
initWidth int
initHeight int
2014-02-11 15:14:15 -06:00
}
2014-02-12 09:51:27 -06:00
// NewWindow creates a new window with the given title and size. The window is not constructed at the OS level until a call to Open().
func NewWindow ( title string , width int , height int ) * Window {
2014-02-11 15:14:15 -06:00
return & Window {
2014-02-14 10:02:59 -06:00
sysData : mksysdata ( c_window ) ,
2014-02-12 09:51:27 -06:00
initTitle : title ,
initWidth : width ,
initHeight : height ,
2014-02-11 15:14:15 -06:00
}
}
2014-02-12 09:51:27 -06:00
// SetTitle sets the window's title.
func ( w * Window ) SetTitle ( title string ) ( err error ) {
w . lock . Lock ( )
defer w . lock . Unlock ( )
if w . created {
2014-02-12 17:14:37 -06:00
return w . sysData . setText ( title )
2014-02-12 09:51:27 -06:00
}
w . initTitle = title
return nil
}
// SetSize sets the window's size.
func ( w * Window ) SetSize ( width int , height int ) ( err error ) {
w . lock . Lock ( )
defer w . lock . Unlock ( )
if w . created {
panic ( "TODO" )
}
w . initWidth = width
w . initHeight = height
return nil
}
2014-02-12 20:23:53 -06:00
// Open opens the window, setting its control to the given control, and then shows the window. This can only be called once per window, and finalizes all initialization of the control.
// TODO rename?
func ( w * Window ) Open ( control Control ) ( err error ) {
2014-02-11 15:14:15 -06:00
w . lock . Lock ( )
defer w . lock . Unlock ( )
2014-02-12 20:23:53 -06:00
if w . created {
// TODO return an error instead?
panic ( "window already open" )
}
w . sysData . event = w . Closing
err = w . sysData . make ( w . initTitle , w . initWidth , w . initHeight , nil )
if err != nil {
return err
}
if control != nil {
2014-02-13 04:28:26 -06:00
w . sysData . resize = control . setRect
2014-02-14 10:12:08 -06:00
err = control . make ( w . sysData )
2014-02-11 15:14:15 -06:00
if err != nil {
return err
}
}
2014-02-12 20:23:53 -06:00
w . created = true
2014-02-14 22:19:35 -06:00
// TODO resize window to apply control sizes
// TODO ensure the window has been shown before setting create?
2014-02-11 15:14:15 -06:00
return w . sysData . show ( )
}
2014-02-12 20:23:53 -06:00
// Show shows the window.
func ( w * Window ) Show ( ) ( err error ) {
return w . sysData . show ( )
}
// Hide hides the window.
func ( w * Window ) Hide ( ) ( err error ) {
2014-02-11 15:14:15 -06:00
return w . sysData . hide ( )
}