More uiWindow positioning refinement and implementation on OS X.
This commit is contained in:
parent
4465d37d2e
commit
53bec81925
|
@ -9,12 +9,16 @@ struct uiWindow {
|
||||||
int (*onClosing)(uiWindow *, void *);
|
int (*onClosing)(uiWindow *, void *);
|
||||||
void *onClosingData;
|
void *onClosingData;
|
||||||
struct singleChildConstraints constraints;
|
struct singleChildConstraints constraints;
|
||||||
|
void (*onPositionChanged)(uiWindow *, void *);
|
||||||
|
void *onPositionChangedData;
|
||||||
|
BOOL suppressPositionChanged;
|
||||||
};
|
};
|
||||||
|
|
||||||
@interface windowDelegateClass : NSObject<NSWindowDelegate> {
|
@interface windowDelegateClass : NSObject<NSWindowDelegate> {
|
||||||
struct mapTable *windows;
|
struct mapTable *windows;
|
||||||
}
|
}
|
||||||
- (BOOL)windowShouldClose:(id)sender;
|
- (BOOL)windowShouldClose:(id)sender;
|
||||||
|
- (void)windowDidMove:(NSNotification *)note;
|
||||||
- (void)registerWindow:(uiWindow *)w;
|
- (void)registerWindow:(uiWindow *)w;
|
||||||
- (void)unregisterWindow:(uiWindow *)w;
|
- (void)unregisterWindow:(uiWindow *)w;
|
||||||
- (uiWindow *)lookupWindow:(NSWindow *)w;
|
- (uiWindow *)lookupWindow:(NSWindow *)w;
|
||||||
|
@ -47,6 +51,16 @@ struct uiWindow {
|
||||||
return NO;
|
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
|
- (void)registerWindow:(uiWindow *)w
|
||||||
{
|
{
|
||||||
mapSet(self->windows, w->window, w);
|
mapSet(self->windows, w->window, w);
|
||||||
|
@ -204,6 +218,47 @@ void uiWindowSetTitle(uiWindow *w, const char *title)
|
||||||
[w->window setTitle:toNSString(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)
|
void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data)
|
||||||
{
|
{
|
||||||
w->onClosing = f;
|
w->onClosing = f;
|
||||||
|
@ -245,6 +300,11 @@ static int defaultOnClosing(uiWindow *w, void *data)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void defaultOnPositionChanged(uiWindow *w, void *data)
|
||||||
|
{
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
|
uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
|
||||||
{
|
{
|
||||||
uiWindow *w;
|
uiWindow *w;
|
||||||
|
@ -269,6 +329,7 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar)
|
||||||
}
|
}
|
||||||
[windowDelegate registerWindow:w];
|
[windowDelegate registerWindow:w];
|
||||||
uiWindowOnClosing(w, defaultOnClosing, NULL);
|
uiWindowOnClosing(w, defaultOnClosing, NULL);
|
||||||
|
uiWindowOnPositionChanged(w, defaultOnPositionChanged, NULL);
|
||||||
|
|
||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,52 +1,76 @@
|
||||||
// 15 june 2016
|
// 15 june 2016
|
||||||
#include "test.h"
|
#include "test.h"
|
||||||
|
|
||||||
void moveX(uiSpinbox *s, void *data)
|
static uiSpinbox *x, *y;
|
||||||
|
|
||||||
|
static void moveX(uiSpinbox *s, void *data)
|
||||||
{
|
{
|
||||||
uiWindow *w = uiWindow(data);
|
uiWindow *w = uiWindow(data);
|
||||||
int x, y;
|
int xp, yp;
|
||||||
|
|
||||||
uiWindowPosition(w, &x, &y);
|
uiWindowPosition(w, &xp, &yp);
|
||||||
x = uiSpinboxValue(s);
|
xp = uiSpinboxValue(x);
|
||||||
uiWindowSetPosition(w, x, y);
|
uiWindowSetPosition(w, xp, yp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void moveX(uiSpinbox *s, void *data)
|
static void moveY(uiSpinbox *s, void *data)
|
||||||
{
|
{
|
||||||
uiWindow *w = uiWindow(data);
|
uiWindow *w = uiWindow(data);
|
||||||
int x, y;
|
int xp, yp;
|
||||||
|
|
||||||
uiWindowPosition(w, &x, &y);
|
uiWindowPosition(w, &xp, &yp);
|
||||||
y = uiSpinboxValue(s);
|
yp = uiSpinboxValue(y);
|
||||||
uiWindowSetPosition(w, x, 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 *makePage15(uiWindow *w)
|
||||||
{
|
{
|
||||||
uiBox *page15;
|
uiBox *page15;
|
||||||
uiBox *hbox;
|
uiBox *hbox;
|
||||||
uiSpinbox *x;
|
uiButton *button;
|
||||||
uiSpinbox *y;
|
|
||||||
int curx, cury;
|
|
||||||
|
|
||||||
page15 = newVerticalBox();
|
page15 = newVerticalBox();
|
||||||
|
|
||||||
hbox = newHorizontalBox();
|
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);
|
uiBoxAppend(hbox, uiControl(uiNewLabel("Position")), 0);
|
||||||
x = uiNewSpinbox(INT_MIN, INT_MAX);
|
x = uiNewSpinbox(INT_MIN, INT_MAX);
|
||||||
uiBoxAppend(hbox, uiControl(x), 1);
|
uiBoxAppend(hbox, uiControl(x), 1);
|
||||||
y = uiNewSpinbox(INT_MIN, INT_MAX);
|
y = uiNewSpinbox(INT_MIN, INT_MAX);
|
||||||
uiBoxAppend(hbox, uiControl(y), 1);
|
uiBoxAppend(hbox, uiControl(y), 1);
|
||||||
|
button = uiNewButton("Center");
|
||||||
|
uiBoxAppend(hbox, uiControl(button), 0);
|
||||||
|
|
||||||
uiSpinboxOnChanged(x, moveX, w);
|
uiSpinboxOnChanged(x, moveX, w);
|
||||||
uiSpinboxOnChanged(y, moveY, w);
|
uiSpinboxOnChanged(y, moveY, w);
|
||||||
uiWindowPosition(w, &curX, &curY);
|
uiButtonOnClicked(button, center, w);
|
||||||
uiSpinboxSetValue(x, curX);
|
uiWindowOnPositionChanged(w, onMove, NULL);
|
||||||
uiSpinboxSetValue(y, curY);
|
update(w);
|
||||||
|
|
||||||
return page15;
|
return page15;
|
||||||
}
|
}
|
||||||
|
|
2
ui.h
2
ui.h
|
@ -101,6 +101,8 @@ _UI_EXTERN char *uiWindowTitle(uiWindow *w);
|
||||||
_UI_EXTERN void uiWindowSetTitle(uiWindow *w, const char *title);
|
_UI_EXTERN void uiWindowSetTitle(uiWindow *w, const char *title);
|
||||||
_UI_EXTERN void uiWindowPosition(uiWindow *w, int *x, int *y);
|
_UI_EXTERN void uiWindowPosition(uiWindow *w, int *x, int *y);
|
||||||
_UI_EXTERN void uiWindowSetPosition(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 uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *w, void *data), void *data);
|
||||||
_UI_EXTERN void uiWindowSetChild(uiWindow *w, uiControl *child);
|
_UI_EXTERN void uiWindowSetChild(uiWindow *w, uiControl *child);
|
||||||
_UI_EXTERN int uiWindowMargined(uiWindow *w);
|
_UI_EXTERN int uiWindowMargined(uiWindow *w);
|
||||||
|
|
Loading…
Reference in New Issue