File: text-edit.cpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (112 lines) | stat: -rw-r--r-- 3,006 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#if defined(Hiro_TextEdit)

@implementation CocoaTextEdit : NSScrollView

-(id) initWith:(hiro::mTextEdit&)textEditReference {
  if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
    textEdit = &textEditReference;

    content = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)];
    [content setDelegate:self];
    [content setRichText:NO];

    [self setBorderType:NSBezelBorder];
    [self setDocumentView:content];
    [self configure];
  }
  return self;
}

-(NSTextView*) content {
  return content;
}

-(void) configure {
  [content setMinSize:NSMakeSize(0, 0)];
  [content setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];

  [[content textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
  [[content textContainer] setWidthTracksTextView:textEdit->wordWrap()];

  [content setHorizontallyResizable:YES];
  [content setVerticallyResizable:YES];
  [content setAutoresizingMask:NSViewNotSizable];

  [self setHasHorizontalScroller:!textEdit->wordWrap()];
  [self setHasVerticalScroller:YES];
}

-(void) textDidChange:(NSNotification*)notification {
  textEdit->state.text = [[content string] UTF8String];
  textEdit->doChange();
}

@end

namespace hiro {

auto pTextEdit::construct() -> void {
  cocoaView = cocoaTextEdit = [[CocoaTextEdit alloc] initWith:self()];
  pWidget::construct();

  setEditable(state().editable);
  setWordWrap(state().wordWrap);
  setText(state().text);
  setTextCursor(state().textCursor);
}

auto pTextEdit::destruct() -> void {
  [cocoaView removeFromSuperview];
}

auto pTextEdit::setBackgroundColor(Color color) -> void {
}

auto pTextEdit::setEditable(bool editable) -> void {
  [[(CocoaTextEdit*)cocoaView content] setEditable:(editable && self().enabled(true))];
}

auto pTextEdit::setEnabled(bool enabled) -> void {
  pWidget::setEnabled(enabled);
  setEditable(state().editable);  //Cocoa lacks NSTextView::setEnabled; simulate via setEnabled()
}

auto pTextEdit::setFont(const Font& font) -> void {
  [[(CocoaTextEdit*)cocoaView content] setFont:pFont::create(font)];
}

auto pTextEdit::setForegroundColor(Color color) -> void {
}

auto pTextEdit::setGeometry(Geometry geometry) -> void {
  pWidget::setGeometry(geometry);
  [(CocoaTextEdit*)cocoaView configure];
}

auto pTextEdit::setText(const string& text) -> void {
  [[(CocoaTextEdit*)cocoaView content] setString:[NSString stringWithUTF8String:text]];
}

auto pTextEdit::setTextCursor(TextCursor cursor) -> void {
  //todo: handle text selection (cursor.length())
  string text = [[[(CocoaTextEdit*)cocoaView content] string] UTF8String];
  auto offset = min(cursor.offset(), text.length());
  [[(CocoaTextEdit*)cocoaView content] setSelectedRange:NSMakeRange(offset, 0)];
}

auto pTextEdit::setWordWrap(bool wordWrap) -> void {
  [(CocoaTextEdit*)cocoaView configure];
}

auto pTextEdit::text() const -> string {
  return [[[(CocoaTextEdit*)cocoaView content] string] UTF8String];
}

auto pTextEdit::textCursor() const -> TextCursor {
  //TODO
  return state().textCursor;
}

}

#endif