-
Notifications
You must be signed in to change notification settings - Fork 30
/
app.go
105 lines (83 loc) · 2.26 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright (C) 2019 The Winc Authors. All Rights Reserved.
* Copyright (C) 2010-2013 Allen Dang. All Rights Reserved.
*/
package winc
import (
"runtime"
"unsafe"
"github.com/tadvi/winc/w32"
)
var (
// resource compilation tool assigns app.ico ID of 3
// rsrc -manifest app.manifest -ico app.ico -o rsrc.syso
AppIconID = 3
)
func init() {
runtime.LockOSThread()
gAppInstance = w32.GetModuleHandle("")
if gAppInstance == 0 {
panic("Error occurred in App.Init")
}
// Initialize the common controls
var initCtrls w32.INITCOMMONCONTROLSEX
initCtrls.DwSize = uint32(unsafe.Sizeof(initCtrls))
initCtrls.DwICC =
w32.ICC_LISTVIEW_CLASSES | w32.ICC_PROGRESS_CLASS | w32.ICC_TAB_CLASSES |
w32.ICC_TREEVIEW_CLASSES | w32.ICC_BAR_CLASSES
w32.InitCommonControlsEx(&initCtrls)
}
// SetAppIconID sets recource icon ID for the apps windows.
func SetAppIcon(appIconID int) {
AppIconID = appIconID
}
func GetAppInstance() w32.HINSTANCE {
return gAppInstance
}
func PreTranslateMessage(msg *w32.MSG) bool {
// This functions is called by the MessageLoop. It processes the
// keyboard accelerator keys and calls Controller.PreTranslateMessage for
// keyboard and mouse events.
processed := false
if (msg.Message >= w32.WM_KEYFIRST && msg.Message <= w32.WM_KEYLAST) ||
(msg.Message >= w32.WM_MOUSEFIRST && msg.Message <= w32.WM_MOUSELAST) {
if msg.Hwnd != 0 {
if controller := GetMsgHandler(msg.Hwnd); controller != nil {
// Search the chain of parents for pretranslated messages.
for p := controller; p != nil; p = p.Parent() {
if processed = p.PreTranslateMessage(msg); processed {
break
}
}
}
}
}
return processed
}
// RunMainLoop processes messages in main application loop.
func RunMainLoop() int {
var m w32.MSG
for w32.GetMessage(&m, 0, 0, 0) != 0 {
if !PreTranslateMessage(&m) {
w32.TranslateMessage(&m)
w32.DispatchMessage(&m)
}
}
w32.GdiplusShutdown()
return int(m.WParam)
}
// PostMessages processes recent messages. Sometimes helpful for instant window refresh.
func PostMessages() {
var m w32.MSG
for i := 0; i < 10; i++ {
if w32.GetMessage(&m, 0, 0, 0) != 0 {
if !PreTranslateMessage(&m) {
w32.TranslateMessage(&m)
w32.DispatchMessage(&m)
}
}
}
}
func Exit() {
w32.PostQuitMessage(0)
}