2014-02-11 15:14:15 -06:00
// 11 february 2014
2014-02-19 10:41:10 -06:00
package ui
2014-02-11 15:14:15 -06:00
import (
2014-02-15 11:45:17 -06:00
"fmt"
2014-02-11 15:14:15 -06:00
"sync"
)
// 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-15 11:04:01 -06:00
err = w . sysData . setText ( title )
if err != nil {
return fmt . Errorf ( "error setting window title: %v" , err )
}
return nil
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 {
2014-02-15 12:12:46 -06:00
err := w . sysData . setWindowSize ( width , height )
if err != nil {
return fmt . Errorf ( "error setting window size: %v" , err )
}
return nil
2014-02-12 09:51:27 -06:00
}
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
2014-02-15 12:07:46 -06:00
err = w . sysData . make ( w . initTitle , nil )
2014-02-12 20:23:53 -06:00
if err != nil {
2014-02-15 11:04:01 -06:00
return fmt . Errorf ( "error opening window: %v" , err )
2014-02-12 20:23:53 -06:00
}
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 {
2014-02-15 11:04:01 -06:00
return fmt . Errorf ( "error adding window's control: %v" , err )
2014-02-11 15:14:15 -06:00
}
}
2014-02-24 13:16:05 -06:00
err = w . sysData . setWindowSize ( w . initWidth , w . initHeight )
if err != nil {
return fmt . Errorf ( "error setting window size (in Window.Open()): %v" , err )
}
2014-02-15 11:04:01 -06:00
// TODO separate showing?
2014-03-09 20:40:14 -05:00
err = w . sysData . firstShow ( )
2014-02-15 11:04:01 -06:00
if err != nil {
return fmt . Errorf ( "error showing window (in Window.Open()): %v" , err )
}
w . created = true
return nil
2014-02-11 15:14:15 -06:00
}
2014-02-12 20:23:53 -06:00
// Show shows the window.
func ( w * Window ) Show ( ) ( err error ) {
2014-02-15 11:04:01 -06:00
err = w . sysData . show ( )
if err != nil {
return fmt . Errorf ( "error showing window: %v" , err )
}
return nil
2014-02-12 20:23:53 -06:00
}
// Hide hides the window.
func ( w * Window ) Hide ( ) ( err error ) {
2014-02-15 11:04:01 -06:00
err = w . sysData . hide ( )
if err != nil {
return fmt . Errorf ( "error hiding window: %v" , err )
}
return nil
2014-02-11 15:14:15 -06:00
}