4 * Copyright 2004 - 2006 Thomas Weidenmueller <w3seek@reactos.com>
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.
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.
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
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.
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.
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(progress
);
44 INT WINAPI
StrCmpNIW(LPCWSTR
,LPCWSTR
,INT
);
51 } DOC_TEXTBLOCK
, *PDOC_TEXTBLOCK
;
53 #define LIF_FLAGSMASK (LIF_STATE | LIF_ITEMID | LIF_URL)
54 #define LIS_MASK (LIS_FOCUSED | LIS_ENABLED | LIS_VISITED)
62 typedef struct _DOC_ITEM
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 */
72 UINT state
; /* Link state */
73 WCHAR
*szID
; /* Link ID string */
74 WCHAR
*szUrl
; /* Link URL string */
81 WCHAR Text
[1]; /* Text of the document item */
82 } DOC_ITEM
, *PDOC_ITEM
;
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 */
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 };
107 /* Control configuration constants */
109 #define SL_LEFTMARGIN (0)
110 #define SL_TOPMARGIN (0)
111 #define SL_RIGHTMARGIN (0)
112 #define SL_BOTTOMMARGIN (0)
114 /***********************************************************************
115 * SYSLINK_FreeDocItem
116 * Frees all data and gdi objects associated with a document item
118 static VOID
SYSLINK_FreeDocItem (PDOC_ITEM DocItem
)
120 if(DocItem
->Type
== slLink
)
122 Free(DocItem
->u
.Link
.szID
);
123 Free(DocItem
->u
.Link
.szUrl
);
126 /* we don't free Text because it's just a pointer to a character in the
127 entire window text string */
132 /***********************************************************************
133 * SYSLINK_AppendDocItem
134 * Create and append a new document item.
136 static PDOC_ITEM
SYSLINK_AppendDocItem (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
, UINT textlen
,
137 SL_ITEM_TYPE type
, PDOC_ITEM LastItem
)
141 textlen
= min(textlen
, strlenW(Text
));
142 Item
= Alloc(FIELD_OFFSET(DOC_ITEM
, Text
[textlen
+ 1]));
145 ERR("Failed to alloc DOC_ITEM structure!\n");
150 Item
->nText
= textlen
;
156 LastItem
->Next
= Item
;
160 infoPtr
->Items
= Item
;
163 lstrcpynW(Item
->Text
, Text
, textlen
+ 1);
168 /***********************************************************************
170 * Clears the document tree
172 static VOID
SYSLINK_ClearDoc (SYSLINK_INFO
*infoPtr
)
174 PDOC_ITEM Item
, Next
;
176 Item
= infoPtr
->Items
;
180 SYSLINK_FreeDocItem(Item
);
184 infoPtr
->Items
= NULL
;
187 /***********************************************************************
189 * Parses the window text string and creates a document. Returns the
190 * number of document items created.
192 static UINT
SYSLINK_ParseText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
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
;
201 for(current
= Text
; *current
!= 0;)
205 if(!StrCmpNIW(current
, SL_LINKOPEN
, 2) && (CurrentType
== slText
))
207 BOOL ValidParam
= FALSE
, ValidLink
= FALSE
;
209 if(*(current
+ 2) == '>')
211 /* we just have to deal with a <a> tag */
220 else if(*(current
+ 2) == infoPtr
->BreakChar
)
222 /* we expect parameters, parse them */
223 LPCWSTR
*CurrentParameter
= NULL
, tmp
;
224 UINT
*CurrentParameterLen
= NULL
;
227 tmp
= current
+ taglen
;
232 /* compare the current position with all known parameters */
233 if(!StrCmpNIW(tmp
, SL_HREF
, 6))
237 CurrentParameter
= &lpUrl
;
238 CurrentParameterLen
= &lenUrl
;
240 else if(!StrCmpNIW(tmp
, SL_ID
, 4))
244 CurrentParameter
= &lpID
;
245 CurrentParameterLen
= &lenId
;
254 /* we got a known parameter, now search until the next " character.
255 If we can't find a " character, there's a syntax error and we just assume it's text */
257 *CurrentParameter
= current
+ taglen
;
258 *CurrentParameterLen
= 0;
260 for(tmp
= *CurrentParameter
; *tmp
!= 0; tmp
++)
269 (*CurrentParameterLen
)++;
274 /* we're done with this parameter, now there are only 2 possibilities:
275 * 1. another parameter is coming, so expect a ' ' (space) character
276 * 2. the tag is being closed, so expect a '<' character
278 if(*tmp
== infoPtr
->BreakChar
)
280 /* we expect another parameter, do the whole thing again */
287 /* the tag is being closed, we're done */
294 if(ValidLink
&& ValidParam
)
296 /* the <a ...> tag appears to be valid. save all information
297 so we can add the link if we find a valid </a> tag later */
298 CurrentType
= slLink
;
299 linktext
= current
+ taglen
;
308 if(textstart
== NULL
)
314 else if(!StrCmpNIW(current
, SL_LINKCLOSE
, 4) && (CurrentType
== slLink
) && firsttag
)
316 /* there's a <a...> tag opened, first add the previous text, if present */
317 if(textstart
!= NULL
&& textlen
> 0 && firsttag
> textstart
)
319 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, firsttag
- textstart
, slText
, Last
);
322 ERR("Unable to create new document item!\n");
330 /* now it's time to add the link to the document */
332 if(linktext
!= NULL
&& linklen
> 0)
334 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slLink
, Last
);
337 ERR("Unable to create new document item!\n");
341 if(CurrentType
== slLink
)
345 if(!(infoPtr
->Style
& WS_DISABLED
))
347 Last
->u
.Link
.state
|= LIS_ENABLED
;
349 /* Copy the tag parameters */
352 nc
= min(lenId
, strlenW(lpID
));
353 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
354 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
355 if(Last
->u
.Link
.szID
!= NULL
)
357 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
361 Last
->u
.Link
.szID
= NULL
;
364 nc
= min(lenUrl
, strlenW(lpUrl
));
365 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
366 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
367 if(Last
->u
.Link
.szUrl
!= NULL
)
369 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
373 Last
->u
.Link
.szUrl
= NULL
;
377 CurrentType
= slText
;
384 /* we don't know what tag it is, so just continue */
387 if(CurrentType
== slText
&& textstart
== NULL
)
401 /* save the pointer of the current text item if we couldn't find a tag */
402 if(textstart
== NULL
&& CurrentType
== slText
)
411 if(textstart
!= NULL
&& textlen
> 0)
413 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, textlen
, CurrentType
, Last
);
416 ERR("Unable to create new document item!\n");
419 if(CurrentType
== slLink
)
423 if(!(infoPtr
->Style
& WS_DISABLED
))
425 Last
->u
.Link
.state
|= LIS_ENABLED
;
427 /* Copy the tag parameters */
430 nc
= min(lenId
, strlenW(lpID
));
431 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
432 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
433 if(Last
->u
.Link
.szID
!= NULL
)
435 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
439 Last
->u
.Link
.szID
= NULL
;
442 nc
= min(lenUrl
, strlenW(lpUrl
));
443 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
444 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
445 if(Last
->u
.Link
.szUrl
!= NULL
)
447 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
451 Last
->u
.Link
.szUrl
= NULL
;
456 if(linktext
!= NULL
&& linklen
> 0)
458 /* we got an unclosed link, just display the text */
459 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slText
, Last
);
462 ERR("Unable to create new document item!\n");
471 /***********************************************************************
472 * SYSLINK_RepaintLink
475 static VOID
SYSLINK_RepaintLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
480 if(DocItem
->Type
!= slLink
)
482 ERR("DocItem not a link!\n");
486 bl
= DocItem
->Blocks
;
493 InvalidateRect(infoPtr
->Self
, &bl
->rc
, TRUE
);
494 n
-= bl
->nChars
+ bl
->nSkip
;
500 /***********************************************************************
501 * SYSLINK_GetLinkItemByIndex
502 * Retrieves a document link by its index
504 static PDOC_ITEM
SYSLINK_GetLinkItemByIndex (const SYSLINK_INFO
*infoPtr
, int iLink
)
506 PDOC_ITEM Current
= infoPtr
->Items
;
508 while(Current
!= NULL
)
510 if((Current
->Type
== slLink
) && (iLink
-- <= 0))
514 Current
= Current
->Next
;
519 /***********************************************************************
520 * SYSLINK_GetFocusLink
521 * Retrieves the link that has the LIS_FOCUSED bit
523 static PDOC_ITEM
SYSLINK_GetFocusLink (const SYSLINK_INFO
*infoPtr
, int *LinkId
)
525 PDOC_ITEM Current
= infoPtr
->Items
;
528 while(Current
!= NULL
)
530 if((Current
->Type
== slLink
))
532 if(Current
->u
.Link
.state
& LIS_FOCUSED
)
540 Current
= Current
->Next
;
545 /***********************************************************************
546 * SYSLINK_GetNextLink
549 static PDOC_ITEM
SYSLINK_GetNextLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
551 for(Current
= (Current
!= NULL
? Current
->Next
: infoPtr
->Items
);
553 Current
= Current
->Next
)
555 if(Current
->Type
== slLink
)
563 /***********************************************************************
564 * SYSLINK_GetPrevLink
565 * Gets the previous link
567 static PDOC_ITEM
SYSLINK_GetPrevLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
571 /* returns the last link */
572 PDOC_ITEM Last
= NULL
;
574 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
576 if(Current
->Type
== slLink
)
585 /* returns the previous link */
586 PDOC_ITEM Cur
, Prev
= NULL
;
588 for(Cur
= infoPtr
->Items
; Cur
!= NULL
; Cur
= Cur
->Next
)
594 if(Cur
->Type
== slLink
)
603 /***********************************************************************
605 * Tries to wrap a line.
607 static BOOL
SYSLINK_WrapLine (LPWSTR Text
, WCHAR BreakChar
, int *LineLen
,
608 int nFit
, LPSIZE Extent
)
619 Current
= Text
+ nFit
;
621 /* check if we're in the middle of a word */
622 if((*Current
) != BreakChar
)
624 /* search for the beginning of the word */
625 while(Current
> Text
&& (*(Current
- 1)) != BreakChar
)
642 /***********************************************************************
644 * Renders the document in memory
646 static VOID
SYSLINK_Render (const SYSLINK_INFO
*infoPtr
, HDC hdc
, PRECT pRect
)
651 int x
, y
, LineHeight
;
654 szDoc
.cx
= szDoc
.cy
= 0;
657 rc
.right
-= SL_RIGHTMARGIN
;
658 rc
.bottom
-= SL_BOTTOMMARGIN
;
660 if(rc
.right
- SL_LEFTMARGIN
< 0)
662 if (rc
.bottom
- SL_TOPMARGIN
< 0)
665 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
671 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
675 PDOC_TEXTBLOCK bl
, cbl
;
679 if(Current
->nText
== 0)
687 Free(Current
->Blocks
);
688 Current
->Blocks
= NULL
;
692 if(Current
->Type
== slText
)
694 SelectObject(hdc
, infoPtr
->Font
);
696 else if(Current
->Type
== slLink
)
698 SelectObject(hdc
, infoPtr
->LinkFont
);
705 /* skip break characters unless they're the first of the doc item */
706 if(tx
!= Current
->Text
|| x
== SL_LEFTMARGIN
)
708 while(n
> 0 && (*tx
) == infoPtr
->BreakChar
)
716 if((n
== 0 && SkipChars
!= 0) ||
717 GetTextExtentExPointW(hdc
, tx
, n
, rc
.right
- x
, &nFit
, NULL
, &szDim
))
725 Wrap
= SYSLINK_WrapLine(tx
, infoPtr
->BreakChar
, &LineLen
, nFit
, &szDim
);
729 if(x
> SL_LEFTMARGIN
)
731 /* move one line down, the word didn't fit into the line */
739 /* the word starts at the beginning of the line and doesn't
740 fit into the line, so break it at the last character that fits */
741 LineLen
= max(nFit
, 1);
747 if(!GetTextExtentExPointW(hdc
, tx
, LineLen
, rc
.right
- x
, NULL
, NULL
, &szDim
))
760 nbl
= ReAlloc(bl
, (nBlocks
+ 1) * sizeof(DOC_TEXTBLOCK
));
766 cbl
= bl
+ nBlocks
- 1;
768 cbl
->nChars
= LineLen
;
769 cbl
->nSkip
= SkipChars
;
772 cbl
->rc
.right
= x
+ szDim
.cx
;
773 cbl
->rc
.bottom
= y
+ szDim
.cy
;
775 if (cbl
->rc
.right
> szDoc
.cx
)
776 szDoc
.cx
= cbl
->rc
.right
;
777 if (cbl
->rc
.bottom
> szDoc
.cy
)
778 szDoc
.cy
= cbl
->rc
.bottom
;
783 LineHeight
= max(LineHeight
, szDim
.cy
);
799 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
813 Current
->Blocks
= bl
;
817 SelectObject(hdc
, hOldFont
);
819 pRect
->right
= pRect
->left
+ szDoc
.cx
;
820 pRect
->bottom
= pRect
->top
+ szDoc
.cy
;
823 /***********************************************************************
825 * Draws the SysLink control.
827 static LRESULT
SYSLINK_Draw (const SYSLINK_INFO
*infoPtr
, HDC hdc
)
832 COLORREF OldTextColor
, OldBkColor
;
834 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
835 OldTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
836 OldBkColor
= SetBkColor(hdc
, infoPtr
->BackColor
);
838 GetClientRect(infoPtr
->Self
, &rc
);
839 rc
.right
-= SL_RIGHTMARGIN
+ SL_LEFTMARGIN
;
840 rc
.bottom
-= SL_BOTTOMMARGIN
+ SL_TOPMARGIN
;
842 if(rc
.right
< 0 || rc
.bottom
< 0) return 0;
844 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
850 bl
= Current
->Blocks
;
856 if(Current
->Type
== slText
)
858 SelectObject(hdc
, infoPtr
->Font
);
859 SetTextColor(hdc
, infoPtr
->TextColor
);
863 SelectObject(hdc
, infoPtr
->LinkFont
);
864 SetTextColor(hdc
, (!(Current
->u
.Link
.state
& LIS_VISITED
) ? infoPtr
->LinkColor
: infoPtr
->VisitedColor
));
870 ExtTextOutW(hdc
, bl
->rc
.left
, bl
->rc
.top
, ETO_OPAQUE
| ETO_CLIPPED
, &bl
->rc
, tx
, bl
->nChars
, NULL
);
871 if((Current
->Type
== slLink
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && infoPtr
->HasFocus
)
873 COLORREF PrevTextColor
;
874 PrevTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
875 DrawFocusRect(hdc
, &bl
->rc
);
876 SetTextColor(hdc
, PrevTextColor
);
879 n
-= bl
->nChars
+ bl
->nSkip
;
885 SetBkColor(hdc
, OldBkColor
);
886 SetTextColor(hdc
, OldTextColor
);
887 SelectObject(hdc
, hOldFont
);
893 /***********************************************************************
895 * Handles the WM_PAINT message.
897 static LRESULT
SYSLINK_Paint (const SYSLINK_INFO
*infoPtr
, HDC hdcParam
)
902 hdc
= hdcParam
? hdcParam
: BeginPaint (infoPtr
->Self
, &ps
);
905 SYSLINK_Draw (infoPtr
, hdc
);
906 if (!hdcParam
) EndPaint (infoPtr
->Self
, &ps
);
911 /***********************************************************************
913 * Handles the WM_ERASEBKGND message.
915 static LRESULT
SYSLINK_EraseBkgnd (const SYSLINK_INFO
*infoPtr
, HDC hdc
)
920 GetClientRect(infoPtr
->Self
, &r
);
921 hbr
= CreateSolidBrush(infoPtr
->BackColor
);
922 FillRect(hdc
, &r
, hbr
);
928 /***********************************************************************
930 * Set new Font for the SysLink control.
932 static HFONT
SYSLINK_SetFont (SYSLINK_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
938 HFONT hOldFont
= infoPtr
->Font
;
939 infoPtr
->Font
= hFont
;
941 /* free the underline font */
942 if(infoPtr
->LinkFont
!= NULL
)
944 DeleteObject(infoPtr
->LinkFont
);
945 infoPtr
->LinkFont
= NULL
;
948 /* Render text position and word wrapping in memory */
949 if (GetClientRect(infoPtr
->Self
, &rcClient
))
951 hdc
= GetDC(infoPtr
->Self
);
954 /* create a new underline font */
955 if(GetTextMetricsW(hdc
, &tm
) &&
956 GetObjectW(infoPtr
->Font
, sizeof(LOGFONTW
), &lf
))
958 lf
.lfUnderline
= TRUE
;
959 infoPtr
->LinkFont
= CreateFontIndirectW(&lf
);
960 infoPtr
->BreakChar
= tm
.tmBreakChar
;
964 ERR("Failed to create link font!\n");
967 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
968 ReleaseDC(infoPtr
->Self
, hdc
);
974 RedrawWindow(infoPtr
->Self
, NULL
, NULL
, RDW_INVALIDATE
| RDW_UPDATENOW
);
980 /***********************************************************************
982 * Set new text for the SysLink control.
984 static LRESULT
SYSLINK_SetText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
986 /* clear the document */
987 SYSLINK_ClearDoc(infoPtr
);
989 if(Text
== NULL
|| *Text
== 0)
994 /* let's parse the string and create a document */
995 if(SYSLINK_ParseText(infoPtr
, Text
) > 0)
999 /* Render text position and word wrapping in memory */
1000 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1002 HDC hdc
= GetDC(infoPtr
->Self
);
1005 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1006 ReleaseDC(infoPtr
->Self
, hdc
);
1008 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1016 /***********************************************************************
1017 * SYSLINK_SetFocusLink
1018 * Updates the focus status bits and focusses the specified link.
1019 * If no document item is specified, the focus bit will be removed from all links.
1020 * Returns the previous focused item.
1022 static PDOC_ITEM
SYSLINK_SetFocusLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
1024 PDOC_ITEM Current
, PrevFocus
= NULL
;
1026 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1028 if(Current
->Type
== slLink
)
1030 if((PrevFocus
== NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
))
1032 PrevFocus
= Current
;
1035 if(Current
== DocItem
)
1037 Current
->u
.Link
.state
|= LIS_FOCUSED
;
1041 Current
->u
.Link
.state
&= ~LIS_FOCUSED
;
1049 /***********************************************************************
1051 * Sets the states and attributes of a link item.
1053 static LRESULT
SYSLINK_SetItem (const SYSLINK_INFO
*infoPtr
, const LITEM
*Item
)
1059 BOOL Repaint
= FALSE
;
1061 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1063 ERR("Invalid Flags!\n");
1067 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1070 ERR("Link %d couldn't be found\n", Item
->iLink
);
1074 if(Item
->mask
& LIF_ITEMID
)
1076 nc
= min(lstrlenW(Item
->szID
), MAX_LINKID_TEXT
- 1);
1077 szId
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1080 lstrcpynW(szId
, Item
->szID
, nc
+ 1);
1084 ERR("Unable to allocate memory for link id\n");
1089 if(Item
->mask
& LIF_URL
)
1091 nc
= min(lstrlenW(Item
->szUrl
), L_MAX_URL_LENGTH
- 1);
1092 szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1095 lstrcpynW(szUrl
, Item
->szUrl
, nc
+ 1);
1101 ERR("Unable to allocate memory for link url\n");
1106 if(Item
->mask
& LIF_ITEMID
)
1108 Free(di
->u
.Link
.szID
);
1109 di
->u
.Link
.szID
= szId
;
1112 if(Item
->mask
& LIF_URL
)
1114 Free(di
->u
.Link
.szUrl
);
1115 di
->u
.Link
.szUrl
= szUrl
;
1118 if(Item
->mask
& LIF_STATE
)
1120 UINT oldstate
= di
->u
.Link
.state
;
1121 /* clear the masked bits */
1122 di
->u
.Link
.state
&= ~(Item
->stateMask
& LIS_MASK
);
1124 di
->u
.Link
.state
|= (Item
->state
& Item
->stateMask
) & LIS_MASK
;
1125 Repaint
= (oldstate
!= di
->u
.Link
.state
);
1127 /* update the focus */
1128 SYSLINK_SetFocusLink(infoPtr
, ((di
->u
.Link
.state
& LIS_FOCUSED
) ? di
: NULL
));
1133 SYSLINK_RepaintLink(infoPtr
, di
);
1139 /***********************************************************************
1141 * Retrieves the states and attributes of a link item.
1143 static LRESULT
SYSLINK_GetItem (const SYSLINK_INFO
*infoPtr
, PLITEM Item
)
1147 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1149 ERR("Invalid Flags!\n");
1153 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1156 ERR("Link %d couldn't be found\n", Item
->iLink
);
1160 if(Item
->mask
& LIF_STATE
)
1162 Item
->state
= (di
->u
.Link
.state
& Item
->stateMask
);
1163 if(!infoPtr
->HasFocus
)
1165 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1166 Item
->state
&= ~LIS_FOCUSED
;
1170 if(Item
->mask
& LIF_ITEMID
)
1174 lstrcpyW(Item
->szID
, di
->u
.Link
.szID
);
1182 if(Item
->mask
& LIF_URL
)
1184 if(di
->u
.Link
.szUrl
)
1186 lstrcpyW(Item
->szUrl
, di
->u
.Link
.szUrl
);
1197 /***********************************************************************
1198 * SYSLINK_PtInDocItem
1199 * Determines if a point is in the region of a document item
1201 static BOOL
SYSLINK_PtInDocItem (const DOC_ITEM
*DocItem
, POINT pt
)
1206 bl
= DocItem
->Blocks
;
1213 if (PtInRect(&bl
->rc
, pt
))
1217 n
-= bl
->nChars
+ bl
->nSkip
;
1225 /***********************************************************************
1227 * Determines the link the user clicked on.
1229 static LRESULT
SYSLINK_HitTest (const SYSLINK_INFO
*infoPtr
, PLHITTESTINFO HitTest
)
1234 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1236 if(Current
->Type
== slLink
)
1238 if(SYSLINK_PtInDocItem(Current
, HitTest
->pt
))
1240 HitTest
->item
.mask
= 0;
1241 HitTest
->item
.iLink
= id
;
1242 HitTest
->item
.state
= 0;
1243 HitTest
->item
.stateMask
= 0;
1244 if(Current
->u
.Link
.szID
)
1246 lstrcpyW(HitTest
->item
.szID
, Current
->u
.Link
.szID
);
1250 HitTest
->item
.szID
[0] = 0;
1252 if(Current
->u
.Link
.szUrl
)
1254 lstrcpyW(HitTest
->item
.szUrl
, Current
->u
.Link
.szUrl
);
1258 HitTest
->item
.szUrl
[0] = 0;
1269 /***********************************************************************
1270 * SYSLINK_GetIdealHeight
1271 * Returns the preferred height of a link at the current control's width.
1273 static LRESULT
SYSLINK_GetIdealHeight (const SYSLINK_INFO
*infoPtr
)
1275 HDC hdc
= GetDC(infoPtr
->Self
);
1280 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1282 if(GetTextMetricsW(hdc
, &tm
))
1284 height
= tm
.tmHeight
;
1290 SelectObject(hdc
, hOldFont
);
1291 ReleaseDC(infoPtr
->Self
, hdc
);
1298 /***********************************************************************
1299 * SYSLINK_SendParentNotify
1300 * Sends a WM_NOTIFY message to the parent window.
1302 static LRESULT
SYSLINK_SendParentNotify (const SYSLINK_INFO
*infoPtr
, UINT code
, const DOC_ITEM
*Link
, int iLink
)
1306 nml
.hdr
.hwndFrom
= infoPtr
->Self
;
1307 nml
.hdr
.idFrom
= GetWindowLongPtrW(infoPtr
->Self
, GWLP_ID
);
1308 nml
.hdr
.code
= code
;
1311 nml
.item
.iLink
= iLink
;
1313 nml
.item
.stateMask
= 0;
1314 if(Link
->u
.Link
.szID
)
1316 lstrcpyW(nml
.item
.szID
, Link
->u
.Link
.szID
);
1320 nml
.item
.szID
[0] = 0;
1322 if(Link
->u
.Link
.szUrl
)
1324 lstrcpyW(nml
.item
.szUrl
, Link
->u
.Link
.szUrl
);
1328 nml
.item
.szUrl
[0] = 0;
1331 return SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, nml
.hdr
.idFrom
, (LPARAM
)&nml
);
1334 /***********************************************************************
1336 * Handles receiving the input focus.
1338 static LRESULT
SYSLINK_SetFocus (SYSLINK_INFO
*infoPtr
)
1342 infoPtr
->HasFocus
= TRUE
;
1344 /* We always select the first link, even if we activated the control using
1345 SHIFT+TAB. This is the default behavior */
1346 Focus
= SYSLINK_GetNextLink(infoPtr
, NULL
);
1349 SYSLINK_SetFocusLink(infoPtr
, Focus
);
1350 SYSLINK_RepaintLink(infoPtr
, Focus
);
1355 /***********************************************************************
1357 * Handles losing the input focus.
1359 static LRESULT
SYSLINK_KillFocus (SYSLINK_INFO
*infoPtr
)
1363 infoPtr
->HasFocus
= FALSE
;
1364 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1368 SYSLINK_RepaintLink(infoPtr
, Focus
);
1374 /***********************************************************************
1376 * Returns a link at the specified position
1378 static PDOC_ITEM
SYSLINK_LinkAtPt (const SYSLINK_INFO
*infoPtr
, const POINT
*pt
, int *LinkId
, BOOL MustBeEnabled
)
1383 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1385 if((Current
->Type
== slLink
) && SYSLINK_PtInDocItem(Current
, *pt
) &&
1386 (!MustBeEnabled
|| (MustBeEnabled
&& (Current
->u
.Link
.state
& LIS_ENABLED
))))
1400 /***********************************************************************
1401 * SYSLINK_LButtonDown
1402 * Handles mouse clicks
1404 static LRESULT
SYSLINK_LButtonDown (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1406 PDOC_ITEM Current
, Old
;
1409 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1412 SetFocus(infoPtr
->Self
);
1414 Old
= SYSLINK_SetFocusLink(infoPtr
, Current
);
1415 if(Old
!= NULL
&& Old
!= Current
)
1417 SYSLINK_RepaintLink(infoPtr
, Old
);
1419 infoPtr
->MouseDownID
= id
;
1420 SYSLINK_RepaintLink(infoPtr
, Current
);
1426 /***********************************************************************
1428 * Handles mouse clicks
1430 static LRESULT
SYSLINK_LButtonUp (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1432 if(infoPtr
->MouseDownID
> -1)
1437 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1438 if((Current
!= NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && (infoPtr
->MouseDownID
== id
))
1440 SYSLINK_SendParentNotify(infoPtr
, NM_CLICK
, Current
, id
);
1444 infoPtr
->MouseDownID
= -1;
1449 /***********************************************************************
1451 * Handles ENTER key events
1453 static BOOL
SYSLINK_OnEnter (const SYSLINK_INFO
*infoPtr
)
1455 if(infoPtr
->HasFocus
&& !infoPtr
->IgnoreReturn
)
1460 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1463 SYSLINK_SendParentNotify(infoPtr
, NM_RETURN
, Focus
, id
);
1470 /***********************************************************************
1471 * SYSKEY_SelectNextPrevLink
1472 * Changes the currently focused link
1474 static BOOL
SYSKEY_SelectNextPrevLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1476 if(infoPtr
->HasFocus
)
1481 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1484 PDOC_ITEM NewFocus
, OldFocus
;
1487 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1489 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1491 if(NewFocus
!= NULL
)
1493 OldFocus
= SYSLINK_SetFocusLink(infoPtr
, NewFocus
);
1495 if(OldFocus
&& OldFocus
!= NewFocus
)
1497 SYSLINK_RepaintLink(infoPtr
, OldFocus
);
1499 SYSLINK_RepaintLink(infoPtr
, NewFocus
);
1507 /***********************************************************************
1508 * SYSKEY_SelectNextPrevLink
1509 * Determines if there's a next or previous link to decide whether the control
1510 * should capture the tab key message
1512 static BOOL
SYSLINK_NoNextLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1514 PDOC_ITEM Focus
, NewFocus
;
1516 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1518 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1520 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1522 return NewFocus
== NULL
;
1525 /***********************************************************************
1526 * SYSLINK_GetIdealSize
1527 * Calculates the ideal size of a link control at a given maximum width.
1529 static VOID
SYSLINK_GetIdealSize (const SYSLINK_INFO
*infoPtr
, int cxMaxWidth
, LPSIZE lpSize
)
1534 rc
.left
= rc
.top
= rc
.bottom
= 0;
1535 rc
.right
= cxMaxWidth
;
1537 hdc
= GetDC(infoPtr
->Self
);
1540 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1542 SYSLINK_Render(infoPtr
, hdc
, &rc
);
1544 SelectObject(hdc
, hOldFont
);
1545 ReleaseDC(infoPtr
->Self
, hdc
);
1547 lpSize
->cx
= rc
.right
;
1548 lpSize
->cy
= rc
.bottom
;
1552 /***********************************************************************
1555 static LRESULT WINAPI
SysLinkWindowProc(HWND hwnd
, UINT message
,
1556 WPARAM wParam
, LPARAM lParam
)
1558 SYSLINK_INFO
*infoPtr
;
1560 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd
, message
, wParam
, lParam
);
1562 infoPtr
= (SYSLINK_INFO
*)GetWindowLongPtrW(hwnd
, 0);
1564 if (!infoPtr
&& message
!= WM_CREATE
)
1565 goto HandleDefaultMessage
;
1568 case WM_PRINTCLIENT
:
1570 return SYSLINK_Paint (infoPtr
, (HDC
)wParam
);
1573 return SYSLINK_EraseBkgnd(infoPtr
, (HDC
)wParam
);
1578 DWORD mp
= GetMessagePos();
1580 ht
.pt
.x
= (short)LOWORD(mp
);
1581 ht
.pt
.y
= (short)HIWORD(mp
);
1583 ScreenToClient(infoPtr
->Self
, &ht
.pt
);
1584 if(SYSLINK_HitTest (infoPtr
, &ht
))
1586 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_HAND
));
1589 /* let the default window proc handle this message */
1590 goto HandleDefaultMessage
;
1596 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1598 HDC hdc
= GetDC(infoPtr
->Self
);
1601 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1602 ReleaseDC(infoPtr
->Self
, hdc
);
1609 return (LRESULT
)infoPtr
->Font
;
1612 return (LRESULT
)SYSLINK_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
1615 SYSLINK_SetText(infoPtr
, (LPWSTR
)lParam
);
1616 goto HandleDefaultMessage
;
1618 case WM_LBUTTONDOWN
:
1621 pt
.x
= (short)LOWORD(lParam
);
1622 pt
.y
= (short)HIWORD(lParam
);
1623 return SYSLINK_LButtonDown(infoPtr
, &pt
);
1628 pt
.x
= (short)LOWORD(lParam
);
1629 pt
.y
= (short)HIWORD(lParam
);
1630 return SYSLINK_LButtonUp(infoPtr
, &pt
);
1638 SYSLINK_OnEnter(infoPtr
);
1642 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1643 SYSKEY_SelectNextPrevLink(infoPtr
, shift
);
1647 goto HandleDefaultMessage
;
1652 LRESULT Ret
= DLGC_HASSETSEL
;
1653 int vk
= (lParam
!= 0 ? (int)((LPMSG
)lParam
)->wParam
: 0);
1657 Ret
|= DLGC_WANTMESSAGE
;
1661 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1662 if(!SYSLINK_NoNextLink(infoPtr
, shift
))
1664 Ret
|= DLGC_WANTTAB
;
1668 Ret
|= DLGC_WANTCHARS
;
1680 pt
.x
= (short)LOWORD(lParam
);
1681 pt
.y
= (short)HIWORD(lParam
);
1683 GetClientRect(infoPtr
->Self
, &rc
);
1684 ScreenToClient(infoPtr
->Self
, &pt
);
1685 if(pt
.x
< 0 || pt
.y
< 0 || pt
.x
> rc
.right
|| pt
.y
> rc
.bottom
)
1690 if(SYSLINK_LinkAtPt(infoPtr
, &pt
, NULL
, FALSE
))
1695 return HTTRANSPARENT
;
1699 return SYSLINK_HitTest(infoPtr
, (PLHITTESTINFO
)lParam
);
1702 return SYSLINK_SetItem(infoPtr
, (PLITEM
)lParam
);
1705 return SYSLINK_GetItem(infoPtr
, (PLITEM
)lParam
);
1707 case LM_GETIDEALHEIGHT
:
1710 /* LM_GETIDEALSIZE */
1711 SYSLINK_GetIdealSize(infoPtr
, (int)wParam
, (LPSIZE
)lParam
);
1713 return SYSLINK_GetIdealHeight(infoPtr
);
1716 return SYSLINK_SetFocus(infoPtr
);
1719 return SYSLINK_KillFocus(infoPtr
);
1722 infoPtr
->Style
&= ~WS_DISABLED
;
1723 infoPtr
->Style
|= (wParam
? 0 : WS_DISABLED
);
1724 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
1727 case WM_STYLECHANGED
:
1728 if (wParam
== GWL_STYLE
)
1730 infoPtr
->Style
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
1732 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1737 /* allocate memory for info struct */
1738 infoPtr
= Alloc (sizeof(SYSLINK_INFO
));
1739 if (!infoPtr
) return -1;
1740 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
1742 /* initialize the info struct */
1743 infoPtr
->Self
= hwnd
;
1744 infoPtr
->Notify
= ((LPCREATESTRUCTW
)lParam
)->hwndParent
;
1745 infoPtr
->Style
= ((LPCREATESTRUCTW
)lParam
)->style
;
1747 infoPtr
->LinkFont
= 0;
1748 infoPtr
->Items
= NULL
;
1749 infoPtr
->HasFocus
= FALSE
;
1750 infoPtr
->MouseDownID
= -1;
1751 infoPtr
->TextColor
= comctl32_color
.clrWindowText
;
1752 infoPtr
->LinkColor
= comctl32_color
.clrHighlight
;
1753 infoPtr
->VisitedColor
= comctl32_color
.clrHighlight
;
1754 infoPtr
->BackColor
= infoPtr
->Style
& LWS_TRANSPARENT
?
1755 comctl32_color
.clrWindow
: comctl32_color
.clrBtnFace
;
1756 infoPtr
->BreakChar
= ' ';
1757 infoPtr
->IgnoreReturn
= infoPtr
->Style
& LWS_IGNORERETURN
;
1758 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd
);
1759 SYSLINK_SetText(infoPtr
, ((LPCREATESTRUCTW
)lParam
)->lpszName
);
1763 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd
);
1764 SYSLINK_ClearDoc(infoPtr
);
1765 if(infoPtr
->Font
!= 0) DeleteObject(infoPtr
->Font
);
1766 if(infoPtr
->LinkFont
!= 0) DeleteObject(infoPtr
->LinkFont
);
1767 SetWindowLongPtrW(hwnd
, 0, 0);
1771 case WM_SYSCOLORCHANGE
:
1772 COMCTL32_RefreshSysColors();
1773 if (infoPtr
->Style
& LWS_TRANSPARENT
)
1774 infoPtr
->BackColor
= comctl32_color
.clrWindow
;
1778 HandleDefaultMessage
:
1779 if ((message
>= WM_USER
) && (message
< WM_APP
) && !COMCTL32_IsReflectedMessage(message
))
1781 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message
, wParam
, lParam
);
1783 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1788 /***********************************************************************
1789 * SYSLINK_Register [Internal]
1791 * Registers the SysLink window class.
1793 VOID
SYSLINK_Register (void)
1797 ZeroMemory (&wndClass
, sizeof(wndClass
));
1798 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
1799 wndClass
.lpfnWndProc
= SysLinkWindowProc
;
1800 wndClass
.cbClsExtra
= 0;
1801 wndClass
.cbWndExtra
= sizeof (SYSLINK_INFO
*);
1802 wndClass
.hCursor
= LoadCursorW (0, (LPWSTR
)IDC_ARROW
);
1803 wndClass
.lpszClassName
= WC_LINK
;
1805 RegisterClassW (&wndClass
);
1809 /***********************************************************************
1810 * SYSLINK_Unregister [Internal]
1812 * Unregisters the SysLink window class.
1814 VOID
SYSLINK_Unregister (void)
1816 UnregisterClassW (WC_LINK
, NULL
);