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

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

Updated string helpers.

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