From 0356d0fd7046071c1320db7340a14df54c79cb0f Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Fri, 1 Aug 2014 23:36:13 -0400 Subject: Migrated the Windows backend to use sizer. --- redo/containerctrls_windows.go | 17 +++++----- redo/sizer_windows.go | 72 ++++++++++++++++++++++++++++++++++++++++++ redo/sizing_windows.c | 2 ++ redo/sizing_windows.go | 72 ------------------------------------------ redo/window_windows.go | 4 +-- 5 files changed, 85 insertions(+), 82 deletions(-) create mode 100644 redo/sizer_windows.go delete mode 100644 redo/sizing_windows.go (limited to 'redo') diff --git a/redo/containerctrls_windows.go b/redo/containerctrls_windows.go index e81c340..1aa2c57 100644 --- a/redo/containerctrls_windows.go +++ b/redo/containerctrls_windows.go @@ -19,7 +19,7 @@ TODO type tab struct { *controlbase - tabs []*container + tabs []*sizer supersetParent func(p *controlParent) superallocate func(x int, y int, width int, height int, d *sizing) []*allocation } @@ -48,15 +48,15 @@ func (t *tab) tabsetParent(p *controlParent) { } func (t *tab) Append(name string, control Control) { - c := new(container) - t.tabs = append(t.tabs, c) - c.child = control + s := new(sizer) + t.tabs = append(t.tabs, s) + s.child = control if t.parent != nil { - c.child.setParent(&controlParent{t.parent}) + s.child.setParent(&controlParent{t.parent}) } // initially hide tab 1..n controls; if we don't, they'll appear over other tabs, resulting in weird behavior if len(t.tabs) != 1 { - c.child.containerHide() + s.child.containerHide() } C.tabAppend(t.hwnd, toUTF16(name)) } @@ -74,6 +74,7 @@ func tabChanged(data unsafe.Pointer, new C.LRESULT) { } // a tab control contains other controls; size appropriately +// TODO change this to commitResize() func (t *tab) taballocate(x int, y int, width int, height int, d *sizing) []*allocation { var r C.RECT @@ -85,9 +86,9 @@ func (t *tab) taballocate(x int, y int, width int, height int, d *sizing) []*all C.tabGetContentRect(t.hwnd, &r) // and allocate // don't allocate to just the current tab; allocate to all tabs! - for _, c := range t.tabs { + for _, s := range t.tabs { // because each widget is actually a child of the Window, the origin is the one we calculated above - c.resize(int(r.left), int(r.top), int(r.right - r.left), int(r.bottom - r.top)) + s.resize(int(r.left), int(r.top), int(r.right - r.left), int(r.bottom - r.top)) } // and now allocate the tab control itself return t.superallocate(x, y, width, height, d) diff --git a/redo/sizer_windows.go b/redo/sizer_windows.go new file mode 100644 index 0000000..554b285 --- /dev/null +++ b/redo/sizer_windows.go @@ -0,0 +1,72 @@ +// 24 february 2014 + +package ui + +// #include "winapi_windows.h" +import "C" + +// For Windows, Microsoft just hands you a list of preferred control sizes as part of the MSDN documentation and tells you to roll with it. +// These sizes are given in "dialog units", which are independent of the font in use. +// We need to convert these into standard pixels, which requires we get the device context of the OS window. +// References: +// - http://msdn.microsoft.com/en-us/library/ms645502%28VS.85%29.aspx - the calculation needed +// - http://support.microsoft.com/kb/125681 - to get the base X and Y +// (thanks to http://stackoverflow.com/questions/58620/default-button-size) + +type sizing struct { + sizingbase + + // for size calculations + baseX C.int + baseY C.int + + // for the actual resizing + // possibly the HDWP +} + +// note on MulDiv(): +// div will not be 0 in the usages below +// we also ignore overflow; that isn't likely to happen for our use case anytime soon + +func fromdlgunitsX(du int, d *sizing) int { + return int(C.MulDiv(C.int(du), d.baseX, 4)) +} + +func fromdlgunitsY(du int, d *sizing) int { + return int(C.MulDiv(C.int(du), d.baseY, 8)) +} + +const ( + marginDialogUnits = 7 + paddingDialogUnits = 4 +) + +func (s *sizer) beginResize() (d *sizing) { + d = new(sizing) + + d.baseX = C.baseX + d.baseY = C.baseY + + if spaced { + d.xmargin = fromdlgunitsX(marginDialogUnits, d) + d.ymargin = fromdlgunitsY(marginDialogUnits, d) + d.xpadding = fromdlgunitsX(paddingDialogUnits, d) + d.ypadding = fromdlgunitsY(paddingDialogUnits, d) + } + + return d +} + +func (s *sizer) translateAllocationCoords(allocations []*allocation, winwidth, winheight int) { + // no translation needed on windows +} + +//TODO +/* +func (w *widgetbase) preferredSize(d *sizing) (width int, height int) { + // the preferred size of an Area is its size + if stdDlgSizes[s.ctype].area { + return s.areawidth, s.areaheight + } +} +*/ diff --git a/redo/sizing_windows.c b/redo/sizing_windows.c index e6f27a6..60d57f3 100644 --- a/redo/sizing_windows.c +++ b/redo/sizing_windows.c @@ -3,6 +3,8 @@ #include "winapi_windows.h" #include "_cgo_export.h" +/* TODO rename to sizer_windows.c and move all but the first function to control_windows.c */ + BOOL baseUnitsCalculated = FALSE; int baseX; int baseY; diff --git a/redo/sizing_windows.go b/redo/sizing_windows.go deleted file mode 100644 index 08bb0ab..0000000 --- a/redo/sizing_windows.go +++ /dev/null @@ -1,72 +0,0 @@ -// 24 february 2014 - -package ui - -// #include "winapi_windows.h" -import "C" - -// For Windows, Microsoft just hands you a list of preferred control sizes as part of the MSDN documentation and tells you to roll with it. -// These sizes are given in "dialog units", which are independent of the font in use. -// We need to convert these into standard pixels, which requires we get the device context of the OS window. -// References: -// - http://msdn.microsoft.com/en-us/library/ms645502%28VS.85%29.aspx - the calculation needed -// - http://support.microsoft.com/kb/125681 - to get the base X and Y -// (thanks to http://stackoverflow.com/questions/58620/default-button-size) - -type sizing struct { - sizingbase - - // for size calculations - baseX C.int - baseY C.int - - // for the actual resizing - // possibly the HDWP -} - -// note on MulDiv(): -// div will not be 0 in the usages below -// we also ignore overflow; that isn't likely to happen for our use case anytime soon - -func fromdlgunitsX(du int, d *sizing) int { - return int(C.MulDiv(C.int(du), d.baseX, 4)) -} - -func fromdlgunitsY(du int, d *sizing) int { - return int(C.MulDiv(C.int(du), d.baseY, 8)) -} - -const ( - marginDialogUnits = 7 - paddingDialogUnits = 4 -) - -func (c *container) beginResize() (d *sizing) { - d = new(sizing) - - d.baseX = C.baseX - d.baseY = C.baseY - - if spaced { - d.xmargin = fromdlgunitsX(marginDialogUnits, d) - d.ymargin = fromdlgunitsY(marginDialogUnits, d) - d.xpadding = fromdlgunitsX(paddingDialogUnits, d) - d.ypadding = fromdlgunitsY(paddingDialogUnits, d) - } - - return d -} - -func (c *container) translateAllocationCoords(allocations []*allocation, winwidth, winheight int) { - // no translation needed on windows -} - -//TODO -/* -func (w *widgetbase) preferredSize(d *sizing) (width int, height int) { - // the preferred size of an Area is its size - if stdDlgSizes[s.ctype].area { - return s.areawidth, s.areaheight - } -} -*/ diff --git a/redo/window_windows.go b/redo/window_windows.go index 8c64858..341f226 100644 --- a/redo/window_windows.go +++ b/redo/window_windows.go @@ -17,7 +17,7 @@ type window struct { closing *event - *container + *sizer } const windowclassname = "" @@ -37,7 +37,7 @@ func newWindow(title string, width int, height int, control Control) *window { w := &window{ // hwnd set in WM_CREATE handler closing: newEvent(), - container: new(container), + sizer: new(sizer), } hwnd := C.newWindow(toUTF16(title), C.int(width), C.int(height), unsafe.Pointer(w)) if hwnd != w.hwnd { -- cgit v1.2.3