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

Last change on this file since 39 was 25, checked in by umoeller, 25 years ago

Misc. changes for V0.9.7.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 127.8 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
280VOID 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 *@@ txvSetFormatFont:
339 * creates logical fonts from the specified
340 * font information.
341 *
342 *@@added V0.9.3 (2000-05-06) [umoeller]
343 */
344
345VOID txvSetFormatFont(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
387VOID 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
496PSZ 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 txvCreateWord
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 *@@ txvCreateWord:
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
615PTXVWORD txvCreateWord(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
747PTXVWORD 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 {
811 // four characters with hex anchor index (>=1)
812 // or ####
813 if ( *( (*ppCurrent)+2 )
814 == '#'
815 )
816 pflbuf->usCurrentAnchor = 0;
817 else
818 {
819 PSZ pEnd;
820 memcpy(szDecimal, (*ppCurrent)+2, 4);
821 szDecimal[4] = 0;
822 lDecimal = strtol(szDecimal, &pEnd, 16);
823 pflbuf->usCurrentAnchor = lDecimal;
824 }
825
826 ulSkip = 6;
827 break; }
828
829 case 7: // A NAME= (anchor name)
830 {
831 // this is variable in length and terminated with
832 // another 0xFF char; we completely ignore this
833 // here and just skip the anchor name, this is
834 // only used with TXM_JUMPTOANCHORNAME, which then
835 // searches the buffer
836 PSZ pEnd = strchr((*ppCurrent)+2, 0xFF);
837 if (pEnd)
838 {
839 ulSkip = pEnd - *ppCurrent + 1;
840 // store this with the other words so we can
841 // find this word later
842 fCreateWord = TRUE;
843 // and store this with the rectangles
844 fPaintEscapeWord = TRUE;
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 */
1074
1075VOID txvFormatText(HPS hps, // in: HPS whose font is used for
1076 // calculating text dimensions
1077 PXFORMATDATA pxfd, // in: formatting data
1078 PRECTL prclView, // in: rectangle to format for (window or printer page)
1079 BOOL fFullRecalc) // in: re-calculate word list too? (must be TRUE on the first call)
1080{
1081 /* ULONG ulWinCX = (prclView->xRight - prclView->xLeft),
1082 ulWinCY = (prclView->yTop - prclView->yBottom); */
1083
1084 lstClear(&pxfd->llRectangles);
1085 if (fFullRecalc)
1086 lstClear(&pxfd->llWords);
1087
1088 pxfd->ulViewportCX = 0;
1089 pxfd->ulViewportCY = 0;
1090
1091 if (pxfd->strViewText.cbAllocated)
1092 {
1093 ULONG ulTextLen = pxfd->strViewText.ulLength;
1094
1095 FORMATLINEBUF flbuf;
1096 LONG lcidLast = -99,
1097 lPointSizeLast = -99;
1098
1099 memset(&flbuf, 0, sizeof(flbuf));
1100 // copy default paragraph formatting
1101 memcpy(&flbuf.fmtp, &pxfd->fmtpStandard, sizeof(flbuf.fmtp));
1102 // set font
1103 flbuf.pfmtc = &pxfd->fmtcStandard;
1104 flbuf.lPointSize = pxfd->fmtcStandard.lPointSize;
1105 flbuf.pLastChar = pxfd->strViewText.psz + ulTextLen;
1106
1107 if (ulTextLen)
1108 {
1109 ULONG cWords = 0;
1110
1111 if (fFullRecalc)
1112 {
1113 /*
1114 * step 1: create words
1115 *
1116 */
1117
1118 PSZ pCurrent = pxfd->strViewText.psz;
1119
1120 // loop until null terminator
1121 while (*pCurrent)
1122 {
1123 PTXVWORD pWord;
1124
1125 if (flbuf.fBold)
1126 {
1127 if (flbuf.fItalics)
1128 flbuf.pfmtf = &flbuf.pfmtc->fntBoldItalics;
1129 else
1130 flbuf.pfmtf = &flbuf.pfmtc->fntBold;
1131 }
1132 else
1133 if (flbuf.fItalics)
1134 flbuf.pfmtf = &flbuf.pfmtc->fntItalics;
1135 else
1136 flbuf.pfmtf = &flbuf.pfmtc->fntRegular;
1137
1138 // set font for subsequent calculations,
1139 // if changed (this includes the first call)
1140 if (lcidLast != flbuf.pfmtf->lcid)
1141 {
1142 GpiSetCharSet(hps, flbuf.pfmtf->lcid);
1143 lcidLast = flbuf.pfmtf->lcid;
1144 // force recalc of point size
1145 lPointSizeLast = -99;
1146 }
1147
1148 if (lPointSizeLast != flbuf.lPointSize)
1149 {
1150 if (flbuf.pfmtf->FontMetrics.fsDefn & FM_DEFN_OUTLINE)
1151 // is outline font:
1152 gpihSetPointSize(hps, flbuf.lPointSize);
1153 lPointSizeLast = flbuf.lPointSize;
1154 }
1155
1156 pWord = txvCreateWord(hps,
1157 &pCurrent, // advanced to next word
1158 &flbuf);
1159 if (pWord)
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->pvRectangle = (PVOID)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->pvRectangle = (PVOID)pRect;
1312 lstAppendItem(&pRect->llWords, pWordThis);
1313 // ### 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
1413VOID 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
1867VOID 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 txvSetFormatFont(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
1922VOID 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
1994VOID 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
2082VOID 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
2102VOID 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
2133VOID RepaintWord(PTEXTVIEWWINDATA ptxvd,
2134 PTXVWORD pWordThis,
2135 LONG lColor)
2136{
2137 POINTL ptlStart;
2138 ULONG flOptions = pWordThis->flOptions;
2139 PTXVRECTANGLE pLineRcl = (PTXVRECTANGLE)pWordThis->pvRectangle;
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
2186VOID 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
2257MRESULT 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 ptxvd = (PTEXTVIEWWINDATA)malloc(sizeof(TEXTVIEWWINDATA));
2281 if (ptxvd)
2282 {
2283 SIZEL szlPage = {0, 0};
2284 BOOL fShow = FALSE;
2285 // LONG lcid = 0;
2286
2287 // query message queue
2288 HMQ hmq = WinQueryWindowULong(hwndTextView, QWL_HMQ);
2289 // get codepage of message queue
2290 ULONG ulCodepage = WinQueryCp(hmq);
2291
2292 memset(ptxvd, 0, sizeof(TEXTVIEWWINDATA));
2293 WinSetWindowPtr(hwndTextView, QWL_USER, ptxvd);
2294
2295 ptxvd->hab = WinQueryAnchorBlock(hwndTextView);
2296
2297 ptxvd->hdc = WinOpenWindowDC(hwndTextView);
2298 ptxvd->hps = GpiCreatePS(ptxvd->hab,
2299 ptxvd->hdc,
2300 &szlPage, // use same page size as device
2301 PU_PELS | GPIT_MICRO | GPIA_ASSOC);
2302
2303 gpihSwitchToRGB(ptxvd->hps);
2304
2305 // set codepage; GPI defaults this to
2306 // the process codepage
2307 GpiSetCp(ptxvd->hps, ulCodepage);
2308
2309 txvInitFormat(&ptxvd->xfd);
2310
2311 // copy control data, if present
2312 if (pcd)
2313 memcpy(&ptxvd->cdata, pcd, pcd->cbData);
2314
2315 // check values which might cause null divisions
2316 if (ptxvd->cdata.ulVScrollLineUnit == 0)
2317 ptxvd->cdata.ulVScrollLineUnit = 15;
2318 if (ptxvd->cdata.ulHScrollLineUnit == 0)
2319 ptxvd->cdata.ulHScrollLineUnit = 15;
2320
2321 ptxvd->fAcceptsPresParamsNow = FALSE;
2322
2323 // copy window dimensions from CREATESTRUCT
2324 ptxvd->rclViewReal.xLeft = 0;
2325 ptxvd->rclViewReal.yBottom = 0;
2326 ptxvd->rclViewReal.xRight = pcs->cx;
2327 ptxvd->rclViewReal.yTop = pcs->cy;
2328
2329 sbcd.cb = sizeof(SBCDATA);
2330 sbcd.sHilite = 0;
2331 sbcd.posFirst = 0;
2332 sbcd.posLast = 100;
2333 sbcd.posThumb = 30;
2334 sbcd.cVisible = 50;
2335 sbcd.cTotal = 50;
2336
2337 ptxvd->hwndVScroll = WinCreateWindow(hwndTextView,
2338 WC_SCROLLBAR,
2339 "",
2340 SBS_VERT | SBS_THUMBSIZE | WS_VISIBLE,
2341 10, 10,
2342 20, 100,
2343 hwndTextView, // owner
2344 HWND_TOP,
2345 ID_VSCROLL,
2346 &sbcd,
2347 0);
2348 fShow = ((ptxvd->cdata.flStyle & XTXF_VSCROLL) != 0);
2349 WinShowWindow(ptxvd->hwndVScroll, fShow);
2350 ptxvd->fVScrollVisible = fShow;
2351
2352 ptxvd->hwndHScroll = WinCreateWindow(hwndTextView,
2353 WC_SCROLLBAR,
2354 "",
2355 SBS_THUMBSIZE | WS_VISIBLE,
2356 10, 10,
2357 20, 100,
2358 hwndTextView, // owner
2359 HWND_TOP,
2360 ID_HSCROLL,
2361 &sbcd,
2362 0);
2363 fShow = ((ptxvd->cdata.flStyle & XTXF_HSCROLL) != 0);
2364 WinShowWindow(ptxvd->hwndHScroll, fShow);
2365 ptxvd->fHScrollVisible = fShow;
2366
2367 // set "code" format
2368 txvSetFormatFont(ptxvd->hps,
2369 &ptxvd->xfd.fmtcCode,
2370 6,
2371 "System VIO");
2372
2373 // get colors from presparams/syscolors
2374 UpdateTextViewPresData(hwndTextView, ptxvd);
2375
2376 AdjustViewRects(hwndTextView,
2377 ptxvd);
2378
2379 mrc = (MPARAM)FALSE; // OK
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 = (WNDPARAMS *)mp1;
2394 if (pwndParams)
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 break; }
2414
2415 /*
2416 * WM_WINDOWPOSCHANGED:
2417 *
2418 */
2419
2420 case WM_WINDOWPOSCHANGED:
2421 {
2422 // this msg is passed two SWP structs:
2423 // one for the old, one for the new data
2424 // (from PM docs)
2425 PSWP pswpNew = (PSWP)(mp1);
2426 // PSWP pswpOld = pswpNew + 1;
2427
2428 // resizing?
2429 if (pswpNew->fl & SWP_SIZE)
2430 {
2431 if (ptxvd)
2432 {
2433 WinQueryWindowRect(hwndTextView,
2434 &ptxvd->rclViewReal);
2435 AdjustViewRects(hwndTextView,
2436 ptxvd);
2437 FormatText2Screen(hwndTextView,
2438 ptxvd,
2439 FALSE,
2440 FALSE); // quick format
2441 }
2442 }
2443 break; }
2444
2445 /*
2446 * WM_PAINT:
2447 *
2448 */
2449
2450 case WM_PAINT:
2451 {
2452 HRGN hrgnOldClip;
2453
2454 /* HPS hps = WinBeginPaint(hwndTextView,
2455 ptxvd->hps,
2456 &rcl2Paint); // store invalid rectangle here
2457 */
2458
2459 if (ptxvd)
2460 {
2461 RECTL rclClip;
2462 RECTL rcl2Update;
2463
2464 // get update rectangle
2465 WinQueryUpdateRect(hwndTextView,
2466 &rcl2Update);
2467 // since we're not using WinBeginPaint,
2468 // we must validate the update region,
2469 // or we'll get bombed with WM_PAINT msgs
2470 WinValidateRect(hwndTextView,
2471 NULL,
2472 FALSE);
2473
2474 // reset clip region to "all"
2475 GpiSetClipRegion(ptxvd->hps,
2476 NULLHANDLE,
2477 &hrgnOldClip); // out: old clip region
2478 // reduce clip region to update rectangle
2479 GpiIntersectClipRectangle(ptxvd->hps,
2480 &rcl2Update); // exclusive
2481
2482 // draw little box at the bottom right
2483 // (in between scroll bars) if we have
2484 // both vertical and horizontal scroll bars
2485 if ( (ptxvd->cdata.flStyle & (XTXF_VSCROLL | XTXF_HSCROLL))
2486 == (XTXF_VSCROLL | XTXF_HSCROLL)
2487 && (ptxvd->fVScrollVisible)
2488 && (ptxvd->fHScrollVisible)
2489 )
2490 {
2491 RECTL rclBox;
2492 rclBox.xLeft = ptxvd->rclViewPaint.xRight;
2493 rclBox.yBottom = 0;
2494 rclBox.xRight = rclBox.xLeft + WinQuerySysValue(HWND_DESKTOP, SV_CXVSCROLL);
2495 rclBox.yTop = WinQuerySysValue(HWND_DESKTOP, SV_CYHSCROLL);
2496 WinFillRect(ptxvd->hps,
2497 &rclBox,
2498 WinQuerySysColor(HWND_DESKTOP,
2499 SYSCLR_DIALOGBACKGROUND,
2500 0));
2501 }
2502
2503 // paint "view paint" rectangle white;
2504 // this can be larger than "view text"
2505 WinFillRect(ptxvd->hps,
2506 &ptxvd->rclViewPaint, // exclusive
2507 ptxvd->lBackColor);
2508
2509 // now reduce clipping rectangle to "view text" rectangle
2510 rclClip.xLeft = ptxvd->rclViewText.xLeft;
2511 rclClip.xRight = ptxvd->rclViewText.xRight - 1;
2512 rclClip.yBottom = ptxvd->rclViewText.yBottom;
2513 rclClip.yTop = ptxvd->rclViewText.yTop - 1;
2514 GpiIntersectClipRectangle(ptxvd->hps,
2515 &rclClip); // exclusive
2516 // finally, draw text lines in invalid rectangle;
2517 // this subfunction is smart enough to redraw only
2518 // the lines which intersect with rcl2Update
2519 GpiSetColor(ptxvd->hps, ptxvd->lForeColor);
2520 PaintViewText2Screen(ptxvd,
2521 &rcl2Update);
2522
2523 if (WinQueryFocus(HWND_DESKTOP) == hwndTextView)
2524 {
2525 // we have the focus:
2526 // reset clip region to "all"
2527 GpiSetClipRegion(ptxvd->hps,
2528 NULLHANDLE,
2529 &hrgnOldClip); // out: old clip region
2530 PaintViewFocus(ptxvd->hps,
2531 ptxvd,
2532 TRUE);
2533 }
2534
2535 ptxvd->fAcceptsPresParamsNow = TRUE;
2536 }
2537
2538 // WinEndPaint(hps);
2539 break; }
2540
2541 /*
2542 * WM_PRESPARAMCHANGED:
2543 *
2544 * Changing the color or font settings
2545 * is equivalent to changing the default
2546 * paragraph format. See TXM_SETFORMAT.
2547 */
2548
2549 case WM_PRESPARAMCHANGED:
2550 mrc = WinDefWindowProc(hwndTextView, msg, mp1, mp2);
2551 if (ptxvd)
2552 {
2553 LONG lPPIndex = (LONG)mp1;
2554 switch (lPPIndex)
2555 {
2556 case 0: // layout palette thing dropped
2557 case PP_BACKGROUNDCOLOR:
2558 case PP_FOREGROUNDCOLOR:
2559 case PP_FONTNAMESIZE:
2560 // re-query our presparams
2561 UpdateTextViewPresData(hwndTextView, ptxvd);
2562 }
2563
2564 if (ptxvd->fAcceptsPresParamsNow)
2565 FormatText2Screen(hwndTextView,
2566 ptxvd,
2567 FALSE,
2568 TRUE); // full reformat
2569 }
2570 break;
2571
2572 /*
2573 * WM_VSCROLL:
2574 *
2575 */
2576
2577 case WM_VSCROLL:
2578 {
2579 if (ptxvd->fVScrollVisible)
2580 {
2581 winhHandleScrollMsg(hwndTextView,
2582 ptxvd->hwndVScroll,
2583 &ptxvd->ulViewYOfs,
2584 &ptxvd->rclViewText,
2585 ptxvd->xfd.ulViewportCY,
2586 ptxvd->cdata.ulVScrollLineUnit,
2587 msg,
2588 mp2);
2589 }
2590 break; }
2591
2592 /*
2593 * WM_HSCROLL:
2594 *
2595 */
2596
2597 case WM_HSCROLL:
2598 {
2599 if (ptxvd->fHScrollVisible)
2600 {
2601 winhHandleScrollMsg(hwndTextView,
2602 ptxvd->hwndHScroll,
2603 &ptxvd->ulViewXOfs,
2604 &ptxvd->rclViewText,
2605 ptxvd->xfd.ulViewportCX,
2606 ptxvd->cdata.ulHScrollLineUnit,
2607 msg,
2608 mp2);
2609 }
2610 break; }
2611
2612 /*
2613 * WM_SETFOCUS:
2614 *
2615 */
2616
2617 case WM_SETFOCUS:
2618 {
2619 HPS hps = WinGetPS(hwndTextView);
2620 gpihSwitchToRGB(hps);
2621 PaintViewFocus(hps,
2622 ptxvd,
2623 (mp2 != 0));
2624 WinReleasePS(hps);
2625 break; }
2626
2627 /*
2628 * WM_MOUSEMOVE:
2629 * send WM_CONTROLPOINTER to owner.
2630 */
2631
2632 case WM_MOUSEMOVE:
2633 {
2634 HWND hwndOwner = WinQueryWindow(hwndTextView, QW_OWNER);
2635 if (hwndOwner)
2636 {
2637 HPOINTER hptrSet
2638 = (HPOINTER)WinSendMsg(hwndOwner,
2639 WM_CONTROLPOINTER,
2640 (MPARAM)(LONG)WinQueryWindowUShort(hwndTextView,
2641 QWS_ID),
2642 (MPARAM)WinQuerySysPointer(HWND_DESKTOP,
2643 SPTR_ARROW,
2644 FALSE));
2645 WinSetPointer(HWND_DESKTOP, hptrSet);
2646 }
2647 break; }
2648
2649 /*
2650 * WM_BUTTON1DOWN:
2651 *
2652 */
2653
2654 case WM_BUTTON1DOWN:
2655 {
2656 POINTL ptlPos;
2657 PLISTNODE pWordNodeClicked = NULL;
2658
2659 ptlPos.x = SHORT1FROMMP(mp1) + ptxvd->ulViewXOfs;
2660 ptlPos.y = SHORT2FROMMP(mp1) - ptxvd->ulViewYOfs;
2661
2662 if (hwndTextView != WinQueryFocus(HWND_DESKTOP))
2663 WinSetFocus(HWND_DESKTOP, hwndTextView);
2664
2665 ptxvd->usLastAnchorClicked = 0;
2666
2667 pWordNodeClicked = txvFindWordFromPoint(&ptxvd->xfd,
2668 &ptlPos);
2669
2670 if (pWordNodeClicked)
2671 {
2672 PTXVWORD pWordClicked = (PTXVWORD)pWordNodeClicked->pItemData;
2673
2674 // store anchor (can be 0)
2675 ptxvd->usLastAnchorClicked = pWordClicked->usAnchor;
2676
2677 if (pWordClicked->usAnchor)
2678 {
2679 // word has an anchor:
2680 PLISTNODE pNode = pWordNodeClicked;
2681
2682 // reset first word of anchor
2683 ptxvd->pWordNodeFirstInAnchor = NULL;
2684
2685 // go back to find the first word which has this anchor,
2686 // because we need to repaint them all
2687 while (pNode)
2688 {
2689 PTXVWORD pWordThis = (PTXVWORD)pNode->pItemData;
2690 if (pWordThis->usAnchor == pWordClicked->usAnchor)
2691 {
2692 // still has same anchor:
2693 // go for previous
2694 ptxvd->pWordNodeFirstInAnchor = pNode;
2695 pNode = pNode->pPrevious;
2696 }
2697 else
2698 // different anchor:
2699 // pNodeFirst points to first node with same anchor now
2700 break;
2701 }
2702
2703 RepaintAnchor(ptxvd,
2704 RGBCOL_RED);
2705 }
2706 }
2707
2708 WinSetCapture(HWND_DESKTOP, hwndTextView);
2709 mrc = (MPARAM)TRUE;
2710 break; }
2711
2712 /*
2713 * WM_BUTTON1UP:
2714 *
2715 */
2716
2717 case WM_BUTTON1UP:
2718 {
2719 POINTL ptlPos;
2720 // PTXVWORD pWordClicked = NULL;
2721 HWND hwndOwner = NULLHANDLE;
2722
2723 ptlPos.x = SHORT1FROMMP(mp1) + ptxvd->ulViewXOfs;
2724 ptlPos.y = SHORT2FROMMP(mp1) - ptxvd->ulViewYOfs;
2725 WinSetCapture(HWND_DESKTOP, NULLHANDLE);
2726
2727 if (ptxvd->usLastAnchorClicked)
2728 {
2729 RepaintAnchor(ptxvd,
2730 ptxvd->lForeColor);
2731
2732 // nofify owner
2733 hwndOwner = WinQueryWindow(hwndTextView, QW_OWNER);
2734 if (hwndOwner)
2735 WinPostMsg(hwndOwner,
2736 WM_CONTROL,
2737 MPFROM2SHORT(WinQueryWindowUShort(hwndTextView,
2738 QWS_ID),
2739 TXVN_LINK),
2740 (MPARAM)(ULONG)(ptxvd->usLastAnchorClicked));
2741 }
2742
2743 mrc = (MPARAM)TRUE;
2744 break; }
2745
2746 /*
2747 * WM_CHAR:
2748 *
2749 */
2750
2751 case WM_CHAR:
2752 {
2753 BOOL fDefProc = TRUE;
2754 USHORT usFlags = SHORT1FROMMP(mp1);
2755 // USHORT usch = SHORT1FROMMP(mp2);
2756 USHORT usvk = SHORT2FROMMP(mp2);
2757
2758 if (usFlags & KC_VIRTUALKEY)
2759 {
2760 ULONG ulMsg = 0;
2761 USHORT usID = ID_VSCROLL;
2762 SHORT sPos = 0;
2763 SHORT usCmd = 0;
2764 fDefProc = FALSE;
2765
2766 switch (usvk)
2767 {
2768 case VK_UP:
2769 ulMsg = WM_VSCROLL;
2770 usCmd = SB_LINEUP;
2771 break;
2772
2773 case VK_DOWN:
2774 ulMsg = WM_VSCROLL;
2775 usCmd = SB_LINEDOWN;
2776 break;
2777
2778 case VK_RIGHT:
2779 ulMsg = WM_HSCROLL;
2780 usCmd = SB_LINERIGHT;
2781 break;
2782
2783 case VK_LEFT:
2784 ulMsg = WM_HSCROLL;
2785 usCmd = SB_LINELEFT;
2786 break;
2787
2788 case VK_PAGEUP:
2789 ulMsg = WM_VSCROLL;
2790 if (usFlags & KC_CTRL)
2791 {
2792 sPos = 0;
2793 usCmd = SB_SLIDERPOSITION;
2794 }
2795 else
2796 usCmd = SB_PAGEUP;
2797 break;
2798
2799 case VK_PAGEDOWN:
2800 ulMsg = WM_VSCROLL;
2801 if (usFlags & KC_CTRL)
2802 {
2803 sPos = ptxvd->xfd.ulViewportCY;
2804 usCmd = SB_SLIDERPOSITION;
2805 }
2806 else
2807 usCmd = SB_PAGEDOWN;
2808 break;
2809
2810 case VK_HOME:
2811 if (usFlags & KC_CTRL)
2812 // vertical:
2813 ulMsg = WM_VSCROLL;
2814 else
2815 ulMsg = WM_HSCROLL;
2816
2817 sPos = 0;
2818 usCmd = SB_SLIDERPOSITION;
2819 break;
2820
2821 case VK_END:
2822 if (usFlags & KC_CTRL)
2823 {
2824 // vertical:
2825 ulMsg = WM_VSCROLL;
2826 sPos = ptxvd->xfd.ulViewportCY;
2827 }
2828 else
2829 {
2830 ulMsg = WM_HSCROLL;
2831 sPos = ptxvd->xfd.ulViewportCX;
2832 }
2833
2834 usCmd = SB_SLIDERPOSITION;
2835 break;
2836
2837 default:
2838 // other:
2839 fDefProc = TRUE;
2840 }
2841
2842 if ( ((usFlags & KC_KEYUP) == 0)
2843 && (ulMsg)
2844 )
2845 WinSendMsg(hwndTextView,
2846 ulMsg,
2847 MPFROMSHORT(usID),
2848 MPFROM2SHORT(sPos,
2849 usCmd));
2850 }
2851
2852 if (fDefProc)
2853 mrc = WinDefWindowProc(hwndTextView, msg, mp1, mp2);
2854 // sends to owner
2855 else
2856 mrc = (MPARAM)TRUE;
2857 break; }
2858
2859 /*
2860 *@@ TXM_QUERYPARFORMAT:
2861 * this msg can be sent to the text view control
2862 * to retrieve the paragraph format with the
2863 * index specified in mp1.
2864 *
2865 * Parameters:
2866 * -- ULONG mp1: index of format to query.
2867 * Must be 0 currently for the standard
2868 * paragraph format.
2869 * -- PXFMTPARAGRAPH mp2: pointer to buffer
2870 * which is to receive the formatting
2871 * data.
2872 *
2873 * Returns TRUE if copying was successful.
2874 *
2875 *@@added V0.9.3 (2000-05-06) [umoeller]
2876 */
2877
2878 case TXM_QUERYPARFORMAT:
2879 {
2880 PXFMTPARAGRAPH pFmt = NULL; // source
2881
2882 mrc = (MPARAM)FALSE;
2883
2884 if (mp1 == 0)
2885 pFmt = &ptxvd->xfd.fmtpStandard;
2886 /* else if ((ULONG)mp1 == 1)
2887 pFmt = &ptxvd->xfd.fmtpCode; */
2888
2889 if ((pFmt) && (mp2))
2890 {
2891 memcpy(mp2, pFmt, sizeof(XFMTPARAGRAPH));
2892 mrc = (MPARAM)TRUE;
2893 }
2894 break; }
2895
2896 /*
2897 *@@ TXM_SETPARFORMAT:
2898 * reverse to TXM_QUERYPARFORMAT, this sets a
2899 * paragraph format (line spacings, margins
2900 * and such).
2901 *
2902 * Parameters:
2903 * -- ULONG mp1: index of format to set.
2904 * Must be 0 currently for the standard
2905 * paragraph format.
2906 * -- PXFMTPARAGRAPH mp2: pointer to buffer
2907 * from which to copy formatting data.
2908 * If this pointer is NULL, the format
2909 * is reset to the default.
2910 *
2911 * This reformats the control.
2912 *
2913 *@@added V0.9.3 (2000-05-06) [umoeller]
2914 */
2915
2916 case TXM_SETPARFORMAT:
2917 {
2918 PXFMTPARAGRAPH pFmt = NULL; // target
2919
2920 mrc = (MPARAM)FALSE;
2921
2922 if (mp1 == 0)
2923 pFmt = &ptxvd->xfd.fmtpStandard;
2924 /* else if ((ULONG)mp1 == 1)
2925 pFmt = &ptxvd->xfd.fmtpCode; */
2926
2927 if (pFmt)
2928 {
2929 if (mp2)
2930 // copy
2931 memcpy(pFmt, mp2, sizeof(XFMTPARAGRAPH));
2932 else
2933 // default:
2934 memset(pFmt, 0, sizeof(XFMTPARAGRAPH));
2935
2936 FormatText2Screen(hwndTextView,
2937 ptxvd,
2938 FALSE,
2939 TRUE); // full reformat
2940
2941 mrc = (MPARAM)TRUE;
2942 }
2943 break; }
2944
2945 /*
2946 *@@ TXM_SETWORDWRAP:
2947 * this text view control msg quickly changes
2948 * the word-wrapping style of the default
2949 * paragraph formatting.
2950 * (BOOL)mp1 determines whether word wrapping
2951 * should be turned on or off.
2952 */
2953
2954 case TXM_SETWORDWRAP:
2955 {
2956 BOOL ulOldFlFormat = ptxvd->xfd.fmtpStandard.fWordWrap;
2957 ptxvd->xfd.fmtpStandard.fWordWrap = (BOOL)mp1;
2958 if (ptxvd->xfd.fmtpStandard.fWordWrap != ulOldFlFormat)
2959 FormatText2Screen(hwndTextView,
2960 ptxvd,
2961 FALSE,
2962 FALSE); // quick format
2963 break; }
2964
2965 /*
2966 *@@ TXM_QUERYCDATA:
2967 * copies the current XTEXTVIEWCDATA
2968 * into the specified buffer. This must
2969 * be sent to the control.
2970 *
2971 * Parameters:
2972 * -- PXTEXTVIEWCDATA mp1: target buffer.
2973 * Before calling this, you MUST specify
2974 * XTEXTVIEWCDATA.cbData.
2975 */
2976
2977 case TXM_QUERYCDATA:
2978 if (mp1)
2979 {
2980 PXTEXTVIEWCDATA pTarget = (PXTEXTVIEWCDATA)mp1;
2981 memcpy(pTarget, &ptxvd->cdata, pTarget->cbData);
2982 }
2983 break;
2984
2985 /*
2986 *@@ TXM_SETCDATA:
2987 * updates the current XTEXTVIEWCDATA
2988 * with the data from the specified buffer.
2989 * This must be sent to the control.
2990 *
2991 * Parameters:
2992 * -- PXTEXTVIEWCDATA mp1: source buffer.
2993 * Before calling this, you MUST specify
2994 * XTEXTVIEWCDATA.cbData.
2995 */
2996
2997 case TXM_SETCDATA:
2998 if (mp1)
2999 {
3000 PXTEXTVIEWCDATA pSource = (PXTEXTVIEWCDATA)mp1;
3001 memcpy(&ptxvd->cdata, pSource, pSource->cbData);
3002 }
3003 break;
3004
3005 /*
3006 *@@ TXM_JUMPTOANCHORNAME:
3007 * scrolls the XTextView control contents so that
3008 * the text marked with the specified anchor name
3009 * (TXVESC_ANCHORNAME escape) appears at the top
3010 * of the control.
3011 *
3012 * This must be sent, not posted to the control
3013 *
3014 * Parameters:
3015 * -- PSZ mp1: anchor name (e.g. "anchor1").
3016 *
3017 *@@added V0.9.4 (2000-06-12) [umoeller]
3018 */
3019
3020 case TXM_JUMPTOANCHORNAME:
3021 {
3022 if (mp1)
3023 {
3024 PLISTNODE pWordNode = txvFindWordFromAnchor(&ptxvd->xfd,
3025 (const char*)mp1);
3026 if (pWordNode)
3027 {
3028 // found:
3029 PTXVWORD pWord = (PTXVWORD)pWordNode->pItemData;
3030 if (pWord)
3031 {
3032 PTXVRECTANGLE pRect = (PTXVRECTANGLE)pWord->pvRectangle;
3033 ULONG ulWinCY = (ptxvd->rclViewText.yTop - ptxvd->rclViewText.yBottom);
3034
3035 // now we need to scroll the window so that this rectangle is on top.
3036 // Since rectangles start out with the height of the window (e.g. +768)
3037 // and then have lower y coordinates down to way in the negatives,
3038 // to get the y offset, we must...
3039 ptxvd->ulViewYOfs = (-pRect->rcl.yTop) - ulWinCY;
3040
3041 if (ptxvd->ulViewYOfs < 0)
3042 ptxvd->ulViewYOfs = 0;
3043 if (ptxvd->ulViewYOfs > ((LONG)ptxvd->xfd.ulViewportCY - ulWinCY))
3044 ptxvd->ulViewYOfs = (LONG)ptxvd->xfd.ulViewportCY - ulWinCY;
3045
3046 // vertical scroll bar enabled at all?
3047 if (ptxvd->cdata.flStyle & XTXF_VSCROLL)
3048 {
3049 BOOL fEnabled = winhUpdateScrollBar(ptxvd->hwndVScroll,
3050 ulWinCY,
3051 ptxvd->xfd.ulViewportCY,
3052 ptxvd->ulViewYOfs,
3053 (ptxvd->cdata.flStyle & XTXF_AUTOVHIDE));
3054 WinInvalidateRect(hwndTextView, NULL, FALSE);
3055 }
3056 }
3057 }
3058 }
3059 break; }
3060
3061 /*
3062 * WM_DESTROY:
3063 * clean up.
3064 */
3065
3066 case WM_DESTROY:
3067 xstrClear(&ptxvd->xfd.strViewText);
3068 lstClear(&ptxvd->xfd.llRectangles);
3069 lstClear(&ptxvd->xfd.llWords);
3070 free(ptxvd);
3071 GpiDestroyPS(ptxvd->hps);
3072 mrc = WinDefWindowProc(hwndTextView, msg, mp1, mp2);
3073 break;
3074
3075 default:
3076 mrc = WinDefWindowProc(hwndTextView, msg, mp1, mp2);
3077 }
3078
3079 return (mrc);
3080}
3081
3082/*
3083 *@@ txvRegisterTextView:
3084 * registers the Text View class with PM. Required
3085 * before the text view control can be used.
3086 */
3087
3088BOOL txvRegisterTextView(HAB hab)
3089{
3090 return (WinRegisterClass(hab,
3091 WC_XTEXTVIEW,
3092 fnwpTextView,
3093 0,
3094 sizeof(PVOID))); // QWL_USER
3095}
3096
3097/*
3098 *@@ txvReplaceWithTextView:
3099 * replaces any window with a text view control.
3100 * You must call txvRegisterTextView beforehand.
3101 *
3102 *@@added V0.9.1 (2000-02-13) [umoeller]
3103 */
3104
3105HWND txvReplaceWithTextView(HWND hwndParentAndOwner,
3106 USHORT usID,
3107 ULONG flWinStyle,
3108 ULONG flStyle,
3109 USHORT usBorder)
3110{
3111 HWND hwndMLE = WinWindowFromID(hwndParentAndOwner, usID),
3112 hwndTextView = NULLHANDLE;
3113 if (hwndMLE)
3114 {
3115 ULONG ul,
3116 // attrFound,
3117 abValue[32];
3118 SWP swpMLE;
3119 XTEXTVIEWCDATA xtxCData;
3120 PSZ pszFont = winhQueryWindowFont(hwndMLE);
3121 LONG lBackClr = -1,
3122 lForeClr = -1;
3123
3124 if ((ul = WinQueryPresParam(hwndMLE,
3125 PP_BACKGROUNDCOLOR,
3126 0,
3127 NULL,
3128 (ULONG)sizeof(abValue),
3129 (PVOID)&abValue,
3130 QPF_NOINHERIT)))
3131 lBackClr = abValue[0];
3132
3133 if ((ul = WinQueryPresParam(hwndMLE,
3134 PP_FOREGROUNDCOLOR,
3135 0,
3136 NULL,
3137 (ULONG)sizeof(abValue),
3138 (PVOID)&abValue,
3139 QPF_NOINHERIT)))
3140 lForeClr = abValue[0];
3141
3142 WinQueryWindowPos(hwndMLE, &swpMLE);
3143
3144 WinDestroyWindow(hwndMLE);
3145 memset(&xtxCData, 0, sizeof(xtxCData));
3146 xtxCData.cbData = sizeof(xtxCData);
3147 xtxCData.flStyle = flStyle;
3148 xtxCData.ulXBorder = usBorder;
3149 xtxCData.ulYBorder = usBorder;
3150 hwndTextView = WinCreateWindow(hwndParentAndOwner,
3151 WC_XTEXTVIEW,
3152 "",
3153 flWinStyle,
3154 swpMLE.x,
3155 swpMLE.y,
3156 swpMLE.cx,
3157 swpMLE.cy,
3158 hwndParentAndOwner,
3159 HWND_TOP,
3160 usID,
3161 &xtxCData,
3162 0);
3163 if (pszFont)
3164 {
3165 winhSetWindowFont(hwndTextView, pszFont);
3166 free(pszFont);
3167 }
3168
3169 if (lBackClr != -1)
3170 WinSetPresParam(hwndTextView,
3171 PP_BACKGROUNDCOLOR,
3172 sizeof(ULONG),
3173 &lBackClr);
3174 if (lForeClr != -1)
3175 WinSetPresParam(hwndTextView,
3176 PP_FOREGROUNDCOLOR,
3177 sizeof(ULONG),
3178 &lForeClr);
3179 }
3180 return (hwndTextView);
3181}
3182
3183/* ******************************************************************
3184 *
3185 * Printer-dependent functions
3186 *
3187 ********************************************************************/
3188
3189/*
3190 *@@ prthQueryQueues:
3191 * returns a buffer containing all print queues
3192 * on the system.
3193 *
3194 * This is usually the first step before printing.
3195 * After calling this function, show a dlg to the
3196 * user, allow him to select the printer queue
3197 * to be used. This can then be passed to
3198 * prthCreatePrinterDC.
3199 *
3200 * Use prthFreeBuf to free the returned buffer.
3201 */
3202
3203PRQINFO3* prthEnumQueues(PULONG pulReturned) // out: no. of queues found
3204{
3205 SPLERR rc;
3206 ULONG cTotal;
3207 ULONG cbNeeded = 0;
3208 PRQINFO3 *pprq3 = NULL;
3209
3210 // count queues & get number of bytes needed for buffer
3211 rc = SplEnumQueue(NULL, // default computer
3212 3, // detail level
3213 NULL, // pbuf
3214 0L, // cbBuf
3215 pulReturned, // out: entries returned
3216 &cTotal, // out: total entries available
3217 &cbNeeded,
3218 NULL); // reserved
3219
3220 if (cbNeeded)
3221 {
3222 pprq3 = (PRQINFO3*)malloc(cbNeeded);
3223 if (pprq3)
3224 {
3225 // enum the queues
3226 rc = SplEnumQueue(NULL,
3227 3,
3228 pprq3,
3229 cbNeeded,
3230 pulReturned,
3231 &cTotal,
3232 &cbNeeded,
3233 NULL);
3234 }
3235 }
3236
3237 return (pprq3);
3238}
3239
3240/*
3241 *@@ prthFreeBuf:
3242 *
3243 */
3244
3245VOID prthFreeBuf(PVOID pprq3)
3246{
3247 if (pprq3)
3248 free(pprq3);
3249}
3250
3251/*
3252 *@@ prthCreatePrinterDC:
3253 * creates a device context for the printer
3254 * specified by the given printer queue.
3255 *
3256 * As a nifty feature, this returns printer
3257 * device resolution automatically in the
3258 * specified buffer.
3259 *
3260 * Returns NULLHANDLE (== DEV_ERROR) on errors.
3261 *
3262 * Use DevCloseDC to destroy the DC.
3263 *
3264 * Based on print sample by Peter Fitzsimmons, Fri 95-09-29 02:47:16am.
3265 */
3266
3267HDC prthCreatePrinterDC(HAB hab,
3268 PRQINFO3 *pprq3,
3269 PLONG palRes) // out: 2 longs holding horizontal and vertical
3270 // printer resolution in pels per inch
3271{
3272 HDC hdc = NULLHANDLE;
3273 DEVOPENSTRUC dos;
3274 PSZ p;
3275
3276 memset(&dos, 0, sizeof(dos));
3277 p = strrchr(pprq3->pszDriverName, '.');
3278 if (p)
3279 *p = 0; // del everything after '.'
3280
3281 dos.pszLogAddress = pprq3->pszName;
3282 dos.pszDriverName = pprq3->pszDriverName;
3283 dos.pdriv = pprq3->pDriverData;
3284 dos.pszDataType = "PM_Q_STD";
3285 hdc = DevOpenDC(hab,
3286 OD_QUEUED,
3287 "*",
3288 4L, // count of items in next param
3289 (PDEVOPENDATA)&dos,
3290 0); // compatible DC
3291
3292 if (hdc)
3293 DevQueryCaps(hdc,
3294 CAPS_HORIZONTAL_FONT_RES,
3295 2,
3296 palRes); // buffer
3297
3298 return (hdc);
3299}
3300
3301/*
3302 *@@ prthQueryForms:
3303 * returns a buffer containing all forms
3304 * supported by the specified printer DC.
3305 *
3306 * Use prthFreeBuf to free the returned
3307 * buffer.
3308 *
3309 * HCINFO uses different model spaces for
3310 * the returned info. See PMREF for details.
3311 */
3312
3313HCINFO* prthQueryForms(HDC hdc,
3314 PULONG pulCount)
3315{
3316 HCINFO *pahci = NULL;
3317
3318 LONG cForms;
3319
3320 // get form count
3321 cForms = DevQueryHardcopyCaps(hdc, 0L, 0L, NULL); // phci);
3322 if (cForms)
3323 {
3324 pahci = (HCINFO*)malloc(cForms * sizeof(HCINFO));
3325 if (pahci)
3326 {
3327 *pulCount = DevQueryHardcopyCaps(hdc, 0, cForms, pahci);
3328 }
3329 }
3330
3331 return (pahci);
3332}
3333
3334/*
3335 *@@ prthCreatePS:
3336 * creates a "normal" presentation space from the specified
3337 * printer device context (which can be opened thru
3338 * prthCreatePrinterDC).
3339 *
3340 * Returns NULLHANDLE on errors.
3341 *
3342 * Based on print sample by Peter Fitzsimmons, Fri 95-09-29 02:47:16am.
3343 */
3344
3345HPS prthCreatePS(HAB hab, // in: anchor block
3346 HDC hdc, // in: printer device context
3347 ULONG ulUnits) // in: one of:
3348 // -- PU_PELS
3349 // -- PU_LOMETRIC
3350 // -- PU_HIMETRIC
3351 // -- PU_LOENGLISH
3352 // -- PU_HIENGLISH
3353 // -- PU_TWIPS
3354{
3355 SIZEL sizel;
3356
3357 sizel.cx = 0;
3358 sizel.cy = 0;
3359 return (GpiCreatePS(hab,
3360 hdc,
3361 &sizel,
3362 ulUnits | GPIA_ASSOC | GPIT_NORMAL));
3363}
3364
3365/*
3366 *@@ prthStartDoc:
3367 * calls DevEscape with DEVESC_STARTDOC.
3368 * This must be called before any painting
3369 * into the HDC's HPS. Any GPI calls made
3370 * before this are ignored.
3371 *
3372 * pszDocTitle appears in the spooler.
3373 */
3374
3375VOID prthStartDoc(HDC hdc,
3376 PSZ pszDocTitle)
3377{
3378 DevEscape(hdc,
3379 DEVESC_STARTDOC,
3380 strlen(pszDocTitle),
3381 pszDocTitle,
3382 0L,
3383 0L);
3384}
3385
3386/*
3387 *@@ prthNextPage:
3388 * calls DevEscape with DEVESC_NEWFRAME.
3389 * Signals when an application has finished writing to a page and wants to
3390 * start a new page. It is similar to GpiErase processing for a screen device
3391 * context, and causes a reset of the attributes. This escape is used with a
3392 * printer device to advance to a new page.
3393 */
3394
3395VOID prthNextPage(HDC hdc)
3396{
3397 DevEscape(hdc,
3398 DEVESC_NEWFRAME,
3399 0,
3400 0,
3401 0,
3402 0);
3403}
3404
3405/*
3406 *@@ prthEndDoc:
3407 * calls DevEscape with DEVESC_ENDDOC
3408 * and disassociates the HPS from the HDC.
3409 * Call this right before doing
3410 + GpiDestroyPS(hps);
3411 + DevCloseDC(hdc);
3412 */
3413
3414VOID prthEndDoc(HDC hdc,
3415 HPS hps)
3416{
3417 DevEscape(hdc, DEVESC_ENDDOC, 0L, 0L, 0, NULL);
3418 GpiAssociate(hps, NULLHANDLE);
3419}
3420
3421/*
3422 *@@ txvPrint:
3423 * this does the actual printing.
3424 */
3425
3426BOOL txvPrint(HAB hab,
3427 HDC hdc, // in: printer device context
3428 HPS hps, // in: printer presentation space (using PU_PELS)
3429 PSZ pszViewText, // in: text to print
3430 ULONG ulSize, // in: default font point size
3431 PSZ pszFaceName, // in: default font face name
3432 HCINFO *phci, // in: hardcopy form to use
3433 PSZ pszDocTitle, // in: document title (appears in spooler)
3434 FNPRINTCALLBACK *pfnCallback)
3435{
3436 RECTL rclPageDevice,
3437 rclPageWorld;
3438 XFORMATDATA xfd;
3439 BOOL fAnotherPage = FALSE;
3440 ULONG ulCurrentLineIndex = 0,
3441 ulCurrentPage = 1;
3442 ULONG ulCurrentYOfs = 0;
3443
3444 /* MATRIXLF matlf;
3445 POINTL ptlCenter;
3446 FIXED scalars[2]; */
3447
3448 // important: we must do a STARTDOC before we use the printer HPS.
3449 prthStartDoc(hdc,
3450 pszDocTitle);
3451
3452 // the PS is in TWIPS, but our world coordinate
3453 // space is in pels, so we need to transform
3454 /* GpiQueryViewingTransformMatrix(hps,
3455 1L,
3456 &matlf);
3457 ptlCenter.x = 0;
3458 ptlCenter.y = 0;
3459 scalars[0] = MAKEFIXED(2,0);
3460 scalars[1] = MAKEFIXED(3,0);
3461
3462 GpiScale (hps,
3463 &matlf,
3464 TRANSFORM_REPLACE,
3465 scalars,
3466 &ptlCenter); */
3467
3468 // initialize format with font from window
3469 txvInitFormat(&xfd);
3470
3471 /* txvSetFormatFont(hps,
3472 &xfd,
3473 ulSize,
3474 pszFaceName); */
3475
3476 // use text from window
3477 xstrcpy(&xfd.strViewText, pszViewText, 0);
3478
3479 // setup page
3480 GpiQueryPageViewport(hps,
3481 &rclPageDevice);
3482 // this is in device units; convert this
3483 // to the world coordinate space of the printer PS
3484 memcpy(&rclPageWorld, &rclPageDevice, sizeof(RECTL));
3485 GpiConvert(hps,
3486 CVTC_DEVICE, // source
3487 CVTC_WORLD,
3488 2, // 2 points, it's a rectangle
3489 (PPOINTL)&rclPageWorld);
3490
3491 // left and bottom margins are in millimeters...
3492 /* rclPage.xLeft = 100; // ###
3493 rclPage.yBottom = 100;
3494 rclPage.xRight = rclPage.xLeft + phci->xPels;
3495 rclPage.yTop = rclPage.yBottom + phci->yPels; */
3496
3497 txvFormatText(hps,
3498 &xfd, // in: ptxvd->rclViewText
3499 &rclPageWorld,
3500 TRUE);
3501
3502 do
3503 {
3504 _Pmpf(("---- printing page %d",
3505 ulCurrentPage));
3506
3507 fAnotherPage = txvPaintText(hab,
3508 hps,
3509 &xfd,
3510 &rclPageWorld,
3511 0,
3512 &ulCurrentYOfs,
3513 FALSE, // draw only fully visible lines
3514 &ulCurrentLineIndex); // in/out: line to start with
3515 if (fAnotherPage)
3516 {
3517 prthNextPage(hdc);
3518
3519 if (pfnCallback(ulCurrentPage++, 0) == FALSE)
3520 fAnotherPage = FALSE;
3521 }
3522 } while (fAnotherPage);
3523
3524 prthEndDoc(hdc, hps);
3525
3526 return (TRUE);
3527}
3528
3529/*
3530 *@@ txvPrintWindow:
3531 * one-shot function which prints the contents
3532 * of the specified XTextView control to the
3533 * default printer, using the default form.
3534 *
3535 * Returns a nonzero value upon errors.
3536 *
3537 * Based on print sample by Peter Fitzsimmons, Fri 95-09-29 02:47:16am.
3538 */
3539
3540int txvPrintWindow(HWND hwndTextView,
3541 PSZ pszDocTitle, // in: document title (appears in spooler)
3542 FNPRINTCALLBACK *pfnCallback)
3543{
3544 int irc = 0;
3545
3546 PTEXTVIEWWINDATA ptxvd = (PTEXTVIEWWINDATA)WinQueryWindowPtr(hwndTextView, QWL_USER);
3547
3548 if (!ptxvd)
3549 irc = 1;
3550 else
3551 {
3552 ULONG cReturned = 0;
3553 PRQINFO3 *pprq3 = prthEnumQueues(&cReturned);
3554 HDC hdc = NULLHANDLE;
3555 LONG caps[2];
3556
3557 // find default queue
3558 if (pprq3)
3559 {
3560 ULONG i;
3561 // search for default queue;
3562 for (i = 0; i < cReturned; i++)
3563 if (pprq3[i].fsType & PRQ3_TYPE_APPDEFAULT)
3564 {
3565 hdc = prthCreatePrinterDC(ptxvd->hab,
3566 &pprq3[i],
3567 caps);
3568
3569 break;
3570 }
3571 prthFreeBuf(pprq3);
3572 }
3573
3574 if (!hdc)
3575 irc = 2;
3576 else
3577 {
3578 // OK, we got a printer DC:
3579 HPS hps;
3580 ULONG cForms = 0;
3581 HCINFO *pahci,
3582 *phciSelected = 0;
3583
3584 // find default form
3585 pahci = prthQueryForms(hdc,
3586 &cForms);
3587 if (pahci)
3588 {
3589 HCINFO *phciThis = pahci;
3590 ULONG i;
3591 for (i = 0;
3592 i < cForms;
3593 i++, phciThis++)
3594 {
3595 if (phciThis->flAttributes & HCAPS_CURRENT)
3596 {
3597 phciSelected = phciThis;
3598 }
3599 }
3600 }
3601
3602 if (!phciSelected)
3603 irc = 3;
3604 else
3605 {
3606 // create printer PS
3607 hps = prthCreatePS(ptxvd->hab,
3608 hdc,
3609 PU_PELS);
3610
3611 if (hps == GPI_ERROR)
3612 irc = 4;
3613 else
3614 {
3615 PSZ pszFont;
3616 ULONG ulSize = 0;
3617 PSZ pszFaceName = 0;
3618
3619 if ((pszFont = winhQueryWindowFont(hwndTextView)))
3620 gpihSplitPresFont(pszFont,
3621 &ulSize,
3622 &pszFaceName);
3623 txvPrint(ptxvd->hab,
3624 hdc,
3625 hps,
3626 ptxvd->xfd.strViewText.psz,
3627 ulSize,
3628 pszFaceName,
3629 phciSelected,
3630 pszDocTitle,
3631 pfnCallback);
3632
3633 if (pszFont)
3634 free(pszFont);
3635
3636 GpiDestroyPS(hps);
3637 }
3638 }
3639 DevCloseDC(hdc);
3640 }
3641 }
3642
3643 return (irc);
3644}
3645
3646
Note: See TracBrowser for help on using the repository browser.