Click here to Skip to main content
15,882,163 members
Articles / Artificial Intelligence

Introduction to wxWidgets

Rate me:
Please Sign up or sign in to vote.
4.55/5 (120 votes)
25 Dec 2005GPL321 min read 1.6M   7.2K   330  
A beginner's tutorial on wxWidgets for cross platform GUI development.
/* This is a wxHyperlinkCtrl for static text boxes in wxWindows..
 * 
 * Copyright (C) 2001-2005 Angelo Mandato
 *
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA
 */

/*//////////////// hyperlinkctrl.h ///////////////////
	Last modified:	06/17/2005
	Author:			Angelo Mandato
	Version:		1.3
	Web:			http://www.spaceblue.com/code/
	Email:			angelo@spaceblue.com

	Note:			View hyperlinkctrl.html for more information

	/// Contributors ///
	Name:			Mark McManus
	Email:			mmcmanus@scientificmetrics.com
	Notes:			Contributed code for the EVT_COMMAND_LINK_CLICKED event
					which is fired when AutoBrowse(false) is set and added
					DoPopup(false) to supress pop up menu to copy web link.
					Thanks Mark!
	
*/////////////////////////////////////////////////

#ifndef HYPERLINKCTRL_H
#define HYPERLINKCTRL_H

// Define below if you simply want to use wxCommandEvents for single click
// click events on your links
//#define HYPERLINKCTRL_COMMANDEVENT

#include <wx/wx.h>

BEGIN_DECLARE_EVENT_TYPES()
  DECLARE_EVENT_TYPE(wxEVT_COMMAND_LINK_CLICKED, 7771)
#ifndef HYPERLINKCTRL_COMMANDEVENT
  DECLARE_EVENT_TYPE(wxEVT_COMMAND_LINK_MCLICKED, 7772)
  DECLARE_EVENT_TYPE(wxEVT_COMMAND_LINK_RCLICKED, 7773)
#endif
END_DECLARE_EVENT_TYPES()


#ifdef HYPERLINKCTRL_COMMANDEVENT

#define EVT_LINK(id, fn) \
    DECLARE_EVENT_TABLE_ENTRY( \
        wxEVT_COMMAND_LINK_CLICKED, id, -1, \
        (wxObjectEventFunction)(wxEventFunction)(wxCommandEventFunction)&fn, \
        (wxObject *) NULL \
    ),

#else

class wxHyperlinkEvent : public wxCommandEvent
{
public:
  wxHyperlinkEvent( WXTYPE eventType = wxEVT_COMMAND_LINK_RCLICKED, int id = 0 ) : wxCommandEvent( eventType, id ) { };

	virtual wxEvent *Clone() const { return new wxHyperlinkEvent(*this);  };

	void SetPosition( const wxPoint &pos ) { m_pos = pos; };
	wxPoint GetPosition(void) { return m_pos; };
private:
	wxPoint	m_pos;
};

typedef void (wxEvtHandler::*wxHyperlinkEventFunction)(wxHyperlinkEvent);

// Event types for wxHyperlinkCtrl:
#define EVT_LINK(id, fn) \
    DECLARE_EVENT_TABLE_ENTRY( \
        wxEVT_COMMAND_LINK_CLICKED, id, -1, \
        (wxObjectEventFunction)(wxEventFunction)(wxHyperlinkEventFunction)&fn, \
        (wxObject *) NULL \
    ),
#define EVT_LINK_MCLICKED(id, fn) \
    DECLARE_EVENT_TABLE_ENTRY( \
        wxEVT_COMMAND_LINK_MCLICKED, id, -1, \
        (wxObjectEventFunction)(wxEventFunction)(wxHyperlinkEventFunction)&fn, \
        (wxObject *) NULL \
    ),
#define EVT_LINK_RCLICKED(id, fn) \
    DECLARE_EVENT_TABLE_ENTRY( \
        wxEVT_COMMAND_LINK_RCLICKED, id, -1, \
        (wxObjectEventFunction)(wxEventFunction)(wxHyperlinkEventFunction)&fn, \
        (wxObject *) NULL \
    ),

#endif

enum
{
	HYPERLINKS_POPUP_COPY = 1000,
};

// wxHyperlinkCtrl class
class wxHyperlinkCtrl : public wxStaticText
{
public:

#ifdef __WIN95__
	long MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
#endif

