mshtml: Added nsIURI::Clone implementation.
[wine] / dlls / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 <stdarg.h>
31 #include <string.h>
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wingdi.h"
35 #include "winuser.h"
36 #include "winnls.h"
37 #include "commctrl.h"
38 #include "comctl32.h"
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(progress);
43
44 INT WINAPI StrCmpNIW(LPCWSTR,LPCWSTR,INT);
45
46 typedef struct
47 {
48     int nChars;
49     int nSkip;
50     RECT rc;
51 } DOC_TEXTBLOCK, *PDOC_TEXTBLOCK;
52
53 #define LIF_FLAGSMASK   (LIF_STATE | LIF_ITEMID | LIF_URL)
54 #define LIS_MASK        (LIS_FOCUSED | LIS_ENABLED | LIS_VISITED)
55
56 typedef enum
57 {
58     slText = 0,
59     slLink
60 } SL_ITEM_TYPE;
61
62 typedef struct _DOC_ITEM
63 {
64     struct _DOC_ITEM *Next; /* Address to the next item */
65     UINT nText;             /* Number of characters of the text */
66     SL_ITEM_TYPE Type;      /* type of the item */
67     PDOC_TEXTBLOCK Blocks;  /* Array of text blocks */
68     union
69     {
70         struct
71         {
72             UINT state;     /* Link state */
73             WCHAR *szID;    /* Link ID string */
74             WCHAR *szUrl;   /* Link URL string */
75         } Link;
76         struct
77         {
78             UINT Dummy;
79         } Text;
80     } u;
81     WCHAR Text[1];          /* Text of the document item */
82 } DOC_ITEM, *PDOC_ITEM;
83
84 typedef struct
85 {
86     HWND      Self;         /* The window handle for this control */
87     HWND      Notify;       /* The parent handle to receive notifications */
88     DWORD     Style;        /* Styles for this control */
89     PDOC_ITEM Items;        /* Address to the first document item */
90     BOOL      HasFocus;     /* Whether the control has the input focus */
91     int       MouseDownID;  /* ID of the link that the mouse button first selected */
92     HFONT     Font;         /* Handle to the font for text */
93     HFONT     LinkFont;     /* Handle to the font for links */
94     COLORREF  TextColor;    /* Color of the text */
95     COLORREF  LinkColor;    /* Color of links */
96     COLORREF  VisitedColor; /* Color of visited links */
97     WCHAR     BreakChar;    /* Break Character for the current font */
98 } SYSLINK_INFO;
99
100 static const WCHAR SL_LINKOPEN[] =  { '<','a', 0 };
101 static const WCHAR SL_HREF[] =      { 'h','r','e','f','=','\"',0 };
102 static const WCHAR SL_ID[] =        { 'i','d','=','\"',0 };
103 static const WCHAR SL_LINKCLOSE[] = { '<','/','a','>',0 };
104
105 /* Control configuration constants */
106
107 #define SL_LEFTMARGIN   (0)
108 #define SL_TOPMARGIN    (0)
109 #define SL_RIGHTMARGIN  (0)
110 #define SL_BOTTOMMARGIN (0)
111
112 /***********************************************************************
113  * SYSLINK_FreeDocItem
114  * Frees all data and gdi objects associated with a document item
115  */
116 static VOID SYSLINK_FreeDocItem (PDOC_ITEM DocItem)
117 {
118     if(DocItem->Type == slLink)
119     {
120         if (DocItem->u.Link.szID != NULL)
121         {
122             Free(DocItem->u.Link.szID);
123         }
124         if (DocItem->u.Link.szUrl != NULL)
125         {
126             Free(DocItem->u.Link.szUrl);
127         }
128     }
129
130     /* we don't free Text because it's just a pointer to a character in the
131        entire window text string */
132
133     Free(DocItem);
134 }
135
136 /***********************************************************************
137  * SYSLINK_AppendDocItem
138  * Create and append a new document item.
139  */
140 static PDOC_ITEM SYSLINK_AppendDocItem (SYSLINK_INFO *infoPtr, LPCWSTR Text, UINT textlen,
141                                         SL_ITEM_TYPE type, PDOC_ITEM LastItem)
142 {
143     PDOC_ITEM Item;
144
145     textlen = min(textlen, lstrlenW(Text));
146     Item = Alloc(FIELD_OFFSET(DOC_ITEM, Text[textlen + 1]));
147     if(Item == NULL)
148     {
149         ERR("Failed to alloc DOC_ITEM structure!\n");
150         return NULL;
151     }
152
153     Item->Next = NULL;
154     Item->nText = textlen;
155     Item->Type = type;
156     Item->Blocks = NULL;
157     
158     if(LastItem != NULL)
159     {
160         LastItem->Next = Item;
161     }
162     else
163     {
164         infoPtr->Items = Item;
165     }
166     
167     lstrcpynW(Item->Text, Text, textlen + 1);
168     
169     return Item;
170 }
171
172 /***********************************************************************
173  * SYSLINK_ClearDoc
174  * Clears the document tree
175  */
176 static VOID SYSLINK_ClearDoc (SYSLINK_INFO *infoPtr)
177 {
178     PDOC_ITEM Item, Next;
179     
180     Item = infoPtr->Items;
181     while(Item != NULL)
182     {
183         Next = Item->Next;
184         SYSLINK_FreeDocItem(Item);
185         Item = Next;
186     }
187     
188     infoPtr->Items = NULL;
189 }
190
191 /***********************************************************************
192  * SYSLINK_ParseText
193  * Parses the window text string and creates a document. Returns the
194  * number of document items created.
195  */
196 static UINT SYSLINK_ParseText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
197 {
198     LPCWSTR current, textstart = NULL, linktext = NULL, firsttag = NULL;
199     int taglen = 0, textlen = 0, linklen = 0, docitems = 0;
200     PDOC_ITEM Last = NULL;
201     SL_ITEM_TYPE CurrentType = slText;
202     LPCWSTR lpID, lpUrl;
203     UINT lenId, lenUrl;
204
205     for(current = Text; *current != 0;)
206     {
207         if(*current == '<')
208         {
209             if(!StrCmpNIW(current, SL_LINKOPEN, 2) && (CurrentType == slText))
210             {
211                 BOOL ValidParam = FALSE, ValidLink = FALSE;
212
213                 if(*(current + 2) == '>')
214                 {
215                     /* we just have to deal with a <a> tag */
216                     taglen = 3;
217                     ValidLink = TRUE;
218                     ValidParam = TRUE;
219                     firsttag = current;
220                     linklen = 0;
221                     lpID = NULL;
222                     lpUrl = NULL;
223                 }
224                 else if(*(current + 2) == infoPtr->BreakChar)
225                 {
226                     /* we expect parameters, parse them */
227                     LPCWSTR *CurrentParameter = NULL, tmp;
228                     UINT *CurrentParameterLen = NULL;
229
230                     taglen = 3;
231                     tmp = current + taglen;
232                     lpID = NULL;
233                     lpUrl = NULL;
234                     
235 CheckParameter:
236                     /* compare the current position with all known parameters */
237                     if(!StrCmpNIW(tmp, SL_HREF, 6))
238                     {
239                         taglen += 6;
240                         ValidParam = TRUE;
241                         CurrentParameter = &lpUrl;
242                         CurrentParameterLen = &lenUrl;
243                     }
244                     else if(!StrCmpNIW(tmp, SL_ID, 4))
245                     {
246                         taglen += 4;
247                         ValidParam = TRUE;
248                         CurrentParameter = &lpID;
249                         CurrentParameterLen = &lenId;
250                     }
251                     else
252                     {
253                         ValidParam = FALSE;
254                     }
255                     
256                     if(ValidParam)
257                     {
258                         /* we got a known parameter, now search until the next " character.
259                            If we can't find a " character, there's a syntax error and we just assume it's text */
260                         ValidParam = FALSE;
261                         *CurrentParameter = current + taglen;
262                         *CurrentParameterLen = 0;
263
264                         for(tmp = *CurrentParameter; *tmp != 0; tmp++)
265                         {
266                             taglen++;
267                             if(*tmp == '\"')
268                             {
269                                 ValidParam = TRUE;
270                                 tmp++;
271                                 break;
272                             }
273                             (*CurrentParameterLen)++;
274                         }
275                     }
276                     if(ValidParam)
277                     {
278                         /* we're done with this parameter, now there are only 2 possibilities:
279                          * 1. another parameter is coming, so expect a ' ' (space) character
280                          * 2. the tag is being closed, so expect a '<' character
281                          */
282                         if(*tmp == infoPtr->BreakChar)
283                         {
284                             /* we expect another parameter, do the whole thing again */
285                             taglen++;
286                             tmp++;
287                             goto CheckParameter;
288                         }
289                         else if(*tmp == '>')
290                         {
291                             /* the tag is being closed, we're done */
292                             ValidLink = TRUE;
293                             taglen++;
294                         }
295                         else
296                             tmp++;
297                     }
298                 }
299                 
300                 if(ValidLink && ValidParam)
301                 {
302                     /* the <a ...> tag appears to be valid. save all information
303                        so we can add the link if we find a valid </a> tag later */
304                     CurrentType = slLink;
305                     linktext = current + taglen;
306                     linklen = 0;
307                     firsttag = current;
308                 }
309                 else
310                 {
311                     taglen = 1;
312                     lpID = NULL;
313                     lpUrl = NULL;
314                     if(textstart == NULL)
315                     {
316                         textstart = current;
317                     }
318                 }
319             }
320             else if(!StrCmpNIW(current, SL_LINKCLOSE, 4) && (CurrentType == slLink) && firsttag)
321             {
322                 /* there's a <a...> tag opened, first add the previous text, if present */
323                 if(textstart != NULL && textlen > 0 && firsttag > textstart)
324                 {
325                     Last = SYSLINK_AppendDocItem(infoPtr, textstart, firsttag - textstart, slText, Last);
326                     if(Last == NULL)
327                     {
328                         ERR("Unable to create new document item!\n");
329                         return docitems;
330                     }
331                     docitems++;
332                     textstart = NULL;
333                     textlen = 0;
334                 }
335                 
336                 /* now it's time to add the link to the document */
337                 current += 4;
338                 if(linktext != NULL && linklen > 0)
339                 {
340                     Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slLink, Last);
341                     if(Last == NULL)
342                     {
343                         ERR("Unable to create new document item!\n");
344                         return docitems;
345                     }
346                     docitems++;
347                     if(CurrentType == slLink)
348                     {
349                         int nc;
350
351                         if(!(infoPtr->Style & WS_DISABLED))
352                         {
353                             Last->u.Link.state |= LIS_ENABLED;
354                         }
355                         /* Copy the tag parameters */
356                         if(lpID != NULL)
357                         {
358                             nc = min(lenId, strlenW(lpID));
359                             nc = min(nc, MAX_LINKID_TEXT - 1);
360                             Last->u.Link.szID = Alloc((nc + 1) * sizeof(WCHAR));
361                             if(Last->u.Link.szID != NULL)
362                             {
363                                 lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
364                             }
365                         }
366                         else
367                             Last->u.Link.szID = NULL;
368                         if(lpUrl != NULL)
369                         {
370                             nc = min(lenUrl, strlenW(lpUrl));
371                             nc = min(nc, L_MAX_URL_LENGTH - 1);
372                             Last->u.Link.szUrl = Alloc((nc + 1) * sizeof(WCHAR));
373                             if(Last->u.Link.szUrl != NULL)
374                             {
375                                 lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
376                             }
377                         }
378                         else
379                             Last->u.Link.szUrl = NULL;
380                     }
381                     linktext = NULL;
382                 }
383                 CurrentType = slText;
384                 firsttag = NULL;
385                 textstart = NULL;
386                 continue;
387             }
388             else
389             {
390                 /* we don't know what tag it is, so just continue */
391                 taglen = 1;
392                 linklen++;
393                 if(CurrentType == slText && textstart == NULL)
394                 {
395                     textstart = current;
396                 }
397             }
398             
399             textlen += taglen;
400             current += taglen;
401         }
402         else
403         {
404             textlen++;
405             linklen++;
406
407             /* save the pointer of the current text item if we couldn't find a tag */
408             if(textstart == NULL && CurrentType == slText)
409             {
410                 textstart = current;
411             }
412             
413             current++;
414         }
415     }
416     
417     if(textstart != NULL && textlen > 0)
418     {
419         Last = SYSLINK_AppendDocItem(infoPtr, textstart, textlen, CurrentType, Last);
420         if(Last == NULL)
421         {
422             ERR("Unable to create new document item!\n");
423             return docitems;
424         }
425         if(CurrentType == slLink)
426         {
427             int nc;
428
429             if(!(infoPtr->Style & WS_DISABLED))
430             {
431                 Last->u.Link.state |= LIS_ENABLED;
432             }
433             /* Copy the tag parameters */
434             if(lpID != NULL)
435             {
436                 nc = min(lenId, strlenW(lpID));
437                 nc = min(nc, MAX_LINKID_TEXT - 1);
438                 Last->u.Link.szID = Alloc((nc + 1) * sizeof(WCHAR));
439                 if(Last->u.Link.szID != NULL)
440                 {
441                     lstrcpynW(Last->u.Link.szID, lpID, nc + 1);
442                 }
443             }
444             else
445                 Last->u.Link.szID = NULL;
446             if(lpUrl != NULL)
447             {
448                 nc = min(lenUrl, strlenW(lpUrl));
449                 nc = min(nc, L_MAX_URL_LENGTH - 1);
450                 Last->u.Link.szUrl = Alloc((nc + 1) * sizeof(WCHAR));
451                 if(Last->u.Link.szUrl != NULL)
452                 {
453                     lstrcpynW(Last->u.Link.szUrl, lpUrl, nc + 1);
454                 }
455             }
456             else
457                 Last->u.Link.szUrl = NULL;
458         }
459         docitems++;
460     }
461
462     if(linktext != NULL && linklen > 0)
463     {
464         /* we got an unclosed link, just display the text */
465         Last = SYSLINK_AppendDocItem(infoPtr, linktext, linklen, slText, Last);
466         if(Last == NULL)
467         {
468             ERR("Unable to create new document item!\n");
469             return docitems;
470         }
471         docitems++;
472     }
473
474     return docitems;
475 }
476
477 /***********************************************************************
478  * SYSLINK_RepaintLink
479  * Repaints a link.
480  */
481 static VOID SYSLINK_RepaintLink (SYSLINK_INFO *infoPtr, PDOC_ITEM DocItem)
482 {
483     PDOC_TEXTBLOCK bl;
484     int n;
485
486     if(DocItem->Type != slLink)
487     {
488         ERR("DocItem not a link!\n");
489         return;
490     }
491     
492     bl = DocItem->Blocks;
493     if (bl != NULL)
494     {
495         n = DocItem->nText;
496         
497         while(n > 0)
498         {
499             InvalidateRect(infoPtr->Self, &bl->rc, TRUE);
500             n -= bl->nChars + bl->nSkip;
501             bl++;
502         }
503     }
504 }
505
506 /***********************************************************************
507  * SYSLINK_GetLinkItemByIndex
508  * Retrieves a document link by its index
509  */
510 static PDOC_ITEM SYSLINK_GetLinkItemByIndex (SYSLINK_INFO *infoPtr, int iLink)
511 {
512     PDOC_ITEM Current = infoPtr->Items;
513
514     while(Current != NULL)
515     {
516         if((Current->Type == slLink) && (iLink-- <= 0))
517         {
518             return Current;
519         }
520         Current = Current->Next;
521     }
522     return NULL;
523 }
524
525 /***********************************************************************
526  * SYSLINK_GetFocusLink
527  * Retrieves the link that has the LIS_FOCUSED bit
528  */
529 static PDOC_ITEM SYSLINK_GetFocusLink (SYSLINK_INFO *infoPtr, int *LinkId)
530 {
531     PDOC_ITEM Current = infoPtr->Items;
532     int id = 0;
533
534     while(Current != NULL)
535     {
536         if((Current->Type == slLink))
537         {
538             if(Current->u.Link.state & LIS_FOCUSED)
539             {
540                 if(LinkId != NULL)
541                     *LinkId = id;
542                 return Current;
543             }
544             id++;
545         }
546         Current = Current->Next;
547     }
548     return NULL;
549 }
550
551 /***********************************************************************
552  * SYSLINK_GetNextLink
553  * Gets the next link
554  */
555 static PDOC_ITEM SYSLINK_GetNextLink (SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
556 {
557     for(Current = (Current != NULL ? Current->Next : infoPtr->Items);
558         Current != NULL;
559         Current = Current->Next)
560     {
561         if(Current->Type == slLink)
562         {
563             return Current;
564         }
565     }
566     return NULL;
567 }
568
569 /***********************************************************************
570  * SYSLINK_GetPrevLink
571  * Gets the previous link
572  */
573 static PDOC_ITEM SYSLINK_GetPrevLink (SYSLINK_INFO *infoPtr, PDOC_ITEM Current)
574 {
575     if(Current == NULL)
576     {
577         /* returns the last link */
578         PDOC_ITEM Last = NULL;
579         
580         for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
581         {
582             if(Current->Type == slLink)
583             {
584                 Last = Current;
585             }
586         }
587         return Last;
588     }
589     else
590     {
591         /* returns the previous link */
592         PDOC_ITEM Cur, Prev = NULL;
593         
594         for(Cur = infoPtr->Items; Cur != NULL; Cur = Cur->Next)
595         {
596             if(Cur == Current)
597             {
598                 break;
599             }
600             if(Cur->Type == slLink)
601             {
602                 Prev = Cur;
603             }
604         }
605         return Prev;
606     }
607 }
608
609 /***********************************************************************
610  * SYSLINK_WrapLine
611  * Tries to wrap a line.
612  */
613 static BOOL SYSLINK_WrapLine (HDC hdc, LPWSTR Text, WCHAR BreakChar, int *LineLen,
614                              int nFit, LPSIZE Extent, int Width)
615 {
616     WCHAR *Current;
617
618     if(nFit == *LineLen)
619     {
620         return FALSE;
621     }
622
623     *LineLen = nFit;
624
625     Current = Text + nFit;
626     
627     /* check if we're in the middle of a word */
628     if((*Current) != BreakChar)
629     {
630         /* search for the beginning of the word */
631         while(Current > Text && (*(Current - 1)) != BreakChar)
632         {
633             Current--;
634             (*LineLen)--;
635         }
636         
637         if((*LineLen) == 0)
638         {
639             Extent->cx = 0;
640             Extent->cy = 0;
641         }
642         return TRUE;
643     }
644
645     return TRUE;
646 }
647
648 /***********************************************************************
649  * SYSLINK_Render
650  * Renders the document in memory
651  */
652 static VOID SYSLINK_Render (SYSLINK_INFO *infoPtr, HDC hdc)
653 {
654     RECT rc;
655     PDOC_ITEM Current;
656     HGDIOBJ hOldFont;
657     int x, y, LineHeight;
658     
659     GetClientRect(infoPtr->Self, &rc);
660     rc.right -= SL_RIGHTMARGIN;
661     rc.bottom -= SL_BOTTOMMARGIN;
662     
663     if(rc.right - SL_LEFTMARGIN < 0 || rc.bottom - SL_TOPMARGIN < 0) return;
664     
665     hOldFont = SelectObject(hdc, infoPtr->Font);
666     
667     x = SL_LEFTMARGIN;
668     y = SL_TOPMARGIN;
669     LineHeight = 0;
670     
671     for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
672     {
673         int n, nBlocks;
674         LPWSTR tx;
675         PDOC_TEXTBLOCK bl, cbl;
676         INT nFit;
677         SIZE szDim;
678         
679         if(Current->nText == 0)
680         {
681             continue;
682         }
683         
684         tx = Current->Text;
685         n = Current->nText;
686
687         if (Current->Blocks != NULL)
688         {
689             Free(Current->Blocks);
690             Current->Blocks = NULL;
691         }
692         bl = NULL;
693         nBlocks = 0;
694         
695         if(Current->Type == slText)
696         {
697             SelectObject(hdc, infoPtr->Font);
698         }
699         else if(Current->Type == slLink)
700         {
701             SelectObject(hdc, infoPtr->LinkFont);
702         }
703         
704         while(n > 0)
705         {
706             int SkipChars = 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                 while(n > 0 && (*tx) == infoPtr->BreakChar)
712                 {
713                     tx++;
714                     SkipChars++;
715                     n--;
716                 }
717             }
718
719             if((n == 0 && SkipChars != 0) ||
720                GetTextExtentExPointW(hdc, tx, n, rc.right - x, &nFit, NULL, &szDim))
721             {
722                 int LineLen = n;
723                 BOOL Wrap = FALSE;
724                 
725                 if(n != 0)
726                 {
727                     Wrap = SYSLINK_WrapLine(hdc, tx, infoPtr->BreakChar, &LineLen, nFit, &szDim, rc.right - x);
728
729                     if(LineLen == 0)
730                     {
731                         if(x > SL_LEFTMARGIN)
732                         {
733                             /* move one line down, the word didn't fit into the line */
734                             x = SL_LEFTMARGIN;
735                             y += LineHeight;
736                             LineHeight = 0;
737                             continue;
738                         }
739                         else
740                         {
741                             /* the word starts at the beginning of the line and doesn't
742                                fit into the line, so break it at the last character that fits */
743                             LineLen = max(nFit, 1);
744                         }
745                     }
746
747                     if(LineLen != n)
748                     {
749                         if(!GetTextExtentExPointW(hdc, tx, LineLen, rc.right - x, NULL, NULL, &szDim))
750                         {
751                             if(bl != NULL)
752                             {
753                                 Free(bl);
754                                 bl = NULL;
755                                 nBlocks = 0;
756                             }
757                             break;
758                         }
759                     }
760                 }
761                 
762                 if(bl != NULL)
763                 {
764                     PDOC_TEXTBLOCK nbl = ReAlloc(bl, (nBlocks + 1) * sizeof(DOC_TEXTBLOCK));
765                     if (nbl != NULL)
766                     {
767                         bl = nbl;
768                         nBlocks++;
769                     }
770                     else
771                     {
772                         Free(bl);
773                         bl = NULL;
774                         nBlocks = 0;
775                     }
776                 }
777                 else
778                 {
779                     bl = Alloc(sizeof(DOC_TEXTBLOCK));
780                     if (bl != NULL)
781                         nBlocks++;
782                 }
783                 
784                 if(bl != NULL)
785                 {
786                     cbl = bl + nBlocks - 1;
787                     
788                     cbl->nChars = LineLen;
789                     cbl->nSkip = SkipChars;
790                     cbl->rc.left = x;
791                     cbl->rc.top = y;
792                     cbl->rc.right = x + szDim.cx;
793                     cbl->rc.bottom = y + szDim.cy;
794                     
795                     if(LineLen != 0)
796                     {
797                         x += szDim.cx;
798                         LineHeight = max(LineHeight, szDim.cy);
799
800                         if(Wrap)
801                         {
802                             x = SL_LEFTMARGIN;
803                             y += LineHeight;
804                             LineHeight = 0;
805                         }
806                     }
807                 }
808                 else
809                 {
810                     ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
811                     break;
812                 }
813                 n -= LineLen;
814                 tx += LineLen;
815             }
816             else
817             {
818                 n--;
819             }
820         }
821
822         if(nBlocks != 0)
823         {
824             Current->Blocks = bl;
825         }
826     }
827     
828     SelectObject(hdc, hOldFont);
829 }
830
831 /***********************************************************************
832  * SYSLINK_Draw
833  * Draws the SysLink control.
834  */
835 static LRESULT SYSLINK_Draw (SYSLINK_INFO *infoPtr, HDC hdc)
836 {
837     RECT rc;
838     PDOC_ITEM Current;
839     HFONT hOldFont;
840     COLORREF OldTextColor, OldBkColor;
841
842     hOldFont = SelectObject(hdc, infoPtr->Font);
843     OldTextColor = SetTextColor(hdc, infoPtr->TextColor);
844     OldBkColor = SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
845     
846     GetClientRect(infoPtr->Self, &rc);
847     rc.right -= SL_RIGHTMARGIN + SL_LEFTMARGIN;
848     rc.bottom -= SL_BOTTOMMARGIN + SL_TOPMARGIN;
849
850     if(rc.right < 0 || rc.bottom < 0) return 0;
851
852     for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
853     {
854         int n;
855         LPWSTR tx;
856         PDOC_TEXTBLOCK bl;
857         
858         bl = Current->Blocks;
859         if(bl != NULL)
860         {
861             tx = Current->Text;
862             n = Current->nText;
863
864             if(Current->Type == slText)
865             {
866                  SelectObject(hdc, infoPtr->Font);
867                  SetTextColor(hdc, infoPtr->TextColor);
868             }
869             else
870             {
871                  SelectObject(hdc, infoPtr->LinkFont);
872                  SetTextColor(hdc, (!(Current->u.Link.state & LIS_VISITED) ? infoPtr->LinkColor : infoPtr->VisitedColor));
873             }
874
875             while(n > 0)
876             {
877                 tx += bl->nSkip;
878                 ExtTextOutW(hdc, bl->rc.left, bl->rc.top, ETO_OPAQUE | ETO_CLIPPED, &bl->rc, tx, bl->nChars, NULL);
879                 if((Current->Type == slLink) && (Current->u.Link.state & LIS_FOCUSED) && infoPtr->HasFocus)
880                 {
881                     COLORREF PrevTextColor;
882                     PrevTextColor = SetTextColor(hdc, infoPtr->TextColor);
883                     DrawFocusRect(hdc, &bl->rc);
884                     SetTextColor(hdc, PrevTextColor);
885                 }
886                 tx += bl->nChars;
887                 n -= bl->nChars + bl->nSkip;
888                 bl++;
889             }
890         }
891     }
892
893     SetBkColor(hdc, OldBkColor);
894     SetTextColor(hdc, OldTextColor);
895     SelectObject(hdc, hOldFont);
896     
897     return 0;
898 }
899
900
901 /***********************************************************************
902  * SYSLINK_Paint
903  * Handles the WM_PAINT message.
904  */
905 static LRESULT SYSLINK_Paint (SYSLINK_INFO *infoPtr, HDC hdcParam)
906 {
907     HDC hdc;
908     PAINTSTRUCT ps;
909
910     hdc = hdcParam ? hdcParam : BeginPaint (infoPtr->Self, &ps);
911     if (hdc)
912     {
913         SYSLINK_Draw (infoPtr, hdc);
914         if (!hdcParam) EndPaint (infoPtr->Self, &ps);
915     }
916     return 0;
917 }
918
919
920 /***********************************************************************
921  *           SYSLINK_SetFont
922  * Set new Font for the SysLink control.
923  */
924 static HFONT SYSLINK_SetFont (SYSLINK_INFO *infoPtr, HFONT hFont, BOOL bRedraw)
925 {
926     HDC hdc;
927     LOGFONTW lf;
928     TEXTMETRICW tm;
929     HFONT hOldFont = infoPtr->Font;
930     infoPtr->Font = hFont;
931     
932     /* free the underline font */
933     if(infoPtr->LinkFont != NULL)
934     {
935         DeleteObject(infoPtr->LinkFont);
936         infoPtr->LinkFont = NULL;
937     }
938
939     /* Render text position and word wrapping in memory */
940     hdc = GetDC(infoPtr->Self);
941     if(hdc != NULL)
942     {
943         /* create a new underline font */
944         if(GetTextMetricsW(hdc, &tm) &&
945            GetObjectW(infoPtr->Font, sizeof(LOGFONTW), &lf))
946         {
947             lf.lfUnderline = TRUE;
948             infoPtr->LinkFont = CreateFontIndirectW(&lf);
949             infoPtr->BreakChar = tm.tmBreakChar;
950         }
951         else
952         {
953             ERR("Failed to create link font!\n");
954         }
955
956         SYSLINK_Render(infoPtr, hdc);
957         ReleaseDC(infoPtr->Self, hdc);
958     }
959     
960     if(bRedraw)
961     {
962         RedrawWindow(infoPtr->Self, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
963     }
964     
965     return hOldFont;
966 }
967
968 /***********************************************************************
969  *           SYSLINK_SetText
970  * Set new text for the SysLink control.
971  */
972 static LRESULT SYSLINK_SetText (SYSLINK_INFO *infoPtr, LPCWSTR Text)
973 {
974     int textlen;
975
976     /* clear the document */
977     SYSLINK_ClearDoc(infoPtr);
978     
979     if(Text == NULL || (textlen = lstrlenW(Text)) == 0)
980     {
981         return TRUE;
982     }
983     
984     /* let's parse the string and create a document */
985     if(SYSLINK_ParseText(infoPtr, Text) > 0)
986     {
987         /* Render text position and word wrapping in memory */
988         HDC hdc = GetDC(infoPtr->Self);
989         if (hdc != NULL)
990         {
991             SYSLINK_Render(infoPtr, hdc);
992             ReleaseDC(infoPtr->Self, hdc);
993
994             InvalidateRect(infoPtr->Self, NULL, TRUE);
995         }
996     }
997     
998     return TRUE;
999 }
1000
1001 /***********************************************************************
1002  *           SYSLINK_SetFocusLink
1003  * Updates the focus status bits and focusses the specified link.
1004  * If no document item is specified, the focus bit will be removed from all links.
1005  * Returns the previous focused item.
1006  */
1007 static PDOC_ITEM SYSLINK_SetFocusLink (SYSLINK_INFO *infoPtr, PDOC_ITEM DocItem)
1008 {
1009     PDOC_ITEM Current, PrevFocus = NULL;
1010     
1011     for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1012     {
1013         if(Current->Type == slLink)
1014         {
1015             if((PrevFocus == NULL) && (Current->u.Link.state & LIS_FOCUSED))
1016             {
1017                 PrevFocus = Current;
1018             }
1019             
1020             if(Current == DocItem)
1021             {
1022                 Current->u.Link.state |= LIS_FOCUSED;
1023             }
1024             else
1025             {
1026                 Current->u.Link.state &= ~LIS_FOCUSED;
1027             }
1028         }
1029     }
1030     
1031     return PrevFocus;
1032 }
1033
1034 /***********************************************************************
1035  *           SYSLINK_SetItem
1036  * Sets the states and attributes of a link item.
1037  */
1038 static LRESULT SYSLINK_SetItem (SYSLINK_INFO *infoPtr, PLITEM Item)
1039 {
1040     PDOC_ITEM di;
1041     int nc;
1042     PWSTR szId = NULL;
1043     PWSTR szUrl = NULL;
1044     BOOL Repaint = FALSE;
1045
1046     if(!(Item->mask & LIF_ITEMINDEX) || !(Item->mask & (LIF_FLAGSMASK)))
1047     {
1048         ERR("Invalid Flags!\n");
1049         return FALSE;
1050     }
1051
1052     di = SYSLINK_GetLinkItemByIndex(infoPtr, Item->iLink);
1053     if(di == NULL)
1054     {
1055         ERR("Link %d couldn't be found\n", Item->iLink);
1056         return FALSE;
1057     }
1058
1059     if(Item->mask & LIF_ITEMID)
1060     {
1061         nc = min(lstrlenW(Item->szID), MAX_LINKID_TEXT - 1);
1062         szId = Alloc((nc + 1) * sizeof(WCHAR));
1063         if(szId)
1064         {
1065             lstrcpynW(szId, Item->szID, nc + 1);
1066         }
1067         else
1068         {
1069             ERR("Unable to allocate memory for link id\n");
1070             return FALSE;
1071         }
1072     }
1073
1074     if(Item->mask & LIF_URL)
1075     {
1076         nc = min(lstrlenW(Item->szUrl), L_MAX_URL_LENGTH - 1);
1077         szUrl = Alloc((nc + 1) * sizeof(WCHAR));
1078         if(szUrl)
1079         {
1080             lstrcpynW(szUrl, Item->szUrl, nc + 1);
1081         }
1082         else
1083         {
1084             if (szId)
1085             {
1086                 Free(szId);
1087             }
1088
1089             ERR("Unable to allocate memory for link url\n");
1090             return FALSE;
1091         }
1092     }
1093
1094     if(Item->mask & LIF_ITEMID)
1095     {
1096         if(di->u.Link.szID)
1097         {
1098             Free(di->u.Link.szID);
1099         }
1100         di->u.Link.szID = szId;
1101     }
1102
1103     if(Item->mask & LIF_URL)
1104     {
1105         if(di->u.Link.szUrl)
1106         {
1107             Free(di->u.Link.szUrl);
1108         }
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 (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 (PDOC_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 (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 (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 (SYSLINK_INFO *infoPtr, UINT code, PDOC_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, (WPARAM)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, HWND PrevFocusWindow)
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     }
1345     
1346     SYSLINK_RepaintLink(infoPtr, Focus);
1347     
1348     return 0;
1349 }
1350
1351 /***********************************************************************
1352  *           SYSLINK_KillFocus
1353  * Handles losing the input focus.
1354  */
1355 static LRESULT SYSLINK_KillFocus (SYSLINK_INFO *infoPtr, HWND NewFocusWindow)
1356 {
1357     PDOC_ITEM Focus;
1358     
1359     infoPtr->HasFocus = FALSE;
1360     Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1361     
1362     if(Focus != NULL)
1363     {
1364         SYSLINK_RepaintLink(infoPtr, Focus);
1365     }
1366
1367     return 0;
1368 }
1369
1370 /***********************************************************************
1371  *           SYSLINK_LinkAtPt
1372  * Returns a link at the specified position
1373  */
1374 static PDOC_ITEM SYSLINK_LinkAtPt (SYSLINK_INFO *infoPtr, POINT *pt, int *LinkId, BOOL MustBeEnabled)
1375 {
1376     PDOC_ITEM Current;
1377     int id = 0;
1378
1379     for(Current = infoPtr->Items; Current != NULL; Current = Current->Next)
1380     {
1381         if((Current->Type == slLink) && SYSLINK_PtInDocItem(Current, *pt) &&
1382            (!MustBeEnabled || (MustBeEnabled && (Current->u.Link.state & LIS_ENABLED))))
1383         {
1384             if(LinkId != NULL)
1385             {
1386                 *LinkId = id;
1387             }
1388             return Current;
1389         }
1390         id++;
1391     }
1392
1393     return NULL;
1394 }
1395
1396 /***********************************************************************
1397  *           SYSLINK_LButtonDown
1398  * Handles mouse clicks
1399  */
1400 static LRESULT SYSLINK_LButtonDown (SYSLINK_INFO *infoPtr, DWORD Buttons, POINT *pt)
1401 {
1402     PDOC_ITEM Current, Old;
1403     int id;
1404
1405     Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1406     if(Current != NULL)
1407     {
1408       SetFocus(infoPtr->Self);
1409
1410       Old = SYSLINK_SetFocusLink(infoPtr, Current);
1411       if(Old != NULL && Old != Current)
1412       {
1413           SYSLINK_RepaintLink(infoPtr, Old);
1414       }
1415       infoPtr->MouseDownID = id;
1416       SYSLINK_RepaintLink(infoPtr, Current);
1417     }
1418
1419     return 0;
1420 }
1421
1422 /***********************************************************************
1423  *           SYSLINK_LButtonUp
1424  * Handles mouse clicks
1425  */
1426 static LRESULT SYSLINK_LButtonUp (SYSLINK_INFO *infoPtr, DWORD Buttons, POINT *pt)
1427 {
1428     if(infoPtr->MouseDownID > -1)
1429     {
1430         PDOC_ITEM Current;
1431         int id;
1432         
1433         Current = SYSLINK_LinkAtPt(infoPtr, pt, &id, TRUE);
1434         if((Current != NULL) && (Current->u.Link.state & LIS_FOCUSED) && (infoPtr->MouseDownID == id))
1435         {
1436             SYSLINK_SendParentNotify(infoPtr, NM_CLICK, Current, id);
1437         }
1438     }
1439
1440     infoPtr->MouseDownID = -1;
1441
1442     return 0;
1443 }
1444
1445 /***********************************************************************
1446  *           SYSLINK_OnEnter
1447  * Handles ENTER key events
1448  */
1449 static BOOL SYSLINK_OnEnter (SYSLINK_INFO *infoPtr)
1450 {
1451     if(infoPtr->HasFocus)
1452     {
1453         PDOC_ITEM Focus;
1454         int id;
1455         
1456         Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1457         if(Focus != NULL)
1458         {
1459             SYSLINK_SendParentNotify(infoPtr, NM_RETURN, Focus, id);
1460             return TRUE;
1461         }
1462     }
1463     return FALSE;
1464 }
1465
1466 /***********************************************************************
1467  *           SYSKEY_SelectNextPrevLink
1468  * Changes the currently focused link
1469  */
1470 static BOOL SYSKEY_SelectNextPrevLink (SYSLINK_INFO *infoPtr, BOOL Prev)
1471 {
1472     if(infoPtr->HasFocus)
1473     {
1474         PDOC_ITEM Focus;
1475         int id;
1476
1477         Focus = SYSLINK_GetFocusLink(infoPtr, &id);
1478         if(Focus != NULL)
1479         {
1480             PDOC_ITEM NewFocus, OldFocus;
1481
1482             if(Prev)
1483                 NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1484             else
1485                 NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1486
1487             if(NewFocus != NULL)
1488             {
1489                 OldFocus = SYSLINK_SetFocusLink(infoPtr, NewFocus);
1490
1491                 if(OldFocus != NewFocus)
1492                 {
1493                     SYSLINK_RepaintLink(infoPtr, OldFocus);
1494                 }
1495                 SYSLINK_RepaintLink(infoPtr, NewFocus);
1496                 return TRUE;
1497             }
1498         }
1499     }
1500     return FALSE;
1501 }
1502
1503 /***********************************************************************
1504  *           SYSKEY_SelectNextPrevLink
1505  * Determines if there's a next or previous link to decide whether the control
1506  * should capture the tab key message
1507  */
1508 static BOOL SYSLINK_NoNextLink (SYSLINK_INFO *infoPtr, BOOL Prev)
1509 {
1510     PDOC_ITEM Focus, NewFocus;
1511
1512     Focus = SYSLINK_GetFocusLink(infoPtr, NULL);
1513     if(Prev)
1514         NewFocus = SYSLINK_GetPrevLink(infoPtr, Focus);
1515     else
1516         NewFocus = SYSLINK_GetNextLink(infoPtr, Focus);
1517
1518     return NewFocus == NULL;
1519 }
1520
1521 /***********************************************************************
1522  *           SysLinkWindowProc
1523  */
1524 static LRESULT WINAPI SysLinkWindowProc(HWND hwnd, UINT message,
1525                                         WPARAM wParam, LPARAM lParam)
1526 {
1527     SYSLINK_INFO *infoPtr;
1528
1529     TRACE("hwnd=%p msg=%04x wparam=%x lParam=%lx\n", hwnd, message, wParam, lParam);
1530
1531     infoPtr = (SYSLINK_INFO *)GetWindowLongPtrW(hwnd, 0);
1532
1533     if (!infoPtr && message != WM_CREATE)
1534         goto HandleDefaultMessage;
1535
1536     switch(message) {
1537     case WM_PRINTCLIENT:
1538     case WM_PAINT:
1539         return SYSLINK_Paint (infoPtr, (HDC)wParam);
1540
1541     case WM_SETCURSOR:
1542     {
1543         LHITTESTINFO ht;
1544         DWORD mp = GetMessagePos();
1545         
1546         ht.pt.x = (short)LOWORD(mp);
1547         ht.pt.y = (short)HIWORD(mp);
1548         
1549         ScreenToClient(infoPtr->Self, &ht.pt);
1550         if(SYSLINK_HitTest (infoPtr, &ht))
1551         {
1552             SetCursor(LoadCursorW(0, (LPCWSTR)IDC_HAND));
1553             return TRUE;
1554         }
1555         /* let the default window proc handle this message */
1556         goto HandleDefaultMessage;
1557     }
1558
1559     case WM_SIZE:
1560     {
1561         HDC hdc = GetDC(infoPtr->Self);
1562         if(hdc != NULL)
1563         {
1564           SYSLINK_Render(infoPtr, hdc);
1565           ReleaseDC(infoPtr->Self, hdc);
1566         }
1567         return 0;
1568     }
1569
1570     case WM_GETFONT:
1571         return (LRESULT)infoPtr->Font;
1572
1573     case WM_SETFONT:
1574         return (LRESULT)SYSLINK_SetFont(infoPtr, (HFONT)wParam, (BOOL)lParam);
1575
1576     case WM_SETTEXT:
1577         SYSLINK_SetText(infoPtr, (LPWSTR)lParam);
1578         goto HandleDefaultMessage;
1579
1580     case WM_LBUTTONDOWN:
1581     {
1582         POINT pt;
1583         pt.x = LOWORD(lParam);
1584         pt.y = HIWORD(lParam);
1585         return SYSLINK_LButtonDown(infoPtr, wParam, &pt);
1586     }
1587     case WM_LBUTTONUP:
1588     {
1589         POINT pt;
1590         pt.x = LOWORD(lParam);
1591         pt.y = HIWORD(lParam);
1592         return SYSLINK_LButtonUp(infoPtr, wParam, &pt);
1593     }
1594     
1595     case WM_KEYDOWN:
1596     {
1597         switch(wParam)
1598         {
1599         case VK_RETURN:
1600             SYSLINK_OnEnter(infoPtr);
1601             return 0;
1602         case VK_TAB:
1603         {
1604             BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1605             SYSKEY_SelectNextPrevLink(infoPtr, shift);
1606             return 0;
1607         }
1608         }
1609         goto HandleDefaultMessage;
1610     }
1611     
1612     case WM_GETDLGCODE:
1613     {
1614         LRESULT Ret = DLGC_HASSETSEL;
1615         int vk = (lParam != 0 ? (int)((LPMSG)lParam)->wParam : 0);
1616         switch(vk)
1617         {
1618         case VK_RETURN:
1619             Ret |= DLGC_WANTMESSAGE;
1620             break;
1621         case VK_TAB:
1622         {
1623             BOOL shift = GetKeyState(VK_SHIFT) & 0x8000;
1624             if(!SYSLINK_NoNextLink(infoPtr, shift))
1625             {
1626                 Ret |= DLGC_WANTTAB;
1627             }
1628             else
1629             {
1630                 Ret |= DLGC_WANTCHARS;
1631             }
1632             break;
1633         }
1634         }
1635         return Ret;
1636     }
1637     
1638     case WM_NCHITTEST:
1639     {
1640         POINT pt;
1641         RECT rc;
1642         pt.x = LOWORD(lParam);
1643         pt.y = HIWORD(lParam);
1644         
1645         GetClientRect(infoPtr->Self, &rc);
1646         ScreenToClient(infoPtr->Self, &pt);
1647         if(pt.x < 0 || pt.y < 0 || pt.x > rc.right || pt.y > rc.bottom)
1648         {
1649             return HTNOWHERE;
1650         }
1651
1652         if(SYSLINK_LinkAtPt(infoPtr, &pt, NULL, FALSE))
1653         {
1654             return HTCLIENT;
1655         }
1656         
1657         return HTTRANSPARENT;
1658     }
1659
1660     case LM_HITTEST:
1661         return SYSLINK_HitTest(infoPtr, (PLHITTESTINFO)lParam);
1662
1663     case LM_SETITEM:
1664         return SYSLINK_SetItem(infoPtr, (PLITEM)lParam);
1665
1666     case LM_GETITEM:
1667         return SYSLINK_GetItem(infoPtr, (PLITEM)lParam);
1668
1669     case LM_GETIDEALHEIGHT:
1670         return SYSLINK_GetIdealHeight(infoPtr);
1671
1672     case WM_SETFOCUS:
1673         return SYSLINK_SetFocus(infoPtr, (HWND)wParam);
1674
1675     case WM_KILLFOCUS:
1676         return SYSLINK_KillFocus(infoPtr, (HWND)wParam);
1677
1678     case WM_ENABLE:
1679         infoPtr->Style &= ~WS_DISABLED;
1680         infoPtr->Style |= (wParam ? 0 : WS_DISABLED);
1681         InvalidateRect (infoPtr->Self, NULL, FALSE);
1682         return 0;
1683
1684     case WM_STYLECHANGED:
1685         if (wParam == GWL_STYLE)
1686         {
1687             infoPtr->Style = ((LPSTYLESTRUCT)lParam)->styleNew;
1688
1689             InvalidateRect(infoPtr->Self, NULL, TRUE);
1690         }
1691         return 0;
1692
1693     case WM_CREATE:
1694         /* allocate memory for info struct */
1695         infoPtr = Alloc (sizeof(SYSLINK_INFO));
1696         if (!infoPtr) return -1;
1697         SetWindowLongPtrW (hwnd, 0, (DWORD_PTR)infoPtr);
1698
1699         /* initialize the info struct */
1700         infoPtr->Self = hwnd;
1701         infoPtr->Notify = ((LPCREATESTRUCTW)lParam)->hwndParent;
1702         infoPtr->Style = ((LPCREATESTRUCTW)lParam)->style;
1703         infoPtr->Font = 0;
1704         infoPtr->LinkFont = 0;
1705         infoPtr->Items = NULL;
1706         infoPtr->HasFocus = FALSE;
1707         infoPtr->MouseDownID = -1;
1708         infoPtr->TextColor = GetSysColor(COLOR_WINDOWTEXT);
1709         infoPtr->LinkColor = GetSysColor(COLOR_HIGHLIGHT);
1710         infoPtr->VisitedColor = GetSysColor(COLOR_HIGHLIGHT);
1711         infoPtr->BreakChar = ' ';
1712         TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd);
1713         SYSLINK_SetText(infoPtr, ((LPCREATESTRUCTW)lParam)->lpszName);
1714         return 0;
1715
1716     case WM_DESTROY:
1717         TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd);
1718         SYSLINK_ClearDoc(infoPtr);
1719         if(infoPtr->Font != 0) DeleteObject(infoPtr->Font);
1720         if(infoPtr->LinkFont != 0) DeleteObject(infoPtr->LinkFont);
1721         SetWindowLongPtrW(hwnd, 0, 0);
1722         Free (infoPtr);
1723         return 0;
1724
1725     default:
1726 HandleDefaultMessage:
1727         if ((message >= WM_USER) && (message < WM_APP))
1728         {
1729                 ERR("unknown msg %04x wp=%04x lp=%08lx\n", message, wParam, lParam );
1730         }
1731         return DefWindowProcW(hwnd, message, wParam, lParam);
1732     }
1733 }
1734
1735
1736 /***********************************************************************
1737  * SYSLINK_Register [Internal]
1738  *
1739  * Registers the SysLink window class.
1740  */
1741 VOID SYSLINK_Register (void)
1742 {
1743     WNDCLASSW wndClass;
1744
1745     ZeroMemory (&wndClass, sizeof(wndClass));
1746     wndClass.style         = CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
1747     wndClass.lpfnWndProc   = SysLinkWindowProc;
1748     wndClass.cbClsExtra    = 0;
1749     wndClass.cbWndExtra    = sizeof (SYSLINK_INFO *);
1750     wndClass.hCursor       = LoadCursorW (0, (LPWSTR)IDC_ARROW);
1751     wndClass.lpszClassName = WC_LINK;
1752
1753     RegisterClassW (&wndClass);
1754 }
1755
1756
1757 /***********************************************************************
1758  * SYSLINK_Unregister [Internal]
1759  *
1760  * Unregisters the SysLink window class.
1761  */
1762 VOID SYSLINK_Unregister (void)
1763 {
1764     UnregisterClassW (WC_LINK, NULL);
1765 }