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