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
);
34 INT WINAPI
StrCmpNIW(LPCWSTR
,LPCWSTR
,INT
);
41 } DOC_TEXTBLOCK
, *PDOC_TEXTBLOCK
;
43 #define LIF_FLAGSMASK (LIF_STATE | LIF_ITEMID | LIF_URL)
44 #define LIS_MASK (LIS_FOCUSED | LIS_ENABLED | LIS_VISITED)
52 typedef struct _DOC_ITEM
54 struct _DOC_ITEM
*Next
; /* Address to the next item */
55 UINT nText
; /* Number of characters of the text */
56 SL_ITEM_TYPE Type
; /* type of the item */
57 PDOC_TEXTBLOCK Blocks
; /* Array of text blocks */
62 UINT state
; /* Link state */
63 WCHAR
*szID
; /* Link ID string */
64 WCHAR
*szUrl
; /* Link URL string */
71 WCHAR Text
[1]; /* Text of the document item */
72 } DOC_ITEM
, *PDOC_ITEM
;
76 HWND Self
; /* The window handle for this control */
77 HWND Notify
; /* The parent handle to receive notifications */
78 DWORD Style
; /* Styles for this control */
79 PDOC_ITEM Items
; /* Address to the first document item */
80 BOOL HasFocus
; /* Whether the control has the input focus */
81 int MouseDownID
; /* ID of the link that the mouse button first selected */
82 HFONT Font
; /* Handle to the font for text */
83 HFONT LinkFont
; /* Handle to the font for links */
84 COLORREF TextColor
; /* Color of the text */
85 COLORREF LinkColor
; /* Color of links */
86 COLORREF VisitedColor
; /* Color of visited links */
87 WCHAR BreakChar
; /* Break Character for the current font */
88 BOOL IgnoreReturn
; /* (infoPtr->Style & LWS_IGNORERETURN) on creation */
91 static const WCHAR SL_LINKOPEN
[] = { '<','a', 0 };
92 static const WCHAR SL_HREF
[] = { 'h','r','e','f','=','\"',0 };
93 static const WCHAR SL_ID
[] = { 'i','d','=','\"',0 };
94 static const WCHAR SL_LINKCLOSE
[] = { '<','/','a','>',0 };
96 /* Control configuration constants */
98 #define SL_LEFTMARGIN (0)
99 #define SL_TOPMARGIN (0)
100 #define SL_RIGHTMARGIN (0)
101 #define SL_BOTTOMMARGIN (0)
103 /***********************************************************************
104 * SYSLINK_FreeDocItem
105 * Frees all data and gdi objects associated with a document item
107 static VOID
SYSLINK_FreeDocItem (PDOC_ITEM DocItem
)
109 if(DocItem
->Type
== slLink
)
111 Free(DocItem
->u
.Link
.szID
);
112 Free(DocItem
->u
.Link
.szUrl
);
115 /* we don't free Text because it's just a pointer to a character in the
116 entire window text string */
121 /***********************************************************************
122 * SYSLINK_AppendDocItem
123 * Create and append a new document item.
125 static PDOC_ITEM
SYSLINK_AppendDocItem (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
, UINT textlen
,
126 SL_ITEM_TYPE type
, PDOC_ITEM LastItem
)
130 textlen
= min(textlen
, strlenW(Text
));
131 Item
= Alloc(FIELD_OFFSET(DOC_ITEM
, Text
[textlen
+ 1]));
134 ERR("Failed to alloc DOC_ITEM structure!\n");
139 Item
->nText
= textlen
;
145 LastItem
->Next
= Item
;
149 infoPtr
->Items
= Item
;
152 lstrcpynW(Item
->Text
, Text
, textlen
+ 1);
157 /***********************************************************************
159 * Clears the document tree
161 static VOID
SYSLINK_ClearDoc (SYSLINK_INFO
*infoPtr
)
163 PDOC_ITEM Item
, Next
;
165 Item
= infoPtr
->Items
;
169 SYSLINK_FreeDocItem(Item
);
173 infoPtr
->Items
= NULL
;
176 /***********************************************************************
178 * Parses the window text string and creates a document. Returns the
179 * number of document items created.
181 static UINT
SYSLINK_ParseText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
183 LPCWSTR current
, textstart
= NULL
, linktext
= NULL
, firsttag
= NULL
;
184 int taglen
= 0, textlen
= 0, linklen
= 0, docitems
= 0;
185 PDOC_ITEM Last
= NULL
;
186 SL_ITEM_TYPE CurrentType
= slText
;
190 TRACE("(%p %s)\n", infoPtr
, debugstr_w(Text
));
192 for(current
= Text
; *current
!= 0;)
196 if(!StrCmpNIW(current
, SL_LINKOPEN
, 2) && (CurrentType
== slText
))
198 BOOL ValidParam
= FALSE
, ValidLink
= FALSE
;
200 if(*(current
+ 2) == '>')
202 /* we just have to deal with a <a> tag */
211 else if(*(current
+ 2) == infoPtr
->BreakChar
)
213 /* we expect parameters, parse them */
214 LPCWSTR
*CurrentParameter
= NULL
, tmp
;
215 UINT
*CurrentParameterLen
= NULL
;
218 tmp
= current
+ taglen
;
223 /* compare the current position with all known parameters */
224 if(!StrCmpNIW(tmp
, SL_HREF
, 6))
228 CurrentParameter
= &lpUrl
;
229 CurrentParameterLen
= &lenUrl
;
231 else if(!StrCmpNIW(tmp
, SL_ID
, 4))
235 CurrentParameter
= &lpID
;
236 CurrentParameterLen
= &lenId
;
245 /* we got a known parameter, now search until the next " character.
246 If we can't find a " character, there's a syntax error and we just assume it's text */
248 *CurrentParameter
= current
+ taglen
;
249 *CurrentParameterLen
= 0;
251 for(tmp
= *CurrentParameter
; *tmp
!= 0; tmp
++)
260 (*CurrentParameterLen
)++;
265 /* we're done with this parameter, now there are only 2 possibilities:
266 * 1. another parameter is coming, so expect a ' ' (space) character
267 * 2. the tag is being closed, so expect a '<' character
269 if(*tmp
== infoPtr
->BreakChar
)
271 /* we expect another parameter, do the whole thing again */
278 /* the tag is being closed, we're done */
285 if(ValidLink
&& ValidParam
)
287 /* the <a ...> tag appears to be valid. save all information
288 so we can add the link if we find a valid </a> tag later */
289 CurrentType
= slLink
;
290 linktext
= current
+ taglen
;
299 if(textstart
== NULL
)
305 else if(!StrCmpNIW(current
, SL_LINKCLOSE
, 4) && (CurrentType
== slLink
) && firsttag
)
307 /* there's a <a...> tag opened, first add the previous text, if present */
308 if(textstart
!= NULL
&& textlen
> 0 && firsttag
> textstart
)
310 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, firsttag
- textstart
, slText
, Last
);
313 ERR("Unable to create new document item!\n");
321 /* now it's time to add the link to the document */
323 if(linktext
!= NULL
&& linklen
> 0)
325 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slLink
, Last
);
328 ERR("Unable to create new document item!\n");
332 if(CurrentType
== slLink
)
336 if(!(infoPtr
->Style
& WS_DISABLED
))
338 Last
->u
.Link
.state
|= LIS_ENABLED
;
340 /* Copy the tag parameters */
343 nc
= min(lenId
, strlenW(lpID
));
344 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
345 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
346 if(Last
->u
.Link
.szID
!= NULL
)
348 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
352 Last
->u
.Link
.szID
= NULL
;
355 nc
= min(lenUrl
, strlenW(lpUrl
));
356 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
357 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
358 if(Last
->u
.Link
.szUrl
!= NULL
)
360 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
364 Last
->u
.Link
.szUrl
= NULL
;
368 CurrentType
= slText
;
375 /* we don't know what tag it is, so just continue */
378 if(CurrentType
== slText
&& textstart
== NULL
)
392 /* save the pointer of the current text item if we couldn't find a tag */
393 if(textstart
== NULL
&& CurrentType
== slText
)
402 if(textstart
!= NULL
&& textlen
> 0)
404 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, textlen
, CurrentType
, Last
);
407 ERR("Unable to create new document item!\n");
410 if(CurrentType
== slLink
)
414 if(!(infoPtr
->Style
& WS_DISABLED
))
416 Last
->u
.Link
.state
|= LIS_ENABLED
;
418 /* Copy the tag parameters */
421 nc
= min(lenId
, strlenW(lpID
));
422 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
423 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
424 if(Last
->u
.Link
.szID
!= NULL
)
426 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
430 Last
->u
.Link
.szID
= NULL
;
433 nc
= min(lenUrl
, strlenW(lpUrl
));
434 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
435 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
436 if(Last
->u
.Link
.szUrl
!= NULL
)
438 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
442 Last
->u
.Link
.szUrl
= NULL
;
447 if(linktext
!= NULL
&& linklen
> 0)
449 /* we got an unclosed link, just display the text */
450 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slText
, Last
);
453 ERR("Unable to create new document item!\n");
462 /***********************************************************************
463 * SYSLINK_RepaintLink
466 static VOID
SYSLINK_RepaintLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
471 if(DocItem
->Type
!= slLink
)
473 ERR("DocItem not a link!\n");
477 bl
= DocItem
->Blocks
;
484 InvalidateRect(infoPtr
->Self
, &bl
->rc
, TRUE
);
485 n
-= bl
->nChars
+ bl
->nSkip
;
491 /***********************************************************************
492 * SYSLINK_GetLinkItemByIndex
493 * Retrieves a document link by its index
495 static PDOC_ITEM
SYSLINK_GetLinkItemByIndex (const SYSLINK_INFO
*infoPtr
, int iLink
)
497 PDOC_ITEM Current
= infoPtr
->Items
;
499 while(Current
!= NULL
)
501 if((Current
->Type
== slLink
) && (iLink
-- <= 0))
505 Current
= Current
->Next
;
510 /***********************************************************************
511 * SYSLINK_GetFocusLink
512 * Retrieves the link that has the LIS_FOCUSED bit
514 static PDOC_ITEM
SYSLINK_GetFocusLink (const SYSLINK_INFO
*infoPtr
, int *LinkId
)
516 PDOC_ITEM Current
= infoPtr
->Items
;
519 while(Current
!= NULL
)
521 if(Current
->Type
== slLink
)
523 if(Current
->u
.Link
.state
& LIS_FOCUSED
)
531 Current
= Current
->Next
;
536 /***********************************************************************
537 * SYSLINK_GetNextLink
540 static PDOC_ITEM
SYSLINK_GetNextLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
542 for(Current
= (Current
!= NULL
? Current
->Next
: infoPtr
->Items
);
544 Current
= Current
->Next
)
546 if(Current
->Type
== slLink
)
554 /***********************************************************************
555 * SYSLINK_GetPrevLink
556 * Gets the previous link
558 static PDOC_ITEM
SYSLINK_GetPrevLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
562 /* returns the last link */
563 PDOC_ITEM Last
= NULL
;
565 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
567 if(Current
->Type
== slLink
)
576 /* returns the previous link */
577 PDOC_ITEM Cur
, Prev
= NULL
;
579 for(Cur
= infoPtr
->Items
; Cur
!= NULL
; Cur
= Cur
->Next
)
585 if(Cur
->Type
== slLink
)
594 /***********************************************************************
596 * Tries to wrap a line.
598 static BOOL
SYSLINK_WrapLine (LPWSTR Text
, WCHAR BreakChar
, int x
, int *LineLen
,
599 int nFit
, LPSIZE Extent
)
603 for (i
= 0; i
< nFit
; i
++) if (Text
[i
] == '\n') break;
605 if (i
== *LineLen
) return FALSE
;
607 /* check if we're in the middle of a word */
608 if (Text
[i
] != '\n' && Text
[i
] != BreakChar
)
610 /* search for the beginning of the word */
611 while (i
&& Text
[i
- 1] != BreakChar
) i
--;
617 if (x
== SL_LEFTMARGIN
) i
= max( nFit
, 1 );
624 /***********************************************************************
626 * Renders the document in memory
628 static VOID
SYSLINK_Render (const SYSLINK_INFO
*infoPtr
, HDC hdc
, PRECT pRect
)
633 int x
, y
, LineHeight
;
637 szDoc
.cx
= szDoc
.cy
= 0;
640 rc
.right
-= SL_RIGHTMARGIN
;
641 rc
.bottom
-= SL_BOTTOMMARGIN
;
643 if(rc
.right
- SL_LEFTMARGIN
< 0)
645 if (rc
.bottom
- SL_TOPMARGIN
< 0)
648 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
652 GetTextMetricsW( hdc
, &tm
);
653 LineHeight
= tm
.tmHeight
+ tm
.tmExternalLeading
;
655 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
659 PDOC_TEXTBLOCK bl
, cbl
;
664 if(Current
->nText
== 0)
672 Free(Current
->Blocks
);
673 Current
->Blocks
= NULL
;
677 if(Current
->Type
== slText
)
679 SelectObject(hdc
, infoPtr
->Font
);
681 else if(Current
->Type
== slLink
)
683 SelectObject(hdc
, infoPtr
->LinkFont
);
688 /* skip break characters unless they're the first of the doc item */
689 if(tx
!= Current
->Text
|| x
== SL_LEFTMARGIN
)
691 if (n
&& *tx
== '\n')
697 while(n
> 0 && (*tx
) == infoPtr
->BreakChar
)
705 if((n
== 0 && SkipChars
!= 0) ||
706 GetTextExtentExPointW(hdc
, tx
, n
, rc
.right
- x
, &nFit
, NULL
, &szDim
))
714 Wrap
= SYSLINK_WrapLine(tx
, infoPtr
->BreakChar
, x
, &LineLen
, nFit
, &szDim
);
718 /* move one line down, the word didn't fit into the line */
726 if(!GetTextExtentExPointW(hdc
, tx
, LineLen
, rc
.right
- x
, NULL
, NULL
, &szDim
))
739 nbl
= ReAlloc(bl
, (nBlocks
+ 1) * sizeof(DOC_TEXTBLOCK
));
745 cbl
= bl
+ nBlocks
- 1;
747 cbl
->nChars
= LineLen
;
748 cbl
->nSkip
= SkipChars
;
751 cbl
->rc
.right
= x
+ szDim
.cx
;
752 cbl
->rc
.bottom
= y
+ szDim
.cy
;
754 if (cbl
->rc
.right
> szDoc
.cx
)
755 szDoc
.cx
= cbl
->rc
.right
;
756 if (cbl
->rc
.bottom
> szDoc
.cy
)
757 szDoc
.cy
= cbl
->rc
.bottom
;
775 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
790 Current
->Blocks
= bl
;
794 SelectObject(hdc
, hOldFont
);
796 pRect
->right
= pRect
->left
+ szDoc
.cx
;
797 pRect
->bottom
= pRect
->top
+ szDoc
.cy
;
800 /***********************************************************************
802 * Draws the SysLink control.
804 static LRESULT
SYSLINK_Draw (const SYSLINK_INFO
*infoPtr
, HDC hdc
)
809 COLORREF OldTextColor
, OldBkColor
;
811 UINT text_flags
= ETO_CLIPPED
;
812 UINT mode
= GetBkMode( hdc
);
814 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
815 OldTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
816 OldBkColor
= SetBkColor(hdc
, comctl32_color
.clrWindow
);
818 GetClientRect(infoPtr
->Self
, &rc
);
819 rc
.right
-= SL_RIGHTMARGIN
+ SL_LEFTMARGIN
;
820 rc
.bottom
-= SL_BOTTOMMARGIN
+ SL_TOPMARGIN
;
822 if(rc
.right
< 0 || rc
.bottom
< 0) return 0;
824 hBrush
= (HBRUSH
)SendMessageW(infoPtr
->Notify
, WM_CTLCOLORSTATIC
,
825 (WPARAM
)hdc
, (LPARAM
)infoPtr
->Self
);
826 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
828 FillRect(hdc
, &rc
, hBrush
);
829 if (GetBkMode( hdc
) == OPAQUE
) text_flags
|= ETO_OPAQUE
;
831 else SetBkMode( hdc
, TRANSPARENT
);
833 DeleteObject(hBrush
);
835 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
841 bl
= Current
->Blocks
;
847 if(Current
->Type
== slText
)
849 SelectObject(hdc
, infoPtr
->Font
);
850 SetTextColor(hdc
, infoPtr
->TextColor
);
854 SelectObject(hdc
, infoPtr
->LinkFont
);
855 SetTextColor(hdc
, (!(Current
->u
.Link
.state
& LIS_VISITED
) ? infoPtr
->LinkColor
: infoPtr
->VisitedColor
));
861 ExtTextOutW(hdc
, bl
->rc
.left
, bl
->rc
.top
, text_flags
, &bl
->rc
, tx
, bl
->nChars
, NULL
);
862 if((Current
->Type
== slLink
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && infoPtr
->HasFocus
)
864 COLORREF PrevTextColor
;
865 PrevTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
866 DrawFocusRect(hdc
, &bl
->rc
);
867 SetTextColor(hdc
, PrevTextColor
);
870 n
-= bl
->nChars
+ bl
->nSkip
;
876 SetBkColor(hdc
, OldBkColor
);
877 SetTextColor(hdc
, OldTextColor
);
878 SelectObject(hdc
, hOldFont
);
879 SetBkMode(hdc
, mode
);
884 /***********************************************************************
886 * Handles the WM_PAINT message.
888 static LRESULT
SYSLINK_Paint (const SYSLINK_INFO
*infoPtr
, HDC hdcParam
)
893 hdc
= hdcParam
? hdcParam
: BeginPaint (infoPtr
->Self
, &ps
);
896 SYSLINK_Draw (infoPtr
, hdc
);
897 if (!hdcParam
) EndPaint (infoPtr
->Self
, &ps
);
902 /***********************************************************************
904 * Set new Font for the SysLink control.
906 static HFONT
SYSLINK_SetFont (SYSLINK_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
912 HFONT hOldFont
= infoPtr
->Font
;
913 infoPtr
->Font
= hFont
;
915 /* free the underline font */
916 if(infoPtr
->LinkFont
!= NULL
)
918 DeleteObject(infoPtr
->LinkFont
);
919 infoPtr
->LinkFont
= NULL
;
922 /* Render text position and word wrapping in memory */
923 if (GetClientRect(infoPtr
->Self
, &rcClient
))
925 hdc
= GetDC(infoPtr
->Self
);
928 /* create a new underline font */
929 if(GetTextMetricsW(hdc
, &tm
) &&
930 GetObjectW(infoPtr
->Font
, sizeof(LOGFONTW
), &lf
))
932 lf
.lfUnderline
= TRUE
;
933 infoPtr
->LinkFont
= CreateFontIndirectW(&lf
);
934 infoPtr
->BreakChar
= tm
.tmBreakChar
;
938 ERR("Failed to create link font!\n");
941 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
942 ReleaseDC(infoPtr
->Self
, hdc
);
948 RedrawWindow(infoPtr
->Self
, NULL
, NULL
, RDW_INVALIDATE
| RDW_UPDATENOW
);
954 /***********************************************************************
956 * Set new text for the SysLink control.
958 static LRESULT
SYSLINK_SetText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
960 /* clear the document */
961 SYSLINK_ClearDoc(infoPtr
);
963 if(Text
== NULL
|| *Text
== 0)
968 /* let's parse the string and create a document */
969 if(SYSLINK_ParseText(infoPtr
, Text
) > 0)
973 /* Render text position and word wrapping in memory */
974 if (GetClientRect(infoPtr
->Self
, &rcClient
))
976 HDC hdc
= GetDC(infoPtr
->Self
);
979 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
980 ReleaseDC(infoPtr
->Self
, hdc
);
982 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
990 /***********************************************************************
991 * SYSLINK_SetFocusLink
992 * Updates the focus status bits and focusses the specified link.
993 * If no document item is specified, the focus bit will be removed from all links.
994 * Returns the previous focused item.
996 static PDOC_ITEM
SYSLINK_SetFocusLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
998 PDOC_ITEM Current
, PrevFocus
= NULL
;
1000 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1002 if(Current
->Type
== slLink
)
1004 if((PrevFocus
== NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
))
1006 PrevFocus
= Current
;
1009 if(Current
== DocItem
)
1011 Current
->u
.Link
.state
|= LIS_FOCUSED
;
1015 Current
->u
.Link
.state
&= ~LIS_FOCUSED
;
1023 /***********************************************************************
1025 * Sets the states and attributes of a link item.
1027 static LRESULT
SYSLINK_SetItem (const SYSLINK_INFO
*infoPtr
, const LITEM
*Item
)
1033 BOOL Repaint
= FALSE
;
1035 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1037 ERR("Invalid Flags!\n");
1041 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1044 ERR("Link %d couldn't be found\n", Item
->iLink
);
1048 if(Item
->mask
& LIF_ITEMID
)
1050 nc
= min(lstrlenW(Item
->szID
), MAX_LINKID_TEXT
- 1);
1051 szId
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1054 lstrcpynW(szId
, Item
->szID
, nc
+ 1);
1058 ERR("Unable to allocate memory for link id\n");
1063 if(Item
->mask
& LIF_URL
)
1065 nc
= min(lstrlenW(Item
->szUrl
), L_MAX_URL_LENGTH
- 1);
1066 szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1069 lstrcpynW(szUrl
, Item
->szUrl
, nc
+ 1);
1075 ERR("Unable to allocate memory for link url\n");
1080 if(Item
->mask
& LIF_ITEMID
)
1082 Free(di
->u
.Link
.szID
);
1083 di
->u
.Link
.szID
= szId
;
1086 if(Item
->mask
& LIF_URL
)
1088 Free(di
->u
.Link
.szUrl
);
1089 di
->u
.Link
.szUrl
= szUrl
;
1092 if(Item
->mask
& LIF_STATE
)
1094 UINT oldstate
= di
->u
.Link
.state
;
1095 /* clear the masked bits */
1096 di
->u
.Link
.state
&= ~(Item
->stateMask
& LIS_MASK
);
1098 di
->u
.Link
.state
|= (Item
->state
& Item
->stateMask
) & LIS_MASK
;
1099 Repaint
= (oldstate
!= di
->u
.Link
.state
);
1101 /* update the focus */
1102 SYSLINK_SetFocusLink(infoPtr
, ((di
->u
.Link
.state
& LIS_FOCUSED
) ? di
: NULL
));
1107 SYSLINK_RepaintLink(infoPtr
, di
);
1113 /***********************************************************************
1115 * Retrieves the states and attributes of a link item.
1117 static LRESULT
SYSLINK_GetItem (const SYSLINK_INFO
*infoPtr
, PLITEM Item
)
1121 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1123 ERR("Invalid Flags!\n");
1127 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1130 ERR("Link %d couldn't be found\n", Item
->iLink
);
1134 if(Item
->mask
& LIF_STATE
)
1136 Item
->state
= (di
->u
.Link
.state
& Item
->stateMask
);
1137 if(!infoPtr
->HasFocus
)
1139 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1140 Item
->state
&= ~LIS_FOCUSED
;
1144 if(Item
->mask
& LIF_ITEMID
)
1148 lstrcpyW(Item
->szID
, di
->u
.Link
.szID
);
1156 if(Item
->mask
& LIF_URL
)
1158 if(di
->u
.Link
.szUrl
)
1160 lstrcpyW(Item
->szUrl
, di
->u
.Link
.szUrl
);
1171 /***********************************************************************
1172 * SYSLINK_PtInDocItem
1173 * Determines if a point is in the region of a document item
1175 static BOOL
SYSLINK_PtInDocItem (const DOC_ITEM
*DocItem
, POINT pt
)
1180 bl
= DocItem
->Blocks
;
1187 if (PtInRect(&bl
->rc
, pt
))
1191 n
-= bl
->nChars
+ bl
->nSkip
;
1199 /***********************************************************************
1201 * Determines the link the user clicked on.
1203 static LRESULT
SYSLINK_HitTest (const SYSLINK_INFO
*infoPtr
, PLHITTESTINFO HitTest
)
1208 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1210 if(Current
->Type
== slLink
)
1212 if(SYSLINK_PtInDocItem(Current
, HitTest
->pt
))
1214 HitTest
->item
.mask
= 0;
1215 HitTest
->item
.iLink
= id
;
1216 HitTest
->item
.state
= 0;
1217 HitTest
->item
.stateMask
= 0;
1218 if(Current
->u
.Link
.szID
)
1220 lstrcpyW(HitTest
->item
.szID
, Current
->u
.Link
.szID
);
1224 HitTest
->item
.szID
[0] = 0;
1226 if(Current
->u
.Link
.szUrl
)
1228 lstrcpyW(HitTest
->item
.szUrl
, Current
->u
.Link
.szUrl
);
1232 HitTest
->item
.szUrl
[0] = 0;
1243 /***********************************************************************
1244 * SYSLINK_GetIdealHeight
1245 * Returns the preferred height of a link at the current control's width.
1247 static LRESULT
SYSLINK_GetIdealHeight (const SYSLINK_INFO
*infoPtr
)
1249 HDC hdc
= GetDC(infoPtr
->Self
);
1254 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1256 if(GetTextMetricsW(hdc
, &tm
))
1258 height
= tm
.tmHeight
;
1264 SelectObject(hdc
, hOldFont
);
1265 ReleaseDC(infoPtr
->Self
, hdc
);
1272 /***********************************************************************
1273 * SYSLINK_SendParentNotify
1274 * Sends a WM_NOTIFY message to the parent window.
1276 static LRESULT
SYSLINK_SendParentNotify (const SYSLINK_INFO
*infoPtr
, UINT code
, const DOC_ITEM
*Link
, int iLink
)
1280 nml
.hdr
.hwndFrom
= infoPtr
->Self
;
1281 nml
.hdr
.idFrom
= GetWindowLongPtrW(infoPtr
->Self
, GWLP_ID
);
1282 nml
.hdr
.code
= code
;
1285 nml
.item
.iLink
= iLink
;
1287 nml
.item
.stateMask
= 0;
1288 if(Link
->u
.Link
.szID
)
1290 lstrcpyW(nml
.item
.szID
, Link
->u
.Link
.szID
);
1294 nml
.item
.szID
[0] = 0;
1296 if(Link
->u
.Link
.szUrl
)
1298 lstrcpyW(nml
.item
.szUrl
, Link
->u
.Link
.szUrl
);
1302 nml
.item
.szUrl
[0] = 0;
1305 return SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, nml
.hdr
.idFrom
, (LPARAM
)&nml
);
1308 /***********************************************************************
1310 * Handles receiving the input focus.
1312 static LRESULT
SYSLINK_SetFocus (SYSLINK_INFO
*infoPtr
)
1316 infoPtr
->HasFocus
= TRUE
;
1318 /* We always select the first link, even if we activated the control using
1319 SHIFT+TAB. This is the default behavior */
1320 Focus
= SYSLINK_GetNextLink(infoPtr
, NULL
);
1323 SYSLINK_SetFocusLink(infoPtr
, Focus
);
1324 SYSLINK_RepaintLink(infoPtr
, Focus
);
1329 /***********************************************************************
1331 * Handles losing the input focus.
1333 static LRESULT
SYSLINK_KillFocus (SYSLINK_INFO
*infoPtr
)
1337 infoPtr
->HasFocus
= FALSE
;
1338 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1342 SYSLINK_RepaintLink(infoPtr
, Focus
);
1348 /***********************************************************************
1350 * Returns a link at the specified position
1352 static PDOC_ITEM
SYSLINK_LinkAtPt (const SYSLINK_INFO
*infoPtr
, const POINT
*pt
, int *LinkId
, BOOL MustBeEnabled
)
1357 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1359 if((Current
->Type
== slLink
) && SYSLINK_PtInDocItem(Current
, *pt
) &&
1360 (!MustBeEnabled
|| (MustBeEnabled
&& (Current
->u
.Link
.state
& LIS_ENABLED
))))
1374 /***********************************************************************
1375 * SYSLINK_LButtonDown
1376 * Handles mouse clicks
1378 static LRESULT
SYSLINK_LButtonDown (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1380 PDOC_ITEM Current
, Old
;
1383 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1386 SetFocus(infoPtr
->Self
);
1388 Old
= SYSLINK_SetFocusLink(infoPtr
, Current
);
1389 if(Old
!= NULL
&& Old
!= Current
)
1391 SYSLINK_RepaintLink(infoPtr
, Old
);
1393 infoPtr
->MouseDownID
= id
;
1394 SYSLINK_RepaintLink(infoPtr
, Current
);
1400 /***********************************************************************
1402 * Handles mouse clicks
1404 static LRESULT
SYSLINK_LButtonUp (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1406 if(infoPtr
->MouseDownID
> -1)
1411 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1412 if((Current
!= NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && (infoPtr
->MouseDownID
== id
))
1414 SYSLINK_SendParentNotify(infoPtr
, NM_CLICK
, Current
, id
);
1418 infoPtr
->MouseDownID
= -1;
1423 /***********************************************************************
1425 * Handles ENTER key events
1427 static BOOL
SYSLINK_OnEnter (const SYSLINK_INFO
*infoPtr
)
1429 if(infoPtr
->HasFocus
&& !infoPtr
->IgnoreReturn
)
1434 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1437 SYSLINK_SendParentNotify(infoPtr
, NM_RETURN
, Focus
, id
);
1444 /***********************************************************************
1445 * SYSKEY_SelectNextPrevLink
1446 * Changes the currently focused link
1448 static BOOL
SYSKEY_SelectNextPrevLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1450 if(infoPtr
->HasFocus
)
1455 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1458 PDOC_ITEM NewFocus
, OldFocus
;
1461 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1463 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1465 if(NewFocus
!= NULL
)
1467 OldFocus
= SYSLINK_SetFocusLink(infoPtr
, NewFocus
);
1469 if(OldFocus
&& OldFocus
!= NewFocus
)
1471 SYSLINK_RepaintLink(infoPtr
, OldFocus
);
1473 SYSLINK_RepaintLink(infoPtr
, NewFocus
);
1481 /***********************************************************************
1482 * SYSKEY_SelectNextPrevLink
1483 * Determines if there's a next or previous link to decide whether the control
1484 * should capture the tab key message
1486 static BOOL
SYSLINK_NoNextLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1488 PDOC_ITEM Focus
, NewFocus
;
1490 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1492 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1494 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1496 return NewFocus
== NULL
;
1499 /***********************************************************************
1500 * SYSLINK_GetIdealSize
1501 * Calculates the ideal size of a link control at a given maximum width.
1503 static VOID
SYSLINK_GetIdealSize (const SYSLINK_INFO
*infoPtr
, int cxMaxWidth
, LPSIZE lpSize
)
1508 rc
.left
= rc
.top
= rc
.bottom
= 0;
1509 rc
.right
= cxMaxWidth
;
1511 hdc
= GetDC(infoPtr
->Self
);
1514 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1516 SYSLINK_Render(infoPtr
, hdc
, &rc
);
1518 SelectObject(hdc
, hOldFont
);
1519 ReleaseDC(infoPtr
->Self
, hdc
);
1521 lpSize
->cx
= rc
.right
;
1522 lpSize
->cy
= rc
.bottom
;
1526 /***********************************************************************
1529 static LRESULT WINAPI
SysLinkWindowProc(HWND hwnd
, UINT message
,
1530 WPARAM wParam
, LPARAM lParam
)
1532 SYSLINK_INFO
*infoPtr
;
1534 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd
, message
, wParam
, lParam
);
1536 infoPtr
= (SYSLINK_INFO
*)GetWindowLongPtrW(hwnd
, 0);
1538 if (!infoPtr
&& message
!= WM_CREATE
)
1539 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1542 case WM_PRINTCLIENT
:
1544 return SYSLINK_Paint (infoPtr
, (HDC
)wParam
);
1547 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
1549 HDC hdc
= (HDC
)wParam
;
1550 HBRUSH brush
= CreateSolidBrush( comctl32_color
.clrWindow
);
1553 GetClipBox( hdc
, &rect
);
1554 FillRect( hdc
, &rect
, brush
);
1555 DeleteObject( brush
);
1563 DWORD mp
= GetMessagePos();
1565 ht
.pt
.x
= (short)LOWORD(mp
);
1566 ht
.pt
.y
= (short)HIWORD(mp
);
1568 ScreenToClient(infoPtr
->Self
, &ht
.pt
);
1569 if(SYSLINK_HitTest (infoPtr
, &ht
))
1571 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_HAND
));
1575 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1581 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1583 HDC hdc
= GetDC(infoPtr
->Self
);
1586 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1587 ReleaseDC(infoPtr
->Self
, hdc
);
1594 return (LRESULT
)infoPtr
->Font
;
1597 return (LRESULT
)SYSLINK_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
1600 SYSLINK_SetText(infoPtr
, (LPWSTR
)lParam
);
1601 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1603 case WM_LBUTTONDOWN
:
1606 pt
.x
= (short)LOWORD(lParam
);
1607 pt
.y
= (short)HIWORD(lParam
);
1608 return SYSLINK_LButtonDown(infoPtr
, &pt
);
1613 pt
.x
= (short)LOWORD(lParam
);
1614 pt
.y
= (short)HIWORD(lParam
);
1615 return SYSLINK_LButtonUp(infoPtr
, &pt
);
1623 SYSLINK_OnEnter(infoPtr
);
1627 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1628 SYSKEY_SelectNextPrevLink(infoPtr
, shift
);
1632 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1638 LRESULT Ret
= DLGC_HASSETSEL
;
1639 int vk
= (lParam
!= 0 ? (int)((LPMSG
)lParam
)->wParam
: 0);
1643 Ret
|= DLGC_WANTMESSAGE
;
1647 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1648 if(!SYSLINK_NoNextLink(infoPtr
, shift
))
1650 Ret
|= DLGC_WANTTAB
;
1654 Ret
|= DLGC_WANTCHARS
;
1666 pt
.x
= (short)LOWORD(lParam
);
1667 pt
.y
= (short)HIWORD(lParam
);
1669 GetClientRect(infoPtr
->Self
, &rc
);
1670 ScreenToClient(infoPtr
->Self
, &pt
);
1671 if(pt
.x
< 0 || pt
.y
< 0 || pt
.x
> rc
.right
|| pt
.y
> rc
.bottom
)
1676 if(SYSLINK_LinkAtPt(infoPtr
, &pt
, NULL
, FALSE
))
1681 return HTTRANSPARENT
;
1685 return SYSLINK_HitTest(infoPtr
, (PLHITTESTINFO
)lParam
);
1688 return SYSLINK_SetItem(infoPtr
, (PLITEM
)lParam
);
1691 return SYSLINK_GetItem(infoPtr
, (PLITEM
)lParam
);
1693 case LM_GETIDEALHEIGHT
:
1696 /* LM_GETIDEALSIZE */
1697 SYSLINK_GetIdealSize(infoPtr
, (int)wParam
, (LPSIZE
)lParam
);
1699 return SYSLINK_GetIdealHeight(infoPtr
);
1702 return SYSLINK_SetFocus(infoPtr
);
1705 return SYSLINK_KillFocus(infoPtr
);
1708 infoPtr
->Style
&= ~WS_DISABLED
;
1709 infoPtr
->Style
|= (wParam
? 0 : WS_DISABLED
);
1710 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
1713 case WM_STYLECHANGED
:
1714 if (wParam
== GWL_STYLE
)
1716 infoPtr
->Style
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
1718 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1723 /* allocate memory for info struct */
1724 infoPtr
= Alloc (sizeof(SYSLINK_INFO
));
1725 if (!infoPtr
) return -1;
1726 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
1728 /* initialize the info struct */
1729 infoPtr
->Self
= hwnd
;
1730 infoPtr
->Notify
= ((LPCREATESTRUCTW
)lParam
)->hwndParent
;
1731 infoPtr
->Style
= ((LPCREATESTRUCTW
)lParam
)->style
;
1733 infoPtr
->LinkFont
= 0;
1734 infoPtr
->Items
= NULL
;
1735 infoPtr
->HasFocus
= FALSE
;
1736 infoPtr
->MouseDownID
= -1;
1737 infoPtr
->TextColor
= comctl32_color
.clrWindowText
;
1738 infoPtr
->LinkColor
= comctl32_color
.clrHighlight
;
1739 infoPtr
->VisitedColor
= comctl32_color
.clrHighlight
;
1740 infoPtr
->BreakChar
= ' ';
1741 infoPtr
->IgnoreReturn
= infoPtr
->Style
& LWS_IGNORERETURN
;
1742 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd
);
1743 SYSLINK_SetText(infoPtr
, ((LPCREATESTRUCTW
)lParam
)->lpszName
);
1747 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd
);
1748 SYSLINK_ClearDoc(infoPtr
);
1749 if(infoPtr
->Font
!= 0) DeleteObject(infoPtr
->Font
);
1750 if(infoPtr
->LinkFont
!= 0) DeleteObject(infoPtr
->LinkFont
);
1751 SetWindowLongPtrW(hwnd
, 0, 0);
1755 case WM_SYSCOLORCHANGE
:
1756 COMCTL32_RefreshSysColors();
1760 if ((message
>= WM_USER
) && (message
< WM_APP
) && !COMCTL32_IsReflectedMessage(message
))
1762 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message
, wParam
, lParam
);
1764 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1769 /***********************************************************************
1770 * SYSLINK_Register [Internal]
1772 * Registers the SysLink window class.
1774 VOID
SYSLINK_Register (void)
1778 ZeroMemory (&wndClass
, sizeof(wndClass
));
1779 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
1780 wndClass
.lpfnWndProc
= SysLinkWindowProc
;
1781 wndClass
.cbClsExtra
= 0;
1782 wndClass
.cbWndExtra
= sizeof (SYSLINK_INFO
*);
1783 wndClass
.hCursor
= LoadCursorW (0, (LPWSTR
)IDC_ARROW
);
1784 wndClass
.lpszClassName
= WC_LINK
;
1786 RegisterClassW (&wndClass
);
1790 /***********************************************************************
1791 * SYSLINK_Unregister [Internal]
1793 * Unregisters the SysLink window class.
1795 VOID
SYSLINK_Unregister (void)
1797 UnregisterClassW (WC_LINK
, NULL
);