Skip to content

Commit

Permalink
Add non-modal "Jump to offset" dialog option (#242).
Browse files Browse the repository at this point in the history
  • Loading branch information
solemnwarning committed Sep 22, 2024
1 parent 3ac6518 commit 1d18db8
Show file tree
Hide file tree
Showing 17 changed files with 418 additions and 99 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ APP_OBJS := \
src/FileWriter.$(BUILD_TYPE).o \
src/FillRangeDialog.$(BUILD_TYPE).o \
src/FixedSizeValueRegion.$(BUILD_TYPE).o \
src/GotoOffsetDialog.$(BUILD_TYPE).o \
src/HighlightColourMap.$(BUILD_TYPE).o \
src/HSVColour.$(BUILD_TYPE).o \
src/IntelHexExport.$(BUILD_TYPE).o \
Expand All @@ -417,6 +418,7 @@ APP_OBJS := \
src/search.$(BUILD_TYPE).o \
src/SettingsDialog.$(BUILD_TYPE).o \
src/SettingsDialogByteColour.$(BUILD_TYPE).o \
src/SettingsDialogGeneral.$(BUILD_TYPE).o \
src/SettingsDialogHighlights.$(BUILD_TYPE).o \
src/SettingsDialogKeyboard.$(BUILD_TYPE).o \
src/StringPanel.$(BUILD_TYPE).o \
Expand Down Expand Up @@ -499,6 +501,7 @@ TEST_OBJS := \
src/FileWriter.$(BUILD_TYPE).o \
src/FillRangeDialog.$(BUILD_TYPE).o \
src/FixedSizeValueRegion.$(BUILD_TYPE).o \
src/GotoOffsetDialog.$(BUILD_TYPE).o \
src/HighlightColourMap.$(BUILD_TYPE).o \
src/HSVColour.$(BUILD_TYPE).o \
src/IntelHexExport.$(BUILD_TYPE).o \
Expand All @@ -515,6 +518,7 @@ TEST_OBJS := \
src/search.$(BUILD_TYPE).o \
src/SettingsDialog.$(BUILD_TYPE).o \
src/SettingsDialogByteColour.$(BUILD_TYPE).o \
src/SettingsDialogGeneral.$(BUILD_TYPE).o \
src/SettingsDialogHighlights.$(BUILD_TYPE).o \
src/SettingsDialogKeyboard.$(BUILD_TYPE).o \
src/StringPanel.$(BUILD_TYPE).o \
Expand Down
2 changes: 2 additions & 0 deletions msvc/all-tests/all-tests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@
<ClCompile Include="..\..\src\FileWriter.cpp" />
<ClCompile Include="..\..\src\FillRangeDialog.cpp" />
<ClCompile Include="..\..\src\FixedSizeValueRegion.cpp" />
<ClCompile Include="..\..\src\GotoOffsetDialog.cpp" />
<ClCompile Include="..\..\src\HighlightColourMap.cpp" />
<ClCompile Include="..\..\src\HSVColour.cpp" />
<ClCompile Include="..\..\src\IntelHexExport.cpp" />
Expand All @@ -270,6 +271,7 @@
<ClCompile Include="..\..\src\search.cpp" />
<ClCompile Include="..\..\src\SettingsDialog.cpp" />
<ClCompile Include="..\..\src\SettingsDialogByteColour.cpp" />
<ClCompile Include="..\..\src\SettingsDialogGeneral.cpp" />
<ClCompile Include="..\..\src\SettingsDialogHighlights.cpp" />
<ClCompile Include="..\..\src\SettingsDialogKeyboard.cpp" />
<ClCompile Include="..\..\src\StringPanel.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions msvc/all-tests/all-tests.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@
<ClCompile Include="..\..\tests\WindowCommands.cpp">
<Filter>tests</Filter>
</ClCompile>
<ClCompile Include="..\..\src\GotoOffsetDialog.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\SettingsDialogGeneral.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\res\ascii16.h">
Expand Down
2 changes: 2 additions & 0 deletions msvc/rehex.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@
<ClCompile Include="..\src\FileWriter.cpp" />
<ClCompile Include="..\src\FillRangeDialog.cpp" />
<ClCompile Include="..\src\FixedSizeValueRegion.cpp" />
<ClCompile Include="..\src\GotoOffsetDialog.cpp" />
<ClCompile Include="..\src\HighlightColourMap.cpp" />
<ClCompile Include="..\src\HSVColour.cpp" />
<ClCompile Include="..\src\IntelHexExport.cpp" />
Expand All @@ -485,6 +486,7 @@
<ClCompile Include="..\src\search.cpp" />
<ClCompile Include="..\src\SettingsDialog.cpp" />
<ClCompile Include="..\src\SettingsDialogByteColour.cpp" />
<ClCompile Include="..\src\SettingsDialogGeneral.cpp" />
<ClCompile Include="..\src\SettingsDialogHighlights.cpp" />
<ClCompile Include="..\src\SettingsDialogKeyboard.cpp" />
<ClCompile Include="..\src\StringPanel.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions msvc/rehex.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@
<ClCompile Include="..\src\CustomNumericType.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\GotoOffsetDialog.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\SettingsDialogGeneral.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\AboutDialog.hpp">
Expand Down
17 changes: 16 additions & 1 deletion src/AppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ REHex::AppSettings::AppSettings():
goto_offset_base(GotoOffsetBase::AUTO),
highlight_colours(HighlightColourMap::defaults()),
main_window_commands(MainWindow::get_template_commands()),
cursor_nav_mode(CursorNavMode::BYTE)
cursor_nav_mode(CursorNavMode::BYTE),
goto_offset_modal(true)
{
ByteColourMap bcm_types;
bcm_types.set_label("ASCII Values");
Expand Down Expand Up @@ -169,6 +170,8 @@ REHex::AppSettings::AppSettings(wxConfig *config): AppSettings()
break;
}

goto_offset_modal = config->ReadBool("goto-offset-modal", goto_offset_modal);

wxGetApp().Bind(PALETTE_CHANGED, &REHex::AppSettings::OnColourPaletteChanged, this);
}

