]> git.saurik.com Git - wxWidgets.git/blobdiff - src/x11/textctrl.cpp
Updates to match recent CVS changes.
[wxWidgets.git] / src / x11 / textctrl.cpp
index c79e3f74a700136c883aa5e87083f9046a766114..95cd5e86af26ee17da7bc842dca93329b840c59d 100644 (file)
@@ -135,6 +135,8 @@ BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
     EVT_CHAR(wxTextCtrl::OnChar)
     EVT_MOUSE_EVENTS(wxTextCtrl::OnMouse)
     EVT_IDLE(wxTextCtrl::OnIdle)
+    EVT_KILL_FOCUS(wxTextCtrl::OnKillFocus)
+    EVT_SET_FOCUS(wxTextCtrl::OnSetFocus)
     
     EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
     EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
@@ -214,13 +216,15 @@ bool wxTextCtrl::Create( wxWindow *parent,
     if ((style & wxTE_MULTILINE) != 0)
         style |= wxALWAYS_SHOW_SB;
         
-    wxTextCtrlBase::Create( parent, id, wxDefaultPosition, size,
+    wxTextCtrlBase::Create( parent, id, pos /* wxDefaultPosition */, size,
                               style|wxVSCROLL|wxHSCROLL|wxNO_FULL_REPAINT_ON_RESIZE );
                               
     SetBackgroundColour( *wxWHITE );
     
     SetCursor( wxCursor( wxCURSOR_IBEAM ) );
     
+    m_editable = ((m_windowStyle & wxTE_READONLY) == 0);
+    
     if (HasFlag(wxTE_PASSWORD))
         m_sourceFont = wxFont( 12, wxMODERN, wxNORMAL, wxNORMAL );
     else
@@ -258,7 +262,7 @@ wxString wxTextCtrl::GetValue() const
     for (size_t i = 0; i < m_lines.GetCount(); i++)
     {
         ret += m_lines[i].m_text;
-        if (i < m_lines.GetCount())
+        if (i+1 < m_lines.GetCount())
             ret += wxT('\n');
     }
     
@@ -268,6 +272,13 @@ wxString wxTextCtrl::GetValue() const
 void wxTextCtrl::SetValue(const wxString& value)
 {
     m_modified = TRUE;
+
+    if ((GetWindowStyle() & wxTE_MULTILINE) == 0)
+    {
+        if (value == GetValue())
+            return;
+    }
+    
     m_cursorX = 0;
     m_cursorY = 0;
     ClearSelection();
@@ -983,7 +994,20 @@ bool wxTextCtrl::Enable( bool enable )
 
 bool wxTextCtrl::SetFont(const wxFont& font)
 {
-    return FALSE;
+    wxTextCtrlBase::SetFont( font );
+    
+    m_sourceFont = font;
+    
+    wxClientDC dc(this);
+    dc.SetFont( m_sourceFont );
+    m_lineHeight = dc.GetCharHeight();
+    m_charWidth = dc.GetCharWidth();
+    
+    // TODO: recalc longest lines
+    
+    MyAdjustScrollbars();
+    
+    return TRUE;
 }
 
 bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
@@ -1816,13 +1840,18 @@ void wxTextCtrl::OnPaint( wxPaintEvent &event )
     int scroll_y = 0;
     GetViewStart( NULL, &scroll_y );
     
+    // We have a inner border of two pixels
+    // around the text, so scroll units do
+    // not correspond to lines.
+    if (scroll_y > 0) scroll_y--;
+    
     int size_x = 0;
     int size_y = 0;
     GetClientSize( &size_x, &size_y );
     
     dc.SetPen( *wxTRANSPARENT_PEN );
     dc.SetBrush( wxBrush( wxTHEME_COLOUR(HIGHLIGHT), wxSOLID ) );
-    int upper = wxMin( (int)m_lines.GetCount(), scroll_y+(size_y/m_lineHeight)+1 );
+    int upper = wxMin( (int)m_lines.GetCount(), scroll_y+(size_y/m_lineHeight)+2 );
     for (int i = scroll_y; i < upper; i++)
     {
         int x = 0+2;
@@ -1834,10 +1863,14 @@ void wxTextCtrl::OnPaint( wxPaintEvent &event )
             DrawLine( dc, 0+2, i*m_lineHeight+2, m_lines[i].m_text, i );
     }
     
-    dc.SetBrush( *wxRED_BRUSH );
-    // int xx = m_cursorX*m_charWidth;
-    int xx = PosToPixel( m_cursorY, m_cursorX );
-    dc.DrawRectangle( xx+2, m_cursorY*m_lineHeight+2, 2, m_lineHeight );
+    if (m_editable && (FindFocus() == this))
+    {
+        ///dc.SetBrush( *wxRED_BRUSH );
+        dc.SetBrush( *wxBLACK_BRUSH );
+        // int xx = m_cursorX*m_charWidth;
+        int xx = PosToPixel( m_cursorY, m_cursorX );
+        dc.DrawRectangle( xx+2, m_cursorY*m_lineHeight+2, 2, m_lineHeight );
+    }
 }
 
 void wxTextCtrl::OnMouse( wxMouseEvent &event )
@@ -1891,6 +1924,8 @@ void wxTextCtrl::OnChar( wxKeyEvent &event )
 {
     if (m_lines.GetCount() == 0) return;
 
+    if (!m_editable) return;
+    
     int size_x = 0;
     int size_y = 0;
     GetClientSize( &size_x, &size_y );
@@ -1997,6 +2032,14 @@ void wxTextCtrl::OnChar( wxKeyEvent &event )
         }
         case WXK_RETURN:
         {
+            if (m_windowStyle & wxPROCESS_ENTER)
+            {
+                wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
+                event.SetEventObject(this);
+                event.SetString(GetValue());
+                if (GetEventHandler()->ProcessEvent(event)) return;
+            }
+            
             if (IsSingleLine())
             {
                 event.Skip();
@@ -2175,7 +2218,9 @@ void wxTextCtrl::RefreshDown( int n )
 
 void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre )
 {
-    // if (IsSingleLine())
+    if (!m_editable) return;
+
+    // if (IsSingleLine() || (m_lang == wxSOURCE_LANG_NONE))
     {
         if (new_x > m_lines[new_y].m_text.Len())
             new_x = m_lines[new_y].m_text.Len();
@@ -2189,6 +2234,7 @@ void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre )
     if (shift)
     {
         int x,y,w,h;
+        bool erase_background = TRUE;
         
         if (!has_selection)
         {
@@ -2253,11 +2299,17 @@ void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre )
                 {
                     y = m_selEndY*m_lineHeight;
                     h = (new_y-m_selEndY+1) * m_lineHeight;
+                    
+                    erase_background = ((m_selEndY < m_selStartY) ||
+                                        ((m_selEndY == m_selStartY) && (m_selEndX < m_selStartX)));
                 }
                 else
                 {
                     y = new_y*m_lineHeight;
                     h = (-new_y+m_selEndY+1) * m_lineHeight;
+                    
+                    erase_background = ((m_selEndY > m_selStartY) ||
+                                        ((m_selEndY == m_selStartY) && (m_selEndX > m_selStartX)));
                 }
                 no_cursor_refresh = TRUE;
                 m_cursorX = new_x;
@@ -2270,7 +2322,7 @@ void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre )
         
         CalcScrolledPosition( x, y, &x, &y );
         wxRect rect( x+2, y+2, w, h );
-        Refresh( TRUE, &rect );
+        Refresh( erase_background, &rect );
     }
     else
     {
@@ -2318,14 +2370,18 @@ void wxTextCtrl::MoveCursor( int new_x, int new_y, bool shift, bool centre )
         m_cursorY = new_y;
     
         Refresh( TRUE, &rect );
-        
-        wxClientDC dc(this);
-        PrepareDC( dc );
-        dc.SetPen( *wxTRANSPARENT_PEN );
-        dc.SetBrush( *wxRED_BRUSH );
-        // int xx = m_cursorX*m_charWidth;
-        int xx = PosToPixel( m_cursorY, m_cursorX );
-        dc.DrawRectangle( xx+2, m_cursorY*m_lineHeight+2, 2, m_lineHeight );
+
+        if (FindFocus() == this)
+        {
+            wxClientDC dc(this);
+            PrepareDC( dc );
+            dc.SetPen( *wxTRANSPARENT_PEN );
+            //dc.SetBrush( *wxRED_BRUSH );
+            dc.SetBrush( *wxBLACK_BRUSH );
+            // int xx = m_cursorX*m_charWidth;
+            int xx = PosToPixel( m_cursorY, m_cursorX );
+            dc.DrawRectangle( xx+2, m_cursorY*m_lineHeight+2, 2, m_lineHeight );
+        }
     }
     
     int size_x = 0;
@@ -2467,6 +2523,18 @@ void wxTextCtrl::Thaw()
 {
 }
 
+void wxTextCtrl::OnSetFocus( wxFocusEvent& event )
+{
+    // To hide or show caret, as appropriate
+    Refresh();
+}
+
+void wxTextCtrl::OnKillFocus( wxFocusEvent& event )
+{
+    // To hide or show caret, as appropriate
+    Refresh();
+}
+
 // ----------------------------------------------------------------------------
 // text control scrolling
 // ----------------------------------------------------------------------------