]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/hyperlink.py
1 # --------------------------------------------------------------------------- #
2 # HYPERLINKSCTRL wxPython IMPLEMENTATION
3 # Ported From Angelo Mandato C++ Code By:
5 # Andrea Gavana, @ 27 Mar 2005
6 # Latest Revision: 27 Apr 2005, 22.30 CET
9 # Original Web Site (For The C++ Code):
11 # http://www.spaceblue.com/codedetail.php?CodeID=7
14 # For all kind of problems, requests of enhancements and bug reports, please
17 # andrea.gavana@agip.it
20 # Or, obviously, to the wxPython mailing list!!!
24 # --------------------------------------------------------------------------- #
27 `HyperLinkCtrl` is a control for wxPython that acts like a hyper link
28 in a typical browser. Latest features include the ability to capture
29 your own Left, Middle, and Right click events to perform your own
30 custom event handling and ability to open link in a new or current
33 Special thanks to Robin Dunn for the event binder for the 3 mouse buttons.
36 Latest Revision: Andrea Gavana @ 11 May 2005, 21.00 CET
41 from wx
.lib
.stattext
import GenStaticText
as StaticText
43 # Import the useful webbrowser module for platform-independent results
46 # Set no delay time to open the web page
47 webbrowser
.PROCESS_CREATION_DELAY
= 0
49 # To show a popup that copies the hyperlinks on the clipboard
50 wxHYPERLINKS_POPUP_COPY
= 1000
53 #-----------------------------------#
55 #-----------------------------------#
57 # wxEVT_HYPERLINK_LEFT: Respond To A Left Mouse Button Event
58 # wxEVT_HYPERLINK_MIDDLE: Respond To A Middle Mouse Button Event
59 # wxEVT_HYPERLINK_RIGHT: Respond To A Right Mouse Button Event
61 wxEVT_HYPERLINK_LEFT
= wx
.NewEventType()
62 wxEVT_HYPERLINK_MIDDLE
= wx
.NewEventType()
63 wxEVT_HYPERLINK_RIGHT
= wx
.NewEventType()
65 EVT_HYPERLINK_LEFT
= wx
.PyEventBinder(wxEVT_HYPERLINK_LEFT
, 1)
66 EVT_HYPERLINK_MIDDLE
= wx
.PyEventBinder(wxEVT_HYPERLINK_MIDDLE
, 1)
67 EVT_HYPERLINK_RIGHT
= wx
.PyEventBinder(wxEVT_HYPERLINK_RIGHT
, 1)
70 # ------------------------------------------------------------
71 # This class implements the event listener for the hyperlinks
72 # ------------------------------------------------------------
74 class HyperLinkEvent(wx
.PyCommandEvent
):
76 Event object sent in response to clicking on a `HyperLinkCtrl`.
79 def __init__(self
, eventType
, id):
80 """ Default Class Constructor. """
81 wx
.PyCommandEvent
.__init
__(self
, eventType
, id)
82 self
._eventType
= eventType
85 def SetPosition(self
, pos
):
86 """ Sets Event Position """
90 def GetPosition(self
):
91 """ Returns Event Position """
95 # -------------------------------------------------
96 # This is the main HyperLinkCtrl implementation
97 # it user the StatiText from wx.lib.stattext
98 # because of its "quasi-dynamic" behavior
99 # -------------------------------------------------
101 class HyperLinkCtrl(StaticText
):
103 `HyperLinkCtrl` is a control for wxPython that acts like a hyper
104 link in a typical browser. Latest features include the ability to
105 capture your own Left, Middle, and Right click events to perform
106 your own custom event handling and ability to open link in a new
107 or current browser window.
111 ==================== =======================================
112 EVT_HYPERLINK_LEFT Sent when the left mouse button is
113 clicked, but only if `AutoBrowse` is set
115 EVT_HYPERLINK_MIDDLE Sent when the middle mouse button is
117 EVT_HYPERLINK_RIGHT Sent when the right mouse button is
118 clicked, but only if `DoPopup` is set
120 ==================== =======================================
123 def __init__(self
, parent
, id=-1, label
="", pos
=wx
.DefaultPosition
,
124 size
=wx
.DefaultSize
, style
=0, name
="staticText", URL
=""):
126 Default class constructor.
128 Pass URL == "" to use the label as the url link to navigate to
131 StaticText
.__init
__(self
, parent
, id, label
, pos
, size
,
134 if URL
.strip() == "":
140 self
.SetToolTip(wx
.ToolTip(self
._URL
))
142 # Set default properties
146 # default: True, True, True
149 # default: blue, violet, blue
156 self
.EnableRollover()
161 # default: wx.CURSOR_HAND
171 self
.OpenInSameWindow()
173 # Set control properties and refresh
174 self
.UpdateLink(True)
176 self
.Bind(wx
.EVT_MOUSE_EVENTS
, self
.OnMouseEvent
)
177 self
.Bind(wx
.EVT_MOTION
, self
.OnMouseEvent
)
180 def GotoURL(self
, URL
, ReportErrors
=True, NotSameWinIfPossible
=False):
182 Goto The Specified URL.
184 :param ReportErrors: Use True to display error dialog if an
185 error occurrs navigating to the URL.
187 :param NotSameWinIfPossible: Use True to attempt to open the
188 URL in new browser window.
192 logOff
= wx
.LogNull()
195 webbrowser
.open(URL
, new
=NotSameWinIfPossible
)
196 self
.SetVisited(True)
197 self
.UpdateLink(True)
202 self
.DisplayError("Unable To Launch Browser.", ReportErrors
)
206 def OnMouseEvent(self
, event
):
207 """ Captures mouse events for cursor, link colors and underlines. """
210 # Mouse Is Moving On The StaticText
211 # Set The Hand Cursor On The Link
212 self
.SetCursor(self
._CursorHand
)
214 if self
._EnableRollover
:
215 self
.SetForegroundColour(self
._LinkRolloverColor
)
216 fontTemp
= self
.GetFont()
217 fontTemp
.SetUnderlined(self
._RolloverUnderline
)
219 fontTemp
.SetWeight(wx
.BOLD
)
221 self
.SetFont(fontTemp
)
225 # Restore The Original Cursor
226 self
.SetCursor(wx
.NullCursor
)
227 if self
._EnableRollover
:
228 self
.UpdateLink(True)
231 # Left Button Was Pressed
233 self
.GotoURL(self
._URL
, self
._ReportErrors
,
234 self
._NotSameWinIfPossible
)
237 eventOut
= HyperLinkEvent(wxEVT_HYPERLINK_LEFT
, self
.GetId())
238 eventOut
.SetEventObject(self
)
239 eventOut
.SetPosition(event
.GetPosition())
240 self
.GetEventHandler().ProcessEvent(eventOut
)
242 self
.SetVisited(True)
244 elif event
.RightUp():
245 # Right Button Was Pressed
247 # Popups A Menu With The "Copy HyperLynks" Feature
248 menuPopUp
= wx
.Menu("", wx
.MENU_TEAROFF
)
249 menuPopUp
.Append(wxHYPERLINKS_POPUP_COPY
, "Copy HyperLink")
250 self
.Bind(wx
.EVT_MENU
, self
.OnPopUpCopy
, id=wxHYPERLINKS_POPUP_COPY
)
251 self
.PopupMenu(menuPopUp
, wx
.Point(event
.m_x
, event
.m_y
))
253 self
.Unbind(wx
.EVT_MENU
, id=wxHYPERLINKS_POPUP_COPY
)
256 eventOut
= HyperLinkEvent(wxEVT_HYPERLINK_RIGHT
, self
.GetId())
257 eventOut
.SetEventObject(self
)
258 eventOut
.SetPosition(event
.GetPosition())
259 self
.GetEventHandler().ProcessEvent(eventOut
)
261 elif event
.MiddleUp():
262 # Middle Button Was Pressed
263 eventOut
= HyperLinkEvent(wxEVT_HYPERLINK_MIDDLE
, self
.GetId())
264 eventOut
.SetEventObject(self
)
265 eventOut
.SetPosition(event
.GetPosition())
266 self
.GetEventHandler().ProcessEvent(eventOut
)
271 def OnPopUpCopy(self
, event
):
272 """ Copy data from the HyperLink to the clipboard. """
274 wx
.TheClipboard
.UsePrimarySelection(False)
275 if not wx
.TheClipboard
.Open():
277 data
= wx
.TextDataObject(self
._URL
)
278 wx
.TheClipboard
.SetData(data
)
279 wx
.TheClipboard
.Close()
282 def UpdateLink(self
, OnRefresh
=True):
286 Changing text properties if:
287 - User Specific Setting
293 fontTemp
= self
.GetFont()
296 self
.SetForegroundColour(self
._VisitedColour
)
297 fontTemp
.SetUnderlined(self
._VisitedUnderline
)
301 self
.SetForegroundColour(self
._LinkColour
)
302 fontTemp
.SetUnderlined(self
._LinkUnderline
)
306 fontTemp
.SetWeight(wx
.BOLD
)
308 self
.SetFont(fontTemp
)
309 self
.Refresh(OnRefresh
)
312 def DisplayError(self
, ErrorMessage
, ReportErrors
=True):
314 Displays an error message (according to ReportErrors variable)
318 wx
.MessageBox(ErrorMessage
, "HyperLinks Error", wx
.OK | wx
.CENTRE | wx
.ICON_ERROR
)
322 link
=wx
.Colour(0, 0, 255),
323 visited
=wx
.Colour(79, 47, 79),
324 rollover
=wx
.Colour(0, 0, 255)):
325 """ Sets the colours for the link, the visited link and the mouse rollover.
329 - Visited Link: VIOLET
333 self
._LinkColour
= link
334 self
._VisitedColour
= visited
335 self
._LinkRolloverColor
= rollover
338 def GetColours(self
):
340 Gets the colours for the link, the visited link and the mouse
343 return self
._LinkColour
, self
._VisitedColour
, self
._LinkRolloverColor
346 def SetUnderlines(self
, link
=True, visited
=True, rollover
=True):
347 """ Underlines Properties. """
348 self
._LinkUnderline
= link
349 self
._RolloverUnderline
= rollover
350 self
._VisitedUnderline
= visited
353 def GetUnderlines(self
):
355 Returns if link is underlined, if the mouse rollover is
356 underlined and if the visited link is underlined.
358 return self
._LinkUnderline
, self
._RolloverUnderline
, self
._VisitedUnderline
361 def SetLinkCursor(self
, cur
=wx
.CURSOR_HAND
):
362 """ Sets link cursor properties. """
363 self
._CursorHand
= wx
.StockCursor(cur
)
366 def GetLinkCursor(self
):
367 """ Gets the link cursor. """
368 return self
._CursorHand
371 def SetVisited(self
, Visited
=False):
372 """ Sets a link as visited. """
374 self
._Visited
= Visited
377 def GetVisited(self
):
378 """ Returns whether a link has been visited or not. """
382 def SetBold(self
, Bold
=False):
383 """ Sets the HyperLink in bold text. """
388 """ Returns whether the HyperLink has text in bold or not. """
392 def SetURL(self
, URL
):
393 """ Sets the HyperLink text to the specified URL. """
398 """ Retrieve the URL associated to the HyperLink. """
402 def OpenInSameWindow(self
, NotSameWinIfPossible
=False):
403 """ Open multiple URL in the same window (if possible). """
404 self
._NotSameWinIfPossible
= NotSameWinIfPossible
407 def EnableRollover(self
, EnableRollover
=False):
408 """ Enable/disable rollover. """
409 self
._EnableRollover
= EnableRollover
412 def ReportErrors(self
, ReportErrors
=True):
413 """ Set whether to report browser errors or not. """
414 self
._ReportErrors
= ReportErrors
417 def AutoBrowse(self
, AutoBrowse
=True):
419 Automatically browse to URL when clicked. set to False to
420 receive EVT_HYPERLINK_LEFT event.
422 self
._AutoBrowse
= AutoBrowse
425 def DoPopup(self
, DoPopup
=True):
426 """ Sets whether to show popup menu on right click or not. """
427 self
._DoPopup
= DoPopup