]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/gridctrl.cpp
move Ellipsize() to wxControl so it can be easily used by other controls
[wxWidgets.git] / src / generic / gridctrl.cpp
index 1c69499c2358498969271e7fb7380fa3b9190f86..052dfbd49f2e26a3c3184027feb25710d0b5aefe 100644 (file)
 
 #if wxUSE_GRID
 
+#include "wx/generic/gridctrl.h"
+
 #ifndef WX_PRECOMP
     #include "wx/textctrl.h"
     #include "wx/dc.h"
+    #include "wx/combobox.h"
 #endif // WX_PRECOMP
 
-#include "wx/generic/gridctrl.h"
 #include "wx/tokenzr.h"
 
 // ----------------------------------------------------------------------------
@@ -31,9 +33,9 @@
 
 #if wxUSE_DATETIME
 
-// Enables a grid cell to display a formated date and or time
+// Enables a grid cell to display a formatted date and or time
 
-wxGridCellDateTimeRenderer::wxGridCellDateTimeRenderer(wxString outformat, wxString informat)
+wxGridCellDateTimeRenderer::wxGridCellDateTimeRenderer(const wxString& outformat, const wxString& informat)
 {
     m_iformat = informat;
     m_oformat = outformat;
@@ -52,7 +54,7 @@ wxGridCellRenderer *wxGridCellDateTimeRenderer::Clone() const
     return renderer;
 }
 
-wxString wxGridCellDateTimeRenderer::GetString(wxGrid& grid, int row, int col)
+wxString wxGridCellDateTimeRenderer::GetString(const wxGrid& grid, int row, int col)
 {
     wxGridTableBase *table = grid.GetTable();
 
@@ -63,7 +65,8 @@ wxString wxGridCellDateTimeRenderer::GetString(wxGrid& grid, int row, int col)
     {
         void * tempval = table->GetValueAsCustom(row, col,wxGRID_VALUE_DATETIME);
 
-        if (tempval){
+        if (tempval)
+        {
             val = *((wxDateTime *)tempval);
             hasDatetime = true;
             delete (wxDateTime *)tempval;
@@ -74,13 +77,14 @@ wxString wxGridCellDateTimeRenderer::GetString(wxGrid& grid, int row, int col)
     if (!hasDatetime )
     {
         text = table->GetValue(row, col);
-        hasDatetime = (val.ParseFormat( text, m_iformat, m_dateDef ) != (wxChar *)NULL) ;
+        const char * const end = val.ParseFormat(text, m_iformat, m_dateDef);
+        hasDatetime = end && !*end;
     }
 
     if ( hasDatetime )
         text = val.Format(m_oformat, m_tz );
 
-    //If we faild to parse string just show what we where given?
+    // If we failed to parse string just show what we where given?
     return text;
 }
 
@@ -142,7 +146,7 @@ wxGridCellRenderer *wxGridCellEnumRenderer::Clone() const
     return renderer;
 }
 
-wxString wxGridCellEnumRenderer::GetString(wxGrid& grid, int row, int col)
+wxString wxGridCellEnumRenderer::GetString(const wxGrid& grid, int row, int col)
 {
     wxGridTableBase *table = grid.GetTable();
     wxString text;
@@ -221,7 +225,7 @@ void wxGridCellEnumRenderer::SetParameters(const wxString& params)
 wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString& choices)
                      :wxGridCellChoiceEditor()
 {
-    m_startint = -1;
+    m_index = -1;
 
     if (!choices.empty())
         SetParameters(choices);
@@ -230,7 +234,7 @@ wxGridCellEnumEditor::wxGridCellEnumEditor(const wxString& choices)
 wxGridCellEditor *wxGridCellEnumEditor::Clone() const
 {
     wxGridCellEnumEditor *editor = new wxGridCellEnumEditor();
-    editor->m_startint = m_startint;
+    editor->m_index = m_index;
     return editor;
 }
 
@@ -243,40 +247,49 @@ void wxGridCellEnumEditor::BeginEdit(int row, int col, wxGrid* grid)
 
     if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) )
     {
-        m_startint = table->GetValueAsLong(row, col);
+        m_index = table->GetValueAsLong(row, col);
     }
     else
     {
         wxString startValue = table->GetValue(row, col);
         if (startValue.IsNumber() && !startValue.empty())
         {
-            startValue.ToLong(&m_startint);
+            startValue.ToLong(&m_index);
         }
         else
         {
-            m_startint=-1;
+            m_index = -1;
         }
     }
 
-    Combo()->SetSelection(m_startint);
+    Combo()->SetSelection(m_index);
     Combo()->SetInsertionPointEnd();
     Combo()->SetFocus();
 
 }
 
