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
30 #include "wine/debug.h"
31 #include "wine/list.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(syslink
);
40 } DOC_TEXTBLOCK
, *PDOC_TEXTBLOCK
;
42 #define LIF_FLAGSMASK (LIF_STATE | LIF_ITEMID | LIF_URL)
43 #define LIS_MASK (LIS_FOCUSED | LIS_ENABLED | LIS_VISITED)
51 typedef struct _DOC_ITEM
54 UINT nText
; /* Number of characters of the text */
55 SL_ITEM_TYPE Type
; /* type of the item */
56 PDOC_TEXTBLOCK Blocks
; /* Array of text blocks */
61 UINT state
; /* Link state */
62 WCHAR
*szID
; /* Link ID string */
63 WCHAR
*szUrl
; /* Link URL string */
70 WCHAR Text
[1]; /* Text of the document item */
71 } DOC_ITEM
, *PDOC_ITEM
;
75 HWND Self
; /* The window handle for this control */
76 HWND Notify
; /* The parent handle to receive notifications */
77 DWORD Style
; /* Styles for this control */
78 struct list Items
; /* Document items list */
79 BOOL HasFocus
; /* Whether the control has the input focus */
80 int MouseDownID
; /* ID of the link that the mouse button first selected */
81 HFONT Font
; /* Handle to the font for text */
82 HFONT LinkFont
; /* Handle to the font for links */
83 COLORREF TextColor
; /* Color of the text */
84 COLORREF LinkColor
; /* Color of links */
85 COLORREF VisitedColor
; /* Color of visited links */
86 WCHAR BreakChar
; /* Break Character for the current font */
87 BOOL IgnoreReturn
; /* (infoPtr->Style & LWS_IGNORERETURN) on creation */
90 /* Control configuration constants */
92 #define SL_LEFTMARGIN (0)
93 #define SL_TOPMARGIN (0)
94 #define SL_RIGHTMARGIN (0)
95 #define SL_BOTTOMMARGIN (0)
97 /***********************************************************************
99 * Frees all data and gdi objects associated with a document item
101 static VOID
SYSLINK_FreeDocItem (PDOC_ITEM DocItem
)
103 if(DocItem
->Type
== slLink
)
105 Free(DocItem
->u
.Link
.szID
);
106 Free(DocItem
->u
.Link
.szUrl
);
109 Free(DocItem
->Blocks
);
111 /* we don't free Text because it's just a pointer to a character in the
112 entire window text string */
117 /***********************************************************************
118 * SYSLINK_AppendDocItem
119 * Create and append a new document item.
121 static PDOC_ITEM
SYSLINK_AppendDocItem (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
, UINT textlen
,
122 SL_ITEM_TYPE type
, PDOC_ITEM LastItem
)
126 textlen
= min(textlen
, lstrlenW(Text
));
127 Item
= Alloc(FIELD_OFFSET(DOC_ITEM
, Text
[textlen
+ 1]));
130 ERR("Failed to alloc DOC_ITEM structure!\n");
134 Item
->nText
= textlen
;
137 lstrcpynW(Item
->Text
, Text
, textlen
+ 1);
139 list_add_after(&LastItem
->entry
, &Item
->entry
);
141 list_add_tail(&infoPtr
->Items
, &Item
->entry
);
146 /***********************************************************************
148 * Clears the document tree
150 static VOID
SYSLINK_ClearDoc (SYSLINK_INFO
*infoPtr
)
152 DOC_ITEM
*Item
, *Item2
;
154 LIST_FOR_EACH_ENTRY_SAFE(Item
, Item2
, &infoPtr
->Items
, DOC_ITEM
, entry
)
156 list_remove(&Item
->entry
);
157 SYSLINK_FreeDocItem(Item
);
161 /***********************************************************************
163 * Parses the window text string and creates a document. Returns the
164 * number of document items created.
166 static UINT
SYSLINK_ParseText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
168 LPCWSTR current
, textstart
= NULL
, linktext
= NULL
, firsttag
= NULL
;
169 int taglen
= 0, textlen
= 0, linklen
= 0, docitems
= 0;
170 PDOC_ITEM Last
= NULL
;
171 SL_ITEM_TYPE CurrentType
= slText
;
175 TRACE("(%p %s)\n", infoPtr
, debugstr_w(Text
));
177 for(current
= Text
; *current
!= 0;)
181 if(!wcsnicmp(current
, L
"<a", 2) && (CurrentType
== slText
))
183 BOOL ValidParam
= FALSE
, ValidLink
= FALSE
;
185 if(*(current
+ 2) == '>')
187 /* we just have to deal with a <a> tag */
196 else if(*(current
+ 2) == infoPtr
->BreakChar
)
198 /* we expect parameters, parse them */
199 LPCWSTR
*CurrentParameter
= NULL
, tmp
;
200 UINT
*CurrentParameterLen
= NULL
;
203 tmp
= current
+ taglen
;
208 /* compare the current position with all known parameters */
209 if(!wcsnicmp(tmp
, L
"href=\"", 6))
213 CurrentParameter
= &lpUrl
;
214 CurrentParameterLen
= &lenUrl
;
216 else if(!wcsnicmp(tmp
, L
"id=\"", 4))
220 CurrentParameter
= &lpID
;
221 CurrentParameterLen
= &lenId
;
230 /* we got a known parameter, now search until the next " character.
231 If we can't find a " character, there's a syntax error and we just assume it's text */
233 *CurrentParameter
= current
+ taglen
;
234 *CurrentParameterLen
= 0;
236 for(tmp
= *CurrentParameter
; *tmp
!= 0; tmp
++)
245 (*CurrentParameterLen
)++;
250 /* we're done with this parameter, now there are only 2 possibilities:
251 * 1. another parameter is coming, so expect a ' ' (space) character
252 * 2. the tag is being closed, so expect a '<' character
254 if(*tmp
== infoPtr
->BreakChar
)
256 /* we expect another parameter, do the whole thing again */
263 /* the tag is being closed, we're done */
270 if(ValidLink
&& ValidParam
)
272 /* the <a ...> tag appears to be valid. save all information
273 so we can add the link if we find a valid </a> tag later */
274 CurrentType
= slLink
;
275 linktext
= current
+ taglen
;
284 if(textstart
== NULL
)
290 else if (!wcsnicmp(current
, L
"</a>", 4) && (CurrentType
== slLink
) && firsttag
)
292 /* there's a <a...> tag opened, first add the previous text, if present */
293 if(textstart
!= NULL
&& textlen
> 0 && firsttag
> textstart
)
295 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, firsttag
- textstart
, slText
, Last
);
298 ERR("Unable to create new document item!\n");
306 /* now it's time to add the link to the document */
308 if(linktext
!= NULL
&& linklen
> 0)
310 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slLink
, Last
);
313 ERR("Unable to create new document item!\n");
317 if(CurrentType
== slLink
)
321 if(!(infoPtr
->Style
& WS_DISABLED
))
323 Last
->u
.Link
.state
|= LIS_ENABLED
;
325 /* Copy the tag parameters */
328 nc
= min(lenId
, lstrlenW(lpID
));
329 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
330 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
331 if(Last
->u
.Link
.szID
!= NULL
)
333 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
337 Last
->u
.Link
.szID
= NULL
;
340 nc
= min(lenUrl
, lstrlenW(lpUrl
));
341 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
342 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
343 if(Last
->u
.Link
.szUrl
!= NULL
)
345 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
349 Last
->u
.Link
.szUrl
= NULL
;
353 CurrentType
= slText
;
360 /* we don't know what tag it is, so just continue */
363 if(CurrentType
== slText
&& textstart
== NULL
)
377 /* save the pointer of the current text item if we couldn't find a tag */
378 if(textstart
== NULL
&& CurrentType
== slText
)
387 if(textstart
!= NULL
&& textlen
> 0)
389 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, textlen
, CurrentType
, Last
);
392 ERR("Unable to create new document item!\n");
395 if(CurrentType
== slLink
)
399 if(!(infoPtr
->Style
& WS_DISABLED
))
401 Last
->u
.Link
.state
|= LIS_ENABLED
;
403 /* Copy the tag parameters */
406 nc
= min(lenId
, lstrlenW(lpID
));
407 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
408 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
409 if(Last
->u
.Link
.szID
!= NULL
)
411 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
415 Last
->u
.Link
.szID
= NULL
;
418 nc
= min(lenUrl
, lstrlenW(lpUrl
));
419 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
420 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
421 if(Last
->u
.Link
.szUrl
!= NULL
)
423 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
427 Last
->u
.Link
.szUrl
= NULL
;
432 if(linktext
!= NULL
&& linklen
> 0)
434 /* we got an unclosed link, just display the text */
435 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slText
, Last
);
438 ERR("Unable to create new document item!\n");
447 /***********************************************************************
448 * SYSLINK_RepaintLink
451 static VOID
SYSLINK_RepaintLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
456 if(DocItem
->Type
!= slLink
)
458 ERR("DocItem not a link!\n");
462 bl
= DocItem
->Blocks
;
469 InvalidateRect(infoPtr
->Self
, &bl
->rc
, TRUE
);
470 n
-= bl
->nChars
+ bl
->nSkip
;
476 /***********************************************************************
477 * SYSLINK_GetLinkItemByIndex
478 * Retrieves a document link by its index
480 static PDOC_ITEM
SYSLINK_GetLinkItemByIndex (const SYSLINK_INFO
*infoPtr
, int iLink
)
484 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
486 if ((Current
->Type
== slLink
) && (iLink
-- <= 0))
492 /***********************************************************************
493 * SYSLINK_GetFocusLink
494 * Retrieves the link that has the LIS_FOCUSED bit
496 static PDOC_ITEM
SYSLINK_GetFocusLink (const SYSLINK_INFO
*infoPtr
, int *LinkId
)
501 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
503 if(Current
->Type
== slLink
)
505 if(Current
->u
.Link
.state
& LIS_FOCUSED
)
518 /***********************************************************************
519 * SYSLINK_GetNextLink
522 static PDOC_ITEM
SYSLINK_GetNextLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
526 LIST_FOR_EACH_ENTRY(Next
, Current
? &Current
->entry
: &infoPtr
->Items
, DOC_ITEM
, entry
)
528 if (Next
->Type
== slLink
)
536 /***********************************************************************
537 * SYSLINK_GetPrevLink
538 * Gets the previous link
540 static PDOC_ITEM
SYSLINK_GetPrevLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
544 LIST_FOR_EACH_ENTRY_REV(Prev
, Current
? &Current
->entry
: list_tail(&infoPtr
->Items
), DOC_ITEM
, entry
)
546 if (Prev
->Type
== slLink
)
555 /***********************************************************************
557 * Tries to wrap a line.
559 static BOOL
SYSLINK_WrapLine (LPWSTR Text
, WCHAR BreakChar
, int x
, int *LineLen
,
560 int nFit
, LPSIZE Extent
)
564 for (i
= 0; i
< nFit
; i
++) if (Text
[i
] == '\r' || Text
[i
] == '\n') break;
566 if (i
== *LineLen
) return FALSE
;
568 /* check if we're in the middle of a word */
569 if (Text
[i
] != '\r' && Text
[i
] != '\n' && Text
[i
] != BreakChar
)
571 /* search for the beginning of the word */
572 while (i
&& Text
[i
- 1] != BreakChar
) i
--;
578 if (x
== SL_LEFTMARGIN
) i
= max( nFit
, 1 );
585 /***********************************************************************
587 * Renders the document in memory
589 static VOID
SYSLINK_Render (const SYSLINK_INFO
*infoPtr
, HDC hdc
, PRECT pRect
)
594 int x
, y
, LineHeight
;
598 szDoc
.cx
= szDoc
.cy
= 0;
601 rc
.right
-= SL_RIGHTMARGIN
;
602 rc
.bottom
-= SL_BOTTOMMARGIN
;
604 if(rc
.right
- SL_LEFTMARGIN
< 0)
606 if (rc
.bottom
- SL_TOPMARGIN
< 0)
609 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
613 GetTextMetricsW( hdc
, &tm
);
614 LineHeight
= tm
.tmHeight
+ tm
.tmExternalLeading
;
616 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
620 PDOC_TEXTBLOCK bl
, cbl
;
625 if(Current
->nText
== 0)
633 Free(Current
->Blocks
);
634 Current
->Blocks
= NULL
;
638 if(Current
->Type
== slText
)
640 SelectObject(hdc
, infoPtr
->Font
);
642 else if(Current
->Type
== slLink
)
644 SelectObject(hdc
, infoPtr
->LinkFont
);
649 /* skip break characters unless they're the first of the doc item */
650 if(tx
!= Current
->Text
|| x
== SL_LEFTMARGIN
)
652 if (n
&& *tx
== '\r')
658 if (n
&& *tx
== '\n')
664 while(n
> 0 && (*tx
) == infoPtr
->BreakChar
)
672 if((n
== 0 && SkipChars
!= 0) ||
673 GetTextExtentExPointW(hdc
, tx
, n
, rc
.right
- x
, &nFit
, NULL
, &szDim
))
681 Wrap
= SYSLINK_WrapLine(tx
, infoPtr
->BreakChar
, x
, &LineLen
, nFit
, &szDim
);
685 /* move one line down, the word didn't fit into the line */
693 if(!GetTextExtentExPointW(hdc
, tx
, LineLen
, rc
.right
- x
, NULL
, NULL
, &szDim
))
706 nbl
= ReAlloc(bl
, (nBlocks
+ 1) * sizeof(DOC_TEXTBLOCK
));
712 cbl
= bl
+ nBlocks
- 1;
714 cbl
->nChars
= LineLen
;
715 cbl
->nSkip
= SkipChars
;
716 SetRect(&cbl
->rc
, x
, y
, x
+ szDim
.cx
, y
+ szDim
.cy
);
718 if (cbl
->rc
.right
> szDoc
.cx
)
719 szDoc
.cx
= cbl
->rc
.right
;
720 if (cbl
->rc
.bottom
> szDoc
.cy
)
721 szDoc
.cy
= cbl
->rc
.bottom
;
739 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
754 Current
->Blocks
= bl
;
758 SelectObject(hdc
, hOldFont
);
760 pRect
->right
= pRect
->left
+ szDoc
.cx
;
761 pRect
->bottom
= pRect
->top
+ szDoc
.cy
;
764 /***********************************************************************
766 * Draws the SysLink control.
768 static LRESULT
SYSLINK_Draw (const SYSLINK_INFO
*infoPtr
, HDC hdc
)
773 COLORREF OldTextColor
, OldBkColor
;
775 UINT text_flags
= ETO_CLIPPED
;
776 UINT mode
= GetBkMode( hdc
);
778 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
779 OldTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
780 OldBkColor
= SetBkColor(hdc
, comctl32_color
.clrWindow
);
782 GetClientRect(infoPtr
->Self
, &rc
);
783 rc
.right
-= SL_RIGHTMARGIN
+ SL_LEFTMARGIN
;
784 rc
.bottom
-= SL_BOTTOMMARGIN
+ SL_TOPMARGIN
;
786 if(rc
.right
< 0 || rc
.bottom
< 0) return 0;
788 hBrush
= (HBRUSH
)SendMessageW(infoPtr
->Notify
, WM_CTLCOLORSTATIC
,
789 (WPARAM
)hdc
, (LPARAM
)infoPtr
->Self
);
790 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
792 FillRect(hdc
, &rc
, hBrush
);
793 if (GetBkMode( hdc
) == OPAQUE
) text_flags
|= ETO_OPAQUE
;
795 else SetBkMode( hdc
, TRANSPARENT
);
797 DeleteObject(hBrush
);
799 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
805 bl
= Current
->Blocks
;
811 if(Current
->Type
== slText
)
813 SelectObject(hdc
, infoPtr
->Font
);
814 SetTextColor(hdc
, infoPtr
->TextColor
);
818 SelectObject(hdc
, infoPtr
->LinkFont
);
819 SetTextColor(hdc
, (!(Current
->u
.Link
.state
& LIS_VISITED
) ? infoPtr
->LinkColor
: infoPtr
->VisitedColor
));
825 ExtTextOutW(hdc
, bl
->rc
.left
, bl
->rc
.top
, text_flags
, &bl
->rc
, tx
, bl
->nChars
, NULL
);
826 if((Current
->Type
== slLink
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && infoPtr
->HasFocus
)
828 COLORREF PrevTextColor
;
829 PrevTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
830 DrawFocusRect(hdc
, &bl
->rc
);
831 SetTextColor(hdc
, PrevTextColor
);
834 n
-= bl
->nChars
+ bl
->nSkip
;
840 SetBkColor(hdc
, OldBkColor
);
841 SetTextColor(hdc
, OldTextColor
);
842 SelectObject(hdc
, hOldFont
);
843 SetBkMode(hdc
, mode
);
848 /***********************************************************************
850 * Handles the WM_PAINT message.
852 static LRESULT
SYSLINK_Paint (const SYSLINK_INFO
*infoPtr
, HDC hdcParam
)
857 hdc
= hdcParam
? hdcParam
: BeginPaint (infoPtr
->Self
, &ps
);
860 SYSLINK_Draw (infoPtr
, hdc
);
861 if (!hdcParam
) EndPaint (infoPtr
->Self
, &ps
);
866 /***********************************************************************
868 * Set new Font for the SysLink control.
870 static HFONT
SYSLINK_SetFont (SYSLINK_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
876 HFONT hOldFont
= infoPtr
->Font
;
877 infoPtr
->Font
= hFont
;
879 /* free the underline font */
880 if(infoPtr
->LinkFont
!= NULL
)
882 DeleteObject(infoPtr
->LinkFont
);
883 infoPtr
->LinkFont
= NULL
;
886 /* Render text position and word wrapping in memory */
887 if (GetClientRect(infoPtr
->Self
, &rcClient
))
889 hdc
= GetDC(infoPtr
->Self
);
892 /* create a new underline font */
893 if(GetTextMetricsW(hdc
, &tm
) &&
894 GetObjectW(infoPtr
->Font
, sizeof(LOGFONTW
), &lf
))
896 lf
.lfUnderline
= TRUE
;
897 infoPtr
->LinkFont
= CreateFontIndirectW(&lf
);
898 infoPtr
->BreakChar
= tm
.tmBreakChar
;
902 ERR("Failed to create link font!\n");
905 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
906 ReleaseDC(infoPtr
->Self
, hdc
);
912 RedrawWindow(infoPtr
->Self
, NULL
, NULL
, RDW_INVALIDATE
| RDW_UPDATENOW
);
918 /***********************************************************************
920 * Set new text for the SysLink control.
922 static LRESULT
SYSLINK_SetText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
924 /* clear the document */
925 SYSLINK_ClearDoc(infoPtr
);
927 if(Text
== NULL
|| *Text
== 0)
932 /* let's parse the string and create a document */
933 if(SYSLINK_ParseText(infoPtr
, Text
) > 0)
937 /* Render text position and word wrapping in memory */
938 if (GetClientRect(infoPtr
->Self
, &rcClient
))
940 HDC hdc
= GetDC(infoPtr
->Self
);
943 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
944 ReleaseDC(infoPtr
->Self
, hdc
);
946 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
954 /***********************************************************************
955 * SYSLINK_SetFocusLink
956 * Updates the focus status bits and focuses the specified link.
957 * If no document item is specified, the focus bit will be removed from all links.
958 * Returns the previous focused item.
960 static PDOC_ITEM
SYSLINK_SetFocusLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
962 PDOC_ITEM Current
, PrevFocus
= NULL
;
964 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
966 if(Current
->Type
== slLink
)
968 if((PrevFocus
== NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
))
973 if(Current
== DocItem
)
975 Current
->u
.Link
.state
|= LIS_FOCUSED
;
979 Current
->u
.Link
.state
&= ~LIS_FOCUSED
;
987 /***********************************************************************
989 * Sets the states and attributes of a link item.
991 static LRESULT
SYSLINK_SetItem (const SYSLINK_INFO
*infoPtr
, const LITEM
*Item
)
997 BOOL Repaint
= FALSE
;
999 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1001 ERR("Invalid Flags!\n");
1005 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1008 ERR("Link %d couldn't be found\n", Item
->iLink
);
1012 if(Item
->mask
& LIF_ITEMID
)
1014 nc
= min(lstrlenW(Item
->szID
), MAX_LINKID_TEXT
- 1);
1015 szId
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1018 lstrcpynW(szId
, Item
->szID
, nc
+ 1);
1022 ERR("Unable to allocate memory for link id\n");
1027 if(Item
->mask
& LIF_URL
)
1029 nc
= min(lstrlenW(Item
->szUrl
), L_MAX_URL_LENGTH
- 1);
1030 szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1033 lstrcpynW(szUrl
, Item
->szUrl
, nc
+ 1);
1039 ERR("Unable to allocate memory for link url\n");
1044 if(Item
->mask
& LIF_ITEMID
)
1046 Free(di
->u
.Link
.szID
);
1047 di
->u
.Link
.szID
= szId
;
1050 if(Item
->mask
& LIF_URL
)
1052 Free(di
->u
.Link
.szUrl
);
1053 di
->u
.Link
.szUrl
= szUrl
;
1056 if(Item
->mask
& LIF_STATE
)
1058 UINT oldstate
= di
->u
.Link
.state
;
1059 /* clear the masked bits */
1060 di
->u
.Link
.state
&= ~(Item
->stateMask
& LIS_MASK
);
1062 di
->u
.Link
.state
|= (Item
->state
& Item
->stateMask
) & LIS_MASK
;
1063 Repaint
= (oldstate
!= di
->u
.Link
.state
);
1065 /* update the focus */
1066 SYSLINK_SetFocusLink(infoPtr
, ((di
->u
.Link
.state
& LIS_FOCUSED
) ? di
: NULL
));
1071 SYSLINK_RepaintLink(infoPtr
, di
);
1077 /***********************************************************************
1079 * Retrieves the states and attributes of a link item.
1081 static LRESULT
SYSLINK_GetItem (const SYSLINK_INFO
*infoPtr
, PLITEM Item
)
1085 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1087 ERR("Invalid Flags!\n");
1091 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1094 ERR("Link %d couldn't be found\n", Item
->iLink
);
1098 if(Item
->mask
& LIF_STATE
)
1100 Item
->state
= (di
->u
.Link
.state
& Item
->stateMask
);
1101 if(!infoPtr
->HasFocus
)
1103 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1104 Item
->state
&= ~LIS_FOCUSED
;
1108 if(Item
->mask
& LIF_ITEMID
)
1112 lstrcpyW(Item
->szID
, di
->u
.Link
.szID
);
1120 if(Item
->mask
& LIF_URL
)
1122 if(di
->u
.Link
.szUrl
)
1124 lstrcpyW(Item
->szUrl
, di
->u
.Link
.szUrl
);
1135 /***********************************************************************
1136 * SYSLINK_PtInDocItem
1137 * Determines if a point is in the region of a document item
1139 static BOOL
SYSLINK_PtInDocItem (const DOC_ITEM
*DocItem
, POINT pt
)
1144 bl
= DocItem
->Blocks
;
1151 if (PtInRect(&bl
->rc
, pt
))
1155 n
-= bl
->nChars
+ bl
->nSkip
;
1163 /***********************************************************************
1165 * Determines the link the user clicked on.
1167 static LRESULT
SYSLINK_HitTest (const SYSLINK_INFO
*infoPtr
, PLHITTESTINFO HitTest
)
1172 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
1174 if(Current
->Type
== slLink
)
1176 if(SYSLINK_PtInDocItem(Current
, HitTest
->pt
))
1178 HitTest
->item
.mask
= 0;
1179 HitTest
->item
.iLink
= id
;
1180 HitTest
->item
.state
= 0;
1181 HitTest
->item
.stateMask
= 0;
1182 if(Current
->u
.Link
.szID
)
1184 lstrcpyW(HitTest
->item
.szID
, Current
->u
.Link
.szID
);
1188 HitTest
->item
.szID
[0] = 0;
1190 if(Current
->u
.Link
.szUrl
)
1192 lstrcpyW(HitTest
->item
.szUrl
, Current
->u
.Link
.szUrl
);
1196 HitTest
->item
.szUrl
[0] = 0;
1207 /***********************************************************************
1208 * SYSLINK_GetIdealHeight
1209 * Returns the preferred height of a link at the current control's width.
1211 static LRESULT
SYSLINK_GetIdealHeight (const SYSLINK_INFO
*infoPtr
)
1213 HDC hdc
= GetDC(infoPtr
->Self
);
1218 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1220 if(GetTextMetricsW(hdc
, &tm
))
1222 height
= tm
.tmHeight
;
1228 SelectObject(hdc
, hOldFont
);
1229 ReleaseDC(infoPtr
->Self
, hdc
);
1236 /***********************************************************************
1237 * SYSLINK_SendParentNotify
1238 * Sends a WM_NOTIFY message to the parent window.
1240 static LRESULT
SYSLINK_SendParentNotify (const SYSLINK_INFO
*infoPtr
, UINT code
, const DOC_ITEM
*Link
, int iLink
)
1244 nml
.hdr
.hwndFrom
= infoPtr
->Self
;
1245 nml
.hdr
.idFrom
= GetWindowLongPtrW(infoPtr
->Self
, GWLP_ID
);
1246 nml
.hdr
.code
= code
;
1249 nml
.item
.iLink
= iLink
;
1251 nml
.item
.stateMask
= 0;
1252 if(Link
->u
.Link
.szID
)
1254 lstrcpyW(nml
.item
.szID
, Link
->u
.Link
.szID
);
1258 nml
.item
.szID
[0] = 0;
1260 if(Link
->u
.Link
.szUrl
)
1262 lstrcpyW(nml
.item
.szUrl
, Link
->u
.Link
.szUrl
);
1266 nml
.item
.szUrl
[0] = 0;
1269 return SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, nml
.hdr
.idFrom
, (LPARAM
)&nml
);
1272 /***********************************************************************
1274 * Handles receiving the input focus.
1276 static LRESULT
SYSLINK_SetFocus (SYSLINK_INFO
*infoPtr
)
1280 infoPtr
->HasFocus
= TRUE
;
1282 /* We always select the first link, even if we activated the control using
1283 SHIFT+TAB. This is the default behavior */
1284 Focus
= SYSLINK_GetNextLink(infoPtr
, NULL
);
1287 SYSLINK_SetFocusLink(infoPtr
, Focus
);
1288 SYSLINK_RepaintLink(infoPtr
, Focus
);
1293 /***********************************************************************
1295 * Handles losing the input focus.
1297 static LRESULT
SYSLINK_KillFocus (SYSLINK_INFO
*infoPtr
)
1301 infoPtr
->HasFocus
= FALSE
;
1302 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1306 SYSLINK_RepaintLink(infoPtr
, Focus
);
1312 /***********************************************************************
1314 * Returns a link at the specified position
1316 static PDOC_ITEM
SYSLINK_LinkAtPt (const SYSLINK_INFO
*infoPtr
, const POINT
*pt
, int *LinkId
, BOOL MustBeEnabled
)
1321 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
1323 if((Current
->Type
== slLink
) && SYSLINK_PtInDocItem(Current
, *pt
) &&
1324 (!MustBeEnabled
|| (Current
->u
.Link
.state
& LIS_ENABLED
)))
1338 /***********************************************************************
1339 * SYSLINK_LButtonDown
1340 * Handles mouse clicks
1342 static LRESULT
SYSLINK_LButtonDown (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1344 PDOC_ITEM Current
, Old
;
1347 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1350 SetFocus(infoPtr
->Self
);
1352 Old
= SYSLINK_SetFocusLink(infoPtr
, Current
);
1353 if(Old
!= NULL
&& Old
!= Current
)
1355 SYSLINK_RepaintLink(infoPtr
, Old
);
1357 infoPtr
->MouseDownID
= id
;
1358 SYSLINK_RepaintLink(infoPtr
, Current
);
1364 /***********************************************************************
1366 * Handles mouse clicks
1368 static LRESULT
SYSLINK_LButtonUp (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1370 if(infoPtr
->MouseDownID
> -1)
1375 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1376 if((Current
!= NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && (infoPtr
->MouseDownID
== id
))
1378 SYSLINK_SendParentNotify(infoPtr
, NM_CLICK
, Current
, id
);
1382 infoPtr
->MouseDownID
= -1;
1387 /***********************************************************************
1389 * Handles ENTER key events
1391 static BOOL
SYSLINK_OnEnter (const SYSLINK_INFO
*infoPtr
)
1393 if(infoPtr
->HasFocus
&& !infoPtr
->IgnoreReturn
)
1398 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1401 SYSLINK_SendParentNotify(infoPtr
, NM_RETURN
, Focus
, id
);
1408 /***********************************************************************
1409 * SYSKEY_SelectNextPrevLink
1410 * Changes the currently focused link
1412 static BOOL
SYSKEY_SelectNextPrevLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1414 if(infoPtr
->HasFocus
)
1419 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1422 PDOC_ITEM NewFocus
, OldFocus
;
1425 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1427 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1429 if(NewFocus
!= NULL
)
1431 OldFocus
= SYSLINK_SetFocusLink(infoPtr
, NewFocus
);
1433 if(OldFocus
&& OldFocus
!= NewFocus
)
1435 SYSLINK_RepaintLink(infoPtr
, OldFocus
);
1437 SYSLINK_RepaintLink(infoPtr
, NewFocus
);
1445 /***********************************************************************
1446 * SYSKEY_SelectNextPrevLink
1447 * Determines if there's a next or previous link to decide whether the control
1448 * should capture the tab key message
1450 static BOOL
SYSLINK_NoNextLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1452 PDOC_ITEM Focus
, NewFocus
;
1454 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1456 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1458 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1460 return NewFocus
== NULL
;
1463 /***********************************************************************
1464 * SYSLINK_GetIdealSize
1465 * Calculates the ideal size of a link control at a given maximum width.
1467 static LONG
SYSLINK_GetIdealSize (const SYSLINK_INFO
*infoPtr
, int cxMaxWidth
, SIZE
*lpSize
)
1472 rc
.left
= rc
.top
= rc
.bottom
= 0;
1473 rc
.right
= cxMaxWidth
;
1475 hdc
= GetDC(infoPtr
->Self
);
1478 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1480 SYSLINK_Render(infoPtr
, hdc
, &rc
);
1482 SelectObject(hdc
, hOldFont
);
1483 ReleaseDC(infoPtr
->Self
, hdc
);
1485 lpSize
->cx
= rc
.right
;
1486 lpSize
->cy
= rc
.bottom
;
1492 /***********************************************************************
1495 static LRESULT WINAPI
SysLinkWindowProc(HWND hwnd
, UINT message
,
1496 WPARAM wParam
, LPARAM lParam
)
1498 SYSLINK_INFO
*infoPtr
;
1500 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd
, message
, wParam
, lParam
);
1502 infoPtr
= (SYSLINK_INFO
*)GetWindowLongPtrW(hwnd
, 0);
1504 if (!infoPtr
&& message
!= WM_CREATE
)
1505 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1508 case WM_PRINTCLIENT
:
1510 return SYSLINK_Paint (infoPtr
, (HDC
)wParam
);
1513 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
1515 HDC hdc
= (HDC
)wParam
;
1516 HBRUSH brush
= CreateSolidBrush( comctl32_color
.clrWindow
);
1519 GetClipBox( hdc
, &rect
);
1520 FillRect( hdc
, &rect
, brush
);
1521 DeleteObject( brush
);
1529 DWORD mp
= GetMessagePos();
1531 ht
.pt
.x
= (short)LOWORD(mp
);
1532 ht
.pt
.y
= (short)HIWORD(mp
);
1534 ScreenToClient(infoPtr
->Self
, &ht
.pt
);
1535 if(SYSLINK_HitTest (infoPtr
, &ht
))
1537 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_HAND
));
1541 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1547 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1549 HDC hdc
= GetDC(infoPtr
->Self
);
1552 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1553 ReleaseDC(infoPtr
->Self
, hdc
);
1560 return (LRESULT
)infoPtr
->Font
;
1563 return (LRESULT
)SYSLINK_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
1566 SYSLINK_SetText(infoPtr
, (LPWSTR
)lParam
);
1567 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1569 case WM_LBUTTONDOWN
:
1572 pt
.x
= (short)LOWORD(lParam
);
1573 pt
.y
= (short)HIWORD(lParam
);
1574 return SYSLINK_LButtonDown(infoPtr
, &pt
);
1579 pt
.x
= (short)LOWORD(lParam
);
1580 pt
.y
= (short)HIWORD(lParam
);
1581 return SYSLINK_LButtonUp(infoPtr
, &pt
);
1589 SYSLINK_OnEnter(infoPtr
);
1593 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1594 SYSKEY_SelectNextPrevLink(infoPtr
, shift
);
1598 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1604 LRESULT Ret
= DLGC_HASSETSEL
;
1605 int vk
= (lParam
!= 0 ? (int)((LPMSG
)lParam
)->wParam
: 0);
1609 Ret
|= DLGC_WANTMESSAGE
;
1613 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1614 if(!SYSLINK_NoNextLink(infoPtr
, shift
))
1616 Ret
|= DLGC_WANTTAB
;
1620 Ret
|= DLGC_WANTCHARS
;
1632 pt
.x
= (short)LOWORD(lParam
);
1633 pt
.y
= (short)HIWORD(lParam
);
1635 GetClientRect(infoPtr
->Self
, &rc
);
1636 ScreenToClient(infoPtr
->Self
, &pt
);
1637 if(pt
.x
< 0 || pt
.y
< 0 || pt
.x
> rc
.right
|| pt
.y
> rc
.bottom
)
1642 if(SYSLINK_LinkAtPt(infoPtr
, &pt
, NULL
, FALSE
))
1647 return HTTRANSPARENT
;
1651 return SYSLINK_HitTest(infoPtr
, (PLHITTESTINFO
)lParam
);
1654 return SYSLINK_SetItem(infoPtr
, (PLITEM
)lParam
);
1657 return SYSLINK_GetItem(infoPtr
, (PLITEM
)lParam
);
1659 case LM_GETIDEALHEIGHT
:
1661 return SYSLINK_GetIdealSize(infoPtr
, (int)wParam
, (SIZE
*)lParam
);
1663 return SYSLINK_GetIdealHeight(infoPtr
);
1666 return SYSLINK_SetFocus(infoPtr
);
1669 return SYSLINK_KillFocus(infoPtr
);
1672 infoPtr
->Style
&= ~WS_DISABLED
;
1673 infoPtr
->Style
|= (wParam
? 0 : WS_DISABLED
);
1674 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
1677 case WM_STYLECHANGED
:
1678 if (wParam
== GWL_STYLE
)
1680 infoPtr
->Style
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
1682 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1688 CREATESTRUCTW
*cs
= (CREATESTRUCTW
*)lParam
;
1690 /* allocate memory for info struct */
1691 infoPtr
= Alloc (sizeof(SYSLINK_INFO
));
1692 if (!infoPtr
) return -1;
1693 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
1695 /* initialize the info struct */
1696 infoPtr
->Self
= hwnd
;
1697 infoPtr
->Notify
= cs
->hwndParent
;
1698 infoPtr
->Style
= cs
->style
;
1700 infoPtr
->LinkFont
= 0;
1701 list_init(&infoPtr
->Items
);
1702 infoPtr
->HasFocus
= FALSE
;
1703 infoPtr
->MouseDownID
= -1;
1704 infoPtr
->TextColor
= comctl32_color
.clrWindowText
;
1705 infoPtr
->LinkColor
= comctl32_color
.clrHighlight
;
1706 infoPtr
->VisitedColor
= comctl32_color
.clrHighlight
;
1707 infoPtr
->BreakChar
= ' ';
1708 infoPtr
->IgnoreReturn
= infoPtr
->Style
& LWS_IGNORERETURN
;
1709 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd
);
1710 SYSLINK_SetText(infoPtr
, cs
->lpszName
);
1714 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd
);
1715 SYSLINK_ClearDoc(infoPtr
);
1716 if(infoPtr
->Font
!= 0) DeleteObject(infoPtr
->Font
);
1717 if(infoPtr
->LinkFont
!= 0) DeleteObject(infoPtr
->LinkFont
);
1718 SetWindowLongPtrW(hwnd
, 0, 0);
1722 case WM_SYSCOLORCHANGE
:
1723 COMCTL32_RefreshSysColors();
1727 if ((message
>= WM_USER
) && (message
< WM_APP
) && !COMCTL32_IsReflectedMessage(message
))
1729 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message
, wParam
, lParam
);
1731 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1736 /***********************************************************************
1737 * SYSLINK_Register [Internal]
1739 * Registers the SysLink window class.
1741 VOID
SYSLINK_Register (void)
1745 ZeroMemory (&wndClass
, sizeof(wndClass
));
1746 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
1747 wndClass
.lpfnWndProc
= SysLinkWindowProc
;
1748 wndClass
.cbClsExtra
= 0;
1749 wndClass
.cbWndExtra
= sizeof (SYSLINK_INFO
*);
1750 wndClass
.hCursor
= LoadCursorW (0, (LPWSTR
)IDC_ARROW
);
1751 wndClass
.lpszClassName
= WC_LINK
;
1753 RegisterClassW (&wndClass
);
1757 /***********************************************************************
1758 * SYSLINK_Unregister [Internal]
1760 * Unregisters the SysLink window class.
1762 VOID
SYSLINK_Unregister (void)
1764 UnregisterClassW (WC_LINK
, NULL
);