4 * Copyright 2004 - 2006 Thomas Weidenmueller <w3seek@reactos.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * This code was audited for completeness against the documented features
23 * of Comctl32.dll version 6.0 on Apr. 4, 2005, by Dimitrie O. Paun.
25 * Unless otherwise noted, we believe this code to be complete, as per
26 * the specification mentioned above.
27 * If you discover missing features, or bugs, please note them below.
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(syslink
);
44 INT WINAPI
StrCmpNIW(LPCWSTR
,LPCWSTR
,INT
);
51 } DOC_TEXTBLOCK
, *PDOC_TEXTBLOCK
;
53 #define LIF_FLAGSMASK (LIF_STATE | LIF_ITEMID | LIF_URL)
54 #define LIS_MASK (LIS_FOCUSED | LIS_ENABLED | LIS_VISITED)
62 typedef struct _DOC_ITEM
64 struct _DOC_ITEM
*Next
; /* Address to the next item */
65 UINT nText
; /* Number of characters of the text */
66 SL_ITEM_TYPE Type
; /* type of the item */
67 PDOC_TEXTBLOCK Blocks
; /* Array of text blocks */
72 UINT state
; /* Link state */
73 WCHAR
*szID
; /* Link ID string */
74 WCHAR
*szUrl
; /* Link URL string */
81 WCHAR Text
[1]; /* Text of the document item */
82 } DOC_ITEM
, *PDOC_ITEM
;
86 HWND Self
; /* The window handle for this control */
87 HWND Notify
; /* The parent handle to receive notifications */
88 DWORD Style
; /* Styles for this control */
89 PDOC_ITEM Items
; /* Address to the first document item */
90 BOOL HasFocus
; /* Whether the control has the input focus */
91 int MouseDownID
; /* ID of the link that the mouse button first selected */
92 HFONT Font
; /* Handle to the font for text */
93 HFONT LinkFont
; /* Handle to the font for links */
94 COLORREF TextColor
; /* Color of the text */
95 COLORREF LinkColor
; /* Color of links */
96 COLORREF VisitedColor
; /* Color of visited links */
97 WCHAR BreakChar
; /* Break Character for the current font */
98 BOOL IgnoreReturn
; /* (infoPtr->Style & LWS_IGNORERETURN) on creation */
101 static const WCHAR SL_LINKOPEN
[] = { '<','a', 0 };
102 static const WCHAR SL_HREF
[] = { 'h','r','e','f','=','\"',0 };
103 static const WCHAR SL_ID
[] = { 'i','d','=','\"',0 };
104 static const WCHAR SL_LINKCLOSE
[] = { '<','/','a','>',0 };
106 /* Control configuration constants */
108 #define SL_LEFTMARGIN (0)
109 #define SL_TOPMARGIN (0)
110 #define SL_RIGHTMARGIN (0)
111 #define SL_BOTTOMMARGIN (0)
113 /***********************************************************************
114 * SYSLINK_FreeDocItem
115 * Frees all data and gdi objects associated with a document item
117 static VOID
SYSLINK_FreeDocItem (PDOC_ITEM DocItem
)
119 if(DocItem
->Type
== slLink
)
121 Free(DocItem
->u
.Link
.szID
);
122 Free(DocItem
->u
.Link
.szUrl
);
125 /* we don't free Text because it's just a pointer to a character in the
126 entire window text string */
131 /***********************************************************************
132 * SYSLINK_AppendDocItem
133 * Create and append a new document item.
135 static PDOC_ITEM
SYSLINK_AppendDocItem (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
, UINT textlen
,
136 SL_ITEM_TYPE type
, PDOC_ITEM LastItem
)
140 textlen
= min(textlen
, strlenW(Text
));
141 Item
= Alloc(FIELD_OFFSET(DOC_ITEM
, Text
[textlen
+ 1]));
144 ERR("Failed to alloc DOC_ITEM structure!\n");
149 Item
->nText
= textlen
;
155 LastItem
->Next
= Item
;
159 infoPtr
->Items
= Item
;
162 lstrcpynW(Item
->Text
, Text
, textlen
+ 1);
167 /***********************************************************************
169 * Clears the document tree
171 static VOID
SYSLINK_ClearDoc (SYSLINK_INFO
*infoPtr
)
173 PDOC_ITEM Item
, Next
;
175 Item
= infoPtr
->Items
;
179 SYSLINK_FreeDocItem(Item
);
183 infoPtr
->Items
= NULL
;
186 /***********************************************************************
188 * Wrapper for StrCmpNIW to ensure 'len' is not too big.
190 static INT
SYSLINK_StrCmpNIW (LPCWSTR str
, LPCWSTR comp
, INT len
)
194 for(i
= 0; i
< len
; i
++)
203 return StrCmpNIW(str
, comp
, len
);
206 /***********************************************************************
208 * Parses the window text string and creates a document. Returns the
209 * number of document items created.
211 static UINT
SYSLINK_ParseText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
213 LPCWSTR current
, textstart
= NULL
, linktext
= NULL
, firsttag
= NULL
;
214 int taglen
= 0, textlen
= 0, linklen
= 0, docitems
= 0;
215 PDOC_ITEM Last
= NULL
;
216 SL_ITEM_TYPE CurrentType
= slText
;
220 TRACE("(%p %s)\n", infoPtr
, debugstr_w(Text
));
222 for(current
= Text
; *current
!= 0;)
226 if(!SYSLINK_StrCmpNIW(current
, SL_LINKOPEN
, 2) && (CurrentType
== slText
))
228 BOOL ValidParam
= FALSE
, ValidLink
= FALSE
;
230 if(*(current
+ 2) == '>')
232 /* we just have to deal with a <a> tag */
241 else if(*(current
+ 2) == infoPtr
->BreakChar
)
243 /* we expect parameters, parse them */
244 LPCWSTR
*CurrentParameter
= NULL
, tmp
;
245 UINT
*CurrentParameterLen
= NULL
;
248 tmp
= current
+ taglen
;
253 /* compare the current position with all known parameters */
254 if(!SYSLINK_StrCmpNIW(tmp
, SL_HREF
, 6))
258 CurrentParameter
= &lpUrl
;
259 CurrentParameterLen
= &lenUrl
;
261 else if(!SYSLINK_StrCmpNIW(tmp
, SL_ID
, 4))
265 CurrentParameter
= &lpID
;
266 CurrentParameterLen
= &lenId
;
275 /* we got a known parameter, now search until the next " character.
276 If we can't find a " character, there's a syntax error and we just assume it's text */
278 *CurrentParameter
= current
+ taglen
;
279 *CurrentParameterLen
= 0;
281 for(tmp
= *CurrentParameter
; *tmp
!= 0; tmp
++)
290 (*CurrentParameterLen
)++;
295 /* we're done with this parameter, now there are only 2 possibilities:
296 * 1. another parameter is coming, so expect a ' ' (space) character
297 * 2. the tag is being closed, so expect a '<' character
299 if(*tmp
== infoPtr
->BreakChar
)
301 /* we expect another parameter, do the whole thing again */
308 /* the tag is being closed, we're done */
315 if(ValidLink
&& ValidParam
)
317 /* the <a ...> tag appears to be valid. save all information
318 so we can add the link if we find a valid </a> tag later */
319 CurrentType
= slLink
;
320 linktext
= current
+ taglen
;
329 if(textstart
== NULL
)
335 else if(!SYSLINK_StrCmpNIW(current
, SL_LINKCLOSE
, 4) && (CurrentType
== slLink
) && firsttag
)
337 /* there's a <a...> tag opened, first add the previous text, if present */
338 if(textstart
!= NULL
&& textlen
> 0 && firsttag
> textstart
)
340 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, firsttag
- textstart
, slText
, Last
);
343 ERR("Unable to create new document item!\n");
351 /* now it's time to add the link to the document */
353 if(linktext
!= NULL
&& linklen
> 0)
355 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slLink
, Last
);
358 ERR("Unable to create new document item!\n");
362 if(CurrentType
== slLink
)
366 if(!(infoPtr
->Style
& WS_DISABLED
))
368 Last
->u
.Link
.state
|= LIS_ENABLED
;
370 /* Copy the tag parameters */
373 nc
= min(lenId
, strlenW(lpID
));
374 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
375 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
376 if(Last
->u
.Link
.szID
!= NULL
)
378 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
382 Last
->u
.Link
.szID
= NULL
;
385 nc
= min(lenUrl
, strlenW(lpUrl
));
386 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
387 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
388 if(Last
->u
.Link
.szUrl
!= NULL
)
390 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
394 Last
->u
.Link
.szUrl
= NULL
;
398 CurrentType
= slText
;
405 /* we don't know what tag it is, so just continue */
408 if(CurrentType
== slText
&& textstart
== NULL
)
422 /* save the pointer of the current text item if we couldn't find a tag */
423 if(textstart
== NULL
&& CurrentType
== slText
)
432 if(textstart
!= NULL
&& textlen
> 0)
434 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, textlen
, CurrentType
, Last
);
437 ERR("Unable to create new document item!\n");
440 if(CurrentType
== slLink
)
444 if(!(infoPtr
->Style
& WS_DISABLED
))
446 Last
->u
.Link
.state
|= LIS_ENABLED
;
448 /* Copy the tag parameters */
451 nc
= min(lenId
, strlenW(lpID
));
452 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
453 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
454 if(Last
->u
.Link
.szID
!= NULL
)
456 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
460 Last
->u
.Link
.szID
= NULL
;
463 nc
= min(lenUrl
, strlenW(lpUrl
));
464 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
465 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
466 if(Last
->u
.Link
.szUrl
!= NULL
)
468 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
472 Last
->u
.Link
.szUrl
= NULL
;
477 if(linktext
!= NULL
&& linklen
> 0)
479 /* we got an unclosed link, just display the text */
480 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slText
, Last
);
483 ERR("Unable to create new document item!\n");
492 /***********************************************************************
493 * SYSLINK_RepaintLink
496 static VOID
SYSLINK_RepaintLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
501 if(DocItem
->Type
!= slLink
)
503 ERR("DocItem not a link!\n");
507 bl
= DocItem
->Blocks
;
514 InvalidateRect(infoPtr
->Self
, &bl
->rc
, TRUE
);
515 n
-= bl
->nChars
+ bl
->nSkip
;
521 /***********************************************************************
522 * SYSLINK_GetLinkItemByIndex
523 * Retrieves a document link by its index
525 static PDOC_ITEM
SYSLINK_GetLinkItemByIndex (const SYSLINK_INFO
*infoPtr
, int iLink
)
527 PDOC_ITEM Current
= infoPtr
->Items
;
529 while(Current
!= NULL
)
531 if((Current
->Type
== slLink
) && (iLink
-- <= 0))
535 Current
= Current
->Next
;
540 /***********************************************************************
541 * SYSLINK_GetFocusLink
542 * Retrieves the link that has the LIS_FOCUSED bit
544 static PDOC_ITEM
SYSLINK_GetFocusLink (const SYSLINK_INFO
*infoPtr
, int *LinkId
)
546 PDOC_ITEM Current
= infoPtr
->Items
;
549 while(Current
!= NULL
)
551 if(Current
->Type
== slLink
)
553 if(Current
->u
.Link
.state
& LIS_FOCUSED
)
561 Current
= Current
->Next
;
566 /***********************************************************************
567 * SYSLINK_GetNextLink
570 static PDOC_ITEM
SYSLINK_GetNextLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
572 for(Current
= (Current
!= NULL
? Current
->Next
: infoPtr
->Items
);
574 Current
= Current
->Next
)
576 if(Current
->Type
== slLink
)
584 /***********************************************************************
585 * SYSLINK_GetPrevLink
586 * Gets the previous link
588 static PDOC_ITEM
SYSLINK_GetPrevLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
592 /* returns the last link */
593 PDOC_ITEM Last
= NULL
;
595 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
597 if(Current
->Type
== slLink
)
606 /* returns the previous link */
607 PDOC_ITEM Cur
, Prev
= NULL
;
609 for(Cur
= infoPtr
->Items
; Cur
!= NULL
; Cur
= Cur
->Next
)
615 if(Cur
->Type
== slLink
)
624 /***********************************************************************
626 * Tries to wrap a line.
628 static BOOL
SYSLINK_WrapLine (LPWSTR Text
, WCHAR BreakChar
, int x
, int *LineLen
,
629 int nFit
, LPSIZE Extent
)
633 for (i
= 0; i
< nFit
; i
++) if (Text
[i
] == '\n') break;
635 if (i
== *LineLen
) return FALSE
;
637 /* check if we're in the middle of a word */
638 if (Text
[i
] != '\n' && Text
[i
] != BreakChar
)
640 /* search for the beginning of the word */
641 while (i
&& Text
[i
- 1] != BreakChar
) i
--;
647 if (x
== SL_LEFTMARGIN
) i
= max( nFit
, 1 );
654 /***********************************************************************
656 * Renders the document in memory
658 static VOID
SYSLINK_Render (const SYSLINK_INFO
*infoPtr
, HDC hdc
, PRECT pRect
)
663 int x
, y
, LineHeight
;
667 szDoc
.cx
= szDoc
.cy
= 0;
670 rc
.right
-= SL_RIGHTMARGIN
;
671 rc
.bottom
-= SL_BOTTOMMARGIN
;
673 if(rc
.right
- SL_LEFTMARGIN
< 0)
675 if (rc
.bottom
- SL_TOPMARGIN
< 0)
678 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
682 GetTextMetricsW( hdc
, &tm
);
683 LineHeight
= tm
.tmHeight
+ tm
.tmExternalLeading
;
685 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
689 PDOC_TEXTBLOCK bl
, cbl
;
694 if(Current
->nText
== 0)
702 Free(Current
->Blocks
);
703 Current
->Blocks
= NULL
;
707 if(Current
->Type
== slText
)
709 SelectObject(hdc
, infoPtr
->Font
);
711 else if(Current
->Type
== slLink
)
713 SelectObject(hdc
, infoPtr
->LinkFont
);
718 /* skip break characters unless they're the first of the doc item */
719 if(tx
!= Current
->Text
|| x
== SL_LEFTMARGIN
)
721 if (n
&& *tx
== '\n')
727 while(n
> 0 && (*tx
) == infoPtr
->BreakChar
)
735 if((n
== 0 && SkipChars
!= 0) ||
736 GetTextExtentExPointW(hdc
, tx
, n
, rc
.right
- x
, &nFit
, NULL
, &szDim
))
744 Wrap
= SYSLINK_WrapLine(tx
, infoPtr
->BreakChar
, x
, &LineLen
, nFit
, &szDim
);
748 /* move one line down, the word didn't fit into the line */
756 if(!GetTextExtentExPointW(hdc
, tx
, LineLen
, rc
.right
- x
, NULL
, NULL
, &szDim
))
769 nbl
= ReAlloc(bl
, (nBlocks
+ 1) * sizeof(DOC_TEXTBLOCK
));
775 cbl
= bl
+ nBlocks
- 1;
777 cbl
->nChars
= LineLen
;
778 cbl
->nSkip
= SkipChars
;
781 cbl
->rc
.right
= x
+ szDim
.cx
;
782 cbl
->rc
.bottom
= y
+ szDim
.cy
;
784 if (cbl
->rc
.right
> szDoc
.cx
)
785 szDoc
.cx
= cbl
->rc
.right
;
786 if (cbl
->rc
.bottom
> szDoc
.cy
)
787 szDoc
.cy
= cbl
->rc
.bottom
;
805 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
820 Current
->Blocks
= bl
;
824 SelectObject(hdc
, hOldFont
);
826 pRect
->right
= pRect
->left
+ szDoc
.cx
;
827 pRect
->bottom
= pRect
->top
+ szDoc
.cy
;
830 /***********************************************************************
832 * Draws the SysLink control.
834 static LRESULT
SYSLINK_Draw (const SYSLINK_INFO
*infoPtr
, HDC hdc
)
839 COLORREF OldTextColor
, OldBkColor
;
841 UINT text_flags
= ETO_CLIPPED
;
842 UINT mode
= GetBkMode( hdc
);
844 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
845 OldTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
846 OldBkColor
= SetBkColor(hdc
, comctl32_color
.clrWindow
);
848 GetClientRect(infoPtr
->Self
, &rc
);
849 rc
.right
-= SL_RIGHTMARGIN
+ SL_LEFTMARGIN
;
850 rc
.bottom
-= SL_BOTTOMMARGIN
+ SL_TOPMARGIN
;
852 if(rc
.right
< 0 || rc
.bottom
< 0) return 0;
854 hBrush
= (HBRUSH
)SendMessageW(infoPtr
->Notify
, WM_CTLCOLORSTATIC
,
855 (WPARAM
)hdc
, (LPARAM
)infoPtr
->Self
);
856 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
858 FillRect(hdc
, &rc
, hBrush
);
859 if (GetBkMode( hdc
) == OPAQUE
) text_flags
|= ETO_OPAQUE
;
861 else SetBkMode( hdc
, TRANSPARENT
);
863 DeleteObject(hBrush
);
865 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
871 bl
= Current
->Blocks
;
877 if(Current
->Type
== slText
)
879 SelectObject(hdc
, infoPtr
->Font
);
880 SetTextColor(hdc
, infoPtr
->TextColor
);
884 SelectObject(hdc
, infoPtr
->LinkFont
);
885 SetTextColor(hdc
, (!(Current
->u
.Link
.state
& LIS_VISITED
) ? infoPtr
->LinkColor
: infoPtr
->VisitedColor
));
891 ExtTextOutW(hdc
, bl
->rc
.left
, bl
->rc
.top
, text_flags
, &bl
->rc
, tx
, bl
->nChars
, NULL
);
892 if((Current
->Type
== slLink
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && infoPtr
->HasFocus
)
894 COLORREF PrevTextColor
;
895 PrevTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
896 DrawFocusRect(hdc
, &bl
->rc
);
897 SetTextColor(hdc
, PrevTextColor
);
900 n
-= bl
->nChars
+ bl
->nSkip
;
906 SetBkColor(hdc
, OldBkColor
);
907 SetTextColor(hdc
, OldTextColor
);
908 SelectObject(hdc
, hOldFont
);
909 SetBkMode(hdc
, mode
);
914 /***********************************************************************
916 * Handles the WM_PAINT message.
918 static LRESULT
SYSLINK_Paint (const SYSLINK_INFO
*infoPtr
, HDC hdcParam
)
923 hdc
= hdcParam
? hdcParam
: BeginPaint (infoPtr
->Self
, &ps
);
926 SYSLINK_Draw (infoPtr
, hdc
);
927 if (!hdcParam
) EndPaint (infoPtr
->Self
, &ps
);
932 /***********************************************************************
934 * Set new Font for the SysLink control.
936 static HFONT
SYSLINK_SetFont (SYSLINK_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
942 HFONT hOldFont
= infoPtr
->Font
;
943 infoPtr
->Font
= hFont
;
945 /* free the underline font */
946 if(infoPtr
->LinkFont
!= NULL
)
948 DeleteObject(infoPtr
->LinkFont
);
949 infoPtr
->LinkFont
= NULL
;
952 /* Render text position and word wrapping in memory */
953 if (GetClientRect(infoPtr
->Self
, &rcClient
))
955 hdc
= GetDC(infoPtr
->Self
);
958 /* create a new underline font */
959 if(GetTextMetricsW(hdc
, &tm
) &&
960 GetObjectW(infoPtr
->Font
, sizeof(LOGFONTW
), &lf
))
962 lf
.lfUnderline
= TRUE
;
963 infoPtr
->LinkFont
= CreateFontIndirectW(&lf
);
964 infoPtr
->BreakChar
= tm
.tmBreakChar
;
968 ERR("Failed to create link font!\n");
971 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
972 ReleaseDC(infoPtr
->Self
, hdc
);
978 RedrawWindow(infoPtr
->Self
, NULL
, NULL
, RDW_INVALIDATE
| RDW_UPDATENOW
);
984 /***********************************************************************
986 * Set new text for the SysLink control.
988 static LRESULT
SYSLINK_SetText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
990 /* clear the document */
991 SYSLINK_ClearDoc(infoPtr
);
993 if(Text
== NULL
|| *Text
== 0)
998 /* let's parse the string and create a document */
999 if(SYSLINK_ParseText(infoPtr
, Text
) > 0)
1003 /* Render text position and word wrapping in memory */
1004 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1006 HDC hdc
= GetDC(infoPtr
->Self
);
1009 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1010 ReleaseDC(infoPtr
->Self
, hdc
);
1012 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1020 /***********************************************************************
1021 * SYSLINK_SetFocusLink
1022 * Updates the focus status bits and focusses the specified link.
1023 * If no document item is specified, the focus bit will be removed from all links.
1024 * Returns the previous focused item.
1026 static PDOC_ITEM
SYSLINK_SetFocusLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
1028 PDOC_ITEM Current
, PrevFocus
= NULL
;
1030 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1032 if(Current
->Type
== slLink
)
1034 if((PrevFocus
== NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
))
1036 PrevFocus
= Current
;
1039 if(Current
== DocItem
)
1041 Current
->u
.Link
.state
|= LIS_FOCUSED
;
1045 Current
->u
.Link
.state
&= ~LIS_FOCUSED
;
1053 /***********************************************************************
1055 * Sets the states and attributes of a link item.
1057 static LRESULT
SYSLINK_SetItem (const SYSLINK_INFO
*infoPtr
, const LITEM
*Item
)
1063 BOOL Repaint
= FALSE
;
1065 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1067 ERR("Invalid Flags!\n");
1071 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1074 ERR("Link %d couldn't be found\n", Item
->iLink
);
1078 if(Item
->mask
& LIF_ITEMID
)
1080 nc
= min(lstrlenW(Item
->szID
), MAX_LINKID_TEXT
- 1);
1081 szId
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1084 lstrcpynW(szId
, Item
->szID
, nc
+ 1);
1088 ERR("Unable to allocate memory for link id\n");
1093 if(Item
->mask
& LIF_URL
)
1095 nc
= min(lstrlenW(Item
->szUrl
), L_MAX_URL_LENGTH
- 1);
1096 szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1099 lstrcpynW(szUrl
, Item
->szUrl
, nc
+ 1);
1105 ERR("Unable to allocate memory for link url\n");
1110 if(Item
->mask
& LIF_ITEMID
)
1112 Free(di
->u
.Link
.szID
);
1113 di
->u
.Link
.szID
= szId
;
1116 if(Item
->mask
& LIF_URL
)
1118 Free(di
->u
.Link
.szUrl
);
1119 di
->u
.Link
.szUrl
= szUrl
;
1122 if(Item
->mask
& LIF_STATE
)
1124 UINT oldstate
= di
->u
.Link
.state
;
1125 /* clear the masked bits */
1126 di
->u
.Link
.state
&= ~(Item
->stateMask
& LIS_MASK
);
1128 di
->u
.Link
.state
|= (Item
->state
& Item
->stateMask
) & LIS_MASK
;
1129 Repaint
= (oldstate
!= di
->u
.Link
.state
);
1131 /* update the focus */
1132 SYSLINK_SetFocusLink(infoPtr
, ((di
->u
.Link
.state
& LIS_FOCUSED
) ? di
: NULL
));
1137 SYSLINK_RepaintLink(infoPtr
, di
);
1143 /***********************************************************************
1145 * Retrieves the states and attributes of a link item.
1147 static LRESULT
SYSLINK_GetItem (const SYSLINK_INFO
*infoPtr
, PLITEM Item
)
1151 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1153 ERR("Invalid Flags!\n");
1157 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1160 ERR("Link %d couldn't be found\n", Item
->iLink
);
1164 if(Item
->mask
& LIF_STATE
)
1166 Item
->state
= (di
->u
.Link
.state
& Item
->stateMask
);
1167 if(!infoPtr
->HasFocus
)
1169 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1170 Item
->state
&= ~LIS_FOCUSED
;
1174 if(Item
->mask
& LIF_ITEMID
)
1178 lstrcpyW(Item
->szID
, di
->u
.Link
.szID
);
1186 if(Item
->mask
& LIF_URL
)
1188 if(di
->u
.Link
.szUrl
)
1190 lstrcpyW(Item
->szUrl
, di
->u
.Link
.szUrl
);
1201 /***********************************************************************
1202 * SYSLINK_PtInDocItem
1203 * Determines if a point is in the region of a document item
1205 static BOOL
SYSLINK_PtInDocItem (const DOC_ITEM
*DocItem
, POINT pt
)
1210 bl
= DocItem
->Blocks
;
1217 if (PtInRect(&bl
->rc
, pt
))
1221 n
-= bl
->nChars
+ bl
->nSkip
;
1229 /***********************************************************************
1231 * Determines the link the user clicked on.
1233 static LRESULT
SYSLINK_HitTest (const SYSLINK_INFO
*infoPtr
, PLHITTESTINFO HitTest
)
1238 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1240 if(Current
->Type
== slLink
)
1242 if(SYSLINK_PtInDocItem(Current
, HitTest
->pt
))
1244 HitTest
->item
.mask
= 0;
1245 HitTest
->item
.iLink
= id
;
1246 HitTest
->item
.state
= 0;
1247 HitTest
->item
.stateMask
= 0;
1248 if(Current
->u
.Link
.szID
)
1250 lstrcpyW(HitTest
->item
.szID
, Current
->u
.Link
.szID
);
1254 HitTest
->item
.szID
[0] = 0;
1256 if(Current
->u
.Link
.szUrl
)
1258 lstrcpyW(HitTest
->item
.szUrl
, Current
->u
.Link
.szUrl
);
1262 HitTest
->item
.szUrl
[0] = 0;
1273 /***********************************************************************
1274 * SYSLINK_GetIdealHeight
1275 * Returns the preferred height of a link at the current control's width.
1277 static LRESULT
SYSLINK_GetIdealHeight (const SYSLINK_INFO
*infoPtr
)
1279 HDC hdc
= GetDC(infoPtr
->Self
);
1284 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1286 if(GetTextMetricsW(hdc
, &tm
))
1288 height
= tm
.tmHeight
;
1294 SelectObject(hdc
, hOldFont
);
1295 ReleaseDC(infoPtr
->Self
, hdc
);
1302 /***********************************************************************
1303 * SYSLINK_SendParentNotify
1304 * Sends a WM_NOTIFY message to the parent window.
1306 static LRESULT
SYSLINK_SendParentNotify (const SYSLINK_INFO
*infoPtr
, UINT code
, const DOC_ITEM
*Link
, int iLink
)
1310 nml
.hdr
.hwndFrom
= infoPtr
->Self
;
1311 nml
.hdr
.idFrom
= GetWindowLongPtrW(infoPtr
->Self
, GWLP_ID
);
1312 nml
.hdr
.code
= code
;
1315 nml
.item
.iLink
= iLink
;
1317 nml
.item
.stateMask
= 0;
1318 if(Link
->u
.Link
.szID
)
1320 lstrcpyW(nml
.item
.szID
, Link
->u
.Link
.szID
);
1324 nml
.item
.szID
[0] = 0;
1326 if(Link
->u
.Link
.szUrl
)
1328 lstrcpyW(nml
.item
.szUrl
, Link
->u
.Link
.szUrl
);
1332 nml
.item
.szUrl
[0] = 0;
1335 return SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, nml
.hdr
.idFrom
, (LPARAM
)&nml
);
1338 /***********************************************************************
1340 * Handles receiving the input focus.
1342 static LRESULT
SYSLINK_SetFocus (SYSLINK_INFO
*infoPtr
)
1346 infoPtr
->HasFocus
= TRUE
;
1348 /* We always select the first link, even if we activated the control using
1349 SHIFT+TAB. This is the default behavior */
1350 Focus
= SYSLINK_GetNextLink(infoPtr
, NULL
);
1353 SYSLINK_SetFocusLink(infoPtr
, Focus
);
1354 SYSLINK_RepaintLink(infoPtr
, Focus
);
1359 /***********************************************************************
1361 * Handles losing the input focus.
1363 static LRESULT
SYSLINK_KillFocus (SYSLINK_INFO
*infoPtr
)
1367 infoPtr
->HasFocus
= FALSE
;
1368 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1372 SYSLINK_RepaintLink(infoPtr
, Focus
);
1378 /***********************************************************************
1380 * Returns a link at the specified position
1382 static PDOC_ITEM
SYSLINK_LinkAtPt (const SYSLINK_INFO
*infoPtr
, const POINT
*pt
, int *LinkId
, BOOL MustBeEnabled
)
1387 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1389 if((Current
->Type
== slLink
) && SYSLINK_PtInDocItem(Current
, *pt
) &&
1390 (!MustBeEnabled
|| (MustBeEnabled
&& (Current
->u
.Link
.state
& LIS_ENABLED
))))
1404 /***********************************************************************
1405 * SYSLINK_LButtonDown
1406 * Handles mouse clicks
1408 static LRESULT
SYSLINK_LButtonDown (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1410 PDOC_ITEM Current
, Old
;
1413 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1416 SetFocus(infoPtr
->Self
);
1418 Old
= SYSLINK_SetFocusLink(infoPtr
, Current
);
1419 if(Old
!= NULL
&& Old
!= Current
)
1421 SYSLINK_RepaintLink(infoPtr
, Old
);
1423 infoPtr
->MouseDownID
= id
;
1424 SYSLINK_RepaintLink(infoPtr
, Current
);
1430 /***********************************************************************
1432 * Handles mouse clicks
1434 static LRESULT
SYSLINK_LButtonUp (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1436 if(infoPtr
->MouseDownID
> -1)
1441 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1442 if((Current
!= NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && (infoPtr
->MouseDownID
== id
))
1444 SYSLINK_SendParentNotify(infoPtr
, NM_CLICK
, Current
, id
);
1448 infoPtr
->MouseDownID
= -1;
1453 /***********************************************************************
1455 * Handles ENTER key events
1457 static BOOL
SYSLINK_OnEnter (const SYSLINK_INFO
*infoPtr
)
1459 if(infoPtr
->HasFocus
&& !infoPtr
->IgnoreReturn
)
1464 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1467 SYSLINK_SendParentNotify(infoPtr
, NM_RETURN
, Focus
, id
);
1474 /***********************************************************************
1475 * SYSKEY_SelectNextPrevLink
1476 * Changes the currently focused link
1478 static BOOL
SYSKEY_SelectNextPrevLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1480 if(infoPtr
->HasFocus
)
1485 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1488 PDOC_ITEM NewFocus
, OldFocus
;
1491 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1493 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1495 if(NewFocus
!= NULL
)
1497 OldFocus
= SYSLINK_SetFocusLink(infoPtr
, NewFocus
);
1499 if(OldFocus
&& OldFocus
!= NewFocus
)
1501 SYSLINK_RepaintLink(infoPtr
, OldFocus
);
1503 SYSLINK_RepaintLink(infoPtr
, NewFocus
);
1511 /***********************************************************************
1512 * SYSKEY_SelectNextPrevLink
1513 * Determines if there's a next or previous link to decide whether the control
1514 * should capture the tab key message
1516 static BOOL
SYSLINK_NoNextLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1518 PDOC_ITEM Focus
, NewFocus
;
1520 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1522 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1524 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1526 return NewFocus
== NULL
;
1529 /***********************************************************************
1530 * SYSLINK_GetIdealSize
1531 * Calculates the ideal size of a link control at a given maximum width.
1533 static VOID
SYSLINK_GetIdealSize (const SYSLINK_INFO
*infoPtr
, int cxMaxWidth
, LPSIZE lpSize
)
1538 rc
.left
= rc
.top
= rc
.bottom
= 0;
1539 rc
.right
= cxMaxWidth
;
1541 hdc
= GetDC(infoPtr
->Self
);
1544 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1546 SYSLINK_Render(infoPtr
, hdc
, &rc
);
1548 SelectObject(hdc
, hOldFont
);
1549 ReleaseDC(infoPtr
->Self
, hdc
);
1551 lpSize
->cx
= rc
.right
;
1552 lpSize
->cy
= rc
.bottom
;
1556 /***********************************************************************
1559 static LRESULT WINAPI
SysLinkWindowProc(HWND hwnd
, UINT message
,
1560 WPARAM wParam
, LPARAM lParam
)
1562 SYSLINK_INFO
*infoPtr
;
1564 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd
, message
, wParam
, lParam
);
1566 infoPtr
= (SYSLINK_INFO
*)GetWindowLongPtrW(hwnd
, 0);
1568 if (!infoPtr
&& message
!= WM_CREATE
)
1569 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1572 case WM_PRINTCLIENT
:
1574 return SYSLINK_Paint (infoPtr
, (HDC
)wParam
);
1577 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
1579 HDC hdc
= (HDC
)wParam
;
1580 HBRUSH brush
= CreateSolidBrush( comctl32_color
.clrWindow
);
1583 GetClipBox( hdc
, &rect
);
1584 FillRect( hdc
, &rect
, brush
);
1585 DeleteObject( brush
);
1593 DWORD mp
= GetMessagePos();
1595 ht
.pt
.x
= (short)LOWORD(mp
);
1596 ht
.pt
.y
= (short)HIWORD(mp
);
1598 ScreenToClient(infoPtr
->Self
, &ht
.pt
);
1599 if(SYSLINK_HitTest (infoPtr
, &ht
))
1601 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_HAND
));
1605 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1611 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1613 HDC hdc
= GetDC(infoPtr
->Self
);
1616 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1617 ReleaseDC(infoPtr
->Self
, hdc
);
1624 return (LRESULT
)infoPtr
->Font
;
1627 return (LRESULT
)SYSLINK_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
1630 SYSLINK_SetText(infoPtr
, (LPWSTR
)lParam
);
1631 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1633 case WM_LBUTTONDOWN
:
1636 pt
.x
= (short)LOWORD(lParam
);
1637 pt
.y
= (short)HIWORD(lParam
);
1638 return SYSLINK_LButtonDown(infoPtr
, &pt
);
1643 pt
.x
= (short)LOWORD(lParam
);
1644 pt
.y
= (short)HIWORD(lParam
);
1645 return SYSLINK_LButtonUp(infoPtr
, &pt
);
1653 SYSLINK_OnEnter(infoPtr
);
1657 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1658 SYSKEY_SelectNextPrevLink(infoPtr
, shift
);
1662 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1668 LRESULT Ret
= DLGC_HASSETSEL
;
1669 int vk
= (lParam
!= 0 ? (int)((LPMSG
)lParam
)->wParam
: 0);
1673 Ret
|= DLGC_WANTMESSAGE
;
1677 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1678 if(!SYSLINK_NoNextLink(infoPtr
, shift
))
1680 Ret
|= DLGC_WANTTAB
;
1684 Ret
|= DLGC_WANTCHARS
;
1696 pt
.x
= (short)LOWORD(lParam
);
1697 pt
.y
= (short)HIWORD(lParam
);
1699 GetClientRect(infoPtr
->Self
, &rc
);
1700 ScreenToClient(infoPtr
->Self
, &pt
);
1701 if(pt
.x
< 0 || pt
.y
< 0 || pt
.x
> rc
.right
|| pt
.y
> rc
.bottom
)
1706 if(SYSLINK_LinkAtPt(infoPtr
, &pt
, NULL
, FALSE
))
1711 return HTTRANSPARENT
;
1715 return SYSLINK_HitTest(infoPtr
, (PLHITTESTINFO
)lParam
);
1718 return SYSLINK_SetItem(infoPtr
, (PLITEM
)lParam
);
1721 return SYSLINK_GetItem(infoPtr
, (PLITEM
)lParam
);
1723 case LM_GETIDEALHEIGHT
:
1726 /* LM_GETIDEALSIZE */
1727 SYSLINK_GetIdealSize(infoPtr
, (int)wParam
, (LPSIZE
)lParam
);
1729 return SYSLINK_GetIdealHeight(infoPtr
);
1732 return SYSLINK_SetFocus(infoPtr
);
1735 return SYSLINK_KillFocus(infoPtr
);
1738 infoPtr
->Style
&= ~WS_DISABLED
;
1739 infoPtr
->Style
|= (wParam
? 0 : WS_DISABLED
);
1740 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
1743 case WM_STYLECHANGED
:
1744 if (wParam
== GWL_STYLE
)
1746 infoPtr
->Style
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
1748 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1753 /* allocate memory for info struct */
1754 infoPtr
= Alloc (sizeof(SYSLINK_INFO
));
1755 if (!infoPtr
) return -1;
1756 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
1758 /* initialize the info struct */
1759 infoPtr
->Self
= hwnd
;
1760 infoPtr
->Notify
= ((LPCREATESTRUCTW
)lParam
)->hwndParent
;
1761 infoPtr
->Style
= ((LPCREATESTRUCTW
)lParam
)->style
;
1763 infoPtr
->LinkFont
= 0;
1764 infoPtr
->Items
= NULL
;
1765 infoPtr
->HasFocus
= FALSE
;
1766 infoPtr
->MouseDownID
= -1;
1767 infoPtr
->TextColor
= comctl32_color
.clrWindowText
;
1768 infoPtr
->LinkColor
= comctl32_color
.clrHighlight
;
1769 infoPtr
->VisitedColor
= comctl32_color
.clrHighlight
;
1770 infoPtr
->BreakChar
= ' ';
1771 infoPtr
->IgnoreReturn
= infoPtr
->Style
& LWS_IGNORERETURN
;
1772 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd
);
1773 SYSLINK_SetText(infoPtr
, ((LPCREATESTRUCTW
)lParam
)->lpszName
);
1777 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd
);
1778 SYSLINK_ClearDoc(infoPtr
);
1779 if(infoPtr
->Font
!= 0) DeleteObject(infoPtr
->Font
);
1780 if(infoPtr
->LinkFont
!= 0) DeleteObject(infoPtr
->LinkFont
);
1781 SetWindowLongPtrW(hwnd
, 0, 0);
1785 case WM_SYSCOLORCHANGE
:
1786 COMCTL32_RefreshSysColors();
1790 if ((message
>= WM_USER
) && (message
< WM_APP
) && !COMCTL32_IsReflectedMessage(message
))
1792 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message
, wParam
, lParam
);
1794 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1799 /***********************************************************************
1800 * SYSLINK_Register [Internal]
1802 * Registers the SysLink window class.
1804 VOID
SYSLINK_Register (void)
1808 ZeroMemory (&wndClass
, sizeof(wndClass
));
1809 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
1810 wndClass
.lpfnWndProc
= SysLinkWindowProc
;
1811 wndClass
.cbClsExtra
= 0;
1812 wndClass
.cbWndExtra
= sizeof (SYSLINK_INFO
*);
1813 wndClass
.hCursor
= LoadCursorW (0, (LPWSTR
)IDC_ARROW
);
1814 wndClass
.lpszClassName
= WC_LINK
;
1816 RegisterClassW (&wndClass
);
1820 /***********************************************************************
1821 * SYSLINK_Unregister [Internal]
1823 * Unregisters the SysLink window class.
1825 VOID
SYSLINK_Unregister (void)
1827 UnregisterClassW (WC_LINK
, NULL
);