More uiWindow positioning refinement and implementation on OS X.

This commit is contained in:
Pietro Gagliardi 2016-06-15 14:57:52 -04:00
parent 4465d37d2e
commit 53bec81925
3 changed files with 105 additions and 18 deletions

View File

@ -9,12 +9,16 @@ struct uiWindow {
int (*onClosing)(uiWindow *, void *);
void *onClosingData;
struct singleChildConstraints constraints;
void (*onPositionChanged)(uiWindow *, void *);
void *onPositionChangedData;
BOOL suppressPositionChanged;
};
@interface windowDelegateClass : NSObject<NSWindowDelegate> {
struct mapTable *windows;
}
- (BOOL)windowShouldClose:(id)sender;
- (void)windowDidMove:(NSNotification *)note;
- (void)registerWindow:(uiWindow *)w;
- (void)unregisterWindow:(uiWindow *)w;
- (uiWindow *)lookupWindow:(NSWindow *)w;
@ -47,6 +51,16 @@ struct uiWindow {
return NO;
}
// TODO doesn't happen live
- (void)windowDidMove:(NSNotification *)note
{
uiWindow *w;
w = [self lookupWindow:((NSWindow *) [note object])];
if (!w->suppressPositionChanged)
(*(w->onPositionChanged))(w, w->onPositionChangedData);
}
- (void)registerWindow:(uiWindow *)w
{
mapSet(self->windows, w->window, w);
@ -204,6 +218,47 @@ void uiWindowSetTitle(uiWindow *w, const char *title)
[w->window setTitle:toNSString(title)];
}
void uiWindowPosition(uiWindow *w, int *x, int *y)
{
NSScreen *screen;
NSRect r;
r = [w->window frame];
*x = r.origin.x;
// this is the right screen to use; thanks mikeash in irc.freenode.net/#macdev
// -mainScreen is useless for positioning (it's just the key window's screen)
// and we use -frame, not -visibleFrame, for dealing with absolute positions
screen = (NSScreen *) [[NSScreen screens] objectAtIndex:0];
*y = ([screen frame].size.height - r.origin.y) - r.size.height;
}
void uiWindowSetPosition(uiWindow *w, int x, int y)
{
// -[NSWindow setFrameTopLeftPoint:] is acting weird so...
NSRect r;
NSScreen *screen;
// this fires windowDidMove:
w->suppressPositionChanged = YES;
r = [w->window frame];
r.origin.x = x;
screen = (NSScreen *) [[NSScreen screens] objectAtIndex:0];
r.origin.y = [screen frame].size.height - (y + r.size.height);
[w->window setFrameOrigin:r.origin];
w->suppressPositionChanged = NO;
}
void uiWindowCenter(uiWindow *w)
{
[w->window center];
}
void uiWindowOnPositionChanged(uiWindow *w, void (*f)(uiWindow *, void *), void *data)
{
w->onPositionChanged = f;
w->onPositionChangedData = data;
}
void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data)
{
w->onClosing = f;
@ -245,6 +300,11 @@ static int defaultOnClosing(uiWindow *w, void *data)
return 0;
}
static void defaultOnPositionChanged(uiWindow *w, void *data)
{
// do nothing
}
uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
{
uiWindow *w;
@ -269,6 +329,7 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
}
[windowDelegate registerWindow:w];
uiWindowOnClosing(w, defaultOnClosing, NULL);
uiWindowOnPositionChanged(w, defaultOnPositionChanged, NULL);
return w;
}

View File

@ -1,52 +1,76 @@
// 15 june 2016
#include "test.h"
void moveX(uiSpinbox *s, void *data)
static uiSpinbox *x, *y;
static void moveX(uiSpinbox *s, void *data)
{
uiWindow *w = uiWindow(data);
int x, y;
int xp, yp;
uiWindowPosition(w, &x, &y);
x = uiSpinboxValue(s);
uiWindowSetPosition(w, x, y);
uiWindowPosition(w, &xp, &yp);
xp = uiSpinboxValue(x);
uiWindowSetPosition(w, xp, yp);
}
void moveX(uiSpinbox *s, void *data)
static void moveY(uiSpinbox *s, void *data)
{
uiWindow *w = uiWindow(data);
int x, y;
int xp, yp;
uiWindowPosition(w, &x, &y);
y = uiSpinboxValue(s);
uiWindowSetPosition(w, x, y);
uiWindowPosition(w, &xp, &yp);
yp = uiSpinboxValue(y);
uiWindowSetPosition(w, xp, yp);
}
// TODO onMove
static void update(uiWindow *w)
{
int xp, yp;
uiWindowPosition(w, &xp, &yp);
uiSpinboxSetValue(x, xp);
uiSpinboxSetValue(y, yp);
}
static void center(uiButton *b, void *data)
{
uiWindow *w = uiWindow(data);
uiWindowCenter(w);
update(w);
}
void onMove(uiWindow *w, void *data)
{
printf("move\n");
update(w);
}
uiBox *makePage15(uiWindow *w)
{
uiBox *page15;
uiBox *hbox;
uiSpinbox *x;
uiSpinbox *y;
int curx, cury;
uiButton *button;
page15 = newVerticalBox();
hbox = newHorizontalBox();
uiBoxAppend(page15, uiControl(hbox), 1);
// TODO if I make this 1 and not add anything else, on OS X the box won't be able to grow vertically
uiBoxAppend(page15, uiControl(hbox), 0);
uiBoxAppend(hbox, uiControl(uiNewLabel("Position")), 0);
x = uiNewSpinbox(INT_MIN, INT_MAX);
uiBoxAppend(hbox, uiControl(x), 1);
y = uiNewSpinbox(INT_MIN, INT_MAX);
uiBoxAppend(hbox, uiControl(y), 1);
button = uiNewButton("Center");
uiBoxAppend(hbox, uiControl(button), 0);
uiSpinboxOnChanged(x, moveX, w);
uiSpinboxOnChanged(y, moveY, w);
uiWindowPosition(w, &curX, &curY);
uiSpinboxSetValue(x, curX);
uiSpinboxSetValue(y, curY);
uiButtonOnClicked(button, center, w);
uiWindowOnPositionChanged(w, onMove, NULL);
update(w);
return page15;
}

2
ui.h
View File

@ -101,6 +101,8 @@ _UI_EXTERN char *uiWindowTitle(uiWindow *w);
_UI_EXTERN void uiWindowSetTitle(uiWindow *w, const char *title);
_UI_EXTERN void uiWindowPosition(uiWindow *w, int *x, int *y);
_UI_EXTERN void uiWindowSetPosition(uiWindow *w, int x, int y);
_UI_EXTERN void uiWindowCenter(uiWindow *w);
_UI_EXTERN void uiWindowOnPositionChanged(uiWindow *w, void (*f)(uiWindow *, void *), void *data);
_UI_EXTERN void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *w, void *data), void *data);
_UI_EXTERN void uiWindowSetChild(uiWindow *w, uiControl *child);
_UI_EXTERN int uiWindowMargined(uiWindow *w);