Skip to content

Commit

Permalink
Clean up window class registration
Browse files Browse the repository at this point in the history
  • Loading branch information
lxn committed Sep 15, 2012
1 parent 2938986 commit 3c3df31
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 52 deletions.
6 changes: 3 additions & 3 deletions composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import . "github.com/lxn/go-winapi"

const compositeWindowClass = `\o/ Walk_Composite_Class \o/`

var compositeWindowClassRegistered bool
func init() {
mustRegisterWindowClass(compositeWindowClass)
}

type Composite struct {
ContainerBase
}

func newCompositeWithStyle(parent Container, style uint32) (*Composite, error) {
ensureRegisteredWindowClass(compositeWindowClass, &compositeWindowClassRegistered)

c := &Composite{}
c.children = newWidgetList(c)
c.SetPersistent(true)
Expand Down
6 changes: 3 additions & 3 deletions customwidget.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import . "github.com/lxn/go-winapi"

const customWidgetWindowClass = `\o/ Walk_CustomWidget_Class \o/`

var customWidgetWindowClassRegistered bool
func init() {
mustRegisterWindowClass(customWidgetWindowClass)
}

type PaintFunc func(canvas *Canvas, updateBounds Rectangle) error

Expand All @@ -20,8 +22,6 @@ type CustomWidget struct {
}

func NewCustomWidget(parent Container, style uint, paint PaintFunc) (*CustomWidget, error) {
ensureRegisteredWindowClass(customWidgetWindowClass, &customWidgetWindowClassRegistered)

cw := &CustomWidget{paint: paint}

if err := initChildWidget(
Expand Down
6 changes: 3 additions & 3 deletions dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const (

const dialogWindowClass = `\o/ Walk_Dialog_Class \o/`

var dialogWindowClassRegistered bool
func init() {
mustRegisterWindowClass(dialogWindowClass)
}

type dialogish interface {
DefaultButton() *PushButton
Expand All @@ -44,8 +46,6 @@ type Dialog struct {
}

func NewDialog(owner RootWidget) (*Dialog, error) {
ensureRegisteredWindowClass(dialogWindowClass, &dialogWindowClassRegistered)

dlg := &Dialog{
TopLevelWindow: TopLevelWindow{
owner: owner,
Expand Down
6 changes: 3 additions & 3 deletions groupbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import . "github.com/lxn/go-winapi"

const groupBoxWindowClass = `\o/ Walk_GroupBox_Class \o/`

var groupBoxWindowClassRegistered bool
func init() {
mustRegisterWindowClass(groupBoxWindowClass)
}

type GroupBox struct {
WidgetBase
Expand All @@ -19,8 +21,6 @@ type GroupBox struct {
}

func NewGroupBox(parent Container) (*GroupBox, error) {
ensureRegisteredWindowClass(groupBoxWindowClass, &groupBoxWindowClassRegistered)

gb := &GroupBox{}

if err := initChildWidget(
Expand Down
6 changes: 3 additions & 3 deletions mainwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import . "github.com/lxn/go-winapi"

const mainWindowWindowClass = `\o/ Walk_MainWindow_Class \o/`

var mainWindowWindowClassRegistered bool
func init() {
mustRegisterWindowClass(mainWindowWindowClass)
}

type MainWindow struct {
TopLevelWindow
Expand All @@ -18,8 +20,6 @@ type MainWindow struct {
}

func NewMainWindow() (*MainWindow, error) {
ensureRegisteredWindowClass(mainWindowWindowClass, &mainWindowWindowClassRegistered)

mw := &MainWindow{}

if err := initWidget(
Expand Down
6 changes: 3 additions & 3 deletions notifyicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import . "github.com/lxn/go-winapi"

const notifyIconWindowClass = `\o/ Walk_NotifyIcon_Class \o/`

var notifyIconWindowClassRegistered bool
func init() {
mustRegisterWindowClass(notifyIconWindowClass)
}

func notifyIconWndProc(hwnd HWND, msg uint32, wParam, lParam uintptr) (result uintptr) {
// Retrieve our *NotifyIcon from the message window.
Expand Down Expand Up @@ -82,8 +84,6 @@ type NotifyIcon struct {
//
// The NotifyIcon is initially not visible.
func NewNotifyIcon() (*NotifyIcon, error) {
ensureRegisteredWindowClass(notifyIconWindowClass, &notifyIconWindowClassRegistered)

// Create the message-only window for the NotifyIcon.
hWnd := CreateWindowEx(
0,
Expand Down
6 changes: 3 additions & 3 deletions numberedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import . "github.com/lxn/go-winapi"

const numberEditWindowClass = `\o/ Walk_NumberEdit_Class \o/`

var numberEditWindowClassRegistered bool
func init() {
mustRegisterWindowClass(numberEditWindowClass)
}

type NumberEdit struct {
WidgetBase
Expand All @@ -27,8 +29,6 @@ type NumberEdit struct {
}

func NewNumberEdit(parent Container) (*NumberEdit, error) {
ensureRegisteredWindowClass(numberEditWindowClass, &numberEditWindowClassRegistered)

ne := &NumberEdit{increment: 1}

if err := initChildWidget(
Expand Down
6 changes: 3 additions & 3 deletions spacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package walk

const spacerWindowClass = `\o/ Walk_Spacer_Class \o/`

var spacerWindowClassRegistered bool
func init() {
mustRegisterWindowClass(spacerWindowClass)
}

type Spacer struct {
WidgetBase
Expand All @@ -15,8 +17,6 @@ type Spacer struct {
}

func newSpacer(parent Container, layoutFlags LayoutFlags, sizeHint Size) (*Spacer, error) {
ensureRegisteredWindowClass(spacerWindowClass, &spacerWindowClassRegistered)

s := &Spacer{
layoutFlags: layoutFlags,
sizeHint: sizeHint,
Expand Down
8 changes: 2 additions & 6 deletions splitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ import . "github.com/lxn/go-winapi"

const splitterWindowClass = `\o/ Walk_Splitter_Class \o/`

var (
splitterWindowClassRegistered bool
splitterHandleDraggingBrush *SolidColorBrush
)
var splitterHandleDraggingBrush *SolidColorBrush

func init() {
mustRegisterWindowClass(splitterWindowClass)
splitterHandleDraggingBrush, _ = NewSolidColorBrush(Color(GetSysColor(COLOR_BTNSHADOW)))
}

Expand All @@ -32,8 +30,6 @@ type Splitter struct {
}

func NewSplitter(parent Container) (*Splitter, error) {
ensureRegisteredWindowClass(splitterWindowClass, &splitterWindowClassRegistered)

layout := newSplitterLayout(Horizontal)
s := &Splitter{
ContainerBase: ContainerBase{
Expand Down
6 changes: 3 additions & 3 deletions splitterhandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import . "github.com/lxn/go-winapi"

const splitterHandleWindowClass = `\o/ Walk_SplitterHandle_Class \o/`

var splitterHandleWindowClassRegistered bool
func init() {
mustRegisterWindowClass(splitterHandleWindowClass)
}

type splitterHandle struct {
WidgetBase
Expand All @@ -19,8 +21,6 @@ func newSplitterHandle(splitter *Splitter) (*splitterHandle, error) {
return nil, newError("splitter cannot be nil")
}

ensureRegisteredWindowClass(splitterHandleWindowClass, &splitterHandleWindowClassRegistered)

sh := &splitterHandle{}
sh.parent = splitter

Expand Down
6 changes: 3 additions & 3 deletions tabpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import . "github.com/lxn/go-winapi"

const tabPageWindowClass = `\o/ Walk_TabPage_Class \o/`

var tabPageWindowClassRegistered bool
func init() {
mustRegisterWindowClass(tabPageWindowClass)
}

type TabPage struct {
ContainerBase
Expand All @@ -19,8 +21,6 @@ type TabPage struct {
}

func NewTabPage() (*TabPage, error) {
ensureRegisteredWindowClass(tabPageWindowClass, &tabPageWindowClassRegistered)

tp := &TabPage{}

if err := initWidget(
Expand Down
6 changes: 3 additions & 3 deletions tabwidget.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import . "github.com/lxn/go-winapi"

const tabWidgetWindowClass = `\o/ Walk_TabWidget_Class \o/`

var tabWidgetWindowClassRegistered bool
func init() {
mustRegisterWindowClass(tabWidgetWindowClass)
}

type TabWidget struct {
WidgetBase
Expand All @@ -26,8 +28,6 @@ type TabWidget struct {
}

func NewTabWidget(parent Container) (*TabWidget, error) {
ensureRegisteredWindowClass(tabWidgetWindowClass, &tabWidgetWindowClassRegistered)

tw := &TabWidget{currentIndex: -1}
tw.pages = newTabPageList(tw)

Expand Down
6 changes: 3 additions & 3 deletions webview.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import . "github.com/lxn/go-winapi"

const webViewWindowClass = `\o/ Walk_WebView_Class \o/`

var webViewWindowClassRegistered bool
func init() {
mustRegisterWindowClass(webViewWindowClass)
}

type WebView struct {
WidgetBase
Expand All @@ -22,8 +24,6 @@ type WebView struct {
}

func NewWebView(parent Container) (*WebView, error) {
ensureRegisteredWindowClass(webViewWindowClass, &webViewWindowClassRegistered)

wv := &WebView{
clientSite: webViewIOleClientSite{
IOleClientSite: IOleClientSite{
Expand Down
18 changes: 8 additions & 10 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,28 +313,26 @@ type WidgetBase struct {

var widgetWndProcPtr uintptr = syscall.NewCallback(widgetWndProc)

func ensureRegisteredWindowClass(className string, registered *bool) {
if registered == nil {
panic("registered cannot be nil")
}
var registeredWindowClasses map[string]bool = make(map[string]bool)

if *registered {
return
func mustRegisterWindowClass(className string) {
if registeredWindowClasses[className] {
panic("window class already registered")
}

hInst := GetModuleHandle(nil)
if hInst == 0 {
panic("GetModuleHandle failed")
panic("GetModuleHandle")
}

hIcon := LoadIcon(0, (*uint16)(unsafe.Pointer(uintptr(IDI_APPLICATION))))
if hIcon == 0 {
panic("LoadIcon failed")
panic("LoadIcon")
}

hCursor := LoadCursor(0, (*uint16)(unsafe.Pointer(uintptr(IDC_ARROW))))
if hCursor == 0 {
panic("LoadCursor failed")
panic("LoadCursor")
}

var wc WNDCLASSEX
Expand All @@ -350,7 +348,7 @@ func ensureRegisteredWindowClass(className string, registered *bool) {
panic("RegisterClassEx")
}

*registered = true
registeredWindowClasses[className] = true
}

func initWidget(widget widgetInternal, parent Widget, className string, style, exStyle uint32) error {
Expand Down

0 comments on commit 3c3df31

Please sign in to comment.