⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 custctrl.cpp

📁 非常好用的可移植的多平台C/C++源代码编辑器
💻 CPP
📖 第 1 页 / 共 5 页
字号:
                wxTheClipboard->Close();

                wxTheClipboard->Flush();

                //delete obj;
            }
        }
        else if ( cb_op == 3 )
        {
        // Paste

            if ( wxTheClipboard->Open() )
            {
                if (wxTheClipboard->IsSupported( wxDF_TEXT ))
                {
                    wxTextDataObject data;
                    wxTheClipboard->GetData( data );
                    wxString text = data.GetText();

                    DeleteSelection();

                    m_text.insert ( m_position, text );

                    SetInsertionPoint ( m_position + text.length(), m_position + text.length() - 1 );

                    Refresh();

                    DispatchEvent (wxEVT_COMMAND_TEXT_UPDATED);
                }  
                wxTheClipboard->Close();
            }
        }
        else
            event.Skip();
    }
}

void wxGenericTextCtrl::OnMouseEvent ( wxMouseEvent& event )
{
    int x = event.GetPosition().x;

    if ( event.GetEventType() == wxEVT_MOTION )
    {
        if ( event.Dragging() && m_itemButDown >= 0 )
        {
            wxSize sz = GetClientSize();

            // Fix position;
            if ( x < wxCC_TEXTCTRL_XSPACING )
                x = wxCC_TEXTCTRL_XSPACING;
            else if ( x >= (sz.x-wxCC_TEXTCTRL_XSPACING) )
                x = (sz.x-1-wxCC_TEXTCTRL_XSPACING);

            int hitpos;

            if ( HitTest(x,&hitpos) >= wxTE_HT_ON_TEXT )
            {
                int ns1, ns2;
                if ( hitpos < m_itemButDown )
                {
                    ns1 = hitpos;
                    ns2 = m_itemButDown;
                }
                else if ( hitpos > m_itemButDown )
                {
                    ns1 = m_itemButDown;
                    ns2 = hitpos;
                }
                else
                {
                    ns1 = -1;
                    ns2 = m_selEnd;
                }

                if ( m_selStart != ns1 || m_selEnd != ns2 )
                {
                    m_selStart = ns1;
                    m_selEnd = ns2;
                    Refresh();
                    SetInsertionPoint ( hitpos );
                }
            }
        }
    }
    else if ( event.GetEventType() == wxEVT_LEFT_DOWN 
#if !wxPG_TEXTCTRL_DOUBLE_CLICK_MODE
              || event.GetEventType() == wxEVT_LEFT_DCLICK
#endif
            )
    {
        // Focus now!
        SetFocus();

        //wxLogDebug ( wxT("wxGenericTextCtrl::OnMouseEvent(wxEVT_LEFT_DOWN)") );
        int hitpos;
        int res = HitTest(x,&hitpos);
        if ( res < wxTE_HT_ON_TEXT )
            hitpos = m_text.length();

        //if ( res >= wxTE_HT_ON_TEXT )
        {

            //wxLogDebug(wxT("hitpos:%i"),hitpos);

            SetInsertionPoint ( hitpos );

            m_itemButDown = hitpos;

            // Clear selection?
            if ( m_selStart != -1 )
            {
                m_selStart = -1;
                Refresh ();
            }
        }

    }
    else if ( event.GetEventType() == wxEVT_LEFT_UP )
    {
        m_itemButDown = -1;
    }
#if wxPG_TEXTCTRL_DOUBLE_CLICK_MODE
    else if ( event.GetEventType() == wxEVT_LEFT_DCLICK )
    {
        // Select double-clicked word.
        SetFocus();

        int hitpos;

        if ( HitTest(x,&hitpos) >= wxTE_HT_ON_TEXT )
        {
            // hitpos = first character that is not counted.

            int textlen = m_text.length();

            // Find start pos
            int startpos = hitpos-1;
            if ( startpos >= 0 )
            {
                const wxChar* p = &m_text.c_str()[startpos];
                while ( startpos >= 0 && *p > wxT(' ') )
                {
                    p--;
                    startpos--;
                }
                startpos++;
            }

            // Find end pos
            int endpos = hitpos-1;
            if ( endpos >= 0 )
            {
                const wxChar* p = &m_text.c_str()[endpos];
                while ( endpos < textlen && *p > wxT(' ') )
                {
                    p++;
                    endpos++;
                }
            #if wxPG_TEXTCTRL_DOUBLE_CLICK_MODE == 2
                while ( endpos < textlen && *p <= wxT(' ') )
                {
                    p++;
                    endpos++;
                }
            #endif
            }

            if ( endpos > startpos )
            {
                m_selStart = startpos;
                m_selEnd = endpos;
            }
            else
            {
                m_selStart = -1;
                endpos = hitpos;
            }

            Refresh();
            SetInsertionPoint ( endpos );
        }
    }
#endif
}

