📄 custctrl.cpp
字号:
dc.DrawRectangle ( r2 );
dc.SetPen ( wxSystemSettings::GetColour ( wxSYS_COLOUR_BTNHIGHLIGHT ) );
dc.DrawLine ( r2.x, r2.y, r2.x, r2.y + r2.height-1 );
dc.DrawLine ( r2.x, r2.y, r2.x + r2.width - 1, r2.y );
dc.SetPen ( wxSystemSettings::GetColour ( wxSYS_COLOUR_3DLIGHT ) );
dc.DrawLine ( r1.x, r1.y, r1.x, r1.y + r1.height-1 );
dc.DrawLine ( r1.x, r1.y, r1.x + r1.width - 1, r1.y );
}
else
{
dc.SetPen ( wxSystemSettings::GetColour ( wxSYS_COLOUR_BTNSHADOW ) );
dc.DrawRectangle ( r1 );
}
if ( flags & wxCONTROL_POPUP_ARROW )
{
wxColour buttextcol = wxSystemSettings::GetColour ( wxSYS_COLOUR_BTNTEXT );
// arrow on button
dc.SetBrush ( buttextcol );
dc.SetPen ( buttextcol );
int rect_mid = r1.width / 2;
int arw_wid = r1.width/5;
int arw_top_y = (r1.height/2) - (arw_wid/2);
dropdown_arrow_points[0].x = rect_mid - arw_wid;
dropdown_arrow_points[0].y = arw_top_y;
dropdown_arrow_points[1].x = rect_mid + arw_wid; //rect.width-(rect.width/3);
dropdown_arrow_points[1].y = arw_top_y;
dropdown_arrow_points[2].x = rect_mid;
dropdown_arrow_points[2].y = arw_top_y + arw_wid;
dc.DrawPolygon ( 3, dropdown_arrow_points, r1.x, r1.y );
}
}
#endif
#endif // if wxWidgets < 2.5.5 or wxPG_USE_CUSTOM_CONTROLS
#if wxPG_USE_GENERIC_TEXTCTRL
BEGIN_EVENT_TABLE(wxGenericTextCtrl, wxGTextCtrlBase)
EVT_MOTION(wxGenericTextCtrl::OnMouseEvent)
EVT_LEFT_DOWN(wxGenericTextCtrl::OnMouseEvent)
EVT_LEFT_UP(wxGenericTextCtrl::OnMouseEvent)
//EVT_RIGHT_UP(wxPropertyGrid::OnMouseEvent)
EVT_LEFT_DCLICK(wxGenericTextCtrl::OnMouseEvent)
EVT_PAINT(wxGenericTextCtrl::OnPaint)
//EVT_SIZE(wxPropertyGrid::OnResize)
EVT_KEY_DOWN(wxGenericTextCtrl::OnKeyEvent)
EVT_CHAR(wxGenericTextCtrl::OnKeyEvent)
END_EVENT_TABLE()
IMPLEMENT_DYNAMIC_CLASS(wxGenericTextCtrl, wxGTextCtrlBase)
void wxGenericTextCtrl::Init()
{
m_scrollPosition = 0;
m_position = 0;
m_selStart = -1;
m_selEnd = -1;
m_itemButDown = -1;
m_pCaret = (wxCaret*) NULL;
m_fontHeight = 0;
m_isModified = false;
m_isEditable = true;
}
bool wxGenericTextCtrl::Create(wxWindow *parent, wxWindowID id,
const wxString& value,
const wxPoint& pos,
const wxSize& size,
long style,
const wxValidator& validator,
const wxString& name)
{
bool res = wxGTextCtrlBase::Create(parent,id,pos,size,
style|wxWANTS_CHARS,validator,name);
if ( res )
{
wxFont default_font = wxGTextCtrlBase::GetFont();
wxGTextCtrlBase::SetOwnFont ( default_font );
SetBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW) );
m_text = value;
RecalculateMetrics();
SetCursor( wxCURSOR_IBEAM );
if ( !m_pCaret )
{
m_pCaret = new wxCaret(this,1,m_fontHeight);
}
SetBackgroundStyle( wxBG_STYLE_SYSTEM );
SetInsertionPoint(0);
return true;
}
return false;
}
wxGenericTextCtrl::~wxGenericTextCtrl()
{
delete m_pCaret;
}
void wxGenericTextCtrl::RecalculateMetrics()
{
// Call this on Create and SetFont (atleast)
int x;
GetTextExtent(wxT("jG"), &x, &m_fontHeight, 0, 0, &m_font);
}
void wxGenericTextCtrl::DispatchEvent( int evtId )
{
wxCommandEvent evt(evtId,GetId());
evt.SetEventObject (this);
GetEventHandler()->AddPendingEvent(evt);
}
#define wxTE_HT_NO_TEXT 0
#define wxTE_HT_BEFORE 1
#define wxTE_HT_ON_TEXT 2
#define wxTE_HT_BEYOND 3
void wxGenericTextCtrl::OnKeyEvent ( wxKeyEvent& event )
{
int keycode = event.GetKeyCode();
if ( keycode == WXK_LEFT || keycode == WXK_UP )
{
int pos = (int)m_position;
if ( pos > 0 )
{
if ( event.m_shiftDown )
{
// shift was down - set/modify selection
if ( m_selStart != -1 )
{
// modify
if ( pos == m_selStart )
m_selStart = pos - 1;
else
{
m_selEnd = pos - 1;
if ( m_selStart == m_selEnd )
m_selStart = -1;
}
}
else
{
// set
m_selStart = pos - 1;
m_selEnd = pos;
}
Refresh (); // need to refresh after selection change
}
else
{
// If not shift down, clear the selection
m_selStart = -1;
Refresh ();
}
SetInsertionPoint ( pos - 1 );
}
else if ( m_selStart != -1 && !event.m_shiftDown )
{
// Just reset the selection.
m_selStart = -1;
Refresh ();
}
}
else if ( keycode == WXK_RIGHT || keycode == WXK_DOWN )
{
int pos = (int)m_position;
if ( pos < (int)m_text.length() )
{
if ( event.m_shiftDown )
{
// shift was down - set/modify selection
if ( m_selStart != -1 )
{
// modify
if ( pos == m_selEnd )
m_selEnd = pos + 1;
else
{
m_selStart = pos + 1;
if ( m_selStart == m_selEnd )
m_selStart = -1;
}
}
else
{
// set
m_selStart = pos;
m_selEnd = pos + 1;
}
Refresh (); // need to refresh after selection change
}
else
{
// If not shift down, clear the selection
m_selStart = -1;
Refresh ();
}
SetInsertionPoint ( pos + 1 );
}
else if ( m_selStart != -1 && !event.m_shiftDown )
{
// Just reset the selection.
m_selStart = -1;
Refresh ();
}
}
else if ( keycode == WXK_BACK )
{
if ( m_selStart != -1 )
{
// With selection, backspace works just like delete.
DeleteSelection();
SetInsertionPoint ( m_position );
Refresh();
DispatchEvent (wxEVT_COMMAND_TEXT_UPDATED);
}
else if ( m_position > 0 )
{
m_text.erase ( m_position - 1, 1 );
SetInsertionPoint ( m_position - 1 );
Refresh();
DispatchEvent (wxEVT_COMMAND_TEXT_UPDATED);
}
}
else if ( keycode == WXK_DELETE && !event.ShiftDown() )
{
if ( m_selStart == -1 )
{
// Delete character at cursor
if ( m_position < m_text.length() )
{
m_text.erase ( m_position, 1 );
SetInsertionPoint ( m_position );
Refresh();
DispatchEvent (wxEVT_COMMAND_TEXT_UPDATED);
}
}
else
{
// Delete selection
DeleteSelection();
SetInsertionPoint ( m_position );
Refresh();
DispatchEvent (wxEVT_COMMAND_TEXT_UPDATED);
}
}
else if ( keycode == WXK_RETURN || keycode == WXK_NUMPAD_ENTER )
{
if ( GetWindowStyle() & wxTE_PROCESS_ENTER )
{
DispatchEvent (wxEVT_COMMAND_TEXT_ENTER);
}
}
else if ( keycode == WXK_HOME )
{
int pos = (int)m_position;
if ( pos > 0 )
{
if ( event.m_shiftDown )
{
// shift was down - set/modify selection
if ( m_selStart != -1 )
{
// modify
if ( pos == m_selStart )
m_selStart = 0;
else
{
m_selEnd = m_selStart;
m_selStart = 0;
if ( m_selStart == m_selEnd )
m_selStart = -1;
}
}
else
{
// set
m_selStart = 0;
m_selEnd = pos;
}
Refresh (); // need to refresh after selection change
}
else
{
// If not shift down, clear the selection
m_selStart = -1;
Refresh ();
}
SetInsertionPoint ( 0 );
}
else if ( m_selStart != -1 && !event.m_shiftDown )
{
// Just reset the selection.
m_selStart = -1;
Refresh ();
}
}
else if ( keycode == WXK_END )
{
int pos = (int)m_position;
if ( pos < (int)m_text.length() )
{
if ( event.m_shiftDown )
{
// shift was down - set/modify selection
if ( m_selStart != -1 )
{
// modify
if ( pos == m_selEnd )
m_selEnd = m_text.length();
else
{
m_selStart = m_selEnd;
m_selEnd = m_text.length();
if ( m_selStart == m_selEnd )
m_selStart = -1;
}
}
else
{
// set
m_selStart = pos;
m_selEnd = m_text.length();
}
Refresh (); // need to refresh after selection change
}
else if ( m_selStart != -1 )
{
// If not shift down, clear the selection
m_selStart = -1;
Refresh ();
}
SetInsertionPoint ( m_text.length() );
}
else if ( m_selStart != -1 && !event.m_shiftDown )
{
// Just reset the selection.
m_selStart = -1;
Refresh ();
}
}
else if ( event.GetEventType() == wxEVT_CHAR &&
( (keycode >= WXK_SPACE && keycode < WXK_START )
||
keycode > WXK_WINDOWS_MENU
)
)
{
// character?
#if wxUSE_UNICODE
# if wxMINOR_VERSION < 5 || ( wxMINOR_VERSION == 5 && wxRELEASE_NUMBER < 3 )
// The old way with unicode.
//wxChar c = event.m_uniChar didn't work.
wxChar c = (wxChar) keycode;
# else
// The new way (2.5.3+).
wxChar c = event.GetUnicodeKey();
# endif
#else
wxChar c = (wxChar) keycode;
#endif
DeleteSelection();
m_text.insert ( m_position, 1, c );
SetInsertionPoint ( m_position + 1, m_position );
Refresh();
DispatchEvent (wxEVT_COMMAND_TEXT_UPDATED);
}
else
{
// Clipboard operations
int cb_op = 100; // 100 = invalid
// Cut: Ctrl-X and Shift-Del
// Copy: Ctrl-C and ???
// Paste: Ctrl-V and Shift-Ins
if ( event.ControlDown() )
{
if ( keycode == wxT('X') ) cb_op = 1;
else if ( keycode == wxT('C') ) cb_op = 2;
else if ( keycode == wxT('V') ) cb_op = 3;
}
else if ( event.ShiftDown() )
{
if ( keycode == WXK_DELETE ) cb_op = 1;
//else if ( keycode == wxT('C') ) cb_op = 2;
else if ( keycode == WXK_INSERT ) cb_op = 3;
}
// Clipboard operations
if ( cb_op < 3 )
{
// Cut and Copy
if ( m_selStart >= 0 && wxTheClipboard->Open() )
{
wxTextDataObject* obj = new wxTextDataObject(
m_text.Mid(m_selStart,m_selEnd-m_selStart)
);
//wxLogDebug ( wxT("data_object = 0x%X"), (size_t)obj );
wxTheClipboard->AddData( obj );
// Cut-only Portion
if ( cb_op == 1 )
{
DeleteSelection();
SetInsertionPoint ( m_position );
Refresh();
DispatchEvent (wxEVT_COMMAND_TEXT_UPDATED);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -