text-edit.cpp 3.1 KB

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