Skip to content

Commit

Permalink
[WIP] auto highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
solemnwarning committed Apr 10, 2024
1 parent 0315194 commit 3f3041b
Show file tree
Hide file tree
Showing 9 changed files with 696 additions and 34 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -393,6 +393,7 @@ APP_OBJS := \
src/RangeDialog.$(BUILD_TYPE).o \
src/RangeProcessor.$(BUILD_TYPE).o \
src/search.$(BUILD_TYPE).o \
src/SettingsDialog.$(BUILD_TYPE).o \
src/StringPanel.$(BUILD_TYPE).o \
src/textentrydialog.$(BUILD_TYPE).o \
src/Tab.$(BUILD_TYPE).o \
Expand Down
104 changes: 72 additions & 32 deletions src/DocumentCtrl.cpp
Expand Up @@ -76,7 +76,7 @@ static unsigned int pack_colour(const wxColour &colour)
return (unsigned int)(colour.Red()) | ((unsigned int)(colour.Blue()) << 8) | ((unsigned int)(colour.Green()) << 16);
}

REHex::DocumentCtrl::DocumentCtrl(wxWindow *parent, SharedDocumentPointer &doc):
REHex::DocumentCtrl::DocumentCtrl(wxWindow *parent, SharedDocumentPointer &doc, long style):
wxControl(),
doc(doc),
hex_font(wxFontInfo().FaceName(wxGetApp().get_font_name())),
Expand Down Expand Up @@ -108,7 +108,7 @@ REHex::DocumentCtrl::DocumentCtrl(wxWindow *parent, SharedDocumentPointer &doc):
/* The background style MUST be set before the control is created. */
SetBackgroundStyle(wxBG_STYLE_PAINT);
Create(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
(wxVSCROLL | wxHSCROLL | wxWANTS_CHARS));
(wxVSCROLL | wxHSCROLL | wxWANTS_CHARS | style));

client_width = 0;
client_height = 0;
Expand Down Expand Up @@ -937,7 +937,14 @@ void REHex::DocumentCtrl::_handle_width_change()
}

/* TODO: Preserve/scale the position as the window size changes. */
SetScrollbar(wxHORIZONTAL, 0, client_width, virtual_width);

if((GetWindowStyle() & DCTRL_LOCK_SCROLL) == 0)
{
SetScrollbar(wxHORIZONTAL, 0, client_width, virtual_width);
}
else{
SetScrollbar(wxHORIZONTAL, 0, 0, 0);
}

/* Update vertical scrollbar, since we just recalculated the height of the document. */
_update_vscroll();
Expand Down Expand Up @@ -1031,6 +1038,11 @@ void REHex::DocumentCtrl::_update_vscroll()
scroll_yoff_max = 0;
}

if((GetWindowStyle() & DCTRL_LOCK_SCROLL) != 0)
{
SetScrollbar(wxVERTICAL, 0, 0, 0);
}

