2014-02-11 15:14:15 -06:00
// 11 february 2014
2014-03-12 20:55:45 -05:00
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 {
2014-03-12 20:47:39 -05:00
// Closing gets a message when the user clicks the window's close button.
2014-06-03 21:54:28 -05:00
// You cannot change it once the Window has been created.
2014-04-10 10:59:40 -05:00
// If you do not respond to this signal, nothing will happen; regardless of whether you handle the signal or not, the window will not be closed.
2014-06-10 08:55:14 -05:00
Closing chan struct { }
2014-02-11 15:14:15 -06:00
2014-06-10 08:55:14 -05:00
lock sync . Mutex
created bool
sysData * sysData
initTitle string
initWidth int
initHeight int
shownOnce bool
2014-02-11 15:14:15 -06:00
}
2014-06-03 22:08:51 -05:00
// NewWindow allocates a new Window with the given title and size. The window is not created until a call to Create() or Open().
2014-02-12 09:51:27 -06:00
func NewWindow ( title string , width int , height int ) * Window {
2014-02-11 15:14:15 -06:00
return & Window {
2014-06-10 08:55:14 -05:00
sysData : mksysdata ( c_window ) ,
initTitle : title ,
initWidth : width ,
initHeight : height ,
Closing : newEvent ( ) ,
2014-02-11 15:14:15 -06:00
}
}
2014-02-12 09:51:27 -06:00
// SetTitle sets the window's title.
2014-03-10 09:45:15 -05:00
func ( w * Window ) SetTitle ( title string ) {
2014-02-12 09:51:27 -06:00
w . lock . Lock ( )
defer w . lock . Unlock ( )
if w . created {
2014-03-10 09:45:15 -05:00
w . sysData . setText ( title )
return
2014-02-12 09:51:27 -06:00
}
w . initTitle = title
}
// 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-06-03 22:08:51 -05:00
// Open creates the Window with Create and then shows the Window with Show. As with Create, you cannot call Open more than once per window.
func ( w * Window ) Open ( control Control ) {
w . Create ( control )
w . Show ( )
}
// Create creates the Window, setting its control to the given control. It does not show the window. This can only be called once per window, and finalizes all initialization of the control.
func ( w * Window ) Create ( control Control ) {
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 {
panic ( "window already open" )
}
w . sysData . event = w . Closing
2014-06-03 22:08:51 -05:00
err := w . sysData . make ( nil )
2014-02-12 20:23:53 -06:00
if err != nil {
2014-06-03 22:08:51 -05:00
panic ( 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-06-03 22:08:51 -05:00
panic ( 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 {
2014-06-03 22:08:51 -05:00
panic ( fmt . Errorf ( "error setting window size (in Window.Open()): %v" , err ) )
2014-02-24 13:16:05 -06:00
}
2014-04-01 15:43:56 -05:00
w . sysData . setText ( w . initTitle )
2014-02-15 11:04:01 -06:00
w . created = true
2014-02-11 15:14:15 -06:00
}
2014-02-12 20:23:53 -06:00
// Show shows the window.
2014-03-09 20:47:22 -05:00
func ( w * Window ) Show ( ) {
w . lock . Lock ( )
defer w . lock . Unlock ( )
2014-06-03 22:08:51 -05:00
if ! w . shownOnce {
w . shownOnce = true
err := w . sysData . firstShow ( )
if err != nil {
panic ( fmt . Errorf ( "error showing window for the first time: %v" , err ) )
}
return
}
2014-03-09 20:47:22 -05:00
w . sysData . show ( )
2014-02-12 20:23:53 -06:00
}
// Hide hides the window.
2014-03-09 20:47:22 -05:00
func ( w * Window ) Hide ( ) {
w . lock . Lock ( )
defer w . lock . Unlock ( )
w . sysData . hide ( )
2014-02-11 15:14:15 -06:00
}