diff --git a/AUTHORS b/AUTHORS index 1e358d7f..666d3f6c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -29,3 +29,4 @@ Shawn Sun Tim Dufrane Vincent Vanackere gonutz +xaxys diff --git a/declarative/mainwindow.go b/declarative/mainwindow.go index 2731b223..6824878a 100644 --- a/declarative/mainwindow.go +++ b/declarative/mainwindow.go @@ -46,14 +46,16 @@ type MainWindow struct { // MainWindow - AssignTo **walk.MainWindow - Expressions func() map[string]walk.Expression - Functions map[string]func(args ...interface{}) (interface{}, error) - MenuItems []MenuItem - OnDropFiles walk.DropFilesEventHandler - StatusBarItems []StatusBarItem - ToolBar ToolBar - ToolBarItems []MenuItem // Deprecated: use ToolBar instead + AssignTo **walk.MainWindow + Expressions func() map[string]walk.Expression + Functions map[string]func(args ...interface{}) (interface{}, error) + MenuItems []MenuItem + OnDeviceArrival walk.DeviceArrivalEventHandler + OnDeviceRemove walk.DeviceRemoveEventHandler + OnDropFiles walk.DropFilesEventHandler + StatusBarItems []StatusBarItem + ToolBar ToolBar + ToolBarItems []MenuItem // Deprecated: use ToolBar instead } func (mw MainWindow) Create() error { @@ -157,7 +159,17 @@ func (mw MainWindow) Create() error { } w.ToolBar().SetImageList(imageList) - if mw.OnDropFiles != nil { + if mw.OnDeviceArrival != nil || mw.OnDeviceRemove != nil{ + event := w.DeviceChange() + if mw.OnDeviceArrival != nil { + event.AttachArrival(mw.OnDeviceArrival) + } + if mw.OnDeviceRemove != nil { + event.AttachRemove(mw.OnDeviceRemove) + } + } + + if mw.OnDropFiles != nil{ w.DropFiles().Attach(mw.OnDropFiles) } diff --git a/devicechangeevent.go b/devicechangeevent.go new file mode 100644 index 00000000..e43c30bb --- /dev/null +++ b/devicechangeevent.go @@ -0,0 +1,94 @@ +// Copyright 2011 The Walk Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build windows + +package walk + +import ( + "github.com/lxn/win" +) + +const ( + DBT_DEVICEARRIVAL = 0x8000 + DBT_DEVICEREMOVECOMPLETE = 0x8004 +) + +type DeviceArrivalEventHandler func() +type DeviceRemoveEventHandler func() + +type DeviceChangeEvent struct { + hWnd win.HWND + arrivalhandlers []DeviceArrivalEventHandler + removehandlers []DeviceRemoveEventHandler +} + + +func (e *DeviceChangeEvent) AttachArrival(handler DeviceArrivalEventHandler) int { + for i, h := range e.arrivalhandlers { + if h == nil { + e.arrivalhandlers[i] = handler + return i + } + } + + e.arrivalhandlers = append(e.arrivalhandlers, handler) + return len(e.arrivalhandlers) - 1 +} + +func (e *DeviceChangeEvent) AttachRemove(handler DeviceRemoveEventHandler) int { + for i, h := range e.removehandlers { + if h == nil { + e.removehandlers[i] = handler + return i + } + } + + e.removehandlers = append(e.removehandlers, handler) + return len(e.removehandlers) - 1 +} + +func (e *DeviceChangeEvent) DetachArrival(handle int) { + e.arrivalhandlers[handle] = nil + for _, h := range e.arrivalhandlers { + if h != nil { + return + } + } +} + +func (e *DeviceChangeEvent) DetachRemove(handle int) { + e.removehandlers[handle] = nil + for _, h := range e.removehandlers { + if h != nil { + return + } + } +} + +type DeviceChangeEventPublisher struct { + event DeviceChangeEvent +} + +func (p *DeviceChangeEventPublisher) Event(hWnd win.HWND) *DeviceChangeEvent { + p.event.hWnd = hWnd + return &p.event +} + +func (p *DeviceChangeEventPublisher) Publish(wParam int) { + switch wParam { + case DBT_DEVICEARRIVAL: + for _, handler := range p.event.arrivalhandlers { + if handler != nil { + handler() + } + } + case DBT_DEVICEREMOVECOMPLETE: + for _, handler := range p.event.removehandlers { + if handler != nil { + handler() + } + } + } +} diff --git a/window.go b/window.go index 13a5b1c5..8943c23a 100644 --- a/window.go +++ b/window.go @@ -296,6 +296,7 @@ type WindowBase struct { contextMenu *Menu disposables []Disposable disposingPublisher EventPublisher + devicechangePublisher DeviceChangeEventPublisher dropFilesPublisher DropFilesEventPublisher keyDownPublisher KeyEventPublisher keyPressPublisher KeyEventPublisher @@ -1443,6 +1444,12 @@ func (wb *WindowBase) KeyUp() *KeyEvent { return wb.keyUpPublisher.Event() } +// DeviceChange returns a *DeviceChangeEvent that you can attach to for handling +// device change events for the *WindowBase. +func (wb *WindowBase) DeviceChange() *DeviceChangeEvent { + return wb.devicechangePublisher.Event(wb.hWnd) +} + // DropFiles returns a *DropFilesEvent that you can attach to for handling // drop file events for the *WindowBase. func (wb *WindowBase) DropFiles() *DropFilesEvent { @@ -1868,6 +1875,9 @@ func (wb *WindowBase) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) case win.WM_KEYUP: wb.handleKeyUp(wParam, lParam) + case win.WM_DEVICECHANGE: + wb.devicechangePublisher.Publish((int)(wParam)) + case win.WM_DROPFILES: wb.dropFilesPublisher.Publish(win.HDROP(wParam))