File: text-edit.cpp

package info (click to toggle)
higan 098-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,904 kB
  • ctags: 13,286
  • sloc: cpp: 108,285; ansic: 778; makefile: 32; sh: 18
file content (119 lines) | stat: -rw-r--r-- 2,922 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
113
114
115
116
117
118
119
#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)] autorelease];
    [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 {
  @autoreleasepool {
    cocoaView = cocoaTextEdit = [[CocoaTextEdit alloc] initWith:self()];
    pWidget::construct();

    setEditable(state().editable);
    setWordWrap(state().wordWrap);
    setText(state().text);
    setCursor(state().cursor);
  }
}

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

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

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

auto pTextEdit::setEditable(bool editable) -> void {
  @autoreleasepool {
    [[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 {
  @autoreleasepool {
    [[cocoaView content] setFont:pFont::create(font)];
  }
}

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

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

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

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

}

#endif