[COMCTL32] Sync with Wine Staging 1.7.55. CORE-10536
[reactos.git] / reactos / dll / win32 / comctl32 / syslink.c
1 /*
2 * SysLink control
3 *
4 * Copyright 2004 - 2006 Thomas Weidenmueller <w3seek@reactos.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 * NOTES
21 *
22 * This code was audited for completeness against the documented features
23 * of Comctl32.dll version 6.0 on Apr. 4, 2005, by Dimitrie O. Paun.
24 *
25 * Unless otherwise noted, we believe this code to be complete, as per
26 * the specification mentioned above.
27 * If you discover missing features, or bugs, please note them below.
28 */
29
30 #include "comctl32.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(syslink);
33
34 INT WINAPI StrCmpNIW(LPCWSTR,LPCWSTR,INT);
35
36 typedef struct
37 {
38 int nChars;
39 int nSkip;
40 RECT rc;
41 } DOC_TEXTBLOCK, *PDOC_TEXTBLOCK;
42
43 #define LIF_FLAGSMASK (LIF_STATE | LIF_ITEMID | LIF_URL)
44 #define LIS_MASK (LIS_FOCUSED | LIS_ENABLED | LIS_VISITED)
45
46 typedef enum
47 {
48 slText = 0,
49 slLink
50 } SL_ITEM_TYPE;
51
52 typedef struct _DOC_ITEM
53 {
54 struct _DOC_ITEM *Next; /* Address to the next item */
55 UINT nText; /* Number of characters of the text */
56 SL_ITEM_TYPE Type; /* type of the item */
57 PDOC_TEXTBLOCK Blocks; /* Array of text blocks */
58 union
59 {
60 struct
61 {
62 UINT state; /* Link state */
63 WCHAR *szID; /* Link ID string */
64 WCHAR *szUrl; /* Link URL string */
65 } Link;
66 struct
67 {
68 UINT Dummy;
69 } Text;
70 } u;
71 WCHAR Text[1]; /* Text of the document item */
72 } DOC_ITEM, *PDOC_ITEM;
73
74 typedef struct
75 {
76 HWND Self; /* The window handle for this control */
77 HWND Notify; /* The parent handle to receive notifications */
78 DWORD Style; /* Styles for this control */
79 PDOC_ITEM Items; /* Address to the first document item */
80 BOOL HasFocus; /* Whether the control has the input focus */
81 int MouseDownID; /* ID of the link that the mouse button first selected */
82 HFONT Font; /* Handle to the font for text */
83 HFONT LinkFont; /* Handle to the font for links */
84 COLORREF TextColor; /* Color of the text */
85 COLORREF LinkColor; /* Color of links */
86 COLORREF VisitedColor; /* Color of visited links */
87 WCHAR BreakChar; /* Break Character for the current font */
88 BOOL IgnoreReturn; /* (infoPtr->Style & LWS_IGNORERETURN) on creation */
89 } SYSLINK_INFO;
90
91 static const WCHAR SL_LINKOPEN[] = { '<','a', 0 };
92 static const WCHAR SL_HREF[] = { 'h','r','e','f','=','\"',0 };
93 static const WCHAR SL_ID[] = { 'i','d','=','\"',0 };
94 static const WCHAR SL_LINKCLOSE[] = { '<','/','a','>',0 };
95
96 /* Control configuration constants */
97
98 #define SL_LEFTMARGIN (0)
99 #define SL_TOPMARGIN (0)
100 #define SL_RIGHTMARGIN (0)
101 #define SL_BOTTOMMARGIN (0)
102
103 /***********************************************************************
104 * SYSLINK_FreeDocItem
105 * Frees all data and gdi objects associated with a document item
106 */
107 static VOID SYSLINK_FreeDocItem (PDOC_ITEM DocItem)
108 {
109 if(DocItem->Type == slLink)
110 {
111 Free(DocItem->u.Link.szID);
112 Free(DocItem->u.Link.szUrl);
113 }
114
115 /* we don't free Text because it's just a pointer to a character in the
116 entire window text string */
117
118 Free(DocItem);
119 }
120
121 /***********************************************************************
122 * SYSLINK_AppendDocItem
123 * Create and append a new document item.
124 */
125 static PDOC_ITEM SYSLINK_AppendDocItem (SYSLINK_INFO *infoPtr, LPCWSTR Text, UINT textlen,
126 SL_ITEM_TYPE type, PDOC_ITEM LastItem)
127 {
128 PDOC_ITEM Item;
129
130 textlen = min(textlen, strlenW(Text));
131 Item = Alloc(FIELD_OFFSET(DOC_ITEM, Text[textlen + 1]));
132 if(Item == NULL)
133 {
134 ERR("Failed to alloc DOC_ITEM structure!\n");
135 return NULL;
136 }
137
138 Item->Next = NULL;
139 Item->nText = textlen;
140 Item->Type = type;
141 Item->Blocks = NULL;
142
143 if(LastItem != NULL)
144 {
145 LastItem->Next = Item;
146 }
147 else
148 {
149 infoPtr->Items = Item;
150 }
151
152 lstrcpynW(Item->Text, Text, textlen + 1);
153
154 return Item;
155 }
156
157 /***********************************************************************
158 * SYSLINK_ClearDoc
159 * Clears the document tree
160 */
161 static VOID SYSLINK_ClearDoc (SYSLINK_INFO *infoPtr)
162 {
163 PDOC_ITEM Item, Next;
164
165 Item = infoPtr->Items;
166 while(Item != NULL)
167 {
168 Next = Item->Next;
169 SYSLINK_FreeDocItem(Item);
170 Item = Next;
171 }
172
173 infoPtr->Items = NULL;
174 }
175
176 /***********************************************************************
177 * SYSLINK_StrCmpNIW
178 * Wrapper for StrCmpNIW to ensure 'len' is not too big.
179 */
180 static INT SYSLINK_StrCmpNIW (LPCWSTR str, LPCWSTR comp, INT len)
181 {
182 INT i;
183
184 for(i = 0; i < len; i++)
185 {
186 if(!str[i])
187 {
188 len = i + 1;
189 break;
190 }
191 }
192
193 return StrCmpNIW(str, comp, len);
194 }
195
196 /***********************************************************************
197 * SYSLINK_ParseText
198 * Parses the window text string and creates a document. Returns the
199 * number of document items created.
200 */
201 static UINT SYSLINK_ParseText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
202 {
203 LPCWSTR current, textstart = NULL, linktext = NULL, firsttag = NULL;
204 int taglen = 0, textlen = 0, linklen = 0, docitems = 0;
205 PDOC_ITEM Last = NULL;
206 SL_ITEM_TYPE CurrentType = slText;
207 LPCWSTR lpID, lpUrl;
208 UINT lenId, lenUrl;
209
210 TRACE("(%p %s)\n", infoPtr, debugstr_w(Text));
211
212 for(current = Text; *current != 0;)
213 {
214 if(*current == '<')
215 {
216 if(!SYSLINK_StrCmpNIW(current, SL_LINKOPEN, 2) && (CurrentType == slText))
217 {
218 BOOL ValidParam = FALSE, ValidLink = FALSE;
219
220 if(*(current + 2) == '>')
221 {
222 /* we just have to deal with a <a> tag */
223 taglen = 3;
224 ValidLink = TRUE;
225 ValidParam = TRUE;
226 firsttag = current;
227 linklen = 0;
228 lpID = NULL;
229 lpUrl = NULL;
230 }
231 else if(*(current + 2) == infoPtr->BreakChar)
232 {
233 /* we expect parameters, parse them */
234 LPCWSTR *CurrentParameter = NULL, tmp;
235 UINT *CurrentParameterLen = NULL;
236
237 taglen = 3;
238 tmp = current + taglen;
239 lpID = NULL;
240 lpUrl = NULL;
241
242 CheckParameter:
243 /* compare the current position with all known parameters */
244 if(!SYSLINK_StrCmpNIW(tmp, SL_HREF, 6))
245 {
246 taglen += 6;
247 ValidParam = TRUE;
248 CurrentParameter = &lpUrl;
249 CurrentParameterLen = &lenUrl;
250 }
251 else if(!SYSLINK_StrCmpNIW(tmp, SL_ID, 4))
252 {
253 taglen += 4;
254 ValidParam = TRUE;
255 CurrentParameter = &lpID;
256 CurrentParameterLen = &lenId;
257 }
258 else
259 {
260 ValidParam = FALSE;
261 }
262
263 if(ValidParam)
264 {
265 /* we got a known parameter, now search until the next " character.
266 If we can't find a " character, there's a syntax error and we just assume it's text */
267 ValidParam = FALSE;
268 *CurrentParameter = current + taglen;
269 *CurrentParameterLen = 0;
270
271 for(tmp = *CurrentParameter; *tmp != 0; tmp++)
272 {
273 taglen++;
274 if(*tmp == '\"')
275 {
276 ValidParam = TRUE;
277 tmp++;
278 break;
279 }
280 (*CurrentParameterLen)++;
281 }
282 }
283 if(ValidParam)
284 {
285 /* we're done with this parameter, now there are only 2 possibilities:
286 * 1. another parameter is coming, so expect a ' ' (space) character
287 * 2. the tag is being closed, so expect a '<' character
288 */
289 if(*tmp == infoPtr->BreakChar)
290 {
291 /* we expect another parameter, do the whole thing again */
292 taglen++;
293 tmp++;
294 goto CheckParameter;
295 }
296 else if(*tmp == '>')
297 {
298 /* the tag is being closed, we're done */
299 ValidLink = TRUE;
300 taglen++;
301 }
302 }
303 }
304
305 if(ValidLink && ValidParam)
306 {
307 /* the <a ...> tag appears to be valid. save all information
308 so we can add the link if we find a valid </a> tag later */
309 CurrentType = slLink;
310 linktext = current + taglen;
311 linklen = 0;
312 firsttag = current;
313 }
314 else
315 {
316 taglen = 1;
317 lpID = NULL;
318 lpUrl = NULL;
319 if(textstart == NULL)
320 {
321 textstart = current;
322 }
323 }
324 }
325 else if(!SYSLINK_StrCmpNIW(current, SL_LINKCLOSE, 4) && (CurrentType == slLink) && firsttag)
326 {
327 /* there's a <a...> tag opened, first add the previous text, if present */
328 if(textstart != NULL && textlen > 0 && firsttag > textstart)
329 {
330 Last = SYSLINK_AppendDocItem(infoPtr, textstart, firsttag - textstart, slText, Last);
331 if(Last == NULL)
332 {
333 ERR("Unable to create new document item!\n");
334 return docitems;
335 }
336 docitems++;
337 textstart = NULL;
338 textlen = 0;
339 }
340
341 /* now it's time to add the link to the document */
342 current += 4;
343 if(linktext != NULL && linklen > 0)
344 {
345 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slLink, Last);
346 if(Last == NULL)
347 {
348 ERR("Unable to create new document item!\n");
349 return docitems;
350 }
351 docitems++;
352 if(CurrentType == slLink)
353 {
354 int nc;
355
356 if(!(infoPtr->Style & WS_DISABLED))
357 {
358 Last->u.Link.state |= LIS_ENABLED;
359 }
360 /* Copy the tag parameters */
361 if(lpID != NULL)
362 {
363 nc = min(lenId, strlenW(lpID));
364 nc = min(nc, MAX_LINKID_TEXT - 1);
365 Last->u.Link.szID = Alloc((nc + 1) * sizeof(WCHAR));
366 if(Last->u.Link.szID != NULL)
367 {
368 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
369 }
370 }
371 else
372 Last->u.Link.szID = NULL;
373 if(lpUrl != NULL)
374 {
375 nc = min(lenUrl, strlenW(lpUrl));
376 nc = min(nc, L_MAX_URL_LENGTH - 1);
377 Last->u.Link.szUrl = Alloc((nc + 1) * sizeof(WCHAR));
378 if(Last->u.Link.szUrl != NULL)
379 {
380 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
381 }
382 }
383 else
384 Last->u.Link.szUrl = NULL;
385 }
386 linktext = NULL;
387 }
388 CurrentType = slText;
389 firsttag = NULL;
390 textstart = NULL;
391 continue;
392 }
393 else
394 {
395 /* we don't know what tag it is, so just continue */
396 taglen = 1;
397 linklen++;
398 if(CurrentType == slText && textstart == NULL)
399 {
400 textstart = current;
401 }
402 }
403
404 textlen += taglen;
405 current += taglen;
406 }
407 else
408 {
409 textlen++;
410 linklen++;
411
412 /* save the pointer of the current text item if we couldn't find a tag */
413 if(textstart == NULL && CurrentType == slText)
414 {
415 textstart = current;
416 }
417
418 current++;
419 }
420 }
421
422 if(textstart != NULL && textlen > 0)
423 {
424 Last = SYSLINK_AppendDocItem(infoPtr, textstart, textlen, CurrentType, Last);
425 if(Last == NULL)
426 {
427 ERR("Unable to create new document item!\n");
428 return docitems;
429 }
430 if(CurrentType == slLink)
431 {
432 int nc;
433
434 if(!(infoPtr->Style & WS_DISABLED))
435 {
436 Last->u.Link.state |= LIS_ENABLED;
437 }
438 /* Copy the tag parameters */
439 if(lpID != NULL)
440 {
441 nc = min(lenId, strlenW(lpID));
442 nc = min(nc, MAX_LINKID_TEXT - 1);
443 Last->u.Link.szID = Alloc((nc + 1) * sizeof(WCHAR));
444 if(Last->u.Link.szID != NULL)
445 {
446 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
447 }
448 }
449 else
450 Last->u.Link.szID = NULL;
451 if(lpUrl != NULL)
452 {
453 nc = min(lenUrl, strlenW(lpUrl));
454 nc = min(nc, L_MAX_URL_LENGTH - 1);
455 Last->u.Link.szUrl = Alloc((nc + 1) * sizeof(WCHAR));
456 if(Last->u.Link.szUrl != NULL)
457 {
458 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
459 }
460 }
461 else
462 Last->u.Link.szUrl = NULL;
463 }
464 docitems++;
465 }
466
467 if(linktext != NULL && linklen > 0)
468 {
469 /* we got an unclosed link, just display the text */
470 Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slText, Last);
471 if(Last == NULL)
472 {
473 ERR("Unable to create new document item!\n");
474 return docitems;
475 }
476 docitems++;
477 }
478
479 return docitems;
480 }
481
482 /***********************************************************************
483 * SYSLINK_RepaintLink
484 * Repaints a link.
485 */
486 static VOID SYSLINK_RepaintLink (const SYSLINK_INFO *infoPtr, const DOC_ITEM *DocItem)
487 {
488 PDOC_TEXTBLOCK bl;
489 int n;
490
491 if(DocItem->Type != slLink)
492 {
493 ERR("DocItem not a link!\n");
494 return;
495 }
496
497 bl = DocItem->Blocks;
498 if (bl != NULL)
499 {
500 n = DocItem->nText;
501
502 while(n > 0)
503 {
504 InvalidateRect(infoPtr->Self, &bl->rc, TRUE);
505 n -= bl->nChars + bl->nSkip;
506 bl++;
507 }
508 }
509 }
510
511 /***********************************************************************
512 * SYSLINK_GetLinkItemByIndex
513 * Retrieves a document link by its index
514 */
515 static PDOC_ITEM SYSLINK_GetLinkItemByIndex (const SYSLINK_INFO *infoPtr, int iLink)
516 {
517 PDOC_ITEM Current = infoPtr->Items;
518
519 while(Current != NULL)
520 {
521 if((Current->Type == slLink) && (iLink-- <= 0))
522 {
523 return Current;
524 }
525 Current = Current->Next;
526 }
527 return NULL;
528 }
529
530 /***********************************************************************
531 * SYSLINK_GetFocusLink
532 * Retrieves the link that has the LIS_FOCUSED bit
533 */
534 static PDOC_ITEM SYSLINK_GetFocusLink (const SYSLINK_INFO *infoPtr, int *LinkId)
535 {
536 PDOC_ITEM Current = infoPtr->Items;
537 int id = 0;
538
539 while(Current != NULL)
540 {
541 if(Current->Type == slLink)
542 {
543 if(Current->u.Link.state & LIS_FOCUSED)
544 {
545 if(LinkId != NULL)
546 *LinkId = id;
547 return Current;
548 }
549 id++;
550 }
551 Current = Current->Next;
552 }
553 return NULL;
554 }
555
556 /***********************************************************************
557 * SYSLINK_GetNextLink
558 * Gets the next link
559 */
560 static PDOC_ITEM SYSLINK_GetNextLink (const SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
561 {
562 for(Current = (Current != NULL ? Current->Next : infoPtr->Items);
563 Current != NULL;
564 Current = Current->Next)
565 {
566 if(Current->Type == slLink)
567 {
568 return Current;
569 }
570 }
571 return NULL;
572 }
573
574 /***********************************************************************
575 * SYSLINK_GetPrevLink
576 * Gets the previous link
577 */
578 static PDOC_ITEM SYSLINK_GetPrevLink (const SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
579 {
580 if(Current == NULL)
581 {
582 /* returns the last link */
583 PDOC_ITEM Last = NULL;
584
585 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
586 {
587 if(Current->Type == slLink)
588 {
589 Last = Current;
590 }
591 }
592 return Last;
593 }
594 else
595 {
596 /* returns the previous link */
597 PDOC_ITEM Cur, Prev = NULL;
598
599 for(Cur = infoPtr->Items; Cur != NULL; Cur = Cur->Next)
600 {
601 if(Cur == Current)
602 {
603 break;
604 }
605 if(Cur->Type == slLink)
606 {
607 Prev = Cur;
608 }
609 }
610 return Prev;
611 }
612 }
613
614 /***********************************************************************
615 * SYSLINK_WrapLine
616 * Tries to wrap a line.
617 */
618 static BOOL SYSLINK_WrapLine (LPWSTR Text, WCHAR BreakChar, int x, int *LineLen,
619 int nFit, LPSIZE Extent)
620 {
621 int i;
622
623 for (i = 0; i < nFit; i++) if (Text[i] == '\n') break;
624
625 if (i == *LineLen) return FALSE;
626
627 /* check if we're in the middle of a word */
628 if (Text[i] != '\n' && Text[i] != BreakChar)
629 {
630 /* search for the beginning of the word */
631 while (i && Text[i - 1] != BreakChar) i--;
632
633 if (i == 0)
634 {
635 Extent->cx = 0;
636 Extent->cy = 0;
637 if (x == SL_LEFTMARGIN) i = max( nFit, 1 );
638 }
639 }
640 *LineLen = i;
641 return TRUE;
642 }
643
644 /***********************************************************************
645 * SYSLINK_Render
646 * Renders the document in memory
647 */
648 static VOID SYSLINK_Render (const SYSLINK_INFO *infoPtr, HDC hdc, PRECT pRect)
649 {
650 RECT rc;
651 PDOC_ITEM Current;
652 HGDIOBJ hOldFont;
653 int x, y, LineHeight;
654 SIZE szDoc;
655 TEXTMETRICW tm;
656
657 szDoc.cx = szDoc.cy = 0;
658
659 rc = *pRect;
660 rc.right -= SL_RIGHTMARGIN;
661 rc.bottom -= SL_BOTTOMMARGIN;
662
663 if(rc.right - SL_LEFTMARGIN < 0)
664 rc.right = MAXLONG;
665 if (rc.bottom - SL_TOPMARGIN < 0)
666 rc.bottom = MAXLONG;
667
668 hOldFont = SelectObject(hdc, infoPtr->Font);
669
670 x = SL_LEFTMARGIN;
671 y = SL_TOPMARGIN;
672 GetTextMetricsW( hdc, &tm );
673 LineHeight = tm.tmHeight + tm.tmExternalLeading;
674
675 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
676 {
677 int n, nBlocks;
678 LPWSTR tx;
679 PDOC_TEXTBLOCK bl, cbl;
680 INT nFit;
681 SIZE szDim;
682 int SkipChars = 0;
683
684 if(Current->nText == 0)
685 {
686 continue;
687 }
688
689 tx = Current->Text;
690 n = Current->nText;
691
692 Free(Current->Blocks);
693 Current->Blocks = NULL;
694 bl = NULL;
695 nBlocks = 0;
696
697 if(Current->Type == slText)
698 {
699 SelectObject(hdc, infoPtr->Font);
700 }
701 else if(Current->Type == slLink)
702 {
703 SelectObject(hdc, infoPtr->LinkFont);
704 }
705
706 while(n > 0)
707 {
708 /* skip break characters unless they're the first of the doc item */
709 if(tx != Current->Text || x == SL_LEFTMARGIN)
710 {
711 if (n && *tx == '\n')
712 {
713 tx++;
714 SkipChars++;
715 n--;
716 }
717 while(n > 0 && (*tx) == infoPtr->BreakChar)
718 {
719 tx++;
720 SkipChars++;
721 n--;
722 }
723 }
724
725 if((n == 0 && SkipChars != 0) ||
726 GetTextExtentExPointW(hdc, tx, n, rc.right - x, &nFit, NULL, &szDim))
727 {
728 int LineLen = n;
729 BOOL Wrap = FALSE;
730 PDOC_TEXTBLOCK nbl;
731
732 if(n != 0)
733 {
734 Wrap = SYSLINK_WrapLine(tx, infoPtr->BreakChar, x, &LineLen, nFit, &szDim);
735
736 if(LineLen == 0)
737 {
738 /* move one line down, the word didn't fit into the line */
739 x = SL_LEFTMARGIN;
740 y += LineHeight;
741 continue;
742 }
743
744 if(LineLen != n)
745 {
746 if(!GetTextExtentExPointW(hdc, tx, LineLen, rc.right - x, NULL, NULL, &szDim))
747 {
748 if(bl != NULL)
749 {
750 Free(bl);
751 bl = NULL;
752 nBlocks = 0;
753 }
754 break;
755 }
756 }
757 }
758
759 nbl = ReAlloc(bl, (nBlocks + 1) * sizeof(DOC_TEXTBLOCK));
760 if (nbl != NULL)
761 {
762 bl = nbl;
763 nBlocks++;
764
765 cbl = bl + nBlocks - 1;
766
767 cbl->nChars = LineLen;
768 cbl->nSkip = SkipChars;
769 cbl->rc.left = x;
770 cbl->rc.top = y;
771 cbl->rc.right = x + szDim.cx;
772 cbl->rc.bottom = y + szDim.cy;
773
774 if (cbl->rc.right > szDoc.cx)
775 szDoc.cx = cbl->rc.right;
776 if (cbl->rc.bottom > szDoc.cy)
777 szDoc.cy = cbl->rc.bottom;
778
779 if(LineLen != 0)
780 {
781 x += szDim.cx;
782 if(Wrap)
783 {
784 x = SL_LEFTMARGIN;
785 y += LineHeight;
786 }
787 }
788 }
789 else
790 {
791 Free(bl);
792 bl = NULL;
793 nBlocks = 0;
794
795 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
796 break;
797 }
798 n -= LineLen;
799 tx += LineLen;
800 SkipChars = 0;
801 }
802 else
803 {
804 n--;
805 }
806 }
807
808 if(nBlocks != 0)
809 {
810 Current->Blocks = bl;
811 }
812 }
813
814 SelectObject(hdc, hOldFont);
815
816 pRect->right = pRect->left + szDoc.cx;
817 pRect->bottom = pRect->top + szDoc.cy;
818 }
819
820 /***********************************************************************
821 * SYSLINK_Draw
822 * Draws the SysLink control.
823 */
824 static LRESULT SYSLINK_Draw (const SYSLINK_INFO *infoPtr, HDC hdc)
825 {
826 RECT rc;
827 PDOC_ITEM Current;
828 HFONT hOldFont;
829 COLORREF OldTextColor, OldBkColor;
830 HBRUSH hBrush;
831 UINT text_flags = ETO_CLIPPED;
832 UINT mode = GetBkMode( hdc );
833
834 hOldFont = SelectObject(hdc, infoPtr->Font);
835 OldTextColor = SetTextColor(hdc, infoPtr->TextColor);
836 OldBkColor = SetBkColor(hdc, comctl32_color.clrWindow);
837
838 GetClientRect(infoPtr->Self, &rc);
839 rc.right -= SL_RIGHTMARGIN + SL_LEFTMARGIN;
840 rc.bottom -= SL_BOTTOMMARGIN + SL_TOPMARGIN;
841
842 if(rc.right < 0 || rc.bottom < 0) return 0;
843
844 hBrush = (HBRUSH)SendMessageW(infoPtr->Notify, WM_CTLCOLORSTATIC,
845 (WPARAM)hdc, (LPARAM)infoPtr->Self);
846 if (!(infoPtr->Style & LWS_TRANSPARENT))
847 {
848 FillRect(hdc, &rc, hBrush);
849 if (GetBkMode( hdc ) == OPAQUE) text_flags |= ETO_OPAQUE;
850 }
851 else SetBkMode( hdc, TRANSPARENT );
852
853 DeleteObject(hBrush);
854
855 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
856 {
857 int n;
858 LPWSTR tx;
859 PDOC_TEXTBLOCK bl;
860
861 bl = Current->Blocks;
862 if(bl != NULL)
863 {
864 tx = Current->Text;
865 n = Current->nText;
866
867 if(Current->Type == slText)
868 {
869 SelectObject(hdc, infoPtr->Font);
870 SetTextColor(hdc, infoPtr->TextColor);
871 }
872 else
873 {
874 SelectObject(hdc, infoPtr->LinkFont);
875 SetTextColor(hdc, (!(Current->u.Link.state & LIS_VISITED) ? infoPtr->LinkColor : infoPtr->VisitedColor));
876 }
877
878 while(n > 0)
879 {
880 tx += bl->nSkip;
881 ExtTextOutW(hdc, bl->rc.left, bl->rc.top, text_flags, &bl->rc, tx, bl->nChars, NULL);
882 if((Current->Type == slLink) && (Current->u.Link.state & LIS_FOCUSED) && infoPtr->HasFocus)
883 {
884 COLORREF PrevTextColor;
885 PrevTextColor = SetTextColor(hdc, infoPtr->TextColor);
886 DrawFocusRect(hdc, &bl->rc);
887 SetTextColor(hdc, PrevTextColor);
888 }
889 tx += bl->nChars;
890 n -= bl->nChars + bl->nSkip;
891 bl++;
892 }
893 }
894 }
895
896 SetBkColor(hdc, OldBkColor);
897 SetTextColor(hdc, OldTextColor);
898 SelectObject(hdc, hOldFont);
899 SetBkMode(hdc, mode);
900 return 0;
901 }
902
903
904 /***********************************************************************
905 * SYSLINK_Paint
906 * Handles the WM_PAINT message.
907 */
908 static LRESULT SYSLINK_Paint (const SYSLINK_INFO *infoPtr, HDC hdcParam)
909 {
910 HDC hdc;
911 PAINTSTRUCT ps;
912
913 hdc = hdcParam ? hdcParam : BeginPaint (infoPtr->Self, &ps);
914 if (hdc)
915 {
916 SYSLINK_Draw (infoPtr, hdc);
917 if (!hdcParam) EndPaint (infoPtr->Self, &ps);
918 }
919 return 0;
920 }
921
922 /***********************************************************************
923 * SYSLINK_SetFont
924 * Set new Font for the SysLink control.
925 */
926 static HFONT SYSLINK_SetFont (SYSLINK_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
927 {
928 HDC hdc;
929 LOGFONTW lf;
930 TEXTMETRICW tm;
931 RECT rcClient;
932 HFONT hOldFont = infoPtr->Font;
933 infoPtr->Font = hFont;
934
935 /* free the underline font */
936 if(infoPtr->LinkFont != NULL)
937 {
938 DeleteObject(infoPtr->LinkFont);
939 infoPtr->LinkFont = NULL;
940 }
941
942 /* Render text position and word wrapping in memory */
943 if (GetClientRect(infoPtr->Self, &rcClient))
944 {
945 hdc = GetDC(infoPtr->Self);
946 if(hdc != NULL)
947 {
948 /* create a new underline font */
949 if(GetTextMetricsW(hdc, &tm) &&
950 GetObjectW(infoPtr->Font, sizeof(LOGFONTW), &lf))
951 {
952 lf.lfUnderline = TRUE;
953 infoPtr->LinkFont = CreateFontIndirectW(&lf);
954 infoPtr->BreakChar = tm.tmBreakChar;
955 }
956 else
957 {
958 ERR("Failed to create link font!\n");
959 }
960
961 SYSLINK_Render(infoPtr, hdc, &rcClient);
962 ReleaseDC(infoPtr->Self, hdc);
963 }
964 }
965
966 if(bRedraw)
967 {
968 RedrawWindow(infoPtr->Self, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
969 }
970
971 return hOldFont;
972 }
973
974 /***********************************************************************
975 * SYSLINK_SetText
976 * Set new text for the SysLink control.
977 */
978 static LRESULT SYSLINK_SetText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
979 {
980 /* clear the document */
981 SYSLINK_ClearDoc(infoPtr);
982
983 if(Text == NULL || *Text == 0)
984 {
985 return TRUE;
986 }
987
988 /* let's parse the string and create a document */
989 if(SYSLINK_ParseText(infoPtr, Text) > 0)
990 {
991 RECT rcClient;
992
993 /* Render text position and word wrapping in memory */
994 if (GetClientRect(infoPtr->Self, &rcClient))
995 {
996 HDC hdc = GetDC(infoPtr->Self);
997 if (hdc != NULL)
998 {
999 SYSLINK_Render(infoPtr, hdc, &rcClient);
1000 ReleaseDC(infoPtr->Self, hdc);
1001
1002 InvalidateRect(infoPtr->Self, NULL, TRUE);
1003 }
1004 }
1005 }
1006
1007 return TRUE;
1008 }
1009
1010 /***********************************************************************
1011 * SYSLINK_SetFocusLink
1012 * Updates the focus status bits and focusses the specified link.
1013 * If no document item is specified, the focus bit will be removed from all links.
1014 * Returns the previous focused item.
1015 */
1016 static PDOC_ITEM SYSLINK_SetFocusLink (const SYSLINK_INFO *infoPtr, const DOC_ITEM *DocItem)
1017 {
1018 PDOC_ITEM Current, PrevFocus = NULL;
1019
1020 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1021 {
1022 if(Current->Type == slLink)
1023 {
1024 if((PrevFocus == NULL) && (Current->u.Link.state & LIS_FOCUSED))
1025 {
1026 PrevFocus = Current;
1027 }
1028
1029 if(Current == DocItem)
1030 {
1031 Current->u.Link.state |= LIS_FOCUSED;
1032 }
1033 else
1034 {
1035 Current->u.Link.state &= ~LIS_FOCUSED;
1036 }
1037 }
1038 }
1039
1040 return PrevFocus;
1041 }
1042
1043 /***********************************************************************
1044 * SYSLINK_SetItem
1045 * Sets the states and attributes of a link item.
1046 */
1047 static LRESULT SYSLINK_SetItem (const SYSLINK_INFO *infoPtr, const LITEM *Item)
1048 {
1049 PDOC_ITEM di;
1050 int nc;
1051 PWSTR szId = NULL;
1052 PWSTR szUrl = NULL;
1053 BOOL Repaint = FALSE;
1054
1055 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1056 {
1057 ERR("Invalid Flags!\n");
1058 return FALSE;
1059 }
1060
1061 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1062 if(di == NULL)
1063 {
1064 ERR("Link %d couldn't be found\n", Item->iLink);
1065 return FALSE;
1066 }
1067
1068 if(Item->mask & LIF_ITEMID)
1069 {
1070 nc = min(lstrlenW(Item->szID), MAX_LINKID_TEXT - 1);
1071 szId = Alloc((nc + 1) * sizeof(WCHAR));
1072 if(szId)
1073 {
1074 lstrcpynW(szId, Item->szID, nc + 1);
1075 }
1076 else
1077 {
1078 ERR("Unable to allocate memory for link id\n");
1079 return FALSE;
1080 }
1081 }
1082
1083 if(Item->mask & LIF_URL)
1084 {
1085 nc = min(lstrlenW(Item->szUrl), L_MAX_URL_LENGTH - 1);
1086 szUrl = Alloc((nc + 1) * sizeof(WCHAR));
1087 if(szUrl)
1088 {
1089 lstrcpynW(szUrl, Item->szUrl, nc + 1);
1090 }
1091 else
1092 {
1093 Free(szId);
1094
1095 ERR("Unable to allocate memory for link url\n");
1096 return FALSE;
1097 }
1098 }
1099
1100 if(Item->mask & LIF_ITEMID)
1101 {
1102 Free(di->u.Link.szID);
1103 di->u.Link.szID = szId;
1104 }
1105
1106 if(Item->mask & LIF_URL)
1107 {
1108 Free(di->u.Link.szUrl);
1109 di->u.Link.szUrl = szUrl;
1110 }
1111
1112 if(Item->mask & LIF_STATE)
1113 {
1114 UINT oldstate = di->u.Link.state;
1115 /* clear the masked bits */
1116 di->u.Link.state &= ~(Item->stateMask & LIS_MASK);
1117 /* copy the bits */
1118 di->u.Link.state |= (Item->state & Item->stateMask) & LIS_MASK;
1119 Repaint = (oldstate != di->u.Link.state);
1120
1121 /* update the focus */
1122 SYSLINK_SetFocusLink(infoPtr, ((di->u.Link.state & LIS_FOCUSED) ? di : NULL));
1123 }
1124
1125 if(Repaint)
1126 {
1127 SYSLINK_RepaintLink(infoPtr, di);
1128 }
1129
1130 return TRUE;
1131 }
1132
1133 /***********************************************************************
1134 * SYSLINK_GetItem
1135 * Retrieves the states and attributes of a link item.
1136 */
1137 static LRESULT SYSLINK_GetItem (const SYSLINK_INFO *infoPtr, PLITEM Item)
1138 {
1139 PDOC_ITEM di;
1140
1141 if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1142 {
1143 ERR("Invalid Flags!\n");
1144 return FALSE;
1145 }
1146
1147 di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1148 if(di == NULL)
1149 {
1150 ERR("Link %d couldn't be found\n", Item->iLink);
1151 return FALSE;
1152 }
1153
1154 if(Item->mask & LIF_STATE)
1155 {
1156 Item->state = (di->u.Link.state & Item->stateMask);
1157 if(!infoPtr->HasFocus)
1158 {
1159 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1160 Item->state &= ~LIS_FOCUSED;
1161 }
1162 }
1163
1164 if(Item->mask & LIF_ITEMID)
1165 {
1166 if(di->u.Link.szID)
1167 {
1168 lstrcpyW(Item->szID, di->u.Link.szID);
1169 }
1170 else
1171 {
1172 Item->szID[0] = 0;
1173 }
1174 }
1175
1176 if(Item->mask & LIF_URL)
1177 {
1178 if(di->u.Link.szUrl)
1179 {
1180 lstrcpyW(Item->szUrl, di->u.Link.szUrl);
1181 }
1182 else
1183 {
1184 Item->szUrl[0] = 0;
1185 }
1186 }
1187
1188 return TRUE;
1189 }
1190
1191 /***********************************************************************
1192 * SYSLINK_PtInDocItem
1193 * Determines if a point is in the region of a document item
1194 */
1195 static BOOL SYSLINK_PtInDocItem (const DOC_ITEM *DocItem, POINT pt)
1196 {
1197 PDOC_TEXTBLOCK bl;
1198 int n;
1199
1200 bl = DocItem->Blocks;
1201 if (bl != NULL)
1202 {
1203 n = DocItem->nText;
1204
1205 while(n > 0)
1206 {
1207 if (PtInRect(&bl->rc, pt))
1208 {
1209 return TRUE;
1210 }
1211 n -= bl->nChars + bl->nSkip;
1212 bl++;
1213 }
1214 }
1215
1216 return FALSE;
1217 }
1218
1219 /***********************************************************************
1220 * SYSLINK_HitTest
1221 * Determines the link the user clicked on.
1222 */
1223 static LRESULT SYSLINK_HitTest (const SYSLINK_INFO *infoPtr, PLHITTESTINFO HitTest)
1224 {
1225 PDOC_ITEM Current;
1226 int id = 0;
1227
1228 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1229 {
1230 if(Current->Type == slLink)
1231 {
1232 if(SYSLINK_PtInDocItem(Current, HitTest->pt))
1233 {
1234 HitTest->item.mask = 0;
1235 HitTest->item.iLink = id;
1236 HitTest->item.state = 0;
1237 HitTest->item.stateMask = 0;
1238 if(Current->u.Link.szID)
1239 {
1240 lstrcpyW(HitTest->item.szID, Current->u.Link.szID);
1241 }
1242 else
1243 {
1244 HitTest->item.szID[0] = 0;
1245 }
1246 if(Current->u.Link.szUrl)
1247 {
1248 lstrcpyW(HitTest->item.szUrl, Current->u.Link.szUrl);
1249 }
1250 else
1251 {
1252 HitTest->item.szUrl[0] = 0;
1253 }
1254 return TRUE;
1255 }
1256 id++;
1257 }
1258 }
1259
1260 return FALSE;
1261 }
1262
1263 /***********************************************************************
1264 * SYSLINK_GetIdealHeight
1265 * Returns the preferred height of a link at the current control's width.
1266 */
1267 static LRESULT SYSLINK_GetIdealHeight (const SYSLINK_INFO *infoPtr)
1268 {
1269 HDC hdc = GetDC(infoPtr->Self);
1270 if(hdc != NULL)
1271 {
1272 LRESULT height;
1273 TEXTMETRICW tm;
1274 HGDIOBJ hOldFont = SelectObject(hdc, infoPtr->Font);
1275
1276 if(GetTextMetricsW(hdc, &tm))
1277 {
1278 height = tm.tmHeight;
1279 }
1280 else
1281 {
1282 height = 0;
1283 }
1284 SelectObject(hdc, hOldFont);
1285 ReleaseDC(infoPtr->Self, hdc);
1286
1287 return height;
1288 }
1289 return 0;
1290 }
1291
1292 /***********************************************************************
1293 * SYSLINK_SendParentNotify
1294 * Sends a WM_NOTIFY message to the parent window.
1295 */
1296 static LRESULT SYSLINK_SendParentNotify (const SYSLINK_INFO *infoPtr, UINT code, const DOC_ITEM *Link, int iLink)
1297 {
1298 NMLINK nml;
1299
1300 nml.hdr.hwndFrom = infoPtr->Self;
1301 nml.hdr.idFrom = GetWindowLongPtrW(infoPtr->Self, GWLP_ID);
1302 nml.hdr.code = code;
1303
1304 nml.item.mask = 0;
1305 nml.item.iLink = iLink;
1306 nml.item.state = 0;
1307 nml.item.stateMask = 0;
1308 if(Link->u.Link.szID)
1309 {
1310 lstrcpyW(nml.item.szID, Link->u.Link.szID);
1311 }
1312 else
1313 {
1314 nml.item.szID[0] = 0;
1315 }
1316 if(Link->u.Link.szUrl)
1317 {
1318 lstrcpyW(nml.item.szUrl, Link->u.Link.szUrl);
1319 }
1320 else
1321 {
1322 nml.item.szUrl[0] = 0;
1323 }
1324
1325 return SendMessageW(infoPtr->Notify, WM_NOTIFY, nml.hdr.idFrom, (LPARAM)&nml);
1326 }
1327
1328 /***********************************************************************
1329 * SYSLINK_SetFocus
1330 * Handles receiving the input focus.
1331 */
1332 static LRESULT SYSLINK_SetFocus (SYSLINK_INFO *infoPtr)
1333 {
1334 PDOC_ITEM Focus;
1335
1336 infoPtr->HasFocus = TRUE;
1337
1338 /* We always select the first link, even if we activated the control using
1339 SHIFT+TAB. This is the default behavior */
1340 Focus = SYSLINK_GetNextLink(infoPtr, NULL);
1341 if(Focus != NULL)
1342 {
1343 SYSLINK_SetFocusLink(infoPtr, Focus);
1344 SYSLINK_RepaintLink(infoPtr, Focus);
1345 }
1346 return 0;
1347 }
1348
1349 /***********************************************************************
1350 * SYSLINK_KillFocus
1351 * Handles losing the input focus.
1352 */
1353 static LRESULT SYSLINK_KillFocus (SYSLINK_INFO *infoPtr)
1354 {
1355 PDOC_ITEM Focus;
1356
1357 infoPtr->HasFocus = FALSE;
1358 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1359
1360 if(Focus != NULL)
1361 {
1362 SYSLINK_RepaintLink(infoPtr, Focus);
1363 }
1364
1365 return 0;
1366 }
1367
1368 /***********************************************************************
1369 * SYSLINK_LinkAtPt
1370 * Returns a link at the specified position
1371 */
1372 static PDOC_ITEM SYSLINK_LinkAtPt (const SYSLINK_INFO *infoPtr, const POINT *pt, int *LinkId, BOOL MustBeEnabled)
1373 {
1374 PDOC_ITEM Current;
1375 int id = 0;
1376
1377 for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1378 {
1379 if((Current->Type == slLink) && SYSLINK_PtInDocItem(Current, *pt) &&
1380 (!MustBeEnabled || (Current->u.Link.state & LIS_ENABLED)))
1381 {
1382 if(LinkId != NULL)
1383 {
1384 *LinkId = id;
1385 }
1386 return Current;
1387 }
1388 id++;
1389 }
1390
1391 return NULL;
1392 }
1393
1394 /***********************************************************************
1395 * SYSLINK_LButtonDown
1396 * Handles mouse clicks
1397 */
1398 static LRESULT SYSLINK_LButtonDown (SYSLINK_INFO *infoPtr, const POINT *pt)
1399 {
1400 PDOC_ITEM Current, Old;
1401 int id;
1402
1403 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1404 if(Current != NULL)
1405 {
1406 SetFocus(infoPtr->Self);
1407
1408 Old = SYSLINK_SetFocusLink(infoPtr, Current);
1409 if(Old != NULL && Old != Current)
1410 {
1411 SYSLINK_RepaintLink(infoPtr, Old);
1412 }
1413 infoPtr->MouseDownID = id;
1414 SYSLINK_RepaintLink(infoPtr, Current);
1415 }
1416
1417 return 0;
1418 }
1419
1420 /***********************************************************************
1421 * SYSLINK_LButtonUp
1422 * Handles mouse clicks
1423 */
1424 static LRESULT SYSLINK_LButtonUp (SYSLINK_INFO *infoPtr, const POINT *pt)
1425 {
1426 if(infoPtr->MouseDownID > -1)
1427 {
1428 PDOC_ITEM Current;
1429 int id;
1430
1431 Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1432 if((Current != NULL) && (Current->u.Link.state & LIS_FOCUSED) && (infoPtr->MouseDownID == id))
1433 {
1434 SYSLINK_SendParentNotify(infoPtr, NM_CLICK, Current, id);
1435 }
1436 }
1437
1438 infoPtr->MouseDownID = -1;
1439
1440 return 0;
1441 }
1442
1443 /***********************************************************************
1444 * SYSLINK_OnEnter
1445 * Handles ENTER key events
1446 */
1447 static BOOL SYSLINK_OnEnter (const SYSLINK_INFO *infoPtr)
1448 {
1449 if(infoPtr->HasFocus && !infoPtr->IgnoreReturn)
1450 {
1451 PDOC_ITEM Focus;
1452 int id;
1453
1454 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1455 if(Focus)
1456 {
1457 SYSLINK_SendParentNotify(infoPtr, NM_RETURN, Focus, id);
1458 return TRUE;
1459 }
1460 }
1461 return FALSE;
1462 }
1463
1464 /***********************************************************************
1465 * SYSKEY_SelectNextPrevLink
1466 * Changes the currently focused link
1467 */
1468 static BOOL SYSKEY_SelectNextPrevLink (const SYSLINK_INFO *infoPtr, BOOL Prev)
1469 {
1470 if(infoPtr->HasFocus)
1471 {
1472 PDOC_ITEM Focus;
1473 int id;
1474
1475 Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1476 if(Focus != NULL)
1477 {
1478 PDOC_ITEM NewFocus, OldFocus;
1479
1480 if(Prev)
1481 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1482 else
1483 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1484
1485 if(NewFocus != NULL)
1486 {
1487 OldFocus = SYSLINK_SetFocusLink(infoPtr, NewFocus);
1488
1489 if(OldFocus && OldFocus != NewFocus)
1490 {
1491 SYSLINK_RepaintLink(infoPtr, OldFocus);
1492 }
1493 SYSLINK_RepaintLink(infoPtr, NewFocus);
1494 return TRUE;
1495 }
1496 }
1497 }
1498 return FALSE;
1499 }
1500
1501 /***********************************************************************
1502 * SYSKEY_SelectNextPrevLink
1503 * Determines if there's a next or previous link to decide whether the control
1504 * should capture the tab key message
1505 */
1506 static BOOL SYSLINK_NoNextLink (const SYSLINK_INFO *infoPtr, BOOL Prev)
1507 {
1508 PDOC_ITEM Focus, NewFocus;
1509
1510 Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1511 if(Prev)
1512 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1513 else
1514 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1515
1516 return NewFocus == NULL;
1517 }
1518
1519 /***********************************************************************
1520 * SYSLINK_GetIdealSize
1521 * Calculates the ideal size of a link control at a given maximum width.
1522 */
1523 static VOID SYSLINK_GetIdealSize (const SYSLINK_INFO *infoPtr, int cxMaxWidth, LPSIZE lpSize)
1524 {
1525 RECT rc;
1526 HDC hdc;
1527
1528 rc.left = rc.top = rc.bottom = 0;
1529 rc.right = cxMaxWidth;
1530
1531 hdc = GetDC(infoPtr->Self);
1532 if (hdc != NULL)
1533 {
1534 HGDIOBJ hOldFont = SelectObject(hdc, infoPtr->Font);
1535
1536 SYSLINK_Render(infoPtr, hdc, &rc);
1537
1538 SelectObject(hdc, hOldFont);
1539 ReleaseDC(infoPtr->Self, hdc);
1540
1541 lpSize->cx = rc.right;
1542 lpSize->cy = rc.bottom;
1543 }
1544 }
1545
1546 /***********************************************************************
1547 * SysLinkWindowProc
1548 */
1549 static LRESULT WINAPI SysLinkWindowProc(HWND hwnd, UINT message,
1550 WPARAM wParam, LPARAM lParam)
1551 {
1552 SYSLINK_INFO *infoPtr;
1553
1554 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd, message, wParam, lParam);
1555
1556 infoPtr = (SYSLINK_INFO *)GetWindowLongPtrW(hwnd, 0);
1557
1558 if (!infoPtr && message != WM_CREATE)
1559 return DefWindowProcW(hwnd, message, wParam, lParam);
1560
1561 switch(message) {
1562 case WM_PRINTCLIENT:
1563 case WM_PAINT:
1564 return SYSLINK_Paint (infoPtr, (HDC)wParam);
1565
1566 case WM_ERASEBKGND:
1567 if (!(infoPtr->Style & LWS_TRANSPARENT))
1568 {
1569 HDC hdc = (HDC)wParam;
1570 HBRUSH brush = CreateSolidBrush( comctl32_color.clrWindow );
1571 RECT rect;
1572
1573 GetClipBox( hdc, &rect );
1574 FillRect( hdc, &rect, brush );
1575 DeleteObject( brush );
1576 return 1;
1577 }
1578 return 0;
1579
1580 case WM_SETCURSOR:
1581 {
1582 LHITTESTINFO ht;
1583 DWORD mp = GetMessagePos();
1584
1585 ht.pt.x = (short)LOWORD(mp);
1586 ht.pt.y = (short)HIWORD(mp);
1587
1588 ScreenToClient(infoPtr->Self, &ht.pt);
1589 if(SYSLINK_HitTest (infoPtr, &ht))
1590 {
1591 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_HAND));
1592 return TRUE;
1593 }
1594
1595 return DefWindowProcW(hwnd, message, wParam, lParam);
1596 }
1597
1598 case WM_SIZE:
1599 {
1600 RECT rcClient;
1601 if (GetClientRect(infoPtr->Self, &rcClient))
1602 {
1603 HDC hdc = GetDC(infoPtr->Self);
1604 if(hdc != NULL)
1605 {
1606 SYSLINK_Render(infoPtr, hdc, &rcClient);
1607 ReleaseDC(infoPtr->Self, hdc);
1608 }
1609 }
1610 return 0;
1611 }
1612
1613 case WM_GETFONT:
1614 return (LRESULT)infoPtr->Font;
1615
1616 case WM_SETFONT:
1617 return (LRESULT)SYSLINK_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1618
1619 case WM_SETTEXT:
1620 SYSLINK_SetText(infoPtr, (LPWSTR)lParam);
1621 return DefWindowProcW(hwnd, message, wParam, lParam);
1622
1623 case WM_LBUTTONDOWN:
1624 {
1625 POINT pt;
1626 pt.x = (short)LOWORD(lParam);
1627 pt.y = (short)HIWORD(lParam);
1628 return SYSLINK_LButtonDown(infoPtr, &pt);
1629 }
1630 case WM_LBUTTONUP:
1631 {
1632 POINT pt;
1633 pt.x = (short)LOWORD(lParam);
1634 pt.y = (short)HIWORD(lParam);
1635 return SYSLINK_LButtonUp(infoPtr, &pt);
1636 }
1637
1638 case WM_KEYDOWN:
1639 {
1640 switch(wParam)
1641 {
1642 case VK_RETURN:
1643 SYSLINK_OnEnter(infoPtr);
1644 return 0;
1645 case VK_TAB:
1646 {
1647 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1648 SYSKEY_SelectNextPrevLink(infoPtr, shift);
1649 return 0;
1650 }
1651 default:
1652 return DefWindowProcW(hwnd, message, wParam, lParam);
1653 }
1654 }
1655
1656 case WM_GETDLGCODE:
1657 {
1658 LRESULT Ret = DLGC_HASSETSEL;
1659 int vk = (lParam != 0 ? (int)((LPMSG)lParam)->wParam : 0);
1660 switch(vk)
1661 {
1662 case VK_RETURN:
1663 Ret |= DLGC_WANTMESSAGE;
1664 break;
1665 case VK_TAB:
1666 {
1667 BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1668 if(!SYSLINK_NoNextLink(infoPtr, shift))
1669 {
1670 Ret |= DLGC_WANTTAB;
1671 }
1672 else
1673 {
1674 Ret |= DLGC_WANTCHARS;
1675 }
1676 break;
1677 }
1678 }
1679 return Ret;
1680 }
1681
1682 case WM_NCHITTEST:
1683 {
1684 POINT pt;
1685 RECT rc;
1686 pt.x = (short)LOWORD(lParam);
1687 pt.y = (short)HIWORD(lParam);
1688
1689 GetClientRect(infoPtr->Self, &rc);
1690 ScreenToClient(infoPtr->Self, &pt);
1691 if(pt.x < 0 || pt.y < 0 || pt.x > rc.right || pt.y > rc.bottom)
1692 {
1693 return HTNOWHERE;
1694 }
1695
1696 if(SYSLINK_LinkAtPt(infoPtr, &pt, NULL, FALSE))
1697 {
1698 return HTCLIENT;
1699 }
1700
1701 return HTTRANSPARENT;
1702 }
1703
1704 case LM_HITTEST:
1705 return SYSLINK_HitTest(infoPtr, (PLHITTESTINFO)lParam);
1706
1707 case LM_SETITEM:
1708 return SYSLINK_SetItem(infoPtr, (PLITEM)lParam);
1709
1710 case LM_GETITEM:
1711 return SYSLINK_GetItem(infoPtr, (PLITEM)lParam);
1712
1713 case LM_GETIDEALHEIGHT:
1714 if (lParam)
1715 {
1716 /* LM_GETIDEALSIZE */
1717 SYSLINK_GetIdealSize(infoPtr, (int)wParam, (LPSIZE)lParam);
1718 }
1719 return SYSLINK_GetIdealHeight(infoPtr);
1720
1721 case WM_SETFOCUS:
1722 return SYSLINK_SetFocus(infoPtr);
1723
1724 case WM_KILLFOCUS:
1725 return SYSLINK_KillFocus(infoPtr);
1726
1727 case WM_ENABLE:
1728 infoPtr->Style &= ~WS_DISABLED;
1729 infoPtr->Style |= (wParam ? 0 : WS_DISABLED);
1730 InvalidateRect (infoPtr->Self, NULL, FALSE);
1731 return 0;
1732
1733 case WM_STYLECHANGED:
1734 if (wParam == GWL_STYLE)
1735 {
1736 infoPtr->Style = ((LPSTYLESTRUCT)lParam)->styleNew;
1737
1738 InvalidateRect(infoPtr->Self, NULL, TRUE);
1739 }
1740 return 0;
1741
1742 case WM_CREATE:
1743 /* allocate memory for info struct */
1744 infoPtr = Alloc (sizeof(SYSLINK_INFO));
1745 if (!infoPtr) return -1;
1746 SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1747
1748 /* initialize the info struct */
1749 infoPtr->Self = hwnd;
1750 infoPtr->Notify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1751 infoPtr->Style = ((LPCREATESTRUCTW)lParam)->style;
1752 infoPtr->Font = 0;
1753 infoPtr->LinkFont = 0;
1754 infoPtr->Items = NULL;
1755 infoPtr->HasFocus = FALSE;
1756 infoPtr->MouseDownID = -1;
1757 infoPtr->TextColor = comctl32_color.clrWindowText;
1758 infoPtr->LinkColor = comctl32_color.clrHighlight;
1759 infoPtr->VisitedColor = comctl32_color.clrHighlight;
1760 infoPtr->BreakChar = ' ';
1761 infoPtr->IgnoreReturn = infoPtr->Style & LWS_IGNORERETURN;
1762 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd);
1763 SYSLINK_SetText(infoPtr, ((LPCREATESTRUCTW)lParam)->lpszName);
1764 return 0;
1765
1766 case WM_DESTROY:
1767 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd);
1768 SYSLINK_ClearDoc(infoPtr);
1769 if(infoPtr->Font != 0) DeleteObject(infoPtr->Font);
1770 if(infoPtr->LinkFont != 0) DeleteObject(infoPtr->LinkFont);
1771 SetWindowLongPtrW(hwnd, 0, 0);
1772 Free (infoPtr);
1773 return 0;
1774
1775 case WM_SYSCOLORCHANGE:
1776 COMCTL32_RefreshSysColors();
1777 return 0;
1778
1779 default:
1780 if ((message >= WM_USER) && (message < WM_APP) && !COMCTL32_IsReflectedMessage(message))
1781 {
1782 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message, wParam, lParam );
1783 }
1784 return DefWindowProcW(hwnd, message, wParam, lParam);
1785 }
1786 }
1787
1788
1789 /***********************************************************************
1790 * SYSLINK_Register [Internal]
1791 *
1792 * Registers the SysLink window class.
1793 */
1794 VOID SYSLINK_Register (void)
1795 {
1796 WNDCLASSW wndClass;
1797
1798 ZeroMemory (&wndClass, sizeof(wndClass));
1799 wndClass.style = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1800 wndClass.lpfnWndProc = SysLinkWindowProc;
1801 wndClass.cbClsExtra = 0;
1802 wndClass.cbWndExtra = sizeof (SYSLINK_INFO *);
1803 wndClass.hCursor = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1804 wndClass.lpszClassName = WC_LINK;
1805
1806 RegisterClassW (&wndClass);
1807 }
1808
1809
1810 /***********************************************************************
1811 * SYSLINK_Unregister [Internal]
1812 *
1813 * Unregisters the SysLink window class.
1814 */
1815 VOID SYSLINK_Unregister (void)
1816 {
1817 UnregisterClassW (WC_LINK, NULL);
1818 }