// -----------------------------------------------------------------------
// Pure virtuals
// -----------------------------------------------------------------------

wxString wxGenericTextCtrl::GetValue() const
{
    return m_text;
}

void wxGenericTextCtrl::SetValue ( const wxString& value )
{
    m_text = value;
    m_position = m_scrollPosition = 0;
    m_selStart = m_selEnd = -1;
    SetInsertionPoint(m_position);
    Refresh();
}

wxString wxGenericTextCtrl::GetRange(long from, long to) const
{
    return m_text.SubString(from,to-from);
}

int wxGenericTextCtrl::GetLineLength(long lineNo) const
{
    if ( lineNo == 0 )
        return m_text.length();
    return 0;
}

wxString wxGenericTextCtrl::GetLineText(long lineNo) const
{
    if ( lineNo == 0 )
        return m_text;
    return wxEmptyString;
}

int wxGenericTextCtrl::GetNumberOfLines() const
{
    return 1;
}

bool wxGenericTextCtrl::IsModified() const
{
    return m_isModified;
}

bool wxGenericTextCtrl::IsEditable() const
{
    return m_isEditable;
}

void wxGenericTextCtrl::GetSelection(long* from, long* to) const
{
    if (from)
        *from = m_selStart;
    if (to)
        *to = m_selEnd;
}

void wxGenericTextCtrl::Remove(long from, long to)
{
    m_text.Remove(from,to-from);
    Refresh();
}

void wxGenericTextCtrl::SetFocus()
{
    //wxLogDebug(wxT("wxGenericTextCtrl::SetFocus"));
    wxGTextCtrlBase::SetFocus();
    SetInsertionPoint( m_position, -1 );
}

// -----------------------------------------------------------------------
// Drawing
// -----------------------------------------------------------------------

