static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) { ME_Run *run = &rundi->member.run; ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow); int runofs = run->nCharOfs+para->nCharOfs; int nSelFrom, nSelTo; const WCHAR wszSpace[] = {' ', 0}; if (run->nFlags & MERF_HIDDEN) return; ME_GetSelection(c->editor, &nSelFrom, &nSelTo); /* Draw selected end-of-paragraph mark */ if (run->nFlags & MERF_ENDPARA && runofs >= nSelFrom && runofs < nSelTo) ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, NULL, 0, 1, c->pt.y + start->member.row.nYPos, start->member.row.nHeight); /* you can always comment it out if you need visible paragraph marks */ if (run->nFlags & (MERF_ENDPARA | MERF_TAB | MERF_CELL)) return; if (run->nFlags & MERF_GRAPHICS) ME_DrawGraphics(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo)); else { if (c->editor->cPasswordMask) { ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText)); ME_DrawTextWithStyle(c, x, y, szMasked->szData, ME_StrVLen(szMasked), run->style, NULL, nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight); ME_DestroyString(szMasked); } else ME_DrawTextWithStyle(c, x, y, run->strText->szData, ME_StrVLen(run->strText), run->style, NULL, nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight); } }
int ME_GetCharBack(const ME_String *s, int nPos) { int nVPos = ME_StrVLen(s); assert(nPos < ME_StrLen(s)); if (nPos) nVPos = ME_StrRelPos2(s, nVPos, -nPos); if (nVPos < s->nLen) return s->szData[nVPos]; return -1; }
BOOL ME_InternalDeleteText(ME_TextEditor *editor, int nOfs, int nChars, BOOL bForce) { ME_Cursor c; int shift = 0; int totalChars = nChars; ME_DisplayItem *start_para; { /* Prevent deletion past last end of paragraph run. */ ME_DisplayItem *pTextEnd = editor->pBuffer->pLast; int nMaxChars = pTextEnd->member.para.prev_para->member.para.nCharOfs; nMaxChars += ME_FindItemBack(pTextEnd, diRun)->member.run.nCharOfs; nMaxChars -= nOfs; nChars = min(nChars, nMaxChars); } ME_CursorFromCharOfs(editor, nOfs, &c); start_para = ME_GetParagraph(c.pRun); if (!bForce) { ME_ProtectPartialTableDeletion(editor, nOfs, &nChars); if (nChars == 0) return FALSE; } while(nChars > 0) { ME_Run *run; ME_CursorFromCharOfs(editor, nOfs+nChars, &c); if (!c.nOffset && nOfs+nChars == (c.pRun->member.run.nCharOfs + ME_GetParagraph(c.pRun)->member.para.nCharOfs)) { /* We aren't deleting anything in this run, so we will go back to the * last run we are deleting text in. */ c.pRun = ME_FindItemBack(c.pRun, diRun); if (c.pRun->member.run.nFlags & MERF_ENDPARA) c.nOffset = c.pRun->member.run.nCR + c.pRun->member.run.nLF; else c.nOffset = c.pRun->member.run.strText->nLen; } run = &c.pRun->member.run; if (run->nFlags & MERF_ENDPARA) { int eollen = run->nCR + run->nLF; BOOL keepFirstParaFormat; if (!ME_FindItemFwd(c.pRun, diParagraph)) { return TRUE; } keepFirstParaFormat = (totalChars == nChars && nChars <= eollen && run->nCharOfs); if (!editor->bEmulateVersion10) /* v4.1 */ { ME_DisplayItem *next_para = ME_FindItemFwd(c.pRun, diParagraphOrEnd); ME_DisplayItem *this_para = next_para->member.para.prev_para; /* The end of paragraph before a table row is only deleted if there * is nothing else on the line before it. */ if (this_para == start_para && next_para->member.para.nFlags & MEPF_ROWSTART) { /* If the paragraph will be empty, then it should be deleted, however * it still might have text right now which would inherit the * MEPF_STARTROW property if we joined it right now. * Instead we will delete it after the preceding text is deleted. */ if (nOfs > this_para->member.para.nCharOfs) { /* Skip this end of line. */ nChars -= (eollen < nChars) ? eollen : nChars; continue; } keepFirstParaFormat = TRUE; } } ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun), keepFirstParaFormat); /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */ ME_CheckCharOffsets(editor); nChars -= (eollen < nChars) ? eollen : nChars; continue; } else { ME_Cursor cursor; int nCharsToDelete = min(nChars, c.nOffset); int i; c.nOffset -= nCharsToDelete; ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP; cursor = c; /* nChars is the number of characters that should be deleted from the PRECEDING runs (these BEFORE cursor.pRun) nCharsToDelete is a number of chars to delete from THIS run */ nChars -= nCharsToDelete; shift -= nCharsToDelete; TRACE("Deleting %d (remaning %d) chars at %d in '%s' (%d)\n", nCharsToDelete, nChars, c.nOffset, debugstr_w(run->strText->szData), run->strText->nLen); if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete) { /* undo = reinsert whole run */ /* nOfs is a character offset (from the start of the document to the current (deleted) run */ ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun); if (pUndo) pUndo->di.member.run.nCharOfs = nOfs+nChars; } else { /* undo = reinsert partial run */ ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun); if (pUndo) { ME_DestroyString(pUndo->di.member.run.strText); pUndo->di.member.run.nCharOfs = nOfs+nChars; pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete); } } TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen); TRACE("Shift value: %d\n", shift); ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete); /* update cursors (including c) */ for (i=-1; i<editor->nCursors; i++) { ME_Cursor *pThisCur = editor->pCursors + i; if (i == -1) pThisCur = &c; if (pThisCur->pRun == cursor.pRun) { if (pThisCur->nOffset > cursor.nOffset) { if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete) pThisCur->nOffset = cursor.nOffset; else pThisCur->nOffset -= nCharsToDelete; assert(pThisCur->nOffset >= 0); assert(pThisCur->nOffset <= ME_StrVLen(run->strText)); } if (pThisCur->nOffset == ME_StrVLen(run->strText)) { pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd); assert(pThisCur->pRun->type == diRun); pThisCur->nOffset = 0; } } } /* c = updated data now */ if (c.pRun == cursor.pRun) ME_SkipAndPropagateCharOffset(c.pRun, shift); else ME_PropagateCharOffset(c.pRun, shift); if (!ME_StrVLen(cursor.pRun->member.run.strText)) { TRACE("Removing useless run\n"); ME_Remove(cursor.pRun); ME_DestroyDisplayItem(cursor.pRun); } shift = 0; /* ME_CheckCharOffsets(editor); */ continue; } } return TRUE; }
void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs, int nChars) { ME_Cursor c; int shift = 0; while(nChars > 0) { ME_Run *run; ME_CursorFromCharOfs(editor, nOfs, &c); run = &c.pRun->member.run; if (run->nFlags & MERF_ENDPARA) { int eollen = run->nCR + run->nLF; if (!ME_FindItemFwd(c.pRun, diParagraph)) { return; } ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun)); /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */ ME_CheckCharOffsets(editor); nChars -= (eollen < nChars) ? eollen : nChars; continue; } else { ME_Cursor cursor; int nIntendedChars = nChars; int nCharsToDelete = nChars; int i; int loc = c.nOffset; ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP; cursor = c; ME_StrRelPos(run->strText, loc, &nChars); /* nChars is the number of characters that should be deleted from the FOLLOWING runs (these AFTER cursor.pRun) nCharsToDelete is a number of chars to delete from THIS run */ nCharsToDelete -= nChars; shift -= nCharsToDelete; TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n", nCharsToDelete, nIntendedChars, nChars, c.nOffset, debugstr_w(run->strText->szData), run->strText->nLen); if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete) { /* undo = reinsert whole run */ /* nOfs is a character offset (from the start of the document to the current (deleted) run */ ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun); if (pUndo) pUndo->di.member.run.nCharOfs = nOfs; } else { /* undo = reinsert partial run */ ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun); if (pUndo) { ME_DestroyString(pUndo->di.member.run.strText); pUndo->di.member.run.nCharOfs = nOfs; pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete); } } TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen); TRACE("Shift value: %d\n", shift); ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete); /* update cursors (including c) */ for (i=-1; i<editor->nCursors; i++) { ME_Cursor *pThisCur = editor->pCursors + i; if (i == -1) pThisCur = &c; if (pThisCur->pRun == cursor.pRun) { if (pThisCur->nOffset > cursor.nOffset) { if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete) pThisCur->nOffset = cursor.nOffset; else pThisCur->nOffset -= nCharsToDelete; assert(pThisCur->nOffset >= 0); assert(pThisCur->nOffset <= ME_StrVLen(run->strText)); } if (pThisCur->nOffset == ME_StrVLen(run->strText)) { pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd); assert(pThisCur->pRun->type == diRun); pThisCur->nOffset = 0; } } } /* c = updated data now */ if (c.pRun == cursor.pRun) ME_SkipAndPropagateCharOffset(c.pRun, shift); else ME_PropagateCharOffset(c.pRun, shift); if (!ME_StrVLen(cursor.pRun->member.run.strText)) { TRACE("Removing useless run\n"); ME_Remove(cursor.pRun); ME_DestroyDisplayItem(cursor.pRun); } shift = 0; /* ME_CheckCharOffsets(editor); */ continue; } } }
static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p) { ME_DisplayItem *pp; ME_Run *run; int len; assert(p->type == diRun); if (!wc->pRowStart) wc->pRowStart = p; run = &p->member.run; run->pt.x = wc->pt.x; run->pt.y = wc->pt.y; ME_WrapSizeRun(wc, p); len = ME_StrVLen(run->strText); if (wc->bOverflown) /* just skipping final whitespaces */ { if (run->nFlags & (MERF_WHITESPACE|MERF_TAB)) { p->member.run.nFlags |= MERF_SKIPPED; /* wc->pt.x += run->nWidth; */ /* skip runs consisting of only whitespaces */ return p->next; } if (run->nFlags & MERF_STARTWHITE) { /* try to split the run at the first non-white char */ int black; black = ME_FindNonWhitespaceV(run->strText, 0); if (black) { wc->bOverflown = FALSE; pp = ME_SplitRun(wc, p, black); p->member.run.nFlags |= MERF_SKIPPED; ME_InsertRowStart(wc, pp); return pp; } } /* black run: the row goes from pRowStart to the previous run */ ME_InsertRowStart(wc, p); return p; } /* simply end the current row and move on to next one */ if (run->nFlags & MERF_ENDROW) { p = p->next; ME_InsertRowStart(wc, p); return p; } /* we're not at the end of the row */ if (run->nFlags & MERF_TAB) { /* force recomputation of tabs' size as it depends on position */ ME_CalcRunExtent(wc->context, &ME_GetParagraph(p)->member.para, wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, run); } /* will current run fit? */ if (wc->pt.x + run->nWidth > wc->nAvailWidth) { int loc = wc->nAvailWidth - wc->pt.x; /* total white run ? */ if (run->nFlags & MERF_WHITESPACE) { /* let the overflow logic handle it */ wc->bOverflown = TRUE; return p; } /* TAB: we can split before */ if (run->nFlags & MERF_TAB) { wc->bOverflown = TRUE; return p; } /* graphics: we can split before, if run's width is smaller than row's width */ if ((run->nFlags & MERF_GRAPHICS) && run->nWidth <= wc->nAvailWidth) { wc->bOverflown = TRUE; return p; } /* can we separate out the last spaces ? (to use overflow logic later) */ if (run->nFlags & MERF_ENDWHITE) { /* we aren't sure if it's *really* necessary, it's a good start however */ int black = ME_ReverseFindNonWhitespaceV(run->strText, len); ME_SplitRun(wc, p, black); /* handle both parts again */ return p; } /* determine the split point by backtracking */ pp = ME_SplitByBacktracking(wc, p, loc); if (pp == wc->pRowStart) { /* we had only spaces so far, entire content can be omitted */ wc->pt.x = 0; return p->next; } if (p != pp) /* found a suitable split point */ { wc->bOverflown = TRUE; return pp; } /* we detected that it's best to split on start of this run */ if (wc->bOverflown) return pp; ERR("failure!\n"); /* not found anything - writing over margins is the only option left */ } if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE)) || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart))) { wc->pLastSplittableRun = p; wc->ptLastSplittableRun = wc->pt; } wc->pt.x += run->nWidth; return p->next; }
static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc) { ME_DisplayItem *piter = p, *pp; int i, idesp, len; ME_Run *run = &p->member.run; idesp = i = ME_CharFromPoint(wc->context, loc, run); len = ME_StrVLen(run->strText); assert(len>0); assert(i<len); if (i) { /* don't split words */ i = ME_ReverseFindWhitespaceV(run->strText, i); pp = ME_MaximizeSplit(wc, p, i); if (pp) return pp; } TRACE("Must backtrack to split at: %s\n", debugstr_w(p->member.run.strText->szData)); if (wc->pLastSplittableRun) { if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB)) { wc->pt = wc->ptLastSplittableRun; return wc->pLastSplittableRun; } else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE) { /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier, they serve no other purpose */ ME_UpdateRunFlags(wc->context->editor, run); assert((wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)); piter = wc->pLastSplittableRun; run = &piter->member.run; len = ME_StrVLen(run->strText); /* don't split words */ i = ME_ReverseFindWhitespaceV(run->strText, len); if (i == len) i = ME_ReverseFindNonWhitespaceV(run->strText, len); if (i) { ME_DisplayItem *piter2 = ME_SplitRun(wc, piter, i); wc->pt = piter2->member.run.pt; return piter2; } /* splittable = must have whitespaces */ assert(0 == "Splittable, but no whitespaces"); } else { /* restart from the first run beginning with spaces */ wc->pt = wc->ptLastSplittableRun; return wc->pLastSplittableRun; } } TRACE("Backtracking failed, trying desperate: %s\n", debugstr_w(p->member.run.strText->szData)); /* OK, no better idea, so assume we MAY split words if we can split at all*/ if (idesp) return ME_SplitRun(wc, piter, idesp); else if (wc->pRowStart && piter != wc->pRowStart) { /* don't need to break current run, because it's possible to split before this run */ wc->bOverflown = TRUE; return piter; } else { /* split point inside first character - no choice but split after that char */ int chars = 1; int pos2 = ME_StrRelPos(run->strText, 0, &chars); if (pos2 != len) { /* the run is more than 1 char, so we may split */ return ME_SplitRun(wc, piter, pos2); } /* the run is one char, can't split it */ return piter; } }