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