Expand Down Expand Up @@ -213,6 +216,8 @@ void REHex::AppSettings::write(wxConfig *config)
wxConfigPathChanger scoped_path(config, "main-window-accelerators/");
main_window_commands.save_accelerators(config);
}

config->Write("goto-offset-modal", goto_offset_modal);
}

REHex::AsmSyntax REHex::AppSettings::get_preferred_asm_syntax() const
Expand Down Expand Up @@ -326,6 +331,16 @@ REHex::BitOffset REHex::AppSettings::get_cursor_nav_alignment() const
abort(); /* Unreachable. */
}

bool REHex::AppSettings::get_goto_offset_modal() const
{
return goto_offset_modal;
}

void REHex::AppSettings::set_goto_offset_modal(bool goto_offset_modal)
{
this->goto_offset_modal = goto_offset_modal;
}

void REHex::AppSettings::OnColourPaletteChanged(wxCommandEvent &event)
{
highlight_colours.set_default_lightness(active_palette->get_default_highlight_lightness());
Expand Down
4 changes: 4 additions & 0 deletions src/AppSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,17 @@ namespace REHex
void set_cursor_nav_mode(CursorNavMode cursor_nav_mode);
BitOffset get_cursor_nav_alignment() const;

bool get_goto_offset_modal() const;
void set_goto_offset_modal(bool goto_offset_modal);

private:
AsmSyntax preferred_asm_syntax;
GotoOffsetBase goto_offset_base;
HighlightColourMap highlight_colours;
std::map< int, std::shared_ptr<ByteColourMap> > byte_colour_maps;
WindowCommandTable main_window_commands;
CursorNavMode cursor_nav_mode;
bool goto_offset_modal;

void OnColourPaletteChanged(wxCommandEvent &event);
};
Expand Down
133 changes: 133 additions & 0 deletions src/GotoOffsetDialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* Reverse Engineer's Hex Editor
* Copyright (C) 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
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "platform.hpp"

#include "App.hpp"
#include "GotoOffsetDialog.hpp"

BEGIN_EVENT_TABLE(REHex::GotoOffsetDialog, REHex::NumericEntryDialog<REHex::BitOffset>)
EVT_BUTTON(wxID_OK, REHex::GotoOffsetDialog::OnOK)
EVT_BUTTON(wxID_CANCEL, REHex::GotoOffsetDialog::OnCancel)

EVT_CLOSE(REHex::GotoOffsetDialog::OnClose)
END_EVENT_TABLE()

REHex::GotoOffsetDialog::GotoOffsetDialog(wxWindow *parent, const SharedDocumentPointer &document):
NumericEntryDialog(
parent,
"Jump to offset",
"Prefix offset with -/+ to jump relative to current cursor position",
document->get_cursor_position(),
BitOffset::ZERO,
BitOffset::MAX,
document->get_cursor_position(),
get_last_base()),
document(document),
is_modal(false)
{
this->document.auto_cleanup_bind(CURSOR_UPDATE, &REHex::GotoOffsetDialog::OnDocumentCursorUpdate, this);
}