void wxGenericTextCtrl::OnPaint ( wxPaintEvent& /*event*/ )
{
    wxPaintDC dc(this);

#if __INTENSE_DEBUGGING__
    wxLogDebug ( wxT("wxGenericTextCtrl::Draw()") );
#endif

    wxString* str = &m_text;
    wxString tempstr;

    wxRect rect(wxPoint(0,0),GetClientSize());

    wxRect write_rect(rect.x+wxCC_TEXTCTRL_XSPACING,
                      rect.y,
                      rect.width-(wxCC_TEXTCTRL_XSPACING*2),
                      rect.height);

    //wxLogDebug ( wxT("CC: DRAW ( x = %i, y = %i, text = %s )"), (int)rect.x, (int)rect.y,
    //    str->c_str() );

    HideCaretBalanced();

//    wxColour colBg = GetBackgroundColour();
    wxColour colTx = GetForegroundColour();

/*    dc.SetBrush ( colBg );
    dc.SetPen ( colBg );
    dc.DrawRectangle ( rect );*/

    // get font metrics
    dc.SetFont ( m_font );
    dc.SetTextForeground ( colTx );

    //int fw = dc.GetCharWidth() + 1;
    int fh = dc.GetCharHeight();

    if ( fh < rect.height )
    {
        write_rect.y += (rect.height - fh) / 2;
        write_rect.height = fh;
    }

    // determine number of characters to write.
    unsigned int scrollpos = m_scrollPosition;

    int hitpos;
    if ( HitTest(rect.width-wxCC_TEXTCTRL_XSPACING/*-3+2*/,&hitpos) < wxTE_HT_ON_TEXT )
    {
        ShowCaretBalanced();
        return;
    }
    /*
    int res = HitTest ( wxPoint(rect.width-wxCC_TEXTCTRL_XSPACING-3+2,wxCC_TEXTCTRL_YSPACING+1) );
    if ( res < 1 )
    {
        manager->ShowCaretBalanced();
        return;
    }
    */

    unsigned int write_end = (unsigned int)hitpos;

#ifdef __WXDEBUG__
    if ( write_end < scrollpos)
    {
        wxLogDebug ( wxT("ERROR: write_end (%u) should be more than scrollpos (%u) "),
            write_end, scrollpos );
        wxASSERT ( write_end >= scrollpos );
        ShowCaretBalanced();
        return;
    }
#endif

    unsigned int charcount = write_end - scrollpos;

    //wxLogDebug ( wxT("    (%i, %i, %i, %i)"), rect.x, rect.y, rect.width, rect.height );

    if ( m_selStart < 0 ||
         FindFocus() != this ||
         (unsigned int)m_selStart >= write_end ||
         (unsigned int)m_selEnd <= scrollpos )
    {

        // No selection to be drawn
        tempstr = str->Mid(scrollpos,charcount);
        dc.DrawText ( tempstr, write_rect.x, write_rect.y );
    }
    else
    {
        // Selection has to be drawn

        int w1 = 0, w2 = 0;
        unsigned int selstart = (unsigned int) m_selStart;
        if ( selstart < scrollpos ) selstart = scrollpos;
        unsigned int selend = (unsigned int) m_selEnd;
        if ( selend > write_end ) selend = write_end;

        // unselected portion 1

        if ( selstart > scrollpos )
        {
            tempstr = str->Mid(scrollpos, selstart-scrollpos);
            GetTextExtent (tempstr,&w1,&fh,NULL,NULL,&m_font);
            dc.DrawText ( tempstr, write_rect.x, write_rect.y );
        }

        // selected portion

        if ( selend > scrollpos )
        {
            tempstr = str->Mid(selstart,selend-selstart);
            GetTextExtent (tempstr,&w2,&fh,NULL,NULL,&m_font);

            wxColour colSelBg = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
            wxColour colSelTx = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
            dc.SetBrush ( colSelBg );
            dc.SetPen ( colSelBg );
            dc.DrawRectangle ( write_rect.x+w1, write_rect.y, w2, write_rect.height );
            dc.SetTextForeground ( colSelTx );

            dc.DrawText ( tempstr, write_rect.x+w1, write_rect.y );
        }

        // unselected portion 2

        if ( selend < write_end )
        {
            dc.SetTextForeground ( colTx );
            tempstr = str->Mid(selend, write_end-selend);
            dc.DrawText ( tempstr, write_rect.x+w1+w2, write_rect.y );
        }
    }

#if __INTENSE_DEBUGGING__
    wxLogDebug ( wxT("  ends...") );
#endif

    ShowCaretBalanced();
}

// -----------------------------------------------------------------------

// should be faster than creating new string for a sub-string (as with Mid)
inline void wxstring_copy ( wxString& dst, wxString& src, unsigned int pos, unsigned int len )
{
    wxChar* ptr = (wxChar*)&src.GetWritableChar(pos);
    wxChar cb = ptr[len];
    ptr[len] = 0; // terminate
    dst = ptr;
    ptr[len] = cb; // revert to original
}

// -----------------------------------------------------------------------

#define wxCTC_HITTEST_GETEXTENT(INDEX) \
    wxstring_copy ( tempstr, m_text, m_scrollPosition, INDEX ); \
    dc.GetTextExtent ( tempstr, &tw, NULL );

//parent->GetTextExtent ( tempstr, &tw, &th, NULL, NULL, &m_control->GetFont() );

// -----------------------------------------------------------------------

// Returns index of character at position (-1 if none). Result is absolute,
// not relative to the scroll position.

int wxGenericTextCtrl::HitTest ( wxCoord x, int* pCol )
{
#if __INTENSE_DEBUGGING__
    wxLogDebug ( wxT("wxGenericTextCtrl::HitTest(%i,%i)"), (int)pos.x, (int)pos.y );
#endif

    wxASSERT ( pCol );

    int retval = wxTE_HT_ON_TEXT;

    if ( m_text.length() < 1 )
        return wxTE_HT_NO_TEXT;

    wxClientDC dc(this);
    dc.SetFont(GetFont());

    size_t startpos = m_scrollPosition;
    size_t hi = m_text.length() - startpos;

    if ( x <= wxCC_TEXTCTRL_XSPACING )
    {
        if ( x < 0 )
            return wxTE_HT_BEFORE;
        *pCol = m_scrollPosition;
        return wxTE_HT_ON_TEXT;
    }

    x -= wxCC_TEXTCTRL_XSPACING;

    int tw;

    wxString tempstr;

    wxCTC_HITTEST_GETEXTENT(hi)

    if ( x >= tw )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -