X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b412f9be504e07559a98ae234f30bb1bd0b2aa1f..c693edf3bc9539378a7ac56d90d41d89c7dc7579:/src/generic/gridg.cpp diff --git a/src/generic/gridg.cpp b/src/generic/gridg.cpp index 0747e1917f..db89849d02 100644 --- a/src/generic/gridg.cpp +++ b/src/generic/gridg.cpp @@ -2,7 +2,8 @@ // Name: gridg.cpp // Purpose: wxGenericGrid // Author: Julian Smart -// Modified by: +// Modified by: Michael Bedward 20 Apr 1999 +// Added edit in place facility // Created: 04/01/98 // RCS-ID: $Id$ // Copyright: (c) Julian Smart and Markus Holzem @@ -22,7 +23,10 @@ #endif #ifndef WX_PRECOMP -#include "wx/wx.h" + #include "wx/utils.h" + #include "wx/dcclient.h" + #include "wx/dcmemory.h" + #include "wx/textctrl.h" #endif #include @@ -33,9 +37,9 @@ // Set to zero to use no double-buffering #ifdef __WXMSW__ -#define wxUSE_DOUBLE_BUFFERING 1 + #define wxUSE_DOUBLE_BUFFERING 1 #else -#define wxUSE_DOUBLE_BUFFERING 0 + #define wxUSE_DOUBLE_BUFFERING 0 #endif #define wxGRID_DRAG_NONE 0 @@ -51,6 +55,7 @@ BEGIN_EVENT_TABLE(wxGenericGrid, wxPanel) EVT_ERASE_BACKGROUND(wxGenericGrid::OnEraseBackground) EVT_MOUSE_EVENTS(wxGenericGrid::OnMouseEvent) EVT_TEXT(wxGRID_TEXT_CTRL, wxGenericGrid::OnText) + EVT_TEXT(wxGRID_EDIT_IN_PLACE_TEXT_CTRL, wxGenericGrid::OnTextInPlace) EVT_COMMAND_SCROLL(wxGRID_HSCROLL, wxGenericGrid::OnGridScroll) EVT_COMMAND_SCROLL(wxGRID_VSCROLL, wxGenericGrid::OnGridScroll) @@ -68,8 +73,6 @@ BEGIN_EVENT_TABLE(wxGenericGrid, wxPanel) END_EVENT_TABLE() - - wxGenericGrid::wxGenericGrid(void) { m_batchCount = 0; @@ -86,6 +89,10 @@ wxGenericGrid::wxGenericGrid(void) m_textItem = (wxTextCtrl *) NULL; m_currentRectVisible = FALSE; m_editable = TRUE; + + m_editInPlace = TRUE; + m_inOnTextInPlace = FALSE; + #if defined(__WIN95__) m_scrollWidth = wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X); #elif defined(__WXGTK__) @@ -206,8 +213,9 @@ bool wxGenericGrid::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, m_editingPanel = new wxPanel(this); m_textItem = new wxTextCtrl(m_editingPanel, wxGRID_TEXT_CTRL, "", - wxPoint(m_editControlPosition.x, m_editControlPosition.y), wxSize(m_editControlPosition.width, -1), - 0); + wxPoint(m_editControlPosition.x, m_editControlPosition.y), + wxSize(m_editControlPosition.width, -1), + 0); m_textItem->Show(TRUE); m_textItem->SetFocus(); int controlW, controlH; @@ -222,6 +230,15 @@ bool wxGenericGrid::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, m_hScrollBar = new wxScrollBar(this, wxGRID_HSCROLL, wxPoint(0, 0), wxSize(20, 100), wxHORIZONTAL); m_vScrollBar = new wxScrollBar(this, wxGRID_VSCROLL, wxPoint(0, 0), wxSize(100, 20), wxVERTICAL); +// SetSize(pos.x, pos.y, size.x, size.y); + + m_inPlaceTextItem = new wxTextCtrl( (wxPanel*)this, wxGRID_EDIT_IN_PLACE_TEXT_CTRL, "", + wxPoint( m_currentRect.x-2, m_currentRect.y-2 ), + wxSize( m_currentRect.width+4, m_currentRect.height+4 ), + wxNO_BORDER ); + m_inPlaceTextItem->Show(TRUE); + m_inPlaceTextItem->SetFocus(); + return TRUE; } @@ -512,7 +529,7 @@ void wxGenericGrid::PaintGrid(wxDC& dc) /* Hilight present cell */ SetCurrentRect(m_wCursorRow, m_wCursorColumn); - if (m_currentRectVisible) + if (m_currentRectVisible && !(m_editable && m_editInPlace)) HighlightCell(& dc); dc.DestroyClippingRegion(); @@ -1395,8 +1412,11 @@ void wxGenericGrid::OnSelectCellImplementation(wxDC *dc, int row, int col) SetGridClippingRegion(dc); // Remove the highlight from the old cell - if (m_currentRectVisible) - HighlightCell(dc); + if ( m_currentRectVisible && !(m_editable && m_editInPlace) ) + { + HighlightCell(dc); + } + // Highlight the new cell and copy its content to the edit control SetCurrentRect(m_wCursorRow, m_wCursorColumn); @@ -1411,18 +1431,48 @@ void wxGenericGrid::OnSelectCellImplementation(wxDC *dc, int row, int col) SetGridClippingRegion(dc); - // 1) Why isn't this needed for Windows?? - // Probably because of the SetValue?? JS. - // 2) Arrrrrgh. This isn't needed anywhere, - // of course. One hour of debugging... RR. -#ifndef __WXMSW__ -// HighlightCell(dc); + + if ( m_editable && m_editInPlace ) + { + m_inPlaceTextItem->SetSize( m_currentRect.x-2, m_currentRect.y-2, + m_currentRect.width+4, m_currentRect.height+4 ); + + if ( cell ) + { + if ( cell->GetTextValue().IsNull() ) + { + m_inPlaceTextItem->SetValue( "" ); + } + else + { + m_inPlaceTextItem->SetFont( cell->GetFont() ); + m_inPlaceTextItem->SetValue( cell->GetTextValue() ); + } + } + + m_inPlaceTextItem->Show(TRUE); + m_inPlaceTextItem->SetFocus(); + } + else + { + // 1) Why isn't this needed for Windows?? + // Probably because of the SetValue?? JS. + // 2) Arrrrrgh. This isn't needed anywhere, + // of course. One hour of debugging... RR. + // + // 3) It *is* needed for Motif - michael + // +#ifdef __WXMOTIF__ + if ( !(m_editable && m_editInPlace) ) + HighlightCell(dc); #endif + } + dc->DestroyClippingRegion(); - //OnSelectCell(row, col); + OnSelectCell(row, col); wxGridEvent g_evt2(GetId(), wxEVT_GRID_SELECT_CELL, this, row, col); - GetEventHandler()->ProcessEvent(g_evt2); + GetEventHandler()->ProcessEvent(g_evt2); } wxGridCell *wxGenericGrid::OnCreateCell(void) @@ -1508,8 +1558,6 @@ void wxGenericGrid::DrawCellText(void) if (!cell) return; - static char szEdit[300]; - wxClientDC dc(this); dc.BeginDrawing(); @@ -1518,7 +1566,7 @@ void wxGenericGrid::DrawCellText(void) dc.SetBackgroundMode(wxTRANSPARENT); dc.SetBrush(cell->GetBackgroundBrush()); - strcpy(szEdit, m_textItem->GetValue()); + wxString editValue = m_textItem->GetValue(); wxRect rect; rect = m_currentRect; @@ -1527,8 +1575,9 @@ void wxGenericGrid::DrawCellText(void) rect.width -= 5; rect.height -= 4; + // FIXME: what's this string of spaces supposed to represent? DrawTextRect(& dc, " ", &rect, wxLEFT); - DrawTextRect(& dc, szEdit, &rect, cell->GetAlignment()); + DrawTextRect(& dc, editValue, &rect, cell->GetAlignment()); dc.DestroyClippingRegion(); @@ -1959,6 +2008,12 @@ void wxGenericGrid::SetEditable(bool edit) m_textItem->Show(TRUE); m_textItem->SetFocus(); } + + if (m_inPlaceTextItem) + { + m_inPlaceTextItem->Show(TRUE); + m_inPlaceTextItem->SetFocus(); + } } else { @@ -1968,6 +2023,11 @@ void wxGenericGrid::SetEditable(bool edit) m_textItem->Show(FALSE); m_editingPanel->Show(FALSE); } + + if ( m_inPlaceTextItem ) + { + m_inPlaceTextItem->Show(FALSE); + } } UpdateDimensions(); SetCurrentRect(GetCursorRow(), GetCursorColumn()); @@ -1987,6 +2047,47 @@ void wxGenericGrid::SetEditable(bool edit) */ } + +void wxGenericGrid::SetEditInPlace(bool edit) +{ + if ( m_editInPlace != edit ) + { + m_editInPlace = edit; + + if ( m_editInPlace ) // switched on + { + if ( m_currentRectVisible && m_editable ) + { + m_inPlaceTextItem->SetSize( m_currentRect.x-2, m_currentRect.y-2, + m_currentRect.width+4, m_currentRect.height+4 ); + + wxGridCell *cell = GetCell(m_wCursorRow, m_wCursorColumn); + + if ( cell ) + { + if ( cell->GetTextValue().IsNull() ) + { + m_inPlaceTextItem->SetValue( "" ); + } + else + { + m_inPlaceTextItem->SetFont( cell->GetFont() ); + m_inPlaceTextItem->SetValue( cell->GetTextValue() ); + } + } + + m_inPlaceTextItem->Show( TRUE ); + m_inPlaceTextItem->SetFocus(); + } + } + else // switched off + { + m_inPlaceTextItem->Show( FALSE ); + } + } +} + + void wxGenericGrid::SetCellAlignment(int flag, int row, int col) { wxGridCell *cell = GetCell(row, col); @@ -2387,7 +2488,7 @@ void wxGenericGrid::SetGridCursor(int row, int col) SetGridClippingRegion(& dc); - if (m_currentRectVisible) + if (m_currentRectVisible && !(m_editable && m_editInPlace) ) HighlightCell(& dc); m_wCursorRow = row; @@ -2398,7 +2499,7 @@ void wxGenericGrid::SetGridCursor(int row, int col) SetCurrentRect(row, col, cw, ch); - if (m_currentRectVisible) + if (m_currentRectVisible && !(m_editable && m_editInPlace) ) HighlightCell(& dc); dc.DestroyClippingRegion(); @@ -2448,28 +2549,56 @@ void wxGridCell::SetBackgroundColour(const wxColour& colour) void wxGenericGrid::OnText(wxCommandEvent& WXUNUSED(ev) ) { - wxGenericGrid *grid = this; - wxGridCell *cell = grid->GetCell(grid->GetCursorRow(), grid->GetCursorColumn()); - if (cell && grid->CurrentCellVisible()) - { - cell->SetTextValue(grid->GetTextItem()->GetValue()); - wxClientDC dc(grid); - - dc.BeginDrawing(); - grid->SetGridClippingRegion(& dc); - grid->DrawCellBackground(& dc, &grid->GetCurrentRect(), grid->GetCursorRow(), grid->GetCursorColumn()); - grid->DrawCellValue(& dc, &grid->GetCurrentRect(), grid->GetCursorRow(), grid->GetCursorColumn()); - grid->HighlightCell(& dc); - dc.DestroyClippingRegion(); - dc.EndDrawing(); - - //grid->OnCellChange(grid->GetCursorRow(), grid->GetCursorColumn()); - wxGridEvent g_evt(GetId(), wxEVT_GRID_CELL_CHANGE, grid, - grid->GetCursorRow(), grid->GetCursorColumn()); - GetEventHandler()->ProcessEvent(g_evt); + // michael - added this conditional to prevent change to + // grid cell text when edit control is hidden but still has + // focus + // + if ( m_editable ) + { + wxGenericGrid *grid = this; + wxGridCell *cell = grid->GetCell(grid->GetCursorRow(), grid->GetCursorColumn()); + if (cell && grid->CurrentCellVisible()) + { + cell->SetTextValue(grid->GetTextItem()->GetValue()); + if ( m_editInPlace && !m_inOnTextInPlace ) + { + m_inPlaceTextItem->SetValue( grid->GetTextItem()->GetValue() ); + } + + wxClientDC dc(grid); + + dc.BeginDrawing(); + grid->SetGridClippingRegion(& dc); + grid->DrawCellBackground(& dc, &grid->GetCurrentRect(), grid->GetCursorRow(), grid->GetCursorColumn()); + grid->DrawCellValue(& dc, &grid->GetCurrentRect(), grid->GetCursorRow(), grid->GetCursorColumn()); + if ( !(m_editable && m_editInPlace ) ) grid->HighlightCell(& dc); + dc.DestroyClippingRegion(); + dc.EndDrawing(); + + //grid->OnCellChange(grid->GetCursorRow(), grid->GetCursorColumn()); + wxGridEvent g_evt(GetId(), wxEVT_GRID_CELL_CHANGE, grid, + grid->GetCursorRow(), grid->GetCursorColumn()); + GetEventHandler()->ProcessEvent(g_evt); + + // grid->DrawCellText(); + } + } +} -// grid->DrawCellText(); - } +void wxGenericGrid::OnTextInPlace(wxCommandEvent& ev ) +{ + if ( m_editable ) + { + wxGenericGrid *grid = this; + wxGridCell *cell = grid->GetCell(grid->GetCursorRow(), grid->GetCursorColumn()); + if (cell && grid->CurrentCellVisible()) + { + m_inOnTextInPlace = TRUE; + grid->GetTextItem()->SetValue( m_inPlaceTextItem->GetValue() ); + OnText( ev ); + m_inOnTextInPlace = FALSE; + } + } } void wxGenericGrid::OnGridScroll(wxScrollEvent& ev) @@ -2479,6 +2608,8 @@ void wxGenericGrid::OnGridScroll(wxScrollEvent& ev) if ( inScroll ) return; + if ( m_editInPlace ) m_inPlaceTextItem->Show(FALSE); + inScroll = TRUE; wxGenericGrid *win = this; @@ -2505,6 +2636,15 @@ void wxGenericGrid::OnGridScroll(wxScrollEvent& ev) AdjustScrollbars(); if (change) win->Refresh(FALSE); + + if ( m_editInPlace && m_currentRectVisible ) + { + m_inPlaceTextItem->SetSize( m_currentRect.x-2, m_currentRect.y-2, + m_currentRect.width+4, m_currentRect.height+4 ); + m_inPlaceTextItem->Show( TRUE ); + m_inPlaceTextItem->SetFocus(); + } + inScroll = FALSE; }