| |
1 /* |
| |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| |
3 * |
| |
4 * Copyright 2024 Olaf Wintermann. All rights reserved. |
| |
5 * |
| |
6 * Redistribution and use in source and binary forms, with or without |
| |
7 * modification, are permitted provided that the following conditions are met: |
| |
8 * |
| |
9 * 1. Redistributions of source code must retain the above copyright |
| |
10 * notice, this list of conditions and the following disclaimer. |
| |
11 * |
| |
12 * 2. Redistributions in binary form must reproduce the above copyright |
| |
13 * notice, this list of conditions and the following disclaimer in the |
| |
14 * documentation and/or other materials provided with the distribution. |
| |
15 * |
| |
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| |
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| |
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| |
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| |
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| |
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| |
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| |
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| |
26 * POSSIBILITY OF SUCH DAMAGE. |
| |
27 */ |
| |
28 |
| |
29 #import "TabView.h" |
| |
30 #import "BoxContainer.h" |
| |
31 #import "GridLayout.h" |
| |
32 |
| |
33 @implementation UiTopTabView |
| |
34 |
| |
35 @synthesize container = _container; |
| |
36 |
| |
37 - (id)init:(UiObject*)obj args:(UiTabViewArgs*)args { |
| |
38 self = [super init]; |
| |
39 _obj = obj; |
| |
40 _subcontainer = args->subcontainer; |
| |
41 _padding = args->padding; |
| |
42 _spacing = args->spacing; |
| |
43 _columnspacing = args->columnspacing; |
| |
44 _rowspacing = args->rowspacing; |
| |
45 _onchange = args->onchange; |
| |
46 _onchangedata = args->onchangedata; |
| |
47 _var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_INTEGER); |
| |
48 |
| |
49 if(args->tabview == UI_TABVIEW_INVISIBLE || args->tabview == UI_TABVIEW_NAVIGATION_SIDE) { |
| |
50 self.tabViewType = NSNoTabsNoBorder; |
| |
51 } |
| |
52 |
| |
53 return self; |
| |
54 } |
| |
55 |
| |
56 - (void) addView:(NSView*)view layout:(UiLayout*)layout { |
| |
57 // noop |
| |
58 } |
| |
59 |
| |
60 - (NSView<Container>*) createTab:(int)index title:(NSString*)title { |
| |
61 NSTabViewItem *item = [[NSTabViewItem alloc]initWithIdentifier:nil]; |
| |
62 [item setLabel:title]; |
| |
63 if(index < 0) { |
| |
64 [self addTabViewItem:item]; |
| |
65 } else { |
| |
66 [self insertTabViewItem:item atIndex:index]; |
| |
67 } |
| |
68 |
| |
69 BoxContainer *content = [[BoxContainer alloc]init]; |
| |
70 item.view = content; |
| |
71 |
| |
72 GridLayout *sub; |
| |
73 switch(_subcontainer) { |
| |
74 default: sub = [[BoxContainer alloc] init:NSUserInterfaceLayoutOrientationVertical spacing:_spacing]; break; |
| |
75 case UI_CONTAINER_HBOX: sub = [[BoxContainer alloc] init:NSUserInterfaceLayoutOrientationHorizontal spacing:_spacing]; break; |
| |
76 case UI_CONTAINER_GRID: { |
| |
77 sub = [[GridLayout alloc] init]; |
| |
78 sub.columnspacing = _columnspacing; |
| |
79 sub.rowspacing = _rowspacing; |
| |
80 break; |
| |
81 } |
| |
82 } |
| |
83 UiLayout layout = { |
| |
84 .margin = _padding, |
| |
85 .margin_left = _padding, .margin_right = _padding, .margin_top = _padding, .margin_bottom = _padding, |
| |
86 .fill = TRUE }; |
| |
87 [content addView:sub layout:&layout]; |
| |
88 |
| |
89 return sub; |
| |
90 } |
| |
91 |
| |
92 - (void) selectTab:(int)index { |
| |
93 [self selectTabViewItemAtIndex:index]; |
| |
94 } |
| |
95 |
| |
96 - (void) removeTab:(int)index { |
| |
97 NSTabViewItem *item = [self tabViewItemAtIndex:index]; |
| |
98 if(item != nil) { |
| |
99 [self removeTabViewItem:item]; |
| |
100 } |
| |
101 } |
| |
102 |
| |
103 - (UiObject*) addTab:(int)index title:(NSString*)title { |
| |
104 NSView<Container> *sub = [self createTab:index title:title]; |
| |
105 |
| |
106 UiObject *newobj = uic_object_new_toplevel(); |
| |
107 newobj->widget = (__bridge void*)sub; |
| |
108 |
| |
109 UiContainerX *container = ui_create_container(newobj, sub); |
| |
110 uic_object_push_container(newobj, container); |
| |
111 |
| |
112 return newobj; |
| |
113 } |
| |
114 |
| |
115 @end |