ui/cocoa/container.m

changeset 846
ffa983c223c1
parent 811
1391ba7e533f
child 847
50de0f36973f
equal deleted inserted replaced
845:f3ab28ed22e5 846:ffa983c223c1
28 28
29 #import "Container.h" 29 #import "Container.h"
30 #import "GridLayout.h" 30 #import "GridLayout.h"
31 #import "BoxContainer.h" 31 #import "BoxContainer.h"
32 32
33 /* ------------------------- container classes ------------------------- */
34
35 /*
36 @implementation BoxContainer
37
38 @synthesize label=_label;
39 @synthesize uilayout=_uilayout;
40 @synthesize newline=_newline;
41
42 - (BoxContainer*)init:(NSUserInterfaceLayoutOrientation)orientation spacing:(int)spacing {
43 self = [super init];
44 _label = NULL;
45 _uilayout = (UiLayout){ 0 };
46 _newline = false;
47
48 self.distribution = NSStackViewDistributionFillProportionally;
49 self.spacing = spacing;
50
51 self.orientation = orientation;
52 if(orientation == NSUserInterfaceLayoutOrientationHorizontal) {
53 self.alignment = NSLayoutAttributeHeight;
54 } else {
55 self.alignment = NSLayoutAttributeWidth;
56 }
57
58
59 return self;
60 }
61
62 - (void) addView:(NSView*)view {
63 UiBool fill = _uilayout.fill;
64
65 [self addArrangedSubview:view];
66
67 if(self.orientation == NSUserInterfaceLayoutOrientationHorizontal) {
68 [view.heightAnchor constraintEqualToAnchor:self.heightAnchor].active = YES;
69 if(!fill) {
70 NSSize isize = view.intrinsicContentSize;
71 [view.widthAnchor constraintEqualToConstant:isize.width].active = YES;
72 }
73 } else {
74 [view.widthAnchor constraintEqualToAnchor:self.widthAnchor].active = YES;
75 if(!fill) {
76 NSSize isize = view.intrinsicContentSize;
77 NSRect frame = view.frame;
78 CGFloat height = isize.height > 0 ? isize.height : frame.size.height;
79 if(height == 0) {
80 printf("debug");
81 }
82 if(height > 0) {
83 [view.heightAnchor constraintEqualToConstant:height].active = YES;
84 }
85 }
86 }
87
88 // at the moment, only the fill layout option needs to be reset
89 _uilayout.fill = UI_DEFAULT;
90 }
91
92 @end
93 */
94
95
96 /* -------------------- public container functions --------------------- */ 33 /* -------------------- public container functions --------------------- */
97 34
98 static UIWIDGET ui_box_create(UiObject *obj, UiContainerArgs *args, NSUserInterfaceLayoutOrientation orientation) { 35 static UIWIDGET ui_box_create(UiObject *obj, UiContainerArgs *args, NSUserInterfaceLayoutOrientation orientation) {
99 BoxContainer *box = [[BoxContainer alloc] init:orientation spacing:args->spacing]; 36 BoxContainer *box = [[BoxContainer alloc] init:orientation spacing:args->spacing];
100 box.translatesAutoresizingMaskIntoConstraints = false; 37 box.translatesAutoresizingMaskIntoConstraints = false;
38 UiContainerX *container = ui_create_container(obj, box);
39 box.container = container;
101 40
102 // add box to the parent 41 // add box to the parent
103 UiLayout layout = UI_INIT_LAYOUT(args); 42 UiLayout layout = UI_INIT_LAYOUT(args);
104 ui_container_add(obj, box, &layout); 43 ui_container_add(obj, box, &layout);
105 44
106 // add new box to the obj container chain 45 // add new box to the obj container chain
107 uic_object_push_container(obj, ui_create_container(obj, box)); 46 uic_object_push_container(obj, container);
108 47
109 return (__bridge void*)box; 48 return (__bridge void*)box;
110 } 49 }
111 50
112 UIWIDGET ui_vbox_create(UiObject *obj, UiContainerArgs *args) { 51 UIWIDGET ui_vbox_create(UiObject *obj, UiContainerArgs *args) {
120 UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs *args) { 59 UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs *args) {
121 GridLayout *grid = [[GridLayout alloc] init]; 60 GridLayout *grid = [[GridLayout alloc] init];
122 grid.translatesAutoresizingMaskIntoConstraints = false; 61 grid.translatesAutoresizingMaskIntoConstraints = false;
123 grid.columnspacing = args->columnspacing; 62 grid.columnspacing = args->columnspacing;
124 grid.rowspacing = args->rowspacing; 63 grid.rowspacing = args->rowspacing;
64 UiContainerX *container = ui_create_container(obj, grid);
65 grid.container = container;
125 66
126 // add box to the parent 67 // add box to the parent
127 UiLayout layout = UI_INIT_LAYOUT(args); 68 UiLayout layout = UI_INIT_LAYOUT(args);
128 ui_container_add(obj, grid, &layout); 69 ui_container_add(obj, grid, &layout);
129 70
130 // add new box to the obj container chain 71 // add new box to the obj container chain
131 uic_object_push_container(obj, ui_create_container(obj, grid)); 72 uic_object_push_container(obj, container);
132 73
133 return (__bridge void*)grid; 74 return (__bridge void*)grid;
75 }
76
77 UIWIDGET ui_frame_create(UiObject *obj, UiFrameArgs *args) {
78 NSString *title = args->label ? [[NSString alloc]initWithUTF8String:args->label] : nil;
79 FrameContainer *frame = [[FrameContainer alloc] init:title]; UiLayout layout = UI_ARGS2LAYOUT(args);
80 ui_container_add(obj, frame, &layout);
81
82 // add container to the chain
83 UiContainerX *container;
84 UiLayout subLayout = {0};
85 switch(args->subcontainer) {
86 default: {
87 // UI_CONTAINER_NO_SUB
88 container = ui_create_container(obj, frame);
89 break;
90 }
91 case UI_CONTAINER_VBOX: {
92 BoxContainer *box = [[BoxContainer alloc]init:NSUserInterfaceLayoutOrientationVertical spacing:args->spacing];
93 box.translatesAutoresizingMaskIntoConstraints = false;
94 [frame addView:box layout:&subLayout];
95 container = ui_create_container(obj, box);
96 break;
97 }
98 case UI_CONTAINER_HBOX: {
99 BoxContainer *box = [[BoxContainer alloc]init:NSUserInterfaceLayoutOrientationHorizontal spacing:args->spacing];
100 box.translatesAutoresizingMaskIntoConstraints = false;
101 [frame addView:box layout:&subLayout];
102 container = ui_create_container(obj, box);
103 break;
104 }
105 case UI_CONTAINER_GRID: {
106 GridLayout *grid = [[GridLayout alloc] init];
107 grid.translatesAutoresizingMaskIntoConstraints = false;
108 grid.columnspacing = args->columnspacing;
109 grid.rowspacing = args->rowspacing;
110 [frame addView:grid layout:&subLayout];
111 container = ui_create_container(obj, grid);
112 break;
113 }
114 }
115
116 uic_object_push_container(obj, container);
117
118 return NULL;
134 } 119 }
135 120
136 void ui_container_begin_close(UiObject *obj) { 121 void ui_container_begin_close(UiObject *obj) {
137 UiContainerX *ct = obj->container_end; 122 UiContainerX *ct = obj->container_end;
138 ct->close = 1; 123 ct->close = 1;
145 return 0; 130 return 0;
146 } 131 }
147 return 1; 132 return 1;
148 } 133 }
149 134
135 /* -------------------------- Frame Container -------------------------- */
136
137 @implementation FrameContainer
138
139 @synthesize container = _container;
140
141 - (id)init:(NSString*)title {
142 self = [super init];
143 self.title = title;
144 self.boxType = NSBoxPrimary;
145 if(title != nil) {
146 self.titlePosition = NSAtTop;
147 }
148 return self;
149 }
150
151 - (void) addView:(NSView*)view layout:(UiLayout*)layout {
152 [self.contentView addSubview:view];
153 view.translatesAutoresizingMaskIntoConstraints = NO;
154 [NSLayoutConstraint activateConstraints:@[
155 [view.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:0],
156 [view.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:0],
157 [view.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:-0],
158 [view.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-0]
159 ]];
160 }
161
162 @end
163
150 /* ------------------------- private functions ------------------------- */ 164 /* ------------------------- private functions ------------------------- */
151 165
152 UiContainerX* ui_create_container(UiObject *obj, id<Container> container) { 166 UiContainerX* ui_create_container(UiObject *obj, id<Container> container) {
153 UiContainerX *ctn = ui_malloc(obj->ctx, sizeof(UiContainerX)); 167 UiContainerX *ctn = ui_malloc(obj->ctx, sizeof(UiContainerX));
154 ctn->container = (__bridge void*)container; 168 ctn->container = (__bridge void*)container;
160 } 174 }
161 175
162 void ui_container_add(UiObject *obj, NSView *view, UiLayout *layout) { 176 void ui_container_add(UiObject *obj, NSView *view, UiLayout *layout) {
163 UiContainerX *ctn = obj->container_end; 177 UiContainerX *ctn = obj->container_end;
164 id<Container> container = (__bridge id<Container>)ctn->container; 178 id<Container> container = (__bridge id<Container>)ctn->container;
165 container.uilayout = *layout; 179 UiLayout adjustedLayout = *layout;
166 NSEdgeInsets margin = {0}; 180 if(adjustedLayout.margin > 0) {
167 if(layout->margin > 0) { 181 adjustedLayout.margin_left = adjustedLayout.margin;
168 margin.left = layout->margin; 182 adjustedLayout.margin_right = adjustedLayout.margin;
169 margin.right = layout->margin; 183 adjustedLayout.margin_top = adjustedLayout.margin;
170 margin.top = layout->margin; 184 adjustedLayout.margin_bottom = adjustedLayout.margin;
171 margin.bottom = layout->margin;
172 } else {
173 margin.left = layout->margin_left;
174 margin.right = layout->margin_right;
175 margin.top = layout->margin_top;
176 margin.bottom = layout->margin_bottom;
177 } 185 }
178 [container addView:view margin:margin]; 186 [container addView:view layout:layout];
179 } 187 }
180 188

mercurial