Implement drawing text line by line

This commit is contained in:
2025-05-09 22:58:24 +05:00
parent 611c742435
commit 42299aedf5
2 changed files with 146 additions and 15 deletions

View File

@@ -16,16 +16,19 @@ use32
dd 0, 0 ; I_Param, I_Path dd 0, 0 ; I_Param, I_Path
WIN_BORDER_WIDTH = 5 WIN_BORDER_WIDTH = 5
FONT_WIDTH = 8
FONT_HEIGHT = 16 FONT_HEIGHT = 16
SCROLL_WIDTH = 16 SCROLL_WIDTH = 16
SCROLL_TOP = 34 SCROLL_TOP = 34
ED_SEND_MAX_LEN = 256
CONN_WIN_STACK_SIZE = 1024 CONN_WIN_STACK_SIZE = 1024
BTN_CLOSE = 1 BTN_CLOSE = 1
BTN_SETUP = 2 BTN_SETUP = 2
BTN_CONN = 3 BTN_CONN = 3
BTN_SEND = 4 BTN_CLEAR = 4
BTN_SEND = 5
BTN_OK = 2 BTN_OK = 2
BTN_CANCEL = 3 BTN_CANCEL = 3
@@ -53,6 +56,8 @@ start:
mcall SF_SET_EVENTS_MASK, EVM_MOUSE + EVM_MOUSE_FILTER + EVM_REDRAW + EVM_BUTTON + EVM_KEY mcall SF_SET_EVENTS_MASK, EVM_MOUSE + EVM_MOUSE_FILTER + EVM_REDRAW + EVM_BUTTON + EVM_KEY
mov byte [ed_send_val], 0
stdcall text_view_init, text_view
call .draw_window call .draw_window
.loop: .loop:
@@ -85,6 +90,16 @@ start:
jne @f jne @f
call show_conn_window call show_conn_window
jmp .loop jmp .loop
@@:
cmp ah, BTN_CLEAR
jne @f
; TODO
jmp .loop
@@:
cmp ah, BTN_SEND
jne @f
call send_text
jmp .loop
@@: @@:
cmp ah, BTN_CLOSE cmp ah, BTN_CLOSE
jne .loop jne .loop
@@ -116,7 +131,7 @@ start:
mcall SF_DEFINE_BUTTON, <1, 24>, <5, 24>, BTN_SETUP, [sc.work_light] mcall SF_DEFINE_BUTTON, <1, 24>, <5, 24>, BTN_SETUP, [sc.work_light]
mcall SF_DEFINE_BUTTON, <30, 24>, <5, 24>, BTN_CONN, [sc.work_light] mcall SF_DEFINE_BUTTON, <30, 24>, <5, 24>, BTN_CONN, [sc.work_light]
mcall SF_DEFINE_BUTTON, <59, 24>, <5, 24>, 123, [sc.work_light] mcall SF_DEFINE_BUTTON, <59, 24>, <5, 24>, BTN_CLEAR, [sc.work_light]
mcall SF_PUT_IMAGE_EXT, icons, <20, 20>, <3, 7>, 1, icons.palette, 0 mcall SF_PUT_IMAGE_EXT, icons, <20, 20>, <3, 7>, 1, icons.palette, 0
@@ -129,6 +144,11 @@ start:
mcall SF_PUT_IMAGE_EXT, , <20, 20>, <32, 7>, 1, icons.palette, 0 mcall SF_PUT_IMAGE_EXT, , <20, 20>, <32, 7>, 1, icons.palette, 0
; text view ; text view
mov eax, [sc.work_light]
mov [text_view.bg_color], eax
mov eax, [sc.work_text]
mov [text_view.fg_color], eax
xor eax, eax xor eax, eax
mov [text_view.left], eax mov [text_view.left], eax
add eax, SCROLL_TOP add eax, SCROLL_TOP
@@ -316,7 +336,7 @@ show_conn_window:
mcall SF_DEFINE_BUTTON, <CONN_WIN_WIDTH - 137, 60>, \ mcall SF_DEFINE_BUTTON, <CONN_WIN_WIDTH - 137, 60>, \
<CONN_WIN_HEIGHT - 55, 24>, BTN_OK, \ <CONN_WIN_HEIGHT - 55, 24>, BTN_OK, \
[sc.work_button] [sc.work_button]
mcall SF_DEFINE_BUTTON, <CONN_WIN_WIDTH - 71, 60>, \ mcall SF_DEFINE_BUTTON, <CONN_WIN_WIDTH - 71, 60>, \
<CONN_WIN_HEIGHT - 55, 24>, BTN_CANCEL, \ <CONN_WIN_HEIGHT - 55, 24>, BTN_CANCEL, \
[sc.work_button] [sc.work_button]
@@ -332,6 +352,16 @@ show_conn_window:
mcall SF_REDRAW, SSF_END_DRAW mcall SF_REDRAW, SSF_END_DRAW
ret ret
send_text:
stdcall text_view_append_line, text_view, ed_send_val
xor eax, eax
push eax
invoke edit_box_set_text, ed_send, esp
pop eax
stdcall text_view_draw, text_view
invoke edit_box_draw, ed_send
ret
align 16 align 16
@IMPORT: @IMPORT:
@@ -341,6 +371,7 @@ import box_lib,\
edit_box_draw, 'edit_box_draw',\ edit_box_draw, 'edit_box_draw',\
edit_box_key, 'edit_box_key',\ edit_box_key, 'edit_box_key',\
edit_box_mouse, 'edit_box_mouse',\ edit_box_mouse, 'edit_box_mouse',\
edit_box_set_text, 'edit_box_set_text',\
scrollbar_draw, 'scrollbar_v_draw',\ scrollbar_draw, 'scrollbar_v_draw',\
scrollbar_mouse, 'scrollbar_v_mouse' scrollbar_mouse, 'scrollbar_v_mouse'
@@ -354,7 +385,7 @@ ed_baud edit_box 80, CONN_WIN_WIDTH - 80 - 11, 42, 0xffffff, 0x6f9480, \
conn_win_edits_end: conn_win_edits_end:
main_win_edits_start: main_win_edits_start:
ed_send edit_box 0, 0, 0, 0xffffff, 0x6f9480, \ ed_send edit_box 0, 0, 0, 0xffffff, 0x6f9480, \
0, 0, 0x10000000, 10, ed_send_val, mouse_dd, \ 0, 0, 0x10000000, ED_SEND_MAX_LEN - 1, ed_send_val, mouse_dd, \
ed_focus, 0, 0 ed_focus, 0, 0
main_win_edits_end: main_win_edits_end:
@@ -372,7 +403,6 @@ cancel_label db 'Cancel', 0
send_label db 'Send', 0 send_label db 'Send', 0
status_msg db ' ', 0 status_msg db ' ', 0
port_num dd 0 port_num dd 0
ed_send_val db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
port_conf: port_conf:
dd port_conf_end - port_conf dd port_conf_end - port_conf
dd 9600 dd 9600
@@ -405,6 +435,7 @@ icons:
i_end: i_end:
ed_port_val rb 7 ed_port_val rb 7
ed_baud_val rb 7 ed_baud_val rb 7
ed_send_val rb ED_SEND_MAX_LEN
mouse_dd dd ? mouse_dd dd ?
conn_win_pid dd ? conn_win_pid dd ?
sc system_colors sc system_colors

