Made the previous commit's changes on Windows. If the Stack Overflow question tells me I'm wrong, we can deal with it then. But yay simple sizing code again :D

This commit is contained in:
Pietro Gagliardi 2014-07-28 14:43:34 -04:00
parent bea4df1abf
commit 13bcf728ba
5 changed files with 28 additions and 29 deletions

View File

@ -3,29 +3,34 @@
#include "winapi_windows.h"
#include "_cgo_export.h"
HDC getDC(HWND hwnd)
BOOL baseUnitsCalculated = FALSE;
int baseX;
int baseY;
/* called by newWindow() so we can calculate base units when we have a window */
void calculateBaseUnits(HWND hwnd)
{
HDC dc;
HFONT prevFont;
TEXTMETRICW tm;
if (baseUnitsCalculated)
return;
dc = GetDC(hwnd);
if (dc == NULL)
xpanic("error getting DC for preferred size calculations", GetLastError());
/* TODO save for restoring later */
if (SelectObject(dc, controlFont) == NULL)
prevFont = (HFONT) SelectObject(dc, controlFont);
if (prevFont == NULL)
xpanic("error loading control font into device context for preferred size calculation", GetLastError());
return dc;
}
void releaseDC(HWND hwnd, HDC dc)
{
if (GetTextMetricsW(dc, &tm) == 0)
xpanic("error getting text metrics for preferred size calculations", GetLastError());
baseX = (int) tm.tmAveCharWidth; /* TODO not optimal; third reference below has better way */
baseY = (int) tm.tmHeight;
if (SelectObject(dc, prevFont) != controlFont)
xpanic("error restoring previous font into device context after preferred size calculations", GetLastError());
if (ReleaseDC(hwnd, dc) == 0)
xpanic("error releasing DC for preferred size calculations", GetLastError());
}
void getTextMetricsW(HDC dc, TEXTMETRICW *tm)
{
if (GetTextMetricsW(dc, tm) == 0)
xpanic("error getting text metrics for preferred size calculations", GetLastError());
baseUnitsCalculated = TRUE;
}
void moveWindow(HWND hwnd, int x, int y, int width, int height)

View File

@ -21,20 +21,13 @@ const (
paddingDialogUnits = 4
)
// only windows are containers, so only windows get beginResize()
func (w *window) beginResize() (d *sizing) {
func (c *container) beginResize() (d *sizing) {
d = new(sizing)
dc := C.getDC(w.hwnd)
defer C.releaseDC(w.hwnd, dc)
d.baseX = int(C.baseX)
d.baseY = int(C.baseY)
var tm C.TEXTMETRICW
C.getTextMetricsW(dc, &tm)
d.baseX = int(tm.tmAveCharWidth) // TODO not optimal; third reference below has better way
d.baseY = int(tm.tmHeight)
if w.spaced {
if spaced {
d.xmargin = int(C.MulDiv(marginDialogUnits, C.int(d.baseX), 4))
d.ymargin = int(C.MulDiv(marginDialogUnits, C.int(d.baseY), 8))
d.xpadding = int(C.MulDiv(paddingDialogUnits, C.int(d.baseX), 4))

View File

@ -63,9 +63,10 @@ extern HFONT statusbarFont;
extern DWORD initWindows(char **);
/* sizing_windows.c */
extern HDC getDC(HWND);
extern void releaseDC(HWND, HDC);
extern void getTextMetricsW(HDC, TEXTMETRICW *);
extern BOOL baseUnitsCalculated;
extern int baseX;
extern int baseY;
extern void calculateBaseUnits(HWND);
extern void moveWindow(HWND, int, int, int, int);
/* window_windows.c */

View File

@ -73,6 +73,7 @@ HWND newWindow(LPCWSTR title, int width, int height, void *data)
NULL, NULL, hInstance, data);
if (hwnd == NULL)
xpanic("Window creation failed", GetLastError());
calculateBaseUnits(hwnd);
return hwnd;
}

View File

@ -96,7 +96,6 @@ func storeWindowHWND(data unsafe.Pointer, hwnd C.HWND) {
//export windowResize
func windowResize(data unsafe.Pointer, r *C.RECT) {
w := (*window)(data)
w.container.d = w.beginResize()
w.resize(int(r.right - r.left), int(r.bottom - r.top))
}