	// Constructor
	wxHyperlinkCtrl( wxWindow *parent, wxWindowID id, const wxString &label,
			const wxPoint &pos, const wxSize &size, int style = 0, const wxString& name = "staticText", const wxString& szURL = "" );

	// Link Colours properties
	void SetColours( const wxColour &link = wxColour( "BLUE" ), const wxColour &visited  = wxColour("VIOLET"), const wxColour &rollover = wxColour( "BLUE" ))
	{
		m_crLinkColour = link;
		m_crVisitedColour = visited;
		m_crLinkRolloverColor = rollover;
	};
	void GetColous( wxColour &link, wxColour &visited, wxColour &rollover )
	{
		link = m_crLinkColour;
		visited = m_crVisitedColour;
		rollover = m_crLinkRolloverColor;
	};

	// Underline properties
	void SetUnderlines( const bool &link = true, const bool &visited = true, const bool &rollover = true )
	{
		m_bLinkUnderline = link;
		m_bRolloverUnderline = rollover;
		m_bVisitedUnderline = visited;
	};
	void GetUnderlines( bool &link, bool &visited, bool &rollover )
	{
		link = m_bLinkUnderline;
		rollover = m_bRolloverUnderline;
		visited = m_bVisitedUnderline;
	};

	// Set Link Cursor properties
	void SetLinkCursor( const wxCursor &cur = wxCURSOR_HAND) { m_crHand = cur; };
	void GetLinkCursor( wxCursor &cur ) { cur = m_crHand; };

	// Visited properties
	void SetVisited( const bool &bVisited = false ) { m_bVisited = bVisited; };
	bool GetVisited( void ) { return m_bVisited; }

	// Bold font properties
	void SetBold( const bool &bBold = false) { m_bBold = bBold; };
	bool GetBold( void ) { return m_bBold; };

	// URL properties
	void SetURL( const wxString &szURL ) { m_szURL = szURL; }
	wxString GetURL( void ) { return m_szURL; }

  // Open in same window property
  void OpenInSameWindow( const bool &bIfPossible = false ) { m_bSameWinIfPossible = bIfPossible; };

	// Broswer Path
	void SetBrowserPath( const wxString &browser ) { m_szBrowserPath = browser; };
	wxString GetBrowserPath( void ) { return m_szBrowserPath; };

  // Roll over properties
	void EnableRollover( const bool &bEnable = false ) { m_bEnableRollover = bEnable; };

	// Report errors property
	void ReportErrors( const bool &bReport = true ) { m_bReportErrors = bReport; };

//**Added By Mark McManus
  //AutoBrowse property
  void AutoBrowse( const bool &bBrowse = true ){ m_bAutoBrowse = bBrowse; };

  //DoPopup property
  void DoPopup( const bool &bPopup = true ) { m_bDoPopup = bPopup; };

//**Mark McManus

	// Goto URL
	static bool GotoURL( const wxString &szUrl, const wxString &szBrowser = "", const bool &bReportErrors = false, const bool &bSameWinIfPossible = false );

	// Refresh link properties
	void UpdateLink( const bool &bRefresh = true );

private:
	// Copy URL event
	void OnPopUpCopy( wxCommandEvent &event );

	// Mouse event
	void OnMouseEvent( wxMouseEvent& event );

	// ErrorMmessage
	static void DisplayError( const wxString &szError, const bool &bReportErrors );
	
	// The URL to goto.
	wxString m_szURL;

	// Browser Path
	wxString m_szBrowserPath;

	// Colours
	wxColour m_crLinkColour;
	wxColour m_crVisitedColour;
	wxColour m_crLinkRolloverColor;

	// Underlines
	bool m_bLinkUnderline;
	bool m_bRolloverUnderline;
	bool m_bVisitedUnderline;

	// Rollover enabled?
	bool m_bEnableRollover;

	// Visited link?
	bool m_bVisited;

	// Make link bold?
	bool m_bBold;

	// Specify a cursor
	wxCursor m_crHand;

	// Should Control report errors in dialog?
	bool m_bReportErrors;

//**Added By Mark McManus

  //Should Control browse to the URL (if false just fire wxEVT_COMMAND_LINK_CLICKED)
  bool m_bAutoBrowse;

  //Should the control show the Copy popup menu (if false just fire wxEVT_COMMAND_RLINK_CLICKED)
  bool m_bDoPopup;
//**Mark McManus

  bool m_bSameWinIfPossible;

  wxMenu *m_menuPopUp;

  DECLARE_EVENT_TABLE()
};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions