From 8cb3991ef9245ebc84d3760bd2bb279b0a3e09ed Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 1 Apr 2014 21:17:27 -0400 Subject: [PATCH] Fixed Windows control sizing: turns out simply calling GetTextMetrics() was not enough, as the GetDC() functions don't load the control font into the DC; we have to do it ourselves with SelectObject() (according to GetTextMetrics()'s docs on MSDN). Upon re-evaluation, the only things that need custom fonts are menus and statusbars; I don't know if menus can be done with the standard contorls and statusbars change the font of all controls inside... so how fonts are handled in classData needs to change now. --- prefsize_windows.go | 7 +++++++ sysdata_windows.go | 4 ++++ todo.md | 5 +++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/prefsize_windows.go b/prefsize_windows.go index 5a34032..9a16f8d 100644 --- a/prefsize_windows.go +++ b/prefsize_windows.go @@ -62,6 +62,7 @@ var stdDlgSizes = [nctypes]dlgunits{ } var ( + _selectObject = gdi32.NewProc("SelectObject") _getTextMetrics = gdi32.NewProc("GetTextMetricsW") _getWindowDC = user32.NewProc("GetWindowDC") _releaseDC = user32.NewProc("ReleaseDC") @@ -79,6 +80,12 @@ func (s *sysData) preferredSize() (width int, height int) { panic(fmt.Errorf("error getting DC for preferred size calculations: %v", err)) } dc = _HANDLE(r1) + r1, _, err = _selectObject.Call( + uintptr(dc), + uintptr(unsafe.Pointer(*classTypes[s.ctype].font))) + if r1 == 0 { // failure + panic(fmt.Errorf("error loading control font into device context for preferred size calculation: %v", err)) + } r1, _, err = _getTextMetrics.Call( uintptr(dc), uintptr(unsafe.Pointer(&tm))) diff --git a/sysdata_windows.go b/sysdata_windows.go index 978cbb3..6093f12 100644 --- a/sysdata_windows.go +++ b/sysdata_windows.go @@ -107,11 +107,13 @@ var classTypes = [nctypes]*classData{ name: _PROGRESS_CLASS, style: _PBS_SMOOTH | controlstyle, xstyle: 0 | controlxstyle, + font: &controlFont, }, c_area: &classData{ register: registerAreaWndClass, style: areastyle, xstyle: areaxstyle, + font: &controlFont, }, } @@ -192,6 +194,8 @@ func (s *sysData) make(window *sysData) (err error) { ret: ret, } <-ret + } else if s.ctype != c_window { + panic(fmt.Errorf("internal ui package error: control type %d does not have a font (needed for preferred size calculations)", s.ctype)) } return nil } diff --git a/todo.md b/todo.md index cdf835f..8d11830 100644 --- a/todo.md +++ b/todo.md @@ -36,8 +36,9 @@ important things: - NSComboBox scans the entered text to see if it matches one of the items and returns the index of that item if it does; find out how to suppress this so that it returns -1 unless the item was chosen from the list (like the other platforms) - some Cocoa controls don't seem to resize correctly: Buttons have space around the edges and don't satisfy stretchiness - make sure GTK+ documentation version point differences (x in 4.3.x) don't matter -- button sizes and LineEdit sizes on Windows seem too big; Comboboxes have margins - - Cocoa has similar margining issues (like on Comboboxes) +- LineEdit heights on Windows seem too big; either that or LineEdit and Button text is not vertically centered properly + - are Checkboxes too small? + - Cocoa has similar margining issues (like Comboboxes having margins) - sometimes the size of the drop-down part of a Combobox becomes 0 or 1 or some other impossibly small value on Windows - make gcc (Unix)/clang (Mac OS X) pedantic about warnings/errors; also -Werror - make sure scrollbars in Listbox work identically on all platforms (specifically the existence and autohiding of both horizontal and vertical scrollbars)