list.c 5.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * RichEdit - Basic operations on double linked lists.
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23
 */


#include "editor.h"

24
WINE_DEFAULT_DEBUG_CHANNEL(richedit_lists);
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

void ME_InsertBefore(ME_DisplayItem *diWhere, ME_DisplayItem *diWhat)
{
  diWhat->next = diWhere;
  diWhat->prev = diWhere->prev;

  diWhere->prev->next = diWhat;
  diWhat->next->prev = diWhat;
}

void ME_Remove(ME_DisplayItem *diWhere)
{
  ME_DisplayItem *diNext = diWhere->next;
  ME_DisplayItem *diPrev = diWhere->prev;
  assert(diNext);
  assert(diPrev);
  diPrev->next = diNext;
  diNext->prev = diPrev;
}

45 46
static BOOL ME_DITypesEqual(ME_DIType type, ME_DIType nTypeOrClass)
{
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  switch (nTypeOrClass)
  {
    case diRunOrParagraph:
      return type == diRun || type == diParagraph;
    case diRunOrStartRow:
      return type == diRun || type == diStartRow;
    case diParagraphOrEnd:
      return type == diTextEnd || type == diParagraph;
    case diStartRowOrParagraph:
      return type == diStartRow || type == diParagraph;
    case diStartRowOrParagraphOrEnd:
      return type == diStartRow || type == diParagraph || type == diTextEnd;
    case diRunOrParagraphOrEnd:
      return type == diRun || type == diParagraph || type == diTextEnd;
    default:
      return type == nTypeOrClass;
  }
64 65
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
/* Modifies run pointer to point to the next run, and modify the
 * paragraph pointer if moving into the next paragraph.
 *
 * Returns TRUE if next run is found, otherwise returns FALSE. */
BOOL ME_NextRun(ME_DisplayItem **para, ME_DisplayItem **run)
{
  ME_DisplayItem *p = (*run)->next;
  while (p->type != diTextEnd)
  {
    if (p->type == diParagraph) {
      *para = p;
    } else if (p->type == diRun) {
      *run = p;
      return TRUE;
    }
    p = p->next;
  }
  return FALSE;
}

/* Modifies run pointer to point to the previous run, and modify the
 * paragraph pointer if moving into the previous paragraph.
 *
 * Returns TRUE if previous run is found, otherwise returns FALSE. */
BOOL ME_PrevRun(ME_DisplayItem **para, ME_DisplayItem **run)
{
  ME_DisplayItem *p = (*run)->prev;
  while (p->type != diTextStart)
  {
    if (p->type == diParagraph) {
      if (p->member.para.prev_para->type == diParagraph)
        *para = p->member.para.prev_para;
    } else if (p->type == diRun) {
      *run = p;
      return TRUE;
    }
    p = p->prev;
  }
  return FALSE;
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
ME_DisplayItem *ME_FindItemBack(ME_DisplayItem *di, ME_DIType nTypeOrClass)
{
  if (!di)
    return NULL;
  di = di->prev;
  while(di!=NULL) {
    if (ME_DITypesEqual(di->type, nTypeOrClass))
      return di;
    di = di->prev;
  }
  return NULL;
}

ME_DisplayItem *ME_FindItemBackOrHere(ME_DisplayItem *di, ME_DIType nTypeOrClass)
{
  while(di!=NULL) {
    if (ME_DITypesEqual(di->type, nTypeOrClass))
      return di;
    di = di->prev;
  }
  return NULL;
}

ME_DisplayItem *ME_FindItemFwd(ME_DisplayItem *di, ME_DIType nTypeOrClass)
{
  if (!di) return NULL;
  di = di->next;
  while(di!=NULL) {
    if (ME_DITypesEqual(di->type, nTypeOrClass))
      return di;
    di = di->next;
  }
  return NULL;
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155
static const char *ME_GetDITypeName(ME_DIType type)
{
  switch(type)
  {
    case diParagraph: return "diParagraph";
    case diRun: return "diRun";
    case diCell: return "diCell";
    case diTextStart: return "diTextStart";
    case diTextEnd: return "diTextEnd";
    case diStartRow: return "diStartRow";
    default: return "?";
  }
}

156 157
void ME_DestroyDisplayItem(ME_DisplayItem *item)
{
158 159
  if (0)
    TRACE("type=%s\n", ME_GetDITypeName(item->type));
160
  if (item->type==diParagraph)
161
  {
162
    FREE_OBJ(item->member.para.pFmt);
163 164
    ME_DestroyString(item->member.para.text);
  }
165 166 167

  if (item->type==diRun)
  {
168
    if (item->member.run.ole_obj) ME_DeleteReObject(item->member.run.ole_obj);
169 170
    heap_free( item->member.run.glyphs );
    heap_free( item->member.run.clusters );
171 172 173 174 175
    ME_ReleaseStyle(item->member.run.style);
  }
  FREE_OBJ(item);
}

176 177
ME_DisplayItem *ME_MakeDI(ME_DIType type)
{
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
  ME_DisplayItem *item = ALLOC_OBJ(ME_DisplayItem);
  ZeroMemory(item, sizeof(ME_DisplayItem));
  item->type = type;
  item->prev = item->next = NULL;
  return item;
}

void ME_DumpDocument(ME_TextBuffer *buffer)
{
  /* FIXME this is useless, */
  ME_DisplayItem *pItem = buffer->pFirst;
  TRACE("DOCUMENT DUMP START\n");
  while(pItem) {
    switch(pItem->type)
    {
      case diTextStart:
194
        TRACE("Start\n");
195
        break;
196 197 198 199 200
      case diCell:
        TRACE("Cell(level=%d%s)\n", pItem->member.cell.nNestingLevel,
              !pItem->member.cell.next_cell ? ", END" :
                (!pItem->member.cell.prev_cell ? ", START" :""));
        break;
201
      case diParagraph:
202
        TRACE("Paragraph(ofs=%d)\n", pItem->member.para.nCharOfs);
203 204 205 206
        if (pItem->member.para.nFlags & MEPF_ROWSTART)
          TRACE(" - (Table Row Start)\n");
        if (pItem->member.para.nFlags & MEPF_ROWEND)
          TRACE(" - (Table Row End)\n");
207 208
        break;
      case diStartRow:
209
        TRACE(" - StartRow\n");
210 211
        break;
      case diRun:
212
        TRACE(" - Run(%s, %d, flags=%x)\n", debugstr_run( &pItem->member.run ),
213
          pItem->member.run.nCharOfs, pItem->member.run.nFlags);
214 215
        break;
      case diTextEnd:
216
        TRACE("End(ofs=%d)\n", pItem->member.para.nCharOfs);
217 218 219 220 221 222 223 224
        break;
      default:
        break;
    }
    pItem = pItem->next;
  }
  TRACE("DOCUMENT DUMP END\n");
}