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