* view.c (view_display_hex): Completely rewritten. Now the code

distinguishes several phases (state selection, color selection,
	printing) and does not mix them wildly, as the previous code
	did.
This commit is contained in:
Roland Illig 2005-08-18 03:33:33 +00:00
parent ba8ba542f0
commit b7cba56d21
2 changed files with 80 additions and 90 deletions

View File

@ -10,6 +10,10 @@
of the hex editor out of this function. Whoever wrote that of the hex editor out of this function. Whoever wrote that
should be blamed for it, but the code has been there since the should be blamed for it, but the code has been there since the
beginning. beginning.
* view.c (view_display_hex): Completely rewritten. Now the code
distinguishes several phases (state selection, color selection,
printing) and does not mix them wildly, as the previous code
did.
2005-08-16 Roland Illig <roland.illig@gmx.de> 2005-08-16 Roland Illig <roland.illig@gmx.de>

View File

@ -1596,10 +1596,10 @@ view_display_clean (WView *view)
#define view_gotoyx(v,r,c) widget_move (v,r,c) #define view_gotoyx(v,r,c) widget_move (v,r,c)
typedef enum { typedef enum {
MARK_NORMAL = 0, MARK_NORMAL,
MARK_SELECTED = 1, MARK_SELECTED,
MARK_CURSOR = 2, MARK_CURSOR,
MARK_CHANGED = 3 MARK_CHANGED
} mark_t; } mark_t;
static inline int static inline int
@ -1677,7 +1677,6 @@ view_display_hex (WView *view)
curr = curr->next; curr = curr->next;
} }
attrset (NORMAL_COLOR);
for (row = 0; get_byte (view, from) != -1 && row < height; row++) { for (row = 0; get_byte (view, from) != -1 && row < height; row++) {
/* Print the hex offset */ /* Print the hex offset */
attrset (MARKED_COLOR); attrset (MARKED_COLOR);
@ -1687,112 +1686,99 @@ view_display_hex (WView *view)
attrset (NORMAL_COLOR); attrset (NORMAL_COLOR);
col = hex_start; col = hex_start;
/* Each hex number is two digits */ for (bytes = 0; bytes < view->bytes_per_line; bytes++, from++) {
hex_buff[2] = '\0';
for (bytes = 0;
bytes < view->bytes_per_line
&& (c = get_byte (view, from)) != -1;
bytes++, from++) {
/* Display and mark changed bytes */
if (curr && from == curr->offset) {
c = curr->value;
curr = curr->next;
boldflag = MARK_CHANGED;
attrset (VIEW_UNDERLINED_COLOR);
}
if (view->found_len && from >= view->search_start if ((c = get_byte (view, from)) == -1)
&& from < view->search_start + view->found_len) { break;
boldflag = MARK_SELECTED;
attrset (MARKED_COLOR); /* Save the cursor position for view_place_cursor() */
} if (from == view->hex_cursor && !view->hexview_in_text) {
/* Display the navigation cursor */
if (from == view->hex_cursor) {
if (!view->hexview_in_text) {
view->cursor_row = row; view->cursor_row = row;
view->cursor_col = col; view->cursor_col = col;
} }
/* Determine the state of the current byte */
/* The current cursor position */
if (from == view->hex_cursor) {
boldflag = MARK_CURSOR; boldflag = MARK_CURSOR;
attrset (view->hexview_in_text
? MARKED_SELECTED_COLOR /* Changed bytes from the hex editor */
: VIEW_UNDERLINED_COLOR); } else if (curr && from == curr->offset) {
c = curr->value;
curr = curr->next;
boldflag = MARK_CHANGED;
/* Marked bytes from the search functions */
} else if (view->search_start <= from
&& from < view->search_start + view->found_len) {
boldflag = MARK_SELECTED;
} else {
boldflag = MARK_NORMAL;
} }
/* Print a hex number (sprintf is too slow) */ /* Select the color for the hex number */
hex_buff[0] = hex_char[(c >> 4)]; attrset (
hex_buff[1] = hex_char[c & 15]; boldflag == MARK_NORMAL ? NORMAL_COLOR :
view_gotoyx (view, top + row, left + col); boldflag == MARK_SELECTED ? MARKED_COLOR :
view_add_string (view, hex_buff); boldflag == MARK_CHANGED ? VIEW_UNDERLINED_COLOR :
col += 3; /* boldflag == MARK_CURSOR */
/* Turn off the cursor or changed byte highlighting here */ view->hexview_in_text ? MARKED_SELECTED_COLOR :
if (boldflag == MARK_CURSOR || boldflag == MARK_CHANGED) VIEW_UNDERLINED_COLOR);
attrset (NORMAL_COLOR);
if ((bytes & 3) == 3 && bytes + 1 < view->bytes_per_line) {
/* Turn off the search highlighting */
if (boldflag == MARK_SELECTED
&& from == view->search_start + view->found_len - 1)
attrset (NORMAL_COLOR);
/* Hex numbers are printed in the groups of four */ /* Print the hex number */
/* Groups are separated by a vline */
view_gotoyx (view, top + row, left + col - 1);
view_add_character (view, ' ');
view_gotoyx (view, top + row, left + col); view_gotoyx (view, top + row, left + col);
if (view->data_area.width < 80) view_add_character (view, hex_char[c / 16]);
col += 1; view_add_character (view, hex_char[c % 16]);
else {
view_add_one_vline ();
col += 2; col += 2;
/* Print the separator */
attrset (NORMAL_COLOR);
if (bytes != view->bytes_per_line - 1) {
view_add_character (view, ' ');
col += 1;
/* After every four bytes, print a group separator */
if (bytes % 4 == 3) {
if (view->data_area.width >= 80) {
view_add_one_vline ();
col += 1;
}
view_add_character (view, ' ');
col += 1;
}
} }
if (boldflag != MARK_NORMAL /* Select the color for the character; this differs from the
&& from == view->search_start + view->found_len - 1) * hex color when boldflag == MARK_CURSOR */
attrset (MARKED_COLOR); attrset (
} boldflag == MARK_NORMAL ? NORMAL_COLOR :
if (boldflag != MARK_NORMAL boldflag == MARK_SELECTED ? MARKED_COLOR :
&& from < view->search_start + view->found_len - 1 boldflag == MARK_CHANGED ? VIEW_UNDERLINED_COLOR :
&& bytes != view->bytes_per_line - 1) { /* boldflag == MARK_CURSOR */
view_gotoyx (view, top + row, left + col); view->hexview_in_text ? VIEW_UNDERLINED_COLOR :
view_add_character (view, ' '); MARKED_SELECTED_COLOR);
}
c = convert_to_display_c (c);
if (!is_printable (c))
c = '.';
/* Print corresponding character on the text side */ /* Print corresponding character on the text side */
view_gotoyx (view, top + row, left + text_start + bytes); view_gotoyx (view, top + row, left + text_start + bytes);
c = convert_to_display_c (c);
if (!is_printable (c))
c = '.';
switch (boldflag) {
case MARK_NORMAL:
break;
case MARK_SELECTED:
attrset (MARKED_COLOR);
break;
case MARK_CURSOR:
if (view->hexview_in_text) {
/* Our side is active */
view->cursor_col = text_start + bytes;
view->cursor_row = row;
attrset (VIEW_UNDERLINED_COLOR);
} else {
/* Other side is active */
attrset (MARKED_SELECTED_COLOR);
}
break;
case MARK_CHANGED:
attrset (VIEW_UNDERLINED_COLOR);
break;
}
view_add_character (view, c); view_add_character (view, c);
if (boldflag != MARK_NORMAL) { /* Save the cursor position for view_place_cursor() */
boldflag = MARK_NORMAL; if (from == view->hex_cursor && view->hexview_in_text) {
view->cursor_row = row;
view->cursor_col = text_start + bytes;
}
}
}
/* Be polite to the other functions */
attrset (NORMAL_COLOR); attrset (NORMAL_COLOR);
}
}
}
view_place_cursor (view); view_place_cursor (view);
view->dpy_complete = (get_byte (view, from) == -1); view->dpy_complete = (get_byte (view, from) == -1);
} }