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:
parent
bea4df1abf
commit
13bcf728ba
|
@ -3,29 +3,34 @@
|
||||||
#include "winapi_windows.h"
|
#include "winapi_windows.h"
|
||||||
#include "_cgo_export.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;
|
HDC dc;
|
||||||
|
HFONT prevFont;
|
||||||
|
TEXTMETRICW tm;
|
||||||
|
|
||||||
|
if (baseUnitsCalculated)
|
||||||
|
return;
|
||||||
dc = GetDC(hwnd);
|
dc = GetDC(hwnd);
|
||||||
if (dc == NULL)
|
if (dc == NULL)
|
||||||
xpanic("error getting DC for preferred size calculations", GetLastError());
|
xpanic("error getting DC for preferred size calculations", GetLastError());
|
||||||
/* TODO save for restoring later */
|
prevFont = (HFONT) SelectObject(dc, controlFont);
|
||||||
if (SelectObject(dc, controlFont) == NULL)
|
if (prevFont == NULL)
|
||||||
xpanic("error loading control font into device context for preferred size calculation", GetLastError());
|
xpanic("error loading control font into device context for preferred size calculation", GetLastError());
|
||||||
return 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 */
|
||||||
void releaseDC(HWND hwnd, HDC dc)
|
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)
|
if (ReleaseDC(hwnd, dc) == 0)
|
||||||
xpanic("error releasing DC for preferred size calculations", GetLastError());
|
xpanic("error releasing DC for preferred size calculations", GetLastError());
|
||||||
}
|
baseUnitsCalculated = TRUE;
|
||||||
|
|
||||||
void getTextMetricsW(HDC dc, TEXTMETRICW *tm)
|
|
||||||
{
|
|
||||||
if (GetTextMetricsW(dc, tm) == 0)
|
|
||||||
xpanic("error getting text metrics for preferred size calculations", GetLastError());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void moveWindow(HWND hwnd, int x, int y, int width, int height)
|
void moveWindow(HWND hwnd, int x, int y, int width, int height)
|
||||||
|
|
|
@ -21,20 +21,13 @@ const (
|
||||||
paddingDialogUnits = 4
|
paddingDialogUnits = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
// only windows are containers, so only windows get beginResize()
|
func (c *container) beginResize() (d *sizing) {
|
||||||
func (w *window) beginResize() (d *sizing) {
|
|
||||||
d = new(sizing)
|
d = new(sizing)
|
||||||
|
|
||||||
dc := C.getDC(w.hwnd)
|
d.baseX = int(C.baseX)
|
||||||
defer C.releaseDC(w.hwnd, dc)
|
d.baseY = int(C.baseY)
|
||||||
|
|
||||||
var tm C.TEXTMETRICW
|
if spaced {
|
||||||
|
|
||||||
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 {
|
|
||||||
d.xmargin = int(C.MulDiv(marginDialogUnits, C.int(d.baseX), 4))
|
d.xmargin = int(C.MulDiv(marginDialogUnits, C.int(d.baseX), 4))
|
||||||
d.ymargin = int(C.MulDiv(marginDialogUnits, C.int(d.baseY), 8))
|
d.ymargin = int(C.MulDiv(marginDialogUnits, C.int(d.baseY), 8))
|
||||||
d.xpadding = int(C.MulDiv(paddingDialogUnits, C.int(d.baseX), 4))
|
d.xpadding = int(C.MulDiv(paddingDialogUnits, C.int(d.baseX), 4))
|
||||||
|
|
|
@ -63,9 +63,10 @@ extern HFONT statusbarFont;
|
||||||
extern DWORD initWindows(char **);
|
extern DWORD initWindows(char **);
|
||||||
|
|
||||||
/* sizing_windows.c */
|
/* sizing_windows.c */
|
||||||
extern HDC getDC(HWND);
|
extern BOOL baseUnitsCalculated;
|
||||||
extern void releaseDC(HWND, HDC);
|
extern int baseX;
|
||||||
extern void getTextMetricsW(HDC, TEXTMETRICW *);
|
extern int baseY;
|
||||||
|
extern void calculateBaseUnits(HWND);
|
||||||
extern void moveWindow(HWND, int, int, int, int);
|
extern void moveWindow(HWND, int, int, int, int);
|
||||||
|
|
||||||
/* window_windows.c */
|
/* window_windows.c */
|
||||||
|
|
|
@ -73,6 +73,7 @@ HWND newWindow(LPCWSTR title, int width, int height, void *data)
|
||||||
NULL, NULL, hInstance, data);
|
NULL, NULL, hInstance, data);
|
||||||
if (hwnd == NULL)
|
if (hwnd == NULL)
|
||||||
xpanic("Window creation failed", GetLastError());
|
xpanic("Window creation failed", GetLastError());
|
||||||
|
calculateBaseUnits(hwnd);
|
||||||
return hwnd;
|
return hwnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -96,7 +96,6 @@ func storeWindowHWND(data unsafe.Pointer, hwnd C.HWND) {
|
||||||
//export windowResize
|
//export windowResize
|
||||||
func windowResize(data unsafe.Pointer, r *C.RECT) {
|
func windowResize(data unsafe.Pointer, r *C.RECT) {
|
||||||
w := (*window)(data)
|
w := (*window)(data)
|
||||||
w.container.d = w.beginResize()
|
|
||||||
w.resize(int(r.right - r.left), int(r.bottom - r.top))
|
w.resize(int(r.right - r.left), int(r.bottom - r.top))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue