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
);
45 INT WINAPI
StrCmpNIW(LPCWSTR
,LPCWSTR
,INT
);
52 } DOC_TEXTBLOCK
, *PDOC_TEXTBLOCK
;
54 #define LIF_FLAGSMASK (LIF_STATE | LIF_ITEMID | LIF_URL)
55 #define LIS_MASK (LIS_FOCUSED | LIS_ENABLED | LIS_VISITED)
63 typedef struct _DOC_ITEM
66 UINT nText
; /* Number of characters of the text */
67 SL_ITEM_TYPE Type
; /* type of the item */
68 PDOC_TEXTBLOCK Blocks
; /* Array of text blocks */
73 UINT state
; /* Link state */
74 WCHAR
*szID
; /* Link ID string */
75 WCHAR
*szUrl
; /* Link URL string */
82 WCHAR Text
[1]; /* Text of the document item */
83 } DOC_ITEM
, *PDOC_ITEM
;
87 HWND Self
; /* The window handle for this control */
88 HWND Notify
; /* The parent handle to receive notifications */
89 DWORD Style
; /* Styles for this control */
90 struct list Items
; /* Document items list */
91 BOOL HasFocus
; /* Whether the control has the input focus */
92 int MouseDownID
; /* ID of the link that the mouse button first selected */
93 HFONT Font
; /* Handle to the font for text */
94 HFONT LinkFont
; /* Handle to the font for links */
95 COLORREF TextColor
; /* Color of the text */
96 COLORREF LinkColor
; /* Color of links */
97 COLORREF VisitedColor
; /* Color of visited links */
98 WCHAR BreakChar
; /* Break Character for the current font */
99 BOOL IgnoreReturn
; /* (infoPtr->Style & LWS_IGNORERETURN) on creation */
102 static const WCHAR SL_LINKOPEN
[] = { '<','a', 0 };
103 static const WCHAR SL_HREF
[] = { 'h','r','e','f','=','\"',0 };
104 static const WCHAR SL_ID
[] = { 'i','d','=','\"',0 };
105 static const WCHAR SL_LINKCLOSE
[] = { '<','/','a','>',0 };
107 /* Control configuration constants */
109 #define SL_LEFTMARGIN (0)
110 #define SL_TOPMARGIN (0)
111 #define SL_RIGHTMARGIN (0)
112 #define SL_BOTTOMMARGIN (0)
114 /***********************************************************************
115 * SYSLINK_FreeDocItem
116 * Frees all data and gdi objects associated with a document item
118 static VOID
SYSLINK_FreeDocItem (PDOC_ITEM DocItem
)
120 if(DocItem
->Type
== slLink
)
122 Free(DocItem
->u
.Link
.szID
);
123 Free(DocItem
->u
.Link
.szUrl
);
126 Free(DocItem
->Blocks
);
128 /* we don't free Text because it's just a pointer to a character in the
129 entire window text string */
134 /***********************************************************************
135 * SYSLINK_AppendDocItem
136 * Create and append a new document item.
138 static PDOC_ITEM
SYSLINK_AppendDocItem (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
, UINT textlen
,
139 SL_ITEM_TYPE type
, PDOC_ITEM LastItem
)
143 textlen
= min(textlen
, strlenW(Text
));
144 Item
= Alloc(FIELD_OFFSET(DOC_ITEM
, Text
[textlen
+ 1]));
147 ERR("Failed to alloc DOC_ITEM structure!\n");
151 Item
->nText
= textlen
;
154 lstrcpynW(Item
->Text
, Text
, textlen
+ 1);
156 list_add_after(&LastItem
->entry
, &Item
->entry
);
158 list_add_tail(&infoPtr
->Items
, &Item
->entry
);
163 /***********************************************************************
165 * Clears the document tree
167 static VOID
SYSLINK_ClearDoc (SYSLINK_INFO
*infoPtr
)
169 DOC_ITEM
*Item
, *Item2
;
171 LIST_FOR_EACH_ENTRY_SAFE(Item
, Item2
, &infoPtr
->Items
, DOC_ITEM
, entry
)
173 list_remove(&Item
->entry
);
174 SYSLINK_FreeDocItem(Item
);
178 /***********************************************************************
180 * Wrapper for StrCmpNIW to ensure 'len' is not too big.
182 static INT
SYSLINK_StrCmpNIW (LPCWSTR str
, LPCWSTR comp
, INT len
)
186 for(i
= 0; i
< len
; i
++)
195 return StrCmpNIW(str
, comp
, len
);
198 /***********************************************************************
200 * Parses the window text string and creates a document. Returns the
201 * number of document items created.
203 static UINT
SYSLINK_ParseText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
205 LPCWSTR current
, textstart
= NULL
, linktext
= NULL
, firsttag
= NULL
;
206 int taglen
= 0, textlen
= 0, linklen
= 0, docitems
= 0;
207 PDOC_ITEM Last
= NULL
;
208 SL_ITEM_TYPE CurrentType
= slText
;
212 TRACE("(%p %s)\n", infoPtr
, debugstr_w(Text
));
214 for(current
= Text
; *current
!= 0;)
218 if(!SYSLINK_StrCmpNIW(current
, SL_LINKOPEN
, 2) && (CurrentType
== slText
))
220 BOOL ValidParam
= FALSE
, ValidLink
= FALSE
;
222 if(*(current
+ 2) == '>')
224 /* we just have to deal with a <a> tag */
233 else if(*(current
+ 2) == infoPtr
->BreakChar
)
235 /* we expect parameters, parse them */
236 LPCWSTR
*CurrentParameter
= NULL
, tmp
;
237 UINT
*CurrentParameterLen
= NULL
;
240 tmp
= current
+ taglen
;
245 /* compare the current position with all known parameters */
246 if(!SYSLINK_StrCmpNIW(tmp
, SL_HREF
, 6))
250 CurrentParameter
= &lpUrl
;
251 CurrentParameterLen
= &lenUrl
;
253 else if(!SYSLINK_StrCmpNIW(tmp
, SL_ID
, 4))
257 CurrentParameter
= &lpID
;
258 CurrentParameterLen
= &lenId
;
267 /* we got a known parameter, now search until the next " character.
268 If we can't find a " character, there's a syntax error and we just assume it's text */
270 *CurrentParameter
= current
+ taglen
;
271 *CurrentParameterLen
= 0;
273 for(tmp
= *CurrentParameter
; *tmp
!= 0; tmp
++)
282 (*CurrentParameterLen
)++;
287 /* we're done with this parameter, now there are only 2 possibilities:
288 * 1. another parameter is coming, so expect a ' ' (space) character
289 * 2. the tag is being closed, so expect a '<' character
291 if(*tmp
== infoPtr
->BreakChar
)
293 /* we expect another parameter, do the whole thing again */
300 /* the tag is being closed, we're done */
307 if(ValidLink
&& ValidParam
)
309 /* the <a ...> tag appears to be valid. save all information
310 so we can add the link if we find a valid </a> tag later */
311 CurrentType
= slLink
;
312 linktext
= current
+ taglen
;
321 if(textstart
== NULL
)
327 else if(!SYSLINK_StrCmpNIW(current
, SL_LINKCLOSE
, 4) && (CurrentType
== slLink
) && firsttag
)
329 /* there's a <a...> tag opened, first add the previous text, if present */
330 if(textstart
!= NULL
&& textlen
> 0 && firsttag
> textstart
)
332 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, firsttag
- textstart
, slText
, Last
);
335 ERR("Unable to create new document item!\n");
343 /* now it's time to add the link to the document */
345 if(linktext
!= NULL
&& linklen
> 0)
347 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slLink
, Last
);
350 ERR("Unable to create new document item!\n");
354 if(CurrentType
== slLink
)
358 if(!(infoPtr
->Style
& WS_DISABLED
))
360 Last
->u
.Link
.state
|= LIS_ENABLED
;
362 /* Copy the tag parameters */
365 nc
= min(lenId
, strlenW(lpID
));
366 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
367 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
368 if(Last
->u
.Link
.szID
!= NULL
)
370 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
374 Last
->u
.Link
.szID
= NULL
;
377 nc
= min(lenUrl
, strlenW(lpUrl
));
378 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
379 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
380 if(Last
->u
.Link
.szUrl
!= NULL
)
382 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
386 Last
->u
.Link
.szUrl
= NULL
;
390 CurrentType
= slText
;
397 /* we don't know what tag it is, so just continue */
400 if(CurrentType
== slText
&& textstart
== NULL
)
414 /* save the pointer of the current text item if we couldn't find a tag */
415 if(textstart
== NULL
&& CurrentType
== slText
)
424 if(textstart
!= NULL
&& textlen
> 0)
426 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, textlen
, CurrentType
, Last
);
429 ERR("Unable to create new document item!\n");
432 if(CurrentType
== slLink
)
436 if(!(infoPtr
->Style
& WS_DISABLED
))
438 Last
->u
.Link
.state
|= LIS_ENABLED
;
440 /* Copy the tag parameters */
443 nc
= min(lenId
, strlenW(lpID
));
444 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
445 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
446 if(Last
->u
.Link
.szID
!= NULL
)
448 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
452 Last
->u
.Link
.szID
= NULL
;
455 nc
= min(lenUrl
, strlenW(lpUrl
));
456 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
457 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
458 if(Last
->u
.Link
.szUrl
!= NULL
)
460 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
464 Last
->u
.Link
.szUrl
= NULL
;
469 if(linktext
!= NULL
&& linklen
> 0)
471 /* we got an unclosed link, just display the text */
472 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slText
, Last
);
475 ERR("Unable to create new document item!\n");
484 /***********************************************************************
485 * SYSLINK_RepaintLink
488 static VOID
SYSLINK_RepaintLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
493 if(DocItem
->Type
!= slLink
)
495 ERR("DocItem not a link!\n");
499 bl
= DocItem
->Blocks
;
506 InvalidateRect(infoPtr
->Self
, &bl
->rc
, TRUE
);
507 n
-= bl
->nChars
+ bl
->nSkip
;
513 /***********************************************************************
514 * SYSLINK_GetLinkItemByIndex
515 * Retrieves a document link by its index
517 static PDOC_ITEM
SYSLINK_GetLinkItemByIndex (const SYSLINK_INFO
*infoPtr
, int iLink
)
521 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
523 if ((Current
->Type
== slLink
) && (iLink
-- <= 0))
529 /***********************************************************************
530 * SYSLINK_GetFocusLink
531 * Retrieves the link that has the LIS_FOCUSED bit
533 static PDOC_ITEM
SYSLINK_GetFocusLink (const SYSLINK_INFO
*infoPtr
, int *LinkId
)
538 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
540 if(Current
->Type
== slLink
)
542 if(Current
->u
.Link
.state
& LIS_FOCUSED
)
555 /***********************************************************************
556 * SYSLINK_GetNextLink
559 static PDOC_ITEM
SYSLINK_GetNextLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
563 LIST_FOR_EACH_ENTRY(Next
, Current
? &Current
->entry
: &infoPtr
->Items
, DOC_ITEM
, entry
)
565 if (Next
->Type
== slLink
)
573 /***********************************************************************
574 * SYSLINK_GetPrevLink
575 * Gets the previous link
577 static PDOC_ITEM
SYSLINK_GetPrevLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
581 LIST_FOR_EACH_ENTRY_REV(Prev
, Current
? &Current
->entry
: list_tail(&infoPtr
->Items
), DOC_ITEM
, entry
)
583 if (Prev
->Type
== slLink
)
592 /***********************************************************************
594 * Tries to wrap a line.
596 static BOOL
SYSLINK_WrapLine (LPWSTR Text
, WCHAR BreakChar
, int x
, int *LineLen
,
597 int nFit
, LPSIZE Extent
)
601 for (i
= 0; i
< nFit
; i
++) if (Text
[i
] == '\n') break;
603 if (i
== *LineLen
) return FALSE
;
605 /* check if we're in the middle of a word */
606 if (Text
[i
] != '\n' && Text
[i
] != BreakChar
)
608 /* search for the beginning of the word */
609 while (i
&& Text
[i
- 1] != BreakChar
) i
--;
615 if (x
== SL_LEFTMARGIN
) i
= max( nFit
, 1 );
622 /***********************************************************************
624 * Renders the document in memory
626 static VOID
SYSLINK_Render (const SYSLINK_INFO
*infoPtr
, HDC hdc
, PRECT pRect
)
631 int x
, y
, LineHeight
;
635 szDoc
.cx
= szDoc
.cy
= 0;
638 rc
.right
-= SL_RIGHTMARGIN
;
639 rc
.bottom
-= SL_BOTTOMMARGIN
;
641 if(rc
.right
- SL_LEFTMARGIN
< 0)
643 if (rc
.bottom
- SL_TOPMARGIN
< 0)
646 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
650 GetTextMetricsW( hdc
, &tm
);
651 LineHeight
= tm
.tmHeight
+ tm
.tmExternalLeading
;
653 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
657 PDOC_TEXTBLOCK bl
, cbl
;
662 if(Current
->nText
== 0)
670 Free(Current
->Blocks
);
671 Current
->Blocks
= NULL
;
675 if(Current
->Type
== slText
)
677 SelectObject(hdc
, infoPtr
->Font
);
679 else if(Current
->Type
== slLink
)
681 SelectObject(hdc
, infoPtr
->LinkFont
);
686 /* skip break characters unless they're the first of the doc item */
687 if(tx
!= Current
->Text
|| x
== SL_LEFTMARGIN
)
689 if (n
&& *tx
== '\n')
695 while(n
> 0 && (*tx
) == infoPtr
->BreakChar
)
703 if((n
== 0 && SkipChars
!= 0) ||
704 GetTextExtentExPointW(hdc
, tx
, n
, rc
.right
- x
, &nFit
, NULL
, &szDim
))
712 Wrap
= SYSLINK_WrapLine(tx
, infoPtr
->BreakChar
, x
, &LineLen
, nFit
, &szDim
);
716 /* move one line down, the word didn't fit into the line */
724 if(!GetTextExtentExPointW(hdc
, tx
, LineLen
, rc
.right
- x
, NULL
, NULL
, &szDim
))
737 nbl
= ReAlloc(bl
, (nBlocks
+ 1) * sizeof(DOC_TEXTBLOCK
));
743 cbl
= bl
+ nBlocks
- 1;
745 cbl
->nChars
= LineLen
;
746 cbl
->nSkip
= SkipChars
;
747 SetRect(&cbl
->rc
, x
, y
, x
+ szDim
.cx
, y
+ szDim
.cy
);
749 if (cbl
->rc
.right
> szDoc
.cx
)
750 szDoc
.cx
= cbl
->rc
.right
;
751 if (cbl
->rc
.bottom
> szDoc
.cy
)
752 szDoc
.cy
= cbl
->rc
.bottom
;
770 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
785 Current
->Blocks
= bl
;
789 SelectObject(hdc
, hOldFont
);
791 pRect
->right
= pRect
->left
+ szDoc
.cx
;
792 pRect
->bottom
= pRect
->top
+ szDoc
.cy
;
795 /***********************************************************************
797 * Draws the SysLink control.
799 static LRESULT
SYSLINK_Draw (const SYSLINK_INFO
*infoPtr
, HDC hdc
)
804 COLORREF OldTextColor
, OldBkColor
;
806 UINT text_flags
= ETO_CLIPPED
;
807 UINT mode
= GetBkMode( hdc
);
809 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
810 OldTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
811 OldBkColor
= SetBkColor(hdc
, comctl32_color
.clrWindow
);
813 GetClientRect(infoPtr
->Self
, &rc
);
814 rc
.right
-= SL_RIGHTMARGIN
+ SL_LEFTMARGIN
;
815 rc
.bottom
-= SL_BOTTOMMARGIN
+ SL_TOPMARGIN
;
817 if(rc
.right
< 0 || rc
.bottom
< 0) return 0;
819 hBrush
= (HBRUSH
)SendMessageW(infoPtr
->Notify
, WM_CTLCOLORSTATIC
,
820 (WPARAM
)hdc
, (LPARAM
)infoPtr
->Self
);
821 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
823 FillRect(hdc
, &rc
, hBrush
);
824 if (GetBkMode( hdc
) == OPAQUE
) text_flags
|= ETO_OPAQUE
;
826 else SetBkMode( hdc
, TRANSPARENT
);
828 DeleteObject(hBrush
);
830 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
836 bl
= Current
->Blocks
;
842 if(Current
->Type
== slText
)
844 SelectObject(hdc
, infoPtr
->Font
);
845 SetTextColor(hdc
, infoPtr
->TextColor
);
849 SelectObject(hdc
, infoPtr
->LinkFont
);
850 SetTextColor(hdc
, (!(Current
->u
.Link
.state
& LIS_VISITED
) ? infoPtr
->LinkColor
: infoPtr
->VisitedColor
));
856 ExtTextOutW(hdc
, bl
->rc
.left
, bl
->rc
.top
, text_flags
, &bl
->rc
, tx
, bl
->nChars
, NULL
);
857 if((Current
->Type
== slLink
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && infoPtr
->HasFocus
)
859 COLORREF PrevTextColor
;
860 PrevTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
861 DrawFocusRect(hdc
, &bl
->rc
);
862 SetTextColor(hdc
, PrevTextColor
);
865 n
-= bl
->nChars
+ bl
->nSkip
;
871 SetBkColor(hdc
, OldBkColor
);
872 SetTextColor(hdc
, OldTextColor
);
873 SelectObject(hdc
, hOldFont
);
874 SetBkMode(hdc
, mode
);
879 /***********************************************************************
881 * Handles the WM_PAINT message.
883 static LRESULT
SYSLINK_Paint (const SYSLINK_INFO
*infoPtr
, HDC hdcParam
)
888 hdc
= hdcParam
? hdcParam
: BeginPaint (infoPtr
->Self
, &ps
);
891 SYSLINK_Draw (infoPtr
, hdc
);
892 if (!hdcParam
) EndPaint (infoPtr
->Self
, &ps
);
897 /***********************************************************************
899 * Set new Font for the SysLink control.
901 static HFONT
SYSLINK_SetFont (SYSLINK_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
907 HFONT hOldFont
= infoPtr
->Font
;
908 infoPtr
->Font
= hFont
;
910 /* free the underline font */
911 if(infoPtr
->LinkFont
!= NULL
)
913 DeleteObject(infoPtr
->LinkFont
);
914 infoPtr
->LinkFont
= NULL
;
917 /* Render text position and word wrapping in memory */
918 if (GetClientRect(infoPtr
->Self
, &rcClient
))
920 hdc
= GetDC(infoPtr
->Self
);
923 /* create a new underline font */
924 if(GetTextMetricsW(hdc
, &tm
) &&
925 GetObjectW(infoPtr
->Font
, sizeof(LOGFONTW
), &lf
))
927 lf
.lfUnderline
= TRUE
;
928 infoPtr
->LinkFont
= CreateFontIndirectW(&lf
);
929 infoPtr
->BreakChar
= tm
.tmBreakChar
;
933 ERR("Failed to create link font!\n");
936 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
937 ReleaseDC(infoPtr
->Self
, hdc
);
943 RedrawWindow(infoPtr
->Self
, NULL
, NULL
, RDW_INVALIDATE
| RDW_UPDATENOW
);
949 /***********************************************************************
951 * Set new text for the SysLink control.
953 static LRESULT
SYSLINK_SetText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
955 /* clear the document */
956 SYSLINK_ClearDoc(infoPtr
);
958 if(Text
== NULL
|| *Text
== 0)
963 /* let's parse the string and create a document */
964 if(SYSLINK_ParseText(infoPtr
, Text
) > 0)
968 /* Render text position and word wrapping in memory */
969 if (GetClientRect(infoPtr
->Self
, &rcClient
))
971 HDC hdc
= GetDC(infoPtr
->Self
);
974 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
975 ReleaseDC(infoPtr
->Self
, hdc
);
977 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
985 /***********************************************************************
986 * SYSLINK_SetFocusLink
987 * Updates the focus status bits and focusses the specified link.
988 * If no document item is specified, the focus bit will be removed from all links.
989 * Returns the previous focused item.
991 static PDOC_ITEM
SYSLINK_SetFocusLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
993 PDOC_ITEM Current
, PrevFocus
= NULL
;
995 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
997 if(Current
->Type
== slLink
)
999 if((PrevFocus
== NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
))
1001 PrevFocus
= Current
;
1004 if(Current
== DocItem
)
1006 Current
->u
.Link
.state
|= LIS_FOCUSED
;
1010 Current
->u
.Link
.state
&= ~LIS_FOCUSED
;
1018 /***********************************************************************
1020 * Sets the states and attributes of a link item.
1022 static LRESULT
SYSLINK_SetItem (const SYSLINK_INFO
*infoPtr
, const LITEM
*Item
)
1028 BOOL Repaint
= FALSE
;
1030 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1032 ERR("Invalid Flags!\n");
1036 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1039 ERR("Link %d couldn't be found\n", Item
->iLink
);
1043 if(Item
->mask
& LIF_ITEMID
)
1045 nc
= min(lstrlenW(Item
->szID
), MAX_LINKID_TEXT
- 1);
1046 szId
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1049 lstrcpynW(szId
, Item
->szID
, nc
+ 1);
1053 ERR("Unable to allocate memory for link id\n");
1058 if(Item
->mask
& LIF_URL
)
1060 nc
= min(lstrlenW(Item
->szUrl
), L_MAX_URL_LENGTH
- 1);
1061 szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1064 lstrcpynW(szUrl
, Item
->szUrl
, nc
+ 1);
1070 ERR("Unable to allocate memory for link url\n");
1075 if(Item
->mask
& LIF_ITEMID
)
1077 Free(di
->u
.Link
.szID
);
1078 di
->u
.Link
.szID
= szId
;
1081 if(Item
->mask
& LIF_URL
)
1083 Free(di
->u
.Link
.szUrl
);
1084 di
->u
.Link
.szUrl
= szUrl
;
1087 if(Item
->mask
& LIF_STATE
)
1089 UINT oldstate
= di
->u
.Link
.state
;
1090 /* clear the masked bits */
1091 di
->u
.Link
.state
&= ~(Item
->stateMask
& LIS_MASK
);
1093 di
->u
.Link
.state
|= (Item
->state
& Item
->stateMask
) & LIS_MASK
;
1094 Repaint
= (oldstate
!= di
->u
.Link
.state
);
1096 /* update the focus */
1097 SYSLINK_SetFocusLink(infoPtr
, ((di
->u
.Link
.state
& LIS_FOCUSED
) ? di
: NULL
));
1102 SYSLINK_RepaintLink(infoPtr
, di
);
1108 /***********************************************************************
1110 * Retrieves the states and attributes of a link item.
1112 static LRESULT
SYSLINK_GetItem (const SYSLINK_INFO
*infoPtr
, PLITEM Item
)
1116 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1118 ERR("Invalid Flags!\n");
1122 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1125 ERR("Link %d couldn't be found\n", Item
->iLink
);
1129 if(Item
->mask
& LIF_STATE
)
1131 Item
->state
= (di
->u
.Link
.state
& Item
->stateMask
);
1132 if(!infoPtr
->HasFocus
)
1134 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1135 Item
->state
&= ~LIS_FOCUSED
;
1139 if(Item
->mask
& LIF_ITEMID
)
1143 lstrcpyW(Item
->szID
, di
->u
.Link
.szID
);
1151 if(Item
->mask
& LIF_URL
)
1153 if(di
->u
.Link
.szUrl
)
1155 lstrcpyW(Item
->szUrl
, di
->u
.Link
.szUrl
);
1166 /***********************************************************************
1167 * SYSLINK_PtInDocItem
1168 * Determines if a point is in the region of a document item
1170 static BOOL
SYSLINK_PtInDocItem (const DOC_ITEM
*DocItem
, POINT pt
)
1175 bl
= DocItem
->Blocks
;
1182 if (PtInRect(&bl
->rc
, pt
))
1186 n
-= bl
->nChars
+ bl
->nSkip
;
1194 /***********************************************************************
1196 * Determines the link the user clicked on.
1198 static LRESULT
SYSLINK_HitTest (const SYSLINK_INFO
*infoPtr
, PLHITTESTINFO HitTest
)
1203 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
1205 if(Current
->Type
== slLink
)
1207 if(SYSLINK_PtInDocItem(Current
, HitTest
->pt
))
1209 HitTest
->item
.mask
= 0;
1210 HitTest
->item
.iLink
= id
;
1211 HitTest
->item
.state
= 0;
1212 HitTest
->item
.stateMask
= 0;
1213 if(Current
->u
.Link
.szID
)
1215 lstrcpyW(HitTest
->item
.szID
, Current
->u
.Link
.szID
);
1219 HitTest
->item
.szID
[0] = 0;
1221 if(Current
->u
.Link
.szUrl
)
1223 lstrcpyW(HitTest
->item
.szUrl
, Current
->u
.Link
.szUrl
);
1227 HitTest
->item
.szUrl
[0] = 0;
1238 /***********************************************************************
1239 * SYSLINK_GetIdealHeight
1240 * Returns the preferred height of a link at the current control's width.
1242 static LRESULT
SYSLINK_GetIdealHeight (const SYSLINK_INFO
*infoPtr
)
1244 HDC hdc
= GetDC(infoPtr
->Self
);
1249 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1251 if(GetTextMetricsW(hdc
, &tm
))
1253 height
= tm
.tmHeight
;
1259 SelectObject(hdc
, hOldFont
);
1260 ReleaseDC(infoPtr
->Self
, hdc
);
1267 /***********************************************************************
1268 * SYSLINK_SendParentNotify
1269 * Sends a WM_NOTIFY message to the parent window.
1271 static LRESULT
SYSLINK_SendParentNotify (const SYSLINK_INFO
*infoPtr
, UINT code
, const DOC_ITEM
*Link
, int iLink
)
1275 nml
.hdr
.hwndFrom
= infoPtr
->Self
;
1276 nml
.hdr
.idFrom
= GetWindowLongPtrW(infoPtr
->Self
, GWLP_ID
);
1277 nml
.hdr
.code
= code
;
1280 nml
.item
.iLink
= iLink
;
1282 nml
.item
.stateMask
= 0;
1283 if(Link
->u
.Link
.szID
)
1285 lstrcpyW(nml
.item
.szID
, Link
->u
.Link
.szID
);
1289 nml
.item
.szID
[0] = 0;
1291 if(Link
->u
.Link
.szUrl
)
1293 lstrcpyW(nml
.item
.szUrl
, Link
->u
.Link
.szUrl
);
1297 nml
.item
.szUrl
[0] = 0;
1300 return SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, nml
.hdr
.idFrom
, (LPARAM
)&nml
);
1303 /***********************************************************************
1305 * Handles receiving the input focus.
1307 static LRESULT
SYSLINK_SetFocus (SYSLINK_INFO
*infoPtr
)
1311 infoPtr
->HasFocus
= TRUE
;
1313 /* We always select the first link, even if we activated the control using
1314 SHIFT+TAB. This is the default behavior */
1315 Focus
= SYSLINK_GetNextLink(infoPtr
, NULL
);
1318 SYSLINK_SetFocusLink(infoPtr
, Focus
);
1319 SYSLINK_RepaintLink(infoPtr
, Focus
);
1324 /***********************************************************************
1326 * Handles losing the input focus.
1328 static LRESULT
SYSLINK_KillFocus (SYSLINK_INFO
*infoPtr
)
1332 infoPtr
->HasFocus
= FALSE
;
1333 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1337 SYSLINK_RepaintLink(infoPtr
, Focus
);
1343 /***********************************************************************
1345 * Returns a link at the specified position
1347 static PDOC_ITEM
SYSLINK_LinkAtPt (const SYSLINK_INFO
*infoPtr
, const POINT
*pt
, int *LinkId
, BOOL MustBeEnabled
)
1352 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
1354 if((Current
->Type
== slLink
) && SYSLINK_PtInDocItem(Current
, *pt
) &&
1355 (!MustBeEnabled
|| (Current
->u
.Link
.state
& LIS_ENABLED
)))
1369 /***********************************************************************
1370 * SYSLINK_LButtonDown
1371 * Handles mouse clicks
1373 static LRESULT
SYSLINK_LButtonDown (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1375 PDOC_ITEM Current
, Old
;
1378 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1381 SetFocus(infoPtr
->Self
);
1383 Old
= SYSLINK_SetFocusLink(infoPtr
, Current
);
1384 if(Old
!= NULL
&& Old
!= Current
)
1386 SYSLINK_RepaintLink(infoPtr
, Old
);
1388 infoPtr
->MouseDownID
= id
;
1389 SYSLINK_RepaintLink(infoPtr
, Current
);
1395 /***********************************************************************
1397 * Handles mouse clicks
1399 static LRESULT
SYSLINK_LButtonUp (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1401 if(infoPtr
->MouseDownID
> -1)
1406 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1407 if((Current
!= NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && (infoPtr
->MouseDownID
== id
))
1409 SYSLINK_SendParentNotify(infoPtr
, NM_CLICK
, Current
, id
);
1413 infoPtr
->MouseDownID
= -1;
1418 /***********************************************************************
1420 * Handles ENTER key events
1422 static BOOL
SYSLINK_OnEnter (const SYSLINK_INFO
*infoPtr
)
1424 if(infoPtr
->HasFocus
&& !infoPtr
->IgnoreReturn
)
1429 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1432 SYSLINK_SendParentNotify(infoPtr
, NM_RETURN
, Focus
, id
);
1439 /***********************************************************************
1440 * SYSKEY_SelectNextPrevLink
1441 * Changes the currently focused link
1443 static BOOL
SYSKEY_SelectNextPrevLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1445 if(infoPtr
->HasFocus
)
1450 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1453 PDOC_ITEM NewFocus
, OldFocus
;
1456 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1458 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1460 if(NewFocus
!= NULL
)
1462 OldFocus
= SYSLINK_SetFocusLink(infoPtr
, NewFocus
);
1464 if(OldFocus
&& OldFocus
!= NewFocus
)
1466 SYSLINK_RepaintLink(infoPtr
, OldFocus
);
1468 SYSLINK_RepaintLink(infoPtr
, NewFocus
);
1476 /***********************************************************************
1477 * SYSKEY_SelectNextPrevLink
1478 * Determines if there's a next or previous link to decide whether the control
1479 * should capture the tab key message
1481 static BOOL
SYSLINK_NoNextLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1483 PDOC_ITEM Focus
, NewFocus
;
1485 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1487 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1489 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1491 return NewFocus
== NULL
;
1494 /***********************************************************************
1495 * SYSLINK_GetIdealSize
1496 * Calculates the ideal size of a link control at a given maximum width.
1498 static VOID
SYSLINK_GetIdealSize (const SYSLINK_INFO
*infoPtr
, int cxMaxWidth
, LPSIZE lpSize
)
1503 rc
.left
= rc
.top
= rc
.bottom
= 0;
1504 rc
.right
= cxMaxWidth
;
1506 hdc
= GetDC(infoPtr
->Self
);
1509 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1511 SYSLINK_Render(infoPtr
, hdc
, &rc
);
1513 SelectObject(hdc
, hOldFont
);
1514 ReleaseDC(infoPtr
->Self
, hdc
);
1516 lpSize
->cx
= rc
.right
;
1517 lpSize
->cy
= rc
.bottom
;
1521 /***********************************************************************
1524 static LRESULT WINAPI
SysLinkWindowProc(HWND hwnd
, UINT message
,
1525 WPARAM wParam
, LPARAM lParam
)
1527 SYSLINK_INFO
*infoPtr
;
1529 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd
, message
, wParam
, lParam
);
1531 infoPtr
= (SYSLINK_INFO
*)GetWindowLongPtrW(hwnd
, 0);
1533 if (!infoPtr
&& message
!= WM_CREATE
)
1534 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1537 case WM_PRINTCLIENT
:
1539 return SYSLINK_Paint (infoPtr
, (HDC
)wParam
);
1542 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
1544 HDC hdc
= (HDC
)wParam
;
1545 HBRUSH brush
= CreateSolidBrush( comctl32_color
.clrWindow
);
1548 GetClipBox( hdc
, &rect
);
1549 FillRect( hdc
, &rect
, brush
);
1550 DeleteObject( brush
);
1558 DWORD mp
= GetMessagePos();
1560 ht
.pt
.x
= (short)LOWORD(mp
);
1561 ht
.pt
.y
= (short)HIWORD(mp
);
1563 ScreenToClient(infoPtr
->Self
, &ht
.pt
);
1564 if(SYSLINK_HitTest (infoPtr
, &ht
))
1566 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_HAND
));
1570 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1576 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1578 HDC hdc
= GetDC(infoPtr
->Self
);
1581 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1582 ReleaseDC(infoPtr
->Self
, hdc
);
1589 return (LRESULT
)infoPtr
->Font
;
1592 return (LRESULT
)SYSLINK_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
1595 SYSLINK_SetText(infoPtr
, (LPWSTR
)lParam
);
1596 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1598 case WM_LBUTTONDOWN
:
1601 pt
.x
= (short)LOWORD(lParam
);
1602 pt
.y
= (short)HIWORD(lParam
);
1603 return SYSLINK_LButtonDown(infoPtr
, &pt
);
1608 pt
.x
= (short)LOWORD(lParam
);
1609 pt
.y
= (short)HIWORD(lParam
);
1610 return SYSLINK_LButtonUp(infoPtr
, &pt
);
1618 SYSLINK_OnEnter(infoPtr
);
1622 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1623 SYSKEY_SelectNextPrevLink(infoPtr
, shift
);
1627 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1633 LRESULT Ret
= DLGC_HASSETSEL
;
1634 int vk
= (lParam
!= 0 ? (int)((LPMSG
)lParam
)->wParam
: 0);
1638 Ret
|= DLGC_WANTMESSAGE
;
1642 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1643 if(!SYSLINK_NoNextLink(infoPtr
, shift
))
1645 Ret
|= DLGC_WANTTAB
;
1649 Ret
|= DLGC_WANTCHARS
;
1661 pt
.x
= (short)LOWORD(lParam
);
1662 pt
.y
= (short)HIWORD(lParam
);
1664 GetClientRect(infoPtr
->Self
, &rc
);
1665 ScreenToClient(infoPtr
->Self
, &pt
);
1666 if(pt
.x
< 0 || pt
.y
< 0 || pt
.x
> rc
.right
|| pt
.y
> rc
.bottom
)
1671 if(SYSLINK_LinkAtPt(infoPtr
, &pt
, NULL
, FALSE
))
1676 return HTTRANSPARENT
;
1680 return SYSLINK_HitTest(infoPtr
, (PLHITTESTINFO
)lParam
);
1683 return SYSLINK_SetItem(infoPtr
, (PLITEM
)lParam
);
1686 return SYSLINK_GetItem(infoPtr
, (PLITEM
)lParam
);
1688 case LM_GETIDEALHEIGHT
:
1691 /* LM_GETIDEALSIZE */
1692 SYSLINK_GetIdealSize(infoPtr
, (int)wParam
, (LPSIZE
)lParam
);
1694 return SYSLINK_GetIdealHeight(infoPtr
);
1697 return SYSLINK_SetFocus(infoPtr
);
1700 return SYSLINK_KillFocus(infoPtr
);
1703 infoPtr
->Style
&= ~WS_DISABLED
;
1704 infoPtr
->Style
|= (wParam
? 0 : WS_DISABLED
);
1705 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
1708 case WM_STYLECHANGED
:
1709 if (wParam
== GWL_STYLE
)
1711 infoPtr
->Style
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
1713 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1719 CREATESTRUCTW
*cs
= (CREATESTRUCTW
*)lParam
;
1721 /* allocate memory for info struct */
1722 infoPtr
= Alloc (sizeof(SYSLINK_INFO
));
1723 if (!infoPtr
) return -1;
1724 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
1726 /* initialize the info struct */
1727 infoPtr
->Self
= hwnd
;
1728 infoPtr
->Notify
= cs
->hwndParent
;
1729 infoPtr
->Style
= cs
->style
;
1731 infoPtr
->LinkFont
= 0;
1732 list_init(&infoPtr
->Items
);
1733 infoPtr
->HasFocus
= FALSE
;
1734 infoPtr
->MouseDownID
= -1;
1735 infoPtr
->TextColor
= comctl32_color
.clrWindowText
;
1736 infoPtr
->LinkColor
= comctl32_color
.clrHighlight
;
1737 infoPtr
->VisitedColor
= comctl32_color
.clrHighlight
;
1738 infoPtr
->BreakChar
= ' ';
1739 infoPtr
->IgnoreReturn
= infoPtr
->Style
& LWS_IGNORERETURN
;
1740 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd
);
1741 SYSLINK_SetText(infoPtr
, cs
->lpszName
);
1745 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd
);
1746 SYSLINK_ClearDoc(infoPtr
);
1747 if(infoPtr
->Font
!= 0) DeleteObject(infoPtr
->Font
);
1748 if(infoPtr
->LinkFont
!= 0) DeleteObject(infoPtr
->LinkFont
);
1749 SetWindowLongPtrW(hwnd
, 0, 0);
1753 case WM_SYSCOLORCHANGE
:
1754 COMCTL32_RefreshSysColors();
1758 if ((message
>= WM_USER
) && (message
< WM_APP
) && !COMCTL32_IsReflectedMessage(message
))
1760 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message
, wParam
, lParam
);
1762 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1767 /***********************************************************************
1768 * SYSLINK_Register [Internal]
1770 * Registers the SysLink window class.
1772 VOID
SYSLINK_Register (void)
1776 ZeroMemory (&wndClass
, sizeof(wndClass
));
1777 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
1778 wndClass
.lpfnWndProc
= SysLinkWindowProc
;
1779 wndClass
.cbClsExtra
= 0;
1780 wndClass
.cbWndExtra
= sizeof (SYSLINK_INFO
*);
1781 wndClass
.hCursor
= LoadCursorW (0, (LPWSTR
)IDC_ARROW
);
1782 wndClass
.lpszClassName
= WC_LINK
;
1784 RegisterClassW (&wndClass
);
1788 /***********************************************************************
1789 * SYSLINK_Unregister [Internal]
1791 * Unregisters the SysLink window class.
1793 VOID
SYSLINK_Unregister (void)
1795 UnregisterClassW (WC_LINK
, NULL
);