2021-05-23 08:36:30 +00:00
/*
2022-02-10 19:28:48 +00:00
* Copyright ( c ) 2021 - 2022 , the SerenityOS developers .
2021-05-23 08:36:30 +00:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# include "GoToOffsetDialog.h"
# include <LibGUI/BoxLayout.h>
# include <LibGUI/Button.h>
# include <LibGUI/ComboBox.h>
# include <LibGUI/ItemListModel.h>
# include <LibGUI/Label.h>
# include <LibGUI/MessageBox.h>
# include <LibGUI/Statusbar.h>
# include <LibGUI/TextBox.h>
# include <LibGUI/Widget.h>
2022-05-13 12:10:27 +00:00
GUI : : Dialog : : ExecResult GoToOffsetDialog : : show ( GUI : : Window * parent_window , int & history_offset , int & out_offset , int selection_offset , int buffer_size )
2021-05-23 08:36:30 +00:00
{
2023-09-28 15:06:49 +00:00
auto dialog_or_error = GoToOffsetDialog : : try_create ( ) ;
if ( dialog_or_error . is_error ( ) ) {
GUI : : MessageBox : : show ( parent_window , " Couldn't load \" go to offset \" dialog " sv , " Error while opening \" go to offset \" dialog " sv , GUI : : MessageBox : : Type : : Error ) ;
return ExecResult : : Aborted ;
}
auto dialog = dialog_or_error . release_value ( ) ;
2021-05-23 08:36:30 +00:00
dialog - > m_selection_offset = selection_offset ;
dialog - > m_buffer_size = buffer_size ;
if ( parent_window )
dialog - > set_icon ( parent_window - > icon ( ) ) ;
if ( history_offset )
2023-08-13 17:16:49 +00:00
dialog - > m_text_editor - > set_text ( String : : number ( history_offset ) . release_value_but_fixme_should_propagate_errors ( ) ) ;
2021-05-23 08:36:30 +00:00
auto result = dialog - > exec ( ) ;
2022-05-13 12:10:27 +00:00
if ( result ! = ExecResult : : OK )
2021-05-23 08:36:30 +00:00
return result ;
auto input_offset = dialog - > process_input ( ) ;
history_offset = move ( input_offset ) ;
auto new_offset = dialog - > calculate_new_offset ( input_offset ) ;
dbgln ( " Go to offset: value={} " , new_offset ) ;
out_offset = move ( new_offset ) ;
2022-05-13 12:10:27 +00:00
return ExecResult : : OK ;
2021-05-23 08:36:30 +00:00
}
int GoToOffsetDialog : : process_input ( )
{
2023-12-16 14:19:34 +00:00
auto input_offset = String : : from_byte_string ( m_text_editor - > text ( ) . trim_whitespace ( ) ) . release_value_but_fixme_should_propagate_errors ( ) ;
2021-05-23 08:36:30 +00:00
int offset ;
auto type = m_offset_type_box - > text ( ) . trim_whitespace ( ) ;
if ( type = = " Decimal " ) {
2023-08-13 17:16:49 +00:00
offset = input_offset . to_number < int > ( ) . value_or ( 0 ) ;
2021-05-23 08:36:30 +00:00
} else if ( type = = " Hexadecimal " ) {
2023-08-13 17:16:49 +00:00
// FIXME: Find a better way of parsing hex to a number that doesn't require a zero terminated string
2023-12-16 14:19:34 +00:00
offset = strtol ( input_offset . to_byte_string ( ) . characters ( ) , nullptr , 16 ) ;
2021-05-23 08:36:30 +00:00
} else {
VERIFY_NOT_REACHED ( ) ;
}
return offset ;
}
int GoToOffsetDialog : : calculate_new_offset ( int input_offset )
{
int new_offset ;
auto from = m_offset_from_box - > text ( ) . trim_whitespace ( ) ;
if ( from = = " Start " ) {
new_offset = input_offset ;
} else if ( from = = " End " ) {
new_offset = m_buffer_size - input_offset ;
} else if ( from = = " Here " ) {
new_offset = input_offset + m_selection_offset ;
} else {
VERIFY_NOT_REACHED ( ) ;
}
if ( new_offset > m_buffer_size )
new_offset = m_buffer_size ;
if ( new_offset < 0 )
new_offset = 0 ;
return new_offset ;
}
void GoToOffsetDialog : : update_statusbar ( )
{
auto new_offset = calculate_new_offset ( process_input ( ) ) ;
2023-06-04 08:24:38 +00:00
m_statusbar - > set_text ( 0 , String : : formatted ( " HEX: {:#08X} " , new_offset ) . release_value_but_fixme_should_propagate_errors ( ) ) ;
m_statusbar - > set_text ( 1 , String : : formatted ( " DEC: {} " , new_offset ) . release_value_but_fixme_should_propagate_errors ( ) ) ;
2021-05-23 08:36:30 +00:00
}
2023-09-28 15:06:49 +00:00
ErrorOr < NonnullRefPtr < GoToOffsetDialog > > GoToOffsetDialog : : try_create ( )
{
auto offset_widget = TRY ( HexEditor : : GoToOffsetWidget : : try_create ( ) ) ;
auto offset_dialog = TRY ( adopt_nonnull_ref_or_enomem ( new ( nothrow )
GoToOffsetDialog ( move ( offset_widget ) ) ) ) ;
return offset_dialog ;
}
GoToOffsetDialog : : GoToOffsetDialog ( NonnullRefPtr < HexEditor : : GoToOffsetWidget > goto_offset_widget )
2021-05-23 08:36:30 +00:00
: Dialog ( nullptr )
{
resize ( 300 , 80 ) ;
center_on_screen ( ) ;
set_resizable ( false ) ;
set_title ( " Go to Offset " ) ;
2023-09-28 15:06:49 +00:00
set_main_widget ( goto_offset_widget ) ;
2021-05-23 08:36:30 +00:00
2023-09-28 15:06:49 +00:00
m_text_editor = * goto_offset_widget - > find_descendant_of_type_named < GUI : : TextBox > ( " text_editor " ) ;
m_go_button = * goto_offset_widget - > find_descendant_of_type_named < GUI : : Button > ( " go_button " ) ;
m_offset_type_box = * goto_offset_widget - > find_descendant_of_type_named < GUI : : ComboBox > ( " offset_type " ) ;
m_offset_from_box = * goto_offset_widget - > find_descendant_of_type_named < GUI : : ComboBox > ( " offset_from " ) ;
m_statusbar = * goto_offset_widget - > find_descendant_of_type_named < GUI : : Statusbar > ( " statusbar " ) ;
2021-05-23 08:36:30 +00:00
2023-08-13 17:16:49 +00:00
m_offset_type . append ( " Decimal " sv ) ;
m_offset_type . append ( " Hexadecimal " sv ) ;
m_offset_type_box - > set_model ( GUI : : ItemListModel < StringView > : : create ( m_offset_type ) ) ;
2021-05-23 08:36:30 +00:00
m_offset_type_box - > set_selected_index ( 0 ) ;
m_offset_type_box - > set_only_allow_values_from_model ( true ) ;
2023-08-13 17:16:49 +00:00
m_offset_from . append ( " Start " sv ) ;
m_offset_from . append ( " Here " sv ) ;
m_offset_from . append ( " End " sv ) ;
m_offset_from_box - > set_model ( GUI : : ItemListModel < StringView > : : create ( m_offset_from ) ) ;
2021-05-23 08:36:30 +00:00
m_offset_from_box - > set_selected_index ( 0 ) ;
m_offset_from_box - > set_only_allow_values_from_model ( true ) ;
m_go_button - > on_click = [ this ] ( auto ) {
2022-05-13 12:10:27 +00:00
done ( ExecResult : : OK ) ;
2021-05-23 08:36:30 +00:00
} ;
2022-05-27 17:36:06 +00:00
m_go_button - > set_default ( true ) ;
2021-05-23 08:36:30 +00:00
m_text_editor - > on_change = [ this ] ( ) {
auto text = m_text_editor - > text ( ) ;
2022-07-11 17:32:29 +00:00
if ( text . starts_with ( " 0x " sv ) ) {
2021-05-23 08:36:30 +00:00
m_offset_type_box - > set_selected_index ( 1 ) ;
2022-07-11 17:32:29 +00:00
m_text_editor - > set_text ( text . replace ( " 0x " sv , " " sv , ReplaceMode : : FirstOnly ) ) ;
2021-05-23 08:36:30 +00:00
}
update_statusbar ( ) ;
} ;
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 18:34:31 +00:00
m_offset_type_box - > on_change = [ this ] ( auto & , auto & ) {
2021-05-23 08:36:30 +00:00
update_statusbar ( ) ;
} ;
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 18:34:31 +00:00
m_offset_from_box - > on_change = [ this ] ( auto & , auto & ) {
2021-05-23 08:36:30 +00:00
update_statusbar ( ) ;
} ;
update_statusbar ( ) ;
}