linked_scroll_visit_others([this](DocumentCtrl *other)
{
other->scroll_yoff = scroll_yoff;
Expand All @@ -1047,6 +1059,11 @@ void REHex::DocumentCtrl::_update_vscroll()

void REHex::DocumentCtrl::_update_vscroll_pos(bool update_linked_scroll_others)
{
if((GetWindowStyle() & DCTRL_LOCK_SCROLL) != 0)
{
return;
}

int range = GetScrollRange(wxVERTICAL);
int thumb = GetScrollThumb(wxVERTICAL);

Expand Down Expand Up @@ -1207,6 +1224,11 @@ void REHex::DocumentCtrl::restore_scroll_position()

void REHex::DocumentCtrl::OnScroll(wxScrollWinEvent &event)
{
if((GetWindowStyle() & DCTRL_LOCK_SCROLL) != 0)
{
return;
}

wxEventType type = event.GetEventType();
int orientation = event.GetOrientation();

Expand Down Expand Up @@ -1307,6 +1329,11 @@ void REHex::DocumentCtrl::OnScroll(wxScrollWinEvent &event)

void REHex::DocumentCtrl::OnWheel(wxMouseEvent &event)
{
if((GetWindowStyle() & DCTRL_LOCK_SCROLL) != 0)
{
return;
}

wxMouseWheelAxis axis = event.GetWheelAxis();
int delta = event.GetWheelDelta();
int ticks_per_delta = event.GetLinesPerAction();
Expand Down Expand Up @@ -1618,7 +1645,7 @@ void REHex::DocumentCtrl::OnChar(wxKeyEvent &event)

_set_cursor_position(new_cursor_pos, Document::CSTATE_GOTO);

if (update_scrollpos)
if (update_scrollpos && (GetWindowStyle() & DCTRL_LOCK_SCROLL) == 0)
{
scroll_yoff = new_scroll_yoff;
_update_vscroll_pos();
Expand Down Expand Up @@ -1988,40 +2015,43 @@ void REHex::DocumentCtrl::OnMotionTick(int mouse_x, int mouse_y)

wxClientDC dc(this);

int scroll_xoff_max = GetScrollRange(wxHORIZONTAL) - GetScrollThumb(wxHORIZONTAL);

if(mouse_x < 0)
{
scroll_xoff -= std::min(abs(mouse_x), scroll_xoff);
SetScrollPos(wxHORIZONTAL, scroll_xoff);

mouse_x = 0;
}
else if(mouse_x >= client_width)
if((GetWindowStyle() & DCTRL_LOCK_SCROLL) == 0)
{
scroll_xoff += std::min((int)(mouse_x - client_width), (scroll_xoff_max - scroll_xoff));
SetScrollPos(wxHORIZONTAL, scroll_xoff);
int scroll_xoff_max = GetScrollRange(wxHORIZONTAL) - GetScrollThumb(wxHORIZONTAL);

mouse_x = client_width - 1;
}

if(mouse_y < 0)
{
scroll_yoff -= std::min((int64_t)(abs(mouse_y) / hf_height + 1), scroll_yoff);
_update_vscroll_pos();
if(mouse_x < 0)
{
scroll_xoff -= std::min(abs(mouse_x), scroll_xoff);
SetScrollPos(wxHORIZONTAL, scroll_xoff);

mouse_x = 0;
}
else if(mouse_x >= client_width)
{
scroll_xoff += std::min((int)(mouse_x - client_width), (scroll_xoff_max - scroll_xoff));
SetScrollPos(wxHORIZONTAL, scroll_xoff);

mouse_x = client_width - 1;
}

mouse_y = 0;
}
else if(mouse_y >= client_height)
{
scroll_yoff += std::min((int64_t)((mouse_y - client_height) / hf_height + 1), (scroll_yoff_max - scroll_yoff));
_update_vscroll_pos();
if(mouse_y < 0)
{
scroll_yoff -= std::min((int64_t)(abs(mouse_y) / hf_height + 1), scroll_yoff);
_update_vscroll_pos();

mouse_y = 0;
}
else if(mouse_y >= client_height)
{
scroll_yoff += std::min((int64_t)((mouse_y - client_height) / hf_height + 1), (scroll_yoff_max - scroll_yoff));
_update_vscroll_pos();

mouse_y = client_height - 1;
}

mouse_y = client_height - 1;
save_scroll_position();
}

save_scroll_position();

int rel_x = mouse_x + scroll_xoff;

/* Find the region containing the first visible line. */
Expand Down Expand Up @@ -2685,6 +2715,11 @@ void REHex::DocumentCtrl::_make_x_visible(int x_px, int width_px)
*/
void REHex::DocumentCtrl::_make_byte_visible(BitOffset offset)
{
if((GetWindowStyle() & DCTRL_LOCK_SCROLL) != 0)
{
return;
}

auto dr = _data_region_by_offset(offset);
assert(dr != data_regions.end());

Expand Down Expand Up @@ -3199,6 +3234,11 @@ void REHex::DocumentCtrl::set_scroll_yoff(int64_t scroll_yoff, bool update_linke

void REHex::DocumentCtrl::set_scroll_yoff_clamped(int64_t scroll_yoff)
{
if((GetWindowStyle() & DCTRL_LOCK_SCROLL) != 0)
{
return;
}

if(scroll_yoff < 0)
{
scroll_yoff = 0;
Expand Down
6 changes: 5 additions & 1 deletion src/DocumentCtrl.hpp
Expand Up @@ -41,6 +41,10 @@
#include "SharedDocumentPointer.hpp"
#include "util.hpp"

/* DocumentCtrl style flags */
#define DCTRL_LOCK_SCROLL 1
#define DCTRL_HIDE_CURSOR 2

namespace REHex {
class DocumentCtrl: public wxControl {
public:
Expand Down Expand Up @@ -407,7 +411,7 @@ namespace REHex {
wx_char(wx_char), unicode_char(unicode_char), char_size(char_size), column(column) {}
};

DocumentCtrl(wxWindow *parent, SharedDocumentPointer &doc);
DocumentCtrl(wxWindow *parent, SharedDocumentPointer &doc, long style = 0);
~DocumentCtrl();

static const int BYTES_PER_LINE_FIT_BYTES = 0;
Expand Down
28 changes: 28 additions & 0 deletions src/Palette.cpp
Expand Up @@ -266,3 +266,31 @@ REHex::Palette *REHex::Palette::create_dark_palette()

return new Palette("dark", "Dark", colours);
}

REHex::ByteColourMap::ByteColourMap() {}

// REHex::ByteColourMap::ByteColourMap(const wxConfigBase *config)

void REHex::ByteColourMap::set_colour(unsigned char min_byte, unsigned char max_byte, wxColour colour)
{
map.set_range(min_byte, max_byte - min_byte + 1, colour);
}

void REHex::ByteColourMap::clear_colour(unsigned char min_byte, unsigned char max_byte)
{
map.clear_range(min_byte, max_byte - min_byte + 1);
}

const wxColour *REHex::ByteColourMap::get_colour(unsigned char byte) const
{
auto it = map.get_range(byte);
if(it != map.end())
{
return &(it->second);
}
else{
return NULL;
}
}

// void REHex::ByteColourMap::save(wxConfigBase *config);
26 changes: 25 additions & 1 deletion src/Palette.hpp
@@ -1,5 +1,5 @@
/* Reverse Engineer's Hex Editor
* Copyright (C) 2018-2020 Daniel Collins <solemnwarning@solemnwarning.net>
* Copyright (C) 2018-2024 Daniel Collins <solemnwarning@solemnwarning.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
Expand All @@ -20,8 +20,28 @@

#include <string>
#include <wx/colour.h>
#include <wx/config.h>

#include "ByteRangeMap.hpp"

namespace REHex {
class ByteColourMap
{
private:
ByteRangeMap<wxColour> map;

public:
ByteColourMap();
ByteColourMap(const wxConfigBase *config);

void set_colour(unsigned char min_byte, unsigned char max_byte, wxColour colour);
void clear_colour(unsigned char min_byte, unsigned char max_byte);

const wxColour *get_colour(unsigned char byte) const;

void save(wxConfigBase *config);
};

/**
* @brief Colour palette to use when drawing custom controls.
*/
Expand Down Expand Up @@ -90,6 +110,10 @@ namespace REHex {
*/
const wxColour &get_highlight_fg(int highlight_idx) const;

wxColour get_byte_fg(unsigned char byte, bool alternate_row) const;

wxColour get_byte_bg(unsigned char byte, bool alternate_row) const;

/**
* @brief Get the background colour palette index for the given text highlight colour.
*
Expand Down

0 comments on commit 3f3041b

Please sign in to comment.