Debian Codegraph @

hiro/cocoa/widget/hex-edit.cpp

raw
1 #if defined(Hiro_HexEdit)
2
3 @implementation CocoaHexEdit
4
5 -(id) initWith:(hiro::mHexEdit&)hexEditReference {
6 if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
7 hexEdit = &hexEditReference;
8 if (tableView = [[NSTableView alloc] initWithFrame:[self bounds]]) {
9 [tableView setDataSource:self];
10 // [tableView setHeaderView:nil];
11 [tableView setUsesAlternatingRowBackgroundColors:true];
12 [tableView setGridStyleMask:(NSTableViewDashedHorizontalGridLineMask | NSTableViewSolidVerticalGridLineMask)];
13 [tableView setRowHeight:14.0];
14
15 // remove padding and stuff to fit all 16 columns
16 [tableView setIntercellSpacing:NSMakeSize(0, 0)];
17 // [tableView setStyle:NSTableViewStylePlain];
18 NSTableColumn* addressCol = [[NSTableColumn alloc] initWithIdentifier:@"Address"];
19 addressCol.editable = NO;
20 addressCol.title = @" Address";
21 [tableView addTableColumn:addressCol];
22 addressCol.width = 60;
23
24 for (int i = 0; i < hexEdit->columns(); i++) {
25 NSTableColumn* col = [[NSTableColumn alloc] initWithIdentifier:[NSString stringWithFormat:@"%d",
26 i]];
27 col.title = [NSString stringWithFormat:@"0%x", i];
28 col.width = 21;
29 [tableView addTableColumn:col];
30 }
31
32 NSTableColumn* charCol = [[NSTableColumn alloc] initWithIdentifier:@"Char"];
33 charCol.title = @" Plain Text";
34 charCol.editable = NO;
35 [tableView addTableColumn:charCol];
36
37 // must programmatically do this so the table shows up.
38 [self setDocumentView:tableView];
39 [self setHasVerticalScroller:YES];
40 [self setHasHorizontalScroller:YES];
41 }
42 }
43 return self;
44 }
45
46 - (NSTableView *) tableView {
47 return tableView;
48 }
49
50 - (hiro::mHexEdit *) hexEdit {
51 return hexEdit;
52 }
53
54
55 - (NSInteger) numberOfRowsInTableView:(NSTableView *)senderTableView {
56 return (max(1, hexEdit->length()) + hexEdit->columns() - 1) / hexEdit->columns();
57 }
58
59 -(id) tableView:(NSTableView *) senderTableView
60 objectValueForTableColumn:(NSTableColumn *) tableColumn
61 row:(NSInteger) row {
62 // return a string with the content of (row, column)
63 // u32 address = hexEdit->address() + row * hexEdit->columns();
64 u32 address = row * hexEdit->columns();
65 // address only
66 if ([[tableColumn identifier] isEqualToString:@"Address"]) {
67 return [NSString stringWithUTF8String:hex(address, 8L).data()];
68 } else if ([[tableColumn identifier] isEqualToString:@"Char"]){
69 // create the char string by concatenating
70 NSMutableString *output = [NSMutableString stringWithCapacity:hexEdit->columns()];
71 for (auto column : range(hexEdit->columns())) {
72 if (address < hexEdit->length()) {
73 u8 data = hexEdit->doRead(address++);
74 [output appendString:[NSString stringWithFormat:@"%c",
75 (data >= 0x20 && data <= 0x7e ? (char)data : '.')]];
76 }
77 }
78 return output;
79 } else {
80 // return only the single byte at the correct position
81 NSInteger columnNumber = [tableColumn.identifier integerValue];
82 address += columnNumber;
83 if (address < hexEdit->length()) {
84 u8 data = hexEdit->doRead(address);
85 return [NSString stringWithUTF8String:hex(data, 2L).data()];
86 } else {
87 return @" ";
88 }
89 }
90 }
91
92 -(void) tableView:(NSTableView *) senderTableView
93 setObjectValue:(id) object
94 forTableColumn:(NSTableColumn *) tableColumn
95 row:(NSInteger) row {
96 // when table is edited, modify underlying data source
97 NSInteger colNumber = [tableColumn.identifier integerValue];
98 u32 address = row * hexEdit->columns() + colNumber;
99
100 if (address < hexEdit->length()) {
101 // only get the first 2 characters
102 NSString* newVal = [(NSString *)object substringToIndex:2];
103 NSScanner* hexScanner = [NSScanner scannerWithString:newVal];
104 unsigned int data = 0;
105 if ([hexScanner scanHexInt:&data]) {
106 // this.......is probably ok....???
107 hexEdit->doWrite(address, (u8)data);
108 }
109 }
110 [senderTableView reloadData];
111 }
112
113 @end
114
115 namespace hiro {
116
117 auto pHexEdit::construct() -> void {
118 cocoaView = cocoaHexEdit = [[CocoaHexEdit alloc] initWith:self()];
119 pWidget::construct();
120
121 for (NSTableColumn *column in cocoaHexEdit.tableView.tableColumns) {
122 if (@available(macOS 10.15, *)) {
123 NSTextFieldCell *cell = column.dataCell;
124 NSTableHeaderCell *headerCell = column.headerCell;
125
126 // Set the font for the data cell.
127 cell.font = [NSFont monospacedSystemFontOfSize:10 weight:NSFontWeightRegular];
128 }
129 }
130 update();
131 }
132
133 auto pHexEdit::destruct() -> void {
134 [cocoaView removeFromSuperview];
135 }
136
137 auto pHexEdit::setAddress(u32 offset) -> void {
138 mHexEdit* hexEdit = [cocoaHexEdit hexEdit];
139 int row = offset / hexEdit->columns();
140 [[cocoaHexEdit tableView] scrollRowToVisible:row];
141 }
142
143 auto pHexEdit::setBackgroundColor(Color color) -> void {
144 update();
145 }
146
147 auto pHexEdit::setColumns(u32 columns) -> void {
148 update();
149 }
150
151 auto pHexEdit::setForegroundColor(Color color) -> void {
152 update();
153 }
154
155 auto pHexEdit::setLength(u32 length) -> void {
156 update();
157 }
158
159 auto pHexEdit::setRows(u32 rows) -> void {
160 update();
161 }
162
163 auto pHexEdit::update() -> void {
164 [[cocoaHexEdit tableView] reloadData];
165 }
166
167 }
168
169 #endif