View File

@@ -1,4 +1,5 @@
BUF_SIZE = 4096 BUF_SIZE = 4096
MARGIN = 1
struct TEXT_VIEW struct TEXT_VIEW
left dd ? left dd ?
@@ -11,22 +12,40 @@ struct TEXT_VIEW
buf_data dd ? buf_data dd ?
buf_size dd ? buf_size dd ?
data_size dd ?
total_lines dd ? total_lines dd ?
curr_line dd ? curr_line dd ?
max_length dd ? max_length dd ?
rows dd ?
cols dd ?
ends ends
proc text_view_init stdcall uses ebx, text_view:dword proc text_view_init stdcall, text_view:dword
mov eax, [text_view] mov ecx, [text_view]
xor eax, eax
mov [ecx + TEXT_VIEW.buf_data], eax
mov [ecx + TEXT_VIEW.buf_size], eax
mov [ecx + TEXT_VIEW.data_size], eax
mov [ecx + TEXT_VIEW.total_lines], eax
mov [ecx + TEXT_VIEW.curr_line], eax
mov [ecx + TEXT_VIEW.max_length], eax
ret ret
endp endp
proc text_view_deinit stdcall, text_view:dword proc text_view_deinit stdcall uses ebx, text_view:dword
mov ebx, [text_view]
mov ecx, [ebx + TEXT_VIEW.buf_data]
test ecx, ecx
jz .exit
mcall SF_SYS_MISC, SSF_MEM_FREE
.exit
ret
endp endp
proc text_view_draw stdcall uses ebx esi, text_view:dword proc text_view_draw stdcall uses ebx esi edi, text_view:dword
; draw frame and background
mov esi, [text_view] mov esi, [text_view]
mov ebx, [esi + TEXT_VIEW.left] mov ebx, [esi + TEXT_VIEW.left]
shl ebx, 16 shl ebx, 16
@@ -34,13 +53,94 @@ proc text_view_draw stdcall uses ebx esi, text_view:dword
mov ecx, [esi + TEXT_VIEW.top] mov ecx, [esi + TEXT_VIEW.top]
shl ecx, 16 shl ecx, 16
add ecx, [esi + TEXT_VIEW.height] add ecx, [esi + TEXT_VIEW.height]
xor edx, edx mov edx, [esi + TEXT_VIEW.fg_color]
call draw_frame call draw_frame
add ebx, 1 shl 16
sub ebx, 2
add ecx, 1 shl 16
sub ecx, 2
mov edx, [esi + TEXT_VIEW.bg_color]
mcall SF_DRAW_RECT
; calc rows and cols
mov eax, [esi + TEXT_VIEW.width]
sub eax, MARGIN * 2
shr eax, 3 ; assume that font width is 8 px
mov [esi + TEXT_VIEW.cols], eax
xor edx, edx
mov eax, [esi + TEXT_VIEW.height]
sub eax, MARGIN * 2
mov ebx, FONT_HEIGHT + MARGIN * 2
div ebx
mov [esi + TEXT_VIEW.rows], eax
; draw lines
xor eax, eax ; rows
mov ebx, [esi + TEXT_VIEW.left]
add ebx, MARGIN + 1 ; frame width
shl ebx, 16
add ebx, [esi + TEXT_VIEW.top]
add ebx, MARGIN + 1 ; frame width
mov edi, [esi + TEXT_VIEW.buf_data]
.draw_lines:
cmp eax, [esi + TEXT_VIEW.total_lines]
jae .draw_lines_end
mov ecx, 0x90000000
or ecx, [esi + TEXT_VIEW.fg_color]
mov edx, edi
push eax
mcall SF_DRAW_TEXT
; move edi pointer to the next string
push esi
mov esi, edi
call strlen
add edi, eax
inc edi
pop esi
; next line num
pop eax
inc eax
add ebx, FONT_HEIGHT + MARGIN
jmp .draw_lines
.draw_lines_end:
ret ret
endp endp
proc text_view_append_line stdcall, text_view:dword, strp:dword proc text_view_append_line stdcall uses ebx esi edi, text_view:dword, string:dword
mov esi, [string]
call strlen
inc eax ; include \0
mov esi, [text_view]
mov edi, eax
add eax, [esi + TEXT_VIEW.data_size]
cmp eax, [esi + TEXT_VIEW.buf_size]
jbe .noalloc
and eax, not (BUF_SIZE - 1)
add eax, BUF_SIZE
mov ecx, eax
mov edx, [esi + TEXT_VIEW.buf_data]
mcall SF_SYS_MISC, SSF_MEM_REALLOC
test eax, eax
jz .exit
mov [esi + TEXT_VIEW.buf_data], eax
mov [esi + TEXT_VIEW.buf_size], ecx
.noalloc:
inc [esi + TEXT_VIEW.total_lines]
mov eax, edi
cmp eax, [esi + TEXT_VIEW.max_length]
jbe .short
mov [esi + TEXT_VIEW.max_length], eax
.short:
mov ecx, edi
mov edi, [esi + TEXT_VIEW.buf_data]
add edi, [esi + TEXT_VIEW.data_size]
add [esi + TEXT_VIEW.data_size], ecx
mov esi, [string]
cld
rep movsb
.exit:
ret
endp endp
draw_frame: draw_frame:
@@ -60,12 +160,12 @@ draw_frame:
push ecx push ecx
mov eax, ecx mov eax, ecx
shr eax, 16 shr eax, 16
mov cx, ax ; ecx = top << 16 + top mov cx, ax
mcall SF_DRAW_LINE mcall SF_DRAW_LINE
pop ecx pop ecx
mov eax, ecx mov eax, ecx
shl ecx, 16 shl ecx, 16
mov cx, ax ; ecx = height << 16 + height mov cx, ax
mcall SF_DRAW_LINE mcall SF_DRAW_LINE
pop ecx pop ecx
push ebx push ebx