| 1 |
#if defined(Hiro_HexEdit) |
| 2 |
|
| 3 |
namespace hiro { |
| 4 |
|
| 5 |
static auto HexEdit_keyPress(GtkWidget* widget, GdkEventKey* event, pHexEdit* p) -> s32 { |
| 6 |
return p->keyPress(event->keyval, event->state); |
| 7 |
} |
| 8 |
|
| 9 |
static auto HexEdit_mouseScroll(GtkWidget* widget, GdkEventScroll* event, pHexEdit* p) -> s32 { |
| 10 |
f64 position = gtk_range_get_value(GTK_RANGE(p->scrollBar)); |
| 11 |
|
| 12 |
if(event->direction == GDK_SCROLL_UP) { |
| 13 |
p->scroll(position - 1); |
| 14 |
} |
| 15 |
|
| 16 |
if(event->direction == GDK_SCROLL_DOWN) { |
| 17 |
p->scroll(position + 1); |
| 18 |
} |
| 19 |
|
| 20 |
return true; //do not propagate event further |
| 21 |
} |
| 22 |
|
| 23 |
static auto HexEdit_scroll(GtkRange* range, GtkScrollType scroll, f64 value, pHexEdit* p) -> s32 { |
| 24 |
p->scroll((s32)value); |
| 25 |
return true; //do not propagate event further |
| 26 |
} |
| 27 |
|
| 28 |
auto pHexEdit::construct() -> void { |
| 29 |
#if HIRO_GTK==2 |
| 30 |
gtkWidget = gtk_hbox_new(false, 0); |
| 31 |
#elif HIRO_GTK==3 |
| 32 |
gtkWidget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); |
| 33 |
#endif |
| 34 |
|
| 35 |
container = gtk_scrolled_window_new(0, 0); |
| 36 |
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(container), GTK_POLICY_AUTOMATIC, GTK_POLICY_NEVER); |
| 37 |
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(container), GTK_SHADOW_ETCHED_IN); |
| 38 |
|
| 39 |
subWidget = gtk_text_view_new(); |
| 40 |
gtk_text_view_set_editable(GTK_TEXT_VIEW(subWidget), false); |
| 41 |
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(subWidget), GTK_WRAP_NONE); |
| 42 |
gtk_container_add(GTK_CONTAINER(container), subWidget); |
| 43 |
|
| 44 |
#if HIRO_GTK==2 |
| 45 |
scrollBar = gtk_vscrollbar_new(nullptr); |
| 46 |
#elif HIRO_GTK==3 |
| 47 |
scrollBar = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, nullptr); |
| 48 |
#endif |
| 49 |
gtk_range_set_range(GTK_RANGE(scrollBar), 0, 255); |
| 50 |
gtk_range_set_increments(GTK_RANGE(scrollBar), 1, 16); |
| 51 |
gtk_widget_set_sensitive(scrollBar, false); |
| 52 |
|
| 53 |
gtk_box_pack_start(GTK_BOX(gtkWidget), container, true, true, 0); |
| 54 |
gtk_box_pack_start(GTK_BOX(gtkWidget), scrollBar, false, false, 1); |
| 55 |
|
| 56 |
textBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(subWidget)); |
| 57 |
textCursor = gtk_text_buffer_get_mark(textBuffer, "insert"); |
| 58 |
|
| 59 |
gtk_widget_show(scrollBar); |
| 60 |
gtk_widget_show(subWidget); |
| 61 |
gtk_widget_show(container); |
| 62 |
|
| 63 |
setAddress(state().address); |
| 64 |
setBackgroundColor(state().backgroundColor); |
| 65 |
setColumns(state().columns); |
| 66 |
setForegroundColor(state().foregroundColor); |
| 67 |
setRows(state().rows); |
| 68 |
setLength(state().length); |
| 69 |
update(); |
| 70 |
|
| 71 |
g_signal_connect(G_OBJECT(subWidget), "key-press-event", G_CALLBACK(HexEdit_keyPress), (gpointer)this); |
| 72 |
g_signal_connect(G_OBJECT(subWidget), "scroll-event", G_CALLBACK(HexEdit_mouseScroll), (gpointer)this); |
| 73 |
|
| 74 |
g_signal_connect(G_OBJECT(scrollBar), "change-value", G_CALLBACK(HexEdit_scroll), (gpointer)this); |
| 75 |
g_signal_connect(G_OBJECT(scrollBar), "scroll-event", G_CALLBACK(HexEdit_mouseScroll), (gpointer)this); |
| 76 |
|
| 77 |
pWidget::construct(); |
| 78 |
} |
| 79 |
|
| 80 |
auto pHexEdit::destruct() -> void { |
| 81 |
gtk_widget_destroy(scrollBar); |
| 82 |
gtk_widget_destroy(subWidget); |
| 83 |
gtk_widget_destroy(container); |
| 84 |
gtk_widget_destroy(gtkWidget); |
| 85 |
} |
| 86 |
|
| 87 |
auto pHexEdit::focused() const -> bool { |
| 88 |
return gtk_widget_has_focus(subWidget) || gtk_widget_has_focus(scrollBar); |
| 89 |
} |
| 90 |
|
| 91 |
auto pHexEdit::setAddress(u32 address) -> void { |
| 92 |
setScroll(); |
| 93 |
updateScroll(); |
| 94 |
update(); |
| 95 |
} |
| 96 |
|
| 97 |
auto pHexEdit::setBackgroundColor(Color color) -> void { |
| 98 |
GdkColor gdkColor = CreateColor(color); |
| 99 |
gtk_widget_modify_base(subWidget, GTK_STATE_NORMAL, color ? &gdkColor : nullptr); |
| 100 |
} |
| 101 |
|
| 102 |
auto pHexEdit::setColumns(u32 columns) -> void { |
| 103 |
setScroll(); |
| 104 |
update(); |
| 105 |
} |
| 106 |
|
| 107 |
auto pHexEdit::setForegroundColor(Color color) -> void { |
| 108 |
GdkColor gdkColor = CreateColor(color); |
| 109 |
gtk_widget_modify_text(subWidget, GTK_STATE_NORMAL, color ? &gdkColor : nullptr); |
| 110 |
} |
| 111 |
|
| 112 |
auto pHexEdit::setLength(u32 length) -> void { |
| 113 |
setScroll(); |
| 114 |
update(); |
| 115 |
} |
| 116 |
|
| 117 |
auto pHexEdit::setRows(u32 rows) -> void { |
| 118 |
setScroll(); |
| 119 |
update(); |
| 120 |
} |
| 121 |
|
| 122 |
auto pHexEdit::update() -> void { |
| 123 |
if(!state().onRead) { |
| 124 |
gtk_text_buffer_set_text(textBuffer, "", -1); |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
u32 position = cursorPosition(); |
| 129 |
|
| 130 |
string output; |
| 131 |
u32 address = state().address; |
| 132 |
for(auto row : range(state().rows)) { |
| 133 |
output.append(hex(address, 8L)); |
| 134 |
output.append(" "); |
| 135 |
|
| 136 |
string hexdata; |
| 137 |
string ansidata = " "; |
| 138 |
for(auto column : range(state().columns)) { |
| 139 |
if(address < state().length) { |
| 140 |
u8 data = self().doRead(address++); |
| 141 |
hexdata.append(hex(data, 2L)); |
| 142 |
hexdata.append(" "); |
| 143 |
ansidata.append(data >= 0x20 && data <= 0x7e ? (char)data : '.'); |
| 144 |
} else { |
| 145 |
hexdata.append(" "); |
| 146 |
ansidata.append(" "); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
output.append(hexdata); |
| 151 |
output.append(ansidata); |
| 152 |
if(address >= state().length) break; |
| 153 |
if(row != state().rows - 1) output.append("\n"); |
| 154 |
} |
| 155 |
|
| 156 |
gtk_text_buffer_set_text(textBuffer, output, -1); |
| 157 |
if(position == 0) position = 10; //start at first position where hex values can be entered |
| 158 |
setCursorPosition(position); |
| 159 |
} |
| 160 |
|
| 161 |
auto pHexEdit::cursorPosition() -> u32 { |
| 162 |
GtkTextIter iter; |
| 163 |
gtk_text_buffer_get_iter_at_mark(textBuffer, &iter, textCursor); |
| 164 |
return gtk_text_iter_get_offset(&iter); |
| 165 |
} |
| 166 |
|
| 167 |
auto pHexEdit::keyPress(u32 scancode, u32 mask) -> bool { |
| 168 |
if(!state().onRead) return false; |
| 169 |
|
| 170 |
if(mask & (GDK_CONTROL_MASK | GDK_MOD1_MASK | GDK_SUPER_MASK)) return false; //allow actions such as Ctrl+C (copy) |
| 171 |
|
| 172 |
s32 position = cursorPosition(); |
| 173 |
s32 lineWidth = 10 + (state().columns * 3) + 1 + state().columns + 1; |
| 174 |
s32 cursorY = position / lineWidth; |
| 175 |
s32 cursorX = position % lineWidth; |
| 176 |
|
| 177 |
if(scancode == GDK_KEY_Home) { |
| 178 |
setCursorPosition(cursorY * lineWidth + 10); |
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
if(scancode == GDK_KEY_End) { |
| 183 |
setCursorPosition(cursorY * lineWidth + 10 + (state().columns * 3 - 1)); |
| 184 |
return true; |
| 185 |
} |
| 186 |
|
| 187 |
if(scancode == GDK_KEY_Up) { |
| 188 |
if(cursorY != 0) return false; |
| 189 |
|
| 190 |
s32 newAddress = state().address - state().columns; |
| 191 |
if(newAddress >= 0) { |
| 192 |
self().setAddress(newAddress); |
| 193 |
update(); |
| 194 |
} |
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
if(scancode == GDK_KEY_Down) { |
| 199 |
if(cursorY >= rows() - 1) return true; |
| 200 |
if(cursorY != state().rows - 1) return false; |
| 201 |
|
| 202 |
s32 newAddress = state().address + state().columns; |
| 203 |
if(newAddress + state().columns * state().rows - (state().columns - 1) <= state().length) { |
| 204 |
self().setAddress(newAddress); |
| 205 |
update(); |
| 206 |
} |
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
if(scancode == GDK_KEY_Page_Up) { |
| 211 |
s32 newAddress = state().address - state().columns * state().rows; |
| 212 |
if(newAddress >= 0) { |
| 213 |
self().setAddress(newAddress); |
| 214 |
} else { |
| 215 |
self().setAddress(0); |
| 216 |
} |
| 217 |
update(); |
| 218 |
return true; |
| 219 |
} |
| 220 |
|
| 221 |
if(scancode == GDK_KEY_Page_Down) { |
| 222 |
s32 newAddress = state().address + state().columns * state().rows; |
| 223 |
for(auto n : range(state().rows)) { |
| 224 |
if(newAddress + state().columns * state().rows - (state().columns - 1) <= state().length) { |
| 225 |
self().setAddress(newAddress); |
| 226 |
update(); |
| 227 |
break; |
| 228 |
} |
| 229 |
newAddress -= state().columns; |
| 230 |
} |
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
//convert scancode to hex nibble |
| 235 |
if(scancode >= '0' && scancode <= '9') scancode = scancode - '0'; |
| 236 |
else if(scancode >= 'A' && scancode <= 'F') scancode = scancode - 'A' + 10; |
| 237 |
else if(scancode >= 'a' && scancode <= 'f') scancode = scancode - 'a' + 10; |
| 238 |
else return false; //not a valid hex value |
| 239 |
|
| 240 |
if(cursorX >= 10) { |
| 241 |
//not on an offset |
| 242 |
cursorX -= 10; |
| 243 |
if((cursorX % 3) != 2) { |
| 244 |
//not on a space |
| 245 |
bool cursorNibble = (cursorX % 3) == 1; //0 = high, 1 = low |
| 246 |
cursorX /= 3; |
| 247 |
if(cursorX < state().columns) { |
| 248 |
//not in ANSI region |
| 249 |
u32 address = state().address + (cursorY * state().columns + cursorX); |
| 250 |
|
| 251 |
if(address >= state().length) return false; //do not edit past end of data |
| 252 |
u8 data = self().doRead(address); |
| 253 |
|
| 254 |
//write modified value |
| 255 |
if(cursorNibble == 1) { |
| 256 |
data = (data & 0xf0) | (scancode << 0); |
| 257 |
} else { |
| 258 |
data = (data & 0x0f) | (scancode << 4); |
| 259 |
} |
| 260 |
self().doWrite(address, data); |
| 261 |
|
| 262 |
//auto-advance cursor to next nibble/byte |
| 263 |
position++; |
| 264 |
if(cursorNibble && cursorX != state().columns - 1) position++; |
| 265 |
setCursorPosition(position); |
| 266 |
|
| 267 |
//refresh output to reflect modified data |
| 268 |
update(); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
return true; |
| 274 |
} |
| 275 |
|
| 276 |
//number of actual rows |
| 277 |
auto pHexEdit::rows() -> s32 { |
| 278 |
return (max(1u, state().length) + state().columns - 1) / state().columns; |
| 279 |
} |
| 280 |
|
| 281 |
//number of scrollable row positions |
| 282 |
auto pHexEdit::rowsScrollable() -> s32 { |
| 283 |
return max(0u, rows() - state().rows); |
| 284 |
} |
| 285 |
|
| 286 |
auto pHexEdit::scroll(s32 position) -> void { |
| 287 |
if(position > rowsScrollable()) position = rowsScrollable(); |
| 288 |
if(position < 0) position = 0; |
| 289 |
self().setAddress(position * state().columns); |
| 290 |
} |
| 291 |
|
| 292 |
auto pHexEdit::setCursorPosition(u32 position) -> void { |
| 293 |
GtkTextIter iter; |
| 294 |
gtk_text_buffer_get_iter_at_mark(textBuffer, &iter, textCursor); |
| 295 |
|
| 296 |
//GTK+ will throw many errors to the terminal if you set iterator past end of buffer |
| 297 |
GtkTextIter endIter; |
| 298 |
gtk_text_buffer_get_end_iter(textBuffer, &iter); |
| 299 |
u32 endPosition = gtk_text_iter_get_offset(&iter); |
| 300 |
|
| 301 |
gtk_text_iter_set_offset(&iter, min(position, endPosition)); |
| 302 |
gtk_text_buffer_place_cursor(textBuffer, &iter); |
| 303 |
} |
| 304 |
|
| 305 |
auto pHexEdit::setScroll() -> void { |
| 306 |
if(rowsScrollable() > 0) { |
| 307 |
gtk_range_set_range(GTK_RANGE(scrollBar), 0, rowsScrollable()); |
| 308 |
gtk_widget_set_sensitive(scrollBar, true); |
| 309 |
} else { |
| 310 |
gtk_widget_set_sensitive(scrollBar, false); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
auto pHexEdit::updateScroll() -> void { |
| 315 |
u32 row = state().address / state().columns; |
| 316 |
gtk_range_set_value(GTK_RANGE(scrollBar), row); |
| 317 |
} |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
#endif |