UNIXworkcode

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 if(_var) { 54 UiInteger *i = _var->value; 55 i->obj = (__bridge void*)self; 56 i->get = ui_nstabview_get; 57 i->set = ui_nstabview_set; 58 } 59 60 return self; 61 } 62 63 - (void) addView:(NSView*)view layout:(UiLayout*)layout { 64 // noop 65 } 66 67 - (NSView<Container>*) createTab:(int)index title:(NSString*)title { 68 NSTabViewItem *item = [[NSTabViewItem alloc]initWithIdentifier:nil]; 69 [item setLabel:title]; 70 if(index < 0) { 71 [self addTabViewItem:item]; 72 } else { 73 [self insertTabViewItem:item atIndex:index]; 74 } 75 76 BoxContainer *content = [[BoxContainer alloc]init]; 77 item.view = content; 78 79 GridLayout *sub; 80 switch(_subcontainer) { 81 default: sub = [[BoxContainer alloc] init:NSUserInterfaceLayoutOrientationVertical spacing:_spacing]; break; 82 case UI_CONTAINER_HBOX: sub = [[BoxContainer alloc] init:NSUserInterfaceLayoutOrientationHorizontal spacing:_spacing]; break; 83 case UI_CONTAINER_GRID: { 84 sub = [[GridLayout alloc] init]; 85 sub.columnspacing = _columnspacing; 86 sub.rowspacing = _rowspacing; 87 break; 88 } 89 } 90 UiLayout layout = { 91 .margin = _padding, 92 .margin_left = _padding, .margin_right = _padding, .margin_top = _padding, .margin_bottom = _padding, 93 .fill = TRUE }; 94 [content addView:sub layout:&layout]; 95 96 return sub; 97 } 98 99 - (void) selectTab:(int)index { 100 [self selectTabViewItemAtIndex:index]; 101 } 102 103 - (void) removeTab:(int)index { 104 NSTabViewItem *item = [self tabViewItemAtIndex:index]; 105 if(item != nil) { 106 [self removeTabViewItem:item]; 107 } 108 } 109 110 - (UiObject*) addTab:(int)index title:(NSString*)title { 111 NSView<Container> *sub = [self createTab:index title:title]; 112 113 UiObject *newobj = uic_object_new_toplevel(); 114 newobj->widget = (__bridge void*)sub; 115 116 UiContainerX *container = ui_create_container(newobj, sub); 117 uic_object_push_container(newobj, container); 118 119 return newobj; 120 } 121 122 @end 123 124 int64_t ui_nstabview_get(UiInteger *i) { 125 UiTopTabView *tabview = (__bridge UiTopTabView*)i->obj; 126 i->value = [tabview indexOfTabViewItem:tabview.selectedTabViewItem]; 127 return i->value; 128 } 129 130 void ui_nstabview_set(UiInteger *i, int64_t value) { 131 UiTopTabView *tabview = (__bridge UiTopTabView*)i->obj; 132 [tabview selectTab:(int)value]; 133 i->value = [tabview indexOfTabViewItem:tabview.selectedTabViewItem]; 134 } 135