| 1 |
@implementation CocoaTextEdit : NSScrollView |
| 2 |
|
| 3 |
-(id) initWith:(phoenix::TextEdit&)textEditReference { |
| 4 |
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) { |
| 5 |
textEdit = &textEditReference; |
| 6 |
|
| 7 |
content = [[[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)] autorelease]; |
| 8 |
[content setDelegate:self]; |
| 9 |
[content setRichText:NO]; |
| 10 |
|
| 11 |
[self setBorderType:NSBezelBorder]; |
| 12 |
[self setDocumentView:content]; |
| 13 |
[self configure]; |
| 14 |
} |
| 15 |
return self; |
| 16 |
} |
| 17 |
|
| 18 |
-(NSTextView*) content { |
| 19 |
return content; |
| 20 |
} |
| 21 |
|
| 22 |
-(void) configure { |
| 23 |
[content setMinSize:NSMakeSize(0, 0)]; |
| 24 |
[content setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)]; |
| 25 |
|
| 26 |
[[content textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)]; |
| 27 |
[[content textContainer] setWidthTracksTextView:textEdit->wordWrap()]; |
| 28 |
|
| 29 |
[content setHorizontallyResizable:YES]; |
| 30 |
[content setVerticallyResizable:YES]; |
| 31 |
[content setAutoresizingMask:NSViewNotSizable]; |
| 32 |
|
| 33 |
[self setHasHorizontalScroller:!textEdit->wordWrap()]; |
| 34 |
[self setHasVerticalScroller:YES]; |
| 35 |
} |
| 36 |
|
| 37 |
-(void) textDidChange:(NSNotification*)notification { |
| 38 |
textEdit->state.text = [[content string] UTF8String]; |
| 39 |
if(textEdit->onChange) textEdit->onChange(); |
| 40 |
} |
| 41 |
|
| 42 |
@end |
| 43 |
|
| 44 |
namespace phoenix { |
| 45 |
|
| 46 |
void pTextEdit::setCursorPosition(unsigned position) { |
| 47 |
@autoreleasepool { |
| 48 |
string text = [[[cocoaView content] string] UTF8String]; |
| 49 |
position = min(position, text.length()); |
| 50 |
[[cocoaView content] setSelectedRange:NSMakeRange(position, 0)]; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
void pTextEdit::setEditable(bool editable) { |
| 55 |
@autoreleasepool { |
| 56 |
[[cocoaView content] setEditable:editable]; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
void pTextEdit::setFont(string font) { |
| 61 |
@autoreleasepool { |
| 62 |
[[cocoaView content] setFont:pFont::cocoaFont(font)]; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void pTextEdit::setText(string text) { |
| 67 |
@autoreleasepool { |
| 68 |
[[cocoaView content] setString:[NSString stringWithUTF8String:text]]; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
void pTextEdit::setWordWrap(bool wordWrap) { |
| 73 |
@autoreleasepool { |
| 74 |
[cocoaView configure]; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
string pTextEdit::text() { |
| 79 |
@autoreleasepool { |
| 80 |
return [[[cocoaView content] string] UTF8String]; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
void pTextEdit::constructor() { |
| 85 |
@autoreleasepool { |
| 86 |
cocoaView = cocoaTextEdit = [[CocoaTextEdit alloc] initWith:textEdit]; |
| 87 |
setEditable(textEdit.state.editable); |
| 88 |
setWordWrap(textEdit.state.wordWrap); |
| 89 |
setFont(textEdit.font()); |
| 90 |
setText(textEdit.state.text); |
| 91 |
setCursorPosition(textEdit.state.cursorPosition); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void pTextEdit::destructor() { |
| 96 |
@autoreleasepool { |
| 97 |
[cocoaView release]; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
} |