2014-02-15 15:27:07 -06:00
// 7 february 2014
2014-03-12 20:55:45 -05:00
2014-02-19 10:41:10 -06:00
package ui
2014-02-15 15:27:07 -06:00
import (
"fmt"
2014-03-12 11:14:24 -05:00
"os"
2014-02-15 15:27:07 -06:00
)
var (
_messageBox = user32 . NewProc ( "MessageBoxW" )
)
2014-06-08 07:50:52 -05:00
func _msgBox ( parent * Window , primarytext string , secondarytext string , uType uint32 ) ( result chan int ) {
2014-03-12 11:14:24 -05:00
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa511267.aspx says "Use task dialogs whenever appropriate to achieve a consistent look and layout. Task dialogs require Windows Vista® or later, so they aren't suitable for earlier versions of Windows. If you must use a message box, separate the main instruction from the supplemental instruction with two line breaks."
2014-04-10 11:34:30 -05:00
text := primarytext
if secondarytext != "" {
text += "\n\n" + secondarytext
}
2014-06-03 10:00:29 -05:00
ptext := toUTF16 ( text )
ptitle := toUTF16 ( os . Args [ 0 ] )
2014-06-04 22:28:43 -05:00
parenthwnd := _HWND ( _NULL )
2014-06-08 07:50:52 -05:00
if parent != dialogWindow {
2014-06-04 22:28:43 -05:00
parenthwnd = parent . sysData . hwnd
2014-06-10 13:49:54 -05:00
uType |= _MB_APPLMODAL // only for this window
2014-06-04 22:28:43 -05:00
} else {
2014-06-10 13:49:54 -05:00
uType |= _MB_TASKMODAL // make modal to every window in the program (they're all windows of the uitask, which is a single thread)
2014-06-04 22:28:43 -05:00
}
2014-06-08 07:50:52 -05:00
retchan := make ( chan int )
go func ( ) {
2014-06-12 10:09:56 -05:00
ret := make ( chan uiret )
2014-06-08 07:50:52 -05:00
defer close ( ret )
2014-06-12 10:09:56 -05:00
uitask <- & uimsg {
call : _messageBox ,
p : [ ] uintptr {
2014-06-08 07:50:52 -05:00
uintptr ( parenthwnd ) ,
utf16ToArg ( ptext ) ,
utf16ToArg ( ptitle ) ,
2014-06-12 10:09:56 -05:00
uintptr ( uType ) ,
} ,
ret : ret ,
2014-06-08 07:50:52 -05:00
}
2014-06-12 10:09:56 -05:00
r := <- ret
if r . ret == 0 { // failure
panic ( fmt . Sprintf ( "error displaying message box to user: %v\nstyle: 0x%08X\ntitle: %q\ntext:\n%s" , r . err , uType , os . Args [ 0 ] , text ) )
}
retchan <- int ( r . ret )
2014-06-08 07:50:52 -05:00
} ( )
return retchan
2014-02-15 15:27:07 -06:00
}
2014-06-08 07:50:52 -05:00
func ( w * Window ) msgBox ( primarytext string , secondarytext string ) ( done chan struct { } ) {
done = make ( chan struct { } )
go func ( ) {
<- _msgBox ( w , primarytext , secondarytext , _MB_OK )
done <- struct { } { }
} ( )
return done
2014-02-15 15:27:07 -06:00
}
2014-06-08 07:50:52 -05:00
func ( w * Window ) msgBoxError ( primarytext string , secondarytext string ) ( done chan struct { } ) {
done = make ( chan struct { } )
go func ( ) {
2014-06-10 13:49:54 -05:00
<- _msgBox ( w , primarytext , secondarytext , _MB_OK | _MB_ICONERROR )
2014-06-08 07:50:52 -05:00
done <- struct { } { }
} ( )
return done
2014-02-15 15:27:07 -06:00
}