📄 custctrl.cpp
字号:
{
#if __INTENSE_DEBUGGING__
wxLogDebug (wxT(" returns: %i"), (int)( hi + startpos ) );
#endif
*pCol = ( hi + startpos );
return retval;
}
// Start from average width based estimate.
int current_pos = x / dc.GetCharWidth();
wxCTC_HITTEST_GETEXTENT(current_pos)
//int retries = 0;
int prev_tw;
if ( tw > x )
{
// Go lower.
do
{
prev_tw = tw;
current_pos--;
wxCTC_HITTEST_GETEXTENT(current_pos)
//retries++;
} while ( tw > x && current_pos > 0 );
// Get nearest.
if ( (prev_tw-x) < (x-tw) )
current_pos++;
//wxLogDebug(wxT("Going lower, %i retries (x=%i,result=%i)"),retries,x,current_pos);
}
else if ( tw < x )
{
// Go higher.
int text_len = m_text.length();
do
{
prev_tw = tw;
current_pos++;
wxCTC_HITTEST_GETEXTENT(current_pos)
//retries++;
} while ( tw < x && current_pos < text_len );
// Get nearest.
if ( (x-prev_tw) < (tw-x) )
current_pos--;
//wxLogDebug(wxT("Going higher, %i retries (x=%i,result=%i)"),retries,x,current_pos);
}
#if __INTENSE_DEBUGGING__
//wxLogDebug (wxT(" returns: %i (%i calls)"), (int)(startpos + lo), (int)calls );
#endif
*pCol = (startpos + current_pos);
return retval;
}
// -----------------------------------------------------------------------
bool wxGenericTextCtrl::SetInsertionPoint ( long pos, long first_visible )
{
#if __INTENSE_DEBUGGING__
wxLogDebug ( wxT("wxGenericTextCtrl::SetInsertionPoint(%i)"), (int)pos );
#endif
wxRect rect = GetClientRect();
bool need_draw = FALSE;
if ( pos >= 0 )
{
m_position = pos;
int tw, th;
if ( first_visible < 0 )
first_visible = pos;
// Need to scroll it?
if ( first_visible < (int)m_scrollPosition )
{
m_scrollPosition = first_visible;
//if ( pos > 0 ) m_scrollPosition = pos - 1;
need_draw = TRUE;
}
wxString s = m_text.Mid(m_scrollPosition,pos-m_scrollPosition);
GetTextExtent (s,&tw,&th,NULL,NULL,&m_font);
//wxLogDebug(wxT("text_extent: %i"),tw);
int area_width = (rect.width-(wxCC_TEXTCTRL_XSPACING*2));
//wxWindow* parent = GetParent();
// Increment scroll position until end becomes visible.
while ( tw >= area_width )
{
m_scrollPosition += 1;
s = m_text.Mid(m_scrollPosition,pos-m_scrollPosition);
GetTextExtent (s,&tw,&th,NULL,NULL,&m_font);
need_draw = TRUE;
}
// Adjust to the space between characters.
//tw -= 1; // Sometime back this was needed. Don't know why not anymore.
if ( FindFocus() == this )
{
wxASSERT ( m_pCaret );
m_pCaret->Move(wxCC_TEXTCTRL_XSPACING + tw,
(rect.height-m_fontHeight)/2);
if ( !m_pCaret->IsVisible() )
m_pCaret->Show();
//wxLogDebug(wxT("CaretVisible: %i"),(int)(m_pCaret->IsVisible()));
}
if ( need_draw )
Refresh();
}
else
{
if ( FindFocus() == this )
{
if ( m_pCaret->IsVisible() )
m_pCaret->Show(false);
}
}
return need_draw;
}
// -----------------------------------------------------------------------
bool wxGenericTextCtrl::SetSelection ( long from, long to )
{
if ( from < 0 )
from = 0;
if ( to < 0 )
to = m_text.length();
if ( m_selStart == from && m_selEnd == to )
return false;
m_selStart = from;
m_selEnd = to;
return true;
}
// -----------------------------------------------------------------------
// like DEL key was pressed
void wxGenericTextCtrl::DeleteSelection ()
{
// remove selected portion?
if ( m_selStart != -1 )
{
wxASSERT( m_selStart < (int)m_text.length() );
wxASSERT( m_selEnd >= m_selStart );
// need to move insertion point to the beginning
//if ( m_position == (unsigned int)m_selEnd )
// SetInsertionPoint ( m_selStart, -1, rect, ctrl );
//wxLogDebug(wxT("%i,%i"),m_selStart,m_selEnd);
m_text.erase ( m_selStart, m_selEnd - m_selStart );
m_position = m_selStart;
m_selStart = m_selEnd = -1;
}
}
#endif
#if wxPG_USE_CUSTOM_CONTROLS
// -----------------------------------------------------------------------
// wxCustomControlHandler
// -----------------------------------------------------------------------
void wxCustomControlHandler::Create ( wxCustomControl* pctrl,
const wxPoint& pos,
const wxSize& sz )
{
m_control = pctrl;
SetSize ( pos, sz );
}
// -----------------------------------------------------------------------
// wxCustomTextCtrlHandler
// -----------------------------------------------------------------------
#define wxTE_HT_NO_TEXT 0
#define wxTE_HT_BEFORE 1
#define wxTE_HT_ON_TEXT 2
#define wxTE_HT_BEYOND 3
bool wxCustomTextCtrlHandler::OnKeyEvent ( wxKeyEvent& event )
{
wxCCustomTextCtrl* ctrl = (wxCCustomTextCtrl*)m_control;
wxCustomControlManager* manager = ctrl->GetManager();
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;
}
ctrl->Draw (); // need to refresh after selection change
}
else
{
// If not shift down, clear the selection
m_selStart = -1;
ctrl->Draw ();
}
ctrl->SetInsertionPoint ( pos - 1 );
}
else if ( m_selStart != -1 && !event.m_shiftDown )
{
// Just reset the selection.
m_selStart = -1;
ctrl->Draw ();
}
}
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;
}
ctrl->Draw (); // need to refresh after selection change
}
else
{
// If not shift down, clear the selection
m_selStart = -1;
ctrl->Draw ();
}
ctrl->SetInsertionPoint ( pos + 1 );
}
else if ( m_selStart != -1 && !event.m_shiftDown )
{
// Just reset the selection.
m_selStart = -1;
ctrl->Draw ();
}
}
else if ( keycode == WXK_BACK )
{
if ( m_selStart != -1 )
{
// With selection, backspace works just like delete.
DeleteSelection();
ctrl->SetInsertionPoint ( m_position );
ctrl->Draw();
manager->AddEvent (ctrl,wxEVT_COMMAND_TEXT_UPDATED);
}
else if ( m_position > 0 )
{
m_text.erase ( m_position - 1, 1 );
ctrl->SetInsertionPoint ( m_position - 1 );
ctrl->Draw();
manager->AddEvent (ctrl,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 );
ctrl->SetInsertionPoint ( m_position );
ctrl->Draw();
manager->AddEvent (ctrl,wxEVT_COMMAND_TEXT_UPDATED);
}
}
else
{
// Delete selection
DeleteSelection();
ctrl->SetInsertionPoint ( m_position );
ctrl->Draw();
manager->AddEvent (ctrl,wxEVT_COMMAND_TEXT_UPDATED);
}
}
else if ( keycode == WXK_RETURN || keycode == WXK_NUMPAD_ENTER )
{
if ( ctrl->GetWindowStyle() & wxTE_PROCESS_ENTER )
{
manager->AddEvent (ctrl,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;
}
ctrl->Draw (); // need to refresh after selection change
}
else
{
// If not shift down, clear the selection
m_selStart = -1;
ctrl->Draw ();
}
ctrl->SetInsertionPoint ( 0 );
}
else if ( m_selStart != -1 && !event.m_shiftDown )
{
// Just reset the selection.
m_selStart = -1;
ctrl->Draw ();
}
}
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();
}
ctrl->Draw (); // need to refresh after selection change
}
else if ( m_selStart != -1 )
{
// If not shift down, clear the selection
m_selStart = -1;
ctrl->Draw ();
}
ctrl->SetInsertionPoint ( m_text.length() );
}
else if ( m_selStart != -1 && !event.m_shiftDown )
{
// Just reset the selection.
m_selStart = -1;
ctrl->Draw ();
}
}
else if ( event.GetEventType() == wxEVT_CHAR &&
( (keycode >= WXK_SPACE && keycode < WXK_START )
||
keycode > WXK_WINDOWS_MENU
)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -