1//
2// This file is part of the aMule Project.
3//
4// Copyright (c) 2004-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5//
6// Any parts of this program derived from the xMule, lMule or eMule project,
7// or contributed by third-party developers are copyrighted by their
8// respective authors.
9//
10// This program is free software; you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation; either version 2 of the License, or
13// (at your option) any later version.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
23//
24
25#include "MuleTextCtrl.h"
26#include <wx/menu.h>
27#include <wx/intl.h>
28#include <wx/dataobj.h>
29#include <wx/clipbrd.h>
30
31/**
32 * These are the IDs used to identify the different menu-items.
33 *
34 * Please note that I make use of predefined wxIDs for the first two, but not
35 * for Paste. This is because wxMenu poses some restrictions on what can be
36 * done with items using those IDs, and by default, Paste is enabled even if
37 * there's nothing to paste!
38 */
39enum CMTC_Events
40{
41	//! Cut text, uses provided ID
42	CMTCE_Cut	= wxID_CUT,
43	//! Copy text, uses privided ID
44	CMTCE_Copy 	= wxID_COPY,
45	//! Paste text, uses custom ID
46	CMTCE_Paste = wxID_HIGHEST + 666,	// Random satanic ID
47	//! Clear text, uses custom ID
48	CMTCE_Clear,
49	//! Select All text, uses custom ID
50	CMTCE_SelAll
51};
52
53
54BEGIN_EVENT_TABLE(CMuleTextCtrl, wxTextCtrl)
55#ifndef __WXGTK__
56	EVT_RIGHT_DOWN	(CMuleTextCtrl::OnRightDown)
57
58	EVT_MENU    	(CMTCE_Paste,	CMuleTextCtrl::OnPaste)
59	EVT_MENU    	(CMTCE_Clear,	CMuleTextCtrl::OnClear)
60	EVT_MENU    	(CMTCE_SelAll,	CMuleTextCtrl::OnSelAll)
61#endif
62END_EVENT_TABLE()
63
64
65CMuleTextCtrl::CMuleTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator, const wxString& name)
66 :  wxTextCtrl( parent, id, value, pos, size, style, validator, name)
67{
68}
69
70
71void CMuleTextCtrl::OnRightDown( wxMouseEvent& evt )
72{
73	// If this control doesn't have focus, then set it
74	if ( FindFocus() != this )
75		SetFocus();
76
77	wxMenu popup_menu;
78
79	popup_menu.Append( CMTCE_Cut, _("Cut") );
80	popup_menu.Append( CMTCE_Copy, _("Copy") );
81	popup_menu.Append( CMTCE_Paste, _("Paste") );
82	popup_menu.Append( CMTCE_Clear, _("Clear") );
83
84	popup_menu.AppendSeparator();
85
86	popup_menu.Append( CMTCE_SelAll, _("Select All") );
87
88
89	// wxMenu will automatically enable/disable the Cut and Copy items,
90	// however, were are a little more pricky about the Paste item than they
91	// are, so we enable/disable it on our own, depending on whenever or not
92	// there's actually something to paste
93	bool canpaste = false;
94	if ( CanPaste() ) {
95		if ( wxTheClipboard->Open() ) {
96			if ( wxTheClipboard->IsSupported( wxDF_TEXT ) ) {
97				wxTextDataObject data;
98	 			wxTheClipboard->GetData( data );
99
100				canpaste = (data.GetTextLength() > 0);
101			}
102			wxTheClipboard->Close();
103		}
104	}
105
106
107	popup_menu.Enable( CMTCE_Paste,		canpaste );
108	popup_menu.Enable( CMTCE_Clear,		IsEditable() && !GetValue().IsEmpty() );
109
110	PopupMenu( &popup_menu, evt.GetX(), evt.GetY() );
111}
112
113
114void CMuleTextCtrl::OnPaste( wxCommandEvent& WXUNUSED(evt) )
115{
116	Paste();
117}
118
119
120void CMuleTextCtrl::OnSelAll( wxCommandEvent& WXUNUSED(evt) )
121{
122	// Move the pointer to the front
123	SetInsertionPoint( 0 );
124
125	// Selects everything
126	SetSelection( -1, -1 );
127}
128
129
130void CMuleTextCtrl::OnClear( wxCommandEvent& WXUNUSED(evt) )
131{
132	Clear();
133}
134
135
136#ifdef __WXMAC__
137//#warning Remove this when wxMAC has been fixed.
138// https://sourceforge.net/tracker/?func=detail&atid=109863&aid=1189859&group_id=9863
139void CMuleTextCtrl::Clear()
140{
141	if (IsMultiLine()) {
142		wxFont font = GetFont();
143		wxTextCtrl::Clear();
144		SetFont(font);
145	} else {
146		wxTextCtrl::Clear();
147	}
148}
149#endif
150
151// File_checked_for_headers
152