+ lines.Add( value.Mid( startPos ) );
+ }
+}
+
+
+void wxGrid::GetTextBoxSize( wxDC& dc,
+ wxArrayString& lines,
+ long *width, long *height )
+{
+ long w = 0;
+ long h = 0;
+ long lineW, lineH;
+
+ size_t i;
+ for ( i = 0; i < lines.GetCount(); i++ )
+ {
+ dc.GetTextExtent( lines[i], &lineW, &lineH );
+ w = wxMax( w, lineW );
+ h += lineH;
+ }
+
+ *width = w;
+ *height = h;
+}
+
+
+//
+// ------ Edit control functions
+//
+
+
+void wxGrid::EnableEditing( bool edit )
+{
+ // TODO: improve this ?
+ //
+ if ( edit != m_editable )
+ {
+ m_editable = edit;
+
+ // TODO: extend this for other edit control types
+ //
+ if ( m_editCtrlType == wxGRID_TEXTCTRL )
+ {
+ ((wxTextCtrl *)m_cellEditCtrl)->SetEditable( m_editable );
+ }
+ }
+}
+
+
+#if 0 // disabled for the moment - the cell control is always active
+void wxGrid::EnableCellEditControl( bool enable )
+{
+ if ( m_cellEditCtrl &&
+ enable != m_cellEditCtrlEnabled )
+ {
+ m_cellEditCtrlEnabled = enable;
+
+ if ( m_cellEditCtrlEnabled )
+ {
+ SetEditControlValue();
+ ShowCellEditControl();
+ }
+ else
+ {
+ HideCellEditControl();
+ SaveEditControlValue();
+ }
+ }
+}
+#endif
+
+
+void wxGrid::ShowCellEditControl()
+{
+ wxRect rect;
+
+ if ( IsCellEditControlEnabled() )
+ {
+ if ( !IsVisible( m_currentCellCoords ) )
+ {
+ return;
+ }
+ else
+ {
+ rect = CellToRect( m_currentCellCoords );
+
+ // convert to scrolled coords
+ //
+ int left, top, right, bottom;
+ CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top );
+ CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom );
+
+ int cw, ch;
+ m_gridWin->GetClientSize( &cw, &ch );
+
+ // Make the edit control large enough to allow for internal margins
+ // TODO: remove this if the text ctrl sizing is improved esp. for unix
+ //
+ int extra;
+#if defined(__WXMOTIF__)
+ if ( m_currentCellCoords.GetRow() == 0 ||
+ m_currentCellCoords.GetCol() == 0 )
+ {
+ extra = 2;
+ }
+ else
+ {
+ extra = 4;
+ }
+#else
+ if ( m_currentCellCoords.GetRow() == 0 ||
+ m_currentCellCoords.GetCol() == 0 )
+ {
+ extra = 1;
+ }
+ else
+ {
+ extra = 2;
+ }
+#endif
+
+#if defined(__WXGTK__)
+ int top_diff = 0;
+ int left_diff = 0;
+ if (left != 0) left_diff++;
+ if (top != 0) top_diff++;
+ rect.SetLeft( left + left_diff );
+ rect.SetTop( top + top_diff );
+ rect.SetRight( rect.GetRight() - left_diff );
+ rect.SetBottom( rect.GetBottom() - top_diff );
+#else
+ rect.SetLeft( wxMax(0, left - extra) );
+ rect.SetTop( wxMax(0, top - extra) );
+ rect.SetRight( rect.GetRight() + 2*extra );
+ rect.SetBottom( rect.GetBottom() + 2*extra );
+#endif
+
+ m_cellEditCtrl->SetSize( rect );
+ m_cellEditCtrl->Show( TRUE );
+
+ switch ( m_editCtrlType )
+ {
+ case wxGRID_TEXTCTRL:
+ ((wxTextCtrl *) m_cellEditCtrl)->SetInsertionPointEnd();
+ break;
+
+ case wxGRID_CHECKBOX:
+ // TODO: anything ???
+ //
+ break;
+
+ case wxGRID_CHOICE:
+ // TODO: anything ???
+ //
+ break;
+
+ case wxGRID_COMBOBOX:
+ // TODO: anything ???
+ //
+ break;
+ }
+
+ m_cellEditCtrl->SetFocus();
+ }
+ }
+}
+
+
+void wxGrid::HideCellEditControl()
+{
+ if ( IsCellEditControlEnabled() )
+ {
+ m_cellEditCtrl->Show( FALSE );
+ }
+}
+
+
+void wxGrid::SetEditControlValue( const wxString& value )
+{
+ if ( m_table )
+ {
+ wxString s;
+ if ( !value )
+ s = GetCellValue(m_currentCellCoords);
+ else
+ s = value;
+
+ if ( IsCellEditControlEnabled() )
+ {
+ switch ( m_editCtrlType )
+ {
+ case wxGRID_TEXTCTRL:
+ ((wxGridTextCtrl *)m_cellEditCtrl)->SetStartValue(s);
+ break;
+
+ case wxGRID_CHECKBOX:
+ // TODO: implement this
+ //
+ break;
+
+ case wxGRID_CHOICE:
+ // TODO: implement this
+ //
+ break;
+
+ case wxGRID_COMBOBOX:
+ // TODO: implement this
+ //
+ break;
+ }
+ }
+ }
+}
+
+
+void wxGrid::SaveEditControlValue()
+{
+ if ( m_table )
+ {
+ wxWindow *ctrl = (wxWindow *)NULL;
+
+ if ( IsCellEditControlEnabled() )
+ {
+ ctrl = m_cellEditCtrl;
+ }
+ else
+ {
+ return;
+ }
+
+ bool valueChanged = FALSE;
+
+ switch ( m_editCtrlType )
+ {
+ case wxGRID_TEXTCTRL:
+ valueChanged = (((wxGridTextCtrl *)ctrl)->GetValue() !=
+ ((wxGridTextCtrl *)ctrl)->GetStartValue());
+ SetCellValue( m_currentCellCoords,
+ ((wxTextCtrl *) ctrl)->GetValue() );
+ break;
+
+ case wxGRID_CHECKBOX:
+ // TODO: implement this
+ //
+ break;
+
+ case wxGRID_CHOICE:
+ // TODO: implement this
+ //
+ break;
+
+ case wxGRID_COMBOBOX:
+ // TODO: implement this
+ //
+ break;
+ }
+
+ if ( valueChanged )
+ {
+ SendEvent( EVT_GRID_CELL_CHANGE,
+ m_currentCellCoords.GetRow(),
+ m_currentCellCoords.GetCol() );
+ }
+ }
+}
+
+
+//
+// ------ Grid location functions
+// Note that all of these functions work with the logical coordinates of
+// grid cells and labels so you will need to convert from device
+// coordinates for mouse events etc.
+//
+
+void wxGrid::XYToCell( int x, int y, wxGridCellCoords& coords )
+{
+ int row = YToRow(y);
+ int col = XToCol(x);
+
+ if ( row == -1 || col == -1 )
+ {
+ coords = wxGridNoCellCoords;
+ }
+ else
+ {
+ coords.Set( row, col );
+ }
+}
+
+
+int wxGrid::YToRow( int y )
+{
+ int i;
+
+ for ( i = 0; i < m_numRows; i++ )
+ {
+ if ( y < m_rowBottoms[i] ) return i;