From 35342e44e7ea28aef20708906675129c552f4484 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 7 Apr 2015 14:42:52 -0400 Subject: [PATCH] Added the uiDarwinControl logic. --- newcontrol_darwin.c | 99 +++++++++++++++++++++++++++++++++++++++++++++ ui_darwin.h | 17 ++++++++ uipriv_darwin.h | 1 + 3 files changed, 117 insertions(+) create mode 100644 newcontrol_darwin.c create mode 100644 ui_darwin.h diff --git a/newcontrol_darwin.c b/newcontrol_darwin.c new file mode 100644 index 00000000..66a7ecc4 --- /dev/null +++ b/newcontrol_darwin.c @@ -0,0 +1,99 @@ +// 7 april 2015 +#include "uipriv_darwin.h" + +typedef struct uiSingleWidgetControl uiSingleWidgetControl; + +struct uiSingleViewControl { + uiControl control; + NSView *control; + NSScrollView *scrollView; + NSView *immediate; // the control that is added to the parent container; either control or scrollView + void *data; +}; + +#define S(c) ((uiSingleViewControl *) (c)) + +static uintptr_t singleHandle(uiControl *c) +{ + return (uintptr_t) (S(c)->control); +} + +static void singleSetParent(uiControl *c, uintptr_t parent) +{ + NSView *parentView = (NSView *) parent; + + [parentView addSubview:S(c)->immediate]; +} + +static uiSize singlePreferredSize(uiControl *c, uiSizing *d) +{ + uiSize size; + + // TODO + size.width = size.height = 0; + return size; +} + +static void singleResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d) +{ + NSRect frame; + + frame.origin.x = x; + frame.origin.y = y; + frame.size.width = width; + frame.size.height = height; + frame = [S(c)->immediate frameForAlignmentRect:frame]; + [S(c)->immediate setFrame:frame]; +} + +static void singleContainerShow(uiControl *c) +{ + [S(c)->immediate setHidden:NO]; +} + +static void singleContainerHide(uiControl *c) +{ + [S(c)->immediate setHidden:YES]; +} + +// TODO connect free function + +uiControl *uiDarwinNewControl(Class class, BOOL inScrollView, BOOL scrollViewHasBorder, void *data) +{ + uiSingleViewControl *c; + + c = g_new0(uiSingleViewControl, 1); + // thanks to autoxr and arwyn in irc.freenode.net/#macdev + c->widget = (NSView *) [[class alloc] initWithFrame:NSZeroRect]; + c->immediate = c->control; + + // TODO turn into bit field? + if (inScrollView) { + c->scrollView = [[NSScrollView alloc] initWithFrame:NSZeroRect]; + [c->scrollView setDocumentView:c->control]; + [c->scrollView setHasHorizontalScroller:YES]; + [c->scrollView setHasVerticalScroller:YES]; + [c->scrollView setAutohidesScrollers:YES]; + if (scrollViewHasBorder) + [c->scrollView setBorderType:NSBezelBorder]; + else + [c->scrollView setBorderType:NSNoBorder]; + c->immediate = (NSView *) (c->scrolledWindow); + } + + c->control.handle = singleHandle; + c->control.setParent = singleSetParent; + c->control.preferredSize = singlePreferredSize; + c->control.resize = singleResize; + c->control.containerShow = singleContainerShow; + c->control.containerHide = singleContainerHide; + + c->data = data; + + return (uiControl *) c; +} + +void *uiDarwinControlData(uiControl *c) +{ + return S(c)->data; +} diff --git a/ui_darwin.h b/ui_darwin.h new file mode 100644 index 00000000..0e7afb2a --- /dev/null +++ b/ui_darwin.h @@ -0,0 +1,17 @@ +// 7 april 2015 + +/* +This file assumes that you have imported and "ui.h" beforehand. It provides API-specific functions for interfacing with foreign controls on Mac OS X. +*/ + +#ifndef __UI_UI_DARWIN_H__ +#define __UI_UI_DARWIN_H__ + +// uiDarwinNewControl() creates a new uiControl with the given Cocoa control inside. +// The first parameter should come from [RealControlType class]. +// The two scrollView parameters allow placing scrollbars on the new control. +// The data parameter can be accessed with uiDarwinControlData(). +extern uiControl *uiDarwinNewControl(Class class, gboolean inScrollView, gboolean scrollViewHasBorder, void *data); +extern void *uiDarwinControlData(uiControl *c); + +#endif diff --git a/uipriv_darwin.h b/uipriv_darwin.h index 60b3893b..191355f0 100644 --- a/uipriv_darwin.h +++ b/uipriv_darwin.h @@ -3,6 +3,7 @@ #define MAC_OS_X_VERSION_MAX_ALLOWED MAC_OS_X_VERSION_10_7 #import #import "uipriv.h" +#import "ui_darwin.h" #define toNSString(str) [NSString stringWithUTF8String:(str)]