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

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

Tons of updates.

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