Subversion Repositories Kolibri OS

Rev

Rev 6026 | Rev 9092 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2016. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;   Written by hidnplayr@kolibrios.org                            ;;
  7. ;;                                                                 ;;
  8. ;;         GNU GENERAL PUBLIC LICENSE                              ;;
  9. ;;          Version 2, June 1991                                   ;;
  10. ;;                                                                 ;;
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12.  
  13.  
  14. text_insert_newlines:                   ; esi = ASCIIZ string
  15.  
  16.         xor     edx, edx                ; number of lines of text
  17.         cmp     byte[esi], 0
  18.         je      .done
  19.   .next_line:
  20.         xor     ebx, ebx
  21.         mov     ecx, [textbox_width]
  22.         inc     ecx
  23.   .more:
  24.         dec     ecx
  25.         jz      .end_of_line
  26.         lodsb                           ; get one character of the string
  27.         test    al, 0x80
  28.         jnz     .multiball!
  29.         test    al, al                  ; end of string?
  30.         jz      .almost_done
  31.         cmp     al, ' '                 ; it's a space! remember its position
  32.         je      .space
  33.         cmp     al, 13                  ; we already inserted a newline once, make it a space again
  34.         je      .soft_nl
  35.         cmp     al, 10                  ; it's a newline, continue onto the next line
  36.         jne     .more
  37.         inc     edx
  38.         jmp     .next_line
  39.   .soft_nl:
  40.         mov     byte[esi-1], ' '
  41.   .space:
  42.         mov     ebx, esi                ; last detected space
  43.         jmp     .more
  44.   .end_of_line:
  45.         inc     edx
  46.         test    ebx, ebx                ; did we detect any spaces on this line?
  47.         jz      .next_line              ; no:   just continue onto the next line
  48.         mov     byte[ebx-1], 13         ; yes:  replace last space on line with a soft newline
  49.         mov     esi, ebx                ;       and continue parsing just after last space
  50.         jmp     .next_line              ;
  51.   .almost_done:
  52.         dec     esi
  53.   .done:
  54.  
  55.         ret
  56.  
  57.   .multiball!:
  58.         add     esi, 4
  59.         and     al, 11111000b
  60.         cmp     al, 11110000b
  61.         je      .more
  62.         dec     esi
  63.         and     al, 11110000b
  64.         cmp     al, 11100000b
  65.         je      .more
  66.         dec     esi
  67.         jmp     .more
  68.  
  69.  
  70.  
  71. ;----------------------------------
  72. ; scan untill next line is reached
  73. ;
  74. ; When you set the direction flag before calling, you can also scan for previous line!
  75. ; IN:   esi
  76. ; OUT:  esi
  77. ;----------------------------------
  78. text_nextline:
  79.  
  80.         mov     ecx, [textbox_width]
  81.   .loop:
  82.         lodsb
  83.         test    al, al
  84.         jz      .done
  85.         cmp     al, 10
  86.         je      .done
  87.         cmp     al, 13
  88.         je      .done
  89.         dec     ecx
  90.         jnz     .loop
  91.   .done:
  92.  
  93.         ret
  94.  
  95.  
  96. ;----------------------------------
  97. ; print string
  98. ;
  99. ; IN:   esi = ptr to string
  100. ;       bl = char which marks end of string
  101. ; OUT:  esi = ptr to end of str
  102. ;----------------------------------
  103. print_string:
  104.  
  105.         push    eax
  106.   .loop:
  107.         lodsb
  108.         cmp     al, bl
  109.         je      .done
  110.         cmp     al, 13
  111.         je      .loop
  112.         test    al, al
  113.         jz      .done
  114.         call    print_char
  115.         jmp     .loop
  116.   .done:
  117.         pop     eax
  118.  
  119.         ret
  120.  
  121.  
  122. ;----------------------------------
  123. ; print ASCIIZ string
  124. ;
  125. ; IN:   esi = ptr to ASCIIZ string
  126. ; OUT:  esi = ptr to end of str
  127. ;----------------------------------
  128. print_asciiz:
  129.  
  130.         push    eax
  131.   .loop:
  132.         lodsb
  133.         test    al, al
  134.         jz      .done
  135.         call    print_char
  136.         jmp     .loop
  137.   .done:
  138.         pop     eax
  139.  
  140.         ret
  141.  
  142.  
  143. ;----------------------------------
  144. ; print character
  145. ;
  146. ; IN:   al = char to print
  147. ; OUT:  /
  148. ;----------------------------------
  149. print_char:
  150.  
  151.         push    esi edi
  152.         mov     esi, [window_print]
  153.         mov     edi, [esi + window.text_write]
  154.         stosb
  155.         cmp     edi, [esi + window.text_end]
  156.         jae     .uh_ow
  157.         mov     [esi + window.text_write], edi
  158.   .continue:
  159.         or      [esi + window.flags], FLAG_UPDATED
  160.         pop     edi esi
  161.  
  162.         ret
  163.  
  164.   .uh_ow:
  165.         pusha
  166.         mov     edi, [esi + window.text_start]
  167.         mov     [esi + window.text_print], edi
  168.         lea     esi, [edi + TEXT_BUFFERSIZE/2]
  169.         call    text_nextline
  170.         mov     ecx, TEXT_BUFFERSIZE/8
  171.         rep     movsd
  172.         mov     esi, edi
  173.         call    text_insert_newlines
  174.  
  175.         mov     ebx, [window_print]
  176.         mov     [ebx + window.text_lines], edx
  177.         mov     [ebx + window.text_scanned], esi
  178.         mov     [ebx + window.text_write], esi
  179.         mov     [ebx + window.text_line_print], 0
  180.         popa
  181.  
  182.         jmp     .continue
  183.  
  184.  
  185.  
  186. draw_channel_text:
  187.  
  188.         mov     edi, [window_active]
  189.         and     [edi + window.flags], not FLAG_UPDATED  ; clear the 'window is updated' flag
  190.  
  191. ; Scan new text for newlines
  192.         mov     esi, [edi + window.text_scanned]
  193.         call    text_insert_newlines
  194.         add     [edi + window.text_lines], edx
  195.         mov     [edi + window.text_scanned], esi
  196.  
  197. ; Is scrollbar at lowest position?
  198.         test    [edi + window.flags], FLAG_SCROLL_LOW
  199.         jnz     .yesscroll                              ; Yes
  200.         cmp     [scroll2.all_redraw], 1                 ; No
  201.         jnz      .noscroll
  202.         mov     edx, [textbox_height]
  203.         add     edx, [edi + window.text_line_print]
  204.     cmp edx, [edi + window.text_lines]
  205.         jl      .noscroll
  206.   .yesscroll:
  207. ; Scrollbar was at lowest position, scroll down automatically when new text arrived.
  208.         mov     edx, [edi + window.text_lines]
  209.         sub     edx, [textbox_height]
  210.         jg      @f
  211.         mov     [edi + window.text_line_print], 0
  212.         jmp     .noscroll                               ; There are less lines of text than fit into the window, dont scroll..
  213.   @@:
  214.         sub     edx, [edi + window.text_line_print]
  215.         je      .noscroll                               ; We are already at the bottom pos, dont scroll..
  216.   .scroll_to_pos:               ; edx = number of lines to go up/down (flags must indicate direction)
  217.         pushf
  218.         add     [edi + window.text_line_print], edx
  219.         mov     esi, [edi + window.text_print]
  220.         popf
  221.         jg      .loop_forward
  222.         std                     ; set direction flag so we can scan backwards
  223.         dec     esi
  224.         dec     esi             ; move our cursor just in front of newline, for scanning backwards
  225.   .loop_backward:
  226.         call    text_nextline
  227.         inc     edx
  228.         jnz     .loop_backward
  229.         inc     esi
  230.         inc     esi             ; move the cursor just after last newline
  231.         cld
  232.         jmp     .ok
  233.  
  234.   .loop_forward:
  235.         call    text_nextline
  236.         dec     edx
  237.         jnz     .loop_forward
  238.   .ok:
  239.         mov     [edi + window.text_print], esi
  240.   .noscroll:
  241.  
  242. ; Update and draw scrollbar when nescessary
  243.         mov     edx, [edi + window.text_lines]
  244.         cmp     edx, [textbox_height]
  245.         jbe     .scroll_done
  246.  
  247.         mov     [scroll2.max_area], edx
  248.         mov     eax, [edi + window.text_line_print]
  249.         mov     [scroll2.position], eax
  250.  
  251.         push    dword scroll2                   ; redraw scrollbar
  252.         call    [scrollbar_draw]
  253.         mov     [scroll2.all_redraw], 0         ; next time, dont redraw it completely
  254.   .scroll_done:
  255.  
  256. ; Calculate start offset coordinates (align text to bottom)
  257.         mov     ebx, [textbox_height]
  258.         sub     ebx, [edi + window.text_lines]
  259.         jb      .no_offset
  260.         imul    ebx, FONT_HEIGHT
  261.         push    [edi + window.text_start]
  262.         pop     [edi + window.text_print]
  263.         jmp     .draw_text
  264.   .no_offset:
  265.         xor     ebx, ebx
  266.   .draw_text:
  267.  
  268. ; Prepare to actually draw some text
  269.         add     ebx, TEXT_X shl 16 + TEXT_Y     ; text coordinates
  270.         mov     ecx, [colors.work_text]         ; default text color
  271.         or      ecx, 0x30000000
  272.         mov     edx, [edi + window.text_print]  ; start of text to print
  273.  
  274. ; Scan backwards on line for color escape codes
  275.         mov     esi, edx
  276.         push    edx
  277.         std
  278.   @@:
  279.         lodsb
  280.         cmp     al, 0           ; end of text
  281.         je      @f
  282.         cmp     al, 10          ; hard newline
  283.         je      @f
  284.         cmp     al, 3           ; mIRC escape code
  285.         jne     @b
  286.  
  287.         cld
  288.         lea     edx, [esi+2]
  289.         call    dec_to_esi
  290.         jz      @f
  291.         mov     ecx, [irc_colors + 4*esi]
  292.         or      ecx, 0x30000000                 ; UTF-8 text
  293.  
  294.         cmp     byte[edx], ','                  ; background color?
  295.         jne     @f
  296.         inc     edx
  297.         call    dec_to_esi
  298.         jz      @f
  299.         mov     edi, [irc_colors + 4*esi]
  300.         or      ecx, 0x40000000                 ; enable background color
  301.   @@:
  302.         cld
  303.  
  304.         pop     edx
  305.         mov     eax, [textbox_height]           ; max number of lines to draw
  306.   .drawloop:
  307.         cmp     byte[edx], 0
  308.         je      .end_of_text
  309.  
  310. ; Clear one row of characters
  311.         pusha
  312.         mov     cx, bx
  313.         shl     ecx, 16
  314.         mov     cx, FONT_HEIGHT
  315.         mov     ebx, TEXT_X shl 16
  316.         mov     bx, word[textbox_width]
  317.         imul    bx, FONT_WIDTH
  318.         mov     edx, [colors.work]
  319.         mcall   13                              ; draw rectangle
  320.         popa
  321.  
  322.         push    eax
  323.         mov     esi, [textbox_width]
  324.   .line:
  325.         cmp     byte[edx], 0
  326.         je      .end_of_text
  327.         cmp     byte[edx], 13
  328.         je      .newline_soft
  329.         cmp     byte[edx], 10
  330.         je      .newline_hard
  331.  
  332.         push    esi
  333.         cmp     byte[edx], 3                    ; escape code for mIRC colors
  334.         jne     .no_colors
  335.         inc     edx
  336.         call    dec_to_esi
  337.         jz      .no_colors
  338.         mov     ecx, [irc_colors + 4*esi]
  339.         or      ecx, 0x30000000
  340.  
  341.         cmp     byte[edx], ','                  ; background color?
  342.         jne     .no_colors
  343.         inc     edx
  344.         call    dec_to_esi
  345.         jz      .no_colors
  346.         mov     edi, [irc_colors + 4*esi]
  347.         or      ecx, 0x40000000
  348.   .no_colors:
  349.  
  350.         mov     esi, 1
  351.         mcall   4                               ; draw text
  352.  
  353.         mov     esi, 1
  354.         mov     al, byte[edx]
  355.         test    al, 10000000b
  356.         jz      @f
  357.         mov     esi, 4
  358.         and     al, 11111000b
  359.         cmp     al, 11110000b
  360.         je      @f
  361.         dec     esi
  362.         and     al, 11110000b
  363.         cmp     al, 11100000b
  364.         je      @f
  365.         dec     esi
  366.   @@:
  367.  
  368.         add     ebx, FONT_WIDTH shl 16
  369.         add     edx, esi
  370.         pop     esi
  371.         dec     esi
  372.         jnz     .line
  373.         jmp     .line_full
  374.  
  375.   .newline_hard:
  376.         mov     ecx, [colors.work_text]
  377.         or      ecx, 0x30000000
  378.   .newline_soft:
  379.         inc     edx
  380.   .line_full:
  381.         and     ebx, 0x0000ffff
  382.         add     ebx, TEXT_X shl 16 + FONT_HEIGHT
  383.         pop     eax
  384.         dec     eax
  385.         jnz     .drawloop
  386.   .end_of_text:
  387.  
  388.         ret
  389.  
  390.  
  391.  
  392.  
  393. dec_to_esi:
  394.  
  395.         xor     esi, esi
  396.   .loop:
  397.         movzx   eax, byte[edx]
  398.         sub     al, '0'
  399.         jb      .done
  400.         cmp     al, 9
  401.         ja      .done
  402.         inc     edx
  403.         lea     esi, [esi*4 + esi]      ; esi * 5
  404.         lea     esi, [esi*2 + eax]      ; esi * 2 + eax
  405.         jmp     .loop
  406.   .done:
  407.         cmp     esi, 16
  408.         jae     .fail
  409.         ret
  410.  
  411.   .fail:
  412.         xor     esi, esi
  413.         ret
  414.  
  415.  
  416.  
  417. if TIMESTAMP
  418. print_timestamp:
  419.  
  420.         pusha
  421.         mcall   3                       ; get system time
  422.         mov     ebx, eax
  423.  
  424.         mov     al, '['
  425.         call    print_char
  426.         mov     ecx, TIMESTAMP
  427.   .loop:
  428.         mov     al, bl
  429.         shr     al, 4
  430.         add     al, '0'
  431.         call    print_char
  432.  
  433.         mov     al, bl
  434.         and     al, 0x0f
  435.         add     al, '0'
  436.         call    print_char
  437.  
  438.         dec     ecx
  439.         jz      .done
  440.  
  441.         mov     al, ':'
  442.         call    print_char
  443.         shr     ebx, 8
  444.         jmp     .loop
  445.   .done:
  446.         mov     al, ']'
  447.         call    print_char
  448.         mov     al, ' '
  449.         call    print_char
  450.  
  451.         popa
  452.         ret
  453. end if