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
)
// Window represents an on-screen window.
type Window struct {
2014-06-30 22:48:08 -05:00
// Closing is called when the Close button is pressed by the user, or when the application needs to be quit (should the underlying system provide a concept of application distinct from window).
// Return true to allow the window to be closed; false otherwise.
// You cannot change this field after the Window has been created.
// [TODO close vs. hide]
2014-07-01 08:44:57 -05:00
// If Closing is nil, a default which rejects the close will be used.
2014-06-30 22:48:08 -05:00
Closing func ( ) bool
2014-06-10 08:55:14 -05:00
created bool
sysData * sysData
initTitle string
initWidth int
initHeight int
shownOnce bool
2014-06-25 21:07:37 -05:00
spaced bool
2014-06-30 21:48:12 -05: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-07-01 08:33:49 -05: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 ,
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
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 ) {
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-25 21:07:37 -05:00
// SetSpaced sets whether the Window's child control takes padding and spacing into account.
// That is, with w.SetSpaced(true), w's child will have a margin around the window frame and will have sub-controls separated by an implementation-defined amount.
// Currently, only Stack and Grid explicitly understand this property.
// This property is visible recursively throughout the widget hierarchy of the Window.
// This property cannot be set after the Window has been created.
func ( w * Window ) SetSpaced ( spaced bool ) {
if w . created {
panic ( fmt . Errorf ( "Window.SetSpaced() called after window created" ) )
}
w . spaced = spaced
}
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 ) {
2014-07-03 09:02:27 -05:00
w . Create ( control )
w . Show ( )
2014-06-03 22:08:51 -05:00
}
// 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-07-01 10:55:11 -05:00
if w . created {
panic ( "window already open" )
}
w . sysData . spaced = w . spaced
w . sysData . close = w . Closing
if w . sysData . close == nil {
w . sysData . close = func ( ) bool {
return false
2014-06-30 21:48:12 -05:00
}
2014-07-01 10:55:11 -05:00
}
err := w . sysData . make ( nil )
if err != nil {
panic ( fmt . Errorf ( "error opening window: %v" , err ) )
}
if control != nil {
w . sysData . allocate = control . allocate
err = control . make ( w . sysData )
2014-06-30 21:48:12 -05:00
if err != nil {
2014-07-01 10:55:11 -05:00
panic ( fmt . Errorf ( "error adding window's control: %v" , err ) )
2014-06-30 21:48:12 -05:00
}
2014-07-01 10:55:11 -05:00
}
err = w . sysData . setWindowSize ( w . initWidth , w . initHeight )
if err != nil {
panic ( fmt . Errorf ( "error setting window size (in Window.Open()): %v" , err ) )
}
w . sysData . setText ( w . initTitle )
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 ( ) {
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 . sysData . hide ( )
2014-02-11 15:14:15 -06:00
}
2014-06-10 07:34:26 -05:00
2014-06-11 08:51:00 -05:00
// Center centers the Window on-screen.
// The concept of "screen" in the case of a multi-monitor setup is implementation-defined.
// It presently panics if the Window has not been created.
2014-06-10 07:34:26 -05:00
func ( w * Window ) Center ( ) {
if ! w . created {
2014-06-11 08:51:00 -05:00
panic ( "attempt to center Window before it has been created" )
2014-06-10 07:34:26 -05:00
}
w . sysData . center ( )
}