Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * RichEdit - Paragraph wrapping. Don't try to understand it. You've been |
| 3 | * warned ! |
| 4 | * |
| 5 | * Copyright 2004 by Krzysztof Foltman |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | #include "editor.h" |
| 24 | |
| 25 | WINE_DEFAULT_DEBUG_CHANNEL(richedit); |
| 26 | |
| 27 | /* |
| 28 | * Unsolved problems: |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 29 | * |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 30 | * - center and right align in WordPad omits all spaces at the start, we don't |
| 31 | * - objects/images are not handled yet |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 32 | * - no tabs |
| 33 | */ |
| 34 | |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 35 | ME_DisplayItem *ME_MakeRow(int height, int baseline, int width) |
| 36 | { |
| 37 | ME_DisplayItem *item = ME_MakeDI(diStartRow); |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 38 | |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 39 | item->member.row.nHeight = height; |
| 40 | item->member.row.nBaseline = baseline; |
| 41 | item->member.row.nWidth = width; |
| 42 | return item; |
| 43 | } |
| 44 | |
Stefan Huehner | 70c80fb | 2005-07-06 15:53:04 +0000 | [diff] [blame] | 45 | static void ME_BeginRow(ME_WrapContext *wc) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 46 | { |
| 47 | wc->pRowStart = NULL; |
| 48 | wc->bOverflown = FALSE; |
| 49 | wc->pLastSplittableRun = NULL; |
| 50 | wc->nAvailWidth = wc->nTotalWidth - (wc->nRow ? wc->nLeftMargin : wc->nFirstMargin) - wc->nRightMargin; |
| 51 | wc->pt.x = 0; |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 52 | } |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 53 | |
| 54 | void ME_InsertRowStart(ME_WrapContext *wc, ME_DisplayItem *pEnd) |
| 55 | { |
| 56 | ME_DisplayItem *p, *row, *para; |
| 57 | int ascent = 0, descent = 0, width=0, shift = 0, align = 0; |
| 58 | /* wrap text */ |
| 59 | para = ME_GetParagraph(wc->pRowStart); |
| 60 | for (p = wc->pRowStart; p!=pEnd; p = p->next) |
| 61 | { |
| 62 | /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */ |
| 63 | if (p->type==diRun && ((p==wc->pRowStart) || !(p->member.run.nFlags & MERF_ENDPARA))) { /* FIXME add more run types */ |
| 64 | if (p->member.run.nAscent>ascent) |
| 65 | ascent = p->member.run.nAscent; |
| 66 | if (p->member.run.nDescent>descent) |
| 67 | descent = p->member.run.nDescent; |
| 68 | if (!(p->member.run.nFlags & (MERF_ENDPARA|MERF_SKIPPED))) |
| 69 | width += p->member.run.nWidth; |
| 70 | } |
| 71 | } |
| 72 | row = ME_MakeRow(ascent+descent, ascent, width); |
| 73 | row->member.row.nYPos = wc->pt.y; |
| 74 | row->member.row.nLMargin = (!wc->nRow ? wc->nFirstMargin : wc->nLeftMargin); |
| 75 | row->member.row.nRMargin = wc->nRightMargin; |
| 76 | assert(para->member.para.pFmt->dwMask & PFM_ALIGNMENT); |
| 77 | align = para->member.para.pFmt->wAlignment; |
| 78 | if (align == PFA_CENTER) |
| 79 | shift = (wc->nAvailWidth-width)/2; |
| 80 | if (align == PFA_RIGHT) |
| 81 | shift = wc->nAvailWidth-width; |
| 82 | for (p = wc->pRowStart; p!=pEnd; p = p->next) |
| 83 | { |
| 84 | if (p->type==diRun) { /* FIXME add more run types */ |
| 85 | p->member.run.pt.x += row->member.row.nLMargin+shift; |
| 86 | } |
| 87 | } |
| 88 | ME_InsertBefore(wc->pRowStart, row); |
| 89 | wc->nRow++; |
| 90 | wc->pt.y += ascent+descent; |
| 91 | ME_BeginRow(wc); |
| 92 | } |
| 93 | |
Stefan Huehner | 70c80fb | 2005-07-06 15:53:04 +0000 | [diff] [blame] | 94 | static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 95 | { |
| 96 | if (wc->pRowStart) |
| 97 | ME_InsertRowStart(wc, p->next); |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 98 | |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 99 | /* |
| 100 | p = p->member.para.prev_para->next; |
| 101 | while(p) { |
| 102 | if (p->type == diParagraph || p->type == diTextEnd) |
| 103 | return; |
| 104 | if (p->type == diRun) |
| 105 | { |
| 106 | ME_Run *run = &p->member.run; |
| 107 | TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y); |
| 108 | } |
| 109 | p = p->next; |
| 110 | } |
| 111 | */ |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 112 | } |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 113 | |
Stefan Huehner | 70c80fb | 2005-07-06 15:53:04 +0000 | [diff] [blame] | 114 | static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 115 | { |
| 116 | /* FIXME compose style (out of character and paragraph styles) here */ |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 117 | |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 118 | ME_UpdateRunFlags(wc->context->editor, &p->member.run); |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 119 | |
| 120 | ME_CalcRunExtent(wc->context, &ME_GetParagraph(p)->member.para, &p->member.run); |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Stefan Huehner | 70c80fb | 2005-07-06 15:53:04 +0000 | [diff] [blame] | 123 | static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 124 | { |
| 125 | ME_DisplayItem *pp, *piter = p; |
| 126 | int j; |
| 127 | if (!i) |
| 128 | return NULL; |
| 129 | j = ME_ReverseFindNonWhitespaceV(p->member.run.strText, i); |
| 130 | if (j>0) { |
| 131 | pp = ME_SplitRun(wc->context, piter, j); |
| 132 | wc->pt.x += piter->member.run.nWidth; |
| 133 | return pp; |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | pp = piter; |
| 138 | /* omit all spaces before split point */ |
| 139 | while(piter != wc->pRowStart) |
| 140 | { |
| 141 | piter = ME_FindItemBack(piter, diRun); |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 142 | if (piter->member.run.nFlags & MERF_WHITESPACE) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 143 | { |
| 144 | pp = piter; |
| 145 | continue; |
| 146 | } |
| 147 | if (piter->member.run.nFlags & MERF_ENDWHITE) |
| 148 | { |
| 149 | j = ME_ReverseFindNonWhitespaceV(piter->member.run.strText, i); |
| 150 | pp = ME_SplitRun(wc->context, piter, i); |
| 151 | wc->pt = pp->member.run.pt; |
| 152 | return pp; |
| 153 | } |
| 154 | /* this run is the end of spaces, so the run edge is a good point to split */ |
| 155 | wc->pt = pp->member.run.pt; |
| 156 | wc->bOverflown = TRUE; |
| 157 | TRACE("Split point is: %s|%s\n", debugstr_w(piter->member.run.strText->szData), debugstr_w(pp->member.run.strText->szData)); |
| 158 | return pp; |
| 159 | } |
| 160 | wc->pt = piter->member.run.pt; |
| 161 | return piter; |
| 162 | } |
| 163 | } |
| 164 | |
Stefan Huehner | 70c80fb | 2005-07-06 15:53:04 +0000 | [diff] [blame] | 165 | static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 166 | { |
| 167 | ME_DisplayItem *piter = p, *pp; |
| 168 | int i, idesp, len; |
| 169 | ME_Run *run = &p->member.run; |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 170 | |
Phil Krylov | 325e06d | 2006-02-04 17:00:18 +0100 | [diff] [blame] | 171 | idesp = i = ME_CharFromPoint(wc->context->editor, loc, run); |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 172 | len = ME_StrVLen(run->strText); |
| 173 | assert(len>0); |
| 174 | assert(i<len); |
| 175 | if (i) { |
| 176 | /* don't split words */ |
| 177 | i = ME_ReverseFindWhitespaceV(run->strText, i); |
| 178 | pp = ME_MaximizeSplit(wc, p, i); |
| 179 | if (pp) |
| 180 | return pp; |
| 181 | } |
| 182 | TRACE("Must backtrack to split at: %s\n", debugstr_w(p->member.run.strText->szData)); |
| 183 | if (wc->pLastSplittableRun) |
| 184 | { |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 185 | if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB)) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 186 | { |
| 187 | wc->pt = wc->ptLastSplittableRun; |
| 188 | return wc->pLastSplittableRun; |
| 189 | } |
| 190 | else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE) |
| 191 | { |
| 192 | /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier, |
| 193 | they serve no other purpose */ |
| 194 | ME_UpdateRunFlags(wc->context->editor, run); |
| 195 | assert((wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)); |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 196 | |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 197 | piter = wc->pLastSplittableRun; |
| 198 | run = &piter->member.run; |
| 199 | len = ME_StrVLen(run->strText); |
| 200 | /* don't split words */ |
| 201 | i = ME_ReverseFindWhitespaceV(run->strText, len); |
| 202 | if (i == len) |
| 203 | i = ME_ReverseFindNonWhitespaceV(run->strText, len); |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 204 | if (i) { |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 205 | ME_DisplayItem *piter2 = ME_SplitRun(wc->context, piter, i); |
| 206 | wc->pt = piter2->member.run.pt; |
| 207 | return piter2; |
| 208 | } |
| 209 | /* splittable = must have whitespaces */ |
| 210 | assert(0 == "Splittable, but no whitespaces"); |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | /* restart from the first run beginning with spaces */ |
| 215 | wc->pt = wc->ptLastSplittableRun; |
| 216 | return wc->pLastSplittableRun; |
| 217 | } |
| 218 | } |
| 219 | TRACE("Backtracking failed, trying desperate: %s\n", debugstr_w(p->member.run.strText->szData)); |
| 220 | /* OK, no better idea, so assume we MAY split words if we can split at all*/ |
| 221 | if (idesp) |
| 222 | return ME_SplitRun(wc->context, piter, idesp); |
| 223 | else |
| 224 | if (wc->pRowStart && piter != wc->pRowStart) |
| 225 | { |
| 226 | /* don't need to break current run, because it's possible to split |
| 227 | before this run */ |
| 228 | wc->bOverflown = TRUE; |
| 229 | return piter; |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | /* split point inside first character - no choice but split after that char */ |
| 234 | int chars = 1; |
| 235 | int pos2 = ME_StrRelPos(run->strText, 0, &chars); |
| 236 | if (pos2 != len) { |
| 237 | /* the run is more than 1 char, so we may split */ |
| 238 | return ME_SplitRun(wc->context, piter, pos2); |
| 239 | } |
| 240 | /* the run is one char, can't split it */ |
| 241 | return piter; |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 242 | } |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Stefan Huehner | 70c80fb | 2005-07-06 15:53:04 +0000 | [diff] [blame] | 245 | static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 246 | { |
| 247 | ME_DisplayItem *pp; |
| 248 | ME_Run *run; |
| 249 | int len; |
| 250 | |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 251 | assert(p->type == diRun); |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 252 | if (!wc->pRowStart) |
| 253 | wc->pRowStart = p; |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 254 | run = &p->member.run; |
| 255 | run->pt.x = wc->pt.x; |
| 256 | run->pt.y = wc->pt.y; |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 257 | ME_WrapSizeRun(wc, p); |
| 258 | len = ME_StrVLen(run->strText); |
| 259 | |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 260 | if (wc->bOverflown) /* just skipping final whitespaces */ |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 261 | { |
| 262 | if (run->nFlags & (MERF_WHITESPACE|MERF_TAB)) { |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 263 | p->member.run.nFlags |= MERF_SKIPPED; |
| 264 | /* wc->pt.x += run->nWidth; */ |
| 265 | /* skip runs consisting of only whitespaces */ |
| 266 | return p->next; |
| 267 | } |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 268 | |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 269 | if (run->nFlags & MERF_STARTWHITE) { |
| 270 | /* try to split the run at the first non-white char */ |
| 271 | int black; |
| 272 | black = ME_FindNonWhitespaceV(run->strText, 0); |
| 273 | if (black) { |
| 274 | wc->bOverflown = FALSE; |
| 275 | pp = ME_SplitRun(wc->context, p, black); |
| 276 | p->member.run.nFlags |= MERF_SKIPPED; |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 277 | ME_InsertRowStart(wc, pp); |
| 278 | return pp; |
| 279 | } |
| 280 | } |
| 281 | /* black run: the row goes from pRowStart to the previous run */ |
| 282 | ME_InsertRowStart(wc, p); |
| 283 | return p; |
| 284 | } |
| 285 | /* we're not at the end of the row */ |
| 286 | /* will current run fit? */ |
| 287 | if (wc->pt.x + run->nWidth > wc->nAvailWidth) |
| 288 | { |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 289 | int loc = wc->nAvailWidth - wc->pt.x; |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 290 | /* total white run ? */ |
| 291 | if (run->nFlags & MERF_WHITESPACE) { |
| 292 | /* let the overflow logic handle it */ |
| 293 | wc->bOverflown = TRUE; |
| 294 | return p; |
| 295 | } |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 296 | /* graphics or TAB - we can split before */ |
| 297 | if (run->nFlags & (MERF_GRAPHICS|MERF_TAB)) { |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 298 | wc->bOverflown = TRUE; |
| 299 | return p; |
| 300 | } |
| 301 | /* can we separate out the last spaces ? (to use overflow logic later) */ |
| 302 | if (run->nFlags & MERF_ENDWHITE) |
| 303 | { |
| 304 | /* we aren't sure if it's *really* necessary, it's a good start however */ |
| 305 | int black = ME_ReverseFindNonWhitespaceV(run->strText, len); |
| 306 | ME_SplitRun(wc->context, p, black); |
| 307 | /* handle both parts again */ |
| 308 | return p; |
| 309 | } |
| 310 | /* determine the split point by backtracking */ |
| 311 | pp = ME_SplitByBacktracking(wc, p, loc); |
| 312 | if (pp == wc->pRowStart) |
| 313 | { |
| 314 | /* we had only spaces so far, entire content can be omitted */ |
| 315 | wc->pt.x = 0; |
| 316 | return p->next; |
| 317 | } |
| 318 | if (p != pp) /* found a suitable split point */ |
| 319 | { |
| 320 | wc->bOverflown = TRUE; |
| 321 | return pp; |
| 322 | } |
| 323 | /* we detected that it's best to split on start of this run */ |
| 324 | if (wc->bOverflown) |
| 325 | return pp; |
| 326 | ERR("failure!\n"); |
| 327 | /* not found anything - writing over margins is the only option left */ |
| 328 | } |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 329 | if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE)) |
| 330 | || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart))) |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 331 | { |
| 332 | wc->pLastSplittableRun = p; |
| 333 | wc->ptLastSplittableRun = wc->pt; |
| 334 | } |
| 335 | wc->pt.x += run->nWidth; |
| 336 | return p->next; |
| 337 | } |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 338 | |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 339 | void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp) { |
| 340 | ME_DisplayItem *p; |
| 341 | ME_WrapContext wc; |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 342 | int dpi = GetDeviceCaps(c->hDC, LOGPIXELSX); |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 343 | |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 344 | assert(tp->type == diParagraph); |
Krzysztof Foltman | 89075fb | 2005-03-09 11:48:59 +0000 | [diff] [blame] | 345 | if (!(tp->member.para.nFlags & MEPF_REWRAP)) { |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 346 | return; |
| 347 | } |
| 348 | ME_PrepareParagraphForWrapping(c, tp); |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 349 | |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 350 | wc.context = c; |
| 351 | /* wc.para_style = tp->member.para.style; */ |
| 352 | wc.style = NULL; |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 353 | tp->member.para.nRightMargin = tp->member.para.pFmt->dxRightIndent*dpi/1440; |
| 354 | tp->member.para.nFirstMargin = tp->member.para.pFmt->dxStartIndent*dpi/1440; |
| 355 | tp->member.para.nLeftMargin = (tp->member.para.pFmt->dxStartIndent+tp->member.para.pFmt->dxOffset)*dpi/1440; |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 356 | wc.nFirstMargin = tp->member.para.nFirstMargin; |
| 357 | wc.nLeftMargin = tp->member.para.nLeftMargin; |
| 358 | wc.nRightMargin = tp->member.para.nRightMargin; |
| 359 | wc.nRow = 0; |
| 360 | wc.pt.x = 0; |
| 361 | wc.pt.y = 0; |
| 362 | wc.nTotalWidth = c->rcView.right - c->rcView.left; |
| 363 | wc.nAvailWidth = wc.nTotalWidth - wc.nFirstMargin - wc.nRightMargin; |
| 364 | wc.pRowStart = NULL; |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 365 | |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 366 | ME_BeginRow(&wc); |
| 367 | for (p = tp->next; p!=tp->member.para.next_para; ) { |
| 368 | assert(p->type != diStartRow); |
| 369 | if (p->type == diRun) { |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 370 | p = ME_WrapHandleRun(&wc, p); |
| 371 | continue; |
| 372 | } |
| 373 | p = p->next; |
| 374 | } |
| 375 | ME_WrapEndParagraph(&wc, p); |
Krzysztof Foltman | 89075fb | 2005-03-09 11:48:59 +0000 | [diff] [blame] | 376 | tp->member.para.nFlags &= ~MEPF_REWRAP; |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 377 | tp->member.para.nHeight = wc.pt.y; |
Phil Krylov | e6cee96 | 2005-07-05 20:56:14 +0000 | [diff] [blame] | 378 | tp->member.para.nRows = wc.nRow; |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | |
| 382 | void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) { |
Phil Krylov | 6818762 | 2006-01-10 20:23:41 +0100 | [diff] [blame] | 383 | ME_DisplayItem *p, *pRow; |
Phil Krylov | eb1c665 | 2005-06-30 18:10:22 +0000 | [diff] [blame] | 384 | |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 385 | /* remove all items that will be reinserted by paragraph wrapper anyway */ |
Phil Krylov | eb1c665 | 2005-06-30 18:10:22 +0000 | [diff] [blame] | 386 | tp->member.para.nRows = 0; |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 387 | for (p = tp->next; p!=tp->member.para.next_para; p = p->next) { |
| 388 | switch(p->type) { |
| 389 | case diStartRow: |
Phil Krylov | 6818762 | 2006-01-10 20:23:41 +0100 | [diff] [blame] | 390 | pRow = p; |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 391 | p = p->prev; |
Phil Krylov | 6818762 | 2006-01-10 20:23:41 +0100 | [diff] [blame] | 392 | ME_Remove(pRow); |
| 393 | ME_DestroyDisplayItem(pRow); |
Krzysztof Foltman | d488f3f | 2005-03-05 11:19:14 +0000 | [diff] [blame] | 394 | break; |
| 395 | default: |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | /* join runs that can be joined, set up flags */ |
| 400 | for (p = tp->next; p!=tp->member.para.next_para; p = p->next) { |
| 401 | int changed = 0; |
| 402 | switch(p->type) { |
| 403 | case diStartRow: assert(0); break; /* should have deleted it */ |
| 404 | case diRun: |
| 405 | while (p->next->type == diRun) { /* FIXME */ |
| 406 | if (ME_CanJoinRuns(&p->member.run, &p->next->member.run)) { |
| 407 | ME_JoinRuns(c->editor, p); |
| 408 | changed = 1; |
| 409 | } |
| 410 | else |
| 411 | break; |
| 412 | } |
| 413 | p->member.run.nFlags &= ~MERF_CALCBYWRAP; |
| 414 | break; |
| 415 | default: |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | } |
Krzysztof Foltman | 89075fb | 2005-03-09 11:48:59 +0000 | [diff] [blame] | 420 | |
Krzysztof Foltman | 810b261 | 2005-03-19 17:06:17 +0000 | [diff] [blame] | 421 | BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor) { |
Krzysztof Foltman | 89075fb | 2005-03-09 11:48:59 +0000 | [diff] [blame] | 422 | HWND hWnd = editor->hWnd; |
| 423 | HDC hDC = GetDC(hWnd); |
| 424 | ME_DisplayItem *item; |
| 425 | ME_Context c; |
Krzysztof Foltman | 810b261 | 2005-03-19 17:06:17 +0000 | [diff] [blame] | 426 | BOOL bModified = FALSE; |
Phil Krylov | 6818762 | 2006-01-10 20:23:41 +0100 | [diff] [blame] | 427 | int yStart = -1, yEnd = -1; |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 428 | |
Krzysztof Foltman | 89075fb | 2005-03-09 11:48:59 +0000 | [diff] [blame] | 429 | ME_InitContext(&c, editor, hDC); |
| 430 | c.pt.x = 0; |
| 431 | c.pt.y = 0; |
| 432 | item = editor->pBuffer->pFirst->next; |
| 433 | while(item != editor->pBuffer->pLast) { |
| 434 | BOOL bRedraw = FALSE; |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 435 | |
Krzysztof Foltman | 89075fb | 2005-03-09 11:48:59 +0000 | [diff] [blame] | 436 | assert(item->type == diParagraph); |
| 437 | if ((item->member.para.nFlags & MEPF_REWRAP) |
| 438 | || (item->member.para.nYPos != c.pt.y)) |
| 439 | bRedraw = TRUE; |
| 440 | item->member.para.nYPos = c.pt.y; |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 441 | |
Krzysztof Foltman | 89075fb | 2005-03-09 11:48:59 +0000 | [diff] [blame] | 442 | ME_WrapTextParagraph(&c, item); |
| 443 | |
| 444 | if (bRedraw) |
Phil Krylov | 6818762 | 2006-01-10 20:23:41 +0100 | [diff] [blame] | 445 | { |
Krzysztof Foltman | 89075fb | 2005-03-09 11:48:59 +0000 | [diff] [blame] | 446 | item->member.para.nFlags |= MEPF_REPAINT; |
Phil Krylov | 6818762 | 2006-01-10 20:23:41 +0100 | [diff] [blame] | 447 | if (yStart == -1) |
| 448 | yStart = c.pt.y; |
| 449 | } |
Krzysztof Foltman | 89075fb | 2005-03-09 11:48:59 +0000 | [diff] [blame] | 450 | |
Krzysztof Foltman | 810b261 | 2005-03-19 17:06:17 +0000 | [diff] [blame] | 451 | bModified = bModified | bRedraw; |
| 452 | |
Krzysztof Foltman | 89075fb | 2005-03-09 11:48:59 +0000 | [diff] [blame] | 453 | c.pt.y += item->member.para.nHeight; |
Phil Krylov | 6818762 | 2006-01-10 20:23:41 +0100 | [diff] [blame] | 454 | if (bRedraw) |
| 455 | yEnd = c.pt.y; |
Krzysztof Foltman | 89075fb | 2005-03-09 11:48:59 +0000 | [diff] [blame] | 456 | item = item->member.para.next_para; |
| 457 | } |
| 458 | editor->sizeWindow.cx = c.rcView.right-c.rcView.left; |
| 459 | editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top; |
Phil Krylov | a91ba78 | 2005-11-03 09:52:29 +0000 | [diff] [blame] | 460 | |
Krzysztof Foltman | f7e0f7c | 2005-03-09 18:43:18 +0000 | [diff] [blame] | 461 | editor->nTotalLength = c.pt.y; |
Krzysztof Foltman | 13578c8 | 2005-04-16 10:48:35 +0000 | [diff] [blame] | 462 | |
Krzysztof Foltman | 89075fb | 2005-03-09 11:48:59 +0000 | [diff] [blame] | 463 | ME_DestroyContext(&c); |
| 464 | ReleaseDC(hWnd, hDC); |
Phil Krylov | 6818762 | 2006-01-10 20:23:41 +0100 | [diff] [blame] | 465 | |
| 466 | if (editor->bRedraw) |
| 467 | { |
| 468 | RECT rc = c.rcView; |
| 469 | |
| 470 | /* Invalidate rewrapped rows */ |
| 471 | if (yStart != -1) |
| 472 | { |
| 473 | yStart -= ME_GetYScrollPos(editor); |
| 474 | yEnd -= ME_GetYScrollPos(editor); |
| 475 | if ((yStart >= 0 && yStart < c.rcView.bottom - c.rcView.top) |
| 476 | || (yEnd >= 0 && yEnd < c.rcView.bottom - c.rcView.top)) |
| 477 | { |
| 478 | rc.top = yStart; |
| 479 | rc.bottom = yEnd; |
| 480 | InvalidateRect(editor->hWnd, &rc, TRUE); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /* Invalidate cursor row */ |
| 485 | if (editor->nInvalidOfs != -1) |
| 486 | { |
| 487 | ME_InvalidateFromOfs(editor, editor->nInvalidOfs); |
| 488 | editor->nInvalidOfs = -1; |
| 489 | } |
| 490 | } |
Krzysztof Foltman | 810b261 | 2005-03-19 17:06:17 +0000 | [diff] [blame] | 491 | return bModified; |
Krzysztof Foltman | 89075fb | 2005-03-09 11:48:59 +0000 | [diff] [blame] | 492 | } |
Phil Krylov | a91ba78 | 2005-11-03 09:52:29 +0000 | [diff] [blame] | 493 | |
| 494 | |
| 495 | void |
| 496 | ME_SendRequestResize(ME_TextEditor *editor, BOOL force) |
| 497 | { |
| 498 | if (editor->nEventMask & ENM_REQUESTRESIZE) |
| 499 | { |
| 500 | RECT rc; |
| 501 | |
| 502 | GetClientRect(editor->hWnd, &rc); |
| 503 | |
| 504 | if (force || rc.bottom != editor->nTotalLength) |
| 505 | { |
| 506 | REQRESIZE info; |
| 507 | |
| 508 | info.nmhdr.hwndFrom = editor->hWnd; |
| 509 | info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID); |
| 510 | info.nmhdr.code = EN_REQUESTRESIZE; |
| 511 | info.rc = rc; |
| 512 | info.rc.bottom = editor->nTotalLength; |
| 513 | |
| 514 | SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, |
| 515 | info.nmhdr.idFrom, (LPARAM)&info); |
| 516 | } |
| 517 | } |
| 518 | } |