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/unicode.h"
31 #include "wine/debug.h"
32 #include "wine/list.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(syslink
);
41 } DOC_TEXTBLOCK
, *PDOC_TEXTBLOCK
;
43 #define LIF_FLAGSMASK (LIF_STATE | LIF_ITEMID | LIF_URL)
44 #define LIS_MASK (LIS_FOCUSED | LIS_ENABLED | LIS_VISITED)
52 typedef struct _DOC_ITEM
55 UINT nText
; /* Number of characters of the text */
56 SL_ITEM_TYPE Type
; /* type of the item */
57 PDOC_TEXTBLOCK Blocks
; /* Array of text blocks */
62 UINT state
; /* Link state */
63 WCHAR
*szID
; /* Link ID string */
64 WCHAR
*szUrl
; /* Link URL string */
71 WCHAR Text
[1]; /* Text of the document item */
72 } DOC_ITEM
, *PDOC_ITEM
;
76 HWND Self
; /* The window handle for this control */
77 HWND Notify
; /* The parent handle to receive notifications */
78 DWORD Style
; /* Styles for this control */
79 struct list Items
; /* Document items list */
80 BOOL HasFocus
; /* Whether the control has the input focus */
81 int MouseDownID
; /* ID of the link that the mouse button first selected */
82 HFONT Font
; /* Handle to the font for text */
83 HFONT LinkFont
; /* Handle to the font for links */
84 COLORREF TextColor
; /* Color of the text */
85 COLORREF LinkColor
; /* Color of links */
86 COLORREF VisitedColor
; /* Color of visited links */
87 WCHAR BreakChar
; /* Break Character for the current font */
88 BOOL IgnoreReturn
; /* (infoPtr->Style & LWS_IGNORERETURN) on creation */
91 /* Control configuration constants */
93 #define SL_LEFTMARGIN (0)
94 #define SL_TOPMARGIN (0)
95 #define SL_RIGHTMARGIN (0)
96 #define SL_BOTTOMMARGIN (0)
98 /***********************************************************************
100 * Frees all data and gdi objects associated with a document item
102 static VOID
SYSLINK_FreeDocItem (PDOC_ITEM DocItem
)
104 if(DocItem
->Type
== slLink
)
106 Free(DocItem
->u
.Link
.szID
);
107 Free(DocItem
->u
.Link
.szUrl
);
110 Free(DocItem
->Blocks
);
112 /* we don't free Text because it's just a pointer to a character in the
113 entire window text string */
118 /***********************************************************************
119 * SYSLINK_AppendDocItem
120 * Create and append a new document item.
122 static PDOC_ITEM
SYSLINK_AppendDocItem (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
, UINT textlen
,
123 SL_ITEM_TYPE type
, PDOC_ITEM LastItem
)
127 textlen
= min(textlen
, strlenW(Text
));
128 Item
= Alloc(FIELD_OFFSET(DOC_ITEM
, Text
[textlen
+ 1]));
131 ERR("Failed to alloc DOC_ITEM structure!\n");
135 Item
->nText
= textlen
;
138 lstrcpynW(Item
->Text
, Text
, textlen
+ 1);
140 list_add_after(&LastItem
->entry
, &Item
->entry
);
142 list_add_tail(&infoPtr
->Items
, &Item
->entry
);
147 /***********************************************************************
149 * Clears the document tree
151 static VOID
SYSLINK_ClearDoc (SYSLINK_INFO
*infoPtr
)
153 DOC_ITEM
*Item
, *Item2
;
155 LIST_FOR_EACH_ENTRY_SAFE(Item
, Item2
, &infoPtr
->Items
, DOC_ITEM
, entry
)
157 list_remove(&Item
->entry
);
158 SYSLINK_FreeDocItem(Item
);
162 /***********************************************************************
164 * Parses the window text string and creates a document. Returns the
165 * number of document items created.
167 static UINT
SYSLINK_ParseText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
169 static const WCHAR SL_LINKOPEN
[] = { '<','a' };
170 static const WCHAR SL_HREF
[] = { 'h','r','e','f','=','\"' };
171 static const WCHAR SL_ID
[] = { 'i','d','=','\"' };
172 static const WCHAR SL_LINKCLOSE
[] = { '<','/','a','>' };
173 LPCWSTR current
, textstart
= NULL
, linktext
= NULL
, firsttag
= NULL
;
174 int taglen
= 0, textlen
= 0, linklen
= 0, docitems
= 0;
175 PDOC_ITEM Last
= NULL
;
176 SL_ITEM_TYPE CurrentType
= slText
;
180 TRACE("(%p %s)\n", infoPtr
, debugstr_w(Text
));
182 for(current
= Text
; *current
!= 0;)
186 if(!strncmpiW(current
, SL_LINKOPEN
, ARRAY_SIZE(SL_LINKOPEN
)) && (CurrentType
== slText
))
188 BOOL ValidParam
= FALSE
, ValidLink
= FALSE
;
190 if(*(current
+ 2) == '>')
192 /* we just have to deal with a <a> tag */
201 else if(*(current
+ 2) == infoPtr
->BreakChar
)
203 /* we expect parameters, parse them */
204 LPCWSTR
*CurrentParameter
= NULL
, tmp
;
205 UINT
*CurrentParameterLen
= NULL
;
208 tmp
= current
+ taglen
;
213 /* compare the current position with all known parameters */
214 if(!strncmpiW(tmp
, SL_HREF
, ARRAY_SIZE(SL_HREF
)))
218 CurrentParameter
= &lpUrl
;
219 CurrentParameterLen
= &lenUrl
;
221 else if(!strncmpiW(tmp
, SL_ID
, ARRAY_SIZE(SL_ID
)))
225 CurrentParameter
= &lpID
;
226 CurrentParameterLen
= &lenId
;
235 /* we got a known parameter, now search until the next " character.
236 If we can't find a " character, there's a syntax error and we just assume it's text */
238 *CurrentParameter
= current
+ taglen
;
239 *CurrentParameterLen
= 0;
241 for(tmp
= *CurrentParameter
; *tmp
!= 0; tmp
++)
250 (*CurrentParameterLen
)++;
255 /* we're done with this parameter, now there are only 2 possibilities:
256 * 1. another parameter is coming, so expect a ' ' (space) character
257 * 2. the tag is being closed, so expect a '<' character
259 if(*tmp
== infoPtr
->BreakChar
)
261 /* we expect another parameter, do the whole thing again */
268 /* the tag is being closed, we're done */
275 if(ValidLink
&& ValidParam
)
277 /* the <a ...> tag appears to be valid. save all information
278 so we can add the link if we find a valid </a> tag later */
279 CurrentType
= slLink
;
280 linktext
= current
+ taglen
;
289 if(textstart
== NULL
)
295 else if(!strncmpiW(current
, SL_LINKCLOSE
, ARRAY_SIZE(SL_LINKCLOSE
)) && (CurrentType
== slLink
) && firsttag
)
297 /* there's a <a...> tag opened, first add the previous text, if present */
298 if(textstart
!= NULL
&& textlen
> 0 && firsttag
> textstart
)
300 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, firsttag
- textstart
, slText
, Last
);
303 ERR("Unable to create new document item!\n");
311 /* now it's time to add the link to the document */
313 if(linktext
!= NULL
&& linklen
> 0)
315 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slLink
, Last
);
318 ERR("Unable to create new document item!\n");
322 if(CurrentType
== slLink
)
326 if(!(infoPtr
->Style
& WS_DISABLED
))
328 Last
->u
.Link
.state
|= LIS_ENABLED
;
330 /* Copy the tag parameters */
333 nc
= min(lenId
, strlenW(lpID
));
334 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
335 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
336 if(Last
->u
.Link
.szID
!= NULL
)
338 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
342 Last
->u
.Link
.szID
= NULL
;
345 nc
= min(lenUrl
, strlenW(lpUrl
));
346 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
347 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
348 if(Last
->u
.Link
.szUrl
!= NULL
)
350 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
354 Last
->u
.Link
.szUrl
= NULL
;
358 CurrentType
= slText
;
365 /* we don't know what tag it is, so just continue */
368 if(CurrentType
== slText
&& textstart
== NULL
)
382 /* save the pointer of the current text item if we couldn't find a tag */
383 if(textstart
== NULL
&& CurrentType
== slText
)
392 if(textstart
!= NULL
&& textlen
> 0)
394 Last
= SYSLINK_AppendDocItem(infoPtr
, textstart
, textlen
, CurrentType
, Last
);
397 ERR("Unable to create new document item!\n");
400 if(CurrentType
== slLink
)
404 if(!(infoPtr
->Style
& WS_DISABLED
))
406 Last
->u
.Link
.state
|= LIS_ENABLED
;
408 /* Copy the tag parameters */
411 nc
= min(lenId
, strlenW(lpID
));
412 nc
= min(nc
, MAX_LINKID_TEXT
- 1);
413 Last
->u
.Link
.szID
= Alloc((nc
+ 1) * sizeof(WCHAR
));
414 if(Last
->u
.Link
.szID
!= NULL
)
416 lstrcpynW(Last
->u
.Link
.szID
, lpID
, nc
+ 1);
420 Last
->u
.Link
.szID
= NULL
;
423 nc
= min(lenUrl
, strlenW(lpUrl
));
424 nc
= min(nc
, L_MAX_URL_LENGTH
- 1);
425 Last
->u
.Link
.szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
426 if(Last
->u
.Link
.szUrl
!= NULL
)
428 lstrcpynW(Last
->u
.Link
.szUrl
, lpUrl
, nc
+ 1);
432 Last
->u
.Link
.szUrl
= NULL
;
437 if(linktext
!= NULL
&& linklen
> 0)
439 /* we got an unclosed link, just display the text */
440 Last
= SYSLINK_AppendDocItem(infoPtr
, linktext
, linklen
, slText
, Last
);
443 ERR("Unable to create new document item!\n");
452 /***********************************************************************
453 * SYSLINK_RepaintLink
456 static VOID
SYSLINK_RepaintLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
461 if(DocItem
->Type
!= slLink
)
463 ERR("DocItem not a link!\n");
467 bl
= DocItem
->Blocks
;
474 InvalidateRect(infoPtr
->Self
, &bl
->rc
, TRUE
);
475 n
-= bl
->nChars
+ bl
->nSkip
;
481 /***********************************************************************
482 * SYSLINK_GetLinkItemByIndex
483 * Retrieves a document link by its index
485 static PDOC_ITEM
SYSLINK_GetLinkItemByIndex (const SYSLINK_INFO
*infoPtr
, int iLink
)
489 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
491 if ((Current
->Type
== slLink
) && (iLink
-- <= 0))
497 /***********************************************************************
498 * SYSLINK_GetFocusLink
499 * Retrieves the link that has the LIS_FOCUSED bit
501 static PDOC_ITEM
SYSLINK_GetFocusLink (const SYSLINK_INFO
*infoPtr
, int *LinkId
)
506 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
508 if(Current
->Type
== slLink
)
510 if(Current
->u
.Link
.state
& LIS_FOCUSED
)
523 /***********************************************************************
524 * SYSLINK_GetNextLink
527 static PDOC_ITEM
SYSLINK_GetNextLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
531 LIST_FOR_EACH_ENTRY(Next
, Current
? &Current
->entry
: &infoPtr
->Items
, DOC_ITEM
, entry
)
533 if (Next
->Type
== slLink
)
541 /***********************************************************************
542 * SYSLINK_GetPrevLink
543 * Gets the previous link
545 static PDOC_ITEM
SYSLINK_GetPrevLink (const SYSLINK_INFO
*infoPtr
, PDOC_ITEM Current
)
549 LIST_FOR_EACH_ENTRY_REV(Prev
, Current
? &Current
->entry
: list_tail(&infoPtr
->Items
), DOC_ITEM
, entry
)
551 if (Prev
->Type
== slLink
)
560 /***********************************************************************
562 * Tries to wrap a line.
564 static BOOL
SYSLINK_WrapLine (LPWSTR Text
, WCHAR BreakChar
, int x
, int *LineLen
,
565 int nFit
, LPSIZE Extent
)
569 for (i
= 0; i
< nFit
; i
++) if (Text
[i
] == '\n') break;
571 if (i
== *LineLen
) return FALSE
;
573 /* check if we're in the middle of a word */
574 if (Text
[i
] != '\n' && Text
[i
] != BreakChar
)
576 /* search for the beginning of the word */
577 while (i
&& Text
[i
- 1] != BreakChar
) i
--;
583 if (x
== SL_LEFTMARGIN
) i
= max( nFit
, 1 );
590 /***********************************************************************
592 * Renders the document in memory
594 static VOID
SYSLINK_Render (const SYSLINK_INFO
*infoPtr
, HDC hdc
, PRECT pRect
)
599 int x
, y
, LineHeight
;
603 szDoc
.cx
= szDoc
.cy
= 0;
606 rc
.right
-= SL_RIGHTMARGIN
;
607 rc
.bottom
-= SL_BOTTOMMARGIN
;
609 if(rc
.right
- SL_LEFTMARGIN
< 0)
611 if (rc
.bottom
- SL_TOPMARGIN
< 0)
614 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
618 GetTextMetricsW( hdc
, &tm
);
619 LineHeight
= tm
.tmHeight
+ tm
.tmExternalLeading
;
621 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
625 PDOC_TEXTBLOCK bl
, cbl
;
630 if(Current
->nText
== 0)
638 Free(Current
->Blocks
);
639 Current
->Blocks
= NULL
;
643 if(Current
->Type
== slText
)
645 SelectObject(hdc
, infoPtr
->Font
);
647 else if(Current
->Type
== slLink
)
649 SelectObject(hdc
, infoPtr
->LinkFont
);
654 /* skip break characters unless they're the first of the doc item */
655 if(tx
!= Current
->Text
|| x
== SL_LEFTMARGIN
)
657 if (n
&& *tx
== '\n')
663 while(n
> 0 && (*tx
) == infoPtr
->BreakChar
)
671 if((n
== 0 && SkipChars
!= 0) ||
672 GetTextExtentExPointW(hdc
, tx
, n
, rc
.right
- x
, &nFit
, NULL
, &szDim
))
680 Wrap
= SYSLINK_WrapLine(tx
, infoPtr
->BreakChar
, x
, &LineLen
, nFit
, &szDim
);
684 /* move one line down, the word didn't fit into the line */
692 if(!GetTextExtentExPointW(hdc
, tx
, LineLen
, rc
.right
- x
, NULL
, NULL
, &szDim
))
705 nbl
= ReAlloc(bl
, (nBlocks
+ 1) * sizeof(DOC_TEXTBLOCK
));
711 cbl
= bl
+ nBlocks
- 1;
713 cbl
->nChars
= LineLen
;
714 cbl
->nSkip
= SkipChars
;
715 SetRect(&cbl
->rc
, x
, y
, x
+ szDim
.cx
, y
+ szDim
.cy
);
717 if (cbl
->rc
.right
> szDoc
.cx
)
718 szDoc
.cx
= cbl
->rc
.right
;
719 if (cbl
->rc
.bottom
> szDoc
.cy
)
720 szDoc
.cy
= cbl
->rc
.bottom
;
738 ERR("Failed to alloc DOC_TEXTBLOCK structure!\n");
753 Current
->Blocks
= bl
;
757 SelectObject(hdc
, hOldFont
);
759 pRect
->right
= pRect
->left
+ szDoc
.cx
;
760 pRect
->bottom
= pRect
->top
+ szDoc
.cy
;
763 /***********************************************************************
765 * Draws the SysLink control.
767 static LRESULT
SYSLINK_Draw (const SYSLINK_INFO
*infoPtr
, HDC hdc
)
772 COLORREF OldTextColor
, OldBkColor
;
774 UINT text_flags
= ETO_CLIPPED
;
775 UINT mode
= GetBkMode( hdc
);
777 hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
778 OldTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
779 OldBkColor
= SetBkColor(hdc
, comctl32_color
.clrWindow
);
781 GetClientRect(infoPtr
->Self
, &rc
);
782 rc
.right
-= SL_RIGHTMARGIN
+ SL_LEFTMARGIN
;
783 rc
.bottom
-= SL_BOTTOMMARGIN
+ SL_TOPMARGIN
;
785 if(rc
.right
< 0 || rc
.bottom
< 0) return 0;
787 hBrush
= (HBRUSH
)SendMessageW(infoPtr
->Notify
, WM_CTLCOLORSTATIC
,
788 (WPARAM
)hdc
, (LPARAM
)infoPtr
->Self
);
789 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
791 FillRect(hdc
, &rc
, hBrush
);
792 if (GetBkMode( hdc
) == OPAQUE
) text_flags
|= ETO_OPAQUE
;
794 else SetBkMode( hdc
, TRANSPARENT
);
796 DeleteObject(hBrush
);
798 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
804 bl
= Current
->Blocks
;
810 if(Current
->Type
== slText
)
812 SelectObject(hdc
, infoPtr
->Font
);
813 SetTextColor(hdc
, infoPtr
->TextColor
);
817 SelectObject(hdc
, infoPtr
->LinkFont
);
818 SetTextColor(hdc
, (!(Current
->u
.Link
.state
& LIS_VISITED
) ? infoPtr
->LinkColor
: infoPtr
->VisitedColor
));
824 ExtTextOutW(hdc
, bl
->rc
.left
, bl
->rc
.top
, text_flags
, &bl
->rc
, tx
, bl
->nChars
, NULL
);
825 if((Current
->Type
== slLink
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && infoPtr
->HasFocus
)
827 COLORREF PrevTextColor
;
828 PrevTextColor
= SetTextColor(hdc
, infoPtr
->TextColor
);
829 DrawFocusRect(hdc
, &bl
->rc
);
830 SetTextColor(hdc
, PrevTextColor
);
833 n
-= bl
->nChars
+ bl
->nSkip
;
839 SetBkColor(hdc
, OldBkColor
);
840 SetTextColor(hdc
, OldTextColor
);
841 SelectObject(hdc
, hOldFont
);
842 SetBkMode(hdc
, mode
);
847 /***********************************************************************
849 * Handles the WM_PAINT message.
851 static LRESULT
SYSLINK_Paint (const SYSLINK_INFO
*infoPtr
, HDC hdcParam
)
856 hdc
= hdcParam
? hdcParam
: BeginPaint (infoPtr
->Self
, &ps
);
859 SYSLINK_Draw (infoPtr
, hdc
);
860 if (!hdcParam
) EndPaint (infoPtr
->Self
, &ps
);
865 /***********************************************************************
867 * Set new Font for the SysLink control.
869 static HFONT
SYSLINK_SetFont (SYSLINK_INFO
*infoPtr
, HFONT hFont
, BOOL bRedraw
)
875 HFONT hOldFont
= infoPtr
->Font
;
876 infoPtr
->Font
= hFont
;
878 /* free the underline font */
879 if(infoPtr
->LinkFont
!= NULL
)
881 DeleteObject(infoPtr
->LinkFont
);
882 infoPtr
->LinkFont
= NULL
;
885 /* Render text position and word wrapping in memory */
886 if (GetClientRect(infoPtr
->Self
, &rcClient
))
888 hdc
= GetDC(infoPtr
->Self
);
891 /* create a new underline font */
892 if(GetTextMetricsW(hdc
, &tm
) &&
893 GetObjectW(infoPtr
->Font
, sizeof(LOGFONTW
), &lf
))
895 lf
.lfUnderline
= TRUE
;
896 infoPtr
->LinkFont
= CreateFontIndirectW(&lf
);
897 infoPtr
->BreakChar
= tm
.tmBreakChar
;
901 ERR("Failed to create link font!\n");
904 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
905 ReleaseDC(infoPtr
->Self
, hdc
);
911 RedrawWindow(infoPtr
->Self
, NULL
, NULL
, RDW_INVALIDATE
| RDW_UPDATENOW
);
917 /***********************************************************************
919 * Set new text for the SysLink control.
921 static LRESULT
SYSLINK_SetText (SYSLINK_INFO
*infoPtr
, LPCWSTR Text
)
923 /* clear the document */
924 SYSLINK_ClearDoc(infoPtr
);
926 if(Text
== NULL
|| *Text
== 0)
931 /* let's parse the string and create a document */
932 if(SYSLINK_ParseText(infoPtr
, Text
) > 0)
936 /* Render text position and word wrapping in memory */
937 if (GetClientRect(infoPtr
->Self
, &rcClient
))
939 HDC hdc
= GetDC(infoPtr
->Self
);
942 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
943 ReleaseDC(infoPtr
->Self
, hdc
);
945 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
953 /***********************************************************************
954 * SYSLINK_SetFocusLink
955 * Updates the focus status bits and focusses the specified link.
956 * If no document item is specified, the focus bit will be removed from all links.
957 * Returns the previous focused item.
959 static PDOC_ITEM
SYSLINK_SetFocusLink (const SYSLINK_INFO
*infoPtr
, const DOC_ITEM
*DocItem
)
961 PDOC_ITEM Current
, PrevFocus
= NULL
;
963 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
965 if(Current
->Type
== slLink
)
967 if((PrevFocus
== NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
))
972 if(Current
== DocItem
)
974 Current
->u
.Link
.state
|= LIS_FOCUSED
;
978 Current
->u
.Link
.state
&= ~LIS_FOCUSED
;
986 /***********************************************************************
988 * Sets the states and attributes of a link item.
990 static LRESULT
SYSLINK_SetItem (const SYSLINK_INFO
*infoPtr
, const LITEM
*Item
)
996 BOOL Repaint
= FALSE
;
998 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1000 ERR("Invalid Flags!\n");
1004 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1007 ERR("Link %d couldn't be found\n", Item
->iLink
);
1011 if(Item
->mask
& LIF_ITEMID
)
1013 nc
= min(lstrlenW(Item
->szID
), MAX_LINKID_TEXT
- 1);
1014 szId
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1017 lstrcpynW(szId
, Item
->szID
, nc
+ 1);
1021 ERR("Unable to allocate memory for link id\n");
1026 if(Item
->mask
& LIF_URL
)
1028 nc
= min(lstrlenW(Item
->szUrl
), L_MAX_URL_LENGTH
- 1);
1029 szUrl
= Alloc((nc
+ 1) * sizeof(WCHAR
));
1032 lstrcpynW(szUrl
, Item
->szUrl
, nc
+ 1);
1038 ERR("Unable to allocate memory for link url\n");
1043 if(Item
->mask
& LIF_ITEMID
)
1045 Free(di
->u
.Link
.szID
);
1046 di
->u
.Link
.szID
= szId
;
1049 if(Item
->mask
& LIF_URL
)
1051 Free(di
->u
.Link
.szUrl
);
1052 di
->u
.Link
.szUrl
= szUrl
;
1055 if(Item
->mask
& LIF_STATE
)
1057 UINT oldstate
= di
->u
.Link
.state
;
1058 /* clear the masked bits */
1059 di
->u
.Link
.state
&= ~(Item
->stateMask
& LIS_MASK
);
1061 di
->u
.Link
.state
|= (Item
->state
& Item
->stateMask
) & LIS_MASK
;
1062 Repaint
= (oldstate
!= di
->u
.Link
.state
);
1064 /* update the focus */
1065 SYSLINK_SetFocusLink(infoPtr
, ((di
->u
.Link
.state
& LIS_FOCUSED
) ? di
: NULL
));
1070 SYSLINK_RepaintLink(infoPtr
, di
);
1076 /***********************************************************************
1078 * Retrieves the states and attributes of a link item.
1080 static LRESULT
SYSLINK_GetItem (const SYSLINK_INFO
*infoPtr
, PLITEM Item
)
1084 if(!(Item
->mask
& LIF_ITEMINDEX
) || !(Item
->mask
& (LIF_FLAGSMASK
)))
1086 ERR("Invalid Flags!\n");
1090 di
= SYSLINK_GetLinkItemByIndex(infoPtr
, Item
->iLink
);
1093 ERR("Link %d couldn't be found\n", Item
->iLink
);
1097 if(Item
->mask
& LIF_STATE
)
1099 Item
->state
= (di
->u
.Link
.state
& Item
->stateMask
);
1100 if(!infoPtr
->HasFocus
)
1102 /* remove the LIS_FOCUSED bit if the control doesn't have focus */
1103 Item
->state
&= ~LIS_FOCUSED
;
1107 if(Item
->mask
& LIF_ITEMID
)
1111 lstrcpyW(Item
->szID
, di
->u
.Link
.szID
);
1119 if(Item
->mask
& LIF_URL
)
1121 if(di
->u
.Link
.szUrl
)
1123 lstrcpyW(Item
->szUrl
, di
->u
.Link
.szUrl
);
1134 /***********************************************************************
1135 * SYSLINK_PtInDocItem
1136 * Determines if a point is in the region of a document item
1138 static BOOL
SYSLINK_PtInDocItem (const DOC_ITEM
*DocItem
, POINT pt
)
1143 bl
= DocItem
->Blocks
;
1150 if (PtInRect(&bl
->rc
, pt
))
1154 n
-= bl
->nChars
+ bl
->nSkip
;
1162 /***********************************************************************
1164 * Determines the link the user clicked on.
1166 static LRESULT
SYSLINK_HitTest (const SYSLINK_INFO
*infoPtr
, PLHITTESTINFO HitTest
)
1171 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
1173 if(Current
->Type
== slLink
)
1175 if(SYSLINK_PtInDocItem(Current
, HitTest
->pt
))
1177 HitTest
->item
.mask
= 0;
1178 HitTest
->item
.iLink
= id
;
1179 HitTest
->item
.state
= 0;
1180 HitTest
->item
.stateMask
= 0;
1181 if(Current
->u
.Link
.szID
)
1183 lstrcpyW(HitTest
->item
.szID
, Current
->u
.Link
.szID
);
1187 HitTest
->item
.szID
[0] = 0;
1189 if(Current
->u
.Link
.szUrl
)
1191 lstrcpyW(HitTest
->item
.szUrl
, Current
->u
.Link
.szUrl
);
1195 HitTest
->item
.szUrl
[0] = 0;
1206 /***********************************************************************
1207 * SYSLINK_GetIdealHeight
1208 * Returns the preferred height of a link at the current control's width.
1210 static LRESULT
SYSLINK_GetIdealHeight (const SYSLINK_INFO
*infoPtr
)
1212 HDC hdc
= GetDC(infoPtr
->Self
);
1217 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1219 if(GetTextMetricsW(hdc
, &tm
))
1221 height
= tm
.tmHeight
;
1227 SelectObject(hdc
, hOldFont
);
1228 ReleaseDC(infoPtr
->Self
, hdc
);
1235 /***********************************************************************
1236 * SYSLINK_SendParentNotify
1237 * Sends a WM_NOTIFY message to the parent window.
1239 static LRESULT
SYSLINK_SendParentNotify (const SYSLINK_INFO
*infoPtr
, UINT code
, const DOC_ITEM
*Link
, int iLink
)
1243 nml
.hdr
.hwndFrom
= infoPtr
->Self
;
1244 nml
.hdr
.idFrom
= GetWindowLongPtrW(infoPtr
->Self
, GWLP_ID
);
1245 nml
.hdr
.code
= code
;
1248 nml
.item
.iLink
= iLink
;
1250 nml
.item
.stateMask
= 0;
1251 if(Link
->u
.Link
.szID
)
1253 lstrcpyW(nml
.item
.szID
, Link
->u
.Link
.szID
);
1257 nml
.item
.szID
[0] = 0;
1259 if(Link
->u
.Link
.szUrl
)
1261 lstrcpyW(nml
.item
.szUrl
, Link
->u
.Link
.szUrl
);
1265 nml
.item
.szUrl
[0] = 0;
1268 return SendMessageW(infoPtr
->Notify
, WM_NOTIFY
, nml
.hdr
.idFrom
, (LPARAM
)&nml
);
1271 /***********************************************************************
1273 * Handles receiving the input focus.
1275 static LRESULT
SYSLINK_SetFocus (SYSLINK_INFO
*infoPtr
)
1279 infoPtr
->HasFocus
= TRUE
;
1281 /* We always select the first link, even if we activated the control using
1282 SHIFT+TAB. This is the default behavior */
1283 Focus
= SYSLINK_GetNextLink(infoPtr
, NULL
);
1286 SYSLINK_SetFocusLink(infoPtr
, Focus
);
1287 SYSLINK_RepaintLink(infoPtr
, Focus
);
1292 /***********************************************************************
1294 * Handles losing the input focus.
1296 static LRESULT
SYSLINK_KillFocus (SYSLINK_INFO
*infoPtr
)
1300 infoPtr
->HasFocus
= FALSE
;
1301 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1305 SYSLINK_RepaintLink(infoPtr
, Focus
);
1311 /***********************************************************************
1313 * Returns a link at the specified position
1315 static PDOC_ITEM
SYSLINK_LinkAtPt (const SYSLINK_INFO
*infoPtr
, const POINT
*pt
, int *LinkId
, BOOL MustBeEnabled
)
1320 LIST_FOR_EACH_ENTRY(Current
, &infoPtr
->Items
, DOC_ITEM
, entry
)
1322 if((Current
->Type
== slLink
) && SYSLINK_PtInDocItem(Current
, *pt
) &&
1323 (!MustBeEnabled
|| (Current
->u
.Link
.state
& LIS_ENABLED
)))
1337 /***********************************************************************
1338 * SYSLINK_LButtonDown
1339 * Handles mouse clicks
1341 static LRESULT
SYSLINK_LButtonDown (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1343 PDOC_ITEM Current
, Old
;
1346 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1349 SetFocus(infoPtr
->Self
);
1351 Old
= SYSLINK_SetFocusLink(infoPtr
, Current
);
1352 if(Old
!= NULL
&& Old
!= Current
)
1354 SYSLINK_RepaintLink(infoPtr
, Old
);
1356 infoPtr
->MouseDownID
= id
;
1357 SYSLINK_RepaintLink(infoPtr
, Current
);
1363 /***********************************************************************
1365 * Handles mouse clicks
1367 static LRESULT
SYSLINK_LButtonUp (SYSLINK_INFO
*infoPtr
, const POINT
*pt
)
1369 if(infoPtr
->MouseDownID
> -1)
1374 Current
= SYSLINK_LinkAtPt(infoPtr
, pt
, &id
, TRUE
);
1375 if((Current
!= NULL
) && (Current
->u
.Link
.state
& LIS_FOCUSED
) && (infoPtr
->MouseDownID
== id
))
1377 SYSLINK_SendParentNotify(infoPtr
, NM_CLICK
, Current
, id
);
1381 infoPtr
->MouseDownID
= -1;
1386 /***********************************************************************
1388 * Handles ENTER key events
1390 static BOOL
SYSLINK_OnEnter (const SYSLINK_INFO
*infoPtr
)
1392 if(infoPtr
->HasFocus
&& !infoPtr
->IgnoreReturn
)
1397 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1400 SYSLINK_SendParentNotify(infoPtr
, NM_RETURN
, Focus
, id
);
1407 /***********************************************************************
1408 * SYSKEY_SelectNextPrevLink
1409 * Changes the currently focused link
1411 static BOOL
SYSKEY_SelectNextPrevLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1413 if(infoPtr
->HasFocus
)
1418 Focus
= SYSLINK_GetFocusLink(infoPtr
, &id
);
1421 PDOC_ITEM NewFocus
, OldFocus
;
1424 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1426 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1428 if(NewFocus
!= NULL
)
1430 OldFocus
= SYSLINK_SetFocusLink(infoPtr
, NewFocus
);
1432 if(OldFocus
&& OldFocus
!= NewFocus
)
1434 SYSLINK_RepaintLink(infoPtr
, OldFocus
);
1436 SYSLINK_RepaintLink(infoPtr
, NewFocus
);
1444 /***********************************************************************
1445 * SYSKEY_SelectNextPrevLink
1446 * Determines if there's a next or previous link to decide whether the control
1447 * should capture the tab key message
1449 static BOOL
SYSLINK_NoNextLink (const SYSLINK_INFO
*infoPtr
, BOOL Prev
)
1451 PDOC_ITEM Focus
, NewFocus
;
1453 Focus
= SYSLINK_GetFocusLink(infoPtr
, NULL
);
1455 NewFocus
= SYSLINK_GetPrevLink(infoPtr
, Focus
);
1457 NewFocus
= SYSLINK_GetNextLink(infoPtr
, Focus
);
1459 return NewFocus
== NULL
;
1462 /***********************************************************************
1463 * SYSLINK_GetIdealSize
1464 * Calculates the ideal size of a link control at a given maximum width.
1466 static LONG
SYSLINK_GetIdealSize (const SYSLINK_INFO
*infoPtr
, int cxMaxWidth
, SIZE
*lpSize
)
1471 rc
.left
= rc
.top
= rc
.bottom
= 0;
1472 rc
.right
= cxMaxWidth
;
1474 hdc
= GetDC(infoPtr
->Self
);
1477 HGDIOBJ hOldFont
= SelectObject(hdc
, infoPtr
->Font
);
1479 SYSLINK_Render(infoPtr
, hdc
, &rc
);
1481 SelectObject(hdc
, hOldFont
);
1482 ReleaseDC(infoPtr
->Self
, hdc
);
1484 lpSize
->cx
= rc
.right
;
1485 lpSize
->cy
= rc
.bottom
;
1491 /***********************************************************************
1494 static LRESULT WINAPI
SysLinkWindowProc(HWND hwnd
, UINT message
,
1495 WPARAM wParam
, LPARAM lParam
)
1497 SYSLINK_INFO
*infoPtr
;
1499 TRACE("hwnd=%p msg=%04x wparam=%lx lParam=%lx\n", hwnd
, message
, wParam
, lParam
);
1501 infoPtr
= (SYSLINK_INFO
*)GetWindowLongPtrW(hwnd
, 0);
1503 if (!infoPtr
&& message
!= WM_CREATE
)
1504 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1507 case WM_PRINTCLIENT
:
1509 return SYSLINK_Paint (infoPtr
, (HDC
)wParam
);
1512 if (!(infoPtr
->Style
& LWS_TRANSPARENT
))
1514 HDC hdc
= (HDC
)wParam
;
1515 HBRUSH brush
= CreateSolidBrush( comctl32_color
.clrWindow
);
1518 GetClipBox( hdc
, &rect
);
1519 FillRect( hdc
, &rect
, brush
);
1520 DeleteObject( brush
);
1528 DWORD mp
= GetMessagePos();
1530 ht
.pt
.x
= (short)LOWORD(mp
);
1531 ht
.pt
.y
= (short)HIWORD(mp
);
1533 ScreenToClient(infoPtr
->Self
, &ht
.pt
);
1534 if(SYSLINK_HitTest (infoPtr
, &ht
))
1536 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_HAND
));
1540 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1546 if (GetClientRect(infoPtr
->Self
, &rcClient
))
1548 HDC hdc
= GetDC(infoPtr
->Self
);
1551 SYSLINK_Render(infoPtr
, hdc
, &rcClient
);
1552 ReleaseDC(infoPtr
->Self
, hdc
);
1559 return (LRESULT
)infoPtr
->Font
;
1562 return (LRESULT
)SYSLINK_SetFont(infoPtr
, (HFONT
)wParam
, (BOOL
)lParam
);
1565 SYSLINK_SetText(infoPtr
, (LPWSTR
)lParam
);
1566 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1568 case WM_LBUTTONDOWN
:
1571 pt
.x
= (short)LOWORD(lParam
);
1572 pt
.y
= (short)HIWORD(lParam
);
1573 return SYSLINK_LButtonDown(infoPtr
, &pt
);
1578 pt
.x
= (short)LOWORD(lParam
);
1579 pt
.y
= (short)HIWORD(lParam
);
1580 return SYSLINK_LButtonUp(infoPtr
, &pt
);
1588 SYSLINK_OnEnter(infoPtr
);
1592 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1593 SYSKEY_SelectNextPrevLink(infoPtr
, shift
);
1597 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1603 LRESULT Ret
= DLGC_HASSETSEL
;
1604 int vk
= (lParam
!= 0 ? (int)((LPMSG
)lParam
)->wParam
: 0);
1608 Ret
|= DLGC_WANTMESSAGE
;
1612 BOOL shift
= GetKeyState(VK_SHIFT
) & 0x8000;
1613 if(!SYSLINK_NoNextLink(infoPtr
, shift
))
1615 Ret
|= DLGC_WANTTAB
;
1619 Ret
|= DLGC_WANTCHARS
;
1631 pt
.x
= (short)LOWORD(lParam
);
1632 pt
.y
= (short)HIWORD(lParam
);
1634 GetClientRect(infoPtr
->Self
, &rc
);
1635 ScreenToClient(infoPtr
->Self
, &pt
);
1636 if(pt
.x
< 0 || pt
.y
< 0 || pt
.x
> rc
.right
|| pt
.y
> rc
.bottom
)
1641 if(SYSLINK_LinkAtPt(infoPtr
, &pt
, NULL
, FALSE
))
1646 return HTTRANSPARENT
;
1650 return SYSLINK_HitTest(infoPtr
, (PLHITTESTINFO
)lParam
);
1653 return SYSLINK_SetItem(infoPtr
, (PLITEM
)lParam
);
1656 return SYSLINK_GetItem(infoPtr
, (PLITEM
)lParam
);
1658 case LM_GETIDEALHEIGHT
:
1660 return SYSLINK_GetIdealSize(infoPtr
, (int)wParam
, (SIZE
*)lParam
);
1662 return SYSLINK_GetIdealHeight(infoPtr
);
1665 return SYSLINK_SetFocus(infoPtr
);
1668 return SYSLINK_KillFocus(infoPtr
);
1671 infoPtr
->Style
&= ~WS_DISABLED
;
1672 infoPtr
->Style
|= (wParam
? 0 : WS_DISABLED
);
1673 InvalidateRect (infoPtr
->Self
, NULL
, FALSE
);
1676 case WM_STYLECHANGED
:
1677 if (wParam
== GWL_STYLE
)
1679 infoPtr
->Style
= ((LPSTYLESTRUCT
)lParam
)->styleNew
;
1681 InvalidateRect(infoPtr
->Self
, NULL
, TRUE
);
1687 CREATESTRUCTW
*cs
= (CREATESTRUCTW
*)lParam
;
1689 /* allocate memory for info struct */
1690 infoPtr
= Alloc (sizeof(SYSLINK_INFO
));
1691 if (!infoPtr
) return -1;
1692 SetWindowLongPtrW (hwnd
, 0, (DWORD_PTR
)infoPtr
);
1694 /* initialize the info struct */
1695 infoPtr
->Self
= hwnd
;
1696 infoPtr
->Notify
= cs
->hwndParent
;
1697 infoPtr
->Style
= cs
->style
;
1699 infoPtr
->LinkFont
= 0;
1700 list_init(&infoPtr
->Items
);
1701 infoPtr
->HasFocus
= FALSE
;
1702 infoPtr
->MouseDownID
= -1;
1703 infoPtr
->TextColor
= comctl32_color
.clrWindowText
;
1704 infoPtr
->LinkColor
= comctl32_color
.clrHighlight
;
1705 infoPtr
->VisitedColor
= comctl32_color
.clrHighlight
;
1706 infoPtr
->BreakChar
= ' ';
1707 infoPtr
->IgnoreReturn
= infoPtr
->Style
& LWS_IGNORERETURN
;
1708 TRACE("SysLink Ctrl creation, hwnd=%p\n", hwnd
);
1709 SYSLINK_SetText(infoPtr
, cs
->lpszName
);
1713 TRACE("SysLink Ctrl destruction, hwnd=%p\n", hwnd
);
1714 SYSLINK_ClearDoc(infoPtr
);
1715 if(infoPtr
->Font
!= 0) DeleteObject(infoPtr
->Font
);
1716 if(infoPtr
->LinkFont
!= 0) DeleteObject(infoPtr
->LinkFont
);
1717 SetWindowLongPtrW(hwnd
, 0, 0);
1721 case WM_SYSCOLORCHANGE
:
1722 COMCTL32_RefreshSysColors();
1726 if ((message
>= WM_USER
) && (message
< WM_APP
) && !COMCTL32_IsReflectedMessage(message
))
1728 ERR("unknown msg %04x wp=%04lx lp=%08lx\n", message
, wParam
, lParam
);
1730 return DefWindowProcW(hwnd
, message
, wParam
, lParam
);
1735 /***********************************************************************
1736 * SYSLINK_Register [Internal]
1738 * Registers the SysLink window class.
1740 VOID
SYSLINK_Register (void)
1744 ZeroMemory (&wndClass
, sizeof(wndClass
));
1745 wndClass
.style
= CS_GLOBALCLASS
| CS_VREDRAW
| CS_HREDRAW
;
1746 wndClass
.lpfnWndProc
= SysLinkWindowProc
;
1747 wndClass
.cbClsExtra
= 0;
1748 wndClass
.cbWndExtra
= sizeof (SYSLINK_INFO
*);
1749 wndClass
.hCursor
= LoadCursorW (0, (LPWSTR
)IDC_ARROW
);
1750 wndClass
.lpszClassName
= WC_LINK
;
1752 RegisterClassW (&wndClass
);
1756 /***********************************************************************
1757 * SYSLINK_Unregister [Internal]
1759 * Unregisters the SysLink window class.
1761 VOID
SYSLINK_Unregister (void)
1763 UnregisterClassW (WC_LINK
, NULL
);