Migrated the Mac OS X code to the new uiControl/uiSizing system.

This commit is contained in:
Pietro Gagliardi 2015-04-09 18:39:21 -04:00
parent 22db738dd1
commit e752356037
3 changed files with 48 additions and 40 deletions

View File

@ -1,32 +1,33 @@
// 7 april 2015 // 7 april 2015
#include "uipriv_darwin.h" #include "uipriv_darwin.h"
typedef struct uiSingleViewControl uiSingleViewControl; typedef struct singleView singleView;
struct uiSingleViewControl { struct singleView {
uiControl control;
NSView *view; NSView *view;
NSScrollView *scrollView; NSScrollView *scrollView;
NSView *immediate; // the control that is added to the parent container; either view or scrollView NSView *immediate; // the control that is added to the parent container; either view or scrollView
uintptr_t parent; uintptr_t parent;
}; };
#define S(c) ((uiSingleViewControl *) (c))
// TODO this will need to change if we want to provide removal // TODO this will need to change if we want to provide removal
static void singleDestroy(uiControl *c) static void singleDestroy(uiControl *c)
{ {
[S(c)->view removeFromSuperview]; singleView *s = (singleView *) (c->internal);
[s->view removeFromSuperview];
} }
static uintptr_t singleHandle(uiControl *c) static uintptr_t singleHandle(uiControl *c)
{ {
return (uintptr_t) (S(c)->view); singleView *s = (singleView *) (c->internal);
return (uintptr_t) (s->view);
} }
static void singleSetParent(uiControl *c, uintptr_t parent) static void singleSetParent(uiControl *c, uintptr_t parent)
{ {
uiSingleViewControl *s = S(c); singleView *s = (singleView *) (c->internal);
NSView *parentView; NSView *parentView;
s->parent = parent; s->parent = parent;
@ -37,7 +38,7 @@ static void singleSetParent(uiControl *c, uintptr_t parent)
static void singleRemoveParent(uiControl *c) static void singleRemoveParent(uiControl *c)
{ {
uiSingleViewControl *s = S(c); singleView *s = (singleView *) (c->internal);
uintptr_t oldparent; uintptr_t oldparent;
oldparent = s->parent; oldparent = s->parent;
@ -47,69 +48,75 @@ static void singleRemoveParent(uiControl *c)
} }
// also good for NSBox and NSProgressIndicator // also good for NSBox and NSProgressIndicator
static uiSize singlePreferredSize(uiControl *c, uiSizing *d) static void singlePreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
{ {
uiSize size; singleView *s = (singleView *) (c->internal);
NSControl *control; NSControl *control;
NSRect r; NSRect r;
control = (NSControl *) (S(c)->view); control = (NSControl *) (s->view);
[control sizeToFit]; [control sizeToFit];
// use alignmentRect here instead of frame because we'll be resizing based on that // use alignmentRect here instead of frame because we'll be resizing based on that
r = [control alignmentRectForFrame:[control frame]]; r = [control alignmentRectForFrame:[control frame]];
size.width = (intmax_t) r.size.width; *width = (intmax_t) r.size.width;
size.height = (intmax_t) r.size.height; *height = (intmax_t) r.size.height;
return size;
} }
static void singleResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d) static void singleResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
{ {
singleView *s = (singleView *) (c->internal);
NSRect frame; NSRect frame;
frame.origin.x = x; frame.origin.x = x;
// mac os x coordinate system has (0,0) in the lower-left // mac os x coordinate system has (0,0) in the lower-left
frame.origin.y = ([[S(c)->immediate superview] bounds].size.height - height) - y; frame.origin.y = ([[s->immediate superview] bounds].size.height - height) - y;
frame.size.width = width; frame.size.width = width;
frame.size.height = height; frame.size.height = height;
frame = [S(c)->immediate frameForAlignmentRect:frame]; frame = [s->immediate frameForAlignmentRect:frame];
[S(c)->immediate setFrame:frame]; [s->immediate setFrame:frame];
} }
uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder) uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder)
{ {
uiSingleViewControl *c; uiControl *c;
singleView *s;
c = uiNew(uiSingleViewControl); s = uiNew(singleView);
// thanks to autoxr and arwyn in irc.freenode.net/#macdev // thanks to autoxr and arwyn in irc.freenode.net/#macdev
c->view = (NSView *) [[class alloc] initWithFrame:NSZeroRect]; s->view = (NSView *) [[class alloc] initWithFrame:NSZeroRect];
c->immediate = c->view; s->immediate = s->view;
if (inScrollView) { if (inScrollView) {
c->scrollView = [[NSScrollView alloc] initWithFrame:NSZeroRect]; s->scrollView = [[NSScrollView alloc] initWithFrame:NSZeroRect];
[c->scrollView setDocumentView:c->view]; [s->scrollView setDocumentView:s->view];
[c->scrollView setHasHorizontalScroller:YES]; [s->scrollView setHasHorizontalScroller:YES];
[c->scrollView setHasVerticalScroller:YES]; [s->scrollView setHasVerticalScroller:YES];
[c->scrollView setAutohidesScrollers:YES]; [s->scrollView setAutohidesScrollers:YES];
if (scrollViewHasBorder) if (scrollViewHasBorder)
[c->scrollView setBorderType:NSBezelBorder]; [s->scrollView setBorderType:NSBezelBorder];
else else
[c->scrollView setBorderType:NSNoBorder]; [s->scrollView setBorderType:NSNoBorder];
c->immediate = (NSView *) (c->scrollView); s->immediate = (NSView *) (s->scrollView);
} }
c->control.destroy = singleDestroy; c = uiNew(uiControl);
c->control.handle = singleHandle; c->internal = s;
c->control.setParent = singleSetParent; c->destroy = singleDestroy;
c->control.removeParent = singleRemoveParent; c->handle = singleHandle;
c->control.preferredSize = singlePreferredSize; c->setParent = singleSetParent;
c->control.resize = singleResize; c->removeParent = singleRemoveParent;
c->preferredSize = singlePreferredSize;
c->resize = singleResize;
return (uiControl *) c; return c;
} }
BOOL uiDarwinControlFreeWhenAppropriate(uiControl *c, NSView *newSuperview) BOOL uiDarwinControlFreeWhenAppropriate(uiControl *c, NSView *newSuperview)
{ {
singleView *s = (singleView *) (c->internal);
if (newSuperview == nil) { if (newSuperview == nil) {
uiFree(s);
uiFree(c); uiFree(c);
return YES; return YES;
} }

View File

@ -15,4 +15,8 @@ This file assumes that you have imported <Cocoa/Cocoa.h> and "ui.h" beforehand.
extern uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder); extern uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder);
extern BOOL uiDarwinControlFreeWhenAppropriate(uiControl *c, NSView *newSuperview); extern BOOL uiDarwinControlFreeWhenAppropriate(uiControl *c, NSView *newSuperview);
struct uiSizingSys {
// this structure currently left blank
};
#endif #endif

View File

@ -32,9 +32,6 @@
extern void setStandardControlFont(NSControl *); extern void setStandardControlFont(NSControl *);
// container_darwin.m // container_darwin.m
struct uiSizing {
uiSizingCommon
};
@interface uiContainer : NSView @interface uiContainer : NSView
// TODO rename to uiChild // TODO rename to uiChild
@property uiControl *child; @property uiControl *child;