source: branches/branch-1-0/src/helpers/textview.c@ 374

Last change on this file since 374 was 374, checked in by pr, 17 years ago

Fix scroll bar bugs in textview controls. Bug 1086.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 139.0 KB
Line 
1
2/*
3 *@@sourcefile textview.c:
4 * all-new XTextView control as well as device-independent
5 * text formatting and printing. Whoa.
6 *
7 * <B>Text view control</B>
8 *
9 * This is a read-only control to display any given text
10 * (PSZ) in any given font. As opposed to a multi-line entry
11 * field (MLE), this can handle multiple fonts and character
12 * and paragraph formatting. Also, this thing sets its scroll
13 * bars right, which is one of the most annoying bugs in the
14 * MLE control.
15 *
16 * This is currently in the process of turning into a full-fledged
17 * "rich text" control. For example, the WarpIN "Readme" pages
18 * use this control.
19 *
20 * This is all new with V0.9.1. Great changes have been made
21 * with V0.9.3.
22 *
23 * To use the text view control, you must call txvRegisterTextView
24 * in your application first. This registers the WC_XTEXTVIEW
25 * window class with PM.
26 *
27 * Then create your XTextView using WinCreateWindow. The
28 * XTextView control has an XTEXTVIEWCDATA control data
29 * structure which optionally can be passed to WinCreateWindow
30 * like this:
31 *
32 + XTEXTVIEWCDATA xtxvCData;
33 + memset(&xtxvCData, 0, sizeof(xtxvCData));
34 + xtxvCData.cbData = sizeof(xtxvCData);
35 + xtxvCData.flStyle = XTXF_VSCROLL;
36 + xtxvCData.ulXBorder = 20;
37 + xtxvCData.ulYBorder = 20;
38 + G_hwndProcView = WinCreateWindow(hwndClient, // parent
39 + WC_XTEXTVIEW, // class
40 + "", // title, always ignored
41 + WS_VISIBLE, // style flags
42 + 0, 0, 100, 100, // pos and size
43 + hwndClient, // owner
44 + HWND_TOP, // z-order
45 + ID_PROCINFO, // win ID
46 + &xtxvCData, // control data
47 + 0); // presparams
48 +
49 * <B>Setting the text to be displayed</B>
50 *
51 * The text to be displayed must be passed to the control using
52 * the standard WinSetWindowText function (upon which PM sends a
53 * WM_SETWINDOWPARMS message to the control), which is then
54 * automatically formatted and painted.
55 *
56 * However, since the XTextView control is extremely capable,
57 * a few things need to be noted:
58 *
59 * -- The text view assumes that lines terminate with \n ONLY.
60 * The \r char is used for soft line breaks (start new line
61 * in the same paragraph, similar to the HTML BR tag). If
62 * you give the control the usual OS/2 \r\n sequence, you
63 * get large spacings. Use txvStripLinefeeds to strip the
64 * \r characters before setting the text.
65 *
66 * In short, to give the control any text, do this:
67 +
68 + PSZ psz = ... // whatever, load string
69 + txvStripLinefeeds(&psz); // reallocates
70 + WinSetWindowText(hwndTextView, psz);
71 *
72 * -- The control uses the \xFF (255) character internally as
73 * an escape code for formatting commands. See "Escape codes"
74 * below. If your text contains this character, you should
75 * overwrite all occurences with spaces, or they will be
76 * considered an escape code, which will cause problems.
77 *
78 * -- If you don't care about learning all the escape codes,
79 * you can automatically have HTML code converted to the
80 * XTextView format using txvConvertFromHTML, which will
81 * automatically insert all the codes right from plain
82 * HTML. In the above code, use txvConvertFromHTML instead
83 * of txvStripLinefeeds.
84 *
85 * <B>Code page support</B>
86 *
87 * The XTextView control assumes that the text given to it uses
88 * the same codepage as the message queue (thread) on which
89 * the control is running. So if you need codepage support,
90 * issue WinSetCp before creating the text view control.
91 *
92 * <B>Text formatting</B>
93 *
94 * The XTextView control has a default paragraph format which
95 * determines how text is formatted. If you don't change this
96 * format, the control performs no word-wrapping and displays
97 * all text "as is", that is, practically no formatting is
98 * performed.
99 *
100 * You can change the default paragraph format by sending
101 * TXM_SETPARFORMAT to the control. This takes a XFMTPARAGRAPH
102 * structure for input.
103 *
104 * To quickly enable word-wrapping only, we have the extra
105 * TXM_SETWORDWRAP message. This changes the word-wrapping flag
106 * in the default paragraph format only so you don't have to
107 * mess with all the rest.
108 *
109 * The XTextView control is extremely fast in formatting. It
110 * does pre-calculations once so that resizing the text
111 * window does not perform a full reformat, but a quick
112 * format based on the pre-calculations.
113 *
114 * Presently, formatting is done synchronously. It is planned
115 * to put formatting into a separate thread. Performance is
116 * acceptable already now unless very large texts (> 200 KB)
117 * are formatted (tested on a PII-400 machine).
118 *
119 * <B>Presentation Parameters</B>
120 *
121 * The XTextView control recognizes the following presentation
122 * parameters:
123 *
124 * -- PP_BACKGROUNDCOLOR; if not set, SYSCLR_DIALOGBACKGROUND
125 * (per default gray) is used.
126 *
127 * -- PP_FOREGROUNDCOLOR: if not set, SYSCLR_WINDOWSTATICTEXT
128 * (per default blue) is used to signify that the text
129 * cannot be worked on.
130 *
131 * -- PP_FONTNAMESIZE: default font. This is the system font,
132 * if not set.
133 *
134 * This implies that fonts and colors can be dropped on the
135 * control in the normal way. Font changes will cause a reformat.
136 *
137 * Changing those presentation parameters is equivalent to
138 * changing the corresponding fields in the default paragraph
139 * format using TXM_SETPARFORMAT.
140 *
141 * <B>Escape codes</B>
142 *
143 * All XTextView escape codes start with a \xFF (255) character,
144 * followed by at least one more character. The escape sequences
145 * are variable in length and can have parameters. For details,
146 * see textview.h where all these are listed.
147 *
148 * Escape codes are evaluated by txvFormatText during formatting.
149 *
150 * If you choose to give the text view control a text which
151 * contains escape codes, you better make sure that you get the
152 * exact codes right, or the text view control can crash. The
153 * control has been optimized for speed, so no checking is done
154 * on escape sequences.
155 *
156 * <B>Device-independent text formatting</B>
157 *
158 * If the features of the XTextView control satisfy your needs,
159 * there's not much to worry about. However, if you're interested
160 * in formatting the text yourself, here's more:
161 *
162 * This file has the txvFormatText function, which is capable
163 * of formatting an input string into any HPS. This works for
164 * windows (used by the text view control) and printers (used
165 * by txvPrint). Word-wrapping is supported. This is used by
166 * the XTextView control internally whenever (re)formatting is
167 * needed: either when the text is set or the formatting parameters
168 * (fonts, margins, etc.) have changed.
169 *
170 * These functions are designed to be used in a two-step process:
171 * first format the text (using txvFormatText), then paint it
172 * (using txvPaintText) for viewing or printing.
173 * This speeds up painting dramatically, because formatting
174 * might take some time.
175 *
176 * Note: Version numbering in this file relates to XWorkplace version
177 * numbering.
178 *
179 *@@header "helpers\textview.h"
180 *
181 *@@added V0.9.1 (2000-02-13) [umoeller]
182 */
183
184/*
185 * Copyright (C) 2000-2008 Ulrich M”ller.
186 * This program is part of the XWorkplace package.
187 * This program is free software; you can redistribute it and/or modify
188 * it under the terms of the GNU General Public License as published by
189 * the Free Software Foundation, in version 2 as it comes in the COPYING
190 * file of the XWorkplace main distribution.
191 * This program is distributed in the hope that it will be useful,
192 * but WITHOUT ANY WARRANTY; without even the implied warranty of
193 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
194 * GNU General Public License for more details.
195 */
196
197#define OS2EMX_PLAIN_CHAR
198 // this is needed for "os2emx.h"; if this is defined,
199 // emx will define PSZ as _signed_ char, otherwise
200 // as unsigned char
201
202#define OS2EMX_PLAIN_CHAR
203 // this is needed for "os2emx.h"; if this is defined,
204 // emx will define PSZ as _signed_ char, otherwise
205 // as unsigned char
206
207#define INCL_WINWINDOWMGR
208#define INCL_WINFRAMEMGR
209#define INCL_WINMESSAGEMGR
210#define INCL_WININPUT
211#define INCL_WINRECTANGLES
212#define INCL_WINPOINTERS
213#define INCL_WINSYS
214#define INCL_WINSCROLLBARS
215#define INCL_WINSTDFONT
216#define INCL_WINCOUNTRY
217
218#define INCL_DEV
219#define INCL_SPL
220#define INCL_SPLDOSPRINT
221
222#define INCL_GPIPRIMITIVES
223#define INCL_GPILCIDS
224#define INCL_GPILOGCOLORTABLE
225#define INCL_GPITRANSFORMS
226#define INCL_GPIREGIONS
227
228#define INCL_ERRORS
229#include <os2.h>
230
231#include <stdlib.h>
232#include <stdio.h>
233#include <string.h>
234
235#include "setup.h" // code generation and debugging options
236
237#include "helpers\comctl.h"
238#include "helpers\gpih.h"
239#include "helpers\linklist.h"
240#include "helpers\stringh.h"
241#include "helpers\winh.h"
242#include "helpers\xstring.h" // extended string helpers
243
244#include "helpers\textview.h"
245#include "helpers\textv_html.h"
246
247#pragma hdrstop
248
249/*
250 *@@category: Helpers\PM helpers\Window classes\XTextView control
251 * See textview.c.
252 */
253
254/* ******************************************************************
255 *
256 * Device-independent functions
257 *
258 ********************************************************************/
259
260/*
261 *@@ txvInitFormat:
262 *
263 */
264
265VOID txvInitFormat(PXFORMATDATA pxfd)
266{
267 memset(pxfd, 0, sizeof(XFORMATDATA));
268 lstInit(&pxfd->llRectangles,
269 TRUE); // auto-free items
270 lstInit(&pxfd->llWords,
271 TRUE); // auto-free items
272 xstrInit(&pxfd->strViewText, 0);
273}
274
275/*
276 *@@ SetSubFont:
277 *
278 *@@added V0.9.3 (2000-05-06) [umoeller]
279 */
280
281STATIC VOID SetSubFont(HPS hps,
282 PXFMTFONT pFont,
283 ULONG ulPointSize,
284 PSZ pszFaceName,
285 ULONG flFormat)
286{
287 CHAR ac[256];
288 ULONG ul;
289 POINTL ptlStart = {0, 0},
290 aptl[257];
291
292 if (pFont->lcid)
293 {
294 // font already loaded:
295 if (GpiQueryCharSet(hps) == pFont->lcid)
296 // font currently selected:
297 GpiSetCharSet(hps, LCID_DEFAULT);
298 GpiDeleteSetId(hps, pFont->lcid);
299
300 }
301
302 if (pszFaceName)
303 pFont->lcid = gpihFindFont(hps,
304 ulPointSize,
305 TRUE, // family, not face name
306 pszFaceName,
307 flFormat,
308 &pFont->FontMetrics);
309 else
310 pFont->lcid = LCID_DEFAULT; // 0
311
312 GpiSetCharSet(hps, pFont->lcid);
313 if (pFont->FontMetrics.fsDefn & FM_DEFN_OUTLINE)
314 // is outline font:
315 gpihSetPointSize(hps, ulPointSize);
316
317 for (ul = 0;
318 ul < 256;
319 ul++)
320 ac[ul] = ul;
321
322 GpiQueryCharStringPosAt(hps,
323 &ptlStart,
324 0,
325 254, // starting at one
326 ac + 1, // starting at one
327 NULL,
328 aptl);
329 // now compute width of every char
330 for (ul = 1;
331 ul < 256;
332 ul++)
333 {
334 pFont->alCX[ul] = aptl[ul+1].x - aptl[ul].x;
335 }
336}
337
338/*
339 *@@ SetFormatFont:
340 * creates logical fonts from the specified
341 * font information.
342 *
343 *@@added V0.9.3 (2000-05-06) [umoeller]
344 */
345
346STATIC VOID SetFormatFont(HPS hps, // in: HPS to select default font into
347 PXFMTCHARACTER pxfmtc, // in/out: format data
348 ULONG ulPointSize, // in: font point size (e.g. 12) or 0
349 PSZ pszFaceName) // in: font face name (e.g. "Courier") or NULL
350{
351 pxfmtc->lPointSize = ulPointSize;
352
353 // regular
354 SetSubFont(hps,
355 &pxfmtc->fntRegular,
356 ulPointSize,
357 pszFaceName,
358 0);
359
360 // bold
361 SetSubFont(hps,
362 &pxfmtc->fntBold,
363 ulPointSize,
364 pszFaceName,
365 FATTR_SEL_BOLD);
366
367 // italics
368 SetSubFont(hps,
369 &pxfmtc->fntItalics,
370 ulPointSize,
371 pszFaceName,
372 FATTR_SEL_ITALIC);
373
374 // bold italics
375 SetSubFont(hps,
376 &pxfmtc->fntBoldItalics,
377 ulPointSize,
378 pszFaceName,
379 FATTR_SEL_BOLD | FATTR_SEL_ITALIC);
380}
381
382/*
383 *@@ AppendCharNoCheck:
384 *
385 *@@added V0.9.3 (2000-05-07) [umoeller]
386 */
387
388STATIC VOID AppendCharNoCheck(char **ppszNew,
389 PULONG pcbNew,
390 char **ppTarget,
391 char c)
392{
393 ULONG cbSizeThis = *ppTarget - *ppszNew;
394 if (cbSizeThis >= *pcbNew)
395 {
396 // more mem needed:
397 *pcbNew += 10000;
398 *ppszNew = (PSZ)realloc(*ppszNew, *pcbNew);
399 // if first call, pszNew is NULL, and realloc
400 // behaves just like malloc
401 // adjust target, because ptr might have changed
402 *ppTarget = *ppszNew + cbSizeThis;
403 }
404
405 **ppTarget = c;
406 (*ppTarget)++;
407}
408
409/*
410 *@@ txvStripLinefeeds:
411 * this removes all linefeeds (\r) from
412 * the specified string to prepare it
413 * for display with the text view control.
414 *
415 * This also replaces tabs (\t) with ulTabSize spaces.
416 *
417 * The buffer gets reallocated by this function, so it
418 * must be free()'able.
419 *
420 *@@added V0.9.3 (2000-05-07) [umoeller]
421 *@@changed V0.9.20 (2002-08-10) [umoeller]: now stripping \xFF too
422 */
423
424VOID txvStripLinefeeds(char **ppszText,
425 ULONG ulTabSize)
426{
427 PSZ pSource = *ppszText;
428 ULONG cbNew = 1000;
429 PSZ pszNew = (PSZ)malloc(cbNew);
430 PSZ pTarget = pszNew;
431 ULONG ul;
432
433 while (*pSource)
434 {
435 switch (*pSource)
436 {
437 case '\r':
438 pSource++;
439 break;
440
441 case '\t':
442 for (ul = 0;
443 ul < ulTabSize;
444 ul++)
445 AppendCharNoCheck(&pszNew,
446 &cbNew,
447 &pTarget,
448 ' ');
449
450 // skip the tab
451 pSource++;
452 break;
453
454 case '\xFF': // V0.9.20 (2002-08-10) [umoeller]
455 AppendCharNoCheck(&pszNew,
456 &cbNew,
457 &pTarget,
458 ' ');
459 pSource++;
460 break;
461
462 default:
463 AppendCharNoCheck(&pszNew,
464 &cbNew,
465 &pTarget,
466 *pSource++);
467 }
468 }
469
470 AppendCharNoCheck(&pszNew,
471 &cbNew,
472 &pTarget,
473 '\n');
474 AppendCharNoCheck(&pszNew,
475 &cbNew,
476 &pTarget,
477 0);
478
479 free(*ppszText);
480 *ppszText = pszNew;
481}
482
483/* ******************************************************************
484 *
485 * Device-independent text formatting
486 *
487 ********************************************************************/
488
489/*
490 *@@ strhFindEOL2:
491 * finds the end of a line.
492 *
493 * An "end of line" means the next \r, \n, or \0 character
494 * after *ppszSearchIn.
495 *
496 * This returns the pointer to that exact character, which
497 * can be equal or higher than *ppszSearchIn.
498 * This should never return NULL because at some point,
499 * there will be a null byte in your string (unless you have
500 * a heap problem).
501 *
502 * If the EOL character is not null (\0), *ppszSearchIN is
503 * advanced to the first character of the _next_ line. This
504 * can be the EOL pointer plus one if you have a UNIX-style
505 * string (\n only at the end of each line) or EOL + 2 for
506 * DOS and OS/2-style EOLs (which have \r\n at the end of
507 * each line).
508 *
509 *@added V0.9.3 (2000-05-06) [umoeller]
510 */
511
512STATIC PSZ strhFindEOL2(PSZ *ppszSearchIn, // in: where to search
513 PULONG pulOffset) // out: offset (ptr can be NULL)
514{
515 PSZ pThis = *ppszSearchIn,
516 prc = NULL;
517 while (TRUE)
518 {
519 if ( (*pThis == '\r') || (*pThis == '\n') || (*pThis == 0) )
520 {
521 prc = pThis;
522 break;
523 }
524 pThis++;
525 }
526
527 // before modifying pointer, store offset
528 if (pulOffset)
529 *pulOffset = prc - *ppszSearchIn;
530
531 if (*prc == 0)
532 {
533 // null byte (end of string):
534 *ppszSearchIn = prc;
535 }
536 else
537 {
538 // not null byte (end of string):
539 // skip following newline characters
540 if (*prc == '\r')
541 {
542 if ( *(prc+1) == '\n')
543 // we have a \r char next,
544 // that's the DOS and OS/2 format (\r\n):
545 // skip that too
546 *ppszSearchIn = prc + 2;
547 else
548 *ppszSearchIn = prc + 1;
549 }
550 else if (*prc == '\n')
551 // UNIX format (used by HTML formatter also):
552 *ppszSearchIn = prc + 1;
553 }
554
555 // now:
556 // 1) prc points to the \r, \n, or \0 character (EOL)
557 // 2) *ppszSearchIn has been advanced to the first character
558 // of the next line or points to the \0 character
559
560 return prc;
561}
562
563/* #define TXVFRECTF_EMPTY 0x0001
564#define TXVFRECTF_PARAGRAPHDONE 0x0002
565#define TXVFRECTF_WORDSLEFT 0x0004
566#define TXVFRECTF_STOPPEDONESCAPE 0x0008
567#define TXVFRECTF_ENDOFTEXT 0x0010
568 */
569
570/*
571 *@@ FORMATLINEBUF:
572 * worker structure to store various data
573 * in txvFormatText in between CreateWord
574 * calls. This has been created for speed
575 * so we don't have to pass all these on
576 * the stack all the time.
577 *
578 *@@added V0.9.3 (2000-05-06) [umoeller]
579 */
580
581typedef struct _FORMATLINEBUF
582{
583 PSZ pLastChar; // ptr to null terminator in text
584
585 // formatting data; this is set by txvFormatText according
586 // to escape characters and read by txvCreateRectangle
587 XFMTPARAGRAPH fmtp;
588 PXFMTCHARACTER pfmtc; // pointer to character formatting data
589 PXFMTFONT pfmtf; // pointer to font to use
590
591 BOOL fPre,
592 fBold,
593 fItalics;
594
595 // current anchor
596 PCSZ pcszCurrentLinkTarget;
597 // this is != NULL if we're currently in a link block
598 // and points to an item in XFORMATDATA.llLinks
599 // (simply copied to the word structs that are created)
600
601 // data copied to TXVWORD
602 LONG lcid;
603 LONG lPointSize;
604 ULONG flChar; // any combination of CHS_UNDERSCORE and CHS_STRIKEOUT
605
606 // counters, ...
607 LONG lXCurrent; // current X position while adding words to rectangle
608} FORMATLINEBUF, *PFORMATLINEBUF;
609
610/*
611 *@@ CreateWord:
612 *
613 * -- If the word ends with one or several spaces,
614 * ppStartOfWord is set to the beginning of the
615 * next word (non-space character).
616 * pWord->ulFlags is set to 0.
617 *
618 * -- If the word ends with an escape character,
619 * ppStartOfWord is set to point to the escape,
620 * which must be handled by the caller.
621 * pWord->ulFlags is set to TXVWORDF_GLUEWITHNEXT.
622 *
623 * -- If the word ends with a \n or \r,
624 * ppStartOfWord is set to the beginning of the
625 * next line (first char after \n or \r). This
626 * may be another \n or \r, but the first one
627 * is skipped.
628 * pWord->ulFlags is set to TXVWORDF_LINEBREAK or
629 * TXVWORDF_LINEFEED.
630 *
631 *@@added V0.9.3 (2000-05-14) [umoeller]
632 *@@changed V0.9.20 (2002-08-10) [umoeller]: rewrote link implementation
633 */
634
635STATIC PTXVWORD CreateWord(HPS hps,
636 PSZ *ppStartOfWord,
637 PFORMATLINEBUF pflbuf)
638{
639 PTXVWORD pWord = NULL;
640
641 // find next word:
642 if (**ppStartOfWord)
643 {
644 PSZ pWordStart = *ppStartOfWord,
645 pWordEnd = NULL;
646 PSZ pCheck = *ppStartOfWord;
647 ULONG cChars = 0;
648 // cCheck = 0;
649
650 pWord = (PTXVWORD)malloc(sizeof(TXVWORD));
651 memset(pWord, 0, sizeof(TXVWORD));
652 // this includes fIsEscapeSequence = FALSE;
653 pWord->pStart = pWordStart;
654
655 // initially, this has pWordStart pointing
656 // to *ppStartOfWord. If a word is found,
657 // pWordStart is set to the first char of
658 // the word and pWordEnd receives the
659 // pointer to the first character after the word (probably space)
660 if (strhGetWord(&pWordStart, // in/out
661 pflbuf->pLastChar,
662 " ",
663 "\x0d\x0a \xFF", // in: end chars; includes our escape!
664 &pWordEnd)) // out: first char after word
665 {
666 // whoa, found a word:
667 while (*pWordEnd == ' ')
668 pWordEnd++;
669
670 cChars = (pWordEnd - *ppStartOfWord);
671 }
672
673 if (cChars)
674 {
675 POINTL aptlText[TXTBOX_COUNT];
676 // cChars is != 0 if strhGetWord succeeded AND the
677 // line is not empty, so go on
678 // cCheck = cChars;
679
680 // advance input pointer
681 *ppStartOfWord = pWordEnd;
682
683 GpiQueryTextBox(hps,
684 // no. of chars since start of word:
685 cChars,
686 // first char:
687 pCheck,
688 TXTBOX_COUNT,
689 (PPOINTL)&aptlText);
690
691 pWord->cChars = cChars;
692 pWord->ulFlags = 0;
693
694 if (cChars)
695 pWord->ulCXWithSpaces = aptlText[TXTBOX_TOPRIGHT].x;
696 else
697 pWord->ulCXWithSpaces = 0;
698
699 pWord->ulCY = aptlText[TXTBOX_TOPRIGHT].y
700 - aptlText[TXTBOX_BOTTOMRIGHT].y;
701 // store base line ofs; aptlText[TXTBOX_BOTTOMRIGHT].y is negative
702 // if the string has any characters drawn below the base line, e.g.
703 // for the "g" and "y" characters
704 pWord->ulBaseLineOfs = -aptlText[TXTBOX_BOTTOMRIGHT].y;
705 }
706 else
707 {
708 // no word found or empty line:
709 pWord->ulCY = pflbuf->pfmtf->FontMetrics.lMaxBaselineExt;
710 }
711
712 switch (**ppStartOfWord)
713 {
714 case TXVESC_CHAR: // '\xFF':
715 pWord->ulFlags = TXVWORDF_GLUEWITHNEXT;
716 break;
717
718 case '\n':
719 pWord->ulFlags = TXVWORDF_LINEBREAK;
720 (*ppStartOfWord)++; // skip \n
721 break;
722
723 case '\r':
724 pWord->ulFlags = TXVWORDF_LINEFEED;
725 (*ppStartOfWord)++; // skip \r
726 break;
727 }
728
729 pWord->lcid = pflbuf->pfmtf->lcid;
730 pWord->lPointSize = pflbuf->lPointSize;
731 pWord->flChar = pflbuf->flChar;
732
733 pWord->pcszLinkTarget = pflbuf->pcszCurrentLinkTarget; // 0 if none
734 }
735
736 return pWord;
737}
738
739/*
740 *@@ ProcessEscapes:
741 * gets called when txvFormatText stops on an
742 * escape character (\xFF). This evaluates the
743 * escape sequence, reacts accordingly, and
744 * advances *ppCurrent to after the escape
745 * sequence so that regular processing can
746 * continue.
747 *
748 * There are two types of escape sequences:
749 *
750 * -- Those which are only relevant during word processing,
751 * such as character formatting attributes (bold, italics,
752 * font, size, ...). Those affect the TXVWORD structure
753 * directly and are thus never evaluated in step 2,
754 * rectangles correlation.
755 *
756 * -- Those which affect spacings, margins, etc. (paragraph
757 * formatting). These need to be re-evaluated even during
758 * "quick" format, without words being recalculated, because
759 * those spacings affect the output rectangles.
760 *
761 * If one of those sequences is encountered, this function
762 * appends a special TXVWORD structure to XFORMATDATA.llWords.
763 *
764 *@@added V0.9.3 (2000-05-07) [umoeller]
765 */
766
767STATIC PTXVWORD ProcessEscapes(char **ppCurrent, // in/out: current position; initially points to esc char
768 PXFORMATDATA pxfd, // in/out: formatting data
769 PFORMATLINEBUF pflbuf, // in/out: formatting buffer
770 BOOL fWordsProcessed) // FALSE during step 1 (words processing),
771 // TRUE during step 2 (rectangles correlation)
772{
773 PTXVWORD pEscapeWord = NULL;
774
775 // this is set to TRUE either above or by txvCreateRectangle if
776 // an escape character was found; txvCreateRectangle
777 // then sets pCurrent to the escape character (\xFF)
778 CHAR cCode1 = *((*ppCurrent) + 1);
779 CHAR cCode2 = *((*ppCurrent) + 2);
780 ULONG ulSkip = 3; // per default, skip \xFF plus two
781 CHAR szDecimal[10];
782 LONG lDecimal;
783
784 BOOL fCreateWord = FALSE,
785 fPaintEscapeWord = FALSE;
786
787 switch (cCode1)
788 {
789 case 1: // change font:
790 // three decimals follow specifying the font
791 memcpy(szDecimal, (*ppCurrent) + 2, 3);
792 szDecimal[3] = 0;
793 lDecimal = atoi(szDecimal);
794 if (lDecimal == 0)
795 pflbuf->pfmtc = &pxfd->fmtcStandard;
796 else if (lDecimal == 1)
797 pflbuf->pfmtc = &pxfd->fmtcCode;
798 ulSkip = 5;
799 break;
800
801 case 2: // B or /B
802 if (cCode2 == 1)
803 pflbuf->fBold = TRUE;
804 else
805 pflbuf->fBold = FALSE;
806 break;
807
808 case 3: // I or /I
809 if (cCode2 == 1)
810 pflbuf->fItalics = TRUE;
811 else
812 pflbuf->fItalics = FALSE;
813 break;
814
815 case 4: // U or /U
816 if (cCode2 == 1)
817 pflbuf->flChar |= CHS_UNDERSCORE;
818 else
819 pflbuf->flChar &= ~CHS_UNDERSCORE;
820 break;
821
822 case 5: // STRIKE or /STRIKE
823 if (cCode2 == 1)
824 pflbuf->flChar |= CHS_STRIKEOUT;
825 else
826 pflbuf->flChar &= ~CHS_STRIKEOUT;
827 break;
828
829 case 6: // A HREF= (link)
830 // changed implementation V0.9.20 (2002-08-10) [umoeller]
831 {
832 // this is variable in length and terminated with
833 // another 0xFF char; what's in between is the
834 // link target name and gets appended to
835 // XFORMATDATA.llLinks
836 PSZ pEnd;
837 if (pEnd = strchr((*ppCurrent) + 2, 0xFF))
838 {
839 PSZ pszNewLink = strhSubstr((*ppCurrent) + 2, pEnd);
840 lstAppendItem(&pxfd->llLinks,
841 pszNewLink);
842
843 pflbuf->pcszCurrentLinkTarget = pszNewLink;
844
845 ulSkip = pEnd - *ppCurrent + 1;
846 }
847 }
848 break;
849
850 case 7: // /A HREF (end of link)
851 pflbuf->pcszCurrentLinkTarget = NULL;
852 ulSkip = 2;
853 break;
854
855 case 8: // A NAME= (anchor name)
856 {
857 // this is variable in length and terminated with
858 // another 0xFF char; we completely ignore this
859 // here and just skip the anchor name, this is
860 // only used with TXM_JUMPTOANCHORNAME, which then
861 // searches the buffer
862 PSZ pEnd;
863 if (pEnd = strchr((*ppCurrent) + 2, 0xFF))
864 {
865 ulSkip = pEnd - *ppCurrent + 1;
866 // store this with the other words so we can
867 // find this word later
868 fCreateWord = TRUE;
869 // and store this with the rectangles
870 fPaintEscapeWord = TRUE;
871 }
872 }
873 break;
874
875 case 0x10: // relative point size in percent
876 // three characters follow specifying the
877 // percentage
878 memcpy(szDecimal, (*ppCurrent) + 2, 3);
879 szDecimal[3] = 0;
880 lDecimal = atoi(szDecimal);
881
882 pflbuf->lPointSize = pflbuf->pfmtc->lPointSize * lDecimal / 100;
883 ulSkip = 5;
884 break;
885
886 case 0x20: // left margin changed:
887 memcpy(szDecimal, (*ppCurrent) + 2, 4); // four decimals xxxx
888 szDecimal[4] = 0;
889 lDecimal = atoi(szDecimal);
890
891 // this is based on the current average font width, so
892 // find this:
893 pflbuf->fmtp.lLeftMargin = (lDecimal
894 * pflbuf->lPointSize);
895 ulSkip = 6;
896 fCreateWord = TRUE; // for rectangle correlation
897 break;
898
899 case 0x21: // first line margin changed:
900 memcpy(szDecimal, (*ppCurrent) + 2, 4); // +xxx, -xxx
901 szDecimal[4] = 0;
902 lDecimal = atoi(szDecimal);
903
904 // this is based on the current average font width, so
905 // find this:
906 pflbuf->fmtp.lFirstLineMargin = (lDecimal
907 * pflbuf->lPointSize);
908 ulSkip = 6;
909 fCreateWord = TRUE; // for rectangle correlation
910 break;
911
912 case 0x22: // tab: forward current X to left margin
913 pflbuf->lXCurrent = pflbuf->fmtp.lLeftMargin;
914
915 ulSkip = 2;
916 fCreateWord = TRUE; // for rectangle correlation
917 break;
918
919 case 0x23: // marker: store this in output, this needs
920 // to be painted
921 fCreateWord = TRUE;
922 fPaintEscapeWord = TRUE;
923 ulSkip = 3;
924 break;
925
926 case 0x30: // spacing before paragraph:
927 // four chars follow with either "####" or decimal spacing
928 memcpy(szDecimal, (*ppCurrent) + 2, 4);
929 szDecimal[4] = 0;
930 if (memcmp(szDecimal, "####", 4) == 0)
931 // reset to default:
932 pflbuf->fmtp.lSpaceBefore = pxfd->fmtpStandard.lSpaceBefore;
933 else
934 {
935 lDecimal = atoi(szDecimal);
936 pflbuf->fmtp.lSpaceBefore = lDecimal;
937 }
938 ulSkip = 6;
939 fCreateWord = TRUE; // for rectangle correlation
940 break;
941
942 case 0x31: // spacing before paragraph:
943 // four chars follow with either "####" or decimal spacing
944 memcpy(szDecimal, (*ppCurrent) + 2, 4);
945 szDecimal[4] = 0;
946 if (memcmp(szDecimal, "####", 4) == 0)
947 // reset to default:
948 pflbuf->fmtp.lSpaceAfter = pxfd->fmtpStandard.lSpaceAfter;
949 else
950 {
951 lDecimal = atoi(szDecimal);
952 pflbuf->fmtp.lSpaceAfter = lDecimal;
953 }
954 ulSkip = 6;
955 fCreateWord = TRUE; // for rectangle correlation
956 break;
957
958 case 0x32: // word-wrapping:
959 // here follows a single char being "0" or "1"
960 if ( *((*ppCurrent) + 2) == '0')
961 pflbuf->fmtp.fWordWrap = FALSE;
962 else
963 pflbuf->fmtp.fWordWrap = TRUE;
964 fCreateWord = TRUE; // for rectangle correlation
965 }
966
967 if (fCreateWord) // append for rectangle correlation?
968 if (!fWordsProcessed) // are we processing words still (step 1)?
969 {
970 // yes: append to list for rectangle correlation later
971 pEscapeWord = (PTXVWORD)malloc(sizeof(TXVWORD));
972 memset(pEscapeWord, 0, sizeof(TXVWORD));
973 // mark as escape sequence
974 pEscapeWord->pStart = *ppCurrent;
975 pEscapeWord->cChars = ulSkip;
976 pEscapeWord->cEscapeCode = *(*ppCurrent + 1);
977 pEscapeWord->fPaintEscapeWord = fPaintEscapeWord;
978 pEscapeWord->pcszLinkTarget = pflbuf->pcszCurrentLinkTarget;
979 // V0.9.20 (2002-08-10) [umoeller]
980 // NULL if none
981 if (fPaintEscapeWord)
982 {
983 pEscapeWord->lX = pflbuf->lXCurrent;
984 pEscapeWord->lcid = pflbuf->pfmtf->lcid;
985 pEscapeWord->lPointSize = pflbuf->lPointSize;
986 pEscapeWord->flChar = pflbuf->flChar;
987 }
988 lstAppendItem(&pxfd->llWords, pEscapeWord);
989 }
990
991 if (!fWordsProcessed)
992 // if we're still processing words, advance
993 // current pointer by the escape length
994 *ppCurrent += ulSkip;
995
996 return pEscapeWord;
997}
998
999/*
1000 *@@ txvFormatText:
1001 * this is the core function to text formatting, which
1002 * must be done before the text can be painted into an
1003 * HPS. See the top of textview.c for details.
1004 *
1005 * Even though this function does not seem to have a
1006 * lot of parameters, it is extremely powerful. This
1007 * function handles paragraph and character formatting
1008 * automatically. See XFMTPARAGRAPH and XFMTCHARACTER
1009 * for possible formatting attributes, which are part
1010 * of the XFORMATDATA structure passed to this function.
1011 *
1012 * "Formatting" means splitting up any zero-terminated
1013 * string (XFORMATDATA.pszViewText) into a possibly
1014 * large list of TXVRECTANGLE structures, which each
1015 * hold a rectangle to be painted. This allows for
1016 * extremely fast painting.
1017 *
1018 * Each TXVRECTANGLE in turn holds several "words" to
1019 * be painted. A word consists of a TXVWORD structure
1020 * and is normally a sequence of characters between
1021 * spaces, \n and \r characters. As an exception, if
1022 * escape sequences come up, such a "word" is split up
1023 * into several words because character formatting
1024 * (font, size, ...) is done on a per-word basis when painting.
1025 *
1026 * This approach allows for quicker word-wrapping when only
1027 * the output (paint) rectangle is changed because we don't
1028 * have to re-calculate all the character widths (TXVWORD) once we
1029 * got the words. Instead, when re-formatting, we just recompose
1030 * the rectangles based on the words we calculated already.
1031 * Of course, when character widths change (e.g. because
1032 * fonts are changed), everything has to be redone.
1033 *
1034 * Processing depends on the current formatting settings
1035 * of the XFORMATDATA structure passed to this func and
1036 * can become quite complicated:
1037 *
1038 * -- In the simplest possible formatting mode, that is, if
1039 * word wrapping is disabled, each such TXVRECTANGLE
1040 * structure will hold one paragraph from the text
1041 * (that is, the text between two \n chars).
1042 *
1043 * -- If word wrapping is enabled, each paragraph in the text
1044 * can consist of several such rectangles if the paragraph
1045 * does not fit into one line. In that case, we create
1046 * one XFMTRECTANGLE for each line which is needed to
1047 * display the paragraph word-wrapped.
1048 *
1049 * This uses an XFORMATDATA structure for input and output
1050 * (besides the other parameters).
1051 *
1052 * On input, specify the following:
1053 *
1054 * -- hps: window or printer HPS. This is used for
1055 * formatting only, but nothing is painted.
1056 *
1057 * -- XFORMATDATA.pszViewText: the text to be formatted.
1058 * This must follow certain conventions; the \xFF,
1059 * \r, and \n characters have a special meaning.
1060 * See the top of textview.c for details.
1061 *
1062 * -- XFORMATDATA.fmtpStandard, fmtcStandard, fmtcCode:
1063 * paragraph and character formatting attributes.
1064 * For the simplest possible formatting, memset
1065 * all these to 0. Word-wrapping depends on
1066 * the paragraph formats.
1067 *
1068 * -- prclView: rectangle for which formatting should take
1069 * place. When this is called for a screen window,
1070 * this should be the visible area of the window
1071 * (WinQueryWindowRect).
1072 * When this is called with a printer PS, this should
1073 * be the size of a printer page.
1074 *
1075 * This function updates the following:
1076 *
1077 * -- XFORMATDATA.llWords: list of TXVWORD structures,
1078 * holding all the "words" in the text as described
1079 * above. This list can grow very long, but only needs
1080 * to be recalculated when fonts change.
1081 *
1082 * -- XFORMATDATA.llRectangles: list of TXVRECTANGLE
1083 * structures, correlating the words on the words list
1084 * to paint rectangles.
1085 *
1086 * -- XFORMATDATA.szlWorkspace: total width
1087 * and height of the "workspace", i.e. the total space
1088 * needed to display the text (in pels). This might
1089 * be smaller, the same, or larger than prclView,
1090 * depending on whether the text fits into prclView.
1091 *
1092 * When displaying text, you should display scroll bars
1093 * if the workspace is larger than the window (prclView).
1094 *
1095 * When printing, if the workspace is larger than the
1096 * printer page (prclView), you will need to call
1097 * txvPaintText several times for each page.
1098 *
1099 * All coordinates are in world space (PU_PELS).
1100 *
1101 *@@changed V0.9.3 (2000-05-06) [umoeller]: largely rewritten; now handling paragraph and character formats
1102 *@@changed V1.0.18 (2008-11-16) [pr]: bodge formatting to remove unwanted scroll bars @@fixes 1086
1103 *@@todo TXVWORDF_GLUEWITHNEXT
1104 */
1105
1106VOID txvFormatText(HPS hps, // in: HPS whose font is used for
1107 // calculating text dimensions
1108 PXFORMATDATA pxfd, // in: formatting data
1109 PRECTL prclView, // in: rectangle to format for (window or printer page)
1110 BOOL fFullRecalc) // in: re-calculate word list too? (must be TRUE on the first call)
1111{
1112 /* ULONG ulWinCX = (prclView->xRight - prclView->xLeft),
1113 ulWinCY = (prclView->yTop - prclView->yBottom); */
1114
1115 lstClear(&pxfd->llRectangles);
1116 if (fFullRecalc)
1117 lstClear(&pxfd->llWords);
1118
1119 pxfd->szlWorkspace.cx = 0;
1120 pxfd->szlWorkspace.cy = 0;
1121
1122 if (pxfd->strViewText.cbAllocated)
1123 {
1124 ULONG ulTextLen = pxfd->strViewText.ulLength;
1125
1126 FORMATLINEBUF flbuf;
1127 LONG lcidLast = -99,
1128 lPointSizeLast = -99;
1129
1130 memset(&flbuf, 0, sizeof(flbuf));
1131 // copy default paragraph formatting
1132 memcpy(&flbuf.fmtp, &pxfd->fmtpStandard, sizeof(flbuf.fmtp));
1133 // set font
1134 flbuf.pfmtc = &pxfd->fmtcStandard;
1135 flbuf.lPointSize = pxfd->fmtcStandard.lPointSize;
1136 flbuf.pLastChar = pxfd->strViewText.psz + ulTextLen;
1137
1138 if (ulTextLen)
1139 {
1140 ULONG cWords = 0;
1141
1142 if (fFullRecalc)
1143 {
1144 /*
1145 * step 1: create words
1146 *
1147 */
1148
1149 PSZ pCurrent = pxfd->strViewText.psz;
1150
1151 // loop until null terminator
1152 while (*pCurrent)
1153 {
1154 PTXVWORD pWord;
1155
1156 if (flbuf.fBold)
1157 {
1158 if (flbuf.fItalics)
1159 flbuf.pfmtf = &flbuf.pfmtc->fntBoldItalics;
1160 else
1161 flbuf.pfmtf = &flbuf.pfmtc->fntBold;
1162 }
1163 else
1164 if (flbuf.fItalics)
1165 flbuf.pfmtf = &flbuf.pfmtc->fntItalics;
1166 else
1167 flbuf.pfmtf = &flbuf.pfmtc->fntRegular;
1168
1169 // set font for subsequent calculations,
1170 // if changed (this includes the first call)
1171 if (lcidLast != flbuf.pfmtf->lcid)
1172 {
1173 GpiSetCharSet(hps, flbuf.pfmtf->lcid);
1174 lcidLast = flbuf.pfmtf->lcid;
1175 // force recalc of point size
1176 lPointSizeLast = -99;
1177 }
1178
1179 if (lPointSizeLast != flbuf.lPointSize)
1180 {
1181 if (flbuf.pfmtf->FontMetrics.fsDefn & FM_DEFN_OUTLINE)
1182 // is outline font:
1183 gpihSetPointSize(hps, flbuf.lPointSize);
1184 lPointSizeLast = flbuf.lPointSize;
1185 }
1186
1187 if (pWord = CreateWord(hps,
1188 &pCurrent, // advanced to next word
1189 &flbuf))
1190 {
1191 lstAppendItem(&pxfd->llWords, pWord);
1192
1193 /* {
1194 CHAR szWord[3000];
1195 strhncpy0(szWord, pWord->pStart, min(pWord->cChars, sizeof(szWord)));
1196 _Pmpf(("Found word '%s'", szWord));
1197 } */
1198
1199 cWords++;
1200
1201 while (*pCurrent == TXVESC_CHAR) // '\xFF')
1202 {
1203 // handle escapes;
1204 // this advances pCurrent depending on the
1205 // escape sequence length and might append
1206 // another "word" for the escape sequence
1207 // if it's relevant for rectangle correlation
1208 ProcessEscapes(&pCurrent,
1209 pxfd,
1210 &flbuf,
1211 FALSE); // fWordsProcessed
1212 }
1213 }
1214 else
1215 break;
1216 }
1217 } // end if (fFullRecalc)
1218 else
1219 cWords = lstCountItems(&pxfd->llWords);
1220
1221 /*
1222 * step 2: create rectangles
1223 *
1224 */
1225
1226 if (cWords)
1227 {
1228 PLISTNODE pWordNode = lstQueryFirstNode(&pxfd->llWords);
1229
1230 LONG lCurrentYTop = prclView->yTop,
1231 lOrigYTop = lCurrentYTop;
1232
1233 BOOL fRects2Go = TRUE;
1234
1235 // space before paragraph; this is reset
1236 // to 0 if we start a new rectangle for
1237 // the same paragraph
1238 ULONG ulYPre = flbuf.fmtp.lSpaceBefore;
1239
1240 // rectangles loop
1241 while (fRects2Go)
1242 {
1243 BOOL fWords2Go = TRUE;
1244 ULONG ulWordsInThisRect = 0;
1245
1246 // maximum height of words in this rect
1247 ULONG lWordsMaxCY = 0;
1248
1249 // start a new rectangle:
1250 PTXVRECTANGLE pRect = (PTXVRECTANGLE)malloc(sizeof(TXVRECTANGLE));
1251 lstInit(&pRect->llWords,
1252 FALSE); // no auto-free; the words are stored in the main
1253 // list also, which is freed
1254 // rectangle's xLeft;
1255 // xRight will be set when we're done with this rectangle
1256 pRect->rcl.xLeft = prclView->xLeft + flbuf.fmtp.lLeftMargin;
1257 if (ulYPre)
1258 // starting new paragraph:
1259 // add first-line offset also
1260 pRect->rcl.xLeft += flbuf.fmtp.lFirstLineMargin;
1261
1262 // current X pos: start with left of rectangle
1263 flbuf.lXCurrent = pRect->rcl.xLeft;
1264
1265 // max baseline ofs: set to 0, this will be raised
1266 pRect->ulMaxBaseLineOfs = 0;
1267
1268 // words-per-rectangle loop;
1269 // we keep adding words to the rectangle until
1270 // a) words no longer fit and word-wrapping is on;
1271 // b) a newline or line feed is found;
1272 // c) the last word has been reached;
1273 while (fWords2Go)
1274 {
1275 PTXVWORD pWordThis = (PTXVWORD)pWordNode->pItemData;
1276/*
1277 #define TXVWORDF_GLUEWITHNEXT 1 // escape
1278 #define TXVWORDF_LINEBREAK 2 // \n
1279 #define TXVWORDF_LINEFEED 4 // \r
1280*/
1281 BOOL fNextWord = FALSE;
1282
1283 if (pWordThis->cEscapeCode)
1284 {
1285 // pseudo-word for escape sequence:
1286 // process...
1287 ProcessEscapes((PSZ*)&pWordThis->pStart,
1288 pxfd,
1289 &flbuf,
1290 TRUE);
1291
1292 // append this sequence only if it's needed
1293 // for painting (list markers etc.)
1294 if (pWordThis->fPaintEscapeWord)
1295 {
1296 pWordThis->lX = flbuf.lXCurrent;
1297 pWordThis->pRectangle = pRect;
1298 lstAppendItem(&pRect->llWords, pWordThis);
1299 ulWordsInThisRect++;
1300 }
1301
1302 fNextWord = TRUE;
1303 }
1304 else
1305 {
1306 BOOL fWordWrapped = FALSE;
1307
1308 // not escape sequence, but real word: format...
1309 // is word-wrapping on?
1310 if (flbuf.fmtp.fWordWrap)
1311 {
1312 // yes: check if the word still fits
1313 // WarpIN V1.0.18 @@todo add fudge factor of 2 - makes things work
1314 if ( (flbuf.lXCurrent + pWordThis->ulCXWithSpaces + 2
1315 > prclView->xRight)
1316 // > ulWinCX)
1317 // but always add the first word in the rectangle,
1318 // because otherwise we get infinite loops
1319 && (ulWordsInThisRect > 0)
1320 )
1321 // no:
1322 fWordWrapped = TRUE;
1323 }
1324
1325 if (fWordWrapped)
1326 // start a new rectangle with the current word:
1327 fWords2Go = FALSE;
1328 // and do _not_ advance to the next word,
1329 // but start with this word for the next
1330 // rectangle...
1331 else
1332 {
1333 // add this word to the rectangle:
1334
1335 // store current X pos in word
1336 pWordThis->lX = flbuf.lXCurrent;
1337
1338 // increase current X pos by word width
1339 flbuf.lXCurrent += pWordThis->ulCXWithSpaces;
1340
1341 // store word in rectangle
1342 pWordThis->pRectangle = pRect;
1343 lstAppendItem(&pRect->llWords, pWordThis);
1344 // @@todo memory leak right here!!!
1345 ulWordsInThisRect++;
1346
1347 // store highest word width found for this rect
1348 if (pWordThis->ulCY > lWordsMaxCY)
1349 lWordsMaxCY = pWordThis->ulCY;
1350
1351 // store highest base line ofs found for this rect
1352 if (pWordThis->ulBaseLineOfs > pRect->ulMaxBaseLineOfs)
1353 pRect->ulMaxBaseLineOfs = pWordThis->ulBaseLineOfs;
1354
1355 // go for next word in any case
1356 fNextWord = TRUE;
1357 } // end if (!fBreakThisWord)
1358
1359 // now check: add more words to this rectangle?
1360 if ( (pWordThis->ulFlags == TXVWORDF_LINEBREAK)
1361 // no if linebreak found
1362 || (pWordThis->ulFlags == TXVWORDF_LINEFEED)
1363 // no if linefeed found
1364 || (!fWords2Go)
1365 // no if we're out of words or
1366 // word-break was forced
1367 )
1368 {
1369 // no: finish up this rectangle...
1370
1371 // xLeft has been set on top
1372 pRect->rcl.xRight = flbuf.lXCurrent;
1373 pRect->rcl.yTop = lCurrentYTop - ulYPre;
1374 pRect->rcl.yBottom = pRect->rcl.yTop - lWordsMaxCY;
1375
1376 // decrease current y top for next line
1377 lCurrentYTop = pRect->rcl.yBottom;
1378 if (!fRects2Go)
1379 // we're done completely:
1380 // add another one
1381 lCurrentYTop -= lWordsMaxCY;
1382
1383 if (fWordWrapped)
1384 // starting with wrapped word in next line:
1385 ulYPre = 0;
1386 else
1387 if (pWordThis->ulFlags == TXVWORDF_LINEFEED)
1388 ulYPre = 0;
1389 else if (pWordThis->ulFlags == TXVWORDF_LINEBREAK)
1390 {
1391 // line break:
1392 // set y-pre for next loop
1393 ulYPre = flbuf.fmtp.lSpaceBefore;
1394 // and add paragraph post-y
1395 lCurrentYTop -= flbuf.fmtp.lSpaceAfter;
1396 }
1397
1398 // update x extents
1399 if (pRect->rcl.xRight > pxfd->szlWorkspace.cx)
1400 pxfd->szlWorkspace.cx = pRect->rcl.xRight;
1401
1402 // and quit the inner loop
1403 fWords2Go = FALSE;
1404 } // end finish up rectangle
1405 } // end else if (pWordThis->fIsEscapeSequence)
1406
1407 if (fNextWord)
1408 {
1409 pWordNode = pWordNode->pNext;
1410 if (!pWordNode)
1411 {
1412 // no more to go:
1413 // quit
1414 fWords2Go = FALSE;
1415 fRects2Go = FALSE;
1416 }
1417 }
1418 } // end while (fWords2Go)
1419
1420 // store rectangle
1421 lstAppendItem(&pxfd->llRectangles, pRect);
1422 }
1423
1424 // lCurrentYTop now has the bottommost point we've used;
1425 // store this as workspace (this might be negative)
1426 pxfd->szlWorkspace.cy = lOrigYTop - lCurrentYTop;
1427 }
1428 }
1429 }
1430}
1431
1432/* ******************************************************************
1433 *
1434 * Device-independent text painting
1435 *
1436 ********************************************************************/
1437
1438/*
1439 *@@ DrawListMarker:
1440 *
1441 *@@added V0.9.3 (2000-05-17) [umoeller]
1442 */
1443
1444STATIC VOID DrawListMarker(HPS hps,
1445 PRECTL prclLine, // current line rectangle
1446 PTXVWORD pWordThis, // current word
1447 LONG lViewXOfs) // in: x offset to paint; 0 means rightmost
1448{
1449 POINTL ptl;
1450
1451 ULONG ulBulletSize = pWordThis->lPointSize * 2 / 3; // 2/3 of point size
1452
1453 ARCPARAMS arcp = {1, 1, 0, 0};
1454
1455 // pWordThis->pStart points to the \xFF character;
1456 // next is the "marker" escape (\x23),
1457 // next is the marker type
1458 CHAR cBulletType = *((pWordThis->pStart) + 2) ;
1459
1460 switch (cBulletType)
1461 {
1462 case 2: // square (filled box)
1463 ptl.x = pWordThis->lX - lViewXOfs;
1464 // center bullet vertically
1465 ptl.y = prclLine->yBottom
1466 + ( (prclLine->yTop - prclLine->yBottom) // height
1467 - ulBulletSize
1468 ) / 2;
1469
1470 GpiMove(hps, &ptl);
1471 ptl.x += ulBulletSize;
1472 ptl.y += ulBulletSize;
1473 GpiBox(hps, DRO_FILL, &ptl, 0, 0);
1474 break;
1475
1476 default: // case 1: // disc (filled circle)
1477 ptl.x = pWordThis->lX - lViewXOfs;
1478 // center bullet vertically;
1479 // the arc is drawn with the current position in its center
1480 ptl.y = prclLine->yBottom
1481 + ( (prclLine->yTop - prclLine->yBottom) // height
1482 / 2
1483 );
1484
1485 GpiSetArcParams(hps, &arcp);
1486 GpiMove(hps, &ptl);
1487 GpiFullArc(hps,
1488 (cBulletType == 3)
1489 ? DRO_OUTLINE
1490 : DRO_FILL,
1491 MAKEFIXED(ulBulletSize / 2, // radius!
1492 0));
1493 break;
1494
1495 }
1496}
1497
1498/*
1499 *@@ txvPaintText:
1500 * device-independent function for painting.
1501 * This can only be called after the text has
1502 * been formatted (using txvFormatText).
1503 *
1504 * This only paints rectangles which are within
1505 * prcl2Paint.
1506 *
1507 * -- For WM_PAINT, set this to the
1508 * update rectangle, and set fPaintHalfLines
1509 * to TRUE.
1510 *
1511 * -- For printing, set this to the page rectangle,
1512 * and set fPaintHalfLines to FALSE.
1513 *
1514 * All coordinates are in world space (PU_PELS).
1515 *
1516 *@@changed V0.9.3 (2000-05-05) [umoeller]: fixed wrong visible lines calculations; great speedup painting!
1517 *@@changed V0.9.3 (2000-05-06) [umoeller]: now using gpihCharStringPosAt
1518 */
1519
1520BOOL txvPaintText(HAB hab,
1521 HPS hps, // in: window or printer PS
1522 PXFORMATDATA pxfd,
1523 PRECTL prcl2Paint, // in: invalid rectangle to be drawn,
1524 // can be NULL to paint all
1525 LONG lViewXOfs, // in: x offset to paint; 0 means rightmost
1526 PULONG pulViewYOfs, // in: y offset to paint; 0 means _top_most;
1527 // out: y offset which should be passed to next call
1528 // (if TRUE is returned and fPaintHalfLines == FALSE)
1529 BOOL fPaintHalfLines, // in: if FALSE, lines which do not fully fit on
1530 // the page are dropped (useful for printing)
1531 PULONG pulLineIndex) // in: line to start painting with;
1532 // out: next line to paint, if any
1533 // (if TRUE is returned and fPaintHalfLines == FALSE)
1534{
1535 BOOL brc = FALSE,
1536 fAnyLinesPainted = FALSE;
1537 ULONG ulCurrentLineIndex = *pulLineIndex;
1538 // LONG lViewYOfsSaved = *pulViewYOfs;
1539 PLISTNODE pRectNode = lstNodeFromIndex(&pxfd->llRectangles,
1540 ulCurrentLineIndex);
1541
1542 LONG lcidLast = -99;
1543 LONG lPointSizeLast = -99;
1544
1545 while (pRectNode)
1546 {
1547 PTXVRECTANGLE pLineRcl = (PTXVRECTANGLE)pRectNode->pItemData;
1548 BOOL fPaintThis = FALSE;
1549
1550 // compose rectangle to draw for this line
1551 RECTL rclLine;
1552 rclLine.xLeft = pLineRcl->rcl.xLeft - lViewXOfs;
1553 rclLine.xRight = pLineRcl->rcl.xRight - lViewXOfs;
1554 rclLine.yBottom = pLineRcl->rcl.yBottom + *pulViewYOfs;
1555 rclLine.yTop = pLineRcl->rcl.yTop + *pulViewYOfs;
1556
1557 /* if (pmpf)
1558 {
1559 CHAR szTemp[100];
1560 ULONG cb = min(pLineRcl->cLineChars, 99);
1561 strhncpy0(szTemp, pLineRcl->pStartOfLine, cb);
1562
1563 _Pmpf(("Checking line %d: '%s'",
1564 ulCurrentLineIndex,
1565 szTemp));
1566
1567 _Pmpf((" (yB stored %d -> in HPS %d against win yB %d)",
1568 pLineRcl->rcl.yBottom,
1569 rclLine.yBottom,
1570 prcl2Paint->yBottom));
1571 } */
1572
1573 if (prcl2Paint == NULL)
1574 // draw all:
1575 fPaintThis = TRUE;
1576 else
1577 {
1578 BOOL fBottomInPaint = ( (rclLine.yBottom >= prcl2Paint->yBottom)
1579 && (rclLine.yBottom <= prcl2Paint->yTop)
1580 );
1581 BOOL fTopInPaint = ( (rclLine.yTop >= prcl2Paint->yBottom)
1582 && (rclLine.yTop <= prcl2Paint->yTop)
1583 );
1584
1585 if ((fBottomInPaint) && (fTopInPaint))
1586 // both in update rect:
1587 fPaintThis = TRUE;
1588 else
1589 if (fPaintHalfLines)
1590 {
1591 if ((fBottomInPaint) || (fTopInPaint))
1592 // only one in update rect:
1593 fPaintThis = TRUE;
1594 else
1595 // now, for very small update rectangles,
1596 // especially with slow scrolling,
1597 // we can have the case that the paint rectangle
1598 // is only a few pixels high so that the top of
1599 // the line is above the repaint, and the bottom
1600 // of the line is below it!
1601 if ( (rclLine.yTop >= prcl2Paint->yTop)
1602 && (rclLine.yBottom <= prcl2Paint->yBottom)
1603 )
1604 fPaintThis = TRUE;
1605 }
1606 }
1607
1608 if (fPaintThis)
1609 {
1610 // rectangle invalid: paint this rectangle
1611 // by going thru the member words
1612 PLISTNODE pWordNode = lstQueryFirstNode(&pLineRcl->llWords);
1613
1614 POINTL ptlStart;
1615
1616 while (pWordNode)
1617 {
1618 PTXVWORD pWordThis = (PTXVWORD)pWordNode->pItemData;
1619 ULONG flChar = pWordThis->flChar;
1620
1621 if (pWordThis->pcszLinkTarget) // V0.9.20 (2002-08-10) [umoeller]
1622 flChar |= CHS_UNDERSCORE;
1623
1624 // x start: this word's X coordinate
1625 ptlStart.x = pWordThis->lX - lViewXOfs;
1626 // y start: bottom line of rectangle plus highest
1627 // base line offset found in all words (format step 2)
1628 ptlStart.y = rclLine.yBottom + pLineRcl->ulMaxBaseLineOfs;
1629 // pWordThis->ulBaseLineOfs;
1630
1631 // set font for subsequent calculations,
1632 // if changed (this includes the first call)
1633 if (lcidLast != pWordThis->lcid)
1634 {
1635 GpiSetCharSet(hps, pWordThis->lcid);
1636 lcidLast = pWordThis->lcid;
1637 // force recalc of point size
1638 lPointSizeLast = -99;
1639 }
1640
1641 if (lPointSizeLast != pWordThis->lPointSize)
1642 {
1643 if (pWordThis->lPointSize)
1644 // is outline font:
1645 gpihSetPointSize(hps, pWordThis->lPointSize);
1646 lPointSizeLast = pWordThis->lPointSize;
1647 }
1648
1649 if (!pWordThis->cEscapeCode)
1650 // regular word:
1651 gpihCharStringPosAt(hps,
1652 &ptlStart,
1653 &rclLine,
1654 flChar,
1655 pWordThis->cChars,
1656 (PSZ)pWordThis->pStart);
1657 else
1658 {
1659 // check escape code
1660 switch (pWordThis->cEscapeCode)
1661 {
1662 case 0x23:
1663 // escape to be painted:
1664 DrawListMarker(hps,
1665 &rclLine,
1666 pWordThis,
1667 lViewXOfs);
1668 break;
1669 }
1670 }
1671
1672 // ptlStart.x += pWordThis->ulCXWithSpaces;
1673
1674 fAnyLinesPainted = TRUE;
1675 pWordNode = pWordNode->pNext;
1676 }
1677
1678 /* {
1679 LONG lColor = GpiQueryColor(hps);
1680 POINTL ptl2;
1681 GpiSetColor(hps, RGBCOL_RED);
1682 ptl2.x = rclLine.xLeft;
1683 ptl2.y = rclLine.yBottom;
1684 GpiMove(hps, &ptl2);
1685 ptl2.x = rclLine.xRight;
1686 ptl2.y = rclLine.yTop;
1687 GpiBox(hps,
1688 DRO_OUTLINE,
1689 &ptl2,
1690 0, 0);
1691 GpiSetColor(hps, lColor);
1692 } */
1693
1694 }
1695 else
1696 {
1697 // this line is no longer fully visible:
1698
1699 if (fAnyLinesPainted)
1700 {
1701 // we had painted lines already:
1702 // this means that all the following lines are
1703 // too far below the window, so quit
1704 /* if (pmpf)
1705 _Pmpf(("Quitting with line %d (xL = %d yB = %d)",
1706 ulCurrentLineIndex, rclLine.xLeft, rclLine.yBottom)); */
1707
1708 *pulLineIndex = ulCurrentLineIndex;
1709 if (pRectNode->pNext)
1710 {
1711 // another line to paint:
1712 PTXVRECTANGLE pLineRcl2 = (PTXVRECTANGLE)pRectNode->pNext->pItemData;
1713 // return TRUE
1714 brc = TRUE;
1715 // and set *pulViewYOfs to the top of
1716 // the next line, which wasn't visible
1717 // on the page any more
1718 *pulViewYOfs = pLineRcl2->rcl.yTop + *pulViewYOfs;
1719 }
1720 break;
1721 }
1722 // else no lines painted yet:
1723 // go for next node, because we're still above the visible window
1724 }
1725
1726 // next line
1727 pRectNode = pRectNode->pNext;
1728 // raise index to return
1729 ulCurrentLineIndex++;
1730 }
1731
1732 if (!fAnyLinesPainted)
1733 brc = FALSE;
1734
1735 return brc;
1736}
1737
1738/*
1739 *@@ txvFindWordFromPoint:
1740 * returns the list node of the word under the
1741 * given point. The list node is from the global
1742 * words list in pxfd.
1743 *
1744 *@@added V0.9.3 (2000-05-18) [umoeller]
1745 */
1746
1747PLISTNODE txvFindWordFromPoint(PXFORMATDATA pxfd,
1748 PPOINTL pptl)
1749{
1750 PLISTNODE pWordNodeFound = NULL;
1751
1752 PLISTNODE pRectangleNode = lstQueryFirstNode(&pxfd->llRectangles);
1753 while ((pRectangleNode) && (!pWordNodeFound))
1754 {
1755 PTXVRECTANGLE prclThis = (PTXVRECTANGLE)pRectangleNode->pItemData;
1756 if ( (pptl->x >= prclThis->rcl.xLeft)
1757 && (pptl->x <= prclThis->rcl.xRight)
1758 && (pptl->y >= prclThis->rcl.yBottom)
1759 && (pptl->y <= prclThis->rcl.yTop)
1760 )
1761 {
1762 // cool, we found the rectangle:
1763 // now go thru the words in this rectangle
1764 PLISTNODE pWordNode = lstQueryFirstNode(&prclThis->llWords);
1765 while (pWordNode)
1766 {
1767 PTXVWORD pWordThis = (PTXVWORD)pWordNode->pItemData;
1768
1769 if ( (pptl->x >= pWordThis->lX)
1770 && (pptl->x <= pWordThis->lX + pWordThis->ulCXWithSpaces)
1771 )
1772 {
1773 pWordNodeFound = pWordNode;
1774 break;
1775 }
1776 pWordNode = pWordNode->pNext;
1777 }
1778 }
1779 pRectangleNode = pRectangleNode->pNext;
1780 }
1781
1782 return pWordNodeFound;
1783}
1784
1785/*
1786 *@@ txvFindWordFromAnchor:
1787 * returns the list node from the global words list
1788 * BEFORE the word which represents the escape sequence
1789 * containing the specified anchor name.
1790 *
1791 *@@added V0.9.4 (2000-06-12) [umoeller]
1792 */
1793
1794PLISTNODE txvFindWordFromAnchor(PXFORMATDATA pxfd,
1795 const char *pszAnchorName)
1796{
1797 PLISTNODE pNodeFound = NULL;
1798
1799 ULONG cbAnchorName = strlen(pszAnchorName);
1800
1801 PLISTNODE pWordNode = lstQueryFirstNode(&pxfd->llWords);
1802 while ((pWordNode) && (!pNodeFound))
1803 {
1804 PTXVWORD pWordThis = (PTXVWORD)pWordNode->pItemData;
1805 if (pWordThis->cEscapeCode == 7)
1806 {
1807 // this word is an anchor escape sequence:
1808 if (strnicmp(pszAnchorName, (pWordThis->pStart + 2), cbAnchorName) == 0)
1809 {
1810 // matches: check length
1811 if (*(pWordThis->pStart + 2 + cbAnchorName) == (char)0xFF)
1812 // OK:
1813 pNodeFound = pWordNode;
1814 }
1815 }
1816
1817 pWordNode = pWordNode ->pNext;
1818 }
1819
1820 if (pNodeFound)
1821 {
1822 // anchor found:
1823 // go backwords in word list until we find a "real" word
1824 // which is no escape sequence
1825 while (pNodeFound)
1826 {
1827 PTXVWORD pWordThis = (PTXVWORD)pNodeFound->pItemData;
1828 if (pWordThis->cEscapeCode)
1829 pNodeFound = pNodeFound->pPrevious;
1830 else
1831 break;
1832 }
1833 }
1834
1835 return pNodeFound;
1836}
1837
1838/* ******************************************************************
1839 *
1840 * Window-dependent functions
1841 *
1842 ********************************************************************/
1843
1844#define QWL_PRIVATE 4 // V0.9.20 (2002-08-10) [umoeller]
1845
1846/*
1847 *@@ TEXTVIEWWINDATA:
1848 * view control-internal structure, stored in
1849 * QWL_PRIVATE at fnwpTextView.
1850 * This is device-dependent on the text view
1851 * window.
1852 */
1853
1854typedef struct _TEXTVIEWWINDATA
1855{
1856 HAB hab; // anchor block (for speed)
1857
1858 HDC hdc;
1859 HPS hps;
1860
1861 ULONG flStyle; // window style flags copied on WM_CREATE
1862 // V0.9.20 (2002-08-10) [umoeller]
1863
1864 LONG lBackColor,
1865 lForeColor;
1866
1867 XTEXTVIEWCDATA cdata; // control data, as passed to WM_CREATE
1868
1869 XFORMATDATA xfd;
1870
1871 HWND hwndVScroll, // vertical scroll bar
1872 hwndHScroll; // horizontal scroll bar
1873
1874 BOOL fVScrollVisible, // TRUE if vscroll is currently used
1875 fHScrollVisible; // TRUE if hscroll is currently used
1876
1877 RECTL rclViewReal, // window rect as returned by WinQueryWindowRect
1878 // (top right point is inclusive!)
1879 rclViewPaint, // same as rclViewReal, but excluding scroll bars
1880 rclViewText; // same as rclViewPaint, but excluding cdata borders
1881
1882 ULONG ulViewXOfs, // pixels that we have scrolled to the RIGHT; 0 means very left
1883 ulViewYOfs; // pixels that we have scrolled to the BOTTOM; 0 means very top
1884
1885 BOOL fAcceptsPresParamsNow; // TRUE after first WM_PAINT
1886
1887 // anchor clicking
1888 PLISTNODE pWordNodeFirstInAnchor; // points to first word which belongs to anchor
1889 // USHORT usLastAnchorClicked; // last anchor which was clicked (1-0xFFFF)
1890 PCSZ pcszLastLinkClicked; // last link that was clicked (points into llLinks)
1891 // V0.9.20 (2002-08-10) [umoeller]
1892
1893} TEXTVIEWWINDATA, *PTEXTVIEWWINDATA;
1894
1895#define ID_VSCROLL 100
1896#define ID_HSCROLL 101
1897
1898/*
1899 *@@ UpdateTextViewPresData:
1900 * called from WM_CREATE and WM_PRESPARAMCHANGED
1901 * in fnwpTextView to update the TEXTVIEWWINDATA
1902 * from the window's presparams. This calls
1903 * txvSetDefaultFormat in turn.
1904 */
1905
1906STATIC VOID UpdateTextViewPresData(HWND hwndTextView,
1907 PTEXTVIEWWINDATA ptxvd)
1908{
1909 PSZ pszFont;
1910 ptxvd->lBackColor = winhQueryPresColor(hwndTextView,
1911 PP_BACKGROUNDCOLOR,
1912 FALSE, // no inherit
1913 SYSCLR_DIALOGBACKGROUND);
1914 ptxvd->lForeColor = winhQueryPresColor(hwndTextView,
1915 PP_FOREGROUNDCOLOR,
1916 FALSE, // no inherit
1917 SYSCLR_WINDOWSTATICTEXT);
1918
1919 if ((pszFont = winhQueryWindowFont(hwndTextView)))
1920 {
1921 ULONG ulSize;
1922 PSZ pszFaceName;
1923 // _Pmpf(("font: %s", pszFont));
1924 if (gpihSplitPresFont(pszFont,
1925 &ulSize,
1926 &pszFaceName))
1927 {
1928 SetFormatFont(ptxvd->hps,
1929 &ptxvd->xfd.fmtcStandard,
1930 ulSize,
1931 pszFaceName);
1932 }
1933 free(pszFont);
1934 }
1935}
1936
1937/*
1938 *@@ AdjustViewRects:
1939 * updates the internal size-dependent structures
1940 * and positions the scroll bars.
1941 *
1942 * This is device-dependent for the text view
1943 * control and must be called before FormatText2Screen
1944 * so that the view rectangles get calculated right.
1945 *
1946 * Required input in TEXTVIEWWINDATA:
1947 *
1948 * -- rclViewReal: the actual window dimensions.
1949 *
1950 * -- cdata: control data.
1951 *
1952 * Output from this function in TEXTVIEWWINDATA:
1953 *
1954 * -- rclViewPaint: the paint subrectangle (which
1955 * is rclViewReal minus scrollbars, if any).
1956 *
1957 * -- rclViewText: the text subrectangle (which
1958 * is rclViewPaint minus borders).
1959 *
1960 *@@changed WarpIN V1.0.18 (2008-11-16) [pr]: fix cut/paste/typo. errors @@fixes 1086
1961 */
1962
1963STATIC VOID AdjustViewRects(HWND hwndTextView,
1964 PTEXTVIEWWINDATA ptxvd)
1965{
1966 ULONG ulScrollCX = WinQuerySysValue(HWND_DESKTOP, SV_CXVSCROLL),
1967 ulScrollCY = WinQuerySysValue(HWND_DESKTOP, SV_CYHSCROLL),
1968 ulOfs;
1969
1970 // calculate rclViewPaint:
1971 // 1) left
1972 ptxvd->rclViewPaint.xLeft = ptxvd->rclViewReal.xLeft;
1973 // 2) bottom
1974 ptxvd->rclViewPaint.yBottom = ptxvd->rclViewReal.yBottom;
1975 if (ptxvd->fHScrollVisible)
1976 // if we have a horizontal scroll bar at the bottom,
1977 // raise bottom by its height
1978 ptxvd->rclViewPaint.yBottom += ulScrollCY;
1979 // 3) right
1980 ptxvd->rclViewPaint.xRight = ptxvd->rclViewReal.xRight;
1981 if (ptxvd->fVScrollVisible)
1982 // if we have a vertical scroll bar at the right,
1983 // subtract its width from the right
1984 ptxvd->rclViewPaint.xRight -= ulScrollCX;
1985 ptxvd->rclViewPaint.yTop = ptxvd->rclViewReal.yTop;
1986
1987 // calculate rclViewText from that
1988 ptxvd->rclViewText.xLeft = ptxvd->rclViewPaint.xLeft + ptxvd->cdata.ulXBorder;
1989 ptxvd->rclViewText.yBottom = ptxvd->rclViewPaint.yBottom + ptxvd->cdata.ulYBorder;
1990 ptxvd->rclViewText.xRight = ptxvd->rclViewPaint.xRight - ptxvd->cdata.ulXBorder;
1991 ptxvd->rclViewText.yTop = ptxvd->rclViewPaint.yTop - ptxvd->cdata.ulYBorder; // WarpIN V1.0.18
1992
1993 // now reposition scroll bars; their sizes may change
1994 // if either the vertical or horizontal scroll bar has
1995 // popped up or been hidden
1996 if (ptxvd->flStyle & XS_VSCROLL)
1997 {
1998 // vertical scroll bar enabled:
1999 ulOfs = 0;
2000 if (ptxvd->fHScrollVisible)
2001 ulOfs = ulScrollCY; // WarpIN V1.0.18
2002 WinSetWindowPos(ptxvd->hwndVScroll,
2003 HWND_TOP,
2004 ptxvd->rclViewReal.xRight - ulScrollCX,
2005 ulOfs, // y
2006 ulScrollCX, // cx
2007 ptxvd->rclViewReal.yTop - ulOfs, // cy
2008 SWP_MOVE | SWP_SIZE);
2009 }
2010
2011 if (ptxvd->flStyle & XS_HSCROLL)
2012 {
2013 ulOfs = 0;
2014 if (ptxvd->fVScrollVisible)
2015 ulOfs = ulScrollCX;
2016 WinSetWindowPos(ptxvd->hwndHScroll,
2017 HWND_TOP,
2018 0,
2019 0,
2020 ptxvd->rclViewReal.xRight - ulOfs, // cx
2021 ulScrollCY, // cy
2022 SWP_MOVE | SWP_SIZE);
2023 }
2024}
2025
2026/*
2027 *@@ FormatText2Screen:
2028 * device-dependent version of text formatting
2029 * for the text view window. This calls txvFormatText
2030 * in turn and updates the view's scroll bars.
2031 *
2032 *@@changed V0.9.3 (2000-05-05) [umoeller]: fixed buggy vertical scroll bars
2033 *@@changed WarpIN V1.0.18 (2008-11-16) [pr]: fix buggy horiz. scroll bars @@fixes 1086
2034 */
2035
2036STATIC VOID FormatText2Screen(HWND hwndTextView,
2037 PTEXTVIEWWINDATA ptxvd,
2038 BOOL fAlreadyRecursing, // in: set this to FALSE when calling
2039 BOOL fFullRecalc)
2040{
2041 ULONG ulWinCX,
2042 ulWinCY;
2043
2044 // call device-independent formatter with the
2045 // window presentation space
2046 txvFormatText(ptxvd->hps,
2047 &ptxvd->xfd,
2048 &ptxvd->rclViewText,
2049 fFullRecalc);
2050
2051 ulWinCY = (ptxvd->rclViewText.yTop - ptxvd->rclViewText.yBottom);
2052
2053 if (ptxvd->ulViewYOfs < 0)
2054 ptxvd->ulViewYOfs = 0;
2055 if (ptxvd->ulViewYOfs > ((LONG)ptxvd->xfd.szlWorkspace.cy - ulWinCY))
2056 ptxvd->ulViewYOfs = (LONG)ptxvd->xfd.szlWorkspace.cy - ulWinCY;
2057
2058 // vertical scroll bar enabled at all?
2059 if (ptxvd->flStyle & XS_VSCROLL)
2060 {
2061 BOOL fEnabled = winhUpdateScrollBar(ptxvd->hwndVScroll,
2062 ulWinCY,
2063 ptxvd->xfd.szlWorkspace.cy,
2064 ptxvd->ulViewYOfs,
2065 (ptxvd->flStyle & XS_AUTOVHIDE));
2066 // is auto-hide on?
2067 if (ptxvd->flStyle & XS_AUTOVHIDE)
2068 {
2069 // yes, auto-hide on: did visibility change?
2070 if (fEnabled != ptxvd->fVScrollVisible)
2071 // visibility changed:
2072 // if we're not already recursing,
2073 // force calling ourselves again
2074 if (!fAlreadyRecursing)
2075 {
2076 ptxvd->fVScrollVisible = fEnabled;
2077 AdjustViewRects(hwndTextView,
2078 ptxvd);
2079 FormatText2Screen(hwndTextView,
2080 ptxvd,
2081 TRUE, // fAlreadyRecursing
2082 FALSE); // quick format
2083 }
2084 }
2085 }
2086
2087 ulWinCX = (ptxvd->rclViewText.xRight - ptxvd->rclViewText.xLeft);
2088
2089 // horizontal scroll bar enabled at all?
2090 if (ptxvd->flStyle & XS_HSCROLL)
2091 {
2092 BOOL fEnabled = winhUpdateScrollBar(ptxvd->hwndHScroll,
2093 ulWinCX,
2094 ptxvd->xfd.szlWorkspace.cx,
2095 ptxvd->ulViewXOfs,
2096 (ptxvd->flStyle & XS_AUTOHHIDE));
2097 // is auto-hide on?
2098 if (ptxvd->flStyle & XS_AUTOHHIDE)
2099 {
2100 // yes, auto-hide on: did visibility change?
2101 if (fEnabled != ptxvd->fHScrollVisible)
2102 // visibility changed:
2103 // if we're not already recursing,
2104 // force calling ourselves again (at the bottom)
2105 if (!fAlreadyRecursing)
2106 {
2107 ptxvd->fHScrollVisible = fEnabled;
2108 AdjustViewRects(hwndTextView,
2109 ptxvd);
2110 FormatText2Screen(hwndTextView, // WarpIN V1.0.18
2111 ptxvd,
2112 TRUE, // fAlreadyRecursing
2113 FALSE); // quick format
2114 }
2115 }
2116 }
2117
2118 WinInvalidateRect(hwndTextView, NULL, FALSE);
2119}
2120
2121/*
2122 *@@ SetWindowText:
2123 * implementation for WM_SETWINDOWPARAMS and
2124 * also WM_CREATE to set the window text.
2125 *
2126 *@@added V0.9.20 (2002-08-10) [umoeller]
2127 */
2128
2129VOID SetWindowText(HWND hwndTextView,
2130 PTEXTVIEWWINDATA ptxvd,
2131 PCSZ pcszText)
2132{
2133 if (pcszText && *pcszText)
2134 {
2135 PXSTRING pstr = &ptxvd->xfd.strViewText;
2136 PSZ p;
2137
2138 switch (ptxvd->flStyle & XS_FORMAT_MASK)
2139 {
2140 case XS_PLAINTEXT: // 0x0100
2141 xstrcpy(pstr,
2142 pcszText,
2143 0);
2144 xstrConvertLineFormat(pstr,
2145 CRLF2LF);
2146 p = pstr->psz;
2147 while (p = strchr(p, '\xFF'))
2148 *p = ' ';
2149 break;
2150
2151 case XS_HTML: // 0x0200
2152 if (p = strdup(pcszText))
2153 {
2154 PSZ p2 = p;
2155 while (p2 = strchr(p2, '\xFF'))
2156 *p2 = ' ';
2157 txvConvertFromHTML(&p, NULL, NULL, NULL);
2158 xstrset(pstr, p);
2159 xstrConvertLineFormat(pstr,
2160 CRLF2LF);
2161 }
2162 break;
2163
2164 default: // case XS_PREFORMATTED: // 0x0000
2165 // no conversion (default)
2166 xstrcpy(pstr,
2167 pcszText,
2168 0);
2169 break;
2170 }
2171
2172 // if the last character of the window text is not "\n",
2173 // add it explicitly here, or our lines processing
2174 // is being funny
2175 // V0.9.20 (2002-08-10) [umoeller]
2176 if (pstr->psz[pstr->ulLength - 1] != '\n')
2177 xstrcatc(pstr, '\n');
2178
2179 ptxvd->ulViewXOfs = 0;
2180 ptxvd->ulViewYOfs = 0;
2181 AdjustViewRects(hwndTextView,
2182 ptxvd);
2183 FormatText2Screen(hwndTextView,
2184 ptxvd,
2185 FALSE,
2186 TRUE); // full format
2187 }
2188}
2189
2190/*
2191 *@@ PaintViewText2Screen:
2192 * device-dependent version of text painting
2193 * for the text view window. This calls txvPaintText
2194 * in turn and updates the view's scroll bars.
2195 */
2196
2197STATIC VOID PaintViewText2Screen(PTEXTVIEWWINDATA ptxvd,
2198 PRECTL prcl2Paint) // in: invalid rectangle, can be NULL == paint all
2199{
2200 ULONG ulLineIndex = 0;
2201 ULONG ulYOfs = ptxvd->ulViewYOfs;
2202 txvPaintText(ptxvd->hab,
2203 ptxvd->hps, // paint PS: screen
2204 &ptxvd->xfd, // formatting data
2205 prcl2Paint, // update rectangle given to us
2206 ptxvd->ulViewXOfs, // current X scrolling offset
2207 &ulYOfs, // current Y scrolling offset
2208 TRUE, // draw even partly visible lines
2209 &ulLineIndex);
2210}
2211
2212/*
2213 *@@ PaintViewFocus:
2214 * paint a focus rectangle.
2215 */
2216
2217STATIC VOID PaintViewFocus(HPS hps,
2218 PTEXTVIEWWINDATA ptxvd,
2219 BOOL fFocus)
2220{
2221 POINTL ptl;
2222 HRGN hrgn;
2223 GpiSetClipRegion(hps,
2224 NULLHANDLE,
2225 &hrgn);
2226 GpiSetColor(hps,
2227 (fFocus)
2228 ? WinQuerySysColor(HWND_DESKTOP, SYSCLR_HILITEBACKGROUND, 0)
2229 : ptxvd->lBackColor);
2230 GpiSetLineType(hps, LINETYPE_DOT);
2231 ptl.x = ptxvd->rclViewPaint.xLeft;
2232 ptl.y = ptxvd->rclViewPaint.yBottom;
2233 GpiMove(hps, &ptl);
2234 ptl.x = ptxvd->rclViewPaint.xRight - 1;
2235 ptl.y = ptxvd->rclViewPaint.yTop - 1;
2236 GpiBox(hps,
2237 DRO_OUTLINE,
2238 &ptl,
2239 0, 0);
2240}
2241
2242/*
2243 *@@ RepaintWord:
2244 *
2245 *@@added V0.9.3 (2000-05-18) [umoeller]
2246 */
2247
2248STATIC VOID RepaintWord(PTEXTVIEWWINDATA ptxvd,
2249 PTXVWORD pWordThis,
2250 LONG lColor)
2251{
2252 POINTL ptlStart;
2253 ULONG flChar = pWordThis->flChar;
2254 PTXVRECTANGLE pLineRcl = pWordThis->pRectangle;
2255
2256 RECTL rclLine;
2257 rclLine.xLeft = pLineRcl->rcl.xLeft - ptxvd->ulViewXOfs;
2258 rclLine.xRight = pLineRcl->rcl.xRight - ptxvd->ulViewXOfs;
2259 rclLine.yBottom = pLineRcl->rcl.yBottom + ptxvd->ulViewYOfs;
2260 rclLine.yTop = pLineRcl->rcl.yTop + ptxvd->ulViewYOfs;
2261
2262 if (pWordThis->pcszLinkTarget)
2263 flChar |= CHS_UNDERSCORE;
2264
2265 // x start: this word's X coordinate
2266 ptlStart.x = pWordThis->lX - ptxvd->ulViewXOfs;
2267 // y start: bottom line of rectangle plus highest
2268 // base line offset found in all words (format step 2)
2269 ptlStart.y = rclLine.yBottom + pLineRcl->ulMaxBaseLineOfs;
2270 // pWordThis->ulBaseLineOfs;
2271
2272 GpiSetCharSet(ptxvd->hps, pWordThis->lcid);
2273 if (pWordThis->lPointSize)
2274 // is outline font:
2275 gpihSetPointSize(ptxvd->hps, pWordThis->lPointSize);
2276
2277 GpiSetColor(ptxvd->hps,
2278 lColor);
2279
2280 if (!pWordThis->cEscapeCode)
2281 {
2282 gpihCharStringPosAt(ptxvd->hps,
2283 &ptlStart,
2284 &rclLine,
2285 flChar,
2286 pWordThis->cChars,
2287 (PSZ)pWordThis->pStart);
2288 }
2289 else
2290 // escape to be painted:
2291 DrawListMarker(ptxvd->hps,
2292 &rclLine,
2293 pWordThis,
2294 ptxvd->ulViewXOfs);
2295}
2296
2297/*
2298 *@@ RepaintAnchor:
2299 *
2300 *@@added V0.9.3 (2000-05-18) [umoeller]
2301 */
2302
2303STATIC VOID RepaintAnchor(PTEXTVIEWWINDATA ptxvd,
2304 LONG lColor)
2305{
2306 PLISTNODE pNode = ptxvd->pWordNodeFirstInAnchor;
2307 PCSZ pcszLinkTarget = NULL;
2308 while (pNode)
2309 {
2310 PTXVWORD pWordThis = (PTXVWORD)pNode->pItemData;
2311 if (!pcszLinkTarget)
2312 // first loop:
2313 pcszLinkTarget = pWordThis->pcszLinkTarget;
2314 else
2315 if (pWordThis->pcszLinkTarget != pcszLinkTarget)
2316 // first word with different anchor:
2317 break;
2318
2319 RepaintWord(ptxvd,
2320 pWordThis,
2321 lColor);
2322 pNode = pNode->pNext;
2323 }
2324}
2325
2326/*
2327 *@@ ProcessCreate:
2328 * implementation for WM_CREATE in fnwpTextView.
2329 *
2330 *@@added V1.0.0 (2002-08-12) [umoeller]
2331 */
2332
2333STATIC MRESULT ProcessCreate(HWND hwndTextView, MPARAM mp1, MPARAM mp2)
2334{
2335 PXTEXTVIEWCDATA pcd = (PXTEXTVIEWCDATA)mp1;
2336 // can be NULL
2337 PCREATESTRUCT pcs = (PCREATESTRUCT)mp2;
2338 SBCDATA sbcd;
2339
2340 MRESULT mrc = (MRESULT)TRUE; // error
2341 PTEXTVIEWWINDATA ptxvd;
2342
2343 // allocate TEXTVIEWWINDATA for QWL_PRIVATE
2344 if (ptxvd = (PTEXTVIEWWINDATA)malloc(sizeof(TEXTVIEWWINDATA)))
2345 {
2346 SIZEL szlPage = {0, 0};
2347 BOOL fShow = FALSE;
2348
2349 // query message queue
2350 HMQ hmq = WinQueryWindowULong(hwndTextView, QWL_HMQ);
2351 // get codepage of message queue
2352 ULONG ulCodepage = WinQueryCp(hmq);
2353
2354 memset(ptxvd, 0, sizeof(TEXTVIEWWINDATA));
2355 WinSetWindowPtr(hwndTextView, QWL_PRIVATE, ptxvd);
2356
2357 ptxvd->hab = WinQueryAnchorBlock(hwndTextView);
2358
2359 ptxvd->hdc = WinOpenWindowDC(hwndTextView);
2360 ptxvd->hps = GpiCreatePS(ptxvd->hab,
2361 ptxvd->hdc,
2362 &szlPage, // use same page size as device
2363 PU_PELS | GPIT_MICRO | GPIA_ASSOC);
2364
2365 // copy window style flags V0.9.20 (2002-08-10) [umoeller]
2366 ptxvd->flStyle = pcs->flStyle;
2367
2368 gpihSwitchToRGB(ptxvd->hps);
2369
2370 // set codepage; GPI defaults this to
2371 // the process codepage
2372 GpiSetCp(ptxvd->hps, ulCodepage);
2373
2374 txvInitFormat(&ptxvd->xfd);
2375
2376 // copy control data, if present
2377 if (pcd)
2378 memcpy(&ptxvd->cdata, pcd, pcd->cbData);
2379
2380 // check values which might cause null divisions
2381 if (ptxvd->cdata.ulVScrollLineUnit == 0)
2382 ptxvd->cdata.ulVScrollLineUnit = 15;
2383 if (ptxvd->cdata.ulHScrollLineUnit == 0)
2384 ptxvd->cdata.ulHScrollLineUnit = 15;
2385
2386 ptxvd->fAcceptsPresParamsNow = FALSE;
2387
2388 // copy window dimensions from CREATESTRUCT
2389 ptxvd->rclViewReal.xLeft = 0;
2390 ptxvd->rclViewReal.yBottom = 0;
2391 ptxvd->rclViewReal.xRight = pcs->cx;
2392 ptxvd->rclViewReal.yTop = pcs->cy;
2393
2394 sbcd.cb = sizeof(SBCDATA);
2395 sbcd.sHilite = 0;
2396 sbcd.posFirst = 0;
2397 sbcd.posLast = 100;
2398 sbcd.posThumb = 30;
2399 sbcd.cVisible = 50;
2400 sbcd.cTotal = 50;
2401
2402 ptxvd->hwndVScroll = WinCreateWindow(hwndTextView,
2403 WC_SCROLLBAR,
2404 "",
2405 SBS_VERT | SBS_THUMBSIZE | WS_VISIBLE,
2406 10, 10,
2407 20, 100,
2408 hwndTextView, // owner
2409 HWND_TOP,
2410 ID_VSCROLL,
2411 &sbcd,
2412 0);
2413 fShow = ((ptxvd->flStyle & XS_VSCROLL) != 0);
2414 WinShowWindow(ptxvd->hwndVScroll, fShow);
2415 ptxvd->fVScrollVisible = fShow;
2416
2417 ptxvd->hwndHScroll = WinCreateWindow(hwndTextView,
2418 WC_SCROLLBAR,
2419 "",
2420 SBS_THUMBSIZE | WS_VISIBLE,
2421 10, 10,
2422 20, 100,
2423 hwndTextView, // owner
2424 HWND_TOP,
2425 ID_HSCROLL,
2426 &sbcd,
2427 0);
2428 fShow = ((ptxvd->flStyle & XS_HSCROLL) != 0);
2429 WinShowWindow(ptxvd->hwndHScroll, fShow);
2430 ptxvd->fHScrollVisible = fShow;
2431
2432 if (ptxvd->flStyle & XS_WORDWRAP)
2433 // word-wrapping should be enabled from the start:
2434 // V0.9.20 (2002-08-10) [umoeller]
2435 ptxvd->xfd.fmtpStandard.fWordWrap = TRUE;
2436
2437 // set "code" format
2438 SetFormatFont(ptxvd->hps,
2439 &ptxvd->xfd.fmtcCode,
2440 6,
2441 "System VIO");
2442
2443 // get colors from presparams/syscolors
2444 UpdateTextViewPresData(hwndTextView, ptxvd);
2445
2446 AdjustViewRects(hwndTextView,
2447 ptxvd);
2448
2449 if (ptxvd->flStyle & XS_HTML)
2450 {
2451 // if we're operating in HTML mode, set a
2452 // different default paragraph format to
2453 // make things prettier
2454 // V0.9.20 (2002-08-10) [umoeller]
2455 ptxvd->xfd.fmtpStandard.lSpaceBefore = 5;
2456 ptxvd->xfd.fmtpStandard.lSpaceAfter = 5;
2457 }
2458
2459 // setting the window text on window creation never
2460 // worked V0.9.20 (2002-08-10) [umoeller]
2461 if (pcs->pszText)
2462 SetWindowText(hwndTextView,
2463 ptxvd,
2464 pcs->pszText);
2465
2466 mrc = (MRESULT)FALSE; // OK
2467 }
2468
2469 return mrc;
2470}
2471
2472/*
2473 *@@ ProcessPaint:
2474 * implementation for WM_PAINT in fnwpTextView.
2475 *
2476 *@@added V1.0.0 (2002-08-12) [umoeller]
2477 */
2478
2479STATIC VOID ProcessPaint(HWND hwndTextView)
2480{
2481 PTEXTVIEWWINDATA ptxvd;
2482 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2483 {
2484 HRGN hrgnOldClip;
2485 RECTL rclClip;
2486 RECTL rcl2Update;
2487
2488 // get update rectangle
2489 WinQueryUpdateRect(hwndTextView,
2490 &rcl2Update);
2491 // since we're not using WinBeginPaint,
2492 // we must validate the update region,
2493 // or we'll get bombed with WM_PAINT msgs
2494 WinValidateRect(hwndTextView,
2495 NULL,
2496 FALSE);
2497
2498 // reset clip region to "all"
2499 GpiSetClipRegion(ptxvd->hps,
2500 NULLHANDLE,
2501 &hrgnOldClip); // out: old clip region
2502 // reduce clip region to update rectangle
2503 GpiIntersectClipRectangle(ptxvd->hps,
2504 &rcl2Update); // exclusive
2505
2506 // draw little box at the bottom right
2507 // (in between scroll bars) if we have
2508 // both vertical and horizontal scroll bars
2509 if ( (ptxvd->flStyle & (XS_VSCROLL | XS_HSCROLL))
2510 == (XS_VSCROLL | XS_HSCROLL)
2511 && (ptxvd->fVScrollVisible)
2512 && (ptxvd->fHScrollVisible)
2513 )
2514 {
2515 RECTL rclBox;
2516 rclBox.xLeft = ptxvd->rclViewPaint.xRight;
2517 rclBox.yBottom = 0;
2518 rclBox.xRight = rclBox.xLeft + WinQuerySysValue(HWND_DESKTOP, SV_CXVSCROLL);
2519 rclBox.yTop = WinQuerySysValue(HWND_DESKTOP, SV_CYHSCROLL);
2520 WinFillRect(ptxvd->hps,
2521 &rclBox,
2522 WinQuerySysColor(HWND_DESKTOP,
2523 SYSCLR_DIALOGBACKGROUND,
2524 0));
2525 }
2526
2527 // paint "view paint" rectangle white;
2528 // this can be larger than "view text"
2529 WinFillRect(ptxvd->hps,
2530 &ptxvd->rclViewPaint, // exclusive
2531 ptxvd->lBackColor);
2532
2533 // now reduce clipping rectangle to "view text" rectangle
2534 rclClip.xLeft = ptxvd->rclViewText.xLeft;
2535 rclClip.xRight = ptxvd->rclViewText.xRight - 1;
2536 rclClip.yBottom = ptxvd->rclViewText.yBottom;
2537 rclClip.yTop = ptxvd->rclViewText.yTop - 1;
2538 GpiIntersectClipRectangle(ptxvd->hps,
2539 &rclClip); // exclusive
2540 // finally, draw text lines in invalid rectangle;
2541 // this subfunction is smart enough to redraw only
2542 // the lines which intersect with rcl2Update
2543 GpiSetColor(ptxvd->hps, ptxvd->lForeColor);
2544 PaintViewText2Screen(ptxvd,
2545 &rcl2Update);
2546
2547 if ( (!(ptxvd->flStyle & XS_STATIC))
2548 // V0.9.20 (2002-08-10) [umoeller]
2549 && (WinQueryFocus(HWND_DESKTOP) == hwndTextView)
2550 )
2551 {
2552 // we have the focus:
2553 // reset clip region to "all"
2554 GpiSetClipRegion(ptxvd->hps,
2555 NULLHANDLE,
2556 &hrgnOldClip); // out: old clip region
2557 PaintViewFocus(ptxvd->hps,
2558 ptxvd,
2559 TRUE);
2560 }
2561
2562 ptxvd->fAcceptsPresParamsNow = TRUE;
2563 }
2564}
2565
2566/*
2567 *@@ ProcessPresParamChanged:
2568 * implementation for WM_PRESPARAMCHANGED in fnwpTextView.
2569 *
2570 *@@added V1.0.0 (2002-08-12) [umoeller]
2571 */
2572
2573STATIC VOID ProcessPresParamChanged(HWND hwndTextView, MPARAM mp1)
2574{
2575 PTEXTVIEWWINDATA ptxvd;
2576 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2577 {
2578 switch ((LONG)mp1)
2579 {
2580 case 0: // layout palette thing dropped
2581 case PP_BACKGROUNDCOLOR:
2582 case PP_FOREGROUNDCOLOR:
2583 case PP_FONTNAMESIZE:
2584 // re-query our presparams
2585 UpdateTextViewPresData(hwndTextView, ptxvd);
2586 }
2587
2588 if (ptxvd->fAcceptsPresParamsNow)
2589 FormatText2Screen(hwndTextView,
2590 ptxvd,
2591 FALSE,
2592 TRUE); // full reformat
2593 }
2594}
2595
2596/*
2597 *@@ ProcessSetFocus:
2598 * implementation for WM_SETFOCUS in fnwpTextView.
2599 *
2600 *@@added V1.0.0 (2002-08-12) [umoeller]
2601 */
2602
2603STATIC VOID ProcessSetFocus(HWND hwndTextView, MPARAM mp2)
2604{
2605 PTEXTVIEWWINDATA ptxvd;
2606 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2607 {
2608 if (ptxvd->flStyle & XS_STATIC)
2609 {
2610 if (mp2)
2611 {
2612 // we're receiving the focus, but shouldn't have it:
2613 // then behave like the static control does, that is,
2614 // give focus to the next window in the dialog
2615 HWND hwnd = hwndTextView,
2616 hwndStart = hwnd;
2617
2618 while (TRUE)
2619 {
2620 ULONG flStyle;
2621
2622 if (!(hwnd = WinQueryWindow(hwnd, QW_NEXT)))
2623 hwnd = WinQueryWindow(WinQueryWindow(hwndStart, QW_PARENT), QW_TOP);
2624
2625 // avoid endless looping
2626 if (hwnd == hwndStart)
2627 {
2628 if ( (hwnd = WinQueryWindow(hwnd, QW_OWNER))
2629 && (hwnd == hwndStart)
2630 )
2631 hwnd = NULLHANDLE;
2632
2633 break;
2634 }
2635
2636 if ( (flStyle = WinQueryWindowULong(hwnd, QWL_STYLE))
2637 && (flStyle & (WS_DISABLED | WS_TABSTOP | WS_VISIBLE)
2638 == (WS_TABSTOP | WS_VISIBLE))
2639 )
2640 {
2641 WinSetFocus(HWND_DESKTOP, hwnd);
2642 break;
2643 }
2644 };
2645 }
2646 }
2647 else
2648 {
2649 HPS hps = WinGetPS(hwndTextView);
2650 gpihSwitchToRGB(hps);
2651 PaintViewFocus(hps,
2652 ptxvd,
2653 (mp2 != 0));
2654 WinReleasePS(hps);
2655 }
2656 }
2657}
2658
2659/*
2660 *@@ ProcessButton1Down:
2661 * implementation for WM_BUTTON1DOWN in fnwpTextView.
2662 *
2663 *@@added V1.0.0 (2002-08-12) [umoeller]
2664 */
2665
2666STATIC MRESULT ProcessButton1Down(HWND hwndTextView, MPARAM mp1)
2667{
2668 MRESULT mrc = 0;
2669 PTEXTVIEWWINDATA ptxvd;
2670
2671 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2672 {
2673 POINTL ptlPos;
2674 PLISTNODE pWordNodeClicked;
2675
2676 ptlPos.x = SHORT1FROMMP(mp1) + ptxvd->ulViewXOfs;
2677 ptlPos.y = SHORT2FROMMP(mp1) - ptxvd->ulViewYOfs;
2678
2679 if ( (!(ptxvd->flStyle & XS_STATIC))
2680 // V0.9.20 (2002-08-10) [umoeller]
2681 && (hwndTextView != WinQueryFocus(HWND_DESKTOP))
2682 )
2683 WinSetFocus(HWND_DESKTOP, hwndTextView);
2684
2685 ptxvd->pcszLastLinkClicked = NULL;
2686
2687 if (pWordNodeClicked = txvFindWordFromPoint(&ptxvd->xfd,
2688 &ptlPos))
2689 {
2690 PTXVWORD pWordClicked = (PTXVWORD)pWordNodeClicked->pItemData;
2691
2692 // store link target (can be NULL)
2693 if (ptxvd->pcszLastLinkClicked = pWordClicked->pcszLinkTarget)
2694 {
2695 // word has a link target:
2696 PLISTNODE pNode = pWordNodeClicked;
2697
2698 // reset first word of anchor
2699 ptxvd->pWordNodeFirstInAnchor = NULL;
2700
2701 // go back to find the first word which has this anchor,
2702 // because we need to repaint them all
2703 while (pNode)
2704 {
2705 PTXVWORD pWordThis = (PTXVWORD)pNode->pItemData;
2706 if (pWordThis->pcszLinkTarget == pWordClicked->pcszLinkTarget)
2707 {
2708 // still has same anchor:
2709 // go for previous
2710 ptxvd->pWordNodeFirstInAnchor = pNode;
2711 pNode = pNode->pPrevious;
2712 }
2713 else
2714 // different anchor:
2715 // pNodeFirst points to first node with same anchor now
2716 break;
2717 }
2718
2719 RepaintAnchor(ptxvd,
2720 RGBCOL_RED);
2721 }
2722 }
2723
2724 WinSetCapture(HWND_DESKTOP, hwndTextView);
2725 mrc = (MRESULT)TRUE;
2726 }
2727
2728 return mrc;
2729}
2730
2731/*
2732 *@@ ProcessButton1Up:
2733 * implementation for WM_BUTTON1UP in fnwpTextView.
2734 *
2735 *@@added V1.0.0 (2002-08-12) [umoeller]
2736 */
2737
2738STATIC MRESULT ProcessButton1Up(HWND hwndTextView, MPARAM mp1)
2739{
2740 MRESULT mrc = 0;
2741 PTEXTVIEWWINDATA ptxvd;
2742
2743 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2744 {
2745 POINTL ptlPos;
2746 HWND hwndOwner = NULLHANDLE;
2747
2748 ptlPos.x = SHORT1FROMMP(mp1) + ptxvd->ulViewXOfs;
2749 ptlPos.y = SHORT2FROMMP(mp1) - ptxvd->ulViewYOfs;
2750 WinSetCapture(HWND_DESKTOP, NULLHANDLE);
2751
2752 if (ptxvd->pcszLastLinkClicked)
2753 {
2754 RepaintAnchor(ptxvd,
2755 ptxvd->lForeColor);
2756
2757 // nofify owner
2758 if (hwndOwner = WinQueryWindow(hwndTextView, QW_OWNER))
2759 WinPostMsg(hwndOwner,
2760 WM_CONTROL,
2761 MPFROM2SHORT(WinQueryWindowUShort(hwndTextView,
2762 QWS_ID),
2763 TXVN_LINK),
2764 (MPARAM)(ULONG)(ptxvd->pcszLastLinkClicked));
2765 }
2766
2767 mrc = (MRESULT)TRUE;
2768 }
2769
2770 return mrc;
2771}
2772
2773/*
2774 *@@ ProcessChar:
2775 * implementation for WM_CHAR in fnwpTextView.
2776 *
2777 *@@added V1.0.0 (2002-08-12) [umoeller]
2778 *@@changed WarpIN V1.0.18 (2008-11-16) [pr]: added correct ID for horiz. scroll @@fixes 1086
2779 */
2780
2781STATIC MRESULT ProcessChar(HWND hwndTextView, MPARAM mp1, MPARAM mp2)
2782{
2783 MRESULT mrc = 0;
2784 PTEXTVIEWWINDATA ptxvd;
2785
2786 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2787 {
2788 BOOL fDefProc = TRUE;
2789 USHORT usFlags = SHORT1FROMMP(mp1);
2790 // USHORT usch = SHORT1FROMMP(mp2);
2791 USHORT usvk = SHORT2FROMMP(mp2);
2792
2793 if (usFlags & KC_VIRTUALKEY)
2794 {
2795 ULONG ulMsg = 0;
2796 USHORT usID = ID_VSCROLL;
2797 SHORT sPos = 0;
2798 SHORT usCmd = 0;
2799 fDefProc = FALSE;
2800
2801 switch (usvk)
2802 {
2803 case VK_UP:
2804 ulMsg = WM_VSCROLL;
2805 usCmd = SB_LINEUP;
2806 break;
2807
2808 case VK_DOWN:
2809 ulMsg = WM_VSCROLL;
2810 usCmd = SB_LINEDOWN;
2811 break;
2812
2813 case VK_RIGHT:
2814 ulMsg = WM_HSCROLL;
2815 usCmd = SB_LINERIGHT;
2816 usID = ID_HSCROLL; // WarpIN V1.0.18
2817 break;
2818
2819 case VK_LEFT:
2820 ulMsg = WM_HSCROLL;
2821 usCmd = SB_LINELEFT;
2822 usID = ID_HSCROLL; // WarpIN V1.0.18
2823 break;
2824
2825 case VK_PAGEUP:
2826 ulMsg = WM_VSCROLL;
2827 if (usFlags & KC_CTRL)
2828 {
2829 sPos = 0;
2830 usCmd = SB_SLIDERPOSITION;
2831 }
2832 else
2833 usCmd = SB_PAGEUP;
2834 break;
2835
2836 case VK_PAGEDOWN:
2837 ulMsg = WM_VSCROLL;
2838 if (usFlags & KC_CTRL)
2839 {
2840 sPos = ptxvd->xfd.szlWorkspace.cy;
2841 usCmd = SB_SLIDERPOSITION;
2842 }
2843 else
2844 usCmd = SB_PAGEDOWN;
2845 break;
2846
2847 case VK_HOME:
2848 if (usFlags & KC_CTRL)
2849 // vertical:
2850 ulMsg = WM_VSCROLL;
2851 else
2852 {
2853 ulMsg = WM_HSCROLL;
2854 usID = ID_HSCROLL; // WarpIN V1.0.18
2855 }
2856
2857 sPos = 0;
2858 usCmd = SB_SLIDERPOSITION;
2859 break;
2860
2861 case VK_END:
2862 if (usFlags & KC_CTRL)
2863 {
2864 // vertical:
2865 ulMsg = WM_VSCROLL;
2866 sPos = ptxvd->xfd.szlWorkspace.cy;
2867 }
2868 else
2869 {
2870 ulMsg = WM_HSCROLL;
2871 sPos = ptxvd->xfd.szlWorkspace.cx;
2872 usID = ID_HSCROLL; // WarpIN V1.0.18
2873 }
2874
2875 usCmd = SB_SLIDERPOSITION;
2876 break;
2877
2878 default:
2879 // other:
2880 fDefProc = TRUE;
2881 }
2882
2883 if ( ((usFlags & KC_KEYUP) == 0)
2884 && (ulMsg)
2885 )
2886 WinSendMsg(hwndTextView,
2887 ulMsg,
2888 MPFROMSHORT(usID),
2889 MPFROM2SHORT(sPos,
2890 usCmd));
2891 }
2892
2893 if (fDefProc)
2894 mrc = WinDefWindowProc(hwndTextView, WM_CHAR, mp1, mp2);
2895 // sends to owner
2896 else
2897 mrc = (MPARAM)TRUE;
2898 }
2899
2900 return mrc;
2901}
2902
2903/*
2904 *@@ ProcessJumpToAnchorName:
2905 * implementation for TXM_JUMPTOANCHORNAME in fnwpTextView.
2906 *
2907 *@@added V1.0.0 (2002-08-12) [umoeller]
2908 */
2909
2910STATIC MRESULT ProcessJumpToAnchorName(HWND hwndTextView, MPARAM mp1)
2911{
2912 MRESULT mrc = 0;
2913 PTEXTVIEWWINDATA ptxvd;
2914
2915 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2916 && (mp1)
2917 )
2918 {
2919 PLISTNODE pWordNode;
2920 PTXVWORD pWord;
2921 if ( (pWordNode = txvFindWordFromAnchor(&ptxvd->xfd,
2922 (const char*)mp1))
2923 && (pWord = (PTXVWORD)pWordNode->pItemData)
2924 )
2925 {
2926 // found:
2927 PTXVRECTANGLE pRect = pWord->pRectangle;
2928 ULONG ulWinCY = (ptxvd->rclViewText.yTop - ptxvd->rclViewText.yBottom);
2929
2930 // now we need to scroll the window so that this rectangle is on top.
2931 // Since rectangles start out with the height of the window (e.g. +768)
2932 // and then have lower y coordinates down to way in the negatives,
2933 // to get the y offset, we must...
2934 ptxvd->ulViewYOfs = (-pRect->rcl.yTop) - ulWinCY;
2935
2936 if (ptxvd->ulViewYOfs < 0)
2937 ptxvd->ulViewYOfs = 0;
2938 if (ptxvd->ulViewYOfs > ((LONG)ptxvd->xfd.szlWorkspace.cy - ulWinCY))
2939 ptxvd->ulViewYOfs = (LONG)ptxvd->xfd.szlWorkspace.cy - ulWinCY;
2940
2941 // vertical scroll bar enabled at all?
2942 if (ptxvd->flStyle & XS_VSCROLL)
2943 {
2944 /* BOOL fEnabled = */ winhUpdateScrollBar(ptxvd->hwndVScroll,
2945 ulWinCY,
2946 ptxvd->xfd.szlWorkspace.cy,
2947 ptxvd->ulViewYOfs,
2948 (ptxvd->flStyle & XS_AUTOVHIDE));
2949 WinInvalidateRect(hwndTextView, NULL, FALSE);
2950 }
2951
2952 mrc = (MRESULT)TRUE;
2953 }
2954 }
2955
2956 return mrc;
2957}
2958
2959/*
2960 *@@ ProcessDestroy:
2961 * implementation for WM_DESTROY in fnwpTextView.
2962 *
2963 *@@added V1.0.0 (2002-08-12) [umoeller]
2964 */
2965
2966STATIC MRESULT ProcessDestroy(HWND hwndTextView, MPARAM mp1, MPARAM mp2)
2967{
2968 PTEXTVIEWWINDATA ptxvd;
2969
2970 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2971 {
2972 xstrClear(&ptxvd->xfd.strViewText);
2973 lstClear(&ptxvd->xfd.llRectangles);
2974 lstClear(&ptxvd->xfd.llWords);
2975 GpiDestroyPS(ptxvd->hps);
2976 free(ptxvd);
2977 WinSetWindowPtr(hwndTextView, QWL_PRIVATE, NULL);
2978 }
2979
2980 return WinDefWindowProc(hwndTextView, WM_DESTROY, mp1, mp2);
2981}
2982
2983/*
2984 *@@ fnwpTextView:
2985 * window procedure for the text view control. This is
2986 * registered with the WC_XTEXTVIEW class in txvRegisterTextView.
2987 *
2988 * The text view control is not a subclassed whatever control,
2989 * but a control implemented from scratch. As a result, we
2990 * had to implement all messages which are usually recognized
2991 * by a control. In detail, we have:
2992 *
2993 * -- WM_WINDOWPOSCHANGED: if the control is resized, the
2994 * text is reformatted and the scroll bars are readjusted.
2995 * See AdjustViewRects and txvFormatText.
2996 *
2997 * -- WM_PRESPARAMCHANGED: if fonts or colors are dropped
2998 * on the control, we reformat the text also.
2999 *
3000 * -- WM_HSCROLL and WM_VSCROLL: this calls winhHandleScrollMsg
3001 * to scroll the window contents.
3002 *
3003 * -- WM_BUTTON1DOWN: this sets the focus to the control.
3004 *
3005 * -- WM_SETFOCUS: if we receive the focus, we draw a fine
3006 * dotted line in the "selection" color around the text
3007 * window.
3008 *
3009 * -- WM_CHAR: if we have the focus, the user can move the
3010 * visible part within the workspace using the usual
3011 * cursor and HOME/END keys.
3012 *
3013 * -- WM_MOUSEMOVE: this sends WM_CONTROLPOINTER to the
3014 * owner so the owner can change the mouse pointer.
3015 *
3016 * <B>Painting</B>
3017 *
3018 * The text view control creates a micro presentation space
3019 * from the window's device context upon WM_CREATE, which is
3020 * stored in TEXTVIEWWINDATA. We do not use WinBeginPaint in
3021 * WM_PAINT, but only the PS we created ourselves. This saves
3022 * us from resetting and researching all the fonts etc., which
3023 * should be speedier.
3024 *
3025 * The text view control uses a private window word for storing
3026 * its own data. The client is free to use QWL_USER of the
3027 * text view control.
3028 *
3029 *@@changed V0.9.3 (2000-05-05) [umoeller]: removed TXM_NEWTEXT; now supporting WinSetWindowText
3030 *@@changed V0.9.3 (2000-05-07) [umoeller]: crashed if create param was NULL; fixed
3031 *@@changed V0.9.20 (2002-08-10) [umoeller]: no longer using QWL_USER, which is free now
3032 *@@changed V0.9.20 (2002-08-10) [umoeller]: setting text on window creation never worked, fixed
3033 *@@changed V0.9.20 (2002-08-10) [umoeller]: added TXN_ANCHORCLICKED owner notify for anchors
3034 *@@changed V0.9.20 (2002-08-10) [umoeller]: converted private style flags to XS_* window style flags
3035 *@@changed V0.9.20 (2002-08-10) [umoeller]: added support for XS_STATIC
3036 *@@changed V0.9.20 (2002-08-10) [umoeller]: added support for formatting HTML and plain text automatically
3037 *@@changed V1.0.0 (2002-08-12) [umoeller]: optimized locality by moving big chunks into subfuncs
3038 */
3039
3040STATIC MRESULT EXPENTRY fnwpTextView(HWND hwndTextView, ULONG msg, MPARAM mp1, MPARAM mp2)
3041{
3042 MRESULT mrc = 0;
3043 PTEXTVIEWWINDATA ptxvd;
3044
3045 switch (msg)
3046 {
3047 /*
3048 * WM_CREATE:
3049 *
3050 */
3051
3052 case WM_CREATE:
3053 mrc = ProcessCreate(hwndTextView, mp1, mp2);
3054 // extracted V1.0.0 (2002-08-12) [umoeller]
3055 break;
3056
3057 /*
3058 * WM_SETWINDOWPARAMS:
3059 * this message sets the window parameters,
3060 * most importantly, the window text.
3061 *
3062 * This updates the control.
3063 */
3064
3065 case WM_SETWINDOWPARAMS:
3066 if ( (mp1)
3067 && (((PWNDPARAMS)mp1)->fsStatus & WPM_TEXT)
3068 && (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3069 )
3070 {
3071 SetWindowText(hwndTextView,
3072 ptxvd,
3073 ((PWNDPARAMS)mp1)->pszText);
3074 mrc = (MRESULT)TRUE; // was missing V0.9.20 (2002-08-10) [umoeller]
3075 }
3076 break;
3077
3078 /*
3079 * WM_WINDOWPOSCHANGED:
3080 *
3081 */
3082
3083 case WM_WINDOWPOSCHANGED:
3084 // resizing?
3085 if ( (mp1)
3086 && (((PSWP)mp1)->fl & SWP_SIZE)
3087 && (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3088 )
3089 {
3090 WinQueryWindowRect(hwndTextView,
3091 &ptxvd->rclViewReal);
3092 AdjustViewRects(hwndTextView,
3093 ptxvd);
3094 FormatText2Screen(hwndTextView,
3095 ptxvd,
3096 FALSE,
3097 FALSE); // quick format
3098 }
3099 break;
3100
3101 /*
3102 * WM_PAINT:
3103 *
3104 */
3105
3106 case WM_PAINT:
3107 ProcessPaint(hwndTextView);
3108 // extracted V1.0.0 (2002-08-12) [umoeller]
3109 break;
3110
3111 /*
3112 * WM_PRESPARAMCHANGED:
3113 *
3114 * Changing the color or font settings
3115 * is equivalent to changing the default
3116 * paragraph format. See TXM_SETFORMAT.
3117 */
3118
3119 case WM_PRESPARAMCHANGED:
3120 ProcessPresParamChanged(hwndTextView, mp1);
3121 break;
3122
3123 /*
3124 * WM_VSCROLL:
3125 *
3126 */
3127
3128 case WM_VSCROLL:
3129 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3130 && (ptxvd->fVScrollVisible)
3131 )
3132 {
3133 winhHandleScrollMsg(hwndTextView,
3134 ptxvd->hwndVScroll,
3135 &ptxvd->ulViewYOfs,
3136 &ptxvd->rclViewText,
3137 ptxvd->xfd.szlWorkspace.cy,
3138 ptxvd->cdata.ulVScrollLineUnit,
3139 msg,
3140 mp2);
3141 }
3142 break;
3143
3144 /*
3145 * WM_HSCROLL:
3146 *
3147 */
3148
3149 case WM_HSCROLL:
3150 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3151 && (ptxvd->fHScrollVisible)
3152 )
3153 {
3154 winhHandleScrollMsg(hwndTextView,
3155 ptxvd->hwndHScroll,
3156 &ptxvd->ulViewXOfs,
3157 &ptxvd->rclViewText,
3158 ptxvd->xfd.szlWorkspace.cx,
3159 ptxvd->cdata.ulHScrollLineUnit,
3160 msg,
3161 mp2);
3162 }
3163 break;
3164
3165 /*
3166 * WM_SETFOCUS:
3167 *
3168 */
3169
3170 case WM_SETFOCUS:
3171 ProcessSetFocus(hwndTextView, mp2);
3172 break;
3173
3174 /*
3175 * WM_MOUSEMOVE:
3176 * send WM_CONTROLPOINTER to owner.
3177 */
3178
3179 case WM_MOUSEMOVE:
3180 {
3181 HWND hwndOwner;
3182 if (hwndOwner = WinQueryWindow(hwndTextView, QW_OWNER))
3183 {
3184 HPOINTER hptrSet
3185 = (HPOINTER)WinSendMsg(hwndOwner,
3186 WM_CONTROLPOINTER,
3187 (MPARAM)(LONG)WinQueryWindowUShort(hwndTextView,
3188 QWS_ID),
3189 (MPARAM)WinQuerySysPointer(HWND_DESKTOP,
3190 SPTR_ARROW,
3191 FALSE));
3192 WinSetPointer(HWND_DESKTOP, hptrSet);
3193 }
3194 }
3195 break;
3196
3197 /*
3198 * WM_BUTTON1DOWN:
3199 *
3200 */
3201
3202 case WM_BUTTON1DOWN:
3203 mrc = ProcessButton1Down(hwndTextView, mp1);
3204 break;
3205
3206 /*
3207 * WM_BUTTON1UP:
3208 *
3209 */
3210
3211 case WM_BUTTON1UP:
3212 mrc = ProcessButton1Up(hwndTextView, mp1);
3213 break;
3214
3215 /*
3216 * WM_CHAR:
3217 *
3218 */
3219
3220 case WM_CHAR:
3221 mrc = ProcessChar(hwndTextView, mp1, mp2);
3222 break;
3223
3224 /*
3225 *@@ TXM_QUERYPARFORMAT:
3226 * this msg can be sent to the text view control
3227 * to retrieve the paragraph format with the
3228 * index specified in mp1.
3229 *
3230 * This must be sent, not posted, to the control.
3231 *
3232 * Parameters:
3233 *
3234 * -- ULONG mp1: index of format to query.
3235 * Must be 0 currently for the standard
3236 * paragraph format.
3237 *
3238 * -- PXFMTPARAGRAPH mp2: pointer to buffer
3239 * which is to receive the formatting
3240 * data.
3241 *
3242 * Returns TRUE if copying was successful.
3243 *
3244 *@@added V0.9.3 (2000-05-06) [umoeller]
3245 */
3246
3247 case TXM_QUERYPARFORMAT:
3248 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3249 && (!mp1)
3250 && (mp2)
3251 )
3252 {
3253 memcpy(mp2,
3254 &ptxvd->xfd.fmtpStandard,
3255 sizeof(XFMTPARAGRAPH));
3256 mrc = (MPARAM)TRUE;
3257 }
3258 break;
3259
3260 /*
3261 *@@ TXM_SETPARFORMAT:
3262 * reverse to TXM_QUERYPARFORMAT, this sets a
3263 * paragraph format (line spacings, margins
3264 * and such).
3265 *
3266 * This must be sent, not posted, to the control.
3267 *
3268 * Parameters:
3269 *
3270 * -- ULONG mp1: index of format to set.
3271 * Must be 0 currently for the standard
3272 * paragraph format.
3273 *
3274 * -- PXFMTPARAGRAPH mp2: pointer to buffer
3275 * from which to copy formatting data.
3276 * If this pointer is NULL, the format
3277 * is reset to the default.
3278 *
3279 * This reformats the control.
3280 *
3281 *@@added V0.9.3 (2000-05-06) [umoeller]
3282 */
3283
3284 case TXM_SETPARFORMAT:
3285 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3286 && (!mp1)
3287 )
3288 {
3289 if (mp2)
3290 // copy:
3291 memcpy(&ptxvd->xfd.fmtpStandard,
3292 mp2,
3293 sizeof(XFMTPARAGRAPH));
3294 else
3295 // default:
3296 memset(&ptxvd->xfd.fmtpStandard,
3297 0,
3298 sizeof(XFMTPARAGRAPH));
3299
3300 FormatText2Screen(hwndTextView,
3301 ptxvd,
3302 FALSE,
3303 TRUE); // full reformat
3304
3305 mrc = (MPARAM)TRUE;
3306 }
3307 break;
3308
3309 /*
3310 *@@ TXM_SETWORDWRAP:
3311 * this text view control msg quickly changes
3312 * the word-wrapping style of the default
3313 * paragraph formatting.
3314 *
3315 * This may be sent or posted.
3316 *
3317 * (BOOL)mp1 determines whether word wrapping
3318 * should be turned on or off.
3319 */
3320
3321 case TXM_SETWORDWRAP:
3322 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3323 {
3324 BOOL ulOldFlFormat = ptxvd->xfd.fmtpStandard.fWordWrap;
3325 ptxvd->xfd.fmtpStandard.fWordWrap = (BOOL)mp1;
3326 if (ptxvd->xfd.fmtpStandard.fWordWrap != ulOldFlFormat)
3327 FormatText2Screen(hwndTextView,
3328 ptxvd,
3329 FALSE,
3330 FALSE); // quick format
3331 }
3332 break;
3333
3334 /*
3335 *@@ TXM_QUERYCDATA:
3336 * copies the current XTEXTVIEWCDATA
3337 * into the specified buffer.
3338 *
3339 * This must be sent, not posted, to the control.
3340 *
3341 * Parameters:
3342 *
3343 * -- PXTEXTVIEWCDATA mp1: target buffer.
3344 * Before calling this, you MUST specify
3345 * XTEXTVIEWCDATA.cbData.
3346 *
3347 * Returns the bytes that were copied as
3348 * a ULONG.
3349 */
3350
3351 case TXM_QUERYCDATA:
3352 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3353 && (mp1)
3354 )
3355 {
3356 PXTEXTVIEWCDATA pTarget = (PXTEXTVIEWCDATA)mp1;
3357 mrc = (MRESULT)min(pTarget->cbData, sizeof(XTEXTVIEWCDATA));
3358 memcpy(pTarget,
3359 &ptxvd->cdata,
3360 (ULONG)mrc);
3361 }
3362 break;
3363
3364 /*
3365 *@@ TXM_SETCDATA:
3366 * updates the current XTEXTVIEWCDATA
3367 * with the data from the specified buffer.
3368 *
3369 * This must be sent, not posted, to the control.
3370 *
3371 * Parameters:
3372 *
3373 * -- PXTEXTVIEWCDATA mp1: source buffer.
3374 * Before calling this, you MUST specify
3375 * XTEXTVIEWCDATA.cbData.
3376 *
3377 * Returns the bytes that were copied as
3378 * a ULONG.
3379 *
3380 *@@changed V1.0.0 (2002-08-12) [umoeller]: now returning bytes
3381 */
3382
3383 case TXM_SETCDATA:
3384 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3385 && (mp1)
3386 )
3387 {
3388 PXTEXTVIEWCDATA pSource = (PXTEXTVIEWCDATA)mp1;
3389 mrc = (MRESULT)min(pSource->cbData, sizeof(XTEXTVIEWCDATA));
3390 memcpy(&ptxvd->cdata,
3391 pSource,
3392 (ULONG)mrc);
3393 }
3394 break;
3395
3396 /*
3397 *@@ TXM_JUMPTOANCHORNAME:
3398 * scrolls the XTextView control contents so that
3399 * the text marked with the specified anchor name
3400 * (TXVESC_ANCHORNAME escape) appears at the top
3401 * of the control.
3402 *
3403 * This must be sent, not posted, to the control.
3404 *
3405 * Parameters:
3406 * -- PSZ mp1: anchor name (e.g. "anchor1").
3407 *
3408 * Returns TRUE if the jump was successful.
3409 *
3410 *@@added V0.9.4 (2000-06-12) [umoeller]
3411 */
3412
3413 case TXM_JUMPTOANCHORNAME:
3414 mrc = ProcessJumpToAnchorName(hwndTextView, mp1);
3415 break;
3416
3417 /*
3418 *@@ TXM_QUERYTEXTEXTENT:
3419 * returns the extents of the currently set text,
3420 * that is, the width and height of the internal
3421 * work area, of which the current view rectangle
3422 * displays a subrectangle.
3423 *
3424 * This must be sent, not posted, to the control.
3425 *
3426 * Parameters:
3427 *
3428 * -- PSIZEL mp1: pointer to a SIZEL buffer,
3429 * which receives the extent in the cx and
3430 * cy members. These will be set to null
3431 * values if the control currently has no
3432 * text.
3433 *
3434 * Returns TRUE on success.
3435 *
3436 *@@added V0.9.20 (2002-08-10) [umoeller]
3437 */
3438
3439 case TXM_QUERYTEXTEXTENT:
3440 if ( (mp1)
3441 && (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3442 )
3443 {
3444 memcpy((PSIZEL)mp1,
3445 &ptxvd->xfd.szlWorkspace,
3446 sizeof(SIZEL));
3447 mrc = (MRESULT)TRUE;
3448 }
3449 break;
3450
3451 /*
3452 * WM_DESTROY:
3453 * clean up.
3454 */
3455
3456 case WM_DESTROY:
3457 mrc = ProcessDestroy(hwndTextView, mp1, mp2);
3458 break;
3459
3460 default:
3461 mrc = WinDefWindowProc(hwndTextView, msg, mp1, mp2);
3462 }
3463
3464 return mrc;
3465}
3466
3467/*
3468 *@@ txvRegisterTextView:
3469 * registers the Text View class with PM. Required
3470 * before the text view control can be used.
3471 */
3472
3473BOOL txvRegisterTextView(HAB hab)
3474{
3475 return WinRegisterClass(hab,
3476 WC_XTEXTVIEW,
3477 fnwpTextView,
3478 0,
3479 2 * sizeof(PVOID)); // QWL_USER and QWL_PRIVATE
3480}
3481
3482/*
3483 *@@ txvReplaceWithTextView:
3484 * replaces any window with a text view control.
3485 * You must call txvRegisterTextView beforehand.
3486 *
3487 *@@added V0.9.1 (2000-02-13) [umoeller]
3488 */
3489
3490HWND txvReplaceWithTextView(HWND hwndParentAndOwner,
3491 USHORT usID,
3492 ULONG flWinStyle,
3493 USHORT usBorder)
3494{
3495 HWND hwndMLE = WinWindowFromID(hwndParentAndOwner, usID),
3496 hwndTextView = NULLHANDLE;
3497 if (hwndMLE)
3498 {
3499 ULONG ul,
3500 // attrFound,
3501 abValue[32];
3502 SWP swpMLE;
3503 XTEXTVIEWCDATA xtxCData;
3504 PSZ pszFont = winhQueryWindowFont(hwndMLE);
3505 LONG lBackClr = -1,
3506 lForeClr = -1;
3507
3508 if ((ul = WinQueryPresParam(hwndMLE,
3509 PP_BACKGROUNDCOLOR,
3510 0,
3511 NULL,
3512 (ULONG)sizeof(abValue),
3513 (PVOID)&abValue,
3514 QPF_NOINHERIT)))
3515 lBackClr = abValue[0];
3516
3517 if ((ul = WinQueryPresParam(hwndMLE,
3518 PP_FOREGROUNDCOLOR,
3519 0,
3520 NULL,
3521 (ULONG)sizeof(abValue),
3522 (PVOID)&abValue,
3523 QPF_NOINHERIT)))
3524 lForeClr = abValue[0];
3525
3526 WinQueryWindowPos(hwndMLE, &swpMLE);
3527
3528 WinDestroyWindow(hwndMLE);
3529 memset(&xtxCData, 0, sizeof(xtxCData));
3530 xtxCData.cbData = sizeof(xtxCData);
3531 xtxCData.ulXBorder = usBorder;
3532 xtxCData.ulYBorder = usBorder;
3533 hwndTextView = WinCreateWindow(hwndParentAndOwner,
3534 WC_XTEXTVIEW,
3535 "",
3536 flWinStyle,
3537 swpMLE.x,
3538 swpMLE.y,
3539 swpMLE.cx,
3540 swpMLE.cy,
3541 hwndParentAndOwner,
3542 HWND_TOP,
3543 usID,
3544 &xtxCData,
3545 0);
3546 if (pszFont)
3547 {
3548 winhSetWindowFont(hwndTextView, pszFont);
3549 free(pszFont);
3550 }
3551
3552 if (lBackClr != -1)
3553 WinSetPresParam(hwndTextView,
3554 PP_BACKGROUNDCOLOR,
3555 sizeof(ULONG),
3556 &lBackClr);
3557 if (lForeClr != -1)
3558 WinSetPresParam(hwndTextView,
3559 PP_FOREGROUNDCOLOR,
3560 sizeof(ULONG),
3561 &lForeClr);
3562 }
3563 return hwndTextView;
3564}
3565
3566/* ******************************************************************
3567 *
3568 * Printer-dependent functions
3569 *
3570 ********************************************************************/
3571
3572/*
3573 *@@ prthQueryQueues:
3574 * returns a buffer containing all print queues
3575 * on the system.
3576 *
3577 * This is usually the first step before printing.
3578 * After calling this function, show a dlg to the
3579 * user, allow him to select the printer queue
3580 * to be used. This can then be passed to
3581 * prthCreatePrinterDC.
3582 *
3583 * Use prthFreeBuf to free the returned buffer.
3584 */
3585
3586STATIC PRQINFO3* prthEnumQueues(PULONG pulReturned) // out: no. of queues found
3587{
3588 SPLERR rc;
3589 ULONG cTotal;
3590 ULONG cbNeeded = 0;
3591 PRQINFO3 *pprq3 = NULL;
3592
3593 // count queues & get number of bytes needed for buffer
3594 rc = SplEnumQueue(NULL, // default computer
3595 3, // detail level
3596 NULL, // pbuf
3597 0L, // cbBuf
3598 pulReturned, // out: entries returned
3599 &cTotal, // out: total entries available
3600 &cbNeeded,
3601 NULL); // reserved
3602
3603 if (!rc && cbNeeded)
3604 {
3605 pprq3 = (PRQINFO3*)malloc(cbNeeded);
3606 if (pprq3)
3607 {
3608 // enum the queues
3609 rc = SplEnumQueue(NULL,
3610 3,
3611 pprq3,
3612 cbNeeded,
3613 pulReturned,
3614 &cTotal,
3615 &cbNeeded,
3616 NULL);
3617 }
3618 }
3619
3620 return pprq3;
3621}
3622
3623/*
3624 *@@ prthFreeBuf:
3625 *
3626 */
3627
3628STATIC VOID prthFreeBuf(PVOID pprq3)
3629{
3630 if (pprq3)
3631 free(pprq3);
3632}
3633
3634/*
3635 *@@ prthCreatePrinterDC:
3636 * creates a device context for the printer
3637 * specified by the given printer queue.
3638 *
3639 * As a nifty feature, this returns printer
3640 * device resolution automatically in the
3641 * specified buffer.
3642 *
3643 * Returns NULLHANDLE (== DEV_ERROR) on errors.
3644 *
3645 * Use DevCloseDC to destroy the DC.
3646 *
3647 * Based on print sample by Peter Fitzsimmons, Fri 95-09-29 02:47:16am.
3648 */
3649
3650STATIC HDC prthCreatePrinterDC(HAB hab,
3651 PRQINFO3 *pprq3,
3652 PLONG palRes) // out: 2 longs holding horizontal and vertical
3653 // printer resolution in pels per inch
3654{
3655 HDC hdc = NULLHANDLE;
3656 DEVOPENSTRUC dos;
3657 PSZ p;
3658
3659 memset(&dos, 0, sizeof(dos));
3660 p = strrchr(pprq3->pszDriverName, '.');
3661 if (p)
3662 *p = 0; // del everything after '.'
3663
3664 dos.pszLogAddress = pprq3->pszName;
3665 dos.pszDriverName = pprq3->pszDriverName;
3666 dos.pdriv = pprq3->pDriverData;
3667 dos.pszDataType = "PM_Q_STD";
3668 hdc = DevOpenDC(hab,
3669 OD_QUEUED,
3670 "*",
3671 4L, // count of items in next param
3672 (PDEVOPENDATA)&dos,
3673 0); // compatible DC
3674
3675 if (hdc)
3676 DevQueryCaps(hdc,
3677 CAPS_HORIZONTAL_FONT_RES,
3678 2,
3679 palRes); // buffer
3680
3681 return hdc;
3682}
3683
3684/*
3685 *@@ prthQueryForms:
3686 * returns a buffer containing all forms
3687 * supported by the specified printer DC.
3688 *
3689 * Use prthFreeBuf to free the returned
3690 * buffer.
3691 *
3692 * HCINFO uses different model spaces for
3693 * the returned info. See PMREF for details.
3694 */
3695
3696STATIC HCINFO* prthQueryForms(HDC hdc,
3697 PULONG pulCount)
3698{
3699 HCINFO *pahci = NULL;
3700
3701 LONG cForms;
3702
3703 // get form count
3704 cForms = DevQueryHardcopyCaps(hdc, 0L, 0L, NULL); // phci);
3705 if (cForms)
3706 {
3707 pahci = (HCINFO*)malloc(cForms * sizeof(HCINFO));
3708 if (pahci)
3709 {
3710 *pulCount = DevQueryHardcopyCaps(hdc, 0, cForms, pahci);
3711 }
3712 }
3713
3714 return pahci;
3715}
3716
3717/*
3718 *@@ prthCreatePS:
3719 * creates a "normal" presentation space from the specified
3720 * printer device context (which can be opened thru
3721 * prthCreatePrinterDC).
3722 *
3723 * Returns NULLHANDLE on errors.
3724 *
3725 * Based on print sample by Peter Fitzsimmons, Fri 95-09-29 02:47:16am.
3726 */
3727
3728STATIC HPS prthCreatePS(HAB hab, // in: anchor block
3729 HDC hdc, // in: printer device context
3730 ULONG ulUnits) // in: one of:
3731 // -- PU_PELS
3732 // -- PU_LOMETRIC
3733 // -- PU_HIMETRIC
3734 // -- PU_LOENGLISH
3735 // -- PU_HIENGLISH
3736 // -- PU_TWIPS
3737{
3738 SIZEL sizel;
3739
3740 sizel.cx = 0;
3741 sizel.cy = 0;
3742 return GpiCreatePS(hab,
3743 hdc,
3744 &sizel,
3745 ulUnits | GPIA_ASSOC | GPIT_NORMAL);
3746}
3747
3748/*
3749 *@@ prthStartDoc:
3750 * calls DevEscape with DEVESC_STARTDOC.
3751 * This must be called before any painting
3752 * into the HDC's HPS. Any GPI calls made
3753 * before this are ignored.
3754 *
3755 * pszDocTitle appears in the spooler.
3756 */
3757
3758STATIC VOID prthStartDoc(HDC hdc,
3759 PSZ pszDocTitle)
3760{
3761 DevEscape(hdc,
3762 DEVESC_STARTDOC,
3763 strlen(pszDocTitle),
3764 pszDocTitle,
3765 0L,
3766 0L);
3767}
3768
3769/*
3770 *@@ prthNextPage:
3771 * calls DevEscape with DEVESC_NEWFRAME.
3772 * Signals when an application has finished writing to a page and wants to
3773 * start a new page. It is similar to GpiErase processing for a screen device
3774 * context, and causes a reset of the attributes. This escape is used with a
3775 * printer device to advance to a new page.
3776 */
3777
3778STATIC VOID prthNextPage(HDC hdc)
3779{
3780 DevEscape(hdc,
3781 DEVESC_NEWFRAME,
3782 0,
3783 0,
3784 0,
3785 0);
3786}
3787
3788/*
3789 *@@ prthEndDoc:
3790 * calls DevEscape with DEVESC_ENDDOC
3791 * and disassociates the HPS from the HDC.
3792 * Call this right before doing
3793 + GpiDestroyPS(hps);
3794 + DevCloseDC(hdc);
3795 */
3796
3797STATIC VOID prthEndDoc(HDC hdc,
3798 HPS hps)
3799{
3800 DevEscape(hdc, DEVESC_ENDDOC, 0L, 0L, 0, NULL);
3801 GpiAssociate(hps, NULLHANDLE);
3802}
3803
3804/*
3805 *@@ txvPrint:
3806 * this does the actual printing.
3807 */
3808
3809BOOL txvPrint(HAB hab,
3810 HDC hdc, // in: printer device context
3811 HPS hps, // in: printer presentation space (using PU_PELS)
3812 PSZ pszViewText, // in: text to print
3813 ULONG ulSize, // in: default font point size
3814 PSZ pszFaceName, // in: default font face name
3815 HCINFO *phci, // in: hardcopy form to use
3816 PSZ pszDocTitle, // in: document title (appears in spooler)
3817 FNPRINTCALLBACK *pfnCallback)
3818{
3819 RECTL rclPageDevice,
3820 rclPageWorld;
3821 XFORMATDATA xfd;
3822 BOOL fAnotherPage = FALSE;
3823 ULONG ulCurrentLineIndex = 0,
3824 ulCurrentPage = 1;
3825 ULONG ulCurrentYOfs = 0;
3826
3827 /* MATRIXLF matlf;
3828 POINTL ptlCenter;
3829 FIXED scalars[2]; */
3830
3831 // important: we must do a STARTDOC before we use the printer HPS.
3832 prthStartDoc(hdc,
3833 pszDocTitle);
3834
3835 // the PS is in TWIPS, but our world coordinate
3836 // space is in pels, so we need to transform
3837 /* GpiQueryViewingTransformMatrix(hps,
3838 1L,
3839 &matlf);
3840 ptlCenter.x = 0;
3841 ptlCenter.y = 0;
3842 scalars[0] = MAKEFIXED(2,0);
3843 scalars[1] = MAKEFIXED(3,0);
3844
3845 GpiScale (hps,
3846 &matlf,
3847 TRANSFORM_REPLACE,
3848 scalars,
3849 &ptlCenter); */
3850
3851 // initialize format with font from window
3852 txvInitFormat(&xfd);
3853
3854 /* SetFormatFont(hps,
3855 &xfd,
3856 ulSize,
3857 pszFaceName); */
3858
3859 // use text from window
3860 xstrcpy(&xfd.strViewText, pszViewText, 0);
3861
3862 // setup page
3863 GpiQueryPageViewport(hps,
3864 &rclPageDevice);
3865 // this is in device units; convert this
3866 // to the world coordinate space of the printer PS
3867 memcpy(&rclPageWorld, &rclPageDevice, sizeof(RECTL));
3868 GpiConvert(hps,
3869 CVTC_DEVICE, // source
3870 CVTC_WORLD,
3871 2, // 2 points, it's a rectangle
3872 (PPOINTL)&rclPageWorld);
3873
3874 // left and bottom margins are in millimeters...
3875 /* rclPage.xLeft = 100; // ###
3876 rclPage.yBottom = 100;
3877 rclPage.xRight = rclPage.xLeft + phci->xPels;
3878 rclPage.yTop = rclPage.yBottom + phci->yPels; */
3879
3880 txvFormatText(hps,
3881 &xfd, // in: ptxvd->rclViewText
3882 &rclPageWorld,
3883 TRUE);
3884
3885 do
3886 {
3887 _Pmpf(("---- printing page %d",
3888 ulCurrentPage));
3889
3890 fAnotherPage = txvPaintText(hab,
3891 hps,
3892 &xfd,
3893 &rclPageWorld,
3894 0,
3895 &ulCurrentYOfs,
3896 FALSE, // draw only fully visible lines
3897 &ulCurrentLineIndex); // in/out: line to start with
3898 if (fAnotherPage)
3899 {
3900 prthNextPage(hdc);
3901
3902 if (pfnCallback(ulCurrentPage++, 0) == FALSE)
3903 fAnotherPage = FALSE;
3904 }
3905 } while (fAnotherPage);
3906
3907 prthEndDoc(hdc, hps);
3908
3909 return TRUE;
3910}
3911
3912/*
3913 *@@ txvPrintWindow:
3914 * one-shot function which prints the contents
3915 * of the specified XTextView control to the
3916 * default printer, using the default form.
3917 *
3918 * Returns a nonzero value upon errors.
3919 *
3920 * Based on print sample by Peter Fitzsimmons, Fri 95-09-29 02:47:16am.
3921 */
3922
3923int txvPrintWindow(HWND hwndTextView,
3924 PSZ pszDocTitle, // in: document title (appears in spooler)
3925 FNPRINTCALLBACK *pfnCallback)
3926{
3927 int irc = 0;
3928
3929 PTEXTVIEWWINDATA ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE);
3930
3931 if (!ptxvd)
3932 irc = 1;
3933 else
3934 {
3935 ULONG cReturned = 0;
3936 PRQINFO3 *pprq3 = prthEnumQueues(&cReturned);
3937 HDC hdc = NULLHANDLE;
3938 LONG caps[2];
3939
3940 // find default queue
3941 if (pprq3)
3942 {
3943 ULONG i;
3944 // search for default queue;
3945 for (i = 0; i < cReturned; i++)
3946 if (pprq3[i].fsType & PRQ3_TYPE_APPDEFAULT)
3947 {
3948 hdc = prthCreatePrinterDC(ptxvd->hab,
3949 &pprq3[i],
3950 caps);
3951
3952 break;
3953 }
3954 prthFreeBuf(pprq3);
3955 }
3956
3957 if (!hdc)
3958 irc = 2;
3959 else
3960 {
3961 // OK, we got a printer DC:
3962 HPS hps;
3963 ULONG cForms = 0;
3964 HCINFO *pahci,
3965 *phciSelected = 0;
3966
3967 // find default form
3968 pahci = prthQueryForms(hdc,
3969 &cForms);
3970 if (pahci)
3971 {
3972 HCINFO *phciThis = pahci;
3973 ULONG i;
3974 for (i = 0;
3975 i < cForms;
3976 i++, phciThis++)
3977 {
3978 if (phciThis->flAttributes & HCAPS_CURRENT)
3979 {
3980 phciSelected = phciThis;
3981 }
3982 }
3983 }
3984
3985 if (!phciSelected)
3986 irc = 3;
3987 else
3988 {
3989 // create printer PS
3990 hps = prthCreatePS(ptxvd->hab,
3991 hdc,
3992 PU_PELS);
3993
3994 if (hps == GPI_ERROR)
3995 irc = 4;
3996 else
3997 {
3998 PSZ pszFont;
3999 ULONG ulSize = 0;
4000 PSZ pszFaceName = 0;
4001
4002 if ((pszFont = winhQueryWindowFont(hwndTextView)))
4003 gpihSplitPresFont(pszFont,
4004 &ulSize,
4005 &pszFaceName);
4006 txvPrint(ptxvd->hab,
4007 hdc,
4008 hps,
4009 ptxvd->xfd.strViewText.psz,
4010 ulSize,
4011 pszFaceName,
4012 phciSelected,
4013 pszDocTitle,
4014 pfnCallback);
4015
4016 if (pszFont)
4017 free(pszFont);
4018
4019 GpiDestroyPS(hps);
4020 }
4021 }
4022 DevCloseDC(hdc);
4023 }
4024 }
4025
4026 return irc;
4027}
4028
4029
Note: See TracBrowser for help on using the repository browser.