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"
41 #include "wine/list.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(syslink
);
50 } DOC_TEXTBLOCK
, *PDOC_TEXTBLOCK
;
52 #define LIF_FLAGSMASK (LIF_STATE | LIF_ITEMID | LIF_URL)
53 #define LIS_MASK (LIS_FOCUSED | LIS_ENABLED | LIS_VISITED)
61 typedef struct _DOC_ITEM
64 UINT nText
; /* Number of characters of the text */
65 SL_ITEM_TYPE Type
; /* type of the item */
66 PDOC_TEXTBLOCK Blocks
; /* Array of text blocks */
71 UINT state
; /* Link state */
72 WCHAR
*szID
; /* Link ID string */
73 WCHAR
*szUrl
; /* Link URL string */
80 WCHAR Text
[1]; /* Text of the document item */
81 } DOC_ITEM
, *PDOC_ITEM
;
85 HWND Self
; /* The window handle for this control */
86 HWND Notify
; /* The parent handle to receive notifications */
87 DWORD Style
; /* Styles for this control */
88 struct list Items
; /* Document items list */
89 BOOL HasFocus
; /* Whether the control has the input focus */
90 int MouseDownID
; /* ID of the link that the mouse button first selected */
91 HFONT Font
; /* Handle to the font for text */
92 HFONT LinkFont
; /* Handle to the font for links */
93 COLORREF TextColor
; /* Color of the text */
94 COLORREF LinkColor
; /* Color of links */
95 COLORREF VisitedColor
; /* Color of visited links */
96 WCHAR BreakChar
; /* Break Character for the current font */
97 BOOL IgnoreReturn
; /* (infoPtr->Style & LWS_IGNORERETURN) on creation */
100 /* Control configuration constants */
102 #define SL_LEFTMARGIN (0)
103 #define SL_TOPMARGIN (0)
104 #define SL_RIGHTMARGIN (0)
105 #define SL_BOTTOMMARGIN (0)
107 /***********************************************************************
108 * SYSLINK_FreeDocItem
109 * Frees all data and gdi objects associated with a document item
111 static VOID
SYSLINK_FreeDocItem (PDOC_ITEM DocItem
)
113 if(DocItem
->Type
== slLink
)
115 Free(DocItem
->u
.Link
.szID
);
116 Free(DocItem
->u
.Link
.szUrl
);
119 Free(DocItem
->Blocks
);
121 /* we don't free Text because it's just a pointer to a character in the
122 entire window text string */
127 /***********************************************************************
128 * SYSLINK_AppendDocItem
129 * Create and append a new document item.
131 static PDOC_ITEM
SYSLINK_AppendDocItem (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
, UINT textlen
,
132 SL_ITEM_TYPE type
, PDOC_ITEM LastItem
)
136 textlen
= min(textlen
, strlenW(Text
));
137 Item
= Alloc(FIELD_OFFSET(DOC_ITEM
, Text
[textlen
+ 1]));
140 ERR("Failed to alloc DOC_ITEM structure!\n");
144 Item
->nText
= textlen
;
147 lstrcpynW(Item
->Text
, Text
, textlen
+ 1);
149 list_add_after(&LastItem
->entry
, &Item
->entry
);
151 list_add_tail(&infoPtr
->Items
, &Item
->entry
);
156 /***********************************************************************
158 * Clears the document tree
160 static VOID
SYSLINK_ClearDoc (SYSLINK_INFO
*infoPtr
)
162 DOC_ITEM
*Item
, *Item2
;
164 LIST_FOR_EACH_ENTRY_SAFE(Item
, Item2
, &infoPtr
->Items
, DOC_ITEM
, entry
)
166 list_remove(&Item
->entry
);
167 SYSLINK_FreeDocItem(Item
);
171 /***********************************************************************
173 * Parses the window text string and creates a document. Returns the
174 * number of document items created.
176 static UINT
SYSLINK_ParseText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
178 static const WCHAR SL_LINKOPEN
[] = { '<','a' };
179 static const WCHAR SL_HREF
[] = { 'h','r','e','f','=','\"' };
180 static const WCHAR SL_ID
[] = { 'i','d','=','\"' };
181 static const WCHAR SL_LINKCLOSE
[] = { '<','/','a','>' };
182 LPCWSTR current
, textstart
= NULL
, linktext
= NULL
, firsttag
= NULL
;
183 int taglen
= 0, textlen
= 0, linklen
= 0, docitems
= 0;
184 PDOC_ITEM Last
= NULL
;
185 SL_ITEM_TYPE CurrentType
= slText
;
189 TRACE("(%p %s)\n", infoPtr
, debugstr_w(Text
));
191 for(current
= Text
; *current
!= 0;)
195 if(!strncmpiW(current
, SL_LINKOPEN
, sizeof(SL_LINKOPEN
)/sizeof(SL_LINKOPEN
[0])) && (CurrentType
== slText
))
197 BOOL ValidParam
= FALSE
, ValidLink
= FALSE
;
199 if(*(current
+ 2) == '>')
201 /* we just have to deal with a <a> tag */
210 else if(*(current
+ 2) == infoPtr
->BreakChar
)
212 /* we expect parameters, parse them */
213 LPCWSTR
*CurrentParameter
= NULL
, tmp
;
214 UINT
*CurrentParameterLen
= NULL
;
217 tmp
= current
+ taglen
;
222 /* compare the current position with all known parameters */
223 if(!strncmpiW(tmp
, SL_HREF
, sizeof(SL_HREF
)/sizeof(SL_HREF
[0])))
227 CurrentParameter
= &lpUrl
;
228 CurrentParameterLen
= &lenUrl
;
230 else if(!strncmpiW(tmp
, SL_ID
, sizeof(SL_ID
)/sizeof(SL_ID
[0])))
234 CurrentParameter
= &lpID
;
235 CurrentParameterLen
= &lenId
;
244 /* we got a known parameter, now search until the next " character.
245 If we can't find a " character, there's a syntax error and we just assume it's text */
247 *CurrentParameter
= current
+ taglen
;
248 *CurrentParameterLen
= 0;
250 for(tmp
= *CurrentParameter
; *tmp
!= 0; tmp
++)
259 (*CurrentParameterLen
)++;
264 /* we're done with this parameter, now there are only 2 possibilities:
265 * 1. another parameter is coming, so expect a ' ' (space) character
266 * 2. the tag is being closed, so expect a '<' character
268 if(*tmp
== infoPtr
->BreakChar
)
270 /* we expect another parameter, do the whole thing again */
277 /* the tag is being closed, we're done */
284 if(ValidLink
&& ValidParam
)
286 /* the <a ...> tag appears to be valid. save all information
287 so we can add the link if we find a valid </a> tag later */
288 CurrentType
= slLink
;
289 linktext
= current
+ taglen
;
298 if(textstart
== NULL
)
304 else if(!strncmpiW(current
, SL_LINKCLOSE
, sizeof(SL_LINKCLOSE
)/sizeof(SL_LINKCLOSE
[0])) && (CurrentType
== slLink
) && firsttag
)
306 /* there's a <a...> tag opened, first add the previous text, if present */
307 if(textstart
!= NULL
&& textlen
> 0 && firsttag
> textstart
)
309 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, firsttag
- textstart
, slText
, Last
);
312 ERR("Unable to create new document item!\n");
320 /* now it's time to add the link to the document */
322 if(linktext
!= NULL
&& linklen
> 0)
324 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slLink
, Last
);
327 ERR("Unable to create new document item!\n");
331 if(CurrentType
== slLink
)
335 if(!(infoPtr
->Style
& WS_DISABLED
))
337 Last
->u
.Link
.state
|= LIS_ENABLED
;
339 /* Copy the tag parameters */
342 nc
= min(lenId
, strlenW(lpID
));
343 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
344 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
345 if(Last
->u
.Link
.szID
!= NULL
)
347 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
351 Last
->u
.Link
.szID
= NULL
;
354 nc
= min(lenUrl
, strlenW(lpUrl
));
355 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
356 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
357 if(Last
->u
.Link
.szUrl
!= NULL
)
359 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
363 Last
->u
.Link
.szUrl
= NULL
;
367 CurrentType
= slText
;
374 /* we don't know what tag it is, so just continue */
377 if(CurrentType
== slText
&& textstart
== NULL
)
391 /* save the pointer of the current text item if we couldn't find a tag */
392 if(textstart
== NULL
&& CurrentType
== slText
)
401 if(textstart
!= NULL
&& textlen
> 0)
403 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, textlen
, CurrentType
, Last
);
406 ERR("Unable to create new document item!\n");
409 if(CurrentType
== slLink
)
413 if(!(infoPtr
->Style
& WS_DISABLED
))
415 Last
->u
.Link
.state
|= LIS_ENABLED
;
417 /* Copy the tag parameters */
420 nc
= min(lenId
, strlenW(lpID
));
421 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
422 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
423 if(Last
->u
.Link
.szID
!= NULL
)
425 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
429 Last
->u
.Link
.szID
= NULL
;
432 nc
= min(lenUrl
, strlenW(lpUrl
));
433 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
434 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
435 if(Last
->u
.Link
.szUrl
!= NULL
)
437 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
441 Last
->u
.Link
.szUrl
= NULL
;
446 if(linktext
!= NULL
&& linklen
> 0)
448 /* we got an unclosed link, just display the text */
449 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slText
, Last
);
452 ERR("Unable to create new document item!\n");
461 /***********************************************************************
462 * SYSLINK_RepaintLink
465 static VOID
SYSLINK_RepaintLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
470 if(DocItem
->Type
!= slLink
)
472 ERR("DocItem not a link!\n");
476 bl
= DocItem
->Blocks
;
483 InvalidateRect(infoPtr
->Self
, &bl
->rc
, TRUE
);
484 n
-= bl
->nChars
+ bl
->nSkip
;
490 /***********************************************************************
491 * SYSLINK_GetLinkItemByIndex
492 * Retrieves a document link by its index
494 static PDOC_ITEM
SYSLINK_GetLinkItemByIndex (const SYSLINK_INFO
*infoPtr
, int iLink
)
498 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
500 if ((Current
->Type
== slLink
) && (iLink
-- <= 0))
506 /***********************************************************************
507 * SYSLINK_GetFocusLink
508 * Retrieves the link that has the LIS_FOCUSED bit
510 static PDOC_ITEM
SYSLINK_GetFocusLink (const SYSLINK_INFO
*infoPtr
, int *LinkId
)
515 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
517 if(Current
->Type
== slLink
)
519 if(Current
->u
.Link
.state
& LIS_FOCUSED
)
532 /***********************************************************************
533 * SYSLINK_GetNextLink
536 static PDOC_ITEM
SYSLINK_GetNextLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
540 LIST_FOR_EACH_ENTRY(Next
, Current
? &Current
->entry
: &infoPtr
->Items
, DOC_ITEM
, entry
)
542 if (Next
->Type
== slLink
)
550 /***********************************************************************
551 * SYSLINK_GetPrevLink
552 * Gets the previous link
554 static PDOC_ITEM
SYSLINK_GetPrevLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
558 LIST_FOR_EACH_ENTRY_REV(Prev
, Current
? &Current
->entry
: list_tail(&infoPtr
->Items
), DOC_ITEM
, entry
)
560 if (Prev
->Type
== slLink
)
569 /***********************************************************************
571 * Tries to wrap a line.
573 static BOOL
SYSLINK_WrapLine (LPWSTR Text
, WCHAR BreakChar
, int x
, int *LineLen
,
574 int nFit
, LPSIZE Extent
)
578 for (i
= 0; i
< nFit
; i
++) if (Text
[i
] == '\n') break;
580 if (i
== *LineLen
) return FALSE
;
582 /* check if we're in the middle of a word */
583 if (Text
[i
] != '\n' && Text
[i
] != BreakChar
)
585 /* search for the beginning of the word */
586 while (i
&& Text
[i
- 1] != BreakChar
) i
--;
592 if (x
== SL_LEFTMARGIN
) i
= max( nFit
, 1 );
599 /***********************************************************************
601 * Renders the document in memory
603 static VOID
SYSLINK_Render (const SYSLINK_INFO
*infoPtr
, HDC hdc
, PRECT pRect
)
608 int x
, y
, LineHeight
;
612 szDoc
.cx
= szDoc
.cy
= 0;
615 rc
.right
-= SL_RIGHTMARGIN
;
616 rc
.bottom
-= SL_BOTTOMMARGIN
;
618 if(rc
.right
- SL_LEFTMARGIN
< 0)
620 if (rc
.bottom
- SL_TOPMARGIN
< 0)
623 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
627 GetTextMetricsW( hdc
, &tm
);
628 LineHeight
= tm
.tmHeight
+ tm
.tmExternalLeading
;
630 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
634 PDOC_TEXTBLOCK bl
, cbl
;
639 if(Current
->nText
== 0)
647 Free(Current
->Blocks
);
648 Current
->Blocks
= NULL
;
652 if(Current
->Type
== slText
)
654 SelectObject(hdc
, infoPtr
->Font
);
656 else if(Current
->Type
== slLink
)
658 SelectObject(hdc
, infoPtr
->LinkFont
);
663 /* skip break characters unless they're the first of the doc item */
664 if(tx
!= Current
->Text
|| x
== SL_LEFTMARGIN
)
666 if (n
&& *tx
== '\n')
672 while(n
> 0 && (*tx
) == infoPtr
->BreakChar
)
680 if((n
== 0 && SkipChars
!= 0) ||
681 GetTextExtentExPointW(hdc
, tx
, n
, rc
.right
- x
, &nFit
, NULL
, &szDim
))
689 Wrap
= SYSLINK_WrapLine(tx
, infoPtr
->BreakChar
, x
, &LineLen
, nFit
, &szDim
);
693 /* move one line down, the word didn't fit into the line */
701 if(!GetTextExtentExPointW(hdc
, tx
, LineLen
, rc
.right
- x
, NULL
, NULL
, &szDim
))
714 nbl
= ReAlloc(bl
, (nBlocks
+ 1) * sizeof(DOC_TEXTBLOCK
));
720 cbl
= bl
+ nBlocks
- 1;
722 cbl
->nChars
= LineLen
;
723 cbl
->nSkip
= SkipChars
;
724 SetRect(&cbl
->rc
, x
, y
, x
+ szDim
.cx
, y
+ szDim
.cy
);
726 if (cbl
->rc
.right
> szDoc
.cx
)
727 szDoc
.cx
= cbl
->rc
.right
;
728 if (cbl
->rc
.bottom
> szDoc
.cy
)
729 szDoc
.cy
= cbl
->rc
.bottom
;
747 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
762 Current
->Blocks
= bl
;
766 SelectObject(hdc
, hOldFont
);
768 pRect
->right
= pRect
->left
+ szDoc
.cx
;
769 pRect
->bottom
= pRect
->top
+ szDoc
.cy
;
772 /***********************************************************************
774 * Draws the SysLink control.
776 static LRESULT
SYSLINK_Draw (const SYSLINK_INFO
*infoPtr
, HDC hdc
)
781 COLORREF OldTextColor
, OldBkColor
;
783 UINT text_flags
= ETO_CLIPPED
;
784 UINT mode
= GetBkMode( hdc
);
786 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
787 OldTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
788 OldBkColor
= SetBkColor(hdc
, comctl32_color
.clrWindow
);
790 GetClientRect(infoPtr
->Self
, &rc
);
791 rc
.right
-= SL_RIGHTMARGIN
+ SL_LEFTMARGIN
;
792 rc
.bottom
-= SL_BOTTOMMARGIN
+ SL_TOPMARGIN
;
794 if(rc
.right
< 0 || rc
.bottom
< 0) return 0;
796 hBrush
= (HBRUSH
)SendMessageW(infoPtr
->Notify
, WM_CTLCOLORSTATIC
,
797 (WPARAM
)hdc
, (LPARAM
)infoPtr
->Self
);
798 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
800 FillRect(hdc
, &rc
, hBrush
);
801 if (GetBkMode( hdc
) == OPAQUE
) text_flags
|= ETO_OPAQUE
;
803 else SetBkMode( hdc
, TRANSPARENT
);
805 DeleteObject(hBrush
);
807 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
813 bl
= Current
->Blocks
;
819 if(Current
->Type
== slText
)
821 SelectObject(hdc
, infoPtr
->Font
);
822 SetTextColor(hdc
, infoPtr
->TextColor
);
826 SelectObject(hdc
, infoPtr
->LinkFont
);
827 SetTextColor(hdc
, (!(Current
->u
.Link
.state
& LIS_VISITED
) ? infoPtr
->LinkColor
: infoPtr
->VisitedColor
));
833 ExtTextOutW(hdc
, bl
->rc
.left
, bl
->rc
.top
, text_flags
, &bl
->rc
, tx
, bl
->nChars
, NULL
);
834 if((Current
->Type
== slLink
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && infoPtr
->HasFocus
)
836 COLORREF PrevTextColor
;
837 PrevTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
838 DrawFocusRect(hdc
, &bl
->rc
);
839 SetTextColor(hdc
, PrevTextColor
);
842 n
-= bl
->nChars
+ bl
->nSkip
;
848 SetBkColor(hdc
, OldBkColor
);
849 SetTextColor(hdc
, OldTextColor
);
850 SelectObject(hdc
, hOldFont
);
851 SetBkMode(hdc
, mode
);
856 /***********************************************************************
858 * Handles the WM_PAINT message.
860 static LRESULT
SYSLINK_Paint (const SYSLINK_INFO
*infoPtr
, HDC hdcParam
)
865 hdc
= hdcParam
? hdcParam
: BeginPaint (infoPtr
->Self
, &ps
);
868 SYSLINK_Draw (infoPtr
, hdc
);
869 if (!hdcParam
) EndPaint (infoPtr
->Self
, &ps
);
874 /***********************************************************************
876 * Set new Font for the SysLink control.
878 static HFONT
SYSLINK_SetFont (SYSLINK_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
884 HFONT hOldFont
= infoPtr
->Font
;
885 infoPtr
->Font
= hFont
;
887 /* free the underline font */
888 if(infoPtr
->LinkFont
!= NULL
)
890 DeleteObject(infoPtr
->LinkFont
);
891 infoPtr
->LinkFont
= NULL
;
894 /* Render text position and word wrapping in memory */
895 if (GetClientRect(infoPtr
->Self
, &rcClient
))
897 hdc
= GetDC(infoPtr
->Self
);
900 /* create a new underline font */
901 if(GetTextMetricsW(hdc
, &tm
) &&
902 GetObjectW(infoPtr
->Font
, sizeof(LOGFONTW
), &lf
))
904 lf
.lfUnderline
= TRUE
;
905 infoPtr
->LinkFont
= CreateFontIndirectW(&lf
);
906 infoPtr
->BreakChar
= tm
.tmBreakChar
;
910 ERR("Failed to create link font!\n");
913 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
914 ReleaseDC(infoPtr
->Self
, hdc
);
920 RedrawWindow(infoPtr
->Self
, NULL
, NULL
, RDW_INVALIDATE
| RDW_UPDATENOW
);
926 /***********************************************************************
928 * Set new text for the SysLink control.
930 static LRESULT
SYSLINK_SetText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
932 /* clear the document */
933 SYSLINK_ClearDoc(infoPtr
);
935 if(Text
== NULL
|| *Text
== 0)
940 /* let's parse the string and create a document */
941 if(SYSLINK_ParseText(infoPtr
, Text
) > 0)
945 /* Render text position and word wrapping in memory */
946 if (GetClientRect(infoPtr
->Self
, &rcClient
))
948 HDC hdc
= GetDC(infoPtr
->Self
);
951 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
952 ReleaseDC(infoPtr
->Self
, hdc
);
954 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
962 /***********************************************************************
963 * SYSLINK_SetFocusLink
964 * Updates the focus status bits and focusses the specified link.
965 * If no document item is specified, the focus bit will be removed from all links.
966 * Returns the previous focused item.
968 static PDOC_ITEM
SYSLINK_SetFocusLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
970 PDOC_ITEM Current
, PrevFocus
= NULL
;
972 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
974 if(Current
->Type
== slLink
)
976 if((PrevFocus
== NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
))
981 if(Current
== DocItem
)
983 Current
->u
.Link
.state
|= LIS_FOCUSED
;
987 Current
->u
.Link
.state
&= ~LIS_FOCUSED
;
995 /***********************************************************************
997 * Sets the states and attributes of a link item.
999 static LRESULT
SYSLINK_SetItem (const SYSLINK_INFO
*infoPtr
, const LITEM
*Item
)
1005 BOOL Repaint
= FALSE
;
1007 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1009 ERR("Invalid Flags!\n");
1013 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1016 ERR("Link %d couldn't be found\n", Item
->iLink
);
1020 if(Item
->mask
& LIF_ITEMID
)
1022 nc
= min(lstrlenW(Item
->szID
), MAX_LINKID_TEXT
- 1);
1023 szId
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1026 lstrcpynW(szId
, Item
->szID
, nc
+ 1);
1030 ERR("Unable to allocate memory for link id\n");
1035 if(Item
->mask
& LIF_URL
)
1037 nc
= min(lstrlenW(Item
->szUrl
), L_MAX_URL_LENGTH
- 1);
1038 szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1041 lstrcpynW(szUrl
, Item
->szUrl
, nc
+ 1);
1047 ERR("Unable to allocate memory for link url\n");
1052 if(Item
->mask
& LIF_ITEMID
)
1054 Free(di
->u
.Link
.szID
);
1055 di
->u
.Link
.szID
= szId
;
1058 if(Item
->mask
& LIF_URL
)
1060 Free(di
->u
.Link
.szUrl
);
1061 di
->u
.Link
.szUrl
= szUrl
;
1064 if(Item
->mask
& LIF_STATE
)
1066 UINT oldstate
= di
->u
.Link
.state
;
1067 /* clear the masked bits */
1068 di
->u
.Link
.state
&= ~(Item
->stateMask
& LIS_MASK
);
1070 di
->u
.Link
.state
|= (Item
->state
& Item
->stateMask
) & LIS_MASK
;
1071 Repaint
= (oldstate
!= di
->u
.Link
.state
);
1073 /* update the focus */
1074 SYSLINK_SetFocusLink(infoPtr
, ((di
->u
.Link
.state
& LIS_FOCUSED
) ? di
: NULL
));
1079 SYSLINK_RepaintLink(infoPtr
, di
);
1085 /***********************************************************************
1087 * Retrieves the states and attributes of a link item.
1089 static LRESULT
SYSLINK_GetItem (const SYSLINK_INFO
*infoPtr
, PLITEM Item
)
1093 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1095 ERR("Invalid Flags!\n");
1099 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1102 ERR("Link %d couldn't be found\n", Item
->iLink
);
1106 if(Item
->mask
& LIF_STATE
)
1108 Item
->state
= (di
->u
.Link
.state
& Item
->stateMask
);
1109 if(!infoPtr
->HasFocus
)
1111 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1112 Item
->state
&= ~LIS_FOCUSED
;
1116 if(Item
->mask
& LIF_ITEMID
)
1120 lstrcpyW(Item
->szID
, di
->u
.Link
.szID
);
1128 if(Item
->mask
& LIF_URL
)
1130 if(di
->u
.Link
.szUrl
)
1132 lstrcpyW(Item
->szUrl
, di
->u
.Link
.szUrl
);
1143 /***********************************************************************
1144 * SYSLINK_PtInDocItem
1145 * Determines if a point is in the region of a document item
1147 static BOOL
SYSLINK_PtInDocItem (const DOC_ITEM
*DocItem
, POINT pt
)
1152 bl
= DocItem
->Blocks
;
1159 if (PtInRect(&bl
->rc
, pt
))
1163 n
-= bl
->nChars
+ bl
->nSkip
;
1171 /***********************************************************************
1173 * Determines the link the user clicked on.
1175 static LRESULT
SYSLINK_HitTest (const SYSLINK_INFO
*infoPtr
, PLHITTESTINFO HitTest
)
1180 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
1182 if(Current
->Type
== slLink
)
1184 if(SYSLINK_PtInDocItem(Current
, HitTest
->pt
))
1186 HitTest
->item
.mask
= 0;
1187 HitTest
->item
.iLink
= id
;
1188 HitTest
->item
.state
= 0;
1189 HitTest
->item
.stateMask
= 0;
1190 if(Current
->u
.Link
.szID
)
1192 lstrcpyW(HitTest
->item
.szID
, Current
->u
.Link
.szID
);
1196 HitTest
->item
.szID
[0] = 0;
1198 if(Current
->u
.Link
.szUrl
)
1200 lstrcpyW(HitTest
->item
.szUrl
, Current
->u
.Link
.szUrl
);
1204 HitTest
->item
.szUrl
[0] = 0;
1215 /***********************************************************************
1216 * SYSLINK_GetIdealHeight
1217 * Returns the preferred height of a link at the current control's width.
1219 static LRESULT
SYSLINK_GetIdealHeight (const SYSLINK_INFO
*infoPtr
)
1221 HDC hdc
= GetDC(infoPtr
->Self
);
1226 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1228 if(GetTextMetricsW(hdc
, &tm
))
1230 height
= tm
.tmHeight
;
1236 SelectObject(hdc
, hOldFont
);
1237 ReleaseDC(infoPtr
->Self
, hdc
);
1244 /***********************************************************************
1245 * SYSLINK_SendParentNotify
1246 * Sends a WM_NOTIFY message to the parent window.
1248 static LRESULT
SYSLINK_SendParentNotify (const SYSLINK_INFO
*infoPtr
, UINT code
, const DOC_ITEM
*Link
, int iLink
)
1252 nml
.hdr
.hwndFrom
= infoPtr
->Self
;
1253 nml
.hdr
.idFrom
= GetWindowLongPtrW(infoPtr
->Self
, GWLP_ID
);
1254 nml
.hdr
.code
= code
;
1257 nml
.item
.iLink
= iLink
;
1259 nml
.item
.stateMask
= 0;
1260 if(Link
->u
.Link
.szID
)
1262 lstrcpyW(nml
.item
.szID
, Link
->u
.Link
.szID
);
1266 nml
.item
.szID
[0] = 0;
1268 if(Link
->u
.Link
.szUrl
)
1270 lstrcpyW(nml
.item
.szUrl
, Link
->u
.Link
.szUrl
);
1274 nml
.item
.szUrl
[0] = 0;
1277 return SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, nml
.hdr
.idFrom
, (LPARAM
)&nml
);
1280 /***********************************************************************
1282 * Handles receiving the input focus.
1284 static LRESULT
SYSLINK_SetFocus (SYSLINK_INFO
*infoPtr
)
1288 infoPtr
->HasFocus
= TRUE
;
1290 /* We always select the first link, even if we activated the control using
1291 SHIFT+TAB. This is the default behavior */
1292 Focus
= SYSLINK_GetNextLink(infoPtr
, NULL
);
1295 SYSLINK_SetFocusLink(infoPtr
, Focus
);
1296 SYSLINK_RepaintLink(infoPtr
, Focus
);
1301 /***********************************************************************
1303 * Handles losing the input focus.
1305 static LRESULT
SYSLINK_KillFocus (SYSLINK_INFO
*infoPtr
)
1309 infoPtr
->HasFocus
= FALSE
;
1310 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1314 SYSLINK_RepaintLink(infoPtr
, Focus
);
1320 /***********************************************************************
1322 * Returns a link at the specified position
1324 static PDOC_ITEM
SYSLINK_LinkAtPt (const SYSLINK_INFO
*infoPtr
, const POINT
*pt
, int *LinkId
, BOOL MustBeEnabled
)
1329 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
1331 if((Current
->Type
== slLink
) && SYSLINK_PtInDocItem(Current
, *pt
) &&
1332 (!MustBeEnabled
|| (Current
->u
.Link
.state
& LIS_ENABLED
)))
1346 /***********************************************************************
1347 * SYSLINK_LButtonDown
1348 * Handles mouse clicks
1350 static LRESULT
SYSLINK_LButtonDown (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1352 PDOC_ITEM Current
, Old
;
1355 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1358 SetFocus(infoPtr
->Self
);
1360 Old
= SYSLINK_SetFocusLink(infoPtr
, Current
);
1361 if(Old
!= NULL
&& Old
!= Current
)
1363 SYSLINK_RepaintLink(infoPtr
, Old
);
1365 infoPtr
->MouseDownID
= id
;
1366 SYSLINK_RepaintLink(infoPtr
, Current
);
1372 /***********************************************************************
1374 * Handles mouse clicks
1376 static LRESULT
SYSLINK_LButtonUp (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1378 if(infoPtr
->MouseDownID
> -1)
1383 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1384 if((Current
!= NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && (infoPtr
->MouseDownID
== id
))
1386 SYSLINK_SendParentNotify(infoPtr
, NM_CLICK
, Current
, id
);
1390 infoPtr
->MouseDownID
= -1;
1395 /***********************************************************************
1397 * Handles ENTER key events
1399 static BOOL
SYSLINK_OnEnter (const SYSLINK_INFO
*infoPtr
)
1401 if(infoPtr
->HasFocus
&& !infoPtr
->IgnoreReturn
)
1406 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1409 SYSLINK_SendParentNotify(infoPtr
, NM_RETURN
, Focus
, id
);
1416 /***********************************************************************
1417 * SYSKEY_SelectNextPrevLink
1418 * Changes the currently focused link
1420 static BOOL
SYSKEY_SelectNextPrevLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1422 if(infoPtr
->HasFocus
)
1427 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1430 PDOC_ITEM NewFocus
, OldFocus
;
1433 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1435 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1437 if(NewFocus
!= NULL
)
1439 OldFocus
= SYSLINK_SetFocusLink(infoPtr
, NewFocus
);
1441 if(OldFocus
&& OldFocus
!= NewFocus
)
1443 SYSLINK_RepaintLink(infoPtr
, OldFocus
);
1445 SYSLINK_RepaintLink(infoPtr
, NewFocus
);
1453 /***********************************************************************
1454 * SYSKEY_SelectNextPrevLink
1455 * Determines if there's a next or previous link to decide whether the control
1456 * should capture the tab key message
1458 static BOOL
SYSLINK_NoNextLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1460 PDOC_ITEM Focus
, NewFocus
;
1462 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1464 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1466 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1468 return NewFocus
== NULL
;
1471 /***********************************************************************
1472 * SYSLINK_GetIdealSize
1473 * Calculates the ideal size of a link control at a given maximum width.
1475 static VOID
SYSLINK_GetIdealSize (const SYSLINK_INFO
*infoPtr
, int cxMaxWidth
, LPSIZE lpSize
)
1480 rc
.left
= rc
.top
= rc
.bottom
= 0;
1481 rc
.right
= cxMaxWidth
;
1483 hdc
= GetDC(infoPtr
->Self
);
1486 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1488 SYSLINK_Render(infoPtr
, hdc
, &rc
);
1490 SelectObject(hdc
, hOldFont
);
1491 ReleaseDC(infoPtr
->Self
, hdc
);
1493 lpSize
->cx
= rc
.right
;
1494 lpSize
->cy
= rc
.bottom
;
1498 /***********************************************************************
1501 static LRESULT WINAPI
SysLinkWindowProc(HWND hwnd
, UINT message
,
1502 WPARAM wParam
, LPARAM lParam
)
1504 SYSLINK_INFO
*infoPtr
;
1506 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd
, message
, wParam
, lParam
);
1508 infoPtr
= (SYSLINK_INFO
*)GetWindowLongPtrW(hwnd
, 0);
1510 if (!infoPtr
&& message
!= WM_CREATE
)
1511 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1514 case WM_PRINTCLIENT
:
1516 return SYSLINK_Paint (infoPtr
, (HDC
)wParam
);
1519 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
1521 HDC hdc
= (HDC
)wParam
;
1522 HBRUSH brush
= CreateSolidBrush( comctl32_color
.clrWindow
);
1525 GetClipBox( hdc
, &rect
);
1526 FillRect( hdc
, &rect
, brush
);
1527 DeleteObject( brush
);
1535 DWORD mp
= GetMessagePos();
1537 ht
.pt
.x
= (short)LOWORD(mp
);
1538 ht
.pt
.y
= (short)HIWORD(mp
);
1540 ScreenToClient(infoPtr
->Self
, &ht
.pt
);
1541 if(SYSLINK_HitTest (infoPtr
, &ht
))
1543 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_HAND
));
1547 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1553 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1555 HDC hdc
= GetDC(infoPtr
->Self
);
1558 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1559 ReleaseDC(infoPtr
->Self
, hdc
);
1566 return (LRESULT
)infoPtr
->Font
;
1569 return (LRESULT
)SYSLINK_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
1572 SYSLINK_SetText(infoPtr
, (LPWSTR
)lParam
);
1573 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1575 case WM_LBUTTONDOWN
:
1578 pt
.x
= (short)LOWORD(lParam
);
1579 pt
.y
= (short)HIWORD(lParam
);
1580 return SYSLINK_LButtonDown(infoPtr
, &pt
);
1585 pt
.x
= (short)LOWORD(lParam
);
1586 pt
.y
= (short)HIWORD(lParam
);
1587 return SYSLINK_LButtonUp(infoPtr
, &pt
);
1595 SYSLINK_OnEnter(infoPtr
);
1599 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1600 SYSKEY_SelectNextPrevLink(infoPtr
, shift
);
1604 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1610 LRESULT Ret
= DLGC_HASSETSEL
;
1611 int vk
= (lParam
!= 0 ? (int)((LPMSG
)lParam
)->wParam
: 0);
1615 Ret
|= DLGC_WANTMESSAGE
;
1619 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1620 if(!SYSLINK_NoNextLink(infoPtr
, shift
))
1622 Ret
|= DLGC_WANTTAB
;
1626 Ret
|= DLGC_WANTCHARS
;
1638 pt
.x
= (short)LOWORD(lParam
);
1639 pt
.y
= (short)HIWORD(lParam
);
1641 GetClientRect(infoPtr
->Self
, &rc
);
1642 ScreenToClient(infoPtr
->Self
, &pt
);
1643 if(pt
.x
< 0 || pt
.y
< 0 || pt
.x
> rc
.right
|| pt
.y
> rc
.bottom
)
1648 if(SYSLINK_LinkAtPt(infoPtr
, &pt
, NULL
, FALSE
))
1653 return HTTRANSPARENT
;
1657 return SYSLINK_HitTest(infoPtr
, (PLHITTESTINFO
)lParam
);
1660 return SYSLINK_SetItem(infoPtr
, (PLITEM
)lParam
);
1663 return SYSLINK_GetItem(infoPtr
, (PLITEM
)lParam
);
1665 case LM_GETIDEALHEIGHT
:
1668 /* LM_GETIDEALSIZE */
1669 SYSLINK_GetIdealSize(infoPtr
, (int)wParam
, (LPSIZE
)lParam
);
1671 return SYSLINK_GetIdealHeight(infoPtr
);
1674 return SYSLINK_SetFocus(infoPtr
);
1677 return SYSLINK_KillFocus(infoPtr
);
1680 infoPtr
->Style
&= ~WS_DISABLED
;
1681 infoPtr
->Style
|= (wParam
? 0 : WS_DISABLED
);
1682 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
1685 case WM_STYLECHANGED
:
1686 if (wParam
== GWL_STYLE
)
1688 infoPtr
->Style
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
1690 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1696 CREATESTRUCTW
*cs
= (CREATESTRUCTW
*)lParam
;
1698 /* allocate memory for info struct */
1699 infoPtr
= Alloc (sizeof(SYSLINK_INFO
));
1700 if (!infoPtr
) return -1;
1701 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
1703 /* initialize the info struct */
1704 infoPtr
->Self
= hwnd
;
1705 infoPtr
->Notify
= cs
->hwndParent
;
1706 infoPtr
->Style
= cs
->style
;
1708 infoPtr
->LinkFont
= 0;
1709 list_init(&infoPtr
->Items
);
1710 infoPtr
->HasFocus
= FALSE
;
1711 infoPtr
->MouseDownID
= -1;
1712 infoPtr
->TextColor
= comctl32_color
.clrWindowText
;
1713 infoPtr
->LinkColor
= comctl32_color
.clrHighlight
;
1714 infoPtr
->VisitedColor
= comctl32_color
.clrHighlight
;
1715 infoPtr
->BreakChar
= ' ';
1716 infoPtr
->IgnoreReturn
= infoPtr
->Style
& LWS_IGNORERETURN
;
1717 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd
);
1718 SYSLINK_SetText(infoPtr
, cs
->lpszName
);
1722 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd
);
1723 SYSLINK_ClearDoc(infoPtr
);
1724 if(infoPtr
->Font
!= 0) DeleteObject(infoPtr
->Font
);
1725 if(infoPtr
->LinkFont
!= 0) DeleteObject(infoPtr
->LinkFont
);
1726 SetWindowLongPtrW(hwnd
, 0, 0);
1730 case WM_SYSCOLORCHANGE
:
1731 COMCTL32_RefreshSysColors();
1735 if ((message
>= WM_USER
) && (message
< WM_APP
) && !COMCTL32_IsReflectedMessage(message
))
1737 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message
, wParam
, lParam
);
1739 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1744 /***********************************************************************
1745 * SYSLINK_Register [Internal]
1747 * Registers the SysLink window class.
1749 VOID
SYSLINK_Register (void)
1753 ZeroMemory (&wndClass
, sizeof(wndClass
));
1754 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
1755 wndClass
.lpfnWndProc
= SysLinkWindowProc
;
1756 wndClass
.cbClsExtra
= 0;
1757 wndClass
.cbWndExtra
= sizeof (SYSLINK_INFO
*);
1758 wndClass
.hCursor
= LoadCursorW (0, (LPWSTR
)IDC_ARROW
);
1759 wndClass
.lpszClassName
= WC_LINK
;
1761 RegisterClassW (&wndClass
);
1765 /***********************************************************************
1766 * SYSLINK_Unregister [Internal]
1768 * Unregisters the SysLink window class.
1770 VOID
SYSLINK_Unregister (void)
1772 UnregisterClassW (WC_LINK
, NULL
);