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
"syscall"
"unsafe"
)
2014-03-12 11:16:44 -05:00
// TODO change what the default window titles are?
2014-02-15 15:27:07 -06:00
var (
_messageBox = user32 . NewProc ( "MessageBoxW" )
)
2014-03-12 11:14:24 -05:00
func _msgBox ( primarytext string , secondarytext string , uType uint32 ) ( result int ) {
// 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-02 16:12:10 -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-03-12 11:21:50 -05:00
ret := make ( chan uiret )
defer close ( ret )
uitask <- & uimsg {
call : _messageBox ,
p : [ ] uintptr {
uintptr ( _NULL ) ,
uintptr ( unsafe . Pointer ( syscall . StringToUTF16Ptr ( text ) ) ) ,
uintptr ( unsafe . Pointer ( syscall . StringToUTF16Ptr ( os . Args [ 0 ] ) ) ) ,
uintptr ( uType ) ,
} ,
ret : ret ,
2014-02-15 15:27:07 -06:00
}
2014-03-12 11:21:50 -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 ) )
}
return int ( r . ret )
2014-02-15 15:27:07 -06:00
}
2014-03-12 11:14:24 -05:00
func msgBox ( primarytext string , secondarytext string ) {
_msgBox ( primarytext , secondarytext , _MB_OK )
2014-02-15 15:27:07 -06:00
}
2014-03-12 11:14:24 -05:00
func msgBoxError ( primarytext string , secondarytext string ) {
_msgBox ( primarytext , secondarytext , _MB_OK | _MB_ICONERROR )
2014-02-15 15:27:07 -06:00
}