home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / fed0217s.zip / source / clipbrd.cpp < prev    next >
C/C++ Source or Header  |  2000-12-13  |  7KB  |  258 lines

  1. /*
  2. ** Module   :CLIPBRD.CPP
  3. ** Abstract :Clipboard-related methods of class Buffer
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. **
  7. ** Log: Wed  05/03/1997     Updated to V0.5
  8. **      Sat  12/04/1997       Updated to V0.8
  9. **      Wed  12/11/1997     Updated to V0.9, mostly rewritten
  10. **      Tue  12/12/2000     Single-char ops replaced by group operations
  11. **                          to improve performance
  12. */
  13.  
  14. #include <stdlib.h>
  15. #include <string.h>
  16.  
  17. #include <buffer.h>
  18. #include <version.h>
  19.  
  20. #ifndef max
  21. #define max(a,b) (((a) > (b)) ? (a) : (b))
  22. #define min(a,b) (((a) < (b)) ? (a) : (b))
  23. #endif
  24.  
  25. Buffer* Buffer::copy()
  26. {
  27.     if(!mark_state)
  28.         return 0;
  29.  
  30.     int mark_beg_row = min(old_abs_row, abs_row());
  31.     int mark_end_row = max(old_abs_row, abs_row());
  32.  
  33.     Buffer* clip = new Buffer;
  34.     if(col_block || mark_beg_row == mark_end_row)
  35.     {
  36.         int mark_col_start = min(old_abs_col, abs_col());
  37.         int mark_col_end   = max(old_abs_col, abs_col());
  38.  
  39.         if(col_block)
  40.         {
  41.             clip->set_column_block(1);
  42.             clip->num_cols = mark_col_end - mark_col_start;
  43.         }
  44.  
  45.         for(int i = mark_beg_row; i <= mark_end_row; i++)
  46.         {
  47.             PLine ln = new Line(line(i), mark_col_start,
  48.                                 mark_col_end - mark_col_start);
  49.             ln->xlat(cp_out);
  50.             clip->add_line(ln);
  51.         }
  52.     }
  53.     else
  54.     {
  55.         for(int i = mark_beg_row; i <= mark_end_row; i++)
  56.         {
  57.             if(i == mark_beg_row)
  58.             {
  59.                 int col_start = (mark_beg_row == old_abs_row)
  60.                                 ? old_abs_col : abs_col();
  61.  
  62.                 PLine ln = new Line(line(i), col_start, line(i)->len() - col_start);
  63.  
  64.                 ln->xlat(cp_out);
  65.                 clip->add_line(ln);
  66.                 continue;
  67.             }
  68.             if(i == mark_end_row)
  69.             {
  70.                 int col_end = (mark_end_row == old_abs_row)
  71.                               ? old_abs_col : abs_col();
  72.  
  73.                 if(col_end > line(i)->len())
  74.                     col_end = line(i)->len();
  75.  
  76.                 PLine ln = new Line(line(i), 0, col_end);
  77.  
  78.                 ln->xlat(cp_out);
  79.                 clip->add_line(ln);
  80.                 continue;
  81.             }
  82.  
  83.             //Default action
  84.  
  85.             PLine ln = new Line(line(i));
  86.  
  87.             ln->xlat(cp_out);
  88.             clip->add_line(ln);
  89.         }
  90.     }
  91.     clip->to_pm();
  92.     return clip;
  93. }
  94.  
  95. Buffer* Buffer::cut(Rect& rect)
  96. {
  97.     if(!mark_state)
  98.         return 0;
  99.     Buffer* clip = copy();
  100.     clear(rect);
  101.     changed = 1;
  102.     return clip;
  103. }
  104.  
  105. void Buffer::clear(Rect& rect)
  106. {
  107.     if(!mark_state)
  108.         return;
  109.  
  110.     changed = 1;
  111.  
  112.     int mark_beg_row = min(old_abs_row, abs_row());
  113.     int mark_end_row = max(old_abs_row, abs_row());
  114.  
  115.     if(col_block || mark_beg_row == mark_end_row)
  116.     {
  117.         int mark_col_start = min(old_abs_col, abs_col());
  118.         int mark_col_end   = max(old_abs_col, abs_col());
  119.  
  120.         for(int i = mark_beg_row; i <= mark_end_row; i++)
  121.         {
  122.             track(opRestoreLine,(void *)line(i),(void *)i);
  123.             line(i)->del_char(mark_col_start, mark_col_end - mark_col_start);
  124.         }
  125.  
  126.         if(abs_col() != mark_col_start)
  127.             goto_col(rect, mark_col_start);
  128.     }
  129.     else
  130.     {
  131.         int col_start;
  132.         int col_end  ;
  133.  
  134.         /* mark_beg_row */
  135.  
  136.         col_start = (mark_beg_row == old_abs_row) ? old_abs_col : abs_col();
  137.         col_end   = line(mark_beg_row)->len();
  138.  
  139.         track(opRestoreLine,(void *)line(mark_beg_row),(void *)mark_beg_row);
  140.  
  141.         int res = line(mark_beg_row)->del_char(col_start, col_end - col_start);
  142.  
  143.         /* mark_end_row */
  144.  
  145.         col_start = 0;
  146.         col_end   = (mark_end_row == old_abs_row) ? old_abs_col : abs_col();
  147.  
  148.         track(opRestoreLine,(void *)line(mark_end_row),(void *)mark_end_row);
  149.  
  150.         line(mark_end_row)->del_char(col_start, col_end - col_start);
  151.  
  152.         /* other lines */
  153.  
  154.         int i = mark_end_row - (mark_beg_row + 1);
  155.  
  156.         if(i > 0)
  157.         {
  158.             Buffer *tmp_buf = new Buffer(i);
  159.             remove_items(tmp_buf,(mark_beg_row + 1), i);
  160.             track(opInsBlock,
  161.                   (void *) tmp_buf,
  162.                   (void *) (mark_beg_row + 1));
  163.         }
  164.  
  165.         goto_line(rect, mark_beg_row);
  166.         goto_col(rect, (mark_beg_row == old_abs_row) ? old_abs_col : abs_col());
  167.  
  168.         del_char(rect);
  169.     }
  170.     unmark();
  171.  
  172.     fill_hiliting(mark_beg_row, line(mark_beg_row)->state());
  173. }
  174.  
  175. void Buffer::paste(Rect& rect, Buffer* clip)
  176. {
  177.     if(!clip)
  178.         return;
  179.  
  180.     changed = 1;
  181.     int recalc_row = abs_row()-1;
  182.  
  183.     if(recalc_row < 0)
  184.         recalc_row = 0;
  185.  
  186.     int i,j;
  187.  
  188.     clip->from_pm();
  189.  
  190.     if(clip->col_block || clip->Count() == 1)
  191.     {
  192.         for(i = 0; i < clip->Count(); i++)
  193.         {
  194.             track(opRestoreLine, line(abs_row() + i), (void *)(abs_row() + i));
  195.  
  196.             PLine ln = new Line(clip->line(i));
  197.             ln->xlat(cp_in);
  198.  
  199.             line(abs_row() + i)->ins_char(abs_col(), ln);
  200.  
  201.             if((i < clip->Count() - 1) && (abs_row() + i) == (Count() - 1))
  202.             {
  203.                 track(opDelLine,(void *)(Count()));
  204.                 Add(new Line);
  205.             }
  206.  
  207.             delete ln;
  208.         }
  209.     }
  210.     else
  211.     {
  212.         PLine ln;
  213.  
  214.         for(i = 1; i < clip->Count() - 1; i++)
  215.         {
  216.             track(opDelLine,(void *)(abs_row() + i));
  217.             ln = new Line(clip->line(i));
  218.             ln->xlat(cp_in);
  219.  
  220.             At(ln, abs_row() + i);
  221.         }
  222.  
  223.         ln = line(abs_row());
  224.         track(opRestoreLine,(void *)ln,(void *)(abs_row()));
  225.  
  226.         PLine ln0 = new Line(ln, abs_col(), ln->len() - abs_col());
  227.  
  228.         ln->del_char(abs_col(), ln->len() - abs_col());
  229.  
  230.         ins_line(ln0, abs_row()+clip->Count()-1);
  231.         track(opDelLine,(void *)(abs_row()+clip->Count()-1));
  232.  
  233.         PLine ln1 = new Line(clip->line(0));
  234.         ln1->xlat(cp_in);
  235.         ln->ins_char(abs_col(), ln1);
  236.  
  237.         delete ln1;
  238.  
  239.         ln1 = new Line(clip->line(clip->Count() - 1));
  240.         ln1->xlat(cp_in);
  241.         ln0->ins_char(0, ln1);
  242.  
  243.         delete ln1;
  244.  
  245.         goto_line(rect, abs_row() + clip->Count()-1);
  246.         goto_col(rect, clip->line(clip->Count()-1)->len());
  247.     }
  248.  
  249.     fill_hiliting(recalc_row, line(recalc_row)->state());
  250. }
  251.  
  252. void Buffer::paste_over(Rect&,Buffer*)
  253. {
  254.     //Still unimplemented
  255.     changed = 1;
  256. }
  257.  
  258.