ui/cocoa/text.m

changeset 109
c3dfcb8f0be7
parent 108
77254bd6dccb
child 110
c00e968d018b
equal deleted inserted replaced
108:77254bd6dccb 109:c3dfcb8f0be7
40 NSScrollView *scrollview = [[NSScrollView alloc] init]; 40 NSScrollView *scrollview = [[NSScrollView alloc] init];
41 scrollview.hasVerticalScroller = YES; 41 scrollview.hasVerticalScroller = YES;
42 scrollview.documentView = textview; 42 scrollview.documentView = textview;
43 43
44 UiLayout layout = UI_INIT_LAYOUT(args); 44 UiLayout layout = UI_INIT_LAYOUT(args);
45 ui_container_add(obj, scrollview, &layout, TRUE); 45 ui_container_add(obj, scrollview, &layout);
46 46
47 47
48 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_TEXT); 48 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_TEXT);
49 if(var) { 49 if(var) {
50 UiText *text = var->value; 50 UiText *text = var->value;
83 void ui_textarea_restore(UiText *text) { 83 void ui_textarea_restore(UiText *text) {
84 NSTextView *textview = (__bridge NSTextView*)text->obj; 84 NSTextView *textview = (__bridge NSTextView*)text->obj;
85 NSTextStorage *textStorage; 85 NSTextStorage *textStorage;
86 if(text->data1) { 86 if(text->data1) {
87 textStorage = (__bridge NSTextStorage*)text->data1; 87 textStorage = (__bridge NSTextStorage*)text->data1;
88
89 } else { 88 } else {
90 textStorage = [[NSTextStorage alloc] init]; 89 textStorage = [[NSTextStorage alloc] init];
91 } 90 }
92 [textview.layoutManager replaceTextStorage:textStorage]; 91 [textview.layoutManager replaceTextStorage:textStorage];
93 text->data1 = (__bridge_retained void*)textStorage; 92 text->data1 = (__bridge_retained void*)textStorage;
94 } 93 }
95 94
96 void ui_textarea_set(UiText *text, const char *str) { 95 void ui_textarea_set(UiText *text, const char *str) {
97 96 NSTextView *textview = (__bridge NSTextView*)text->obj;
97 if(text->value.free) {
98 text->value.free(text->value.ptr);
99 }
100 text->value.ptr = strdup(str);
101 text->value.free = free;
102 textview.string = [[NSString alloc] initWithUTF8String:str];
98 } 103 }
99 104
100 char* ui_textarea_get(UiText *text) { 105 char* ui_textarea_get(UiText *text) {
101 return NULL; 106 NSTextView *textview = (__bridge NSTextView*)text->obj;
107 if(text->value.free) {
108 text->value.free(text->value.ptr);
109 }
110 text->value.ptr = strdup(textview.string.UTF8String);
111 text->value.free = free;
112 return text->value.ptr;
102 } 113 }
103 114
104 char* ui_textarea_getsubstr(UiText *text, int begin, int end) { 115 char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
105 return NULL; 116 NSTextView *textview = (__bridge NSTextView*)text->obj;
117 NSString *str = textview.string;
118 NSRange range = NSMakeRange(begin, end-begin);
119 NSString *sub = [str substringWithRange:range];
120 return strdup(sub.UTF8String);
106 } 121 }
107 122
108 void ui_textarea_insert(UiText *text, int pos, char *str) { 123 void ui_textarea_insert(UiText *text, int pos, char *str) {
109 124 NSTextView *textview = (__bridge NSTextView*)text->obj;
125 NSString *s = [[NSString alloc] initWithUTF8String:str];
126 NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:s];
127 [textview.textStorage insertAttributedString:attributedStr atIndex:pos];
110 } 128 }
111 129
112 void ui_textarea_setposition(UiText *text, int pos) { 130 void ui_textarea_setposition(UiText *text, int pos) {
113 131 NSTextView *textview = (__bridge NSTextView*)text->obj;
132 NSRange range = NSMakeRange(pos, 0);
133 [textview setSelectedRange:range];
114 } 134 }
115 135
116 int ui_textarea_position(UiText *text) { 136 int ui_textarea_position(UiText *text) {
117 return 0; 137 NSTextView *textview = (__bridge NSTextView*)text->obj;
138 NSRange range = textview.selectedRange;
139 return (int)range.location;
118 } 140 }
119 141
120 void ui_textarea_setselection(UiText *text, int begin, int end) { 142 void ui_textarea_setselection(UiText *text, int begin, int end) {
121 143 NSTextView *textview = (__bridge NSTextView*)text->obj;
144 NSRange range = NSMakeRange(begin, end-begin);
145 [textview setSelectedRange:range];
122 } 146 }
123 147
124 void ui_textarea_selection(UiText *text, int *begin, int *end) { 148 void ui_textarea_selection(UiText *text, int *begin, int *end) {
125 149 NSTextView *textview = (__bridge NSTextView*)text->obj;
150 NSRange range = textview.selectedRange;
151 if(begin) {
152 *begin = (int)range.location;
153 }
154 if(end) {
155 *end = (int)(range.location+range.length);
156 }
126 } 157 }
127 158
128 int ui_textarea_length(UiText *text) { 159 int ui_textarea_length(UiText *text) {
129 return 0; 160 NSTextView *textview = (__bridge NSTextView*)text->obj;
161 return (int)textview.string.length;
130 } 162 }
131 163
132 void ui_textarea_remove(UiText *text, int begin, int end) { 164 void ui_textarea_remove(UiText *text, int begin, int end) {
133 165 NSTextView *textview = (__bridge NSTextView*)text->obj;
166
167 if (begin < 0 || end < begin || end > textview.string.length) {
168 return;
169 }
170
171 NSRange range = NSMakeRange(begin, end - begin);
172 [[textview textStorage] deleteCharactersInRange:range];
134 } 173 }
135 174
136 175
137 176
138 /* -------------------------- TextField -------------------------- */ 177 /* -------------------------- TextField -------------------------- */
148 if(frameless) { 187 if(frameless) {
149 [textfield setBezeled: NO]; 188 [textfield setBezeled: NO];
150 } 189 }
151 190
152 UiLayout layout = UI_INIT_LAYOUT(args); 191 UiLayout layout = UI_INIT_LAYOUT(args);
153 ui_container_add(obj, textfield, &layout, FALSE); 192 ui_container_add(obj, textfield, &layout);
154 193
155 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_STRING); 194 UiVar* var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_STRING);
156 if(var) { 195 if(var) {
157 UiString *s = var->value; 196 UiString *s = var->value;
158 if(s->value.ptr) { 197 if(s->value.ptr) {

mercurial