source: trunk/src/helpers/textview.c@ 242

Last change on this file since 242 was 242, checked in by umoeller, 23 years ago

First attempt at new container contol.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 136.6 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 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 *@@todo TXVWORDF_GLUEWITHNEXT
1103 */
1104
1105VOID txvFormatText(HPS hps, // in: HPS whose font is used for
1106 // calculating text dimensions
1107 PXFORMATDATA pxfd, // in: formatting data
1108 PRECTL prclView, // in: rectangle to format for (window or printer page)
1109 BOOL fFullRecalc) // in: re-calculate word list too? (must be TRUE on the first call)
1110{
1111 /* ULONG ulWinCX = (prclView->xRight - prclView->xLeft),
1112 ulWinCY = (prclView->yTop - prclView->yBottom); */
1113
1114 lstClear(&pxfd->llRectangles);
1115 if (fFullRecalc)
1116 lstClear(&pxfd->llWords);
1117
1118 pxfd->szlWorkspace.cx = 0;
1119 pxfd->szlWorkspace.cy = 0;
1120
1121 if (pxfd->strViewText.cbAllocated)
1122 {
1123 ULONG ulTextLen = pxfd->strViewText.ulLength;
1124
1125 FORMATLINEBUF flbuf;
1126 LONG lcidLast = -99,
1127 lPointSizeLast = -99;
1128
1129 memset(&flbuf, 0, sizeof(flbuf));
1130 // copy default paragraph formatting
1131 memcpy(&flbuf.fmtp, &pxfd->fmtpStandard, sizeof(flbuf.fmtp));
1132 // set font
1133 flbuf.pfmtc = &pxfd->fmtcStandard;
1134 flbuf.lPointSize = pxfd->fmtcStandard.lPointSize;
1135 flbuf.pLastChar = pxfd->strViewText.psz + ulTextLen;
1136
1137 if (ulTextLen)
1138 {
1139 ULONG cWords = 0;
1140
1141 if (fFullRecalc)
1142 {
1143 /*
1144 * step 1: create words
1145 *
1146 */
1147
1148 PSZ pCurrent = pxfd->strViewText.psz;
1149
1150 // loop until null terminator
1151 while (*pCurrent)
1152 {
1153 PTXVWORD pWord;
1154
1155 if (flbuf.fBold)
1156 {
1157 if (flbuf.fItalics)
1158 flbuf.pfmtf = &flbuf.pfmtc->fntBoldItalics;
1159 else
1160 flbuf.pfmtf = &flbuf.pfmtc->fntBold;
1161 }
1162 else
1163 if (flbuf.fItalics)
1164 flbuf.pfmtf = &flbuf.pfmtc->fntItalics;
1165 else
1166 flbuf.pfmtf = &flbuf.pfmtc->fntRegular;
1167
1168 // set font for subsequent calculations,
1169 // if changed (this includes the first call)
1170 if (lcidLast != flbuf.pfmtf->lcid)
1171 {
1172 GpiSetCharSet(hps, flbuf.pfmtf->lcid);
1173 lcidLast = flbuf.pfmtf->lcid;
1174 // force recalc of point size
1175 lPointSizeLast = -99;
1176 }
1177
1178 if (lPointSizeLast != flbuf.lPointSize)
1179 {
1180 if (flbuf.pfmtf->FontMetrics.fsDefn & FM_DEFN_OUTLINE)
1181 // is outline font:
1182 gpihSetPointSize(hps, flbuf.lPointSize);
1183 lPointSizeLast = flbuf.lPointSize;
1184 }
1185
1186 if (pWord = CreateWord(hps,
1187 &pCurrent, // advanced to next word
1188 &flbuf))
1189 {
1190 lstAppendItem(&pxfd->llWords, pWord);
1191
1192 /* {
1193 CHAR szWord[3000];
1194 strhncpy0(szWord, pWord->pStart, min(pWord->cChars, sizeof(szWord)));
1195 _Pmpf(("Found word '%s'", szWord));
1196 } */
1197
1198 cWords++;
1199
1200 while (*pCurrent == TXVESC_CHAR) // '\xFF')
1201 {
1202 // handle escapes;
1203 // this advances pCurrent depending on the
1204 // escape sequence length and might append
1205 // another "word" for the escape sequence
1206 // if it's relevant for rectangle correlation
1207 ProcessEscapes(&pCurrent,
1208 pxfd,
1209 &flbuf,
1210 FALSE); // fWordsProcessed
1211 }
1212 }
1213 else
1214 break;
1215 }
1216 } // end if (fFullRecalc)
1217 else
1218 cWords = lstCountItems(&pxfd->llWords);
1219
1220 /*
1221 * step 2: create rectangles
1222 *
1223 */
1224
1225 if (cWords)
1226 {
1227 PLISTNODE pWordNode = lstQueryFirstNode(&pxfd->llWords);
1228
1229 LONG lCurrentYTop = prclView->yTop,
1230 lOrigYTop = lCurrentYTop;
1231
1232 BOOL fRects2Go = TRUE;
1233
1234 // space before paragraph; this is reset
1235 // to 0 if we start a new rectangle for
1236 // the same paragraph
1237 ULONG ulYPre = flbuf.fmtp.lSpaceBefore;
1238
1239 // rectangles loop
1240 while (fRects2Go)
1241 {
1242 BOOL fWords2Go = TRUE;
1243 ULONG ulWordsInThisRect = 0;
1244
1245 // maximum height of words in this rect
1246 ULONG lWordsMaxCY = 0;
1247
1248 // start a new rectangle:
1249 PTXVRECTANGLE pRect = (PTXVRECTANGLE)malloc(sizeof(TXVRECTANGLE));
1250 lstInit(&pRect->llWords,
1251 FALSE); // no auto-free; the words are stored in the main
1252 // list also, which is freed
1253 // rectangle's xLeft;
1254 // xRight will be set when we're done with this rectangle
1255 pRect->rcl.xLeft = prclView->xLeft + flbuf.fmtp.lLeftMargin;
1256 if (ulYPre)
1257 // starting new paragraph:
1258 // add first-line offset also
1259 pRect->rcl.xLeft += flbuf.fmtp.lFirstLineMargin;
1260
1261 // current X pos: start with left of rectangle
1262 flbuf.lXCurrent = pRect->rcl.xLeft;
1263
1264 // max baseline ofs: set to 0, this will be raised
1265 pRect->ulMaxBaseLineOfs = 0;
1266
1267 // words-per-rectangle loop;
1268 // we keep adding words to the rectangle until
1269 // a) words no longer fit and word-wrapping is on;
1270 // b) a newline or line feed is found;
1271 // c) the last word has been reached;
1272 while (fWords2Go)
1273 {
1274 PTXVWORD pWordThis = (PTXVWORD)pWordNode->pItemData;
1275/*
1276 #define TXVWORDF_GLUEWITHNEXT 1 // escape
1277 #define TXVWORDF_LINEBREAK 2 // \n
1278 #define TXVWORDF_LINEFEED 4 // \r
1279*/
1280 BOOL fNextWord = FALSE;
1281
1282 if (pWordThis->cEscapeCode)
1283 {
1284 // pseudo-word for escape sequence:
1285 // process...
1286 ProcessEscapes((PSZ*)&pWordThis->pStart,
1287 pxfd,
1288 &flbuf,
1289 TRUE);
1290
1291 // append this sequence only if it's needed
1292 // for painting (list markers etc.)
1293 if (pWordThis->fPaintEscapeWord)
1294 {
1295 pWordThis->lX = flbuf.lXCurrent;
1296 pWordThis->pRectangle = pRect;
1297 lstAppendItem(&pRect->llWords, pWordThis);
1298 ulWordsInThisRect++;
1299 }
1300
1301 fNextWord = TRUE;
1302 }
1303 else
1304 {
1305 BOOL fWordWrapped = FALSE;
1306
1307 // not escape sequence, but real word: format...
1308 // is word-wrapping on?
1309 if (flbuf.fmtp.fWordWrap)
1310 {
1311 // yes: check if the word still fits
1312 if ( (flbuf.lXCurrent + pWordThis->ulCXWithSpaces // ###
1313 > prclView->xRight)
1314 // > ulWinCX)
1315 // but always add the first word in the rectangle,
1316 // because otherwise we get infinite loops
1317 && (ulWordsInThisRect > 0)
1318 )
1319 // no:
1320 fWordWrapped = TRUE;
1321 }
1322
1323 if (fWordWrapped)
1324 // start a new rectangle with the current word:
1325 fWords2Go = FALSE;
1326 // and do _not_ advance to the next word,
1327 // but start with this word for the next
1328 // rectangle...
1329 else
1330 {
1331 // add this word to the rectangle:
1332
1333 // store current X pos in word
1334 pWordThis->lX = flbuf.lXCurrent;
1335
1336 // increase current X pos by word width
1337 flbuf.lXCurrent += pWordThis->ulCXWithSpaces;
1338
1339 // store word in rectangle
1340 pWordThis->pRectangle = pRect;
1341 lstAppendItem(&pRect->llWords, pWordThis);
1342 // @@todo memory leak right here!!!
1343 ulWordsInThisRect++;
1344
1345 // store highest word width found for this rect
1346 if (pWordThis->ulCY > lWordsMaxCY)
1347 lWordsMaxCY = pWordThis->ulCY;
1348
1349 // store highest base line ofs found for this rect
1350 if (pWordThis->ulBaseLineOfs > pRect->ulMaxBaseLineOfs)
1351 pRect->ulMaxBaseLineOfs = pWordThis->ulBaseLineOfs;
1352
1353 // go for next word in any case
1354 fNextWord = TRUE;
1355 } // end if (!fBreakThisWord)
1356
1357 // now check: add more words to this rectangle?
1358 if ( (pWordThis->ulFlags == TXVWORDF_LINEBREAK)
1359 // no if linebreak found
1360 || (pWordThis->ulFlags == TXVWORDF_LINEFEED)
1361 // no if linefeed found
1362 || (!fWords2Go)
1363 // no if we're out of words or
1364 // word-break was forced
1365 )
1366 {
1367 // no: finish up this rectangle...
1368
1369 // xLeft has been set on top
1370 pRect->rcl.xRight = flbuf.lXCurrent;
1371 pRect->rcl.yTop = lCurrentYTop - ulYPre;
1372 pRect->rcl.yBottom = pRect->rcl.yTop - lWordsMaxCY;
1373
1374 // decrease current y top for next line
1375 lCurrentYTop = pRect->rcl.yBottom;
1376 if (!fRects2Go)
1377 // we're done completely:
1378 // add another one
1379 lCurrentYTop -= lWordsMaxCY;
1380
1381 if (fWordWrapped)
1382 // starting with wrapped word in next line:
1383 ulYPre = 0;
1384 else
1385 if (pWordThis->ulFlags == TXVWORDF_LINEFEED)
1386 ulYPre = 0;
1387 else if (pWordThis->ulFlags == TXVWORDF_LINEBREAK)
1388 {
1389 // line break:
1390 // set y-pre for next loop
1391 ulYPre = flbuf.fmtp.lSpaceBefore;
1392 // and add paragraph post-y
1393 lCurrentYTop -= flbuf.fmtp.lSpaceAfter;
1394 }
1395
1396 // update x extents
1397 if (pRect->rcl.xRight > pxfd->szlWorkspace.cx)
1398 pxfd->szlWorkspace.cx = pRect->rcl.xRight;
1399
1400 // and quit the inner loop
1401 fWords2Go = FALSE;
1402 } // end finish up rectangle
1403 } // end else if (pWordThis->fIsEscapeSequence)
1404
1405 if (fNextWord)
1406 {
1407 pWordNode = pWordNode->pNext;
1408 if (!pWordNode)
1409 {
1410 // no more to go:
1411 // quit
1412 fWords2Go = FALSE;
1413 fRects2Go = FALSE;
1414 }
1415 }
1416 } // end while (fWords2Go)
1417
1418 // store rectangle
1419 lstAppendItem(&pxfd->llRectangles, pRect);
1420 }
1421
1422 // lCurrentYTop now has the bottommost point we've used;
1423 // store this as workspace (this might be negative)
1424 pxfd->szlWorkspace.cy = lOrigYTop - lCurrentYTop;
1425 }
1426 }
1427 }
1428}
1429
1430/* ******************************************************************
1431 *
1432 * Device-independent text painting
1433 *
1434 ********************************************************************/
1435
1436/*
1437 *@@ DrawListMarker:
1438 *
1439 *@@added V0.9.3 (2000-05-17) [umoeller]
1440 */
1441
1442STATIC VOID DrawListMarker(HPS hps,
1443 PRECTL prclLine, // current line rectangle
1444 PTXVWORD pWordThis, // current word
1445 LONG lViewXOfs) // in: x offset to paint; 0 means rightmost
1446{
1447 POINTL ptl;
1448
1449 ULONG ulBulletSize = pWordThis->lPointSize * 2 / 3; // 2/3 of point size
1450
1451 ARCPARAMS arcp = {1, 1, 0, 0};
1452
1453 // pWordThis->pStart points to the \xFF character;
1454 // next is the "marker" escape (\x23),
1455 // next is the marker type
1456 CHAR cBulletType = *((pWordThis->pStart) + 2) ;
1457
1458 switch (cBulletType)
1459 {
1460 case 2: // square (filled box)
1461 ptl.x = pWordThis->lX - lViewXOfs;
1462 // center bullet vertically
1463 ptl.y = prclLine->yBottom
1464 + ( (prclLine->yTop - prclLine->yBottom) // height
1465 - ulBulletSize
1466 ) / 2;
1467
1468 GpiMove(hps, &ptl);
1469 ptl.x += ulBulletSize;
1470 ptl.y += ulBulletSize;
1471 GpiBox(hps, DRO_FILL, &ptl, 0, 0);
1472 break;
1473
1474 default: // case 1: // disc (filled circle)
1475 ptl.x = pWordThis->lX - lViewXOfs;
1476 // center bullet vertically;
1477 // the arc is drawn with the current position in its center
1478 ptl.y = prclLine->yBottom
1479 + ( (prclLine->yTop - prclLine->yBottom) // height
1480 / 2
1481 );
1482
1483 GpiSetArcParams(hps, &arcp);
1484 GpiMove(hps, &ptl);
1485 GpiFullArc(hps,
1486 (cBulletType == 3)
1487 ? DRO_OUTLINE
1488 : DRO_FILL,
1489 MAKEFIXED(ulBulletSize / 2, // radius!
1490 0));
1491 break;
1492
1493 }
1494}
1495
1496/*
1497 *@@ txvPaintText:
1498 * device-independent function for painting.
1499 * This can only be called after the text has
1500 * been formatted (using txvFormatText).
1501 *
1502 * This only paints rectangles which are within
1503 * prcl2Paint.
1504 *
1505 * -- For WM_PAINT, set this to the
1506 * update rectangle, and set fPaintHalfLines
1507 * to TRUE.
1508 *
1509 * -- For printing, set this to the page rectangle,
1510 * and set fPaintHalfLines to FALSE.
1511 *
1512 * All coordinates are in world space (PU_PELS).
1513 *
1514 *@@changed V0.9.3 (2000-05-05) [umoeller]: fixed wrong visible lines calculations; great speedup painting!
1515 *@@changed V0.9.3 (2000-05-06) [umoeller]: now using gpihCharStringPosAt
1516 */
1517
1518BOOL txvPaintText(HAB hab,
1519 HPS hps, // in: window or printer PS
1520 PXFORMATDATA pxfd,
1521 PRECTL prcl2Paint, // in: invalid rectangle to be drawn,
1522 // can be NULL to paint all
1523 LONG lViewXOfs, // in: x offset to paint; 0 means rightmost
1524 PULONG pulViewYOfs, // in: y offset to paint; 0 means _top_most;
1525 // out: y offset which should be passed to next call
1526 // (if TRUE is returned and fPaintHalfLines == FALSE)
1527 BOOL fPaintHalfLines, // in: if FALSE, lines which do not fully fit on
1528 // the page are dropped (useful for printing)
1529 PULONG pulLineIndex) // in: line to start painting with;
1530 // out: next line to paint, if any
1531 // (if TRUE is returned and fPaintHalfLines == FALSE)
1532{
1533 BOOL brc = FALSE,
1534 fAnyLinesPainted = FALSE;
1535 ULONG ulCurrentLineIndex = *pulLineIndex;
1536 // LONG lViewYOfsSaved = *pulViewYOfs;
1537 PLISTNODE pRectNode = lstNodeFromIndex(&pxfd->llRectangles,
1538 ulCurrentLineIndex);
1539
1540 LONG lcidLast = -99;
1541 LONG lPointSizeLast = -99;
1542
1543 while (pRectNode)
1544 {
1545 PTXVRECTANGLE pLineRcl = (PTXVRECTANGLE)pRectNode->pItemData;
1546 BOOL fPaintThis = FALSE;
1547
1548 // compose rectangle to draw for this line
1549 RECTL rclLine;
1550 rclLine.xLeft = pLineRcl->rcl.xLeft - lViewXOfs;
1551 rclLine.xRight = pLineRcl->rcl.xRight - lViewXOfs;
1552 rclLine.yBottom = pLineRcl->rcl.yBottom + *pulViewYOfs;
1553 rclLine.yTop = pLineRcl->rcl.yTop + *pulViewYOfs;
1554
1555 /* if (pmpf)
1556 {
1557 CHAR szTemp[100];
1558 ULONG cb = min(pLineRcl->cLineChars, 99);
1559 strhncpy0(szTemp, pLineRcl->pStartOfLine, cb);
1560
1561 _Pmpf(("Checking line %d: '%s'",
1562 ulCurrentLineIndex,
1563 szTemp));
1564
1565 _Pmpf((" (yB stored %d -> in HPS %d against win yB %d)",
1566 pLineRcl->rcl.yBottom,
1567 rclLine.yBottom,
1568 prcl2Paint->yBottom));
1569 } */
1570
1571 if (prcl2Paint == NULL)
1572 // draw all:
1573 fPaintThis = TRUE;
1574 else
1575 {
1576 BOOL fBottomInPaint = ( (rclLine.yBottom >= prcl2Paint->yBottom)
1577 && (rclLine.yBottom <= prcl2Paint->yTop)
1578 );
1579 BOOL fTopInPaint = ( (rclLine.yTop >= prcl2Paint->yBottom)
1580 && (rclLine.yTop <= prcl2Paint->yTop)
1581 );
1582
1583 if ((fBottomInPaint) && (fTopInPaint))
1584 // both in update rect:
1585 fPaintThis = TRUE;
1586 else
1587 if (fPaintHalfLines)
1588 {
1589 if ((fBottomInPaint) || (fTopInPaint))
1590 // only one in update rect:
1591 fPaintThis = TRUE;
1592 else
1593 // now, for very small update rectangles,
1594 // especially with slow scrolling,
1595 // we can have the case that the paint rectangle
1596 // is only a few pixels high so that the top of
1597 // the line is above the repaint, and the bottom
1598 // of the line is below it!
1599 if ( (rclLine.yTop >= prcl2Paint->yTop)
1600 && (rclLine.yBottom <= prcl2Paint->yBottom)
1601 )
1602 fPaintThis = TRUE;
1603 }
1604 }
1605
1606 if (fPaintThis)
1607 {
1608 // rectangle invalid: paint this rectangle
1609 // by going thru the member words
1610 PLISTNODE pWordNode = lstQueryFirstNode(&pLineRcl->llWords);
1611
1612 POINTL ptlStart;
1613
1614 while (pWordNode)
1615 {
1616 PTXVWORD pWordThis = (PTXVWORD)pWordNode->pItemData;
1617 ULONG flChar = pWordThis->flChar;
1618
1619 if (pWordThis->pcszLinkTarget) // V0.9.20 (2002-08-10) [umoeller]
1620 flChar |= CHS_UNDERSCORE;
1621
1622 // x start: this word's X coordinate
1623 ptlStart.x = pWordThis->lX - lViewXOfs;
1624 // y start: bottom line of rectangle plus highest
1625 // base line offset found in all words (format step 2)
1626 ptlStart.y = rclLine.yBottom + pLineRcl->ulMaxBaseLineOfs;
1627 // pWordThis->ulBaseLineOfs;
1628
1629 // set font for subsequent calculations,
1630 // if changed (this includes the first call)
1631 if (lcidLast != pWordThis->lcid)
1632 {
1633 GpiSetCharSet(hps, pWordThis->lcid);
1634 lcidLast = pWordThis->lcid;
1635 // force recalc of point size
1636 lPointSizeLast = -99;
1637 }
1638
1639 if (lPointSizeLast != pWordThis->lPointSize)
1640 {
1641 if (pWordThis->lPointSize)
1642 // is outline font:
1643 gpihSetPointSize(hps, pWordThis->lPointSize);
1644 lPointSizeLast = pWordThis->lPointSize;
1645 }
1646
1647 if (!pWordThis->cEscapeCode)
1648 // regular word:
1649 gpihCharStringPosAt(hps,
1650 &ptlStart,
1651 &rclLine,
1652 flChar,
1653 pWordThis->cChars,
1654 (PSZ)pWordThis->pStart);
1655 else
1656 {
1657 // check escape code
1658 switch (pWordThis->cEscapeCode)
1659 {
1660 case 0x23:
1661 // escape to be painted:
1662 DrawListMarker(hps,
1663 &rclLine,
1664 pWordThis,
1665 lViewXOfs);
1666 break;
1667 }
1668 }
1669
1670 // ptlStart.x += pWordThis->ulCXWithSpaces;
1671
1672 fAnyLinesPainted = TRUE;
1673 pWordNode = pWordNode->pNext;
1674 }
1675
1676 /* {
1677 LONG lColor = GpiQueryColor(hps);
1678 POINTL ptl2;
1679 GpiSetColor(hps, RGBCOL_RED);
1680 ptl2.x = rclLine.xLeft;
1681 ptl2.y = rclLine.yBottom;
1682 GpiMove(hps, &ptl2);
1683 ptl2.x = rclLine.xRight;
1684 ptl2.y = rclLine.yTop;
1685 GpiBox(hps,
1686 DRO_OUTLINE,
1687 &ptl2,
1688 0, 0);
1689 GpiSetColor(hps, lColor);
1690 } */
1691
1692 }
1693 else
1694 {
1695 // this line is no longer fully visible:
1696
1697 if (fAnyLinesPainted)
1698 {
1699 // we had painted lines already:
1700 // this means that all the following lines are
1701 // too far below the window, so quit
1702 /* if (pmpf)
1703 _Pmpf(("Quitting with line %d (xL = %d yB = %d)",
1704 ulCurrentLineIndex, rclLine.xLeft, rclLine.yBottom)); */
1705
1706 *pulLineIndex = ulCurrentLineIndex;
1707 if (pRectNode->pNext)
1708 {
1709 // another line to paint:
1710 PTXVRECTANGLE pLineRcl2 = (PTXVRECTANGLE)pRectNode->pNext->pItemData;
1711 // return TRUE
1712 brc = TRUE;
1713 // and set *pulViewYOfs to the top of
1714 // the next line, which wasn't visible
1715 // on the page any more
1716 *pulViewYOfs = pLineRcl2->rcl.yTop + *pulViewYOfs;
1717 }
1718 break;
1719 }
1720 // else no lines painted yet:
1721 // go for next node, because we're still above the visible window
1722 }
1723
1724 // next line
1725 pRectNode = pRectNode->pNext;
1726 // raise index to return
1727 ulCurrentLineIndex++;
1728 }
1729
1730 if (!fAnyLinesPainted)
1731 brc = FALSE;
1732
1733 return brc;
1734}
1735
1736/*
1737 *@@ txvFindWordFromPoint:
1738 * returns the list node of the word under the
1739 * given point. The list node is from the global
1740 * words list in pxfd.
1741 *
1742 *@@added V0.9.3 (2000-05-18) [umoeller]
1743 */
1744
1745PLISTNODE txvFindWordFromPoint(PXFORMATDATA pxfd,
1746 PPOINTL pptl)
1747{
1748 PLISTNODE pWordNodeFound = NULL;
1749
1750 PLISTNODE pRectangleNode = lstQueryFirstNode(&pxfd->llRectangles);
1751 while ((pRectangleNode) && (!pWordNodeFound))
1752 {
1753 PTXVRECTANGLE prclThis = (PTXVRECTANGLE)pRectangleNode->pItemData;
1754 if ( (pptl->x >= prclThis->rcl.xLeft)
1755 && (pptl->x <= prclThis->rcl.xRight)
1756 && (pptl->y >= prclThis->rcl.yBottom)
1757 && (pptl->y <= prclThis->rcl.yTop)
1758 )
1759 {
1760 // cool, we found the rectangle:
1761 // now go thru the words in this rectangle
1762 PLISTNODE pWordNode = lstQueryFirstNode(&prclThis->llWords);
1763 while (pWordNode)
1764 {
1765 PTXVWORD pWordThis = (PTXVWORD)pWordNode->pItemData;
1766
1767 if ( (pptl->x >= pWordThis->lX)
1768 && (pptl->x <= pWordThis->lX + pWordThis->ulCXWithSpaces)
1769 )
1770 {
1771 pWordNodeFound = pWordNode;
1772 break;
1773 }
1774 pWordNode = pWordNode->pNext;
1775 }
1776 }
1777 pRectangleNode = pRectangleNode->pNext;
1778 }
1779
1780 return pWordNodeFound;
1781}
1782
1783/*
1784 *@@ txvFindWordFromAnchor:
1785 * returns the list node from the global words list
1786 * BEFORE the word which represents the escape sequence
1787 * containing the specified anchor name.
1788 *
1789 *@@added V0.9.4 (2000-06-12) [umoeller]
1790 */
1791
1792PLISTNODE txvFindWordFromAnchor(PXFORMATDATA pxfd,
1793 const char *pszAnchorName)
1794{
1795 PLISTNODE pNodeFound = NULL;
1796
1797 ULONG cbAnchorName = strlen(pszAnchorName);
1798
1799 PLISTNODE pWordNode = lstQueryFirstNode(&pxfd->llWords);
1800 while ((pWordNode) && (!pNodeFound))
1801 {
1802 PTXVWORD pWordThis = (PTXVWORD)pWordNode->pItemData;
1803 if (pWordThis->cEscapeCode == 7)
1804 {
1805 // this word is an anchor escape sequence:
1806 if (strnicmp(pszAnchorName, (pWordThis->pStart + 2), cbAnchorName) == 0)
1807 {
1808 // matches: check length
1809 if (*(pWordThis->pStart + 2 + cbAnchorName) == (char)0xFF)
1810 // OK:
1811 pNodeFound = pWordNode;
1812 }
1813 }
1814
1815 pWordNode = pWordNode ->pNext;
1816 }
1817
1818 if (pNodeFound)
1819 {
1820 // anchor found:
1821 // go backwords in word list until we find a "real" word
1822 // which is no escape sequence
1823 while (pNodeFound)
1824 {
1825 PTXVWORD pWordThis = (PTXVWORD)pNodeFound->pItemData;
1826 if (pWordThis->cEscapeCode)
1827 pNodeFound = pNodeFound->pPrevious;
1828 else
1829 break;
1830 }
1831 }
1832
1833 return pNodeFound;
1834}
1835
1836/* ******************************************************************
1837 *
1838 * Window-dependent functions
1839 *
1840 ********************************************************************/
1841
1842#define QWL_PRIVATE 4 // V0.9.20 (2002-08-10) [umoeller]
1843
1844/*
1845 *@@ TEXTVIEWWINDATA:
1846 * view control-internal structure, stored in
1847 * QWL_PRIVATE at fnwpTextView.
1848 * This is device-dependent on the text view
1849 * window.
1850 */
1851
1852typedef struct _TEXTVIEWWINDATA
1853{
1854 HAB hab; // anchor block (for speed)
1855
1856 HDC hdc;
1857 HPS hps;
1858
1859 ULONG flStyle; // window style flags copied on WM_CREATE
1860 // V0.9.20 (2002-08-10) [umoeller]
1861
1862 LONG lBackColor,
1863 lForeColor;
1864
1865 XTEXTVIEWCDATA cdata; // control data, as passed to WM_CREATE
1866
1867 XFORMATDATA xfd;
1868
1869 HWND hwndVScroll, // vertical scroll bar
1870 hwndHScroll; // horizontal scroll bar
1871
1872 BOOL fVScrollVisible, // TRUE if vscroll is currently used
1873 fHScrollVisible; // TRUE if hscroll is currently used
1874
1875 RECTL rclViewReal, // window rect as returned by WinQueryWindowRect
1876 // (top right point is inclusive!)
1877 rclViewPaint, // same as rclViewReal, but excluding scroll bars
1878 rclViewText; // same as rclViewPaint, but excluding cdata borders
1879
1880 ULONG ulViewXOfs, // pixels that we have scrolled to the RIGHT; 0 means very left
1881 ulViewYOfs; // pixels that we have scrolled to the BOTTOM; 0 means very top
1882
1883 BOOL fAcceptsPresParamsNow; // TRUE after first WM_PAINT
1884
1885 // anchor clicking
1886 PLISTNODE pWordNodeFirstInAnchor; // points to first word which belongs to anchor
1887 // USHORT usLastAnchorClicked; // last anchor which was clicked (1-0xFFFF)
1888 PCSZ pcszLastLinkClicked; // last link that was clicked (points into llLinks)
1889 // V0.9.20 (2002-08-10) [umoeller]
1890
1891} TEXTVIEWWINDATA, *PTEXTVIEWWINDATA;
1892
1893/*
1894 *@@ UpdateTextViewPresData:
1895 * called from WM_CREATE and WM_PRESPARAMCHANGED
1896 * in fnwpTextView to update the TEXTVIEWWINDATA
1897 * from the window's presparams. This calls
1898 * txvSetDefaultFormat in turn.
1899 */
1900
1901STATIC VOID UpdateTextViewPresData(HWND hwndTextView,
1902 PTEXTVIEWWINDATA ptxvd)
1903{
1904 PSZ pszFont;
1905 ptxvd->lBackColor = winhQueryPresColor(hwndTextView,
1906 PP_BACKGROUNDCOLOR,
1907 FALSE, // no inherit
1908 SYSCLR_DIALOGBACKGROUND);
1909 ptxvd->lForeColor = winhQueryPresColor(hwndTextView,
1910 PP_FOREGROUNDCOLOR,
1911 FALSE, // no inherit
1912 SYSCLR_WINDOWSTATICTEXT);
1913
1914 if ((pszFont = winhQueryWindowFont(hwndTextView)))
1915 {
1916 ULONG ulSize;
1917 PSZ pszFaceName;
1918 // _Pmpf(("font: %s", pszFont));
1919 if (gpihSplitPresFont(pszFont,
1920 &ulSize,
1921 &pszFaceName))
1922 {
1923 SetFormatFont(ptxvd->hps,
1924 &ptxvd->xfd.fmtcStandard,
1925 ulSize,
1926 pszFaceName);
1927 }
1928 free(pszFont);
1929 }
1930}
1931
1932/*
1933 *@@ AdjustViewRects:
1934 * updates the internal size-dependent structures
1935 * and positions the scroll bars.
1936 *
1937 * This is device-dependent for the text view
1938 * control and must be called before FormatText2Screen
1939 * so that the view rectangles get calculated right.
1940 *
1941 * Required input in TEXTVIEWWINDATA:
1942 *
1943 * -- rclViewReal: the actual window dimensions.
1944 *
1945 * -- cdata: control data.
1946 *
1947 * Output from this function in TEXTVIEWWINDATA:
1948 *
1949 * -- rclViewPaint: the paint subrectangle (which
1950 * is rclViewReal minus scrollbars, if any).
1951 *
1952 * -- rclViewText: the text subrectangle (which
1953 * is rclViewPaint minus borders).
1954 */
1955
1956STATIC VOID AdjustViewRects(HWND hwndTextView,
1957 PTEXTVIEWWINDATA ptxvd)
1958{
1959 ULONG ulScrollCX = WinQuerySysValue(HWND_DESKTOP, SV_CXVSCROLL),
1960 ulScrollCY = WinQuerySysValue(HWND_DESKTOP, SV_CYHSCROLL),
1961 ulOfs;
1962
1963 // calculate rclViewPaint:
1964 // 1) left
1965 ptxvd->rclViewPaint.xLeft = ptxvd->rclViewReal.xLeft;
1966 // 2) bottom
1967 ptxvd->rclViewPaint.yBottom = ptxvd->rclViewReal.yBottom;
1968 if (ptxvd->fHScrollVisible)
1969 // if we have a horizontal scroll bar at the bottom,
1970 // raise bottom by its height
1971 ptxvd->rclViewPaint.yBottom += ulScrollCY;
1972 // 3) right
1973 ptxvd->rclViewPaint.xRight = ptxvd->rclViewReal.xRight;
1974 if (ptxvd->fVScrollVisible)
1975 // if we have a vertical scroll bar at the right,
1976 // subtract its width from the right
1977 ptxvd->rclViewPaint.xRight -= ulScrollCX;
1978 ptxvd->rclViewPaint.yTop = ptxvd->rclViewReal.yTop;
1979
1980 // calculate rclViewText from that
1981 ptxvd->rclViewText.xLeft = ptxvd->rclViewPaint.xLeft + ptxvd->cdata.ulXBorder;
1982 ptxvd->rclViewText.yBottom = ptxvd->rclViewPaint.yBottom + ptxvd->cdata.ulYBorder;
1983 ptxvd->rclViewText.xRight = ptxvd->rclViewPaint.xRight - ptxvd->cdata.ulXBorder;
1984 ptxvd->rclViewText.yTop = ptxvd->rclViewPaint.yTop - ptxvd->cdata.ulXBorder;
1985
1986 // now reposition scroll bars; their sizes may change
1987 // if either the vertical or horizontal scroll bar has
1988 // popped up or been hidden
1989 if (ptxvd->flStyle & XS_VSCROLL)
1990 {
1991 // vertical scroll bar enabled:
1992 ulOfs = 0;
1993 if (ptxvd->fHScrollVisible)
1994 ulOfs = ulScrollCX;
1995 WinSetWindowPos(ptxvd->hwndVScroll,
1996 HWND_TOP,
1997 ptxvd->rclViewReal.xRight - ulScrollCX,
1998 ulOfs, // y
1999 ulScrollCX, // cx
2000 ptxvd->rclViewReal.yTop - ulOfs, // cy
2001 SWP_MOVE | SWP_SIZE);
2002 }
2003
2004 if (ptxvd->flStyle & XS_HSCROLL)
2005 {
2006 ulOfs = 0;
2007 if (ptxvd->fVScrollVisible)
2008 ulOfs = ulScrollCX;
2009 WinSetWindowPos(ptxvd->hwndHScroll,
2010 HWND_TOP,
2011 0,
2012 0,
2013 ptxvd->rclViewReal.xRight - ulOfs, // cx
2014 ulScrollCY, // cy
2015 SWP_MOVE | SWP_SIZE);
2016 }
2017}
2018
2019/*
2020 *@@ FormatText2Screen:
2021 * device-dependent version of text formatting
2022 * for the text view window. This calls txvFormatText
2023 * in turn and updates the view's scroll bars.
2024 *
2025 *@@changed V0.9.3 (2000-05-05) [umoeller]: fixed buggy vertical scroll bars
2026 */
2027
2028STATIC VOID FormatText2Screen(HWND hwndTextView,
2029 PTEXTVIEWWINDATA ptxvd,
2030 BOOL fAlreadyRecursing, // in: set this to FALSE when calling
2031 BOOL fFullRecalc)
2032{
2033 ULONG ulWinCX,
2034 ulWinCY;
2035
2036 // call device-independent formatter with the
2037 // window presentation space
2038 txvFormatText(ptxvd->hps,
2039 &ptxvd->xfd,
2040 &ptxvd->rclViewText,
2041 fFullRecalc);
2042
2043 ulWinCY = (ptxvd->rclViewText.yTop - ptxvd->rclViewText.yBottom);
2044
2045 if (ptxvd->ulViewYOfs < 0)
2046 ptxvd->ulViewYOfs = 0;
2047 if (ptxvd->ulViewYOfs > ((LONG)ptxvd->xfd.szlWorkspace.cy - ulWinCY))
2048 ptxvd->ulViewYOfs = (LONG)ptxvd->xfd.szlWorkspace.cy - ulWinCY;
2049
2050 // vertical scroll bar enabled at all?
2051 if (ptxvd->flStyle & XS_VSCROLL)
2052 {
2053 BOOL fEnabled = winhUpdateScrollBar(ptxvd->hwndVScroll,
2054 ulWinCY,
2055 ptxvd->xfd.szlWorkspace.cy,
2056 ptxvd->ulViewYOfs,
2057 (ptxvd->flStyle & XS_AUTOVHIDE));
2058 // is auto-hide on?
2059 if (ptxvd->flStyle & XS_AUTOVHIDE)
2060 {
2061 // yes, auto-hide on: did visibility change?
2062 if (fEnabled != ptxvd->fVScrollVisible)
2063 // visibility changed:
2064 // if we're not already recursing,
2065 // force calling ourselves again
2066 if (!fAlreadyRecursing)
2067 {
2068 ptxvd->fVScrollVisible = fEnabled;
2069 AdjustViewRects(hwndTextView,
2070 ptxvd);
2071 FormatText2Screen(hwndTextView,
2072 ptxvd,
2073 TRUE, // fAlreadyRecursing
2074 FALSE); // quick format
2075 }
2076 }
2077 }
2078
2079 ulWinCX = (ptxvd->rclViewText.xRight - ptxvd->rclViewText.xLeft);
2080
2081 // horizontal scroll bar enabled at all?
2082 if (ptxvd->flStyle & XS_HSCROLL)
2083 {
2084 BOOL fEnabled = winhUpdateScrollBar(ptxvd->hwndHScroll,
2085 ulWinCX,
2086 ptxvd->xfd.szlWorkspace.cx,
2087 ptxvd->ulViewXOfs,
2088 (ptxvd->flStyle & XS_AUTOHHIDE));
2089 // is auto-hide on?
2090 if (ptxvd->flStyle & XS_AUTOHHIDE)
2091 {
2092 // yes, auto-hide on: did visibility change?
2093 if (fEnabled != ptxvd->fHScrollVisible)
2094 // visibility changed:
2095 // if we're not already recursing,
2096 // force calling ourselves again (at the bottom)
2097 if (!fAlreadyRecursing)
2098 {
2099 ptxvd->fHScrollVisible = fEnabled;
2100 AdjustViewRects(hwndTextView,
2101 ptxvd);
2102 }
2103 }
2104 }
2105
2106 WinInvalidateRect(hwndTextView, NULL, FALSE);
2107}
2108
2109/*
2110 *@@ SetWindowText:
2111 * implementation for WM_SETWINDOWPARAMS and
2112 * also WM_CREATE to set the window text.
2113 *
2114 *@@added V0.9.20 (2002-08-10) [umoeller]
2115 */
2116
2117VOID SetWindowText(HWND hwndTextView,
2118 PTEXTVIEWWINDATA ptxvd,
2119 PCSZ pcszText)
2120{
2121 if (pcszText && *pcszText)
2122 {
2123 PXSTRING pstr = &ptxvd->xfd.strViewText;
2124 PSZ p;
2125
2126 switch (ptxvd->flStyle & XS_FORMAT_MASK)
2127 {
2128 case XS_PLAINTEXT: // 0x0100
2129 xstrcpy(pstr,
2130 pcszText,
2131 0);
2132 xstrConvertLineFormat(pstr,
2133 CRLF2LF);
2134 p = pstr->psz;
2135 while (p = strchr(p, '\xFF'))
2136 *p = ' ';
2137 break;
2138
2139 case XS_HTML: // 0x0200
2140 if (p = strdup(pcszText))
2141 {
2142 PSZ p2 = p;
2143 while (p2 = strchr(p2, '\xFF'))
2144 *p2 = ' ';
2145 txvConvertFromHTML(&p, NULL, NULL, NULL);
2146 xstrset(pstr, p);
2147 xstrConvertLineFormat(pstr,
2148 CRLF2LF);
2149 }
2150 break;
2151
2152 default: // case XS_PREFORMATTED: // 0x0000
2153 // no conversion (default)
2154 xstrcpy(pstr,
2155 pcszText,
2156 0);
2157 break;
2158 }
2159
2160 // if the last character of the window text is not "\n",
2161 // add it explicitly here, or our lines processing
2162 // is being funny
2163 // V0.9.20 (2002-08-10) [umoeller]
2164 if (pstr->psz[pstr->ulLength - 1] != '\n')
2165 xstrcatc(pstr, '\n');
2166
2167 ptxvd->ulViewXOfs = 0;
2168 ptxvd->ulViewYOfs = 0;
2169 AdjustViewRects(hwndTextView,
2170 ptxvd);
2171 FormatText2Screen(hwndTextView,
2172 ptxvd,
2173 FALSE,
2174 TRUE); // full format
2175 }
2176}
2177
2178/*
2179 *@@ PaintViewText2Screen:
2180 * device-dependent version of text painting
2181 * for the text view window. This calls txvPaintText
2182 * in turn and updates the view's scroll bars.
2183 */
2184
2185STATIC VOID PaintViewText2Screen(PTEXTVIEWWINDATA ptxvd,
2186 PRECTL prcl2Paint) // in: invalid rectangle, can be NULL == paint all
2187{
2188 ULONG ulLineIndex = 0;
2189 ULONG ulYOfs = ptxvd->ulViewYOfs;
2190 txvPaintText(ptxvd->hab,
2191 ptxvd->hps, // paint PS: screen
2192 &ptxvd->xfd, // formatting data
2193 prcl2Paint, // update rectangle given to us
2194 ptxvd->ulViewXOfs, // current X scrolling offset
2195 &ulYOfs, // current Y scrolling offset
2196 TRUE, // draw even partly visible lines
2197 &ulLineIndex);
2198}
2199
2200/*
2201 *@@ PaintViewFocus:
2202 * paint a focus rectangle.
2203 */
2204
2205STATIC VOID PaintViewFocus(HPS hps,
2206 PTEXTVIEWWINDATA ptxvd,
2207 BOOL fFocus)
2208{
2209 POINTL ptl;
2210 HRGN hrgn;
2211 GpiSetClipRegion(hps,
2212 NULLHANDLE,
2213 &hrgn);
2214 GpiSetColor(hps,
2215 (fFocus)
2216 ? WinQuerySysColor(HWND_DESKTOP, SYSCLR_HILITEBACKGROUND, 0)
2217 : ptxvd->lBackColor);
2218 GpiSetLineType(hps, LINETYPE_DOT);
2219 ptl.x = ptxvd->rclViewPaint.xLeft;
2220 ptl.y = ptxvd->rclViewPaint.yBottom;
2221 GpiMove(hps, &ptl);
2222 ptl.x = ptxvd->rclViewPaint.xRight - 1;
2223 ptl.y = ptxvd->rclViewPaint.yTop - 1;
2224 GpiBox(hps,
2225 DRO_OUTLINE,
2226 &ptl,
2227 0, 0);
2228}
2229
2230/*
2231 *@@ RepaintWord:
2232 *
2233 *@@added V0.9.3 (2000-05-18) [umoeller]
2234 */
2235
2236STATIC VOID RepaintWord(PTEXTVIEWWINDATA ptxvd,
2237 PTXVWORD pWordThis,
2238 LONG lColor)
2239{
2240 POINTL ptlStart;
2241 ULONG flChar = pWordThis->flChar;
2242 PTXVRECTANGLE pLineRcl = pWordThis->pRectangle;
2243
2244 RECTL rclLine;
2245 rclLine.xLeft = pLineRcl->rcl.xLeft - ptxvd->ulViewXOfs;
2246 rclLine.xRight = pLineRcl->rcl.xRight - ptxvd->ulViewXOfs;
2247 rclLine.yBottom = pLineRcl->rcl.yBottom + ptxvd->ulViewYOfs;
2248 rclLine.yTop = pLineRcl->rcl.yTop + ptxvd->ulViewYOfs;
2249
2250 if (pWordThis->pcszLinkTarget)
2251 flChar |= CHS_UNDERSCORE;
2252
2253 // x start: this word's X coordinate
2254 ptlStart.x = pWordThis->lX - ptxvd->ulViewXOfs;
2255 // y start: bottom line of rectangle plus highest
2256 // base line offset found in all words (format step 2)
2257 ptlStart.y = rclLine.yBottom + pLineRcl->ulMaxBaseLineOfs;
2258 // pWordThis->ulBaseLineOfs;
2259
2260 GpiSetCharSet(ptxvd->hps, pWordThis->lcid);
2261 if (pWordThis->lPointSize)
2262 // is outline font:
2263 gpihSetPointSize(ptxvd->hps, pWordThis->lPointSize);
2264
2265 GpiSetColor(ptxvd->hps,
2266 lColor);
2267
2268 if (!pWordThis->cEscapeCode)
2269 {
2270 gpihCharStringPosAt(ptxvd->hps,
2271 &ptlStart,
2272 &rclLine,
2273 flChar,
2274 pWordThis->cChars,
2275 (PSZ)pWordThis->pStart);
2276 }
2277 else
2278 // escape to be painted:
2279 DrawListMarker(ptxvd->hps,
2280 &rclLine,
2281 pWordThis,
2282 ptxvd->ulViewXOfs);
2283}
2284
2285/*
2286 *@@ RepaintAnchor:
2287 *
2288 *@@added V0.9.3 (2000-05-18) [umoeller]
2289 */
2290
2291STATIC VOID RepaintAnchor(PTEXTVIEWWINDATA ptxvd,
2292 LONG lColor)
2293{
2294 PLISTNODE pNode = ptxvd->pWordNodeFirstInAnchor;
2295 PCSZ pcszLinkTarget = NULL;
2296 while (pNode)
2297 {
2298 PTXVWORD pWordThis = (PTXVWORD)pNode->pItemData;
2299 if (!pcszLinkTarget)
2300 // first loop:
2301 pcszLinkTarget = pWordThis->pcszLinkTarget;
2302 else
2303 if (pWordThis->pcszLinkTarget != pcszLinkTarget)
2304 // first word with different anchor:
2305 break;
2306
2307 RepaintWord(ptxvd,
2308 pWordThis,
2309 lColor);
2310 pNode = pNode->pNext;
2311 }
2312}
2313
2314/*
2315 *@@ ProcessCreate:
2316 * implementation for WM_CREATE in fnwpTextView.
2317 *
2318 *@@added V1.0.0 (2002-08-12) [umoeller]
2319 */
2320
2321STATIC MRESULT ProcessCreate(HWND hwndTextView, MPARAM mp1, MPARAM mp2)
2322{
2323 PXTEXTVIEWCDATA pcd = (PXTEXTVIEWCDATA)mp1;
2324 // can be NULL
2325 PCREATESTRUCT pcs = (PCREATESTRUCT)mp2;
2326
2327 MRESULT mrc = (MRESULT)TRUE; // error
2328 PTEXTVIEWWINDATA ptxvd;
2329
2330 // allocate TEXTVIEWWINDATA for QWL_PRIVATE
2331 if (ptxvd = (PTEXTVIEWWINDATA)malloc(sizeof(TEXTVIEWWINDATA)))
2332 {
2333 SIZEL szlPage = {0, 0};
2334 BOOL fShow = FALSE;
2335
2336 // query message queue
2337 HMQ hmq = WinQueryWindowULong(hwndTextView, QWL_HMQ);
2338 // get codepage of message queue
2339 ULONG ulCodepage = WinQueryCp(hmq);
2340
2341 memset(ptxvd, 0, sizeof(TEXTVIEWWINDATA));
2342 WinSetWindowPtr(hwndTextView, QWL_PRIVATE, ptxvd);
2343
2344 ptxvd->hab = WinQueryAnchorBlock(hwndTextView);
2345
2346 ptxvd->hdc = WinOpenWindowDC(hwndTextView);
2347 ptxvd->hps = GpiCreatePS(ptxvd->hab,
2348 ptxvd->hdc,
2349 &szlPage, // use same page size as device
2350 PU_PELS | GPIT_MICRO | GPIA_ASSOC);
2351
2352 // copy window style flags V0.9.20 (2002-08-10) [umoeller]
2353 ptxvd->flStyle = pcs->flStyle;
2354
2355 gpihSwitchToRGB(ptxvd->hps);
2356
2357 // set codepage; GPI defaults this to
2358 // the process codepage
2359 GpiSetCp(ptxvd->hps, ulCodepage);
2360
2361 txvInitFormat(&ptxvd->xfd);
2362
2363 // copy control data, if present
2364 if (pcd)
2365 memcpy(&ptxvd->cdata, pcd, pcd->cbData);
2366
2367 // check values which might cause null divisions
2368 if (ptxvd->cdata.ulVScrollLineUnit == 0)
2369 ptxvd->cdata.ulVScrollLineUnit = 15;
2370 if (ptxvd->cdata.ulHScrollLineUnit == 0)
2371 ptxvd->cdata.ulHScrollLineUnit = 15;
2372
2373 ptxvd->fAcceptsPresParamsNow = FALSE;
2374
2375 // copy window dimensions from CREATESTRUCT
2376 ptxvd->rclViewReal.xLeft = 0;
2377 ptxvd->rclViewReal.yBottom = 0;
2378 ptxvd->rclViewReal.xRight = pcs->cx;
2379 ptxvd->rclViewReal.yTop = pcs->cy;
2380
2381 winhCreateScrollBars(hwndTextView,
2382 &ptxvd->hwndVScroll,
2383 &ptxvd->hwndHScroll);
2384
2385 fShow = ((ptxvd->flStyle & XS_VSCROLL) != 0);
2386 WinShowWindow(ptxvd->hwndVScroll, fShow);
2387 ptxvd->fVScrollVisible = fShow;
2388
2389 fShow = ((ptxvd->flStyle & XS_HSCROLL) != 0);
2390 WinShowWindow(ptxvd->hwndHScroll, fShow);
2391 ptxvd->fHScrollVisible = fShow;
2392
2393 if (ptxvd->flStyle & XS_WORDWRAP)
2394 // word-wrapping should be enabled from the start:
2395 // V0.9.20 (2002-08-10) [umoeller]
2396 ptxvd->xfd.fmtpStandard.fWordWrap = TRUE;
2397
2398 // set "code" format
2399 SetFormatFont(ptxvd->hps,
2400 &ptxvd->xfd.fmtcCode,
2401 6,
2402 "System VIO");
2403
2404 // get colors from presparams/syscolors
2405 UpdateTextViewPresData(hwndTextView, ptxvd);
2406
2407 AdjustViewRects(hwndTextView,
2408 ptxvd);
2409
2410 if (ptxvd->flStyle & XS_HTML)
2411 {
2412 // if we're operating in HTML mode, set a
2413 // different default paragraph format to
2414 // make things prettier
2415 // V0.9.20 (2002-08-10) [umoeller]
2416 ptxvd->xfd.fmtpStandard.lSpaceBefore = 5;
2417 ptxvd->xfd.fmtpStandard.lSpaceAfter = 5;
2418 }
2419
2420 // setting the window text on window creation never
2421 // worked V0.9.20 (2002-08-10) [umoeller]
2422 if (pcs->pszText)
2423 SetWindowText(hwndTextView,
2424 ptxvd,
2425 pcs->pszText);
2426
2427 mrc = (MRESULT)FALSE; // OK
2428 }
2429
2430 return mrc;
2431}
2432
2433/*
2434 *@@ ProcessPaint:
2435 * implementation for WM_PAINT in fnwpTextView.
2436 *
2437 *@@added V1.0.0 (2002-08-12) [umoeller]
2438 */
2439
2440STATIC VOID ProcessPaint(HWND hwndTextView)
2441{
2442 PTEXTVIEWWINDATA ptxvd;
2443 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2444 {
2445 HRGN hrgnOldClip;
2446 RECTL rclClip;
2447 RECTL rcl2Update;
2448
2449 // get update rectangle
2450 WinQueryUpdateRect(hwndTextView,
2451 &rcl2Update);
2452 // since we're not using WinBeginPaint,
2453 // we must validate the update region,
2454 // or we'll get bombed with WM_PAINT msgs
2455 WinValidateRect(hwndTextView,
2456 NULL,
2457 FALSE);
2458
2459 // reset clip region to "all"
2460 GpiSetClipRegion(ptxvd->hps,
2461 NULLHANDLE,
2462 &hrgnOldClip); // out: old clip region
2463 // reduce clip region to update rectangle
2464 GpiIntersectClipRectangle(ptxvd->hps,
2465 &rcl2Update); // exclusive
2466
2467 // draw little box at the bottom right
2468 // (in between scroll bars) if we have
2469 // both vertical and horizontal scroll bars
2470 if ( (ptxvd->flStyle & (XS_VSCROLL | XS_HSCROLL))
2471 == (XS_VSCROLL | XS_HSCROLL)
2472 && (ptxvd->fVScrollVisible)
2473 && (ptxvd->fHScrollVisible)
2474 )
2475 {
2476 RECTL rclBox;
2477 rclBox.xLeft = ptxvd->rclViewPaint.xRight;
2478 rclBox.yBottom = 0;
2479 rclBox.xRight = rclBox.xLeft + WinQuerySysValue(HWND_DESKTOP, SV_CXVSCROLL);
2480 rclBox.yTop = WinQuerySysValue(HWND_DESKTOP, SV_CYHSCROLL);
2481 WinFillRect(ptxvd->hps,
2482 &rclBox,
2483 WinQuerySysColor(HWND_DESKTOP,
2484 SYSCLR_DIALOGBACKGROUND,
2485 0));
2486 }
2487
2488 // paint "view paint" rectangle white;
2489 // this can be larger than "view text"
2490 WinFillRect(ptxvd->hps,
2491 &ptxvd->rclViewPaint, // exclusive
2492 ptxvd->lBackColor);
2493
2494 // now reduce clipping rectangle to "view text" rectangle
2495 rclClip.xLeft = ptxvd->rclViewText.xLeft;
2496 rclClip.xRight = ptxvd->rclViewText.xRight - 1;
2497 rclClip.yBottom = ptxvd->rclViewText.yBottom;
2498 rclClip.yTop = ptxvd->rclViewText.yTop - 1;
2499 GpiIntersectClipRectangle(ptxvd->hps,
2500 &rclClip); // exclusive
2501 // finally, draw text lines in invalid rectangle;
2502 // this subfunction is smart enough to redraw only
2503 // the lines which intersect with rcl2Update
2504 GpiSetColor(ptxvd->hps, ptxvd->lForeColor);
2505 PaintViewText2Screen(ptxvd,
2506 &rcl2Update);
2507
2508 if ( (!(ptxvd->flStyle & XS_STATIC))
2509 // V0.9.20 (2002-08-10) [umoeller]
2510 && (WinQueryFocus(HWND_DESKTOP) == hwndTextView)
2511 )
2512 {
2513 // we have the focus:
2514 // reset clip region to "all"
2515 GpiSetClipRegion(ptxvd->hps,
2516 NULLHANDLE,
2517 &hrgnOldClip); // out: old clip region
2518 PaintViewFocus(ptxvd->hps,
2519 ptxvd,
2520 TRUE);
2521 }
2522
2523 ptxvd->fAcceptsPresParamsNow = TRUE;
2524 }
2525}
2526
2527/*
2528 *@@ ProcessPresParamChanged:
2529 * implementation for WM_PRESPARAMCHANGED in fnwpTextView.
2530 *
2531 *@@added V1.0.0 (2002-08-12) [umoeller]
2532 */
2533
2534STATIC VOID ProcessPresParamChanged(HWND hwndTextView, MPARAM mp1)
2535{
2536 PTEXTVIEWWINDATA ptxvd;
2537 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2538 {
2539 switch ((LONG)mp1)
2540 {
2541 case 0: // layout palette thing dropped
2542 case PP_BACKGROUNDCOLOR:
2543 case PP_FOREGROUNDCOLOR:
2544 case PP_FONTNAMESIZE:
2545 // re-query our presparams
2546 UpdateTextViewPresData(hwndTextView, ptxvd);
2547 }
2548
2549 if (ptxvd->fAcceptsPresParamsNow)
2550 FormatText2Screen(hwndTextView,
2551 ptxvd,
2552 FALSE,
2553 TRUE); // full reformat
2554 }
2555}
2556
2557/*
2558 *@@ ProcessSetFocus:
2559 * implementation for WM_SETFOCUS in fnwpTextView.
2560 *
2561 *@@added V1.0.0 (2002-08-12) [umoeller]
2562 */
2563
2564STATIC VOID ProcessSetFocus(HWND hwndTextView, MPARAM mp2)
2565{
2566 PTEXTVIEWWINDATA ptxvd;
2567 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2568 {
2569 if (ptxvd->flStyle & XS_STATIC)
2570 {
2571 if (mp2)
2572 {
2573 // we're receiving the focus, but shouldn't have it:
2574 // then behave like the static control does, that is,
2575 // give focus to the next window in the dialog
2576 HWND hwnd = hwndTextView,
2577 hwndStart = hwnd;
2578
2579 while (TRUE)
2580 {
2581 ULONG flStyle;
2582
2583 if (!(hwnd = WinQueryWindow(hwnd, QW_NEXT)))
2584 hwnd = WinQueryWindow(WinQueryWindow(hwndStart, QW_PARENT), QW_TOP);
2585
2586 // avoid endless looping
2587 if (hwnd == hwndStart)
2588 {
2589 if ( (hwnd = WinQueryWindow(hwnd, QW_OWNER))
2590 && (hwnd == hwndStart)
2591 )
2592 hwnd = NULLHANDLE;
2593
2594 break;
2595 }
2596
2597 if ( (flStyle = WinQueryWindowULong(hwnd, QWL_STYLE))
2598 && (flStyle & (WS_DISABLED | WS_TABSTOP | WS_VISIBLE)
2599 == (WS_TABSTOP | WS_VISIBLE))
2600 )
2601 {
2602 WinSetFocus(HWND_DESKTOP, hwnd);
2603 break;
2604 }
2605 };
2606 }
2607 }
2608 else
2609 {
2610 HPS hps = WinGetPS(hwndTextView);
2611 gpihSwitchToRGB(hps);
2612 PaintViewFocus(hps,
2613 ptxvd,
2614 (mp2 != 0));
2615 WinReleasePS(hps);
2616 }
2617 }
2618}
2619
2620/*
2621 *@@ ProcessButton1Down:
2622 * implementation for WM_BUTTON1DOWN in fnwpTextView.
2623 *
2624 *@@added V1.0.0 (2002-08-12) [umoeller]
2625 */
2626
2627STATIC MRESULT ProcessButton1Down(HWND hwndTextView, MPARAM mp1)
2628{
2629 MRESULT mrc = 0;
2630 PTEXTVIEWWINDATA ptxvd;
2631
2632 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2633 {
2634 POINTL ptlPos;
2635 PLISTNODE pWordNodeClicked;
2636
2637 ptlPos.x = SHORT1FROMMP(mp1) + ptxvd->ulViewXOfs;
2638 ptlPos.y = SHORT2FROMMP(mp1) - ptxvd->ulViewYOfs;
2639
2640 if ( (!(ptxvd->flStyle & XS_STATIC))
2641 // V0.9.20 (2002-08-10) [umoeller]
2642 && (hwndTextView != WinQueryFocus(HWND_DESKTOP))
2643 )
2644 WinSetFocus(HWND_DESKTOP, hwndTextView);
2645
2646 ptxvd->pcszLastLinkClicked = NULL;
2647
2648 if (pWordNodeClicked = txvFindWordFromPoint(&ptxvd->xfd,
2649 &ptlPos))
2650 {
2651 PTXVWORD pWordClicked = (PTXVWORD)pWordNodeClicked->pItemData;
2652
2653 // store link target (can be NULL)
2654 if (ptxvd->pcszLastLinkClicked = pWordClicked->pcszLinkTarget)
2655 {
2656 // word has a link target:
2657 PLISTNODE pNode = pWordNodeClicked;
2658
2659 // reset first word of anchor
2660 ptxvd->pWordNodeFirstInAnchor = NULL;
2661
2662 // go back to find the first word which has this anchor,
2663 // because we need to repaint them all
2664 while (pNode)
2665 {
2666 PTXVWORD pWordThis = (PTXVWORD)pNode->pItemData;
2667 if (pWordThis->pcszLinkTarget == pWordClicked->pcszLinkTarget)
2668 {
2669 // still has same anchor:
2670 // go for previous
2671 ptxvd->pWordNodeFirstInAnchor = pNode;
2672 pNode = pNode->pPrevious;
2673 }
2674 else
2675 // different anchor:
2676 // pNodeFirst points to first node with same anchor now
2677 break;
2678 }
2679
2680 RepaintAnchor(ptxvd,
2681 RGBCOL_RED);
2682 }
2683 }
2684
2685 WinSetCapture(HWND_DESKTOP, hwndTextView);
2686 mrc = (MRESULT)TRUE;
2687 }
2688
2689 return mrc;
2690}
2691
2692/*
2693 *@@ ProcessButton1Up:
2694 * implementation for WM_BUTTON1UP in fnwpTextView.
2695 *
2696 *@@added V1.0.0 (2002-08-12) [umoeller]
2697 */
2698
2699STATIC MRESULT ProcessButton1Up(HWND hwndTextView, MPARAM mp1)
2700{
2701 MRESULT mrc = 0;
2702 PTEXTVIEWWINDATA ptxvd;
2703
2704 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2705 {
2706 POINTL ptlPos;
2707 HWND hwndOwner = NULLHANDLE;
2708
2709 ptlPos.x = SHORT1FROMMP(mp1) + ptxvd->ulViewXOfs;
2710 ptlPos.y = SHORT2FROMMP(mp1) - ptxvd->ulViewYOfs;
2711 WinSetCapture(HWND_DESKTOP, NULLHANDLE);
2712
2713 if (ptxvd->pcszLastLinkClicked)
2714 {
2715 RepaintAnchor(ptxvd,
2716 ptxvd->lForeColor);
2717
2718 // nofify owner
2719 if (hwndOwner = WinQueryWindow(hwndTextView, QW_OWNER))
2720 WinPostMsg(hwndOwner,
2721 WM_CONTROL,
2722 MPFROM2SHORT(WinQueryWindowUShort(hwndTextView,
2723 QWS_ID),
2724 TXVN_LINK),
2725 (MPARAM)(ULONG)(ptxvd->pcszLastLinkClicked));
2726 }
2727
2728 mrc = (MRESULT)TRUE;
2729 }
2730
2731 return mrc;
2732}
2733
2734/*
2735 *@@ ProcessChar:
2736 * implementation for WM_CHAR in fnwpTextView.
2737 *
2738 *@@added V1.0.0 (2002-08-12) [umoeller]
2739 */
2740
2741STATIC MRESULT ProcessChar(HWND hwndTextView, MPARAM mp1, MPARAM mp2)
2742{
2743 MRESULT mrc = 0;
2744 PTEXTVIEWWINDATA ptxvd;
2745
2746 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2747 {
2748 BOOL fDefProc = TRUE;
2749 USHORT usFlags = SHORT1FROMMP(mp1);
2750 // USHORT usch = SHORT1FROMMP(mp2);
2751 USHORT usvk = SHORT2FROMMP(mp2);
2752
2753 if (usFlags & KC_VIRTUALKEY)
2754 {
2755 ULONG ulMsg = 0;
2756 USHORT usID = ID_VSCROLL;
2757 SHORT sPos = 0;
2758 SHORT usCmd = 0;
2759 fDefProc = FALSE;
2760
2761 switch (usvk)
2762 {
2763 case VK_UP:
2764 ulMsg = WM_VSCROLL;
2765 usCmd = SB_LINEUP;
2766 break;
2767
2768 case VK_DOWN:
2769 ulMsg = WM_VSCROLL;
2770 usCmd = SB_LINEDOWN;
2771 break;
2772
2773 case VK_RIGHT:
2774 ulMsg = WM_HSCROLL;
2775 usCmd = SB_LINERIGHT;
2776 break;
2777
2778 case VK_LEFT:
2779 ulMsg = WM_HSCROLL;
2780 usCmd = SB_LINELEFT;
2781 break;
2782
2783 case VK_PAGEUP:
2784 ulMsg = WM_VSCROLL;
2785 if (usFlags & KC_CTRL)
2786 {
2787 sPos = 0;
2788 usCmd = SB_SLIDERPOSITION;
2789 }
2790 else
2791 usCmd = SB_PAGEUP;
2792 break;
2793
2794 case VK_PAGEDOWN:
2795 ulMsg = WM_VSCROLL;
2796 if (usFlags & KC_CTRL)
2797 {
2798 sPos = ptxvd->xfd.szlWorkspace.cy;
2799 usCmd = SB_SLIDERPOSITION;
2800 }
2801 else
2802 usCmd = SB_PAGEDOWN;
2803 break;
2804
2805 case VK_HOME:
2806 if (usFlags & KC_CTRL)
2807 // vertical:
2808 ulMsg = WM_VSCROLL;
2809 else
2810 ulMsg = WM_HSCROLL;
2811
2812 sPos = 0;
2813 usCmd = SB_SLIDERPOSITION;
2814 break;
2815
2816 case VK_END:
2817 if (usFlags & KC_CTRL)
2818 {
2819 // vertical:
2820 ulMsg = WM_VSCROLL;
2821 sPos = ptxvd->xfd.szlWorkspace.cy;
2822 }
2823 else
2824 {
2825 ulMsg = WM_HSCROLL;
2826 sPos = ptxvd->xfd.szlWorkspace.cx;
2827 }
2828
2829 usCmd = SB_SLIDERPOSITION;
2830 break;
2831
2832 default:
2833 // other:
2834 fDefProc = TRUE;
2835 }
2836
2837 if ( ((usFlags & KC_KEYUP) == 0)
2838 && (ulMsg)
2839 )
2840 WinSendMsg(hwndTextView,
2841 ulMsg,
2842 MPFROMSHORT(usID),
2843 MPFROM2SHORT(sPos,
2844 usCmd));
2845 }
2846
2847 if (fDefProc)
2848 mrc = WinDefWindowProc(hwndTextView, WM_CHAR, mp1, mp2);
2849 // sends to owner
2850 else
2851 mrc = (MPARAM)TRUE;
2852 }
2853
2854 return mrc;
2855}
2856
2857/*
2858 *@@ ProcessJumpToAnchorName:
2859 * implementation for TXM_JUMPTOANCHORNAME in fnwpTextView.
2860 *
2861 *@@added V1.0.0 (2002-08-12) [umoeller]
2862 */
2863
2864STATIC MRESULT ProcessJumpToAnchorName(HWND hwndTextView, MPARAM mp1)
2865{
2866 MRESULT mrc = 0;
2867 PTEXTVIEWWINDATA ptxvd;
2868
2869 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2870 && (mp1)
2871 )
2872 {
2873 PLISTNODE pWordNode;
2874 PTXVWORD pWord;
2875 if ( (pWordNode = txvFindWordFromAnchor(&ptxvd->xfd,
2876 (const char*)mp1))
2877 && (pWord = (PTXVWORD)pWordNode->pItemData)
2878 )
2879 {
2880 // found:
2881 PTXVRECTANGLE pRect = pWord->pRectangle;
2882 ULONG ulWinCY = (ptxvd->rclViewText.yTop - ptxvd->rclViewText.yBottom);
2883
2884 // now we need to scroll the window so that this rectangle is on top.
2885 // Since rectangles start out with the height of the window (e.g. +768)
2886 // and then have lower y coordinates down to way in the negatives,
2887 // to get the y offset, we must...
2888 ptxvd->ulViewYOfs = (-pRect->rcl.yTop) - ulWinCY;
2889
2890 if (ptxvd->ulViewYOfs < 0)
2891 ptxvd->ulViewYOfs = 0;
2892 if (ptxvd->ulViewYOfs > ((LONG)ptxvd->xfd.szlWorkspace.cy - ulWinCY))
2893 ptxvd->ulViewYOfs = (LONG)ptxvd->xfd.szlWorkspace.cy - ulWinCY;
2894
2895 // vertical scroll bar enabled at all?
2896 if (ptxvd->flStyle & XS_VSCROLL)
2897 {
2898 /* BOOL fEnabled = */ winhUpdateScrollBar(ptxvd->hwndVScroll,
2899 ulWinCY,
2900 ptxvd->xfd.szlWorkspace.cy,
2901 ptxvd->ulViewYOfs,
2902 (ptxvd->flStyle & XS_AUTOVHIDE));
2903 WinInvalidateRect(hwndTextView, NULL, FALSE);
2904 }
2905
2906 mrc = (MRESULT)TRUE;
2907 }
2908 }
2909
2910 return mrc;
2911}
2912
2913/*
2914 *@@ ProcessDestroy:
2915 * implementation for WM_DESTROY in fnwpTextView.
2916 *
2917 *@@added V1.0.0 (2002-08-12) [umoeller]
2918 */
2919
2920STATIC MRESULT ProcessDestroy(HWND hwndTextView, MPARAM mp1, MPARAM mp2)
2921{
2922 PTEXTVIEWWINDATA ptxvd;
2923
2924 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
2925 {
2926 xstrClear(&ptxvd->xfd.strViewText);
2927 lstClear(&ptxvd->xfd.llRectangles);
2928 lstClear(&ptxvd->xfd.llWords);
2929 GpiDestroyPS(ptxvd->hps);
2930 free(ptxvd);
2931 WinSetWindowPtr(hwndTextView, QWL_PRIVATE, NULL);
2932 }
2933
2934 return WinDefWindowProc(hwndTextView, WM_DESTROY, mp1, mp2);
2935}
2936
2937/*
2938 *@@ fnwpTextView:
2939 * window procedure for the text view control. This is
2940 * registered with the WC_XTEXTVIEW class in txvRegisterTextView.
2941 *
2942 * The text view control is not a subclassed whatever control,
2943 * but a control implemented from scratch. As a result, we
2944 * had to implement all messages which are usually recognized
2945 * by a control. In detail, we have:
2946 *
2947 * -- WM_WINDOWPOSCHANGED: if the control is resized, the
2948 * text is reformatted and the scroll bars are readjusted.
2949 * See AdjustViewRects and txvFormatText.
2950 *
2951 * -- WM_PRESPARAMCHANGED: if fonts or colors are dropped
2952 * on the control, we reformat the text also.
2953 *
2954 * -- WM_HSCROLL and WM_VSCROLL: this calls winhHandleScrollMsg
2955 * to scroll the window contents.
2956 *
2957 * -- WM_BUTTON1DOWN: this sets the focus to the control.
2958 *
2959 * -- WM_SETFOCUS: if we receive the focus, we draw a fine
2960 * dotted line in the "selection" color around the text
2961 * window.
2962 *
2963 * -- WM_CHAR: if we have the focus, the user can move the
2964 * visible part within the workspace using the usual
2965 * cursor and HOME/END keys.
2966 *
2967 * -- WM_MOUSEMOVE: this sends WM_CONTROLPOINTER to the
2968 * owner so the owner can change the mouse pointer.
2969 *
2970 * <B>Painting</B>
2971 *
2972 * The text view control creates a micro presentation space
2973 * from the window's device context upon WM_CREATE, which is
2974 * stored in TEXTVIEWWINDATA. We do not use WinBeginPaint in
2975 * WM_PAINT, but only the PS we created ourselves. This saves
2976 * us from resetting and researching all the fonts etc., which
2977 * should be speedier.
2978 *
2979 * The text view control uses a private window word for storing
2980 * its own data. The client is free to use QWL_USER of the
2981 * text view control.
2982 *
2983 *@@changed V0.9.3 (2000-05-05) [umoeller]: removed TXM_NEWTEXT; now supporting WinSetWindowText
2984 *@@changed V0.9.3 (2000-05-07) [umoeller]: crashed if create param was NULL; fixed
2985 *@@changed V0.9.20 (2002-08-10) [umoeller]: no longer using QWL_USER, which is free now
2986 *@@changed V0.9.20 (2002-08-10) [umoeller]: setting text on window creation never worked, fixed
2987 *@@changed V0.9.20 (2002-08-10) [umoeller]: added TXN_ANCHORCLICKED owner notify for anchors
2988 *@@changed V0.9.20 (2002-08-10) [umoeller]: converted private style flags to XS_* window style flags
2989 *@@changed V0.9.20 (2002-08-10) [umoeller]: added support for XS_STATIC
2990 *@@changed V0.9.20 (2002-08-10) [umoeller]: added support for formatting HTML and plain text automatically
2991 *@@changed V1.0.0 (2002-08-12) [umoeller]: optimized locality by moving big chunks into subfuncs
2992 */
2993
2994STATIC MRESULT EXPENTRY fnwpTextView(HWND hwndTextView, ULONG msg, MPARAM mp1, MPARAM mp2)
2995{
2996 MRESULT mrc = 0;
2997 PTEXTVIEWWINDATA ptxvd;
2998
2999 switch (msg)
3000 {
3001 /*
3002 * WM_CREATE:
3003 *
3004 */
3005
3006 case WM_CREATE:
3007 mrc = ProcessCreate(hwndTextView, mp1, mp2);
3008 // extracted V1.0.0 (2002-08-12) [umoeller]
3009 break;
3010
3011 /*
3012 * WM_SETWINDOWPARAMS:
3013 * this message sets the window parameters,
3014 * most importantly, the window text.
3015 *
3016 * This updates the control.
3017 */
3018
3019 case WM_SETWINDOWPARAMS:
3020 if ( (mp1)
3021 && (((PWNDPARAMS)mp1)->fsStatus & WPM_TEXT)
3022 && (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3023 )
3024 {
3025 SetWindowText(hwndTextView,
3026 ptxvd,
3027 ((PWNDPARAMS)mp1)->pszText);
3028 mrc = (MRESULT)TRUE; // was missing V0.9.20 (2002-08-10) [umoeller]
3029 }
3030 break;
3031
3032 /*
3033 * WM_WINDOWPOSCHANGED:
3034 *
3035 */
3036
3037 case WM_WINDOWPOSCHANGED:
3038 // resizing?
3039 if ( (mp1)
3040 && (((PSWP)mp1)->fl & SWP_SIZE)
3041 && (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3042 )
3043 {
3044 WinQueryWindowRect(hwndTextView,
3045 &ptxvd->rclViewReal);
3046 AdjustViewRects(hwndTextView,
3047 ptxvd);
3048 FormatText2Screen(hwndTextView,
3049 ptxvd,
3050 FALSE,
3051 FALSE); // quick format
3052 }
3053 break;
3054
3055 /*
3056 * WM_PAINT:
3057 *
3058 */
3059
3060 case WM_PAINT:
3061 ProcessPaint(hwndTextView);
3062 // extracted V1.0.0 (2002-08-12) [umoeller]
3063 break;
3064
3065 /*
3066 * WM_PRESPARAMCHANGED:
3067 *
3068 * Changing the color or font settings
3069 * is equivalent to changing the default
3070 * paragraph format. See TXM_SETFORMAT.
3071 */
3072
3073 case WM_PRESPARAMCHANGED:
3074 ProcessPresParamChanged(hwndTextView, mp1);
3075 break;
3076
3077 /*
3078 * WM_VSCROLL:
3079 *
3080 */
3081
3082 case WM_VSCROLL:
3083 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3084 && (ptxvd->fVScrollVisible)
3085 )
3086 {
3087 winhHandleScrollMsg(hwndTextView,
3088 ptxvd->hwndVScroll,
3089 &ptxvd->ulViewYOfs,
3090 &ptxvd->rclViewText,
3091 ptxvd->xfd.szlWorkspace.cy,
3092 ptxvd->cdata.ulVScrollLineUnit,
3093 msg,
3094 mp2);
3095 }
3096 break;
3097
3098 /*
3099 * WM_HSCROLL:
3100 *
3101 */
3102
3103 case WM_HSCROLL:
3104 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3105 && (ptxvd->fHScrollVisible)
3106 )
3107 {
3108 winhHandleScrollMsg(hwndTextView,
3109 ptxvd->hwndHScroll,
3110 &ptxvd->ulViewXOfs,
3111 &ptxvd->rclViewText,
3112 ptxvd->xfd.szlWorkspace.cx,
3113 ptxvd->cdata.ulHScrollLineUnit,
3114 msg,
3115 mp2);
3116 }
3117 break;
3118
3119 /*
3120 * WM_SETFOCUS:
3121 *
3122 */
3123
3124 case WM_SETFOCUS:
3125 ProcessSetFocus(hwndTextView, mp2);
3126 break;
3127
3128 /*
3129 * WM_MOUSEMOVE:
3130 * send WM_CONTROLPOINTER to owner.
3131 */
3132
3133 case WM_MOUSEMOVE:
3134 {
3135 HWND hwndOwner;
3136 if (hwndOwner = WinQueryWindow(hwndTextView, QW_OWNER))
3137 {
3138 HPOINTER hptrSet
3139 = (HPOINTER)WinSendMsg(hwndOwner,
3140 WM_CONTROLPOINTER,
3141 (MPARAM)(LONG)WinQueryWindowUShort(hwndTextView,
3142 QWS_ID),
3143 (MPARAM)WinQuerySysPointer(HWND_DESKTOP,
3144 SPTR_ARROW,
3145 FALSE));
3146 WinSetPointer(HWND_DESKTOP, hptrSet);
3147 }
3148 }
3149 break;
3150
3151 /*
3152 * WM_BUTTON1DOWN:
3153 *
3154 */
3155
3156 case WM_BUTTON1DOWN:
3157 mrc = ProcessButton1Down(hwndTextView, mp1);
3158 break;
3159
3160 /*
3161 * WM_BUTTON1UP:
3162 *
3163 */
3164
3165 case WM_BUTTON1UP:
3166 mrc = ProcessButton1Up(hwndTextView, mp1);
3167 break;
3168
3169 /*
3170 * WM_CHAR:
3171 *
3172 */
3173
3174 case WM_CHAR:
3175 mrc = ProcessChar(hwndTextView, mp1, mp2);
3176 break;
3177
3178 /*
3179 *@@ TXM_QUERYPARFORMAT:
3180 * this msg can be sent to the text view control
3181 * to retrieve the paragraph format with the
3182 * index specified in mp1.
3183 *
3184 * This must be sent, not posted, to the control.
3185 *
3186 * Parameters:
3187 *
3188 * -- ULONG mp1: index of format to query.
3189 * Must be 0 currently for the standard
3190 * paragraph format.
3191 *
3192 * -- PXFMTPARAGRAPH mp2: pointer to buffer
3193 * which is to receive the formatting
3194 * data.
3195 *
3196 * Returns TRUE if copying was successful.
3197 *
3198 *@@added V0.9.3 (2000-05-06) [umoeller]
3199 */
3200
3201 case TXM_QUERYPARFORMAT:
3202 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3203 && (!mp1)
3204 && (mp2)
3205 )
3206 {
3207 memcpy(mp2,
3208 &ptxvd->xfd.fmtpStandard,
3209 sizeof(XFMTPARAGRAPH));
3210 mrc = (MPARAM)TRUE;
3211 }
3212 break;
3213
3214 /*
3215 *@@ TXM_SETPARFORMAT:
3216 * reverse to TXM_QUERYPARFORMAT, this sets a
3217 * paragraph format (line spacings, margins
3218 * and such).
3219 *
3220 * This must be sent, not posted, to the control.
3221 *
3222 * Parameters:
3223 *
3224 * -- ULONG mp1: index of format to set.
3225 * Must be 0 currently for the standard
3226 * paragraph format.
3227 *
3228 * -- PXFMTPARAGRAPH mp2: pointer to buffer
3229 * from which to copy formatting data.
3230 * If this pointer is NULL, the format
3231 * is reset to the default.
3232 *
3233 * This reformats the control.
3234 *
3235 *@@added V0.9.3 (2000-05-06) [umoeller]
3236 */
3237
3238 case TXM_SETPARFORMAT:
3239 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3240 && (!mp1)
3241 )
3242 {
3243 if (mp2)
3244 // copy:
3245 memcpy(&ptxvd->xfd.fmtpStandard,
3246 mp2,
3247 sizeof(XFMTPARAGRAPH));
3248 else
3249 // default:
3250 memset(&ptxvd->xfd.fmtpStandard,
3251 0,
3252 sizeof(XFMTPARAGRAPH));
3253
3254 FormatText2Screen(hwndTextView,
3255 ptxvd,
3256 FALSE,
3257 TRUE); // full reformat
3258
3259 mrc = (MPARAM)TRUE;
3260 }
3261 break;
3262
3263 /*
3264 *@@ TXM_SETWORDWRAP:
3265 * this text view control msg quickly changes
3266 * the word-wrapping style of the default
3267 * paragraph formatting.
3268 *
3269 * This may be sent or posted.
3270 *
3271 * (BOOL)mp1 determines whether word wrapping
3272 * should be turned on or off.
3273 */
3274
3275 case TXM_SETWORDWRAP:
3276 if (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3277 {
3278 BOOL ulOldFlFormat = ptxvd->xfd.fmtpStandard.fWordWrap;
3279 ptxvd->xfd.fmtpStandard.fWordWrap = (BOOL)mp1;
3280 if (ptxvd->xfd.fmtpStandard.fWordWrap != ulOldFlFormat)
3281 FormatText2Screen(hwndTextView,
3282 ptxvd,
3283 FALSE,
3284 FALSE); // quick format
3285 }
3286 break;
3287
3288 /*
3289 *@@ TXM_QUERYCDATA:
3290 * copies the current XTEXTVIEWCDATA
3291 * into the specified buffer.
3292 *
3293 * This must be sent, not posted, to the control.
3294 *
3295 * Parameters:
3296 *
3297 * -- PXTEXTVIEWCDATA mp1: target buffer.
3298 * Before calling this, you MUST specify
3299 * XTEXTVIEWCDATA.cbData.
3300 *
3301 * Returns the bytes that were copied as
3302 * a ULONG.
3303 */
3304
3305 case TXM_QUERYCDATA:
3306 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3307 && (mp1)
3308 )
3309 {
3310 PXTEXTVIEWCDATA pTarget = (PXTEXTVIEWCDATA)mp1;
3311 mrc = (MRESULT)min(pTarget->cbData, sizeof(XTEXTVIEWCDATA));
3312 memcpy(pTarget,
3313 &ptxvd->cdata,
3314 (ULONG)mrc);
3315 }
3316 break;
3317
3318 /*
3319 *@@ TXM_SETCDATA:
3320 * updates the current XTEXTVIEWCDATA
3321 * with the data from the specified buffer.
3322 *
3323 * This must be sent, not posted, to the control.
3324 *
3325 * Parameters:
3326 *
3327 * -- PXTEXTVIEWCDATA mp1: source buffer.
3328 * Before calling this, you MUST specify
3329 * XTEXTVIEWCDATA.cbData.
3330 *
3331 * Returns the bytes that were copied as
3332 * a ULONG.
3333 *
3334 *@@changed V1.0.0 (2002-08-12) [umoeller]: now returning bytes
3335 */
3336
3337 case TXM_SETCDATA:
3338 if ( (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3339 && (mp1)
3340 )
3341 {
3342 PXTEXTVIEWCDATA pSource = (PXTEXTVIEWCDATA)mp1;
3343 mrc = (MRESULT)min(pSource->cbData, sizeof(XTEXTVIEWCDATA));
3344 memcpy(&ptxvd->cdata,
3345 pSource,
3346 (ULONG)mrc);
3347 }
3348 break;
3349
3350 /*
3351 *@@ TXM_JUMPTOANCHORNAME:
3352 * scrolls the XTextView control contents so that
3353 * the text marked with the specified anchor name
3354 * (TXVESC_ANCHORNAME escape) appears at the top
3355 * of the control.
3356 *
3357 * This must be sent, not posted, to the control.
3358 *
3359 * Parameters:
3360 * -- PSZ mp1: anchor name (e.g. "anchor1").
3361 *
3362 * Returns TRUE if the jump was successful.
3363 *
3364 *@@added V0.9.4 (2000-06-12) [umoeller]
3365 */
3366
3367 case TXM_JUMPTOANCHORNAME:
3368 mrc = ProcessJumpToAnchorName(hwndTextView, mp1);
3369 break;
3370
3371 /*
3372 *@@ TXM_QUERYTEXTEXTENT:
3373 * returns the extents of the currently set text,
3374 * that is, the width and height of the internal
3375 * work area, of which the current view rectangle
3376 * displays a subrectangle.
3377 *
3378 * This must be sent, not posted, to the control.
3379 *
3380 * Parameters:
3381 *
3382 * -- PSIZEL mp1: pointer to a SIZEL buffer,
3383 * which receives the extent in the cx and
3384 * cy members. These will be set to null
3385 * values if the control currently has no
3386 * text.
3387 *
3388 * Returns TRUE on success.
3389 *
3390 *@@added V0.9.20 (2002-08-10) [umoeller]
3391 */
3392
3393 case TXM_QUERYTEXTEXTENT:
3394 if ( (mp1)
3395 && (ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE))
3396 )
3397 {
3398 memcpy((PSIZEL)mp1,
3399 &ptxvd->xfd.szlWorkspace,
3400 sizeof(SIZEL));
3401 mrc = (MRESULT)TRUE;
3402 }
3403 break;
3404
3405 /*
3406 * WM_DESTROY:
3407 * clean up.
3408 */
3409
3410 case WM_DESTROY:
3411 mrc = ProcessDestroy(hwndTextView, mp1, mp2);
3412 break;
3413
3414 default:
3415 mrc = WinDefWindowProc(hwndTextView, msg, mp1, mp2);
3416 }
3417
3418 return mrc;
3419}
3420
3421/*
3422 *@@ txvRegisterTextView:
3423 * registers the Text View class with PM. Required
3424 * before the text view control can be used.
3425 */
3426
3427BOOL txvRegisterTextView(HAB hab)
3428{
3429 return WinRegisterClass(hab,
3430 WC_XTEXTVIEW,
3431 fnwpTextView,
3432 0,
3433 2 * sizeof(PVOID)); // QWL_USER and QWL_PRIVATE
3434}
3435
3436/*
3437 *@@ txvReplaceWithTextView:
3438 * replaces any window with a text view control.
3439 * You must call txvRegisterTextView beforehand.
3440 *
3441 *@@added V0.9.1 (2000-02-13) [umoeller]
3442 */
3443
3444HWND txvReplaceWithTextView(HWND hwndParentAndOwner,
3445 USHORT usID,
3446 ULONG flWinStyle,
3447 USHORT usBorder)
3448{
3449 HWND hwndMLE = WinWindowFromID(hwndParentAndOwner, usID),
3450 hwndTextView = NULLHANDLE;
3451 if (hwndMLE)
3452 {
3453 ULONG ul,
3454 // attrFound,
3455 abValue[32];
3456 SWP swpMLE;
3457 XTEXTVIEWCDATA xtxCData;
3458 PSZ pszFont = winhQueryWindowFont(hwndMLE);
3459 LONG lBackClr = -1,
3460 lForeClr = -1;
3461
3462 if ((ul = WinQueryPresParam(hwndMLE,
3463 PP_BACKGROUNDCOLOR,
3464 0,
3465 NULL,
3466 (ULONG)sizeof(abValue),
3467 (PVOID)&abValue,
3468 QPF_NOINHERIT)))
3469 lBackClr = abValue[0];
3470
3471 if ((ul = WinQueryPresParam(hwndMLE,
3472 PP_FOREGROUNDCOLOR,
3473 0,
3474 NULL,
3475 (ULONG)sizeof(abValue),
3476 (PVOID)&abValue,
3477 QPF_NOINHERIT)))
3478 lForeClr = abValue[0];
3479
3480 WinQueryWindowPos(hwndMLE, &swpMLE);
3481
3482 WinDestroyWindow(hwndMLE);
3483 memset(&xtxCData, 0, sizeof(xtxCData));
3484 xtxCData.cbData = sizeof(xtxCData);
3485 xtxCData.ulXBorder = usBorder;
3486 xtxCData.ulYBorder = usBorder;
3487 hwndTextView = WinCreateWindow(hwndParentAndOwner,
3488 WC_XTEXTVIEW,
3489 "",
3490 flWinStyle,
3491 swpMLE.x,
3492 swpMLE.y,
3493 swpMLE.cx,
3494 swpMLE.cy,
3495 hwndParentAndOwner,
3496 HWND_TOP,
3497 usID,
3498 &xtxCData,
3499 0);
3500 if (pszFont)
3501 {
3502 winhSetWindowFont(hwndTextView, pszFont);
3503 free(pszFont);
3504 }
3505
3506 if (lBackClr != -1)
3507 WinSetPresParam(hwndTextView,
3508 PP_BACKGROUNDCOLOR,
3509 sizeof(ULONG),
3510 &lBackClr);
3511 if (lForeClr != -1)
3512 WinSetPresParam(hwndTextView,
3513 PP_FOREGROUNDCOLOR,
3514 sizeof(ULONG),
3515 &lForeClr);
3516 }
3517 return hwndTextView;
3518}
3519
3520/* ******************************************************************
3521 *
3522 * Printer-dependent functions
3523 *
3524 ********************************************************************/
3525
3526/*
3527 *@@ prthQueryQueues:
3528 * returns a buffer containing all print queues
3529 * on the system.
3530 *
3531 * This is usually the first step before printing.
3532 * After calling this function, show a dlg to the
3533 * user, allow him to select the printer queue
3534 * to be used. This can then be passed to
3535 * prthCreatePrinterDC.
3536 *
3537 * Use prthFreeBuf to free the returned buffer.
3538 */
3539
3540STATIC PRQINFO3* prthEnumQueues(PULONG pulReturned) // out: no. of queues found
3541{
3542 SPLERR rc;
3543 ULONG cTotal;
3544 ULONG cbNeeded = 0;
3545 PRQINFO3 *pprq3 = NULL;
3546
3547 // count queues & get number of bytes needed for buffer
3548 rc = SplEnumQueue(NULL, // default computer
3549 3, // detail level
3550 NULL, // pbuf
3551 0L, // cbBuf
3552 pulReturned, // out: entries returned
3553 &cTotal, // out: total entries available
3554 &cbNeeded,
3555 NULL); // reserved
3556
3557 if (!rc && cbNeeded)
3558 {
3559 pprq3 = (PRQINFO3*)malloc(cbNeeded);
3560 if (pprq3)
3561 {
3562 // enum the queues
3563 rc = SplEnumQueue(NULL,
3564 3,
3565 pprq3,
3566 cbNeeded,
3567 pulReturned,
3568 &cTotal,
3569 &cbNeeded,
3570 NULL);
3571 }
3572 }
3573
3574 return pprq3;
3575}
3576
3577/*
3578 *@@ prthFreeBuf:
3579 *
3580 */
3581
3582STATIC VOID prthFreeBuf(PVOID pprq3)
3583{
3584 if (pprq3)
3585 free(pprq3);
3586}
3587
3588/*
3589 *@@ prthCreatePrinterDC:
3590 * creates a device context for the printer
3591 * specified by the given printer queue.
3592 *
3593 * As a nifty feature, this returns printer
3594 * device resolution automatically in the
3595 * specified buffer.
3596 *
3597 * Returns NULLHANDLE (== DEV_ERROR) on errors.
3598 *
3599 * Use DevCloseDC to destroy the DC.
3600 *
3601 * Based on print sample by Peter Fitzsimmons, Fri 95-09-29 02:47:16am.
3602 */
3603
3604STATIC HDC prthCreatePrinterDC(HAB hab,
3605 PRQINFO3 *pprq3,
3606 PLONG palRes) // out: 2 longs holding horizontal and vertical
3607 // printer resolution in pels per inch
3608{
3609 HDC hdc = NULLHANDLE;
3610 DEVOPENSTRUC dos;
3611 PSZ p;
3612
3613 memset(&dos, 0, sizeof(dos));
3614 p = strrchr(pprq3->pszDriverName, '.');
3615 if (p)
3616 *p = 0; // del everything after '.'
3617
3618 dos.pszLogAddress = pprq3->pszName;
3619 dos.pszDriverName = pprq3->pszDriverName;
3620 dos.pdriv = pprq3->pDriverData;
3621 dos.pszDataType = "PM_Q_STD";
3622 hdc = DevOpenDC(hab,
3623 OD_QUEUED,
3624 "*",
3625 4L, // count of items in next param
3626 (PDEVOPENDATA)&dos,
3627 0); // compatible DC
3628
3629 if (hdc)
3630 DevQueryCaps(hdc,
3631 CAPS_HORIZONTAL_FONT_RES,
3632 2,
3633 palRes); // buffer
3634
3635 return hdc;
3636}
3637
3638/*
3639 *@@ prthQueryForms:
3640 * returns a buffer containing all forms
3641 * supported by the specified printer DC.
3642 *
3643 * Use prthFreeBuf to free the returned
3644 * buffer.
3645 *
3646 * HCINFO uses different model spaces for
3647 * the returned info. See PMREF for details.
3648 */
3649
3650STATIC HCINFO* prthQueryForms(HDC hdc,
3651 PULONG pulCount)
3652{
3653 HCINFO *pahci = NULL;
3654
3655 LONG cForms;
3656
3657 // get form count
3658 cForms = DevQueryHardcopyCaps(hdc, 0L, 0L, NULL); // phci);
3659 if (cForms)
3660 {
3661 pahci = (HCINFO*)malloc(cForms * sizeof(HCINFO));
3662 if (pahci)
3663 {
3664 *pulCount = DevQueryHardcopyCaps(hdc, 0, cForms, pahci);
3665 }
3666 }
3667
3668 return pahci;
3669}
3670
3671/*
3672 *@@ prthCreatePS:
3673 * creates a "normal" presentation space from the specified
3674 * printer device context (which can be opened thru
3675 * prthCreatePrinterDC).
3676 *
3677 * Returns NULLHANDLE on errors.
3678 *
3679 * Based on print sample by Peter Fitzsimmons, Fri 95-09-29 02:47:16am.
3680 */
3681
3682STATIC HPS prthCreatePS(HAB hab, // in: anchor block
3683 HDC hdc, // in: printer device context
3684 ULONG ulUnits) // in: one of:
3685 // -- PU_PELS
3686 // -- PU_LOMETRIC
3687 // -- PU_HIMETRIC
3688 // -- PU_LOENGLISH
3689 // -- PU_HIENGLISH
3690 // -- PU_TWIPS
3691{
3692 SIZEL sizel;
3693
3694 sizel.cx = 0;
3695 sizel.cy = 0;
3696 return GpiCreatePS(hab,
3697 hdc,
3698 &sizel,
3699 ulUnits | GPIA_ASSOC | GPIT_NORMAL);
3700}
3701
3702/*
3703 *@@ prthStartDoc:
3704 * calls DevEscape with DEVESC_STARTDOC.
3705 * This must be called before any painting
3706 * into the HDC's HPS. Any GPI calls made
3707 * before this are ignored.
3708 *
3709 * pszDocTitle appears in the spooler.
3710 */
3711
3712STATIC VOID prthStartDoc(HDC hdc,
3713 PSZ pszDocTitle)
3714{
3715 DevEscape(hdc,
3716 DEVESC_STARTDOC,
3717 strlen(pszDocTitle),
3718 pszDocTitle,
3719 0L,
3720 0L);
3721}
3722
3723/*
3724 *@@ prthNextPage:
3725 * calls DevEscape with DEVESC_NEWFRAME.
3726 * Signals when an application has finished writing to a page and wants to
3727 * start a new page. It is similar to GpiErase processing for a screen device
3728 * context, and causes a reset of the attributes. This escape is used with a
3729 * printer device to advance to a new page.
3730 */
3731
3732STATIC VOID prthNextPage(HDC hdc)
3733{
3734 DevEscape(hdc,
3735 DEVESC_NEWFRAME,
3736 0,
3737 0,
3738 0,
3739 0);
3740}
3741
3742/*
3743 *@@ prthEndDoc:
3744 * calls DevEscape with DEVESC_ENDDOC
3745 * and disassociates the HPS from the HDC.
3746 * Call this right before doing
3747 + GpiDestroyPS(hps);
3748 + DevCloseDC(hdc);
3749 */
3750
3751STATIC VOID prthEndDoc(HDC hdc,
3752 HPS hps)
3753{
3754 DevEscape(hdc, DEVESC_ENDDOC, 0L, 0L, 0, NULL);
3755 GpiAssociate(hps, NULLHANDLE);
3756}
3757
3758/*
3759 *@@ txvPrint:
3760 * this does the actual printing.
3761 */
3762
3763BOOL txvPrint(HAB hab,
3764 HDC hdc, // in: printer device context
3765 HPS hps, // in: printer presentation space (using PU_PELS)
3766 PSZ pszViewText, // in: text to print
3767 ULONG ulSize, // in: default font point size
3768 PSZ pszFaceName, // in: default font face name
3769 HCINFO *phci, // in: hardcopy form to use
3770 PSZ pszDocTitle, // in: document title (appears in spooler)
3771 FNPRINTCALLBACK *pfnCallback)
3772{
3773 RECTL rclPageDevice,
3774 rclPageWorld;
3775 XFORMATDATA xfd;
3776 BOOL fAnotherPage = FALSE;
3777 ULONG ulCurrentLineIndex = 0,
3778 ulCurrentPage = 1;
3779 ULONG ulCurrentYOfs = 0;
3780
3781 /* MATRIXLF matlf;
3782 POINTL ptlCenter;
3783 FIXED scalars[2]; */
3784
3785 // important: we must do a STARTDOC before we use the printer HPS.
3786 prthStartDoc(hdc,
3787 pszDocTitle);
3788
3789 // the PS is in TWIPS, but our world coordinate
3790 // space is in pels, so we need to transform
3791 /* GpiQueryViewingTransformMatrix(hps,
3792 1L,
3793 &matlf);
3794 ptlCenter.x = 0;
3795 ptlCenter.y = 0;
3796 scalars[0] = MAKEFIXED(2,0);
3797 scalars[1] = MAKEFIXED(3,0);
3798
3799 GpiScale (hps,
3800 &matlf,
3801 TRANSFORM_REPLACE,
3802 scalars,
3803 &ptlCenter); */
3804
3805 // initialize format with font from window
3806 txvInitFormat(&xfd);
3807
3808 /* SetFormatFont(hps,
3809 &xfd,
3810 ulSize,
3811 pszFaceName); */
3812
3813 // use text from window
3814 xstrcpy(&xfd.strViewText, pszViewText, 0);
3815
3816 // setup page
3817 GpiQueryPageViewport(hps,
3818 &rclPageDevice);
3819 // this is in device units; convert this
3820 // to the world coordinate space of the printer PS
3821 memcpy(&rclPageWorld, &rclPageDevice, sizeof(RECTL));
3822 GpiConvert(hps,
3823 CVTC_DEVICE, // source
3824 CVTC_WORLD,
3825 2, // 2 points, it's a rectangle
3826 (PPOINTL)&rclPageWorld);
3827
3828 // left and bottom margins are in millimeters...
3829 /* rclPage.xLeft = 100; // ###
3830 rclPage.yBottom = 100;
3831 rclPage.xRight = rclPage.xLeft + phci->xPels;
3832 rclPage.yTop = rclPage.yBottom + phci->yPels; */
3833
3834 txvFormatText(hps,
3835 &xfd, // in: ptxvd->rclViewText
3836 &rclPageWorld,
3837 TRUE);
3838
3839 do
3840 {
3841 _Pmpf(("---- printing page %d",
3842 ulCurrentPage));
3843
3844 fAnotherPage = txvPaintText(hab,
3845 hps,
3846 &xfd,
3847 &rclPageWorld,
3848 0,
3849 &ulCurrentYOfs,
3850 FALSE, // draw only fully visible lines
3851 &ulCurrentLineIndex); // in/out: line to start with
3852 if (fAnotherPage)
3853 {
3854 prthNextPage(hdc);
3855
3856 if (pfnCallback(ulCurrentPage++, 0) == FALSE)
3857 fAnotherPage = FALSE;
3858 }
3859 } while (fAnotherPage);
3860
3861 prthEndDoc(hdc, hps);
3862
3863 return TRUE;
3864}
3865
3866/*
3867 *@@ txvPrintWindow:
3868 * one-shot function which prints the contents
3869 * of the specified XTextView control to the
3870 * default printer, using the default form.
3871 *
3872 * Returns a nonzero value upon errors.
3873 *
3874 * Based on print sample by Peter Fitzsimmons, Fri 95-09-29 02:47:16am.
3875 */
3876
3877int txvPrintWindow(HWND hwndTextView,
3878 PSZ pszDocTitle, // in: document title (appears in spooler)
3879 FNPRINTCALLBACK *pfnCallback)
3880{
3881 int irc = 0;
3882
3883 PTEXTVIEWWINDATA ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_PRIVATE);
3884
3885 if (!ptxvd)
3886 irc = 1;
3887 else
3888 {
3889 ULONG cReturned = 0;
3890 PRQINFO3 *pprq3 = prthEnumQueues(&cReturned);
3891 HDC hdc = NULLHANDLE;
3892 LONG caps[2];
3893
3894 // find default queue
3895 if (pprq3)
3896 {
3897 ULONG i;
3898 // search for default queue;
3899 for (i = 0; i < cReturned; i++)
3900 if (pprq3[i].fsType & PRQ3_TYPE_APPDEFAULT)
3901 {
3902 hdc = prthCreatePrinterDC(ptxvd->hab,
3903 &pprq3[i],
3904 caps);
3905
3906 break;
3907 }
3908 prthFreeBuf(pprq3);
3909 }
3910
3911 if (!hdc)
3912 irc = 2;
3913 else
3914 {
3915 // OK, we got a printer DC:
3916 HPS hps;
3917 ULONG cForms = 0;
3918 HCINFO *pahci,
3919 *phciSelected = 0;
3920
3921 // find default form
3922 pahci = prthQueryForms(hdc,
3923 &cForms);
3924 if (pahci)
3925 {
3926 HCINFO *phciThis = pahci;
3927 ULONG i;
3928 for (i = 0;
3929 i < cForms;
3930 i++, phciThis++)
3931 {
3932 if (phciThis->flAttributes & HCAPS_CURRENT)
3933 {
3934 phciSelected = phciThis;
3935 }
3936 }
3937 }
3938
3939 if (!phciSelected)
3940 irc = 3;
3941 else
3942 {
3943 // create printer PS
3944 hps = prthCreatePS(ptxvd->hab,
3945 hdc,
3946 PU_PELS);
3947
3948 if (hps == GPI_ERROR)
3949 irc = 4;
3950 else
3951 {
3952 PSZ pszFont;
3953 ULONG ulSize = 0;
3954 PSZ pszFaceName = 0;
3955
3956 if ((pszFont = winhQueryWindowFont(hwndTextView)))
3957 gpihSplitPresFont(pszFont,
3958 &ulSize,
3959 &pszFaceName);
3960 txvPrint(ptxvd->hab,
3961 hdc,
3962 hps,
3963 ptxvd->xfd.strViewText.psz,
3964 ulSize,
3965 pszFaceName,
3966 phciSelected,
3967 pszDocTitle,
3968 pfnCallback);
3969
3970 if (pszFont)
3971 free(pszFont);
3972
3973 GpiDestroyPS(hps);
3974 }
3975 }
3976 DevCloseDC(hdc);
3977 }
3978 }
3979
3980 return irc;
3981}
3982
3983
Note: See TracBrowser for help on using the repository browser.