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

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

Rewrote winlist widget to use daemon.

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