int REHex::GotoOffsetDialog::ShowModal()
{
bool was_modal = is_modal;

is_modal = true;
int result = wxDialog::ShowModal();
is_modal = was_modal;

return result;
}

REHex::NumericEntryDialog<REHex::BitOffset>::BaseHint REHex::GotoOffsetDialog::get_last_base()
{
NumericEntryDialog<BitOffset>::BaseHint base;
switch(wxGetApp().settings->get_goto_offset_base())
{
case GotoOffsetBase::AUTO:
base = NumericEntryDialog<BitOffset>::BaseHint::AUTO;
break;

case GotoOffsetBase::OCT:
base = NumericEntryDialog<BitOffset>::BaseHint::OCT;
break;

case GotoOffsetBase::DEC:
base = NumericEntryDialog<BitOffset>::BaseHint::DEC;
break;

case GotoOffsetBase::HEX:
base = NumericEntryDialog<BitOffset>::BaseHint::HEX;
break;
}

return base;
}

void REHex::GotoOffsetDialog::OnDocumentCursorUpdate(CursorUpdateEvent &event)
{
set_rel_base(event.cursor_pos);
event.Skip(); /* Continue propagation. */
}

void REHex::GotoOffsetDialog::OnOK(wxCommandEvent &event)
{
NumericEntryDialog<BitOffset>::BaseHint base = GetBase();

switch(base)
{
case NumericEntryDialog<BitOffset>::BaseHint::AUTO:
wxGetApp().settings->set_goto_offset_base(GotoOffsetBase::AUTO);
break;

case NumericEntryDialog<BitOffset>::BaseHint::OCT:
wxGetApp().settings->set_goto_offset_base(GotoOffsetBase::OCT);
break;

case NumericEntryDialog<BitOffset>::BaseHint::DEC:
wxGetApp().settings->set_goto_offset_base(GotoOffsetBase::DEC);
break;

case NumericEntryDialog<BitOffset>::BaseHint::HEX:
wxGetApp().settings->set_goto_offset_base(GotoOffsetBase::HEX);
break;

default:
/* Unreachable. */
abort();
}

/* TODO: Check position is valid. */

document->set_cursor_position(GetValue());

if(is_modal)
{
Close();
}
}

void REHex::GotoOffsetDialog::OnCancel(wxCommandEvent &event)
{
Close();
}

void REHex::GotoOffsetDialog::OnClose(wxCloseEvent &event)
{
Destroy();
}
59 changes: 59 additions & 0 deletions src/GotoOffsetDialog.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Reverse Engineer's Hex Editor
* Copyright (C) 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
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifndef REHEX_GOTOFFSETDIALOG_HPP
#define REHEX_GOTOFFSETDIALOG_HPP

#include "BitOffset.hpp"
#include "NumericEntryDialog.hpp"
#include "SharedDocumentPointer.hpp"

namespace REHex {
/**
* @brief "Jump to offset" dialog.
*
* This specialisation of NumericEntryDialog provides the dialog for the "Jump to offset"
* command in the Edit menu and handles moving the cursor position.
*
* Supports modal and modeless operation.
*
* Unlike typical dialogs, this must ALWAYS be heap allocated (even for modal use).
*/
class GotoOffsetDialog: public NumericEntryDialog<BitOffset>
{
public:
GotoOffsetDialog(wxWindow *parent, const SharedDocumentPointer &document);

virtual int ShowModal() override;

private:
SharedDocumentPointer document;
bool is_modal;

static BaseHint get_last_base();

void OnDocumentCursorUpdate(CursorUpdateEvent &event);

void OnOK(wxCommandEvent &event);
void OnCancel(wxCommandEvent &event);
void OnClose(wxCloseEvent &event);

DECLARE_EVENT_TABLE()
};
}

#endif /* !REHEX_GOTOFFSETDIALOG_HPP */
7 changes: 6 additions & 1 deletion src/NumericEntryDialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace REHex {
private:
const T min_value;
const T max_value;
const T rel_base;
T rel_base;

BaseHint base;

Expand Down Expand Up @@ -189,6 +189,11 @@ namespace REHex {
});
}

void set_rel_base(T rel_base)
{
this->rel_base = rel_base;
}

T GetValue()
{
switch(base)
Expand Down
Loading

0 comments on commit 1d18db8

Please sign in to comment.