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 */
101 static const WCHAR SL_LINKOPEN
[] = { '<','a', 0 };
102 static const WCHAR SL_HREF
[] = { 'h','r','e','f','=','\"',0 };
103 static const WCHAR SL_ID
[] = { 'i','d','=','\"',0 };
104 static const WCHAR SL_LINKCLOSE
[] = { '<','/','a','>',0 };
106 /* Control configuration constants */
108 #define SL_LEFTMARGIN (0)
109 #define SL_TOPMARGIN (0)
110 #define SL_RIGHTMARGIN (0)
111 #define SL_BOTTOMMARGIN (0)
113 /***********************************************************************
114 * SYSLINK_FreeDocItem
115 * Frees all data and gdi objects associated with a document item
117 static VOID
SYSLINK_FreeDocItem (PDOC_ITEM DocItem
)
119 if(DocItem
->Type
== slLink
)
121 Free(DocItem
->u
.Link
.szID
);
122 Free(DocItem
->u
.Link
.szUrl
);
125 /* we don't free Text because it's just a pointer to a character in the
126 entire window text string */
131 /***********************************************************************
132 * SYSLINK_AppendDocItem
133 * Create and append a new document item.
135 static PDOC_ITEM
SYSLINK_AppendDocItem (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
, UINT textlen
,
136 SL_ITEM_TYPE type
, PDOC_ITEM LastItem
)
140 textlen
= min(textlen
, strlenW(Text
));
141 Item
= Alloc(FIELD_OFFSET(DOC_ITEM
, Text
[textlen
+ 1]));
144 ERR("Failed to alloc DOC_ITEM structure!\n");
149 Item
->nText
= textlen
;
155 LastItem
->Next
= Item
;
159 infoPtr
->Items
= Item
;
162 lstrcpynW(Item
->Text
, Text
, textlen
+ 1);
167 /***********************************************************************
169 * Clears the document tree
171 static VOID
SYSLINK_ClearDoc (SYSLINK_INFO
*infoPtr
)
173 PDOC_ITEM Item
, Next
;
175 Item
= infoPtr
->Items
;
179 SYSLINK_FreeDocItem(Item
);
183 infoPtr
->Items
= NULL
;
186 /***********************************************************************
188 * Parses the window text string and creates a document. Returns the
189 * number of document items created.
191 static UINT
SYSLINK_ParseText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
193 LPCWSTR current
, textstart
= NULL
, linktext
= NULL
, firsttag
= NULL
;
194 int taglen
= 0, textlen
= 0, linklen
= 0, docitems
= 0;
195 PDOC_ITEM Last
= NULL
;
196 SL_ITEM_TYPE CurrentType
= slText
;
200 for(current
= Text
; *current
!= 0;)
204 if(!StrCmpNIW(current
, SL_LINKOPEN
, 2) && (CurrentType
== slText
))
206 BOOL ValidParam
= FALSE
, ValidLink
= FALSE
;
208 if(*(current
+ 2) == '>')
210 /* we just have to deal with a <a> tag */
219 else if(*(current
+ 2) == infoPtr
->BreakChar
)
221 /* we expect parameters, parse them */
222 LPCWSTR
*CurrentParameter
= NULL
, tmp
;
223 UINT
*CurrentParameterLen
= NULL
;
226 tmp
= current
+ taglen
;
231 /* compare the current position with all known parameters */
232 if(!StrCmpNIW(tmp
, SL_HREF
, 6))
236 CurrentParameter
= &lpUrl
;
237 CurrentParameterLen
= &lenUrl
;
239 else if(!StrCmpNIW(tmp
, SL_ID
, 4))
243 CurrentParameter
= &lpID
;
244 CurrentParameterLen
= &lenId
;
253 /* we got a known parameter, now search until the next " character.
254 If we can't find a " character, there's a syntax error and we just assume it's text */
256 *CurrentParameter
= current
+ taglen
;
257 *CurrentParameterLen
= 0;
259 for(tmp
= *CurrentParameter
; *tmp
!= 0; tmp
++)
268 (*CurrentParameterLen
)++;
273 /* we're done with this parameter, now there are only 2 possibilities:
274 * 1. another parameter is coming, so expect a ' ' (space) character
275 * 2. the tag is being closed, so expect a '<' character
277 if(*tmp
== infoPtr
->BreakChar
)
279 /* we expect another parameter, do the whole thing again */
286 /* the tag is being closed, we're done */
293 if(ValidLink
&& ValidParam
)
295 /* the <a ...> tag appears to be valid. save all information
296 so we can add the link if we find a valid </a> tag later */
297 CurrentType
= slLink
;
298 linktext
= current
+ taglen
;
307 if(textstart
== NULL
)
313 else if(!StrCmpNIW(current
, SL_LINKCLOSE
, 4) && (CurrentType
== slLink
) && firsttag
)
315 /* there's a <a...> tag opened, first add the previous text, if present */
316 if(textstart
!= NULL
&& textlen
> 0 && firsttag
> textstart
)
318 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, firsttag
- textstart
, slText
, Last
);
321 ERR("Unable to create new document item!\n");
329 /* now it's time to add the link to the document */
331 if(linktext
!= NULL
&& linklen
> 0)
333 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slLink
, Last
);
336 ERR("Unable to create new document item!\n");
340 if(CurrentType
== slLink
)
344 if(!(infoPtr
->Style
& WS_DISABLED
))
346 Last
->u
.Link
.state
|= LIS_ENABLED
;
348 /* Copy the tag parameters */
351 nc
= min(lenId
, strlenW(lpID
));
352 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
353 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
354 if(Last
->u
.Link
.szID
!= NULL
)
356 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
360 Last
->u
.Link
.szID
= NULL
;
363 nc
= min(lenUrl
, strlenW(lpUrl
));
364 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
365 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
366 if(Last
->u
.Link
.szUrl
!= NULL
)
368 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
372 Last
->u
.Link
.szUrl
= NULL
;
376 CurrentType
= slText
;
383 /* we don't know what tag it is, so just continue */
386 if(CurrentType
== slText
&& textstart
== NULL
)
400 /* save the pointer of the current text item if we couldn't find a tag */
401 if(textstart
== NULL
&& CurrentType
== slText
)
410 if(textstart
!= NULL
&& textlen
> 0)
412 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, textlen
, CurrentType
, Last
);
415 ERR("Unable to create new document item!\n");
418 if(CurrentType
== slLink
)
422 if(!(infoPtr
->Style
& WS_DISABLED
))
424 Last
->u
.Link
.state
|= LIS_ENABLED
;
426 /* Copy the tag parameters */
429 nc
= min(lenId
, strlenW(lpID
));
430 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
431 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
432 if(Last
->u
.Link
.szID
!= NULL
)
434 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
438 Last
->u
.Link
.szID
= NULL
;
441 nc
= min(lenUrl
, strlenW(lpUrl
));
442 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
443 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
444 if(Last
->u
.Link
.szUrl
!= NULL
)
446 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
450 Last
->u
.Link
.szUrl
= NULL
;
455 if(linktext
!= NULL
&& linklen
> 0)
457 /* we got an unclosed link, just display the text */
458 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slText
, Last
);
461 ERR("Unable to create new document item!\n");
470 /***********************************************************************
471 * SYSLINK_RepaintLink
474 static VOID
SYSLINK_RepaintLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
479 if(DocItem
->Type
!= slLink
)
481 ERR("DocItem not a link!\n");
485 bl
= DocItem
->Blocks
;
492 InvalidateRect(infoPtr
->Self
, &bl
->rc
, TRUE
);
493 n
-= bl
->nChars
+ bl
->nSkip
;
499 /***********************************************************************
500 * SYSLINK_GetLinkItemByIndex
501 * Retrieves a document link by its index
503 static PDOC_ITEM
SYSLINK_GetLinkItemByIndex (const SYSLINK_INFO
*infoPtr
, int iLink
)
505 PDOC_ITEM Current
= infoPtr
->Items
;
507 while(Current
!= NULL
)
509 if((Current
->Type
== slLink
) && (iLink
-- <= 0))
513 Current
= Current
->Next
;
518 /***********************************************************************
519 * SYSLINK_GetFocusLink
520 * Retrieves the link that has the LIS_FOCUSED bit
522 static PDOC_ITEM
SYSLINK_GetFocusLink (const SYSLINK_INFO
*infoPtr
, int *LinkId
)
524 PDOC_ITEM Current
= infoPtr
->Items
;
527 while(Current
!= NULL
)
529 if((Current
->Type
== slLink
))
531 if(Current
->u
.Link
.state
& LIS_FOCUSED
)
539 Current
= Current
->Next
;
544 /***********************************************************************
545 * SYSLINK_GetNextLink
548 static PDOC_ITEM
SYSLINK_GetNextLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
550 for(Current
= (Current
!= NULL
? Current
->Next
: infoPtr
->Items
);
552 Current
= Current
->Next
)
554 if(Current
->Type
== slLink
)
562 /***********************************************************************
563 * SYSLINK_GetPrevLink
564 * Gets the previous link
566 static PDOC_ITEM
SYSLINK_GetPrevLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
570 /* returns the last link */
571 PDOC_ITEM Last
= NULL
;
573 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
575 if(Current
->Type
== slLink
)
584 /* returns the previous link */
585 PDOC_ITEM Cur
, Prev
= NULL
;
587 for(Cur
= infoPtr
->Items
; Cur
!= NULL
; Cur
= Cur
->Next
)
593 if(Cur
->Type
== slLink
)
602 /***********************************************************************
604 * Tries to wrap a line.
606 static BOOL
SYSLINK_WrapLine (LPWSTR Text
, WCHAR BreakChar
, int *LineLen
,
607 int nFit
, LPSIZE Extent
)
618 Current
= Text
+ nFit
;
620 /* check if we're in the middle of a word */
621 if((*Current
) != BreakChar
)
623 /* search for the beginning of the word */
624 while(Current
> Text
&& (*(Current
- 1)) != BreakChar
)
641 /***********************************************************************
643 * Renders the document in memory
645 static VOID
SYSLINK_Render (const SYSLINK_INFO
*infoPtr
, HDC hdc
, PRECT pRect
)
650 int x
, y
, LineHeight
;
653 szDoc
.cx
= szDoc
.cy
= 0;
656 rc
.right
-= SL_RIGHTMARGIN
;
657 rc
.bottom
-= SL_BOTTOMMARGIN
;
659 if(rc
.right
- SL_LEFTMARGIN
< 0)
661 if (rc
.bottom
- SL_TOPMARGIN
< 0)
664 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
670 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
674 PDOC_TEXTBLOCK bl
, cbl
;
678 if(Current
->nText
== 0)
686 Free(Current
->Blocks
);
687 Current
->Blocks
= NULL
;
691 if(Current
->Type
== slText
)
693 SelectObject(hdc
, infoPtr
->Font
);
695 else if(Current
->Type
== slLink
)
697 SelectObject(hdc
, infoPtr
->LinkFont
);
704 /* skip break characters unless they're the first of the doc item */
705 if(tx
!= Current
->Text
|| x
== SL_LEFTMARGIN
)
707 while(n
> 0 && (*tx
) == infoPtr
->BreakChar
)
715 if((n
== 0 && SkipChars
!= 0) ||
716 GetTextExtentExPointW(hdc
, tx
, n
, rc
.right
- x
, &nFit
, NULL
, &szDim
))
724 Wrap
= SYSLINK_WrapLine(tx
, infoPtr
->BreakChar
, &LineLen
, nFit
, &szDim
);
728 if(x
> SL_LEFTMARGIN
)
730 /* move one line down, the word didn't fit into the line */
738 /* the word starts at the beginning of the line and doesn't
739 fit into the line, so break it at the last character that fits */
740 LineLen
= max(nFit
, 1);
746 if(!GetTextExtentExPointW(hdc
, tx
, LineLen
, rc
.right
- x
, NULL
, NULL
, &szDim
))
759 nbl
= ReAlloc(bl
, (nBlocks
+ 1) * sizeof(DOC_TEXTBLOCK
));
765 cbl
= bl
+ nBlocks
- 1;
767 cbl
->nChars
= LineLen
;
768 cbl
->nSkip
= SkipChars
;
771 cbl
->rc
.right
= x
+ szDim
.cx
;
772 cbl
->rc
.bottom
= y
+ szDim
.cy
;
774 if (cbl
->rc
.right
> szDoc
.cx
)
775 szDoc
.cx
= cbl
->rc
.right
;
776 if (cbl
->rc
.bottom
> szDoc
.cy
)
777 szDoc
.cy
= cbl
->rc
.bottom
;
782 LineHeight
= max(LineHeight
, szDim
.cy
);
798 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
812 Current
->Blocks
= bl
;
816 SelectObject(hdc
, hOldFont
);
818 pRect
->right
= pRect
->left
+ szDoc
.cx
;
819 pRect
->bottom
= pRect
->top
+ szDoc
.cy
;
822 /***********************************************************************
824 * Draws the SysLink control.
826 static LRESULT
SYSLINK_Draw (const SYSLINK_INFO
*infoPtr
, HDC hdc
)
831 COLORREF OldTextColor
, OldBkColor
;
833 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
834 OldTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
835 OldBkColor
= SetBkColor(hdc
, infoPtr
->BackColor
);
837 GetClientRect(infoPtr
->Self
, &rc
);
838 rc
.right
-= SL_RIGHTMARGIN
+ SL_LEFTMARGIN
;
839 rc
.bottom
-= SL_BOTTOMMARGIN
+ SL_TOPMARGIN
;
841 if(rc
.right
< 0 || rc
.bottom
< 0) return 0;
843 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
849 bl
= Current
->Blocks
;
855 if(Current
->Type
== slText
)
857 SelectObject(hdc
, infoPtr
->Font
);
858 SetTextColor(hdc
, infoPtr
->TextColor
);
862 SelectObject(hdc
, infoPtr
->LinkFont
);
863 SetTextColor(hdc
, (!(Current
->u
.Link
.state
& LIS_VISITED
) ? infoPtr
->LinkColor
: infoPtr
->VisitedColor
));
869 ExtTextOutW(hdc
, bl
->rc
.left
, bl
->rc
.top
, ETO_OPAQUE
| ETO_CLIPPED
, &bl
->rc
, tx
, bl
->nChars
, NULL
);
870 if((Current
->Type
== slLink
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && infoPtr
->HasFocus
)
872 COLORREF PrevTextColor
;
873 PrevTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
874 DrawFocusRect(hdc
, &bl
->rc
);
875 SetTextColor(hdc
, PrevTextColor
);
878 n
-= bl
->nChars
+ bl
->nSkip
;
884 SetBkColor(hdc
, OldBkColor
);
885 SetTextColor(hdc
, OldTextColor
);
886 SelectObject(hdc
, hOldFont
);
892 /***********************************************************************
894 * Handles the WM_PAINT message.
896 static LRESULT
SYSLINK_Paint (const SYSLINK_INFO
*infoPtr
, HDC hdcParam
)
901 hdc
= hdcParam
? hdcParam
: BeginPaint (infoPtr
->Self
, &ps
);
904 SYSLINK_Draw (infoPtr
, hdc
);
905 if (!hdcParam
) EndPaint (infoPtr
->Self
, &ps
);
910 /***********************************************************************
912 * Handles the WM_ERASEBKGND message.
914 static LRESULT
SYSLINK_EraseBkgnd (const SYSLINK_INFO
*infoPtr
, HDC hdc
)
919 GetClientRect(infoPtr
->Self
, &r
);
920 hbr
= CreateSolidBrush(infoPtr
->BackColor
);
921 FillRect(hdc
, &r
, hbr
);
927 /***********************************************************************
929 * Set new Font for the SysLink control.
931 static HFONT
SYSLINK_SetFont (SYSLINK_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
937 HFONT hOldFont
= infoPtr
->Font
;
938 infoPtr
->Font
= hFont
;
940 /* free the underline font */
941 if(infoPtr
->LinkFont
!= NULL
)
943 DeleteObject(infoPtr
->LinkFont
);
944 infoPtr
->LinkFont
= NULL
;
947 /* Render text position and word wrapping in memory */
948 if (GetClientRect(infoPtr
->Self
, &rcClient
))
950 hdc
= GetDC(infoPtr
->Self
);
953 /* create a new underline font */
954 if(GetTextMetricsW(hdc
, &tm
) &&
955 GetObjectW(infoPtr
->Font
, sizeof(LOGFONTW
), &lf
))
957 lf
.lfUnderline
= TRUE
;
958 infoPtr
->LinkFont
= CreateFontIndirectW(&lf
);
959 infoPtr
->BreakChar
= tm
.tmBreakChar
;
963 ERR("Failed to create link font!\n");
966 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
967 ReleaseDC(infoPtr
->Self
, hdc
);
973 RedrawWindow(infoPtr
->Self
, NULL
, NULL
, RDW_INVALIDATE
| RDW_UPDATENOW
);
979 /***********************************************************************
981 * Set new text for the SysLink control.
983 static LRESULT
SYSLINK_SetText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
985 /* clear the document */
986 SYSLINK_ClearDoc(infoPtr
);
988 if(Text
== NULL
|| *Text
== 0)
993 /* let's parse the string and create a document */
994 if(SYSLINK_ParseText(infoPtr
, Text
) > 0)
998 /* Render text position and word wrapping in memory */
999 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1001 HDC hdc
= GetDC(infoPtr
->Self
);
1004 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1005 ReleaseDC(infoPtr
->Self
, hdc
);
1007 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1015 /***********************************************************************
1016 * SYSLINK_SetFocusLink
1017 * Updates the focus status bits and focusses the specified link.
1018 * If no document item is specified, the focus bit will be removed from all links.
1019 * Returns the previous focused item.
1021 static PDOC_ITEM
SYSLINK_SetFocusLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
1023 PDOC_ITEM Current
, PrevFocus
= NULL
;
1025 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1027 if(Current
->Type
== slLink
)
1029 if((PrevFocus
== NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
))
1031 PrevFocus
= Current
;
1034 if(Current
== DocItem
)
1036 Current
->u
.Link
.state
|= LIS_FOCUSED
;
1040 Current
->u
.Link
.state
&= ~LIS_FOCUSED
;
1048 /***********************************************************************
1050 * Sets the states and attributes of a link item.
1052 static LRESULT
SYSLINK_SetItem (const SYSLINK_INFO
*infoPtr
, const LITEM
*Item
)
1058 BOOL Repaint
= FALSE
;
1060 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1062 ERR("Invalid Flags!\n");
1066 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1069 ERR("Link %d couldn't be found\n", Item
->iLink
);
1073 if(Item
->mask
& LIF_ITEMID
)
1075 nc
= min(lstrlenW(Item
->szID
), MAX_LINKID_TEXT
- 1);
1076 szId
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1079 lstrcpynW(szId
, Item
->szID
, nc
+ 1);
1083 ERR("Unable to allocate memory for link id\n");
1088 if(Item
->mask
& LIF_URL
)
1090 nc
= min(lstrlenW(Item
->szUrl
), L_MAX_URL_LENGTH
- 1);
1091 szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1094 lstrcpynW(szUrl
, Item
->szUrl
, nc
+ 1);
1100 ERR("Unable to allocate memory for link url\n");
1105 if(Item
->mask
& LIF_ITEMID
)
1107 Free(di
->u
.Link
.szID
);
1108 di
->u
.Link
.szID
= szId
;
1111 if(Item
->mask
& LIF_URL
)
1113 Free(di
->u
.Link
.szUrl
);
1114 di
->u
.Link
.szUrl
= szUrl
;
1117 if(Item
->mask
& LIF_STATE
)
1119 UINT oldstate
= di
->u
.Link
.state
;
1120 /* clear the masked bits */
1121 di
->u
.Link
.state
&= ~(Item
->stateMask
& LIS_MASK
);
1123 di
->u
.Link
.state
|= (Item
->state
& Item
->stateMask
) & LIS_MASK
;
1124 Repaint
= (oldstate
!= di
->u
.Link
.state
);
1126 /* update the focus */
1127 SYSLINK_SetFocusLink(infoPtr
, ((di
->u
.Link
.state
& LIS_FOCUSED
) ? di
: NULL
));
1132 SYSLINK_RepaintLink(infoPtr
, di
);
1138 /***********************************************************************
1140 * Retrieves the states and attributes of a link item.
1142 static LRESULT
SYSLINK_GetItem (const SYSLINK_INFO
*infoPtr
, PLITEM Item
)
1146 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1148 ERR("Invalid Flags!\n");
1152 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1155 ERR("Link %d couldn't be found\n", Item
->iLink
);
1159 if(Item
->mask
& LIF_STATE
)
1161 Item
->state
= (di
->u
.Link
.state
& Item
->stateMask
);
1162 if(!infoPtr
->HasFocus
)
1164 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1165 Item
->state
&= ~LIS_FOCUSED
;
1169 if(Item
->mask
& LIF_ITEMID
)
1173 lstrcpyW(Item
->szID
, di
->u
.Link
.szID
);
1181 if(Item
->mask
& LIF_URL
)
1183 if(di
->u
.Link
.szUrl
)
1185 lstrcpyW(Item
->szUrl
, di
->u
.Link
.szUrl
);
1196 /***********************************************************************
1197 * SYSLINK_PtInDocItem
1198 * Determines if a point is in the region of a document item
1200 static BOOL
SYSLINK_PtInDocItem (const DOC_ITEM
*DocItem
, POINT pt
)
1205 bl
= DocItem
->Blocks
;
1212 if (PtInRect(&bl
->rc
, pt
))
1216 n
-= bl
->nChars
+ bl
->nSkip
;
1224 /***********************************************************************
1226 * Determines the link the user clicked on.
1228 static LRESULT
SYSLINK_HitTest (const SYSLINK_INFO
*infoPtr
, PLHITTESTINFO HitTest
)
1233 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1235 if(Current
->Type
== slLink
)
1237 if(SYSLINK_PtInDocItem(Current
, HitTest
->pt
))
1239 HitTest
->item
.mask
= 0;
1240 HitTest
->item
.iLink
= id
;
1241 HitTest
->item
.state
= 0;
1242 HitTest
->item
.stateMask
= 0;
1243 if(Current
->u
.Link
.szID
)
1245 lstrcpyW(HitTest
->item
.szID
, Current
->u
.Link
.szID
);
1249 HitTest
->item
.szID
[0] = 0;
1251 if(Current
->u
.Link
.szUrl
)
1253 lstrcpyW(HitTest
->item
.szUrl
, Current
->u
.Link
.szUrl
);
1257 HitTest
->item
.szUrl
[0] = 0;
1268 /***********************************************************************
1269 * SYSLINK_GetIdealHeight
1270 * Returns the preferred height of a link at the current control's width.
1272 static LRESULT
SYSLINK_GetIdealHeight (const SYSLINK_INFO
*infoPtr
)
1274 HDC hdc
= GetDC(infoPtr
->Self
);
1279 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1281 if(GetTextMetricsW(hdc
, &tm
))
1283 height
= tm
.tmHeight
;
1289 SelectObject(hdc
, hOldFont
);
1290 ReleaseDC(infoPtr
->Self
, hdc
);
1297 /***********************************************************************
1298 * SYSLINK_SendParentNotify
1299 * Sends a WM_NOTIFY message to the parent window.
1301 static LRESULT
SYSLINK_SendParentNotify (const SYSLINK_INFO
*infoPtr
, UINT code
, const DOC_ITEM
*Link
, int iLink
)
1305 nml
.hdr
.hwndFrom
= infoPtr
->Self
;
1306 nml
.hdr
.idFrom
= GetWindowLongPtrW(infoPtr
->Self
, GWLP_ID
);
1307 nml
.hdr
.code
= code
;
1310 nml
.item
.iLink
= iLink
;
1312 nml
.item
.stateMask
= 0;
1313 if(Link
->u
.Link
.szID
)
1315 lstrcpyW(nml
.item
.szID
, Link
->u
.Link
.szID
);
1319 nml
.item
.szID
[0] = 0;
1321 if(Link
->u
.Link
.szUrl
)
1323 lstrcpyW(nml
.item
.szUrl
, Link
->u
.Link
.szUrl
);
1327 nml
.item
.szUrl
[0] = 0;
1330 return SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, nml
.hdr
.idFrom
, (LPARAM
)&nml
);
1333 /***********************************************************************
1335 * Handles receiving the input focus.
1337 static LRESULT
SYSLINK_SetFocus (SYSLINK_INFO
*infoPtr
)
1341 infoPtr
->HasFocus
= TRUE
;
1343 /* We always select the first link, even if we activated the control using
1344 SHIFT+TAB. This is the default behavior */
1345 Focus
= SYSLINK_GetNextLink(infoPtr
, NULL
);
1348 SYSLINK_SetFocusLink(infoPtr
, Focus
);
1349 SYSLINK_RepaintLink(infoPtr
, Focus
);
1354 /***********************************************************************
1356 * Handles losing the input focus.
1358 static LRESULT
SYSLINK_KillFocus (SYSLINK_INFO
*infoPtr
)
1362 infoPtr
->HasFocus
= FALSE
;
1363 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1367 SYSLINK_RepaintLink(infoPtr
, Focus
);
1373 /***********************************************************************
1375 * Returns a link at the specified position
1377 static PDOC_ITEM
SYSLINK_LinkAtPt (const SYSLINK_INFO
*infoPtr
, const POINT
*pt
, int *LinkId
, BOOL MustBeEnabled
)
1382 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1384 if((Current
->Type
== slLink
) && SYSLINK_PtInDocItem(Current
, *pt
) &&
1385 (!MustBeEnabled
|| (MustBeEnabled
&& (Current
->u
.Link
.state
& LIS_ENABLED
))))
1399 /***********************************************************************
1400 * SYSLINK_LButtonDown
1401 * Handles mouse clicks
1403 static LRESULT
SYSLINK_LButtonDown (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1405 PDOC_ITEM Current
, Old
;
1408 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1411 SetFocus(infoPtr
->Self
);
1413 Old
= SYSLINK_SetFocusLink(infoPtr
, Current
);
1414 if(Old
!= NULL
&& Old
!= Current
)
1416 SYSLINK_RepaintLink(infoPtr
, Old
);
1418 infoPtr
->MouseDownID
= id
;
1419 SYSLINK_RepaintLink(infoPtr
, Current
);
1425 /***********************************************************************
1427 * Handles mouse clicks
1429 static LRESULT
SYSLINK_LButtonUp (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1431 if(infoPtr
->MouseDownID
> -1)
1436 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1437 if((Current
!= NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && (infoPtr
->MouseDownID
== id
))
1439 SYSLINK_SendParentNotify(infoPtr
, NM_CLICK
, Current
, id
);
1443 infoPtr
->MouseDownID
= -1;
1448 /***********************************************************************
1450 * Handles ENTER key events
1452 static BOOL
SYSLINK_OnEnter (const SYSLINK_INFO
*infoPtr
)
1454 if(infoPtr
->HasFocus
)
1459 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1462 SYSLINK_SendParentNotify(infoPtr
, NM_RETURN
, Focus
, id
);
1469 /***********************************************************************
1470 * SYSKEY_SelectNextPrevLink
1471 * Changes the currently focused link
1473 static BOOL
SYSKEY_SelectNextPrevLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1475 if(infoPtr
->HasFocus
)
1480 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1483 PDOC_ITEM NewFocus
, OldFocus
;
1486 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1488 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1490 if(NewFocus
!= NULL
)
1492 OldFocus
= SYSLINK_SetFocusLink(infoPtr
, NewFocus
);
1494 if(OldFocus
&& OldFocus
!= NewFocus
)
1496 SYSLINK_RepaintLink(infoPtr
, OldFocus
);
1498 SYSLINK_RepaintLink(infoPtr
, NewFocus
);
1506 /***********************************************************************
1507 * SYSKEY_SelectNextPrevLink
1508 * Determines if there's a next or previous link to decide whether the control
1509 * should capture the tab key message
1511 static BOOL
SYSLINK_NoNextLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1513 PDOC_ITEM Focus
, NewFocus
;
1515 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1517 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1519 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1521 return NewFocus
== NULL
;
1524 /***********************************************************************
1525 * SYSLINK_GetIdealSize
1526 * Calculates the ideal size of a link control at a given maximum width.
1528 static VOID
SYSLINK_GetIdealSize (const SYSLINK_INFO
*infoPtr
, int cxMaxWidth
, LPSIZE lpSize
)
1533 rc
.left
= rc
.top
= rc
.bottom
= 0;
1534 rc
.right
= cxMaxWidth
;
1536 hdc
= GetDC(infoPtr
->Self
);
1539 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1541 SYSLINK_Render(infoPtr
, hdc
, &rc
);
1543 SelectObject(hdc
, hOldFont
);
1544 ReleaseDC(infoPtr
->Self
, hdc
);
1546 lpSize
->cx
= rc
.right
;
1547 lpSize
->cy
= rc
.bottom
;
1551 /***********************************************************************
1554 static LRESULT WINAPI
SysLinkWindowProc(HWND hwnd
, UINT message
,
1555 WPARAM wParam
, LPARAM lParam
)
1557 SYSLINK_INFO
*infoPtr
;
1559 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd
, message
, wParam
, lParam
);
1561 infoPtr
= (SYSLINK_INFO
*)GetWindowLongPtrW(hwnd
, 0);
1563 if (!infoPtr
&& message
!= WM_CREATE
)
1564 goto HandleDefaultMessage
;
1567 case WM_PRINTCLIENT
:
1569 return SYSLINK_Paint (infoPtr
, (HDC
)wParam
);
1572 return SYSLINK_EraseBkgnd(infoPtr
, (HDC
)wParam
);
1577 DWORD mp
= GetMessagePos();
1579 ht
.pt
.x
= (short)LOWORD(mp
);
1580 ht
.pt
.y
= (short)HIWORD(mp
);
1582 ScreenToClient(infoPtr
->Self
, &ht
.pt
);
1583 if(SYSLINK_HitTest (infoPtr
, &ht
))
1585 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_HAND
));
1588 /* let the default window proc handle this message */
1589 goto HandleDefaultMessage
;
1595 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1597 HDC hdc
= GetDC(infoPtr
->Self
);
1600 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1601 ReleaseDC(infoPtr
->Self
, hdc
);
1608 return (LRESULT
)infoPtr
->Font
;
1611 return (LRESULT
)SYSLINK_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
1614 SYSLINK_SetText(infoPtr
, (LPWSTR
)lParam
);
1615 goto HandleDefaultMessage
;
1617 case WM_LBUTTONDOWN
:
1620 pt
.x
= (short)LOWORD(lParam
);
1621 pt
.y
= (short)HIWORD(lParam
);
1622 return SYSLINK_LButtonDown(infoPtr
, &pt
);
1627 pt
.x
= (short)LOWORD(lParam
);
1628 pt
.y
= (short)HIWORD(lParam
);
1629 return SYSLINK_LButtonUp(infoPtr
, &pt
);
1637 SYSLINK_OnEnter(infoPtr
);
1641 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1642 SYSKEY_SelectNextPrevLink(infoPtr
, shift
);
1646 goto HandleDefaultMessage
;
1651 LRESULT Ret
= DLGC_HASSETSEL
;
1652 int vk
= (lParam
!= 0 ? (int)((LPMSG
)lParam
)->wParam
: 0);
1656 Ret
|= DLGC_WANTMESSAGE
;
1660 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1661 if(!SYSLINK_NoNextLink(infoPtr
, shift
))
1663 Ret
|= DLGC_WANTTAB
;
1667 Ret
|= DLGC_WANTCHARS
;
1679 pt
.x
= (short)LOWORD(lParam
);
1680 pt
.y
= (short)HIWORD(lParam
);
1682 GetClientRect(infoPtr
->Self
, &rc
);
1683 ScreenToClient(infoPtr
->Self
, &pt
);
1684 if(pt
.x
< 0 || pt
.y
< 0 || pt
.x
> rc
.right
|| pt
.y
> rc
.bottom
)
1689 if(SYSLINK_LinkAtPt(infoPtr
, &pt
, NULL
, FALSE
))
1694 return HTTRANSPARENT
;
1698 return SYSLINK_HitTest(infoPtr
, (PLHITTESTINFO
)lParam
);
1701 return SYSLINK_SetItem(infoPtr
, (PLITEM
)lParam
);
1704 return SYSLINK_GetItem(infoPtr
, (PLITEM
)lParam
);
1706 case LM_GETIDEALHEIGHT
:
1709 /* LM_GETIDEALSIZE */
1710 SYSLINK_GetIdealSize(infoPtr
, (int)wParam
, (LPSIZE
)lParam
);
1712 return SYSLINK_GetIdealHeight(infoPtr
);
1715 return SYSLINK_SetFocus(infoPtr
);
1718 return SYSLINK_KillFocus(infoPtr
);
1721 infoPtr
->Style
&= ~WS_DISABLED
;
1722 infoPtr
->Style
|= (wParam
? 0 : WS_DISABLED
);
1723 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
1726 case WM_STYLECHANGED
:
1727 if (wParam
== GWL_STYLE
)
1729 infoPtr
->Style
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
1731 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1736 /* allocate memory for info struct */
1737 infoPtr
= Alloc (sizeof(SYSLINK_INFO
));
1738 if (!infoPtr
) return -1;
1739 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
1741 /* initialize the info struct */
1742 infoPtr
->Self
= hwnd
;
1743 infoPtr
->Notify
= ((LPCREATESTRUCTW
)lParam
)->hwndParent
;
1744 infoPtr
->Style
= ((LPCREATESTRUCTW
)lParam
)->style
;
1746 infoPtr
->LinkFont
= 0;
1747 infoPtr
->Items
= NULL
;
1748 infoPtr
->HasFocus
= FALSE
;
1749 infoPtr
->MouseDownID
= -1;
1750 infoPtr
->TextColor
= comctl32_color
.clrWindowText
;
1751 infoPtr
->LinkColor
= comctl32_color
.clrHighlight
;
1752 infoPtr
->VisitedColor
= comctl32_color
.clrHighlight
;
1753 infoPtr
->BackColor
= infoPtr
->Style
& LWS_TRANSPARENT
?
1754 comctl32_color
.clrWindow
: comctl32_color
.clrBtnFace
;
1755 infoPtr
->BreakChar
= ' ';
1756 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd
);
1757 SYSLINK_SetText(infoPtr
, ((LPCREATESTRUCTW
)lParam
)->lpszName
);
1761 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd
);
1762 SYSLINK_ClearDoc(infoPtr
);
1763 if(infoPtr
->Font
!= 0) DeleteObject(infoPtr
->Font
);
1764 if(infoPtr
->LinkFont
!= 0) DeleteObject(infoPtr
->LinkFont
);
1765 SetWindowLongPtrW(hwnd
, 0, 0);
1769 case WM_SYSCOLORCHANGE
:
1770 COMCTL32_RefreshSysColors();
1771 if (infoPtr
->Style
& LWS_TRANSPARENT
)
1772 infoPtr
->BackColor
= comctl32_color
.clrWindow
;
1776 HandleDefaultMessage
:
1777 if ((message
>= WM_USER
) && (message
< WM_APP
) && !COMCTL32_IsReflectedMessage(message
))
1779 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message
, wParam
, lParam
);
1781 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1786 /***********************************************************************
1787 * SYSLINK_Register [Internal]
1789 * Registers the SysLink window class.
1791 VOID
SYSLINK_Register (void)
1795 ZeroMemory (&wndClass
, sizeof(wndClass
));
1796 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
1797 wndClass
.lpfnWndProc
= SysLinkWindowProc
;
1798 wndClass
.cbClsExtra
= 0;
1799 wndClass
.cbWndExtra
= sizeof (SYSLINK_INFO
*);
1800 wndClass
.hCursor
= LoadCursorW (0, (LPWSTR
)IDC_ARROW
);
1801 wndClass
.lpszClassName
= WC_LINK
;
1803 RegisterClassW (&wndClass
);
1807 /***********************************************************************
1808 * SYSLINK_Unregister [Internal]
1810 * Unregisters the SysLink window class.
1812 VOID
SYSLINK_Unregister (void)
1814 UnregisterClassW (WC_LINK
, NULL
);