Debian Codegraph @

hiro/cocoa/widget/text-edit.cpp

raw
1 #if defined(Hiro_TextEdit)
2
3 @implementation CocoaTextEdit
4
5 -(id) initWith:(hiro::mTextEdit&)textEditReference {
6 if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
7 textEdit = &textEditReference;
8
9 content = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)];
10 [content setDelegate:self];
11 [content setRichText:NO];
12
13 [self setBorderType:NSBezelBorder];
14 [self setDocumentView:content];
15 [self configure];
16 }
17 return self;
18 }
19
20 -(NSTextView*) content {
21 return content;
22 }
23
24 -(void) configure {
25 [content setMinSize:NSMakeSize(0, 0)];
26 [content setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
27
28 [[content textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
29 [[content textContainer] setWidthTracksTextView:textEdit->wordWrap()];
30
31 [content setHorizontallyResizable:YES];
32 [content setVerticallyResizable:YES];
33 [content setAutoresizingMask:NSViewNotSizable];
34
35 [self setHasHorizontalScroller:!textEdit->wordWrap()];
36 [self setHasVerticalScroller:YES];
37 }
38
39 -(void) textDidChange:(NSNotification*)notification {
40 textEdit->state.text = [[content string] UTF8String];
41 textEdit->doChange();
42 }
43 @end
44
45 namespace hiro {
46
47 auto pTextEdit::construct() -> void {
48 cocoaView = cocoaTextEdit = [[CocoaTextEdit alloc] initWith:self()];
49 pWidget::construct();
50
51 setEditable(state().editable);
52 setWordWrap(state().wordWrap);
53 setText(state().text);
54 setTextCursor(state().textCursor);
55 }
56
57 auto pTextEdit::destruct() -> void {
58 [cocoaView removeFromSuperview];
59 }
60
61 auto pTextEdit::setBackgroundColor(Color color) -> void {
62 }
63
64 auto pTextEdit::setEditable(bool editable) -> void {
65 [[(CocoaTextEdit*)cocoaView content] setEditable:(editable && self().enabled(true))];
66 }
67
68 auto pTextEdit::setEnabled(bool enabled) -> void {
69 pWidget::setEnabled(enabled);
70 setEditable(state().editable); //Cocoa lacks NSTextView::setEnabled; simulate via setEnabled()
71 }
72
73 auto pTextEdit::setFont(const Font& font) -> void {
74 [[(CocoaTextEdit*)cocoaView content] setFont:pFont::create(font)];
75 }
76
77 auto pTextEdit::setForegroundColor(Color color) -> void {
78 }
79
80 auto pTextEdit::setGeometry(Geometry geometry) -> void {
81 pWidget::setGeometry(geometry);
82 [(CocoaTextEdit*)cocoaView configure];
83 }
84
85 auto pTextEdit::setText(const string& text) -> void {
86 [[(CocoaTextEdit*)cocoaView content] setString:[NSString stringWithUTF8String:text]];
87 }
88
89 auto pTextEdit::setTextCursor(TextCursor cursor) -> void {
90 //TODO: handle text selection (cursor.length())
91 string text = [[[(CocoaTextEdit*)cocoaView content] string] UTF8String];
92 auto offset = min(cursor.offset(), text.length());
93 [[(CocoaTextEdit*)cocoaView content] setSelectedRange:NSMakeRange(offset, 0)];
94 }
95
96 auto pTextEdit::setWordWrap(bool wordWrap) -> void {
97 [(CocoaTextEdit*)cocoaView configure];
98 }
99
100 auto pTextEdit::text() const -> string {
101 return [[[(CocoaTextEdit*)cocoaView content] string] UTF8String];
102 }
103
104 auto pTextEdit::textCursor() const -> TextCursor {
105 //TODO
106 return state().textCursor;
107 }
108
109 }
110
111 #endif