-bool wxGridCellEnumEditor::EndEdit(int row, int col, wxGrid* grid)
+bool wxGridCellEnumEditor::EndEdit(const wxString& WXUNUSED(oldval),
+                                   wxString *newval)
 {
-    int pos = Combo()->GetSelection();
-    bool changed = (pos != m_startint);
-    if (changed)
-    {
-        if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER))
-            grid->GetTable()->SetValueAsLong(row, col, pos);
-        else
-            grid->GetTable()->SetValue(row, col,wxString::Format(wxT("%i"),pos));
-    }
+    long idx = Combo()->GetSelection();
+    if ( idx == m_index )
+        return false;
 
-    return changed;
+    m_index = idx;
+
+    if ( newval )
+        newval->Printf("%ld", m_index);
+
+    return true;
+}
+
+void wxGridCellEnumEditor::ApplyEdit(int row, int col, wxGrid* grid)
+{
+    wxGridTableBase * const table = grid->GetTable();
+    if ( table->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER) )
+        table->SetValueAsLong(row, col, m_index);
+    else
+        table->SetValue(row, col, wxString::Format("%ld", m_index));
 }
 
 #endif // wxUSE_COMBOBOX
@@ -290,12 +303,8 @@ wxGridCellAutoWrapStringEditor::Create(wxWindow* parent,
                                        wxWindowID id,
                                        wxEvtHandler* evtHandler)
 {
-  m_control = new wxTextCtrl(parent, id, wxEmptyString,
-                             wxDefaultPosition, wxDefaultSize,
-                             wxTE_MULTILINE | wxTE_RICH);
-
-
-  wxGridCellEditor::Create(parent, id, evtHandler);
+  wxGridCellTextEditor::DoCreate(parent, id, evtHandler,
+                                 wxTE_MULTILINE | wxTE_RICH);
 }
 
 void
@@ -326,7 +335,7 @@ wxGridCellAutoWrapStringRenderer::Draw(wxGrid& grid,
 wxArrayString
 wxGridCellAutoWrapStringRenderer::GetTextLines(wxGrid& grid,
                                                wxDC& dc,
-                                               wxGridCellAttr& attr,
+                                               const wxGridCellAttr& attr,
                                                const wxRect& rect,
                                                int row, int col)
 {
@@ -378,7 +387,10 @@ wxGridCellAutoWrapStringRenderer::GetBestSize(wxGrid& grid,
                                               wxDC& dc,
                                               int row, int col)
 {
-    wxCoord x,y, height , width = grid.GetColSize(col) -10;
+    wxCoord x,y, height , width = grid.GetColSize(col) -20;
+    // for width, subtract 20 because ColSize includes a magin of 10 pixels
+    // that we do not want here and because we always start with an increment
+    // by 10 in the loop below.
     int count = 250; //Limit iterations..
 
     wxRect rect(0,0,width,10);
@@ -390,7 +402,7 @@ wxGridCellAutoWrapStringRenderer::GetBestSize(wxGrid& grid,
     {
         width+=10;
         rect.SetWidth(width);
-        height = y *( GetTextLines(grid,dc,attr,rect,row,col).GetCount());
+        height = y * (wx_truncate_cast(wxCoord, GetTextLines(grid,dc,attr,rect,row,col).GetCount()));
         count--;
     // Search for a shape no taller than the golden ratio.
     } while (count && (width  < (height*1.68)) );