row.c 3.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * RichEdit - Operations on rows of text (rows are recreated during
 * wrapping and are used for displaying the document, they don't keep any
 * true document content; delete all rows, rewrap all paragraphs and 
 * you get them back).
 * 
 * Copyright 2004 by Krzysztof Foltman
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 23 24 25 26 27 28 29 30
 */ 


#include "editor.h"

ME_DisplayItem *ME_FindRowStart(ME_Context *c, ME_DisplayItem *item, 
                                int nRelPos) {
  ME_DisplayItem *para = ME_GetParagraph(item);
  ME_MustBeWrapped(c, para);
31
  if(nRelPos<=0) { /* if this or preceding row */
32
    do {
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
      ME_DisplayItem *item2 = ME_FindItemBack(item, diStartRowOrParagraph);
      if (item2->type == diParagraph)
      {
        if (item2->member.para.prev_para == NULL)
          return item;
        /* if skipping to the preceding paragraph, ensure it's wrapped */
        ME_MustBeWrapped(c, item2->member.para.prev_para);
        item = item2;
        continue;
      }
      else if (item2->type == diStartRow)
      {
        nRelPos++;
        if (nRelPos>0)
          return item;
        item = item2;
        continue;
      }
      assert(0 == "bug in FindItemBack(item, diStartRowOrParagraph)");
      item = item2;
53
    } while(1);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  }
  while(nRelPos>0) { /* if one of the next rows */
    ME_DisplayItem *item2 = ME_FindItemFwd(item, diStartRowOrParagraph);
    if (!item2)
      return item;
    if (item2->type == diParagraph)
    {
      if (item2->member.para.next_para == NULL)
        return item;
      continue;
    }
    item = item2;
    nRelPos--;
  }
  return item;
}

/* I'm sure these functions would simplify some code in caret ops etc,
 * I just didn't remember them when I wrote that code
 */ 

ME_DisplayItem *ME_RowStart(ME_DisplayItem *item) {
  return ME_FindItemBackOrHere(item, diStartRow);
}

79
/*
80 81 82 83 84
ME_DisplayItem *ME_RowEnd(ME_DisplayItem *item) {
  ME_DisplayItem *item2 = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
  if (!item2) return NULL;
  return ME_FindItemBack(item, diRun);
}
85
*/
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

ME_DisplayItem *
ME_FindRowWithNumber(ME_TextEditor *editor, int nRow)
{
  ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
  int nCount = 0;
  
  while (item && nCount + item->member.para.nRows <= nRow)
  {
    nCount += item->member.para.nRows;
    item = ME_FindItemFwd(item, diParagraph);
  }
  if (!item)
    return item;
  for (item = ME_FindItemFwd(item, diStartRow); item && nCount < nRow; nCount++)
    item = ME_FindItemFwd(item, diStartRow);
  return item;
}
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118


int
ME_RowNumberFromCharOfs(ME_TextEditor *editor, int nOfs)
{
  ME_DisplayItem *item = editor->pBuffer->pFirst->next;
  int nRow = 0;

  while (item && item->member.para.next_para->member.para.nCharOfs <= nOfs)
  {
    nRow += item->member.para.nRows;
    item = ME_FindItemFwd(item, diParagraph);
  }
  if (item)
  {
119 120
    ME_DisplayItem *next_para = item->member.para.next_para;
    
121 122 123 124
    nOfs -= item->member.para.nCharOfs;
    item = ME_FindItemFwd(item, diRun);
    while ((item = ME_FindItemFwd(item, diStartRowOrParagraph)) != NULL)
    {
125 126
      if (item == next_para)
        break;
127 128 129 130 131 132 133 134
      item = ME_FindItemFwd(item, diRun);
      if (item->member.run.nCharOfs > nOfs)
        break;
      nRow++;
    }
  }
  return nRow;
}