home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / fed0217s.zip / source / bdraw.cpp < prev    next >
C/C++ Source or Header  |  2001-02-21  |  11KB  |  307 lines

  1. /*
  2. ** Module   :BDRAW.CPP
  3. ** Abstract :Buffer::draw implementation
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. **
  7. ** Log: Mon  15/03/1998     Created
  8. */
  9.  
  10. #ifdef __FED_DEBUG__
  11. #define INCL_DOS
  12. #define INCL_VIO
  13. #include <os2.h>
  14. #endif
  15.  
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. #include <buffer.h>
  20. #include <version.h>
  21.  
  22. #define UNDO        1
  23. #define USE_SCROLL     1
  24.  
  25. #ifndef max
  26. #define max(a,b) (((a) > (b)) ? (a) : (b))
  27. #define min(a,b) (((a) < (b)) ? (a) : (b))
  28. #endif
  29.  
  30. void Buffer::draw(Rect& rect)
  31. {
  32.     int i,j,x;
  33.     Parser * parser = gen_parser();
  34.  
  35.     char *save_buff;
  36.  
  37.     int mark_col_start = min(old_abs_col, abs_col());
  38.     int mark_col_end   = max(old_abs_col, abs_col());
  39.     int mark_beg_row   = min(old_abs_row, abs_row());
  40.     int mark_end_row   = max(old_abs_row, abs_row());
  41.  
  42.     mark_col_start = max(mark_col_start, start_col);
  43.     mark_col_end   = min(mark_col_end  , start_col + rect.cols);
  44.  
  45.     char *print_buffer2 = new char[(rect.cols+start_col + 5)];
  46.     char *print_buffer  = &print_buffer2[start_col];
  47.  
  48.     memset(AlignedBuffer, 0, BufLen);
  49.     save_buff = vio_set_work_buff(AlignedBuffer);
  50.  
  51.     for(j = start_row,i = 0; i < rect.rows; i++,j++)
  52.     {
  53.         if(j == Count())
  54.             vio_print(rect.row + i,
  55.                       rect.col,
  56.                       "<EOF>",
  57.                       rect.cols,
  58.                       app_pal[pal_start + CL_EOF]
  59.                       );
  60.         else
  61.         {
  62.             print_buffer[0] = '\x0';
  63.             if(hiliting && j < Count() && start_col < line(j)->len())
  64.             {
  65. ////////////////////////////////////////////////////////////////////////
  66. // This dirty hack triples drawing performance when hiliting is ON
  67. ////////////////////////////////////////////////////////////////////////
  68.  
  69. #define ScreenOffset(Row,Col) (((Row) * Cols + (Col)) << 1)
  70.  
  71.                 char *scr_start = &Screen[ScreenOffset(rect.row + i, rect.col)];
  72.  
  73. ////////////////////////////////////////////////////////////////////////
  74.                 PLine ln = line(j);
  75.  
  76.                 ln->get_print(0, print_buffer2, rect.cols+start_col);
  77.  
  78.                 print_buffer2[rect.cols+start_col] = 0;
  79.  
  80.                 for(x = 0; x < rect.cols+start_col; x++)
  81.                         print_buffer2[x] = chr_out(print_buffer2[x]);
  82.  
  83.                 parser->reset(print_buffer2, line(j)->state());
  84.  
  85.                 int tok_pos = 0;
  86.                 int delta = 0;
  87.  
  88.                 while(*parser->tok)
  89.                 {
  90.                     delta = parser->next_token();
  91.                     tok_pos = (parser->old_tok - print_buffer2);
  92.                     if((tok_pos + parser->tok_len) < start_col)
  93.                     {
  94.                         parser->tok += delta;
  95.                         continue;
  96.                     }
  97.                     else
  98.                         break;
  99.                 }
  100.                 if(tok_pos < start_col)
  101.                 {
  102.                     parser->tok_len -= (start_col - tok_pos);
  103.                     parser->tok     += (start_col - tok_pos);
  104.                     delta   -= (start_col - tok_pos);
  105.                     tok_pos  = start_col;
  106.                 }
  107.                 while(*parser->tok)
  108.                 {
  109.                     char *scr = parser->tok;
  110.                     char clr  = app_pal[parser->color+pal_start];
  111.  
  112.                     for(int counter = 0; counter < parser->tok_len; counter++)
  113.                     {
  114.                         *scr_start++ = *scr++;
  115.                         *scr_start++ = clr;
  116.                     }
  117.                     parser->tok += delta;
  118.                     delta = parser->next_token();
  119.                     tok_pos = (parser->tok-print_buffer2);
  120.                 }
  121.             }
  122.             else
  123.             {
  124.                 if(j < Count() && start_col < line(j)->len())
  125.                 {
  126.                     line(j)->get_print(start_col, print_buffer, rect.cols);
  127.  
  128.                     for(x = 0; x < rect.cols; x++)
  129.                         print_buffer[x] = chr_out(print_buffer[x]);
  130.                 }
  131.                 else
  132.                     print_buffer[0] = '\x0';
  133.  
  134.                 vio_print(rect.row + i,
  135.                           rect.col,
  136.                           print_buffer,
  137.                           rect.cols,
  138. //                          app_pal[pal_start + CL_IDENT]);
  139.                           app_pal[pal_start + CL_DEFAULT]);
  140.             }
  141.             if(mark_state)
  142.             {
  143.                 if(!print_buffer[0] && j < Count() && start_col < line(j)->len())
  144.                 {
  145.                     line(j)->get_print(start_col, print_buffer, rect.cols);
  146.  
  147.                     for(x = 0; x < rect.cols; x++)
  148.                         print_buffer[x] = chr_out(print_buffer[x]);
  149.  
  150.                 }
  151.  
  152.                 if(!print_buffer[0])
  153.                     print_buffer[mark_col_start - start_col] = 0;
  154.  
  155.                 if(col_block)
  156.                 {
  157.                     if(j >= mark_beg_row && j <= mark_end_row)
  158.                         vio_print(rect.row + i,
  159.                                   rect.col + (mark_col_start - start_col),
  160.                                   &print_buffer[mark_col_start - start_col],
  161.                                   (mark_col_end - mark_col_start),
  162.                                    app_pal[pal_start + CL_SELECTION]);
  163.                 }
  164.                 else
  165.                 {
  166.                     if(j > mark_beg_row && j < mark_end_row) // full line
  167.                     {
  168.                         vio_print(rect.row + i,
  169.                                   rect.col,
  170.                                   print_buffer,
  171.                                   rect.cols,
  172.                                   app_pal[pal_start + CL_SELECTION]);
  173.                     }
  174.                     else
  175.                     {
  176.                         if(j == mark_beg_row || j == mark_end_row)
  177.                         {
  178.                             int hi_start = 0;
  179.                             int hi_end   = 0;
  180.  
  181.                             if(mark_beg_row != mark_end_row)
  182.                             {
  183.                                 if(j == mark_beg_row)
  184.                                 {
  185.                                     hi_start = (mark_beg_row == old_abs_row) ?
  186.                                                 old_abs_col : abs_col();
  187.                                     hi_start = max(hi_start, start_col);
  188.                                     hi_end   = start_col+rect.cols;
  189.                                 }
  190.                                 if(j == mark_end_row)
  191.                                 {
  192.                                     hi_start = start_col;
  193.                                     hi_end   = (mark_end_row == old_abs_row) ?
  194.                                                 old_abs_col : abs_col();
  195.                                     hi_end   = min(hi_end, start_col+rect.cols);
  196.                                 }
  197.                             }
  198.                             else
  199.                             {
  200.                                 hi_start = min(old_abs_col, abs_col());
  201.                                 hi_start = max(hi_start, start_col);
  202.                                 hi_end   = max(old_abs_col, abs_col());
  203.                                 hi_end   = min(hi_end, start_col+rect.cols);
  204.                             }
  205.  
  206.                             if(hi_end - hi_start)
  207.                             {
  208.                                 if(!print_buffer[0])
  209.                                     print_buffer[hi_start - start_col] = 0;
  210.  
  211.                                 vio_print(rect.row + i,
  212.                                           rect.col + (hi_start - start_col),
  213.                                           &print_buffer[hi_start - start_col],
  214.                                           (hi_end - hi_start),
  215.                                           app_pal[pal_start + CL_SELECTION]);
  216.                             }
  217.                         }
  218.                     }
  219.                 }
  220.             }
  221.  
  222.             if(found_show && j == found_row && found_len > 0)
  223.             {
  224.                 if(start_col <= found_col &&
  225.                    start_col + rect.cols >= found_col)
  226.                 {
  227.                     int draw_len = found_len;
  228.                     if(found_col + found_len > start_col + rect.cols)
  229.                         draw_len = (start_col + rect.cols) - found_col;
  230.  
  231.                     vio_draw_attr(rect.row + i,
  232.                                   rect.col + (found_col - start_col),
  233.                                   draw_len,
  234.                                   app_pal[pal_start + CL_SELECTION]);
  235.                 }
  236.                 found_show = 0;
  237.             }
  238.         }
  239.     }
  240.     {
  241.         vio_set_work_buff(save_buff);
  242.  
  243.         //try to guess which part of screen can be reused
  244.         //if old start_col and start_row differs from current
  245.         //try to shift screen buffer to proper destination
  246.  
  247.         if(start_row > draw_save.start_row &&
  248.            (start_row - draw_save.start_row) < rect.rows)
  249.         {
  250.             vio_scroll(SCROLL_UP,
  251.                        rect,
  252.                        start_row - draw_save.start_row,
  253. //                       app_pal[pal_start + CL_IDENT]);
  254.                        app_pal[pal_start + CL_DEFAULT]);
  255.         }
  256.  
  257.         if(start_row < draw_save.start_row &&
  258.            (draw_save.start_row - start_row) < rect.rows)
  259.         {
  260.             vio_scroll(SCROLL_DN,
  261.                        rect,
  262.                        draw_save.start_row - start_row,
  263. //                       app_pal[pal_start + CL_IDENT]);
  264.                        app_pal[pal_start + CL_DEFAULT]);
  265.         }
  266.  
  267.         if(start_col > draw_save.start_col &&
  268.            (start_col - draw_save.start_col) < rect.cols)
  269.         {
  270.             vio_scroll(SCROLL_LT,
  271.                        rect,
  272.                        start_col - draw_save.start_col,
  273. //                       app_pal[pal_start + CL_IDENT]);
  274.                        app_pal[pal_start + CL_DEFAULT]);
  275.         }
  276.  
  277.         if(start_col < draw_save.start_col &&
  278.            (draw_save.start_col - start_col) < rect.cols)
  279.         {
  280.             vio_scroll(SCROLL_RT,
  281.                        rect,
  282.                        draw_save.start_col - start_col,
  283. //                       app_pal[pal_start + CL_IDENT]);
  284.                        app_pal[pal_start + CL_DEFAULT]);
  285.         }
  286.         for(i = 0; i < rect.rows; i++)
  287.         {
  288.             int rowstart = ScreenOffset(rect.row + i, rect.col);
  289.  
  290.             if(memcmp(&Screen[rowstart], &AlignedBuffer[rowstart],
  291.                       rect.cols * 2))
  292.             {
  293.                 memcpy(&Screen[rowstart], &AlignedBuffer[rowstart],
  294.                         rect.cols * 2);
  295.                 vio_show_buf(rowstart, rect.cols * 2);
  296.             }
  297.         }
  298.  
  299.         draw_save.start_row = start_row;
  300.         draw_save.start_col = start_col;
  301.  
  302.     }
  303.     delete print_buffer2;
  304.     delete parser;
  305. }
  306.  
  307.