Beispiel #1
0
func CreateWindow(className string, parent Controller, exStyle, style uint) w32.HWND {
	instance := GetAppInstance()
	var parentHwnd w32.HWND
	if parent != nil {
		parentHwnd = w32.HWND(parent.Handle())
	}
	var hwnd w32.HWND
	hwnd = w32.CreateWindowEx(
		exStyle,
		syscall.StringToUTF16Ptr(className),
		nil,
		style,
		w32.CW_USEDEFAULT,
		w32.CW_USEDEFAULT,
		w32.CW_USEDEFAULT,
		w32.CW_USEDEFAULT,
		parentHwnd,
		0,
		instance,
		nil)

	if hwnd == 0 {
		errStr := fmt.Sprintf("Error occurred in CreateWindow(%s, %v, %d, %d)", className, parent, exStyle, style)
		panic(errStr)
	}

	return hwnd
}
Beispiel #2
0
func GetClipboardText() string {
	if !w32.OpenClipboard(w32.HWND(0)) {
		return ""
	}
	defer w32.CloseClipboard()

	if !w32.IsClipboardFormatAvailable(w32.CF_UNICODETEXT) {
		return ""
	}

	traw := w32.GetClipboardData(w32.CF_UNICODETEXT)
	tptr := (*uint16)(unsafe.Pointer(traw))
	return w32.UTF16PtrToString(tptr)
}
Beispiel #3
0
func SetClipboardText(text string) {
	if !w32.OpenClipboard(w32.HWND(0)) {
		return
	}
	defer w32.CloseClipboard()

	w32.EmptyClipboard()

	tuftlen := uint32(len(text)*2 + 1)
	cptrglob := w32.GlobalAlloc(w32.GMEM_MOVEABLE, tuftlen)

	cptr := unsafe.Pointer(w32.GlobalLock(cptrglob))
	tptr := unsafe.Pointer(syscall.StringToUTF16Ptr(text))
	w32.MoveMemory(cptr, tptr, tuftlen)
	w32.GlobalUnlock(cptrglob)

	w32.SetClipboardData(w32.CF_UNICODETEXT, w32.HANDLE(cptrglob))
}
Beispiel #4
0
func generalWndProc(hwnd w32.HWND, msg uint, wparam, lparam uintptr) uintptr {
	if msg == w32.WM_INITDIALOG && gDialogWaiting != nil {
		gDialogWaiting.hwnd = hwnd
		RegMsgHandler(gDialogWaiting)
	}

	if controller := GetMsgHandler(hwnd); controller != nil {
		ret := controller.WndProc(msg, wparam, lparam)
		switch msg {
		case w32.WM_NOTIFY: //Reflect notification to control
			nm := (*w32.NMHDR)(unsafe.Pointer(lparam))
			if controller := GetMsgHandler(nm.HwndFrom); controller != nil {
				ret := controller.WndProc(msg, wparam, lparam)
				if ret != 0 {
					w32.SetWindowLong(hwnd, w32.DWL_MSGRESULT, uint32(ret))
					return w32.TRUE
				}
			}
		case w32.WM_COMMAND:
			if lparam != 0 { //Reflect message to control
				h := w32.HWND(lparam)
				if controller := GetMsgHandler(h); controller != nil {
					ret := controller.WndProc(msg, wparam, lparam)
					if ret != 0 {
						w32.SetWindowLong(hwnd, w32.DWL_MSGRESULT, uint32(ret))
						return w32.TRUE
					}
				}
			}
		case w32.WM_CLOSE:
			controller.OnClose().Fire(NewEventArg(controller, nil))
		case w32.WM_KILLFOCUS:
			controller.OnKillFocus().Fire(NewEventArg(controller, nil))
		case w32.WM_SETFOCUS:
			controller.OnSetFocus().Fire(NewEventArg(controller, nil))
		case w32.WM_DROPFILES:
			controller.OnDropFiles().Fire(NewEventArg(controller, genDropFilesEventArg(wparam)))
		case w32.WM_LBUTTONDOWN:
			controller.OnLBDown().Fire(NewEventArg(controller, genMouseEventArg(wparam, lparam)))
		case w32.WM_LBUTTONUP:
			controller.OnLBUp().Fire(NewEventArg(controller, genMouseEventArg(wparam, lparam)))
		case w32.WM_MBUTTONDOWN:
			controller.OnMBDown().Fire(NewEventArg(controller, genMouseEventArg(wparam, lparam)))
		case w32.WM_MBUTTONUP:
			controller.OnMBUp().Fire(NewEventArg(controller, genMouseEventArg(wparam, lparam)))
		case w32.WM_RBUTTONDOWN:
			controller.OnRBDown().Fire(NewEventArg(controller, genMouseEventArg(wparam, lparam)))
		case w32.WM_RBUTTONUP:
			controller.OnRBUp().Fire(NewEventArg(controller, genMouseEventArg(wparam, lparam)))
		case w32.WM_PAINT:
			canvas := NewCanvasFromHwnd(hwnd)
			defer canvas.Dispose()
			controller.OnPaint().Fire(NewEventArg(controller, &PaintEventData{Canvas: canvas}))
		case w32.WM_KEYUP:
			controller.OnKeyUp().Fire(NewEventArg(controller, &KeyUpEventData{int(wparam), int(lparam)}))
		case w32.WM_SIZE:
			x, y := genPoint(lparam)
			controller.OnSize().Fire(NewEventArg(controller, &SizeEventData{uint(wparam), x, y}))
		}

		//Trigger msg handler registered via "Bind".
		if handler, ok := controller.BindedHandler(msg); ok {
			handler(NewEventArg(controller, &RawMsg{hwnd, msg, wparam, lparam}))
		}

		return ret
	}

	return w32.DefWindowProc(hwnd, uint32(msg), wparam, lparam)
}
Beispiel #5
0
// WinEvent proccess a win32 event.
func winEvent(id w32.HWND, event uint32, wParam, lParam uintptr) uintptr {
	w, ok := widgetTable[id]
	if !ok {
		return w32.DefWindowProc(id, event, wParam, lParam)
	}
	switch event {
	case w32.WM_CHAR:
		r := utf16.Decode([]uint16{uint16(loWord(uint32(wParam)))})
		if len(r) == 0 {
			break
		}
		key := r[0]
		if key == 0 {
			break
		}
		if (key == '\b') || (key == '\t') || (key == '\n') || (key == '\r') {
			break
		}
		ev := sparta.KeyEvent{
			Key:   sparta.Key(key),
			State: getState(),
		}
		x, y, _ := w32.GetCursorPos()
		ev.Loc.X, ev.Loc.Y, _ = w32.ScreenToClient(id, x, y)
		w.OnEvent(ev)
	case w32.WM_CLOSE:
		if w.Property(sparta.Parent) != nil {
			break
		}
		w.OnEvent(sparta.CloseEvent{})
	case w32.WM_KEYDOWN:
		key := getKeyValue(wParam)
		if key == 0 {
			break
		}
		if (key & sparta.KeyNoChar) == 0 {
			break
		}
		ev := sparta.KeyEvent{
			Key:   key,
			State: getState(),
		}
		x, y, _ := w32.GetCursorPos()
		ev.Loc.X, ev.Loc.Y, _ = w32.ScreenToClient(id, x, y)
		w.OnEvent(ev)
	case w32.WM_KEYUP:
		key := getKeyValue(wParam)
		if key == 0 {
			break
		}
		ev := sparta.KeyEvent{
			Key:   -key,
			State: getState(),
		}
		x, y, _ := w32.GetCursorPos()
		ev.Loc.X, ev.Loc.Y, _ = w32.ScreenToClient(id, x, y)
		w.OnEvent(ev)
	case w32.WM_LBUTTONDOWN, w32.WM_RBUTTONDOWN, w32.WM_MBUTTONDOWN:
		ev := sparta.MouseEvent{
			Button: getButton(event),
			State:  getState(),
			Loc:    image.Pt(getXLParam(lParam), getYLParam(lParam)),
		}
		w.OnEvent(ev)
		w.Focus()
	case w32.WM_LBUTTONUP, w32.WM_RBUTTONUP, w32.WM_MBUTTONUP:
		ev := sparta.MouseEvent{
			Button: -getButton(event),
			State:  getState(),
			Loc:    image.Pt(getXLParam(lParam), getYLParam(lParam)),
		}
		w.OnEvent(ev)
	case w32.WM_MOUSEMOVE:
		ev := sparta.MouseEvent{
			Loc: image.Pt(getXLParam(lParam), getYLParam(lParam)),
		}
		w.OnEvent(ev)
	case w32.WM_MOUSEWHEEL:
		ev := sparta.MouseEvent{
			Button: sparta.MouseWheel,
		}
		if getWheelDeltaWParam(wParam) < 0 {
			ev.Button = -sparta.MouseWheel
		}
		ev.Loc.X, ev.Loc.Y, _ = w32.ScreenToClient(id, getXLParam(lParam), getYLParam(lParam))
		w = propagateWheel(w, ev.Loc)
		w.OnEvent(ev)
	case w32.WM_MOVE:
		win := w.Window().(*window)
		win.pos.X, win.pos.Y = int(loWord(uint32(lParam))), int(hiWord(uint32(lParam)))
	case w32.WM_PAINT:
		win := w.Window().(*window)
		ps := &w32.PAINTSTRUCT{}
		win.dc = w32.BeginPaint(id, ps)
		win.isPaint = true

		w32.SetBkMode(win.dc, w32.TRANSPARENT)
		w32.SetBkColor(win.dc, win.back.color)

		// "clear" the area
		w32.SelectObject(win.dc, w32.HGDIOBJ(win.back.brush))
		w32.SelectObject(win.dc, w32.HGDIOBJ(win.back.pen))
		w32.Rectangle(win.dc, int(ps.RcPaint.Left), int(ps.RcPaint.Top), int(ps.RcPaint.Right), int(ps.RcPaint.Bottom))

		w32.SelectObject(win.dc, w32.HGDIOBJ(win.fore.brush))
		w32.SelectObject(win.dc, w32.HGDIOBJ(win.fore.pen))
		w32.SelectObject(win.dc, w32.HGDIOBJ(winFont))
		w32.SetTextColor(win.dc, win.fore.color)
		win.curr = win.fore

		ev := sparta.ExposeEvent{image.Rect(int(ps.RcPaint.Left), int(ps.RcPaint.Top), int(ps.RcPaint.Right), int(ps.RcPaint.Bottom))}
		w.OnEvent(ev)
		w32.EndPaint(id, ps)
		win.isPaint = false
		win.dc = 0
	case w32.WM_SIZE:
		win := w.Window().(*window)
		ev := sparta.ConfigureEvent{image.Rect(win.pos.X, win.pos.Y, win.pos.X+int(loWord(uint32(lParam))), win.pos.Y+int(hiWord(uint32(lParam))))}
		w.OnEvent(ev)
	case w32.WM_USER:
		src, ok := widgetTable[w32.HWND(wParam)]
		if !ok {
			src = nil
		}
		ev := sparta.CommandEvent{
			Source: src,
			Value:  int(int32(lParam)),
		}
		w.OnEvent(ev)
	default:
		return w32.DefWindowProc(id, event, wParam, lParam)
	}
	return 0
}
Beispiel #6
0
func (b *MsgBuilder) error() {
	w32.MessageBox(w32.HWND(0), b.Msg, firstOf(b.Dlg.Title, "Error"), w32.MB_OK|w32.MB_ICONERROR)
}
Beispiel #7
0
func (b *MsgBuilder) yesNo() bool {
	r := w32.MessageBox(w32.HWND(0), b.Msg, firstOf(b.Dlg.Title, "Confirm?"), w32.MB_YESNO)
	return r == w32.IDYES
}