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
;
779 SetRect(&cbl
->rc
, x
, y
, x
+ szDim
.cx
, y
+ szDim
.cy
);
781 if (cbl
->rc
.right
> szDoc
.cx
)
782 szDoc
.cx
= cbl
->rc
.right
;
783 if (cbl
->rc
.bottom
> szDoc
.cy
)
784 szDoc
.cy
= cbl
->rc
.bottom
;
802 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
817 Current
->Blocks
= bl
;
821 SelectObject(hdc
, hOldFont
);
823 pRect
->right
= pRect
->left
+ szDoc
.cx
;
824 pRect
->bottom
= pRect
->top
+ szDoc
.cy
;
827 /***********************************************************************
829 * Draws the SysLink control.
831 static LRESULT
SYSLINK_Draw (const SYSLINK_INFO
*infoPtr
, HDC hdc
)
836 COLORREF OldTextColor
, OldBkColor
;
838 UINT text_flags
= ETO_CLIPPED
;
839 UINT mode
= GetBkMode( hdc
);
841 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
842 OldTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
843 OldBkColor
= SetBkColor(hdc
, comctl32_color
.clrWindow
);
845 GetClientRect(infoPtr
->Self
, &rc
);
846 rc
.right
-= SL_RIGHTMARGIN
+ SL_LEFTMARGIN
;
847 rc
.bottom
-= SL_BOTTOMMARGIN
+ SL_TOPMARGIN
;
849 if(rc
.right
< 0 || rc
.bottom
< 0) return 0;
851 hBrush
= (HBRUSH
)SendMessageW(infoPtr
->Notify
, WM_CTLCOLORSTATIC
,
852 (WPARAM
)hdc
, (LPARAM
)infoPtr
->Self
);
853 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
855 FillRect(hdc
, &rc
, hBrush
);
856 if (GetBkMode( hdc
) == OPAQUE
) text_flags
|= ETO_OPAQUE
;
858 else SetBkMode( hdc
, TRANSPARENT
);
860 DeleteObject(hBrush
);
862 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
868 bl
= Current
->Blocks
;
874 if(Current
->Type
== slText
)
876 SelectObject(hdc
, infoPtr
->Font
);
877 SetTextColor(hdc
, infoPtr
->TextColor
);
881 SelectObject(hdc
, infoPtr
->LinkFont
);
882 SetTextColor(hdc
, (!(Current
->u
.Link
.state
& LIS_VISITED
) ? infoPtr
->LinkColor
: infoPtr
->VisitedColor
));
888 ExtTextOutW(hdc
, bl
->rc
.left
, bl
->rc
.top
, text_flags
, &bl
->rc
, tx
, bl
->nChars
, NULL
);
889 if((Current
->Type
== slLink
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && infoPtr
->HasFocus
)
891 COLORREF PrevTextColor
;
892 PrevTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
893 DrawFocusRect(hdc
, &bl
->rc
);
894 SetTextColor(hdc
, PrevTextColor
);
897 n
-= bl
->nChars
+ bl
->nSkip
;
903 SetBkColor(hdc
, OldBkColor
);
904 SetTextColor(hdc
, OldTextColor
);
905 SelectObject(hdc
, hOldFont
);
906 SetBkMode(hdc
, mode
);
911 /***********************************************************************
913 * Handles the WM_PAINT message.
915 static LRESULT
SYSLINK_Paint (const SYSLINK_INFO
*infoPtr
, HDC hdcParam
)
920 hdc
= hdcParam
? hdcParam
: BeginPaint (infoPtr
->Self
, &ps
);
923 SYSLINK_Draw (infoPtr
, hdc
);
924 if (!hdcParam
) EndPaint (infoPtr
->Self
, &ps
);
929 /***********************************************************************
931 * Set new Font for the SysLink control.
933 static HFONT
SYSLINK_SetFont (SYSLINK_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
939 HFONT hOldFont
= infoPtr
->Font
;
940 infoPtr
->Font
= hFont
;
942 /* free the underline font */
943 if(infoPtr
->LinkFont
!= NULL
)
945 DeleteObject(infoPtr
->LinkFont
);
946 infoPtr
->LinkFont
= NULL
;
949 /* Render text position and word wrapping in memory */
950 if (GetClientRect(infoPtr
->Self
, &rcClient
))
952 hdc
= GetDC(infoPtr
->Self
);
955 /* create a new underline font */
956 if(GetTextMetricsW(hdc
, &tm
) &&
957 GetObjectW(infoPtr
->Font
, sizeof(LOGFONTW
), &lf
))
959 lf
.lfUnderline
= TRUE
;
960 infoPtr
->LinkFont
= CreateFontIndirectW(&lf
);
961 infoPtr
->BreakChar
= tm
.tmBreakChar
;
965 ERR("Failed to create link font!\n");
968 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
969 ReleaseDC(infoPtr
->Self
, hdc
);
975 RedrawWindow(infoPtr
->Self
, NULL
, NULL
, RDW_INVALIDATE
| RDW_UPDATENOW
);
981 /***********************************************************************
983 * Set new text for the SysLink control.
985 static LRESULT
SYSLINK_SetText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
987 /* clear the document */
988 SYSLINK_ClearDoc(infoPtr
);
990 if(Text
== NULL
|| *Text
== 0)
995 /* let's parse the string and create a document */
996 if(SYSLINK_ParseText(infoPtr
, Text
) > 0)
1000 /* Render text position and word wrapping in memory */
1001 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1003 HDC hdc
= GetDC(infoPtr
->Self
);
1006 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1007 ReleaseDC(infoPtr
->Self
, hdc
);
1009 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1017 /***********************************************************************
1018 * SYSLINK_SetFocusLink
1019 * Updates the focus status bits and focusses the specified link.
1020 * If no document item is specified, the focus bit will be removed from all links.
1021 * Returns the previous focused item.
1023 static PDOC_ITEM
SYSLINK_SetFocusLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
1025 PDOC_ITEM Current
, PrevFocus
= NULL
;
1027 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1029 if(Current
->Type
== slLink
)
1031 if((PrevFocus
== NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
))
1033 PrevFocus
= Current
;
1036 if(Current
== DocItem
)
1038 Current
->u
.Link
.state
|= LIS_FOCUSED
;
1042 Current
->u
.Link
.state
&= ~LIS_FOCUSED
;
1050 /***********************************************************************
1052 * Sets the states and attributes of a link item.
1054 static LRESULT
SYSLINK_SetItem (const SYSLINK_INFO
*infoPtr
, const LITEM
*Item
)
1060 BOOL Repaint
= FALSE
;
1062 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1064 ERR("Invalid Flags!\n");
1068 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1071 ERR("Link %d couldn't be found\n", Item
->iLink
);
1075 if(Item
->mask
& LIF_ITEMID
)
1077 nc
= min(lstrlenW(Item
->szID
), MAX_LINKID_TEXT
- 1);
1078 szId
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1081 lstrcpynW(szId
, Item
->szID
, nc
+ 1);
1085 ERR("Unable to allocate memory for link id\n");
1090 if(Item
->mask
& LIF_URL
)
1092 nc
= min(lstrlenW(Item
->szUrl
), L_MAX_URL_LENGTH
- 1);
1093 szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1096 lstrcpynW(szUrl
, Item
->szUrl
, nc
+ 1);
1102 ERR("Unable to allocate memory for link url\n");
1107 if(Item
->mask
& LIF_ITEMID
)
1109 Free(di
->u
.Link
.szID
);
1110 di
->u
.Link
.szID
= szId
;
1113 if(Item
->mask
& LIF_URL
)
1115 Free(di
->u
.Link
.szUrl
);
1116 di
->u
.Link
.szUrl
= szUrl
;
1119 if(Item
->mask
& LIF_STATE
)
1121 UINT oldstate
= di
->u
.Link
.state
;
1122 /* clear the masked bits */
1123 di
->u
.Link
.state
&= ~(Item
->stateMask
& LIS_MASK
);
1125 di
->u
.Link
.state
|= (Item
->state
& Item
->stateMask
) & LIS_MASK
;
1126 Repaint
= (oldstate
!= di
->u
.Link
.state
);
1128 /* update the focus */
1129 SYSLINK_SetFocusLink(infoPtr
, ((di
->u
.Link
.state
& LIS_FOCUSED
) ? di
: NULL
));
1134 SYSLINK_RepaintLink(infoPtr
, di
);
1140 /***********************************************************************
1142 * Retrieves the states and attributes of a link item.
1144 static LRESULT
SYSLINK_GetItem (const SYSLINK_INFO
*infoPtr
, PLITEM Item
)
1148 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1150 ERR("Invalid Flags!\n");
1154 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1157 ERR("Link %d couldn't be found\n", Item
->iLink
);
1161 if(Item
->mask
& LIF_STATE
)
1163 Item
->state
= (di
->u
.Link
.state
& Item
->stateMask
);
1164 if(!infoPtr
->HasFocus
)
1166 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1167 Item
->state
&= ~LIS_FOCUSED
;
1171 if(Item
->mask
& LIF_ITEMID
)
1175 lstrcpyW(Item
->szID
, di
->u
.Link
.szID
);
1183 if(Item
->mask
& LIF_URL
)
1185 if(di
->u
.Link
.szUrl
)
1187 lstrcpyW(Item
->szUrl
, di
->u
.Link
.szUrl
);
1198 /***********************************************************************
1199 * SYSLINK_PtInDocItem
1200 * Determines if a point is in the region of a document item
1202 static BOOL
SYSLINK_PtInDocItem (const DOC_ITEM
*DocItem
, POINT pt
)
1207 bl
= DocItem
->Blocks
;
1214 if (PtInRect(&bl
->rc
, pt
))
1218 n
-= bl
->nChars
+ bl
->nSkip
;
1226 /***********************************************************************
1228 * Determines the link the user clicked on.
1230 static LRESULT
SYSLINK_HitTest (const SYSLINK_INFO
*infoPtr
, PLHITTESTINFO HitTest
)
1235 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1237 if(Current
->Type
== slLink
)
1239 if(SYSLINK_PtInDocItem(Current
, HitTest
->pt
))
1241 HitTest
->item
.mask
= 0;
1242 HitTest
->item
.iLink
= id
;
1243 HitTest
->item
.state
= 0;
1244 HitTest
->item
.stateMask
= 0;
1245 if(Current
->u
.Link
.szID
)
1247 lstrcpyW(HitTest
->item
.szID
, Current
->u
.Link
.szID
);
1251 HitTest
->item
.szID
[0] = 0;
1253 if(Current
->u
.Link
.szUrl
)
1255 lstrcpyW(HitTest
->item
.szUrl
, Current
->u
.Link
.szUrl
);
1259 HitTest
->item
.szUrl
[0] = 0;
1270 /***********************************************************************
1271 * SYSLINK_GetIdealHeight
1272 * Returns the preferred height of a link at the current control's width.
1274 static LRESULT
SYSLINK_GetIdealHeight (const SYSLINK_INFO
*infoPtr
)
1276 HDC hdc
= GetDC(infoPtr
->Self
);
1281 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1283 if(GetTextMetricsW(hdc
, &tm
))
1285 height
= tm
.tmHeight
;
1291 SelectObject(hdc
, hOldFont
);
1292 ReleaseDC(infoPtr
->Self
, hdc
);
1299 /***********************************************************************
1300 * SYSLINK_SendParentNotify
1301 * Sends a WM_NOTIFY message to the parent window.
1303 static LRESULT
SYSLINK_SendParentNotify (const SYSLINK_INFO
*infoPtr
, UINT code
, const DOC_ITEM
*Link
, int iLink
)
1307 nml
.hdr
.hwndFrom
= infoPtr
->Self
;
1308 nml
.hdr
.idFrom
= GetWindowLongPtrW(infoPtr
->Self
, GWLP_ID
);
1309 nml
.hdr
.code
= code
;
1312 nml
.item
.iLink
= iLink
;
1314 nml
.item
.stateMask
= 0;
1315 if(Link
->u
.Link
.szID
)
1317 lstrcpyW(nml
.item
.szID
, Link
->u
.Link
.szID
);
1321 nml
.item
.szID
[0] = 0;
1323 if(Link
->u
.Link
.szUrl
)
1325 lstrcpyW(nml
.item
.szUrl
, Link
->u
.Link
.szUrl
);
1329 nml
.item
.szUrl
[0] = 0;
1332 return SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, nml
.hdr
.idFrom
, (LPARAM
)&nml
);
1335 /***********************************************************************
1337 * Handles receiving the input focus.
1339 static LRESULT
SYSLINK_SetFocus (SYSLINK_INFO
*infoPtr
)
1343 infoPtr
->HasFocus
= TRUE
;
1345 /* We always select the first link, even if we activated the control using
1346 SHIFT+TAB. This is the default behavior */
1347 Focus
= SYSLINK_GetNextLink(infoPtr
, NULL
);
1350 SYSLINK_SetFocusLink(infoPtr
, Focus
);
1351 SYSLINK_RepaintLink(infoPtr
, Focus
);
1356 /***********************************************************************
1358 * Handles losing the input focus.
1360 static LRESULT
SYSLINK_KillFocus (SYSLINK_INFO
*infoPtr
)
1364 infoPtr
->HasFocus
= FALSE
;
1365 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1369 SYSLINK_RepaintLink(infoPtr
, Focus
);
1375 /***********************************************************************
1377 * Returns a link at the specified position
1379 static PDOC_ITEM
SYSLINK_LinkAtPt (const SYSLINK_INFO
*infoPtr
, const POINT
*pt
, int *LinkId
, BOOL MustBeEnabled
)
1384 for(Current
= infoPtr
->Items
; Current
!= NULL
; Current
= Current
->Next
)
1386 if((Current
->Type
== slLink
) && SYSLINK_PtInDocItem(Current
, *pt
) &&
1387 (!MustBeEnabled
|| (Current
->u
.Link
.state
& LIS_ENABLED
)))
1401 /***********************************************************************
1402 * SYSLINK_LButtonDown
1403 * Handles mouse clicks
1405 static LRESULT
SYSLINK_LButtonDown (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1407 PDOC_ITEM Current
, Old
;
1410 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1413 SetFocus(infoPtr
->Self
);
1415 Old
= SYSLINK_SetFocusLink(infoPtr
, Current
);
1416 if(Old
!= NULL
&& Old
!= Current
)
1418 SYSLINK_RepaintLink(infoPtr
, Old
);
1420 infoPtr
->MouseDownID
= id
;
1421 SYSLINK_RepaintLink(infoPtr
, Current
);
1427 /***********************************************************************
1429 * Handles mouse clicks
1431 static LRESULT
SYSLINK_LButtonUp (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1433 if(infoPtr
->MouseDownID
> -1)
1438 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1439 if((Current
!= NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && (infoPtr
->MouseDownID
== id
))
1441 SYSLINK_SendParentNotify(infoPtr
, NM_CLICK
, Current
, id
);
1445 infoPtr
->MouseDownID
= -1;
1450 /***********************************************************************
1452 * Handles ENTER key events
1454 static BOOL
SYSLINK_OnEnter (const SYSLINK_INFO
*infoPtr
)
1456 if(infoPtr
->HasFocus
&& !infoPtr
->IgnoreReturn
)
1461 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1464 SYSLINK_SendParentNotify(infoPtr
, NM_RETURN
, Focus
, id
);
1471 /***********************************************************************
1472 * SYSKEY_SelectNextPrevLink
1473 * Changes the currently focused link
1475 static BOOL
SYSKEY_SelectNextPrevLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1477 if(infoPtr
->HasFocus
)
1482 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1485 PDOC_ITEM NewFocus
, OldFocus
;
1488 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1490 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1492 if(NewFocus
!= NULL
)
1494 OldFocus
= SYSLINK_SetFocusLink(infoPtr
, NewFocus
);
1496 if(OldFocus
&& OldFocus
!= NewFocus
)
1498 SYSLINK_RepaintLink(infoPtr
, OldFocus
);
1500 SYSLINK_RepaintLink(infoPtr
, NewFocus
);
1508 /***********************************************************************
1509 * SYSKEY_SelectNextPrevLink
1510 * Determines if there's a next or previous link to decide whether the control
1511 * should capture the tab key message
1513 static BOOL
SYSLINK_NoNextLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1515 PDOC_ITEM Focus
, NewFocus
;
1517 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1519 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1521 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1523 return NewFocus
== NULL
;
1526 /***********************************************************************
1527 * SYSLINK_GetIdealSize
1528 * Calculates the ideal size of a link control at a given maximum width.
1530 static VOID
SYSLINK_GetIdealSize (const SYSLINK_INFO
*infoPtr
, int cxMaxWidth
, LPSIZE lpSize
)
1535 rc
.left
= rc
.top
= rc
.bottom
= 0;
1536 rc
.right
= cxMaxWidth
;
1538 hdc
= GetDC(infoPtr
->Self
);
1541 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1543 SYSLINK_Render(infoPtr
, hdc
, &rc
);
1545 SelectObject(hdc
, hOldFont
);
1546 ReleaseDC(infoPtr
->Self
, hdc
);
1548 lpSize
->cx
= rc
.right
;
1549 lpSize
->cy
= rc
.bottom
;
1553 /***********************************************************************
1556 static LRESULT WINAPI
SysLinkWindowProc(HWND hwnd
, UINT message
,
1557 WPARAM wParam
, LPARAM lParam
)
1559 SYSLINK_INFO
*infoPtr
;
1561 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd
, message
, wParam
, lParam
);
1563 infoPtr
= (SYSLINK_INFO
*)GetWindowLongPtrW(hwnd
, 0);
1565 if (!infoPtr
&& message
!= WM_CREATE
)
1566 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1569 case WM_PRINTCLIENT
:
1571 return SYSLINK_Paint (infoPtr
, (HDC
)wParam
);
1574 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
1576 HDC hdc
= (HDC
)wParam
;
1577 HBRUSH brush
= CreateSolidBrush( comctl32_color
.clrWindow
);
1580 GetClipBox( hdc
, &rect
);
1581 FillRect( hdc
, &rect
, brush
);
1582 DeleteObject( brush
);
1590 DWORD mp
= GetMessagePos();
1592 ht
.pt
.x
= (short)LOWORD(mp
);
1593 ht
.pt
.y
= (short)HIWORD(mp
);
1595 ScreenToClient(infoPtr
->Self
, &ht
.pt
);
1596 if(SYSLINK_HitTest (infoPtr
, &ht
))
1598 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_HAND
));
1602 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1608 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1610 HDC hdc
= GetDC(infoPtr
->Self
);
1613 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1614 ReleaseDC(infoPtr
->Self
, hdc
);
1621 return (LRESULT
)infoPtr
->Font
;
1624 return (LRESULT
)SYSLINK_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
1627 SYSLINK_SetText(infoPtr
, (LPWSTR
)lParam
);
1628 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1630 case WM_LBUTTONDOWN
:
1633 pt
.x
= (short)LOWORD(lParam
);
1634 pt
.y
= (short)HIWORD(lParam
);
1635 return SYSLINK_LButtonDown(infoPtr
, &pt
);
1640 pt
.x
= (short)LOWORD(lParam
);
1641 pt
.y
= (short)HIWORD(lParam
);
1642 return SYSLINK_LButtonUp(infoPtr
, &pt
);
1650 SYSLINK_OnEnter(infoPtr
);
1654 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1655 SYSKEY_SelectNextPrevLink(infoPtr
, shift
);
1659 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1665 LRESULT Ret
= DLGC_HASSETSEL
;
1666 int vk
= (lParam
!= 0 ? (int)((LPMSG
)lParam
)->wParam
: 0);
1670 Ret
|= DLGC_WANTMESSAGE
;
1674 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1675 if(!SYSLINK_NoNextLink(infoPtr
, shift
))
1677 Ret
|= DLGC_WANTTAB
;
1681 Ret
|= DLGC_WANTCHARS
;
1693 pt
.x
= (short)LOWORD(lParam
);
1694 pt
.y
= (short)HIWORD(lParam
);
1696 GetClientRect(infoPtr
->Self
, &rc
);
1697 ScreenToClient(infoPtr
->Self
, &pt
);
1698 if(pt
.x
< 0 || pt
.y
< 0 || pt
.x
> rc
.right
|| pt
.y
> rc
.bottom
)
1703 if(SYSLINK_LinkAtPt(infoPtr
, &pt
, NULL
, FALSE
))
1708 return HTTRANSPARENT
;
1712 return SYSLINK_HitTest(infoPtr
, (PLHITTESTINFO
)lParam
);
1715 return SYSLINK_SetItem(infoPtr
, (PLITEM
)lParam
);
1718 return SYSLINK_GetItem(infoPtr
, (PLITEM
)lParam
);
1720 case LM_GETIDEALHEIGHT
:
1723 /* LM_GETIDEALSIZE */
1724 SYSLINK_GetIdealSize(infoPtr
, (int)wParam
, (LPSIZE
)lParam
);
1726 return SYSLINK_GetIdealHeight(infoPtr
);
1729 return SYSLINK_SetFocus(infoPtr
);
1732 return SYSLINK_KillFocus(infoPtr
);
1735 infoPtr
->Style
&= ~WS_DISABLED
;
1736 infoPtr
->Style
|= (wParam
? 0 : WS_DISABLED
);
1737 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
1740 case WM_STYLECHANGED
:
1741 if (wParam
== GWL_STYLE
)
1743 infoPtr
->Style
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
1745 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1750 /* allocate memory for info struct */
1751 infoPtr
= Alloc (sizeof(SYSLINK_INFO
));
1752 if (!infoPtr
) return -1;
1753 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
1755 /* initialize the info struct */
1756 infoPtr
->Self
= hwnd
;
1757 infoPtr
->Notify
= ((LPCREATESTRUCTW
)lParam
)->hwndParent
;
1758 infoPtr
->Style
= ((LPCREATESTRUCTW
)lParam
)->style
;
1760 infoPtr
->LinkFont
= 0;
1761 infoPtr
->Items
= NULL
;
1762 infoPtr
->HasFocus
= FALSE
;
1763 infoPtr
->MouseDownID
= -1;
1764 infoPtr
->TextColor
= comctl32_color
.clrWindowText
;
1765 infoPtr
->LinkColor
= comctl32_color
.clrHighlight
;
1766 infoPtr
->VisitedColor
= comctl32_color
.clrHighlight
;
1767 infoPtr
->BreakChar
= ' ';
1768 infoPtr
->IgnoreReturn
= infoPtr
->Style
& LWS_IGNORERETURN
;
1769 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd
);
1770 SYSLINK_SetText(infoPtr
, ((LPCREATESTRUCTW
)lParam
)->lpszName
);
1774 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd
);
1775 SYSLINK_ClearDoc(infoPtr
);
1776 if(infoPtr
->Font
!= 0) DeleteObject(infoPtr
->Font
);
1777 if(infoPtr
->LinkFont
!= 0) DeleteObject(infoPtr
->LinkFont
);
1778 SetWindowLongPtrW(hwnd
, 0, 0);
1782 case WM_SYSCOLORCHANGE
:
1783 COMCTL32_RefreshSysColors();
1787 if ((message
>= WM_USER
) && (message
< WM_APP
) && !COMCTL32_IsReflectedMessage(message
))
1789 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message
, wParam
, lParam
);
1791 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1796 /***********************************************************************
1797 * SYSLINK_Register [Internal]
1799 * Registers the SysLink window class.
1801 VOID
SYSLINK_Register (void)
1805 ZeroMemory (&wndClass
, sizeof(wndClass
));
1806 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
1807 wndClass
.lpfnWndProc
= SysLinkWindowProc
;
1808 wndClass
.cbClsExtra
= 0;
1809 wndClass
.cbWndExtra
= sizeof (SYSLINK_INFO
*);
1810 wndClass
.hCursor
= LoadCursorW (0, (LPWSTR
)IDC_ARROW
);
1811 wndClass
.lpszClassName
= WC_LINK
;
1813 RegisterClassW (&wndClass
);
1817 /***********************************************************************
1818 * SYSLINK_Unregister [Internal]
1820 * Unregisters the SysLink window class.
1822 VOID
SYSLINK_Unregister (void)
1824 UnregisterClassW (WC_LINK
, NULL
);