]>
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: 05 Nov 2005, 22.30 CET
9 # Original Web Site (For The C++ Code):
11 # http://www.spaceblue.com/codedetail.php?CodeID=7
14 # Thanks to E. A. Tacao for his nice suggestions and improvements of the code.
16 # For all kind of problems, requests of enhancements and bug reports, please
19 # andrea.gavana@agip.it
22 # Or, obviously, to the wxPython mailing list!!!
26 # --------------------------------------------------------------------------- #
29 `HyperLinkCtrl` is a control for wxPython that acts like a hyper link
30 in a typical browser. Latest features include the ability to capture
31 your own Left, Middle, and Right click events to perform your own
32 custom event handling and ability to open link in a new or current
35 Special thanks to Robin Dunn for the event binder for the 3 mouse buttons.
38 Latest Revision: Andrea Gavana @ 05 Nov 2005, 22.30 CET
43 from wx
.lib
.stattext
import GenStaticText
as StaticText
45 # Import the useful webbrowser module for platform-independent results
48 # Set no delay time to open the web page
49 webbrowser
.PROCESS_CREATION_DELAY
= 0
51 # To show a popup that copies the hyperlinks on the clipboard
52 wxHYPERLINKS_POPUP_COPY
= 1000
55 #-----------------------------------#
57 #-----------------------------------#
59 # wxEVT_HYPERLINK_LEFT: Respond To A Left Mouse Button Event
60 # wxEVT_HYPERLINK_MIDDLE: Respond To A Middle Mouse Button Event
61 # wxEVT_HYPERLINK_RIGHT: Respond To A Right Mouse Button Event
63 wxEVT_HYPERLINK_LEFT
= wx
.NewEventType()
64 wxEVT_HYPERLINK_MIDDLE
= wx
.NewEventType()
65 wxEVT_HYPERLINK_RIGHT
= wx
.NewEventType()
67 EVT_HYPERLINK_LEFT
= wx
.PyEventBinder(wxEVT_HYPERLINK_LEFT
, 1)
68 EVT_HYPERLINK_MIDDLE
= wx
.PyEventBinder(wxEVT_HYPERLINK_MIDDLE
, 1)
69 EVT_HYPERLINK_RIGHT
= wx
.PyEventBinder(wxEVT_HYPERLINK_RIGHT
, 1)
72 # ------------------------------------------------------------
73 # This class implements the event listener for the hyperlinks
74 # ------------------------------------------------------------
76 class HyperLinkEvent(wx
.PyCommandEvent
):
78 Event object sent in response to clicking on a `HyperLinkCtrl`.
81 def __init__(self
, eventType
, id):
82 """ Default Class Constructor. """
83 wx
.PyCommandEvent
.__init
__(self
, eventType
, id)
84 self
._eventType
= eventType
87 def SetPosition(self
, pos
):
88 """ Sets Event Position """
92 def GetPosition(self
):
93 """ Returns Event Position """
97 # -------------------------------------------------
98 # This is the main HyperLinkCtrl implementation
99 # it user the StatiText from wx.lib.stattext
100 # because of its "quasi-dynamic" behavior
101 # -------------------------------------------------
103 class HyperLinkCtrl(StaticText
):
105 `HyperLinkCtrl` is a control for wxPython that acts like a hyper
106 link in a typical browser. Latest features include the ability to
107 capture your own Left, Middle, and Right click events to perform
108 your own custom event handling and ability to open link in a new
109 or current browser window.
113 ==================== =======================================
114 EVT_HYPERLINK_LEFT Sent when the left mouse button is
115 clicked, but only if `AutoBrowse` is set
117 EVT_HYPERLINK_MIDDLE Sent when the middle mouse button is
119 EVT_HYPERLINK_RIGHT Sent when the right mouse button is
120 clicked, but only if `DoPopup` is set
122 ==================== =======================================
125 def __init__(self
, parent
, id=-1, label
="", pos
=wx
.DefaultPosition
,
126 size
=wx
.DefaultSize
, style
=0, name
="staticText", URL
=""):
128 Default class constructor.
130 Pass URL == "" to use the label as the url link to navigate to
133 StaticText
.__init
__(self
, parent
, id, label
, pos
, size
,
136 if URL
.strip() == "":
142 self
.SetToolTip(wx
.ToolTip(self
._URL
))
144 # Set default properties
148 # default: True, True, True
151 # default: blue, violet, blue
158 self
.EnableRollover()
163 # default: wx.CURSOR_HAND
173 self
.OpenInSameWindow()
175 # Set control properties and refresh
176 self
.UpdateLink(True)
178 self
.Bind(wx
.EVT_MOUSE_EVENTS
, self
.OnMouseEvent
)
179 self
.Bind(wx
.EVT_MOTION
, self
.OnMouseEvent
)
182 def GotoURL(self
, URL
, ReportErrors
=True, NotSameWinIfPossible
=False):
184 Goto The Specified URL.
186 :param ReportErrors: Use True to display error dialog if an
187 error occurrs navigating to the URL.
189 :param NotSameWinIfPossible: Use True to attempt to open the
190 URL in new browser window.
194 logOff
= wx
.LogNull()
197 webbrowser
.open(URL
, new
=NotSameWinIfPossible
)
198 self
.SetVisited(True)
199 self
.UpdateLink(True)
204 self
.DisplayError("Unable To Launch Browser.", ReportErrors
)
208 def OnMouseEvent(self
, event
):
209 """ Captures mouse events for cursor, link colors and underlines. """
212 # Mouse Is Moving On The StaticText
213 # Set The Hand Cursor On The Link
214 self
.SetCursor(self
._CursorHand
)
216 if self
._EnableRollover
:
217 fontTemp
= self
.GetFont()
218 fontTemp
.SetUnderlined(self
._RolloverUnderline
)
220 fontTemp
.SetWeight(wx
.BOLD
)
224 if self
.GetFont() != fontTemp
:
225 self
.SetFont(fontTemp
)
228 if self
.GetForegroundColour() != self
._LinkRolloverColor
:
229 self
.SetForegroundColour(self
._LinkRolloverColor
)
236 # Restore The Original Cursor
237 self
.SetCursor(wx
.NullCursor
)
238 if self
._EnableRollover
:
239 self
.UpdateLink(True)
242 # Left Button Was Pressed
244 self
.GotoURL(self
._URL
, self
._ReportErrors
,
245 self
._NotSameWinIfPossible
)
248 eventOut
= HyperLinkEvent(wxEVT_HYPERLINK_LEFT
, self
.GetId())
249 eventOut
.SetEventObject(self
)
250 eventOut
.SetPosition(event
.GetPosition())
251 self
.GetEventHandler().ProcessEvent(eventOut
)
253 self
.SetVisited(True)
255 elif event
.RightUp():
256 # Right Button Was Pressed
258 # Popups A Menu With The "Copy HyperLynks" Feature
259 menuPopUp
= wx
.Menu("", wx
.MENU_TEAROFF
)
260 menuPopUp
.Append(wxHYPERLINKS_POPUP_COPY
, "Copy HyperLink")
261 self
.Bind(wx
.EVT_MENU
, self
.OnPopUpCopy
, id=wxHYPERLINKS_POPUP_COPY
)
262 self
.PopupMenu(menuPopUp
, wx
.Point(event
.m_x
, event
.m_y
))
264 self
.Unbind(wx
.EVT_MENU
, id=wxHYPERLINKS_POPUP_COPY
)
267 eventOut
= HyperLinkEvent(wxEVT_HYPERLINK_RIGHT
, self
.GetId())
268 eventOut
.SetEventObject(self
)
269 eventOut
.SetPosition(event
.GetPosition())
270 self
.GetEventHandler().ProcessEvent(eventOut
)
272 elif event
.MiddleUp():
273 # Middle Button Was Pressed
274 eventOut
= HyperLinkEvent(wxEVT_HYPERLINK_MIDDLE
, self
.GetId())
275 eventOut
.SetEventObject(self
)
276 eventOut
.SetPosition(event
.GetPosition())
277 self
.GetEventHandler().ProcessEvent(eventOut
)
282 def OnPopUpCopy(self
, event
):
283 """ Copy data from the HyperLink to the clipboard. """
285 wx
.TheClipboard
.UsePrimarySelection(False)
286 if not wx
.TheClipboard
.Open():
288 data
= wx
.TextDataObject(self
._URL
)
289 wx
.TheClipboard
.SetData(data
)
290 wx
.TheClipboard
.Close()
293 def UpdateLink(self
, OnRefresh
=True):
297 Changing text properties if:
298 - User Specific Setting
304 fontTemp
= self
.GetFont()
307 self
.SetForegroundColour(self
._VisitedColour
)
308 fontTemp
.SetUnderlined(self
._VisitedUnderline
)
312 self
.SetForegroundColour(self
._LinkColour
)
313 fontTemp
.SetUnderlined(self
._LinkUnderline
)
316 fontTemp
.SetWeight(wx
.BOLD
)
318 if self
.GetFont() != fontTemp
:
319 self
.SetFont(fontTemp
)
320 self
.Refresh(OnRefresh
)
323 def DisplayError(self
, ErrorMessage
, ReportErrors
=True):
325 Displays an error message (according to ReportErrors variable)
329 wx
.MessageBox(ErrorMessage
, "HyperLinks Error", wx
.OK | wx
.CENTRE | wx
.ICON_ERROR
)
333 link
=wx
.Colour(0, 0, 255),
334 visited
=wx
.Colour(79, 47, 79),
335 rollover
=wx
.Colour(0, 0, 255)):
336 """ Sets the colours for the link, the visited link and the mouse rollover.
340 - Visited Link: VIOLET
344 self
._LinkColour
= link
345 self
._VisitedColour
= visited
346 self
._LinkRolloverColor
= rollover
349 def GetColours(self
):
351 Gets the colours for the link, the visited link and the mouse
354 return self
._LinkColour
, self
._VisitedColour
, self
._LinkRolloverColor
357 def SetUnderlines(self
, link
=True, visited
=True, rollover
=True):
358 """ Underlines Properties. """
359 self
._LinkUnderline
= link
360 self
._RolloverUnderline
= rollover
361 self
._VisitedUnderline
= visited
364 def GetUnderlines(self
):
366 Returns if link is underlined, if the mouse rollover is
367 underlined and if the visited link is underlined.
369 return self
._LinkUnderline
, self
._RolloverUnderline
, self
._VisitedUnderline
372 def SetLinkCursor(self
, cur
=wx
.CURSOR_HAND
):
373 """ Sets link cursor properties. """
374 self
._CursorHand
= wx
.StockCursor(cur
)
377 def GetLinkCursor(self
):
378 """ Gets the link cursor. """
379 return self
._CursorHand
382 def SetVisited(self
, Visited
=False):
383 """ Sets a link as visited. """
385 self
._Visited
= Visited
388 def GetVisited(self
):
389 """ Returns whether a link has been visited or not. """
393 def SetBold(self
, Bold
=False):
394 """ Sets the HyperLink in bold text. """
399 """ Returns whether the HyperLink has text in bold or not. """
403 def SetURL(self
, URL
):
404 """ Sets the HyperLink text to the specified URL. """
409 """ Retrieve the URL associated to the HyperLink. """
413 def OpenInSameWindow(self
, NotSameWinIfPossible
=False):
414 """ Open multiple URL in the same window (if possible). """
415 self
._NotSameWinIfPossible
= NotSameWinIfPossible
418 def EnableRollover(self
, EnableRollover
=False):
419 """ Enable/disable rollover. """
420 self
._EnableRollover
= EnableRollover
423 def ReportErrors(self
, ReportErrors
=True):
424 """ Set whether to report browser errors or not. """
425 self
._ReportErrors
= ReportErrors
428 def AutoBrowse(self
, AutoBrowse
=True):
430 Automatically browse to URL when clicked. set to False to
431 receive EVT_HYPERLINK_LEFT event.
433 self
._AutoBrowse
= AutoBrowse
436 def DoPopup(self
, DoPopup
=True):
437 """ Sets whether to show popup menu on right click or not. """
438 self
._DoPopup
= DoPopup