ui/cocoa/text.m

changeset 13
2dbc56c2323b
parent 7
431dde3c5fbe
child 23
decc6bf584aa
equal deleted inserted replaced
12:fe94e0fb9ef3 13:2dbc56c2323b
31 #import <string.h> 31 #import <string.h>
32 32
33 #import "text.h" 33 #import "text.h"
34 #import "container.h" 34 #import "container.h"
35 35
36 @implementation TextChangeMgr
37
38 - (TextChangeMgr*)initWithValue:(UiText*)text {
39 value = text;
40 return self;
41 }
42
43 - (NSUndoManager*)undoManagerForTextView:(NSTextView*)textview {
44 return (NSUndoManager*)value->undomgr;
45 }
46
47 @end
48
49
36 UIWIDGET ui_textarea(UiObject *obj, UiText *value) { 50 UIWIDGET ui_textarea(UiObject *obj, UiText *value) {
37 UiContainer *ct = uic_get_current_container(obj); 51 UiContainer *ct = uic_get_current_container(obj);
38 52
39 NSRect frame = ct->getframe(ct); 53 NSRect frame = ct->getframe(ct);
40 54
44 [scrollview setBorderType:NSNoBorder]; 58 [scrollview setBorderType:NSNoBorder];
45 //[scrollview setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable]; 59 //[scrollview setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
46 60
47 //frame.size.width = frame.size.width - 15; 61 //frame.size.width = frame.size.width - 15;
48 NSTextView *textview = [[NSTextView alloc]initWithFrame:frame]; 62 NSTextView *textview = [[NSTextView alloc]initWithFrame:frame];
63 [textview setAllowsUndo:TRUE];
49 [textview setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable]; 64 [textview setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
50 65
51 [scrollview setDocumentView:textview]; 66 [scrollview setDocumentView:textview];
52 67
53 ct->add(ct, scrollview); 68 ct->add(ct, scrollview);
54 69
55 // bind value 70 // bind value
56 if(value) { 71 if(value) {
57 value->get = ui_textarea_get; 72 value->get = ui_textarea_get;
58 value->set = ui_textarea_set; 73 value->set = ui_textarea_set;
74 value->getsubstr = ui_textarea_getsubstr;
75 value->insert = ui_textarea_insert;
59 value->value = NULL; 76 value->value = NULL;
60 value->obj = textview; 77 value->obj = textview;
61 78
79 TextChangeMgr *delegate = [[TextChangeMgr alloc]initWithValue:value];
80 [textview setDelegate:delegate];
81
82 NSUndoManager *undomgr = [[NSUndoManager alloc]init];
83 value->undomgr = undomgr;
62 } 84 }
63 85
64 return textview; 86 return textview;
65 } 87 }
66 88
67 char* ui_textarea_get(UiText *text) { 89 char* ui_textarea_get(UiText *text) {
90 if(text->value) {
91 free(text->value);
92 }
68 NSTextView *textview = (NSTextView*)text->obj; 93 NSTextView *textview = (NSTextView*)text->obj;
69 NSString *str = [[textview textStorage]string]; 94 NSString *str = [[textview textStorage]string];
70 return (char*)[str UTF8String]; 95 size_t length = [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
96 const char *cstr = [str UTF8String];
97 char *value = malloc(length + 1);
98 memcpy(value, cstr, length);
99 value[length] = '\0';
100 text->value = value;
101 return value;
71 } 102 }
72 103
73 void ui_textarea_set(UiText *text, char *str) { 104 void ui_textarea_set(UiText *text, char *str) {
105 if(text->value) {
106 free(text->value);
107 }
74 NSTextView *textview = (NSTextView*)text->obj; 108 NSTextView *textview = (NSTextView*)text->obj;
75 NSString *s = [[NSString alloc]initWithUTF8String:str]; 109 NSString *s = [[NSString alloc]initWithUTF8String:str];
76 NSAttributedString *as = [[NSAttributedString alloc]initWithString:s]; 110 NSAttributedString *as = [[NSAttributedString alloc]initWithString:s];
77 [[textview textStorage] setAttributedString:as]; 111 [[textview textStorage] setAttributedString:as];
112 text->value = NULL;
78 } 113 }
114
115 char* ui_textarea_getsubstr(UiText *text, int begin, int end) {
116 if(text->value) {
117 free(text->value);
118 }
119 NSTextView *textview = (NSTextView*)text->obj;
120 NSString *str = [[textview textStorage]string];
121 NSRange range;
122 range.location = begin;
123 range.length = end - begin;
124
125 NSString *substr = [str substringWithRange:range];
126 size_t length = [substr lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
127 const char *cstr = [substr UTF8String];
128 char *value = malloc(length + 1);
129 memcpy(value, cstr, length);
130 value[length] = '\0';
131 text->value = value;
132 return value;
133 }
134
135 void ui_textarea_insert(UiText *text, int pos, char *str) {
136 if(text->value) {
137 free(text->value);
138 }
139 if(text->value) {
140 free(text->value);
141 }
142 NSTextView *textview = (NSTextView*)text->obj;
143 NSString *s = [[NSString alloc]initWithUTF8String:str];
144 NSAttributedString *as = [[NSAttributedString alloc]initWithString:s];
145 [[textview textStorage] insertAttributedString:as atIndex: pos];
146 text->value = NULL;
147 }
148
149 void ui_text_undo(UiText *text) {
150 [(NSUndoManager*)text->undomgr undo];
151 }
152
153 void ui_text_redo(UiText *text) {
154 [(NSUndoManager*)text->undomgr redo];
155 }
156

mercurial