6ba1a0b5b10a3c066dee07f67b0fb5ccfb61d5e6
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.
32 WINE_DEFAULT_DEBUG_CHANNEL(syslink
);
39 } DOC_TEXTBLOCK
, *PDOC_TEXTBLOCK
;
41 #define LIF_FLAGSMASK (LIF_STATE | LIF_ITEMID | LIF_URL)
42 #define LIS_MASK (LIS_FOCUSED | LIS_ENABLED | LIS_VISITED)
50 typedef struct _DOC_ITEM
53 UINT nText
; /* Number of characters of the text */
54 SL_ITEM_TYPE Type
; /* type of the item */
55 PDOC_TEXTBLOCK Blocks
; /* Array of text blocks */
60 UINT state
; /* Link state */
61 WCHAR
*szID
; /* Link ID string */
62 WCHAR
*szUrl
; /* Link URL string */
69 WCHAR Text
[1]; /* Text of the document item */
70 } DOC_ITEM
, *PDOC_ITEM
;
74 HWND Self
; /* The window handle for this control */
75 HWND Notify
; /* The parent handle to receive notifications */
76 DWORD Style
; /* Styles for this control */
77 struct list Items
; /* Document items list */
78 BOOL HasFocus
; /* Whether the control has the input focus */
79 int MouseDownID
; /* ID of the link that the mouse button first selected */
80 HFONT Font
; /* Handle to the font for text */
81 HFONT LinkFont
; /* Handle to the font for links */
82 COLORREF TextColor
; /* Color of the text */
83 COLORREF LinkColor
; /* Color of links */
84 COLORREF VisitedColor
; /* Color of visited links */
85 WCHAR BreakChar
; /* Break Character for the current font */
86 BOOL IgnoreReturn
; /* (infoPtr->Style & LWS_IGNORERETURN) on creation */
89 /* Control configuration constants */
91 #define SL_LEFTMARGIN (0)
92 #define SL_TOPMARGIN (0)
93 #define SL_RIGHTMARGIN (0)
94 #define SL_BOTTOMMARGIN (0)
96 /***********************************************************************
98 * Frees all data and gdi objects associated with a document item
100 static VOID
SYSLINK_FreeDocItem (PDOC_ITEM DocItem
)
102 if(DocItem
->Type
== slLink
)
104 Free(DocItem
->u
.Link
.szID
);
105 Free(DocItem
->u
.Link
.szUrl
);
108 Free(DocItem
->Blocks
);
110 /* we don't free Text because it's just a pointer to a character in the
111 entire window text string */
116 /***********************************************************************
117 * SYSLINK_AppendDocItem
118 * Create and append a new document item.
120 static PDOC_ITEM
SYSLINK_AppendDocItem (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
, UINT textlen
,
121 SL_ITEM_TYPE type
, PDOC_ITEM LastItem
)
125 textlen
= min(textlen
, strlenW(Text
));
126 Item
= Alloc(FIELD_OFFSET(DOC_ITEM
, Text
[textlen
+ 1]));
129 ERR("Failed to alloc DOC_ITEM structure!\n");
133 Item
->nText
= textlen
;
136 lstrcpynW(Item
->Text
, Text
, textlen
+ 1);
138 list_add_after(&LastItem
->entry
, &Item
->entry
);
140 list_add_tail(&infoPtr
->Items
, &Item
->entry
);
145 /***********************************************************************
147 * Clears the document tree
149 static VOID
SYSLINK_ClearDoc (SYSLINK_INFO
*infoPtr
)
151 DOC_ITEM
*Item
, *Item2
;
153 LIST_FOR_EACH_ENTRY_SAFE(Item
, Item2
, &infoPtr
->Items
, DOC_ITEM
, entry
)
155 list_remove(&Item
->entry
);
156 SYSLINK_FreeDocItem(Item
);
160 /***********************************************************************
162 * Parses the window text string and creates a document. Returns the
163 * number of document items created.
165 static UINT
SYSLINK_ParseText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
167 static const WCHAR SL_LINKOPEN
[] = { '<','a' };
168 static const WCHAR SL_HREF
[] = { 'h','r','e','f','=','\"' };
169 static const WCHAR SL_ID
[] = { 'i','d','=','\"' };
170 static const WCHAR SL_LINKCLOSE
[] = { '<','/','a','>' };
171 LPCWSTR current
, textstart
= NULL
, linktext
= NULL
, firsttag
= NULL
;
172 int taglen
= 0, textlen
= 0, linklen
= 0, docitems
= 0;
173 PDOC_ITEM Last
= NULL
;
174 SL_ITEM_TYPE CurrentType
= slText
;
178 TRACE("(%p %s)\n", infoPtr
, debugstr_w(Text
));
180 for(current
= Text
; *current
!= 0;)
184 if(!strncmpiW(current
, SL_LINKOPEN
, sizeof(SL_LINKOPEN
)/sizeof(SL_LINKOPEN
[0])) && (CurrentType
== slText
))
186 BOOL ValidParam
= FALSE
, ValidLink
= FALSE
;
188 if(*(current
+ 2) == '>')
190 /* we just have to deal with a <a> tag */
199 else if(*(current
+ 2) == infoPtr
->BreakChar
)
201 /* we expect parameters, parse them */
202 LPCWSTR
*CurrentParameter
= NULL
, tmp
;
203 UINT
*CurrentParameterLen
= NULL
;
206 tmp
= current
+ taglen
;
211 /* compare the current position with all known parameters */
212 if(!strncmpiW(tmp
, SL_HREF
, sizeof(SL_HREF
)/sizeof(SL_HREF
[0])))
216 CurrentParameter
= &lpUrl
;
217 CurrentParameterLen
= &lenUrl
;
219 else if(!strncmpiW(tmp
, SL_ID
, sizeof(SL_ID
)/sizeof(SL_ID
[0])))
223 CurrentParameter
= &lpID
;
224 CurrentParameterLen
= &lenId
;
233 /* we got a known parameter, now search until the next " character.
234 If we can't find a " character, there's a syntax error and we just assume it's text */
236 *CurrentParameter
= current
+ taglen
;
237 *CurrentParameterLen
= 0;
239 for(tmp
= *CurrentParameter
; *tmp
!= 0; tmp
++)
248 (*CurrentParameterLen
)++;
253 /* we're done with this parameter, now there are only 2 possibilities:
254 * 1. another parameter is coming, so expect a ' ' (space) character
255 * 2. the tag is being closed, so expect a '<' character
257 if(*tmp
== infoPtr
->BreakChar
)
259 /* we expect another parameter, do the whole thing again */
266 /* the tag is being closed, we're done */
273 if(ValidLink
&& ValidParam
)
275 /* the <a ...> tag appears to be valid. save all information
276 so we can add the link if we find a valid </a> tag later */
277 CurrentType
= slLink
;
278 linktext
= current
+ taglen
;
287 if(textstart
== NULL
)
293 else if(!strncmpiW(current
, SL_LINKCLOSE
, sizeof(SL_LINKCLOSE
)/sizeof(SL_LINKCLOSE
[0])) && (CurrentType
== slLink
) && firsttag
)
295 /* there's a <a...> tag opened, first add the previous text, if present */
296 if(textstart
!= NULL
&& textlen
> 0 && firsttag
> textstart
)
298 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, firsttag
- textstart
, slText
, Last
);
301 ERR("Unable to create new document item!\n");
309 /* now it's time to add the link to the document */
311 if(linktext
!= NULL
&& linklen
> 0)
313 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slLink
, Last
);
316 ERR("Unable to create new document item!\n");
320 if(CurrentType
== slLink
)
324 if(!(infoPtr
->Style
& WS_DISABLED
))
326 Last
->u
.Link
.state
|= LIS_ENABLED
;
328 /* Copy the tag parameters */
331 nc
= min(lenId
, strlenW(lpID
));
332 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
333 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
334 if(Last
->u
.Link
.szID
!= NULL
)
336 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
340 Last
->u
.Link
.szID
= NULL
;
343 nc
= min(lenUrl
, strlenW(lpUrl
));
344 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
345 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
346 if(Last
->u
.Link
.szUrl
!= NULL
)
348 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
352 Last
->u
.Link
.szUrl
= NULL
;
356 CurrentType
= slText
;
363 /* we don't know what tag it is, so just continue */
366 if(CurrentType
== slText
&& textstart
== NULL
)
380 /* save the pointer of the current text item if we couldn't find a tag */
381 if(textstart
== NULL
&& CurrentType
== slText
)
390 if(textstart
!= NULL
&& textlen
> 0)
392 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, textlen
, CurrentType
, Last
);
395 ERR("Unable to create new document item!\n");
398 if(CurrentType
== slLink
)
402 if(!(infoPtr
->Style
& WS_DISABLED
))
404 Last
->u
.Link
.state
|= LIS_ENABLED
;
406 /* Copy the tag parameters */
409 nc
= min(lenId
, strlenW(lpID
));
410 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
411 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
412 if(Last
->u
.Link
.szID
!= NULL
)
414 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
418 Last
->u
.Link
.szID
= NULL
;
421 nc
= min(lenUrl
, strlenW(lpUrl
));
422 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
423 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
424 if(Last
->u
.Link
.szUrl
!= NULL
)
426 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
430 Last
->u
.Link
.szUrl
= NULL
;
435 if(linktext
!= NULL
&& linklen
> 0)
437 /* we got an unclosed link, just display the text */
438 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slText
, Last
);
441 ERR("Unable to create new document item!\n");
450 /***********************************************************************
451 * SYSLINK_RepaintLink
454 static VOID
SYSLINK_RepaintLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
459 if(DocItem
->Type
!= slLink
)
461 ERR("DocItem not a link!\n");
465 bl
= DocItem
->Blocks
;
472 InvalidateRect(infoPtr
->Self
, &bl
->rc
, TRUE
);
473 n
-= bl
->nChars
+ bl
->nSkip
;
479 /***********************************************************************
480 * SYSLINK_GetLinkItemByIndex
481 * Retrieves a document link by its index
483 static PDOC_ITEM
SYSLINK_GetLinkItemByIndex (const SYSLINK_INFO
*infoPtr
, int iLink
)
487 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
489 if ((Current
->Type
== slLink
) && (iLink
-- <= 0))
495 /***********************************************************************
496 * SYSLINK_GetFocusLink
497 * Retrieves the link that has the LIS_FOCUSED bit
499 static PDOC_ITEM
SYSLINK_GetFocusLink (const SYSLINK_INFO
*infoPtr
, int *LinkId
)
504 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
506 if(Current
->Type
== slLink
)
508 if(Current
->u
.Link
.state
& LIS_FOCUSED
)
521 /***********************************************************************
522 * SYSLINK_GetNextLink
525 static PDOC_ITEM
SYSLINK_GetNextLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
529 LIST_FOR_EACH_ENTRY(Next
, Current
? &Current
->entry
: &infoPtr
->Items
, DOC_ITEM
, entry
)
531 if (Next
->Type
== slLink
)
539 /***********************************************************************
540 * SYSLINK_GetPrevLink
541 * Gets the previous link
543 static PDOC_ITEM
SYSLINK_GetPrevLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
547 LIST_FOR_EACH_ENTRY_REV(Prev
, Current
? &Current
->entry
: list_tail(&infoPtr
->Items
), DOC_ITEM
, entry
)
549 if (Prev
->Type
== slLink
)
558 /***********************************************************************
560 * Tries to wrap a line.
562 static BOOL
SYSLINK_WrapLine (LPWSTR Text
, WCHAR BreakChar
, int x
, int *LineLen
,
563 int nFit
, LPSIZE Extent
)
567 for (i
= 0; i
< nFit
; i
++) if (Text
[i
] == '\n') break;
569 if (i
== *LineLen
) return FALSE
;
571 /* check if we're in the middle of a word */
572 if (Text
[i
] != '\n' && Text
[i
] != BreakChar
)
574 /* search for the beginning of the word */
575 while (i
&& Text
[i
- 1] != BreakChar
) i
--;
581 if (x
== SL_LEFTMARGIN
) i
= max( nFit
, 1 );
588 /***********************************************************************
590 * Renders the document in memory
592 static VOID
SYSLINK_Render (const SYSLINK_INFO
*infoPtr
, HDC hdc
, PRECT pRect
)
597 int x
, y
, LineHeight
;
601 szDoc
.cx
= szDoc
.cy
= 0;
604 rc
.right
-= SL_RIGHTMARGIN
;
605 rc
.bottom
-= SL_BOTTOMMARGIN
;
607 if(rc
.right
- SL_LEFTMARGIN
< 0)
609 if (rc
.bottom
- SL_TOPMARGIN
< 0)
612 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
616 GetTextMetricsW( hdc
, &tm
);
617 LineHeight
= tm
.tmHeight
+ tm
.tmExternalLeading
;
619 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
623 PDOC_TEXTBLOCK bl
, cbl
;
628 if(Current
->nText
== 0)
636 Free(Current
->Blocks
);
637 Current
->Blocks
= NULL
;
641 if(Current
->Type
== slText
)
643 SelectObject(hdc
, infoPtr
->Font
);
645 else if(Current
->Type
== slLink
)
647 SelectObject(hdc
, infoPtr
->LinkFont
);
652 /* skip break characters unless they're the first of the doc item */
653 if(tx
!= Current
->Text
|| x
== SL_LEFTMARGIN
)
655 if (n
&& *tx
== '\n')
661 while(n
> 0 && (*tx
) == infoPtr
->BreakChar
)
669 if((n
== 0 && SkipChars
!= 0) ||
670 GetTextExtentExPointW(hdc
, tx
, n
, rc
.right
- x
, &nFit
, NULL
, &szDim
))
678 Wrap
= SYSLINK_WrapLine(tx
, infoPtr
->BreakChar
, x
, &LineLen
, nFit
, &szDim
);
682 /* move one line down, the word didn't fit into the line */
690 if(!GetTextExtentExPointW(hdc
, tx
, LineLen
, rc
.right
- x
, NULL
, NULL
, &szDim
))
703 nbl
= ReAlloc(bl
, (nBlocks
+ 1) * sizeof(DOC_TEXTBLOCK
));
709 cbl
= bl
+ nBlocks
- 1;
711 cbl
->nChars
= LineLen
;
712 cbl
->nSkip
= SkipChars
;
713 SetRect(&cbl
->rc
, x
, y
, x
+ szDim
.cx
, y
+ szDim
.cy
);
715 if (cbl
->rc
.right
> szDoc
.cx
)
716 szDoc
.cx
= cbl
->rc
.right
;
717 if (cbl
->rc
.bottom
> szDoc
.cy
)
718 szDoc
.cy
= cbl
->rc
.bottom
;
736 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
751 Current
->Blocks
= bl
;
755 SelectObject(hdc
, hOldFont
);
757 pRect
->right
= pRect
->left
+ szDoc
.cx
;
758 pRect
->bottom
= pRect
->top
+ szDoc
.cy
;
761 /***********************************************************************
763 * Draws the SysLink control.
765 static LRESULT
SYSLINK_Draw (const SYSLINK_INFO
*infoPtr
, HDC hdc
)
770 COLORREF OldTextColor
, OldBkColor
;
772 UINT text_flags
= ETO_CLIPPED
;
773 UINT mode
= GetBkMode( hdc
);
775 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
776 OldTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
777 OldBkColor
= SetBkColor(hdc
, comctl32_color
.clrWindow
);
779 GetClientRect(infoPtr
->Self
, &rc
);
780 rc
.right
-= SL_RIGHTMARGIN
+ SL_LEFTMARGIN
;
781 rc
.bottom
-= SL_BOTTOMMARGIN
+ SL_TOPMARGIN
;
783 if(rc
.right
< 0 || rc
.bottom
< 0) return 0;
785 hBrush
= (HBRUSH
)SendMessageW(infoPtr
->Notify
, WM_CTLCOLORSTATIC
,
786 (WPARAM
)hdc
, (LPARAM
)infoPtr
->Self
);
787 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
789 FillRect(hdc
, &rc
, hBrush
);
790 if (GetBkMode( hdc
) == OPAQUE
) text_flags
|= ETO_OPAQUE
;
792 else SetBkMode( hdc
, TRANSPARENT
);
794 DeleteObject(hBrush
);
796 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
802 bl
= Current
->Blocks
;
808 if(Current
->Type
== slText
)
810 SelectObject(hdc
, infoPtr
->Font
);
811 SetTextColor(hdc
, infoPtr
->TextColor
);
815 SelectObject(hdc
, infoPtr
->LinkFont
);
816 SetTextColor(hdc
, (!(Current
->u
.Link
.state
& LIS_VISITED
) ? infoPtr
->LinkColor
: infoPtr
->VisitedColor
));
822 ExtTextOutW(hdc
, bl
->rc
.left
, bl
->rc
.top
, text_flags
, &bl
->rc
, tx
, bl
->nChars
, NULL
);
823 if((Current
->Type
== slLink
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && infoPtr
->HasFocus
)
825 COLORREF PrevTextColor
;
826 PrevTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
827 DrawFocusRect(hdc
, &bl
->rc
);
828 SetTextColor(hdc
, PrevTextColor
);
831 n
-= bl
->nChars
+ bl
->nSkip
;
837 SetBkColor(hdc
, OldBkColor
);
838 SetTextColor(hdc
, OldTextColor
);
839 SelectObject(hdc
, hOldFont
);
840 SetBkMode(hdc
, mode
);
845 /***********************************************************************
847 * Handles the WM_PAINT message.
849 static LRESULT
SYSLINK_Paint (const SYSLINK_INFO
*infoPtr
, HDC hdcParam
)
854 hdc
= hdcParam
? hdcParam
: BeginPaint (infoPtr
->Self
, &ps
);
857 SYSLINK_Draw (infoPtr
, hdc
);
858 if (!hdcParam
) EndPaint (infoPtr
->Self
, &ps
);
863 /***********************************************************************
865 * Set new Font for the SysLink control.
867 static HFONT
SYSLINK_SetFont (SYSLINK_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
873 HFONT hOldFont
= infoPtr
->Font
;
874 infoPtr
->Font
= hFont
;
876 /* free the underline font */
877 if(infoPtr
->LinkFont
!= NULL
)
879 DeleteObject(infoPtr
->LinkFont
);
880 infoPtr
->LinkFont
= NULL
;
883 /* Render text position and word wrapping in memory */
884 if (GetClientRect(infoPtr
->Self
, &rcClient
))
886 hdc
= GetDC(infoPtr
->Self
);
889 /* create a new underline font */
890 if(GetTextMetricsW(hdc
, &tm
) &&
891 GetObjectW(infoPtr
->Font
, sizeof(LOGFONTW
), &lf
))
893 lf
.lfUnderline
= TRUE
;
894 infoPtr
->LinkFont
= CreateFontIndirectW(&lf
);
895 infoPtr
->BreakChar
= tm
.tmBreakChar
;
899 ERR("Failed to create link font!\n");
902 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
903 ReleaseDC(infoPtr
->Self
, hdc
);
909 RedrawWindow(infoPtr
->Self
, NULL
, NULL
, RDW_INVALIDATE
| RDW_UPDATENOW
);
915 /***********************************************************************
917 * Set new text for the SysLink control.
919 static LRESULT
SYSLINK_SetText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
921 /* clear the document */
922 SYSLINK_ClearDoc(infoPtr
);
924 if(Text
== NULL
|| *Text
== 0)
929 /* let's parse the string and create a document */
930 if(SYSLINK_ParseText(infoPtr
, Text
) > 0)
934 /* Render text position and word wrapping in memory */
935 if (GetClientRect(infoPtr
->Self
, &rcClient
))
937 HDC hdc
= GetDC(infoPtr
->Self
);
940 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
941 ReleaseDC(infoPtr
->Self
, hdc
);
943 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
951 /***********************************************************************
952 * SYSLINK_SetFocusLink
953 * Updates the focus status bits and focusses the specified link.
954 * If no document item is specified, the focus bit will be removed from all links.
955 * Returns the previous focused item.
957 static PDOC_ITEM
SYSLINK_SetFocusLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
959 PDOC_ITEM Current
, PrevFocus
= NULL
;
961 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
963 if(Current
->Type
== slLink
)
965 if((PrevFocus
== NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
))
970 if(Current
== DocItem
)
972 Current
->u
.Link
.state
|= LIS_FOCUSED
;
976 Current
->u
.Link
.state
&= ~LIS_FOCUSED
;
984 /***********************************************************************
986 * Sets the states and attributes of a link item.
988 static LRESULT
SYSLINK_SetItem (const SYSLINK_INFO
*infoPtr
, const LITEM
*Item
)
994 BOOL Repaint
= FALSE
;
996 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
998 ERR("Invalid Flags!\n");
1002 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1005 ERR("Link %d couldn't be found\n", Item
->iLink
);
1009 if(Item
->mask
& LIF_ITEMID
)
1011 nc
= min(lstrlenW(Item
->szID
), MAX_LINKID_TEXT
- 1);
1012 szId
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1015 lstrcpynW(szId
, Item
->szID
, nc
+ 1);
1019 ERR("Unable to allocate memory for link id\n");
1024 if(Item
->mask
& LIF_URL
)
1026 nc
= min(lstrlenW(Item
->szUrl
), L_MAX_URL_LENGTH
- 1);
1027 szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1030 lstrcpynW(szUrl
, Item
->szUrl
, nc
+ 1);
1036 ERR("Unable to allocate memory for link url\n");
1041 if(Item
->mask
& LIF_ITEMID
)
1043 Free(di
->u
.Link
.szID
);
1044 di
->u
.Link
.szID
= szId
;
1047 if(Item
->mask
& LIF_URL
)
1049 Free(di
->u
.Link
.szUrl
);
1050 di
->u
.Link
.szUrl
= szUrl
;
1053 if(Item
->mask
& LIF_STATE
)
1055 UINT oldstate
= di
->u
.Link
.state
;
1056 /* clear the masked bits */
1057 di
->u
.Link
.state
&= ~(Item
->stateMask
& LIS_MASK
);
1059 di
->u
.Link
.state
|= (Item
->state
& Item
->stateMask
) & LIS_MASK
;
1060 Repaint
= (oldstate
!= di
->u
.Link
.state
);
1062 /* update the focus */
1063 SYSLINK_SetFocusLink(infoPtr
, ((di
->u
.Link
.state
& LIS_FOCUSED
) ? di
: NULL
));
1068 SYSLINK_RepaintLink(infoPtr
, di
);
1074 /***********************************************************************
1076 * Retrieves the states and attributes of a link item.
1078 static LRESULT
SYSLINK_GetItem (const SYSLINK_INFO
*infoPtr
, PLITEM Item
)
1082 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1084 ERR("Invalid Flags!\n");
1088 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1091 ERR("Link %d couldn't be found\n", Item
->iLink
);
1095 if(Item
->mask
& LIF_STATE
)
1097 Item
->state
= (di
->u
.Link
.state
& Item
->stateMask
);
1098 if(!infoPtr
->HasFocus
)
1100 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1101 Item
->state
&= ~LIS_FOCUSED
;
1105 if(Item
->mask
& LIF_ITEMID
)
1109 lstrcpyW(Item
->szID
, di
->u
.Link
.szID
);
1117 if(Item
->mask
& LIF_URL
)
1119 if(di
->u
.Link
.szUrl
)
1121 lstrcpyW(Item
->szUrl
, di
->u
.Link
.szUrl
);
1132 /***********************************************************************
1133 * SYSLINK_PtInDocItem
1134 * Determines if a point is in the region of a document item
1136 static BOOL
SYSLINK_PtInDocItem (const DOC_ITEM
*DocItem
, POINT pt
)
1141 bl
= DocItem
->Blocks
;
1148 if (PtInRect(&bl
->rc
, pt
))
1152 n
-= bl
->nChars
+ bl
->nSkip
;
1160 /***********************************************************************
1162 * Determines the link the user clicked on.
1164 static LRESULT
SYSLINK_HitTest (const SYSLINK_INFO
*infoPtr
, PLHITTESTINFO HitTest
)
1169 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
1171 if(Current
->Type
== slLink
)
1173 if(SYSLINK_PtInDocItem(Current
, HitTest
->pt
))
1175 HitTest
->item
.mask
= 0;
1176 HitTest
->item
.iLink
= id
;
1177 HitTest
->item
.state
= 0;
1178 HitTest
->item
.stateMask
= 0;
1179 if(Current
->u
.Link
.szID
)
1181 lstrcpyW(HitTest
->item
.szID
, Current
->u
.Link
.szID
);
1185 HitTest
->item
.szID
[0] = 0;
1187 if(Current
->u
.Link
.szUrl
)
1189 lstrcpyW(HitTest
->item
.szUrl
, Current
->u
.Link
.szUrl
);
1193 HitTest
->item
.szUrl
[0] = 0;
1204 /***********************************************************************
1205 * SYSLINK_GetIdealHeight
1206 * Returns the preferred height of a link at the current control's width.
1208 static LRESULT
SYSLINK_GetIdealHeight (const SYSLINK_INFO
*infoPtr
)
1210 HDC hdc
= GetDC(infoPtr
->Self
);
1215 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1217 if(GetTextMetricsW(hdc
, &tm
))
1219 height
= tm
.tmHeight
;
1225 SelectObject(hdc
, hOldFont
);
1226 ReleaseDC(infoPtr
->Self
, hdc
);
1233 /***********************************************************************
1234 * SYSLINK_SendParentNotify
1235 * Sends a WM_NOTIFY message to the parent window.
1237 static LRESULT
SYSLINK_SendParentNotify (const SYSLINK_INFO
*infoPtr
, UINT code
, const DOC_ITEM
*Link
, int iLink
)
1241 nml
.hdr
.hwndFrom
= infoPtr
->Self
;
1242 nml
.hdr
.idFrom
= GetWindowLongPtrW(infoPtr
->Self
, GWLP_ID
);
1243 nml
.hdr
.code
= code
;
1246 nml
.item
.iLink
= iLink
;
1248 nml
.item
.stateMask
= 0;
1249 if(Link
->u
.Link
.szID
)
1251 lstrcpyW(nml
.item
.szID
, Link
->u
.Link
.szID
);
1255 nml
.item
.szID
[0] = 0;
1257 if(Link
->u
.Link
.szUrl
)
1259 lstrcpyW(nml
.item
.szUrl
, Link
->u
.Link
.szUrl
);
1263 nml
.item
.szUrl
[0] = 0;
1266 return SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, nml
.hdr
.idFrom
, (LPARAM
)&nml
);
1269 /***********************************************************************
1271 * Handles receiving the input focus.
1273 static LRESULT
SYSLINK_SetFocus (SYSLINK_INFO
*infoPtr
)
1277 infoPtr
->HasFocus
= TRUE
;
1279 /* We always select the first link, even if we activated the control using
1280 SHIFT+TAB. This is the default behavior */
1281 Focus
= SYSLINK_GetNextLink(infoPtr
, NULL
);
1284 SYSLINK_SetFocusLink(infoPtr
, Focus
);
1285 SYSLINK_RepaintLink(infoPtr
, Focus
);
1290 /***********************************************************************
1292 * Handles losing the input focus.
1294 static LRESULT
SYSLINK_KillFocus (SYSLINK_INFO
*infoPtr
)
1298 infoPtr
->HasFocus
= FALSE
;
1299 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1303 SYSLINK_RepaintLink(infoPtr
, Focus
);
1309 /***********************************************************************
1311 * Returns a link at the specified position
1313 static PDOC_ITEM
SYSLINK_LinkAtPt (const SYSLINK_INFO
*infoPtr
, const POINT
*pt
, int *LinkId
, BOOL MustBeEnabled
)
1318 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
1320 if((Current
->Type
== slLink
) && SYSLINK_PtInDocItem(Current
, *pt
) &&
1321 (!MustBeEnabled
|| (Current
->u
.Link
.state
& LIS_ENABLED
)))
1335 /***********************************************************************
1336 * SYSLINK_LButtonDown
1337 * Handles mouse clicks
1339 static LRESULT
SYSLINK_LButtonDown (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1341 PDOC_ITEM Current
, Old
;
1344 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1347 SetFocus(infoPtr
->Self
);
1349 Old
= SYSLINK_SetFocusLink(infoPtr
, Current
);
1350 if(Old
!= NULL
&& Old
!= Current
)
1352 SYSLINK_RepaintLink(infoPtr
, Old
);
1354 infoPtr
->MouseDownID
= id
;
1355 SYSLINK_RepaintLink(infoPtr
, Current
);
1361 /***********************************************************************
1363 * Handles mouse clicks
1365 static LRESULT
SYSLINK_LButtonUp (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1367 if(infoPtr
->MouseDownID
> -1)
1372 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1373 if((Current
!= NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && (infoPtr
->MouseDownID
== id
))
1375 SYSLINK_SendParentNotify(infoPtr
, NM_CLICK
, Current
, id
);
1379 infoPtr
->MouseDownID
= -1;
1384 /***********************************************************************
1386 * Handles ENTER key events
1388 static BOOL
SYSLINK_OnEnter (const SYSLINK_INFO
*infoPtr
)
1390 if(infoPtr
->HasFocus
&& !infoPtr
->IgnoreReturn
)
1395 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1398 SYSLINK_SendParentNotify(infoPtr
, NM_RETURN
, Focus
, id
);
1405 /***********************************************************************
1406 * SYSKEY_SelectNextPrevLink
1407 * Changes the currently focused link
1409 static BOOL
SYSKEY_SelectNextPrevLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1411 if(infoPtr
->HasFocus
)
1416 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1419 PDOC_ITEM NewFocus
, OldFocus
;
1422 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1424 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1426 if(NewFocus
!= NULL
)
1428 OldFocus
= SYSLINK_SetFocusLink(infoPtr
, NewFocus
);
1430 if(OldFocus
&& OldFocus
!= NewFocus
)
1432 SYSLINK_RepaintLink(infoPtr
, OldFocus
);
1434 SYSLINK_RepaintLink(infoPtr
, NewFocus
);
1442 /***********************************************************************
1443 * SYSKEY_SelectNextPrevLink
1444 * Determines if there's a next or previous link to decide whether the control
1445 * should capture the tab key message
1447 static BOOL
SYSLINK_NoNextLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1449 PDOC_ITEM Focus
, NewFocus
;
1451 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1453 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1455 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1457 return NewFocus
== NULL
;
1460 /***********************************************************************
1461 * SYSLINK_GetIdealSize
1462 * Calculates the ideal size of a link control at a given maximum width.
1464 static VOID
SYSLINK_GetIdealSize (const SYSLINK_INFO
*infoPtr
, int cxMaxWidth
, LPSIZE lpSize
)
1469 rc
.left
= rc
.top
= rc
.bottom
= 0;
1470 rc
.right
= cxMaxWidth
;
1472 hdc
= GetDC(infoPtr
->Self
);
1475 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1477 SYSLINK_Render(infoPtr
, hdc
, &rc
);
1479 SelectObject(hdc
, hOldFont
);
1480 ReleaseDC(infoPtr
->Self
, hdc
);
1482 lpSize
->cx
= rc
.right
;
1483 lpSize
->cy
= rc
.bottom
;
1487 /***********************************************************************
1490 static LRESULT WINAPI
SysLinkWindowProc(HWND hwnd
, UINT message
,
1491 WPARAM wParam
, LPARAM lParam
)
1493 SYSLINK_INFO
*infoPtr
;
1495 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd
, message
, wParam
, lParam
);
1497 infoPtr
= (SYSLINK_INFO
*)GetWindowLongPtrW(hwnd
, 0);
1499 if (!infoPtr
&& message
!= WM_CREATE
)
1500 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1503 case WM_PRINTCLIENT
:
1505 return SYSLINK_Paint (infoPtr
, (HDC
)wParam
);
1508 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
1510 HDC hdc
= (HDC
)wParam
;
1511 HBRUSH brush
= CreateSolidBrush( comctl32_color
.clrWindow
);
1514 GetClipBox( hdc
, &rect
);
1515 FillRect( hdc
, &rect
, brush
);
1516 DeleteObject( brush
);
1524 DWORD mp
= GetMessagePos();
1526 ht
.pt
.x
= (short)LOWORD(mp
);
1527 ht
.pt
.y
= (short)HIWORD(mp
);
1529 ScreenToClient(infoPtr
->Self
, &ht
.pt
);
1530 if(SYSLINK_HitTest (infoPtr
, &ht
))
1532 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_HAND
));
1536 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1542 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1544 HDC hdc
= GetDC(infoPtr
->Self
);
1547 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1548 ReleaseDC(infoPtr
->Self
, hdc
);
1555 return (LRESULT
)infoPtr
->Font
;
1558 return (LRESULT
)SYSLINK_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
1561 SYSLINK_SetText(infoPtr
, (LPWSTR
)lParam
);
1562 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1564 case WM_LBUTTONDOWN
:
1567 pt
.x
= (short)LOWORD(lParam
);
1568 pt
.y
= (short)HIWORD(lParam
);
1569 return SYSLINK_LButtonDown(infoPtr
, &pt
);
1574 pt
.x
= (short)LOWORD(lParam
);
1575 pt
.y
= (short)HIWORD(lParam
);
1576 return SYSLINK_LButtonUp(infoPtr
, &pt
);
1584 SYSLINK_OnEnter(infoPtr
);
1588 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1589 SYSKEY_SelectNextPrevLink(infoPtr
, shift
);
1593 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1599 LRESULT Ret
= DLGC_HASSETSEL
;
1600 int vk
= (lParam
!= 0 ? (int)((LPMSG
)lParam
)->wParam
: 0);
1604 Ret
|= DLGC_WANTMESSAGE
;
1608 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1609 if(!SYSLINK_NoNextLink(infoPtr
, shift
))
1611 Ret
|= DLGC_WANTTAB
;
1615 Ret
|= DLGC_WANTCHARS
;
1627 pt
.x
= (short)LOWORD(lParam
);
1628 pt
.y
= (short)HIWORD(lParam
);
1630 GetClientRect(infoPtr
->Self
, &rc
);
1631 ScreenToClient(infoPtr
->Self
, &pt
);
1632 if(pt
.x
< 0 || pt
.y
< 0 || pt
.x
> rc
.right
|| pt
.y
> rc
.bottom
)
1637 if(SYSLINK_LinkAtPt(infoPtr
, &pt
, NULL
, FALSE
))
1642 return HTTRANSPARENT
;
1646 return SYSLINK_HitTest(infoPtr
, (PLHITTESTINFO
)lParam
);
1649 return SYSLINK_SetItem(infoPtr
, (PLITEM
)lParam
);
1652 return SYSLINK_GetItem(infoPtr
, (PLITEM
)lParam
);
1654 case LM_GETIDEALHEIGHT
:
1657 /* LM_GETIDEALSIZE */
1658 SYSLINK_GetIdealSize(infoPtr
, (int)wParam
, (LPSIZE
)lParam
);
1660 return SYSLINK_GetIdealHeight(infoPtr
);
1663 return SYSLINK_SetFocus(infoPtr
);
1666 return SYSLINK_KillFocus(infoPtr
);
1669 infoPtr
->Style
&= ~WS_DISABLED
;
1670 infoPtr
->Style
|= (wParam
? 0 : WS_DISABLED
);
1671 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
1674 case WM_STYLECHANGED
:
1675 if (wParam
== GWL_STYLE
)
1677 infoPtr
->Style
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
1679 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1685 CREATESTRUCTW
*cs
= (CREATESTRUCTW
*)lParam
;
1687 /* allocate memory for info struct */
1688 infoPtr
= Alloc (sizeof(SYSLINK_INFO
));
1689 if (!infoPtr
) return -1;
1690 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
1692 /* initialize the info struct */
1693 infoPtr
->Self
= hwnd
;
1694 infoPtr
->Notify
= cs
->hwndParent
;
1695 infoPtr
->Style
= cs
->style
;
1697 infoPtr
->LinkFont
= 0;
1698 list_init(&infoPtr
->Items
);
1699 infoPtr
->HasFocus
= FALSE
;
1700 infoPtr
->MouseDownID
= -1;
1701 infoPtr
->TextColor
= comctl32_color
.clrWindowText
;
1702 infoPtr
->LinkColor
= comctl32_color
.clrHighlight
;
1703 infoPtr
->VisitedColor
= comctl32_color
.clrHighlight
;
1704 infoPtr
->BreakChar
= ' ';
1705 infoPtr
->IgnoreReturn
= infoPtr
->Style
& LWS_IGNORERETURN
;
1706 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd
);
1707 SYSLINK_SetText(infoPtr
, cs
->lpszName
);
1711 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd
);
1712 SYSLINK_ClearDoc(infoPtr
);
1713 if(infoPtr
->Font
!= 0) DeleteObject(infoPtr
->Font
);
1714 if(infoPtr
->LinkFont
!= 0) DeleteObject(infoPtr
->LinkFont
);
1715 SetWindowLongPtrW(hwnd
, 0, 0);
1719 case WM_SYSCOLORCHANGE
:
1720 COMCTL32_RefreshSysColors();
1724 if ((message
>= WM_USER
) && (message
< WM_APP
) && !COMCTL32_IsReflectedMessage(message
))
1726 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message
, wParam
, lParam
);
1728 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1733 /***********************************************************************
1734 * SYSLINK_Register [Internal]
1736 * Registers the SysLink window class.
1738 VOID
SYSLINK_Register (void)
1742 ZeroMemory (&wndClass
, sizeof(wndClass
));
1743 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
1744 wndClass
.lpfnWndProc
= SysLinkWindowProc
;
1745 wndClass
.cbClsExtra
= 0;
1746 wndClass
.cbWndExtra
= sizeof (SYSLINK_INFO
*);
1747 wndClass
.hCursor
= LoadCursorW (0, (LPWSTR
)IDC_ARROW
);
1748 wndClass
.lpszClassName
= WC_LINK
;
1750 RegisterClassW (&wndClass
);
1754 /***********************************************************************
1755 * SYSLINK_Unregister [Internal]
1757 * Unregisters the SysLink window class.
1759 VOID
SYSLINK_Unregister (void)
1761 UnregisterClassW (WC_LINK
, NULL
);