Skip to content

Commit

Permalink
Update "w32" package usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenDang committed Apr 16, 2012
1 parent be70ae3 commit 685f3d4
Show file tree
Hide file tree
Showing 23 changed files with 193 additions and 228 deletions.
18 changes: 7 additions & 11 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package gform

import (
"github.com/AllenDang/w32"
"github.com/AllenDang/w32/comctl32"
"github.com/AllenDang/w32/gdiplus"
"github.com/AllenDang/w32/kernel32"
"github.com/AllenDang/w32/user32"
"unsafe"
)

func Init() {
gAppInstance = kernel32.GetModuleHandle("")
gAppInstance = w32.GetModuleHandle("")
if gAppInstance == 0 {
panic("Error occurred in App.Init")
}
Expand All @@ -22,7 +18,7 @@ func Init() {
w32.ICC_LISTVIEW_CLASSES | w32.ICC_PROGRESS_CLASS | w32.ICC_TAB_CLASSES |
w32.ICC_TREEVIEW_CLASSES | w32.ICC_BAR_CLASSES

comctl32.InitCommonControlsEx(&initCtrls)
w32.InitCommonControlsEx(&initCtrls)
}

func GetAppInstance() w32.HINSTANCE {
Expand Down Expand Up @@ -57,18 +53,18 @@ func PreTranslateMessage(msg *w32.MSG) bool {
func RunMainLoop() int {
var m w32.MSG

for user32.GetMessage(&m, 0, 0, 0) != 0 {
for w32.GetMessage(&m, 0, 0, 0) != 0 {
if !PreTranslateMessage(&m) {
user32.TranslateMessage(&m)
user32.DispatchMessage(&m)
w32.TranslateMessage(&m)
w32.DispatchMessage(&m)
}
}

gdiplus.GdiplusShutdown()
w32.GdiplusShutdown()

return int(m.WParam)
}

func Exit() {
user32.PostQuitMessage(0)
w32.PostQuitMessage(0)
}
38 changes: 17 additions & 21 deletions bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ package gform
import (
"errors"
"github.com/AllenDang/w32"
"github.com/AllenDang/w32/gdi32"
"github.com/AllenDang/w32/gdiplus"
"github.com/AllenDang/w32/kernel32"
"github.com/AllenDang/w32/ole32"
"unsafe"
)

Expand All @@ -17,7 +13,7 @@ type Bitmap struct {

func assembleBitmapFromHBITMAP(hbitmap w32.HBITMAP) (*Bitmap, error) {
var dib w32.DIBSECTION
if gdi32.GetObject(w32.HGDIOBJ(hbitmap), unsafe.Sizeof(dib), unsafe.Pointer(&dib)) == 0 {
if w32.GetObject(w32.HGDIOBJ(hbitmap), unsafe.Sizeof(dib), unsafe.Pointer(&dib)) == 0 {
return nil, errors.New("GetObject for HBITMAP failed")
}

Expand All @@ -32,15 +28,15 @@ func NewBitmapFromFile(filepath string, background Color) (*Bitmap, error) {
var gpBitmap *uintptr
var err error

gpBitmap, err = gdiplus.GdipCreateBitmapFromFile(filepath)
gpBitmap, err = w32.GdipCreateBitmapFromFile(filepath)
if err != nil {
return nil, err
}
defer gdiplus.GdipDisposeImage(gpBitmap)
defer w32.GdipDisposeImage(gpBitmap)

var hbitmap w32.HBITMAP
// Reverse gform.RGB to BGR to satisfy gdiplus color schema.
hbitmap, err = gdiplus.GdipCreateHBITMAPFromBitmap(gpBitmap, uint32(RGB(background.B(), background.G(), background.R())))
hbitmap, err = w32.GdipCreateHBITMAPFromBitmap(gpBitmap, uint32(RGB(background.B(), background.G(), background.R())))
if err != nil {
return nil, err
}
Expand All @@ -53,30 +49,30 @@ func NewBitmapFromResource(instance w32.HINSTANCE, resName *uint16, resType *uin
var err error
var hRes w32.HRSRC

hRes, err = kernel32.FindResource(w32.HMODULE(instance), resName, resType)
hRes, err = w32.FindResource(w32.HMODULE(instance), resName, resType)
if err != nil {
return nil, err
}
resSize := kernel32.SizeofResource(w32.HMODULE(instance), hRes)
pResData := kernel32.LockResource(kernel32.LoadResource(w32.HMODULE(instance), hRes))
resBuffer := kernel32.GlobalAlloc(w32.GMEM_MOVEABLE, resSize)
pResBuffer := kernel32.GlobalLock(resBuffer)
kernel32.MoveMemory(pResBuffer, pResData, resSize)
resSize := w32.SizeofResource(w32.HMODULE(instance), hRes)
pResData := w32.LockResource(w32.LoadResource(w32.HMODULE(instance), hRes))
resBuffer := w32.GlobalAlloc(w32.GMEM_MOVEABLE, resSize)
pResBuffer := w32.GlobalLock(resBuffer)
w32.MoveMemory(pResBuffer, pResData, resSize)

stream := ole32.CreateStreamOnHGlobal(resBuffer, false)
stream := w32.CreateStreamOnHGlobal(resBuffer, false)

gpBitmap, err = gdiplus.GdipCreateBitmapFromStream(stream)
gpBitmap, err = w32.GdipCreateBitmapFromStream(stream)
if err != nil {
return nil, err
}
defer stream.Release()
defer kernel32.GlobalUnlock(resBuffer)
defer kernel32.GlobalFree(resBuffer)
defer gdiplus.GdipDisposeImage(gpBitmap)
defer w32.GlobalUnlock(resBuffer)
defer w32.GlobalFree(resBuffer)
defer w32.GdipDisposeImage(gpBitmap)

var hbitmap w32.HBITMAP
// Reverse gform.RGB to BGR to satisfy gdiplus color schema.
hbitmap, err = gdiplus.GdipCreateHBITMAPFromBitmap(gpBitmap, uint32(RGB(background.B(), background.G(), background.R())))
hbitmap, err = w32.GdipCreateHBITMAPFromBitmap(gpBitmap, uint32(RGB(background.B(), background.G(), background.R())))
if err != nil {
return nil, err
}
Expand All @@ -86,7 +82,7 @@ func NewBitmapFromResource(instance w32.HINSTANCE, resName *uint16, resType *uin

func (this *Bitmap) Dispose() {
if this.handle != 0 {
gdi32.DeleteObject(w32.HGDIOBJ(this.handle))
w32.DeleteObject(w32.HGDIOBJ(this.handle))
this.handle = 0
}
}
Expand Down
7 changes: 3 additions & 4 deletions brush.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gform

import (
"github.com/AllenDang/w32"
"github.com/AllenDang/w32/gdi32"
)

type Brush struct {
Expand All @@ -12,7 +11,7 @@ type Brush struct {

func NewSolidColorBrush(color Color) *Brush {
lb := w32.LOGBRUSH{LbStyle: w32.BS_SOLID, LbColor: w32.COLORREF(color)}
hBrush := gdi32.CreateBrushIndirect(&lb)
hBrush := w32.CreateBrushIndirect(&lb)
if hBrush == 0 {
panic("Faild to create solid color brush")
}
Expand All @@ -22,7 +21,7 @@ func NewSolidColorBrush(color Color) *Brush {

func NewNullBrush() *Brush {
lb := w32.LOGBRUSH{LbStyle: w32.BS_NULL}
hBrush := gdi32.CreateBrushIndirect(&lb)
hBrush := w32.CreateBrushIndirect(&lb)
if hBrush == 0 {
panic("Failed to create null brush")
}
Expand All @@ -40,7 +39,7 @@ func (this *Brush) GetLOGBRUSH() *w32.LOGBRUSH {

func (this *Brush) Dispose() {
if this.hBrush != 0 {
gdi32.DeleteObject(w32.HGDIOBJ(this.hBrush))
w32.DeleteObject(w32.HGDIOBJ(this.hBrush))
this.hBrush = 0
}
}
9 changes: 4 additions & 5 deletions buttons.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package gform

import (
"github.com/AllenDang/w32"
"github.com/AllenDang/w32/user32"
)

type Button struct {
W32Control
}

func (this *Button) Checked() bool {
result := user32.SendMessage(this.hwnd, w32.BM_GETCHECK, 0, 0)
result := w32.SendMessage(this.hwnd, w32.BM_GETCHECK, 0, 0)
return result == w32.BST_CHECKED
}

Expand All @@ -19,7 +18,7 @@ func (this *Button) SetChecked(checked bool) {
if !checked {
wparam = w32.BST_UNCHECKED
}
user32.SendMessage(this.hwnd, w32.BM_SETCHECK, uintptr(wparam), 0)
w32.SendMessage(this.hwnd, w32.BM_SETCHECK, uintptr(wparam), 0)
}

type PushButton struct {
Expand Down Expand Up @@ -55,9 +54,9 @@ func (this *PushButton) WndProc(msg uint, wparam, lparam uintptr) uintptr {
case w32.BN_CLICKED:
println("Clicked")
case w32.WM_LBUTTONDOWN:
user32.SetCapture(this.Handle())
w32.SetCapture(this.Handle())
case w32.WM_LBUTTONUP:
user32.ReleaseCapture()
w32.ReleaseCapture()
}

return this.W32Control.WndProc(msg, wparam, lparam)
Expand Down
56 changes: 27 additions & 29 deletions canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package gform
import (
"fmt"
"github.com/AllenDang/w32"
"github.com/AllenDang/w32/gdi32"
"github.com/AllenDang/w32/user32"
)

type Canvas struct {
Expand All @@ -14,7 +12,7 @@ type Canvas struct {
}

func NewCanvasFromHwnd(hwnd w32.HWND) *Canvas {
hdc := user32.GetDC(hwnd)
hdc := w32.GetDC(hwnd)
if hdc == 0 {
panic(fmt.Sprintf("Create canvas from %v failed.", hwnd))
}
Expand All @@ -33,70 +31,70 @@ func NewCanvasFromHDC(hdc w32.HDC) *Canvas {
func (this *Canvas) Dispose() {
if !this.doNotDispose && this.hdc != 0 {
if this.hwnd == 0 {
gdi32.DeleteDC(this.hdc)
w32.DeleteDC(this.hdc)
} else {
user32.ReleaseDC(this.hwnd, this.hdc)
w32.ReleaseDC(this.hwnd, this.hdc)
}

this.hdc = 0
}
}

func (this *Canvas) DrawBitmap(bmp *Bitmap, x, y int) {
cdc := gdi32.CreateCompatibleDC(0)
defer gdi32.DeleteDC(cdc)
cdc := w32.CreateCompatibleDC(0)
defer w32.DeleteDC(cdc)

hbmpOld := gdi32.SelectObject(cdc, w32.HGDIOBJ(bmp.GetHBITMAP()))
defer gdi32.SelectObject(cdc, w32.HGDIOBJ(hbmpOld))
hbmpOld := w32.SelectObject(cdc, w32.HGDIOBJ(bmp.GetHBITMAP()))
defer w32.SelectObject(cdc, w32.HGDIOBJ(hbmpOld))

w, h := bmp.Size()

gdi32.BitBlt(this.hdc, x, y, w, h, cdc, 0, 0, w32.SRCCOPY)
w32.BitBlt(this.hdc, x, y, w, h, cdc, 0, 0, w32.SRCCOPY)
}

func (this *Canvas) DrawStretchedBitmap(bmp *Bitmap, rect *Rect) {
cdc := gdi32.CreateCompatibleDC(0)
defer gdi32.DeleteDC(cdc)
cdc := w32.CreateCompatibleDC(0)
defer w32.DeleteDC(cdc)

hbmpOld := gdi32.SelectObject(cdc, w32.HGDIOBJ(bmp.GetHBITMAP()))
defer gdi32.SelectObject(cdc, w32.HGDIOBJ(hbmpOld))
hbmpOld := w32.SelectObject(cdc, w32.HGDIOBJ(bmp.GetHBITMAP()))
defer w32.SelectObject(cdc, w32.HGDIOBJ(hbmpOld))

w, h := bmp.Size()

rc := rect.GetW32Rect()
gdi32.StretchBlt(this.hdc, rc.Left, rc.Top, rc.Right, rc.Bottom, cdc, 0, 0, w, h, w32.SRCCOPY)
w32.StretchBlt(this.hdc, rc.Left, rc.Top, rc.Right, rc.Bottom, cdc, 0, 0, w, h, w32.SRCCOPY)
}

func (this *Canvas) DrawIcon(ico *Icon, x, y int) bool {
return user32.DrawIcon(this.hdc, x, y, ico.Handle())
return w32.DrawIcon(this.hdc, x, y, ico.Handle())
}

func (this *Canvas) DrawRect(rect *Rect, pen *Pen, brush *Brush) {
w32Rect := rect.GetW32Rect()

previousPen := gdi32.SelectObject(this.hdc, w32.HGDIOBJ(pen.GetHPEN()))
defer gdi32.SelectObject(this.hdc, previousPen)
previousPen := w32.SelectObject(this.hdc, w32.HGDIOBJ(pen.GetHPEN()))
defer w32.SelectObject(this.hdc, previousPen)

previousBrush := gdi32.SelectObject(this.hdc, w32.HGDIOBJ(brush.GetHBRUSH()))
defer gdi32.SelectObject(this.hdc, previousBrush)
previousBrush := w32.SelectObject(this.hdc, w32.HGDIOBJ(brush.GetHBRUSH()))
defer w32.SelectObject(this.hdc, previousBrush)

gdi32.Rectangle(this.hdc, w32Rect.Left, w32Rect.Top, w32Rect.Right, w32Rect.Bottom)
w32.Rectangle(this.hdc, w32Rect.Left, w32Rect.Top, w32Rect.Right, w32Rect.Bottom)
}

func (this *Canvas) FillRect(rect *Rect, brush *Brush) {
user32.FillRect(this.hdc, rect.GetW32Rect(), brush.GetHBRUSH())
w32.FillRect(this.hdc, rect.GetW32Rect(), brush.GetHBRUSH())
}

// Refer win32 DrawText document for uFormat.
func (this *Canvas) DrawText(text string, rect *Rect, format uint, font *Font, textColor Color) {
previousFont := gdi32.SelectObject(this.hdc, w32.HGDIOBJ(font.GetHFONT()))
defer gdi32.SelectObject(this.hdc, w32.HGDIOBJ(previousFont))
previousFont := w32.SelectObject(this.hdc, w32.HGDIOBJ(font.GetHFONT()))
defer w32.SelectObject(this.hdc, w32.HGDIOBJ(previousFont))

previousBkMode := gdi32.SetBkMode(this.hdc, w32.TRANSPARENT)
defer gdi32.SetBkMode(this.hdc, previousBkMode)
previousBkMode := w32.SetBkMode(this.hdc, w32.TRANSPARENT)
defer w32.SetBkMode(this.hdc, previousBkMode)

previousTextColor := gdi32.SetTextColor(this.hdc, w32.COLORREF(textColor))
defer gdi32.SetTextColor(this.hdc, previousTextColor)
previousTextColor := w32.SetTextColor(this.hdc, w32.COLORREF(textColor))
defer w32.SetTextColor(this.hdc, previousTextColor)

user32.DrawText(this.hdc, text, len(text), rect.GetW32Rect(), format)
w32.DrawText(this.hdc, text, len(text), rect.GetW32Rect(), format)
}
20 changes: 8 additions & 12 deletions commondlgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package gform

import (
"github.com/AllenDang/w32"
"github.com/AllenDang/w32/comdlg32"
"github.com/AllenDang/w32/ole32"
"github.com/AllenDang/w32/shell32"
"github.com/AllenDang/w32/user32"
"syscall"
"unsafe"
)
Expand Down Expand Up @@ -47,7 +43,7 @@ func ShowOpenFileDlg(parent Controller, title, filter string, filterIndex uint,
buf := make([]uint16, 1024)
ofn := genOFN(parent, title, filter, filterIndex, initialDir, buf)

if accepted = comdlg32.GetOpenFileName(ofn); accepted {
if accepted = w32.GetOpenFileName(ofn); accepted {
filePath = syscall.UTF16ToString(buf)
}

Expand All @@ -58,7 +54,7 @@ func ShowSaveFileDlg(parent Controller, title, filter string, filterIndex uint,
buf := make([]uint16, 1024)
ofn := genOFN(parent, title, filter, filterIndex, initialDir, buf)

if accepted = comdlg32.GetSaveFileName(ofn); accepted {
if accepted = w32.GetSaveFileName(ofn); accepted {
filePath = syscall.UTF16ToString(buf)
}

Expand All @@ -71,21 +67,21 @@ func ShowBrowseFolderDlg(parent Controller, title string) (folder string, accept
bi.Title = syscall.StringToUTF16Ptr(title)
bi.Flags = w32.BIF_RETURNONLYFSDIRS | w32.BIF_NEWDIALOGSTYLE

ole32.CoInitialize()
ret := shell32.SHBrowseForFolder(&bi)
ole32.CoUninitialize()
w32.CoInitialize()
ret := w32.SHBrowseForFolder(&bi)
w32.CoUninitialize()

folder = shell32.SHGetPathFromIDList(ret)
folder = w32.SHGetPathFromIDList(ret)
accepted = folder != ""
return
}

func MsgBox(parent Controller, title, caption string, flags uint) int {
var result int
if parent != nil {
result = user32.MessageBox(parent.Handle(), caption, title, flags)
result = w32.MessageBox(parent.Handle(), caption, title, flags)
} else {
result = user32.MessageBox(0, caption, title, flags)
result = w32.MessageBox(0, caption, title, flags)
}

return result
Expand Down
Loading

0 comments on commit 685f3d4

Please sign in to comment.