#include "wx/string.h"
#include "wx/scrolbar.h"
#include "wx/event.h"
-#include "wx/textctrl.h"
#include "wx/combobox.h"
#include "wx/dynarray.h"
#include "wx/timer.h"
class WXDLLEXPORT wxGridTableBase;
class WXDLLEXPORT wxGridWindow;
+class WXDLLEXPORT wxCheckBox;
+class WXDLLEXPORT wxTextCtrl;
+
// ----------------------------------------------------------------------------
// wxGridCellRenderer: this class is responsible for actually drawing the cell
// in the grid. You may pass it to the wxGridCellAttr (below) to change the
bool isSelected);
};
+// renderer for boolean fields
+class WXDLLEXPORT wxGridCellBoolRenderer : public wxGridCellRenderer
+{
+public:
+
+ // draw a check mark or nothing
+ virtual void Draw(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ const wxRect& rect,
+ int row, int col,
+ bool isSelected);
+};
// ----------------------------------------------------------------------------
// wxGridCellEditor: This class is responsible for providing and manipulating
wxFont m_fontOld;
};
-
+// the editor for string/text data
class WXDLLEXPORT wxGridCellTextEditor : public wxGridCellEditor
{
public:
wxString m_startValue;
};
+// the editor for boolean data
+class WXDLLEXPORT wxGridCellBoolEditor : public wxGridCellEditor
+{
+public:
+ virtual void Create(wxWindow* parent,
+ wxWindowID id,
+ wxEvtHandler* evtHandler);
+
+ virtual void SetSize(const wxRect& rect);
+ virtual void Show(bool show, wxGridCellAttr *attr = (wxGridCellAttr *)NULL);
+
+ virtual void BeginEdit(int row, int col, wxGrid* grid);
+ virtual bool EndEdit(int row, int col, bool saveValue, wxGrid* grid);
+
+ virtual void Reset();
+ virtual void StartingKey(wxKeyEvent& event);
+
+protected:
+ wxCheckBox *CBox() const { return (wxCheckBox *)m_control; }
+
+private:
+ bool m_startValue;
+
+ wxRect m_rectCell; // the total size of the cell
+};
+
// ----------------------------------------------------------------------------
// wxGridCellAttr: this class can be used to alter the cells appearance in
// the grid by changing their colour/font/... from default. An object of this
return *this;
}
- bool operator==( const wxGridCellCoords& other )
+ bool operator==( const wxGridCellCoords& other ) const
{
return (m_row == other.m_row && m_col == other.m_col);
}
- bool operator!=( const wxGridCellCoords& other )
+ bool operator!=( const wxGridCellCoords& other ) const
{
return (m_row != other.m_row || m_col != other.m_col);
}
- bool operator!()
+ bool operator!() const
{
return (m_row == -1 && m_col == -1 );
}
wxGRID_CHOICE,
wxGRID_COMBOBOX };
+ // for wxGridCellBoolEditor
+ wxWindow *GetGridWindow() const;
+
protected:
bool m_created;
bool m_displayed;
#include "wx/dcclient.h"
#include "wx/settings.h"
#include "wx/log.h"
+ #include "wx/textctrl.h"
+ #include "wx/checkbox.h"
#endif
// this include needs to be outside precomp for BCC
Destroy();
}
+void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent),
+ wxWindowID WXUNUSED(id),
+ wxEvtHandler* evtHandler)
+{
+ if (evtHandler)
+ m_control->PushEventHandler(evtHandler);
+}
void wxGridCellEditor::Destroy()
{
- if (m_control) {
+ if (m_control)
+ {
m_control->Destroy();
m_control = NULL;
}
wxWindowID id,
wxEvtHandler* evtHandler)
{
- m_control = new wxTextCtrl(parent, -1, "",
+ m_control = new wxTextCtrl(parent, id, wxEmptyString,
wxDefaultPosition, wxDefaultSize
#if defined(__WXMSW__)
, wxTE_MULTILINE | wxTE_NO_VSCROLL // necessary ???
#endif
- );
+ );
- if (evtHandler)
- m_control->PushEventHandler(evtHandler);
+ wxGridCellEditor::Create(parent, id, evtHandler);
}
}
-
bool wxGridCellTextEditor::EndEdit(int row, int col, bool saveValue,
wxGrid* grid)
{
#endif
}
+// ----------------------------------------------------------------------------
+// wxGridCellBoolEditor
+// ----------------------------------------------------------------------------
+
+void wxGridCellBoolEditor::Create(wxWindow* parent,
+ wxWindowID id,
+ wxEvtHandler* evtHandler)
+{
+ m_control = new wxCheckBox(parent, id, wxEmptyString,
+ wxDefaultPosition, wxDefaultSize,
+ wxNO_BORDER);
+
+ wxGridCellEditor::Create(parent, id, evtHandler);
+}
+
+void wxGridCellBoolEditor::SetSize(const wxRect& r)
+{
+ m_rectCell = r;
+ // position it in the centre of the rectangle (TODO: support alignment?)
+ wxCoord w, h;
+ m_control->GetSize(&w, &h);
+
+ // the checkbox without label still has some space to the right in wxGTK,
+ // so shift it to the right
+#ifdef __WXGTK__
+ w += 8;
+#endif // GTK
+
+ m_control->Move(r.x + r.width/2 - w/2, r.y + r.height/2 - h/2);
+}
+
+void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr)
+{
+ wxGridCellEditor::Show(show, attr);
+ if ( !show )
+ return;
+
+ // get the bg colour to use
+ wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY;
+
+ // erase the background because we don't fill the cell
+ if ( m_rectCell.width > 0 )
+ {
+ wxClientDC dc(m_control->GetParent());
+ dc.SetPen(*wxTRANSPARENT_PEN);
+ dc.SetBrush(wxBrush(colBg, wxSOLID));
+ dc.DrawRectangle(m_rectCell);
+
+ m_rectCell.width = 0;
+ }
+
+ CBox()->SetBackgroundColour(colBg);
+}
+
+void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid)
+{
+ wxASSERT_MSG(m_control,
+ wxT("The wxGridCellEditor must be Created first!"));
+
+ m_startValue = !!grid->GetTable()->GetValue(row, col); // FIXME-DATA
+ CBox()->SetValue(m_startValue);
+ CBox()->SetFocus();
+}
+
+bool wxGridCellBoolEditor::EndEdit(int row, int col,
+ bool saveValue,
+ wxGrid* grid)
+{
+ wxASSERT_MSG(m_control,
+ wxT("The wxGridCellEditor must be Created first!"));
+
+ bool changed = FALSE;
+ bool value = CBox()->GetValue();
+ if ( value != m_startValue )
+ changed = TRUE;
+
+ if ( changed )
+ {
+ // FIXME-DATA
+ grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString);
+ }
+
+ return changed;
+}
+
+void wxGridCellBoolEditor::Reset()
+{
+ wxASSERT_MSG(m_control,
+ wxT("The wxGridCellEditor must be Created first!"));
+
+ CBox()->SetValue(m_startValue);
+}
+
+void wxGridCellBoolEditor::StartingKey(wxKeyEvent& event)
+{
+ event.Skip();
+}
+
+// ----------------------------------------------------------------------------
+// wxGridCellEditorEvtHandler
+// ----------------------------------------------------------------------------
void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event)
{
}
}
+// ============================================================================
+// renderer classes
+// ============================================================================
+
// ----------------------------------------------------------------------------
// wxGridCellRenderer
// ----------------------------------------------------------------------------
dc.DrawRectangle(rect);
}
+// ----------------------------------------------------------------------------
+// wxGridCellStringRenderer
+// ----------------------------------------------------------------------------
+
void wxGridCellStringRenderer::Draw(wxGrid& grid,
wxGridCellAttr& attr,
wxDC& dc,
rect, hAlign, vAlign);
}
+// ----------------------------------------------------------------------------
+// wxGridCellBoolRenderer
+// ----------------------------------------------------------------------------
+
+void wxGridCellBoolRenderer::Draw(wxGrid& grid,
+ wxGridCellAttr& attr,
+ wxDC& dc,
+ const wxRect& rect,
+ int row, int col,
+ bool isSelected)
+{
+ wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected);
+
+ // position it in the centre (ignoring alignment - TODO)
+ static const wxCoord checkSize = 16;
+
+ wxRect rectMark;
+ rectMark.x = rect.x + rect.width/2 - checkSize/2;
+ rectMark.y = rect.y + rect.height/2 - checkSize/2;
+ rectMark.width = rectMark.height = checkSize;
+
+ dc.SetBrush(*wxTRANSPARENT_BRUSH);
+ dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID));
+ dc.DrawRectangle(rectMark);
+
+ rectMark.Inflate(-4);
+
+ if ( !!grid.GetTable()->GetValue(row, col) ) // FIXME-DATA
+ {
+ dc.SetTextForeground(attr.GetTextColour());
+ dc.DrawCheckMark(rectMark);
+ }
+}
+
// ----------------------------------------------------------------------------
// wxGridCellAttr
// ----------------------------------------------------------------------------
const wxColour& wxGridCellAttr::GetTextColour() const
{
if (HasTextColour())
+ {
return m_colText;
+ }
else if (m_defGridAttr != this)
+ {
return m_defGridAttr->GetTextColour();
- else {
+ }
+ else
+ {
wxFAIL_MSG(wxT("Missing default cell attribute"));
return wxNullColour;
}
return m_colBack;
else if (m_defGridAttr != this)
return m_defGridAttr->GetBackgroundColour();
- else {
+ else
+ {
wxFAIL_MSG(wxT("Missing default cell attribute"));
return wxNullColour;
}
return m_font;
else if (m_defGridAttr != this)
return m_defGridAttr->GetFont();
- else {
+ else
+ {
wxFAIL_MSG(wxT("Missing default cell attribute"));
return wxNullFont;
}
void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const
{
- if (HasAlignment()) {
+ if (HasAlignment())
+ {
if ( hAlign ) *hAlign = m_hAlign;
if ( vAlign ) *vAlign = m_vAlign;
}
else if (m_defGridAttr != this)
m_defGridAttr->GetAlignment(hAlign, vAlign);
- else {
+ else
+ {
wxFAIL_MSG(wxT("Missing default cell attribute"));
}
}
return m_renderer;
else if (m_defGridAttr != this)
return m_defGridAttr->GetRenderer();
- else {
+ else
+ {
wxFAIL_MSG(wxT("Missing default cell attribute"));
return NULL;
}
return m_editor;
else if (m_defGridAttr != this)
return m_defGridAttr->GetEditor();
- else {
+ else
+ {
wxFAIL_MSG(wxT("Missing default cell attribute"));
return NULL;
}
// Don't start doing anything until the mouse has been drug at
// least 3 pixels in any direction...
- if (! m_isDragging) {
- if (m_startDragPos == wxDefaultPosition) {
+ if (! m_isDragging)
+ {
+ if (m_startDragPos == wxDefaultPosition)
+ {
m_startDragPos = pos;
return;
}
HideCellEditControl();
// Have we captured the mouse yet?
- if (! m_winCapture) {
+ if (! m_winCapture)
+ {
m_winCapture = m_gridWin;
m_winCapture->CaptureMouse();
}
SelectBlock( m_currentCellCoords, coords );
}
- if (! IsVisible(coords)) {
+ if (! IsVisible(coords))
+ {
MakeCellVisible(coords);
// TODO: need to introduce a delay or something here. The
// scrolling is way to fast, at least on MSW.
{
if ( IsSelection() )
{
- if (m_winCapture) {
+ if (m_winCapture)
+ {
m_winCapture->ReleaseMouse();
m_winCapture = NULL;
}
void wxGrid::OnEraseBackground(wxEraseEvent&)
-{ }
-
-
-
+{
+}
void wxGrid::SetCurrentCell( const wxGridCellCoords& coords )
{
DrawCellBorder( dc, coords );
#endif
+ // don't draw the cell over the active edit control!
+ if ( (coords == m_currentCellCoords) && IsCellEditControlEnabled() )
+ return;
+
// but all the rest is drawn by the cell renderer and hence may be
// customized
wxRect rect;
int top, bottom, left, right;
- if (reg.IsEmpty()){
+ if (reg.IsEmpty())
+ {
int cw, ch;
m_gridWin->GetClientSize(&cw, &ch);
CalcUnscrolledPosition( 0, 0, &left, &top );
CalcUnscrolledPosition( cw, ch, &right, &bottom );
}
- else{
+ else
+ {
wxCoord x, y, w, h;
reg.GetBox(x, y, w, h);
CalcUnscrolledPosition( x, y, &left, &top );
return m_cellEditCtrlEnabled ? !IsCurrentCellReadOnly() : FALSE;
}
+wxWindow *wxGrid::GetGridWindow() const
+{
+ return m_gridWin;
+}
+
void wxGrid::ShowCellEditControl()
{
if ( IsCellEditControlEnabled() )
int left, top, right, bottom;
CalcScrolledPosition( rect.GetLeft(), rect.GetTop(), &left, &top );
CalcScrolledPosition( rect.GetRight(), rect.GetBottom(), &right, &bottom );
- left--; top--; right--; bottom--; // cell is shifted by one pixel
- int cw, ch;
- m_gridWin->GetClientSize( &cw, &ch );
+
+ // cell is shifted by one pixel
+ left--;
+ top--;
+ right--;
+ bottom--;
// Make the edit control large enough to allow for internal
// margins
rect.SetBottom( rect.GetBottom() + 2*extra );
#endif
- wxGridCellAttr* attr = GetCellAttr(row, col);
+ wxGridCellAttr* attr = GetCellAttr(row, col);
wxGridCellEditor* editor = attr->GetEditor();
if ( !editor->IsCreated() )
{
attr = m_table ? m_table->GetAttr(row, col) : (wxGridCellAttr *)NULL;
CacheAttr(row, col, attr);
}
- if (attr) {
+ if (attr)
+ {
attr->SetDefAttr(m_defaultCellAttr);
- } else {
+ }
+ else
+ {
attr = m_defaultCellAttr;
attr->IncRef();
}