| 1 | /////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: generic/grid.cpp |
| 3 | // Purpose: wxGrid and related classes |
| 4 | // Author: Michael Bedward (based on code by Julian Smart, Robin Dunn) |
| 5 | // Modified by: Robin Dunn, Vadim Zeitlin |
| 6 | // Created: 1/08/1999 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Michael Bedward (mbedward@ozemail.com.au) |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 21 | #pragma implementation "grid.h" |
| 22 | #endif |
| 23 | |
| 24 | // For compilers that support precompilatixon, includes "wx/wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #include "wx/defs.h" |
| 28 | |
| 29 | #ifdef __BORLANDC__ |
| 30 | #pragma hdrstop |
| 31 | #endif |
| 32 | |
| 33 | #if wxUSE_GRID |
| 34 | |
| 35 | #ifndef WX_PRECOMP |
| 36 | #include "wx/utils.h" |
| 37 | #include "wx/dcclient.h" |
| 38 | #include "wx/settings.h" |
| 39 | #include "wx/log.h" |
| 40 | #include "wx/textctrl.h" |
| 41 | #include "wx/checkbox.h" |
| 42 | #include "wx/combobox.h" |
| 43 | #include "wx/valtext.h" |
| 44 | #include "wx/intl.h" |
| 45 | #endif |
| 46 | |
| 47 | #include "wx/textfile.h" |
| 48 | #include "wx/spinctrl.h" |
| 49 | #include "wx/tokenzr.h" |
| 50 | |
| 51 | #include "wx/grid.h" |
| 52 | #include "wx/generic/gridsel.h" |
| 53 | |
| 54 | #if defined(__WXMOTIF__) |
| 55 | #define WXUNUSED_MOTIF(identifier) WXUNUSED(identifier) |
| 56 | #else |
| 57 | #define WXUNUSED_MOTIF(identifier) identifier |
| 58 | #endif |
| 59 | |
| 60 | #if defined(__WXGTK__) |
| 61 | #define WXUNUSED_GTK(identifier) WXUNUSED(identifier) |
| 62 | #else |
| 63 | #define WXUNUSED_GTK(identifier) identifier |
| 64 | #endif |
| 65 | |
| 66 | // Required for wxIs... functions |
| 67 | #include <ctype.h> |
| 68 | |
| 69 | // ---------------------------------------------------------------------------- |
| 70 | // array classes |
| 71 | // ---------------------------------------------------------------------------- |
| 72 | |
| 73 | WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridCellAttr *, wxArrayAttrs, |
| 74 | class WXDLLIMPEXP_ADV); |
| 75 | |
| 76 | struct wxGridCellWithAttr |
| 77 | { |
| 78 | wxGridCellWithAttr(int row, int col, wxGridCellAttr *attr_) |
| 79 | : coords(row, col), attr(attr_) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | ~wxGridCellWithAttr() |
| 84 | { |
| 85 | attr->DecRef(); |
| 86 | } |
| 87 | |
| 88 | wxGridCellCoords coords; |
| 89 | wxGridCellAttr *attr; |
| 90 | |
| 91 | // Cannot do this: |
| 92 | // DECLARE_NO_COPY_CLASS(wxGridCellWithAttr) |
| 93 | // without rewriting the macros, which require a public copy constructor. |
| 94 | }; |
| 95 | |
| 96 | WX_DECLARE_OBJARRAY_WITH_DECL(wxGridCellWithAttr, wxGridCellWithAttrArray, |
| 97 | class WXDLLIMPEXP_ADV); |
| 98 | |
| 99 | #include "wx/arrimpl.cpp" |
| 100 | |
| 101 | WX_DEFINE_OBJARRAY(wxGridCellCoordsArray) |
| 102 | WX_DEFINE_OBJARRAY(wxGridCellWithAttrArray) |
| 103 | |
| 104 | // ---------------------------------------------------------------------------- |
| 105 | // events |
| 106 | // ---------------------------------------------------------------------------- |
| 107 | |
| 108 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_CLICK) |
| 109 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_CLICK) |
| 110 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_LEFT_DCLICK) |
| 111 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_RIGHT_DCLICK) |
| 112 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_BEGIN_DRAG) |
| 113 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_CLICK) |
| 114 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_CLICK) |
| 115 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_LEFT_DCLICK) |
| 116 | DEFINE_EVENT_TYPE(wxEVT_GRID_LABEL_RIGHT_DCLICK) |
| 117 | DEFINE_EVENT_TYPE(wxEVT_GRID_ROW_SIZE) |
| 118 | DEFINE_EVENT_TYPE(wxEVT_GRID_COL_SIZE) |
| 119 | DEFINE_EVENT_TYPE(wxEVT_GRID_RANGE_SELECT) |
| 120 | DEFINE_EVENT_TYPE(wxEVT_GRID_CELL_CHANGE) |
| 121 | DEFINE_EVENT_TYPE(wxEVT_GRID_SELECT_CELL) |
| 122 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_SHOWN) |
| 123 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_HIDDEN) |
| 124 | DEFINE_EVENT_TYPE(wxEVT_GRID_EDITOR_CREATED) |
| 125 | |
| 126 | // ---------------------------------------------------------------------------- |
| 127 | // private classes |
| 128 | // ---------------------------------------------------------------------------- |
| 129 | |
| 130 | class WXDLLIMPEXP_ADV wxGridRowLabelWindow : public wxWindow |
| 131 | { |
| 132 | public: |
| 133 | wxGridRowLabelWindow() { m_owner = (wxGrid *)NULL; } |
| 134 | wxGridRowLabelWindow( wxGrid *parent, wxWindowID id, |
| 135 | const wxPoint &pos, const wxSize &size ); |
| 136 | |
| 137 | private: |
| 138 | wxGrid *m_owner; |
| 139 | |
| 140 | void OnPaint( wxPaintEvent& event ); |
| 141 | void OnMouseEvent( wxMouseEvent& event ); |
| 142 | void OnMouseWheel( wxMouseEvent& event ); |
| 143 | void OnKeyDown( wxKeyEvent& event ); |
| 144 | void OnKeyUp( wxKeyEvent& ); |
| 145 | |
| 146 | DECLARE_DYNAMIC_CLASS(wxGridRowLabelWindow) |
| 147 | DECLARE_EVENT_TABLE() |
| 148 | DECLARE_NO_COPY_CLASS(wxGridRowLabelWindow) |
| 149 | }; |
| 150 | |
| 151 | |
| 152 | class WXDLLIMPEXP_ADV wxGridColLabelWindow : public wxWindow |
| 153 | { |
| 154 | public: |
| 155 | wxGridColLabelWindow() { m_owner = (wxGrid *)NULL; } |
| 156 | wxGridColLabelWindow( wxGrid *parent, wxWindowID id, |
| 157 | const wxPoint &pos, const wxSize &size ); |
| 158 | |
| 159 | private: |
| 160 | wxGrid *m_owner; |
| 161 | |
| 162 | void OnPaint( wxPaintEvent &event ); |
| 163 | void OnMouseEvent( wxMouseEvent& event ); |
| 164 | void OnMouseWheel( wxMouseEvent& event ); |
| 165 | void OnKeyDown( wxKeyEvent& event ); |
| 166 | void OnKeyUp( wxKeyEvent& ); |
| 167 | |
| 168 | DECLARE_DYNAMIC_CLASS(wxGridColLabelWindow) |
| 169 | DECLARE_EVENT_TABLE() |
| 170 | DECLARE_NO_COPY_CLASS(wxGridColLabelWindow) |
| 171 | }; |
| 172 | |
| 173 | |
| 174 | class WXDLLIMPEXP_ADV wxGridCornerLabelWindow : public wxWindow |
| 175 | { |
| 176 | public: |
| 177 | wxGridCornerLabelWindow() { m_owner = (wxGrid *)NULL; } |
| 178 | wxGridCornerLabelWindow( wxGrid *parent, wxWindowID id, |
| 179 | const wxPoint &pos, const wxSize &size ); |
| 180 | |
| 181 | private: |
| 182 | wxGrid *m_owner; |
| 183 | |
| 184 | void OnMouseEvent( wxMouseEvent& event ); |
| 185 | void OnMouseWheel( wxMouseEvent& event ); |
| 186 | void OnKeyDown( wxKeyEvent& event ); |
| 187 | void OnKeyUp( wxKeyEvent& ); |
| 188 | void OnPaint( wxPaintEvent& event ); |
| 189 | |
| 190 | DECLARE_DYNAMIC_CLASS(wxGridCornerLabelWindow) |
| 191 | DECLARE_EVENT_TABLE() |
| 192 | DECLARE_NO_COPY_CLASS(wxGridCornerLabelWindow) |
| 193 | }; |
| 194 | |
| 195 | class WXDLLIMPEXP_ADV wxGridWindow : public wxWindow |
| 196 | { |
| 197 | public: |
| 198 | wxGridWindow() |
| 199 | { |
| 200 | m_owner = (wxGrid *)NULL; |
| 201 | m_rowLabelWin = (wxGridRowLabelWindow *)NULL; |
| 202 | m_colLabelWin = (wxGridColLabelWindow *)NULL; |
| 203 | } |
| 204 | |
| 205 | wxGridWindow( wxGrid *parent, |
| 206 | wxGridRowLabelWindow *rowLblWin, |
| 207 | wxGridColLabelWindow *colLblWin, |
| 208 | wxWindowID id, const wxPoint &pos, const wxSize &size ); |
| 209 | ~wxGridWindow(); |
| 210 | |
| 211 | void ScrollWindow( int dx, int dy, const wxRect *rect ); |
| 212 | |
| 213 | wxGrid* GetOwner() { return m_owner; } |
| 214 | |
| 215 | private: |
| 216 | wxGrid *m_owner; |
| 217 | wxGridRowLabelWindow *m_rowLabelWin; |
| 218 | wxGridColLabelWindow *m_colLabelWin; |
| 219 | |
| 220 | void OnPaint( wxPaintEvent &event ); |
| 221 | void OnMouseWheel( wxMouseEvent& event ); |
| 222 | void OnMouseEvent( wxMouseEvent& event ); |
| 223 | void OnKeyDown( wxKeyEvent& ); |
| 224 | void OnKeyUp( wxKeyEvent& ); |
| 225 | void OnEraseBackground( wxEraseEvent& ); |
| 226 | void OnFocus( wxFocusEvent& ); |
| 227 | |
| 228 | DECLARE_DYNAMIC_CLASS(wxGridWindow) |
| 229 | DECLARE_EVENT_TABLE() |
| 230 | DECLARE_NO_COPY_CLASS(wxGridWindow) |
| 231 | }; |
| 232 | |
| 233 | |
| 234 | |
| 235 | class wxGridCellEditorEvtHandler : public wxEvtHandler |
| 236 | { |
| 237 | public: |
| 238 | wxGridCellEditorEvtHandler() |
| 239 | : m_grid(0), m_editor(0) |
| 240 | { } |
| 241 | wxGridCellEditorEvtHandler(wxGrid* grid, wxGridCellEditor* editor) |
| 242 | : m_grid(grid), m_editor(editor) |
| 243 | { } |
| 244 | |
| 245 | void OnKeyDown(wxKeyEvent& event); |
| 246 | void OnChar(wxKeyEvent& event); |
| 247 | |
| 248 | private: |
| 249 | wxGrid* m_grid; |
| 250 | wxGridCellEditor* m_editor; |
| 251 | DECLARE_DYNAMIC_CLASS(wxGridCellEditorEvtHandler) |
| 252 | DECLARE_EVENT_TABLE() |
| 253 | DECLARE_NO_COPY_CLASS(wxGridCellEditorEvtHandler) |
| 254 | }; |
| 255 | |
| 256 | |
| 257 | IMPLEMENT_DYNAMIC_CLASS( wxGridCellEditorEvtHandler, wxEvtHandler ) |
| 258 | BEGIN_EVENT_TABLE( wxGridCellEditorEvtHandler, wxEvtHandler ) |
| 259 | EVT_KEY_DOWN( wxGridCellEditorEvtHandler::OnKeyDown ) |
| 260 | EVT_CHAR( wxGridCellEditorEvtHandler::OnChar ) |
| 261 | END_EVENT_TABLE() |
| 262 | |
| 263 | |
| 264 | |
| 265 | // ---------------------------------------------------------------------------- |
| 266 | // the internal data representation used by wxGridCellAttrProvider |
| 267 | // ---------------------------------------------------------------------------- |
| 268 | |
| 269 | // this class stores attributes set for cells |
| 270 | class WXDLLIMPEXP_ADV wxGridCellAttrData |
| 271 | { |
| 272 | public: |
| 273 | void SetAttr(wxGridCellAttr *attr, int row, int col); |
| 274 | wxGridCellAttr *GetAttr(int row, int col) const; |
| 275 | void UpdateAttrRows( size_t pos, int numRows ); |
| 276 | void UpdateAttrCols( size_t pos, int numCols ); |
| 277 | |
| 278 | private: |
| 279 | // searches for the attr for given cell, returns wxNOT_FOUND if not found |
| 280 | int FindIndex(int row, int col) const; |
| 281 | |
| 282 | wxGridCellWithAttrArray m_attrs; |
| 283 | }; |
| 284 | |
| 285 | // this class stores attributes set for rows or columns |
| 286 | class WXDLLIMPEXP_ADV wxGridRowOrColAttrData |
| 287 | { |
| 288 | public: |
| 289 | // empty ctor to suppress warnings |
| 290 | wxGridRowOrColAttrData() { } |
| 291 | ~wxGridRowOrColAttrData(); |
| 292 | |
| 293 | void SetAttr(wxGridCellAttr *attr, int rowOrCol); |
| 294 | wxGridCellAttr *GetAttr(int rowOrCol) const; |
| 295 | void UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ); |
| 296 | |
| 297 | private: |
| 298 | wxArrayInt m_rowsOrCols; |
| 299 | wxArrayAttrs m_attrs; |
| 300 | }; |
| 301 | |
| 302 | // NB: this is just a wrapper around 3 objects: one which stores cell |
| 303 | // attributes, and 2 others for row/col ones |
| 304 | class WXDLLIMPEXP_ADV wxGridCellAttrProviderData |
| 305 | { |
| 306 | public: |
| 307 | wxGridCellAttrData m_cellAttrs; |
| 308 | wxGridRowOrColAttrData m_rowAttrs, |
| 309 | m_colAttrs; |
| 310 | }; |
| 311 | |
| 312 | |
| 313 | // ---------------------------------------------------------------------------- |
| 314 | // data structures used for the data type registry |
| 315 | // ---------------------------------------------------------------------------- |
| 316 | |
| 317 | struct wxGridDataTypeInfo |
| 318 | { |
| 319 | wxGridDataTypeInfo(const wxString& typeName, |
| 320 | wxGridCellRenderer* renderer, |
| 321 | wxGridCellEditor* editor) |
| 322 | : m_typeName(typeName), m_renderer(renderer), m_editor(editor) |
| 323 | { } |
| 324 | |
| 325 | ~wxGridDataTypeInfo() |
| 326 | { |
| 327 | wxSafeDecRef(m_renderer); |
| 328 | wxSafeDecRef(m_editor); |
| 329 | } |
| 330 | |
| 331 | wxString m_typeName; |
| 332 | wxGridCellRenderer* m_renderer; |
| 333 | wxGridCellEditor* m_editor; |
| 334 | |
| 335 | DECLARE_NO_COPY_CLASS(wxGridDataTypeInfo) |
| 336 | }; |
| 337 | |
| 338 | |
| 339 | WX_DEFINE_ARRAY_WITH_DECL_PTR(wxGridDataTypeInfo*, wxGridDataTypeInfoArray, |
| 340 | class WXDLLIMPEXP_ADV); |
| 341 | |
| 342 | |
| 343 | class WXDLLIMPEXP_ADV wxGridTypeRegistry |
| 344 | { |
| 345 | public: |
| 346 | wxGridTypeRegistry() {} |
| 347 | ~wxGridTypeRegistry(); |
| 348 | |
| 349 | void RegisterDataType(const wxString& typeName, |
| 350 | wxGridCellRenderer* renderer, |
| 351 | wxGridCellEditor* editor); |
| 352 | |
| 353 | // find one of already registered data types |
| 354 | int FindRegisteredDataType(const wxString& typeName); |
| 355 | |
| 356 | // try to FindRegisteredDataType(), if this fails and typeName is one of |
| 357 | // standard typenames, register it and return its index |
| 358 | int FindDataType(const wxString& typeName); |
| 359 | |
| 360 | // try to FindDataType(), if it fails see if it is not one of already |
| 361 | // registered data types with some params in which case clone the |
| 362 | // registered data type and set params for it |
| 363 | int FindOrCloneDataType(const wxString& typeName); |
| 364 | |
| 365 | wxGridCellRenderer* GetRenderer(int index); |
| 366 | wxGridCellEditor* GetEditor(int index); |
| 367 | |
| 368 | private: |
| 369 | wxGridDataTypeInfoArray m_typeinfo; |
| 370 | }; |
| 371 | |
| 372 | // ---------------------------------------------------------------------------- |
| 373 | // conditional compilation |
| 374 | // ---------------------------------------------------------------------------- |
| 375 | |
| 376 | #ifndef WXGRID_DRAW_LINES |
| 377 | #define WXGRID_DRAW_LINES 1 |
| 378 | #endif |
| 379 | |
| 380 | // ---------------------------------------------------------------------------- |
| 381 | // globals |
| 382 | // ---------------------------------------------------------------------------- |
| 383 | |
| 384 | //#define DEBUG_ATTR_CACHE |
| 385 | #ifdef DEBUG_ATTR_CACHE |
| 386 | static size_t gs_nAttrCacheHits = 0; |
| 387 | static size_t gs_nAttrCacheMisses = 0; |
| 388 | #endif // DEBUG_ATTR_CACHE |
| 389 | |
| 390 | // ---------------------------------------------------------------------------- |
| 391 | // constants |
| 392 | // ---------------------------------------------------------------------------- |
| 393 | |
| 394 | wxGridCellCoords wxGridNoCellCoords( -1, -1 ); |
| 395 | wxRect wxGridNoCellRect( -1, -1, -1, -1 ); |
| 396 | |
| 397 | // scroll line size |
| 398 | // TODO: this doesn't work at all, grid cells have different sizes and approx |
| 399 | // calculations don't work as because of the size mismatch scrollbars |
| 400 | // sometimes fail to be shown when they should be or vice versa |
| 401 | // |
| 402 | // The scroll bars may be a little flakey once in a while, but that is |
| 403 | // surely much less horrible than having scroll lines of only 1!!! |
| 404 | // -- Robin |
| 405 | // |
| 406 | // Well, it's still seriously broken so it might be better but needs |
| 407 | // fixing anyhow |
| 408 | // -- Vadim |
| 409 | static const size_t GRID_SCROLL_LINE_X = 15; // 1; |
| 410 | static const size_t GRID_SCROLL_LINE_Y = GRID_SCROLL_LINE_X; |
| 411 | |
| 412 | // the size of hash tables used a bit everywhere (the max number of elements |
| 413 | // in these hash tables is the number of rows/columns) |
| 414 | static const int GRID_HASH_SIZE = 100; |
| 415 | |
| 416 | // ---------------------------------------------------------------------------- |
| 417 | // private functions |
| 418 | // ---------------------------------------------------------------------------- |
| 419 | |
| 420 | static inline int GetScrollX(int x) |
| 421 | { |
| 422 | return (x + GRID_SCROLL_LINE_X - 1) / GRID_SCROLL_LINE_X; |
| 423 | } |
| 424 | |
| 425 | static inline int GetScrollY(int y) |
| 426 | { |
| 427 | return (y + GRID_SCROLL_LINE_Y - 1) / GRID_SCROLL_LINE_Y; |
| 428 | } |
| 429 | |
| 430 | // ============================================================================ |
| 431 | // implementation |
| 432 | // ============================================================================ |
| 433 | |
| 434 | // ---------------------------------------------------------------------------- |
| 435 | // wxGridCellEditor |
| 436 | // ---------------------------------------------------------------------------- |
| 437 | |
| 438 | wxGridCellEditor::wxGridCellEditor() |
| 439 | { |
| 440 | m_control = NULL; |
| 441 | m_attr = NULL; |
| 442 | } |
| 443 | |
| 444 | |
| 445 | wxGridCellEditor::~wxGridCellEditor() |
| 446 | { |
| 447 | Destroy(); |
| 448 | } |
| 449 | |
| 450 | void wxGridCellEditor::Create(wxWindow* WXUNUSED(parent), |
| 451 | wxWindowID WXUNUSED(id), |
| 452 | wxEvtHandler* evtHandler) |
| 453 | { |
| 454 | if ( evtHandler ) |
| 455 | m_control->PushEventHandler(evtHandler); |
| 456 | } |
| 457 | |
| 458 | void wxGridCellEditor::PaintBackground(const wxRect& rectCell, |
| 459 | wxGridCellAttr *attr) |
| 460 | { |
| 461 | // erase the background because we might not fill the cell |
| 462 | wxClientDC dc(m_control->GetParent()); |
| 463 | wxGridWindow* gridWindow = wxDynamicCast(m_control->GetParent(), wxGridWindow); |
| 464 | if (gridWindow) |
| 465 | gridWindow->GetOwner()->PrepareDC(dc); |
| 466 | |
| 467 | dc.SetPen(*wxTRANSPARENT_PEN); |
| 468 | dc.SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID)); |
| 469 | dc.DrawRectangle(rectCell); |
| 470 | |
| 471 | // redraw the control we just painted over |
| 472 | m_control->Refresh(); |
| 473 | } |
| 474 | |
| 475 | void wxGridCellEditor::Destroy() |
| 476 | { |
| 477 | if (m_control) |
| 478 | { |
| 479 | m_control->PopEventHandler(true /* delete it*/); |
| 480 | |
| 481 | m_control->Destroy(); |
| 482 | m_control = NULL; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | void wxGridCellEditor::Show(bool show, wxGridCellAttr *attr) |
| 487 | { |
| 488 | wxASSERT_MSG(m_control, |
| 489 | wxT("The wxGridCellEditor must be Created first!")); |
| 490 | m_control->Show(show); |
| 491 | |
| 492 | if ( show ) |
| 493 | { |
| 494 | // set the colours/fonts if we have any |
| 495 | if ( attr ) |
| 496 | { |
| 497 | m_colFgOld = m_control->GetForegroundColour(); |
| 498 | m_control->SetForegroundColour(attr->GetTextColour()); |
| 499 | |
| 500 | m_colBgOld = m_control->GetBackgroundColour(); |
| 501 | m_control->SetBackgroundColour(attr->GetBackgroundColour()); |
| 502 | |
| 503 | m_fontOld = m_control->GetFont(); |
| 504 | m_control->SetFont(attr->GetFont()); |
| 505 | |
| 506 | // can't do anything more in the base class version, the other |
| 507 | // attributes may only be used by the derived classes |
| 508 | } |
| 509 | } |
| 510 | else |
| 511 | { |
| 512 | // restore the standard colours fonts |
| 513 | if ( m_colFgOld.Ok() ) |
| 514 | { |
| 515 | m_control->SetForegroundColour(m_colFgOld); |
| 516 | m_colFgOld = wxNullColour; |
| 517 | } |
| 518 | |
| 519 | if ( m_colBgOld.Ok() ) |
| 520 | { |
| 521 | m_control->SetBackgroundColour(m_colBgOld); |
| 522 | m_colBgOld = wxNullColour; |
| 523 | } |
| 524 | |
| 525 | if ( m_fontOld.Ok() ) |
| 526 | { |
| 527 | m_control->SetFont(m_fontOld); |
| 528 | m_fontOld = wxNullFont; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | void wxGridCellEditor::SetSize(const wxRect& rect) |
| 534 | { |
| 535 | wxASSERT_MSG(m_control, |
| 536 | wxT("The wxGridCellEditor must be Created first!")); |
| 537 | m_control->SetSize(rect, wxSIZE_ALLOW_MINUS_ONE); |
| 538 | } |
| 539 | |
| 540 | void wxGridCellEditor::HandleReturn(wxKeyEvent& event) |
| 541 | { |
| 542 | event.Skip(); |
| 543 | } |
| 544 | |
| 545 | bool wxGridCellEditor::IsAcceptedKey(wxKeyEvent& event) |
| 546 | { |
| 547 | // accept the simple key presses, not anything with Ctrl/Alt/Meta |
| 548 | return !(event.ControlDown() || event.AltDown()); |
| 549 | } |
| 550 | |
| 551 | void wxGridCellEditor::StartingKey(wxKeyEvent& event) |
| 552 | { |
| 553 | event.Skip(); |
| 554 | } |
| 555 | |
| 556 | void wxGridCellEditor::StartingClick() |
| 557 | { |
| 558 | } |
| 559 | |
| 560 | #if wxUSE_TEXTCTRL |
| 561 | |
| 562 | // ---------------------------------------------------------------------------- |
| 563 | // wxGridCellTextEditor |
| 564 | // ---------------------------------------------------------------------------- |
| 565 | |
| 566 | wxGridCellTextEditor::wxGridCellTextEditor() |
| 567 | { |
| 568 | m_maxChars = 0; |
| 569 | } |
| 570 | |
| 571 | void wxGridCellTextEditor::Create(wxWindow* parent, |
| 572 | wxWindowID id, |
| 573 | wxEvtHandler* evtHandler) |
| 574 | { |
| 575 | m_control = new wxTextCtrl(parent, id, wxEmptyString, |
| 576 | wxDefaultPosition, wxDefaultSize |
| 577 | #if defined(__WXMSW__) |
| 578 | , wxTE_PROCESS_TAB | wxTE_AUTO_SCROLL |
| 579 | #endif |
| 580 | ); |
| 581 | |
| 582 | // set max length allowed in the textctrl, if the parameter was set |
| 583 | if (m_maxChars != 0) |
| 584 | { |
| 585 | ((wxTextCtrl*)m_control)->SetMaxLength(m_maxChars); |
| 586 | } |
| 587 | |
| 588 | wxGridCellEditor::Create(parent, id, evtHandler); |
| 589 | } |
| 590 | |
| 591 | void wxGridCellTextEditor::PaintBackground(const wxRect& WXUNUSED(rectCell), |
| 592 | wxGridCellAttr * WXUNUSED(attr)) |
| 593 | { |
| 594 | // as we fill the entire client area, don't do anything here to minimize |
| 595 | // flicker |
| 596 | } |
| 597 | |
| 598 | void wxGridCellTextEditor::SetSize(const wxRect& rectOrig) |
| 599 | { |
| 600 | wxRect rect(rectOrig); |
| 601 | |
| 602 | // Make the edit control large enough to allow for internal |
| 603 | // margins |
| 604 | // |
| 605 | // TODO: remove this if the text ctrl sizing is improved esp. for |
| 606 | // unix |
| 607 | // |
| 608 | #if defined(__WXGTK__) |
| 609 | if (rect.x != 0) |
| 610 | { |
| 611 | rect.x += 1; |
| 612 | rect.y += 1; |
| 613 | rect.width -= 1; |
| 614 | rect.height -= 1; |
| 615 | } |
| 616 | #else // !GTK |
| 617 | int extra_x = ( rect.x > 2 )? 2 : 1; |
| 618 | |
| 619 | // MB: treat MSW separately here otherwise the caret doesn't show |
| 620 | // when the editor is in the first row. |
| 621 | #if defined(__WXMSW__) |
| 622 | int extra_y = 2; |
| 623 | #else |
| 624 | int extra_y = ( rect.y > 2 )? 2 : 1; |
| 625 | #endif // MSW |
| 626 | |
| 627 | #if defined(__WXMOTIF__) |
| 628 | extra_x *= 2; |
| 629 | extra_y *= 2; |
| 630 | #endif |
| 631 | rect.SetLeft( wxMax(0, rect.x - extra_x) ); |
| 632 | rect.SetTop( wxMax(0, rect.y - extra_y) ); |
| 633 | rect.SetRight( rect.GetRight() + 2*extra_x ); |
| 634 | rect.SetBottom( rect.GetBottom() + 2*extra_y ); |
| 635 | #endif // GTK/!GTK |
| 636 | |
| 637 | wxGridCellEditor::SetSize(rect); |
| 638 | } |
| 639 | |
| 640 | void wxGridCellTextEditor::BeginEdit(int row, int col, wxGrid* grid) |
| 641 | { |
| 642 | wxASSERT_MSG(m_control, |
| 643 | wxT("The wxGridCellEditor must be Created first!")); |
| 644 | |
| 645 | m_startValue = grid->GetTable()->GetValue(row, col); |
| 646 | |
| 647 | DoBeginEdit(m_startValue); |
| 648 | } |
| 649 | |
| 650 | void wxGridCellTextEditor::DoBeginEdit(const wxString& startValue) |
| 651 | { |
| 652 | Text()->SetValue(startValue); |
| 653 | Text()->SetInsertionPointEnd(); |
| 654 | Text()->SetSelection(-1,-1); |
| 655 | Text()->SetFocus(); |
| 656 | } |
| 657 | |
| 658 | bool wxGridCellTextEditor::EndEdit(int row, int col, |
| 659 | wxGrid* grid) |
| 660 | { |
| 661 | wxASSERT_MSG(m_control, |
| 662 | wxT("The wxGridCellEditor must be Created first!")); |
| 663 | |
| 664 | bool changed = false; |
| 665 | wxString value = Text()->GetValue(); |
| 666 | if (value != m_startValue) |
| 667 | changed = true; |
| 668 | |
| 669 | if (changed) |
| 670 | grid->GetTable()->SetValue(row, col, value); |
| 671 | |
| 672 | m_startValue = wxEmptyString; |
| 673 | // No point in setting the text of the hidden control |
| 674 | //Text()->SetValue(m_startValue); |
| 675 | |
| 676 | return changed; |
| 677 | } |
| 678 | |
| 679 | |
| 680 | void wxGridCellTextEditor::Reset() |
| 681 | { |
| 682 | wxASSERT_MSG(m_control, |
| 683 | wxT("The wxGridCellEditor must be Created first!")); |
| 684 | |
| 685 | DoReset(m_startValue); |
| 686 | } |
| 687 | |
| 688 | void wxGridCellTextEditor::DoReset(const wxString& startValue) |
| 689 | { |
| 690 | Text()->SetValue(startValue); |
| 691 | Text()->SetInsertionPointEnd(); |
| 692 | } |
| 693 | |
| 694 | bool wxGridCellTextEditor::IsAcceptedKey(wxKeyEvent& event) |
| 695 | { |
| 696 | if ( wxGridCellEditor::IsAcceptedKey(event) ) |
| 697 | { |
| 698 | int keycode = event.GetKeyCode(); |
| 699 | switch ( keycode ) |
| 700 | { |
| 701 | case WXK_NUMPAD0: |
| 702 | case WXK_NUMPAD1: |
| 703 | case WXK_NUMPAD2: |
| 704 | case WXK_NUMPAD3: |
| 705 | case WXK_NUMPAD4: |
| 706 | case WXK_NUMPAD5: |
| 707 | case WXK_NUMPAD6: |
| 708 | case WXK_NUMPAD7: |
| 709 | case WXK_NUMPAD8: |
| 710 | case WXK_NUMPAD9: |
| 711 | case WXK_MULTIPLY: |
| 712 | case WXK_NUMPAD_MULTIPLY: |
| 713 | case WXK_ADD: |
| 714 | case WXK_NUMPAD_ADD: |
| 715 | case WXK_SUBTRACT: |
| 716 | case WXK_NUMPAD_SUBTRACT: |
| 717 | case WXK_DECIMAL: |
| 718 | case WXK_NUMPAD_DECIMAL: |
| 719 | case WXK_DIVIDE: |
| 720 | case WXK_NUMPAD_DIVIDE: |
| 721 | return true; |
| 722 | |
| 723 | default: |
| 724 | // accept 8 bit chars too if isprint() agrees |
| 725 | if ( (keycode < 255) && (wxIsprint(keycode)) ) |
| 726 | return true; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | void wxGridCellTextEditor::StartingKey(wxKeyEvent& event) |
| 734 | { |
| 735 | if ( !Text()->EmulateKeyPress(event) ) |
| 736 | { |
| 737 | event.Skip(); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | void wxGridCellTextEditor::HandleReturn( wxKeyEvent& |
| 742 | WXUNUSED_GTK(WXUNUSED_MOTIF(event)) ) |
| 743 | { |
| 744 | #if defined(__WXMOTIF__) || defined(__WXGTK__) |
| 745 | // wxMotif needs a little extra help... |
| 746 | size_t pos = (size_t)( Text()->GetInsertionPoint() ); |
| 747 | wxString s( Text()->GetValue() ); |
| 748 | s = s.Left(pos) + wxT("\n") + s.Mid(pos); |
| 749 | Text()->SetValue(s); |
| 750 | Text()->SetInsertionPoint( pos ); |
| 751 | #else |
| 752 | // the other ports can handle a Return key press |
| 753 | // |
| 754 | event.Skip(); |
| 755 | #endif |
| 756 | } |
| 757 | |
| 758 | void wxGridCellTextEditor::SetParameters(const wxString& params) |
| 759 | { |
| 760 | if ( !params ) |
| 761 | { |
| 762 | // reset to default |
| 763 | m_maxChars = 0; |
| 764 | } |
| 765 | else |
| 766 | { |
| 767 | long tmp; |
| 768 | if ( !params.ToLong(&tmp) ) |
| 769 | { |
| 770 | wxLogDebug(_T("Invalid wxGridCellTextEditor parameter string '%s' ignored"), params.c_str()); |
| 771 | } |
| 772 | else |
| 773 | { |
| 774 | m_maxChars = (size_t)tmp; |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | // return the value in the text control |
| 780 | wxString wxGridCellTextEditor::GetValue() const |
| 781 | { |
| 782 | return Text()->GetValue(); |
| 783 | } |
| 784 | |
| 785 | // ---------------------------------------------------------------------------- |
| 786 | // wxGridCellNumberEditor |
| 787 | // ---------------------------------------------------------------------------- |
| 788 | |
| 789 | wxGridCellNumberEditor::wxGridCellNumberEditor(int min, int max) |
| 790 | { |
| 791 | m_min = min; |
| 792 | m_max = max; |
| 793 | } |
| 794 | |
| 795 | void wxGridCellNumberEditor::Create(wxWindow* parent, |
| 796 | wxWindowID id, |
| 797 | wxEvtHandler* evtHandler) |
| 798 | { |
| 799 | if ( HasRange() ) |
| 800 | { |
| 801 | // create a spin ctrl |
| 802 | m_control = new wxSpinCtrl(parent, wxID_ANY, wxEmptyString, |
| 803 | wxDefaultPosition, wxDefaultSize, |
| 804 | wxSP_ARROW_KEYS, |
| 805 | m_min, m_max); |
| 806 | |
| 807 | wxGridCellEditor::Create(parent, id, evtHandler); |
| 808 | } |
| 809 | else |
| 810 | { |
| 811 | // just a text control |
| 812 | wxGridCellTextEditor::Create(parent, id, evtHandler); |
| 813 | |
| 814 | #if wxUSE_VALIDATORS |
| 815 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); |
| 816 | #endif // wxUSE_VALIDATORS |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | void wxGridCellNumberEditor::BeginEdit(int row, int col, wxGrid* grid) |
| 821 | { |
| 822 | // first get the value |
| 823 | wxGridTableBase *table = grid->GetTable(); |
| 824 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) |
| 825 | { |
| 826 | m_valueOld = table->GetValueAsLong(row, col); |
| 827 | } |
| 828 | else |
| 829 | { |
| 830 | m_valueOld = 0; |
| 831 | wxString sValue = table->GetValue(row, col); |
| 832 | if (! sValue.ToLong(&m_valueOld) && ! sValue.IsEmpty()) |
| 833 | { |
| 834 | wxFAIL_MSG( _T("this cell doesn't have numeric value") ); |
| 835 | return; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | if ( HasRange() ) |
| 840 | { |
| 841 | Spin()->SetValue((int)m_valueOld); |
| 842 | Spin()->SetFocus(); |
| 843 | } |
| 844 | else |
| 845 | { |
| 846 | DoBeginEdit(GetString()); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | bool wxGridCellNumberEditor::EndEdit(int row, int col, |
| 851 | wxGrid* grid) |
| 852 | { |
| 853 | bool changed; |
| 854 | long value = 0; |
| 855 | wxString text; |
| 856 | |
| 857 | if ( HasRange() ) |
| 858 | { |
| 859 | value = Spin()->GetValue(); |
| 860 | changed = value != m_valueOld; |
| 861 | if (changed) |
| 862 | text = wxString::Format(wxT("%ld"), value); |
| 863 | } |
| 864 | else |
| 865 | { |
| 866 | text = Text()->GetValue(); |
| 867 | changed = (text.IsEmpty() || text.ToLong(&value)) && (value != m_valueOld); |
| 868 | } |
| 869 | |
| 870 | if ( changed ) |
| 871 | { |
| 872 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_NUMBER)) |
| 873 | grid->GetTable()->SetValueAsLong(row, col, value); |
| 874 | else |
| 875 | grid->GetTable()->SetValue(row, col, text); |
| 876 | } |
| 877 | |
| 878 | return changed; |
| 879 | } |
| 880 | |
| 881 | void wxGridCellNumberEditor::Reset() |
| 882 | { |
| 883 | if ( HasRange() ) |
| 884 | { |
| 885 | Spin()->SetValue((int)m_valueOld); |
| 886 | } |
| 887 | else |
| 888 | { |
| 889 | DoReset(GetString()); |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | bool wxGridCellNumberEditor::IsAcceptedKey(wxKeyEvent& event) |
| 894 | { |
| 895 | if ( wxGridCellEditor::IsAcceptedKey(event) ) |
| 896 | { |
| 897 | int keycode = event.GetKeyCode(); |
| 898 | switch ( keycode ) |
| 899 | { |
| 900 | case WXK_NUMPAD0: |
| 901 | case WXK_NUMPAD1: |
| 902 | case WXK_NUMPAD2: |
| 903 | case WXK_NUMPAD3: |
| 904 | case WXK_NUMPAD4: |
| 905 | case WXK_NUMPAD5: |
| 906 | case WXK_NUMPAD6: |
| 907 | case WXK_NUMPAD7: |
| 908 | case WXK_NUMPAD8: |
| 909 | case WXK_NUMPAD9: |
| 910 | case WXK_ADD: |
| 911 | case WXK_NUMPAD_ADD: |
| 912 | case WXK_SUBTRACT: |
| 913 | case WXK_NUMPAD_SUBTRACT: |
| 914 | case WXK_UP: |
| 915 | case WXK_DOWN: |
| 916 | return true; |
| 917 | |
| 918 | default: |
| 919 | if ( (keycode < 128) && wxIsdigit(keycode) ) |
| 920 | return true; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | void wxGridCellNumberEditor::StartingKey(wxKeyEvent& event) |
| 928 | { |
| 929 | if ( !HasRange() ) |
| 930 | { |
| 931 | int keycode = event.GetKeyCode(); |
| 932 | if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-' |
| 933 | || keycode == WXK_NUMPAD0 |
| 934 | || keycode == WXK_NUMPAD1 |
| 935 | || keycode == WXK_NUMPAD2 |
| 936 | || keycode == WXK_NUMPAD3 |
| 937 | || keycode == WXK_NUMPAD4 |
| 938 | || keycode == WXK_NUMPAD5 |
| 939 | || keycode == WXK_NUMPAD6 |
| 940 | || keycode == WXK_NUMPAD7 |
| 941 | || keycode == WXK_NUMPAD8 |
| 942 | || keycode == WXK_NUMPAD9 |
| 943 | || keycode == WXK_ADD |
| 944 | || keycode == WXK_NUMPAD_ADD |
| 945 | || keycode == WXK_SUBTRACT |
| 946 | || keycode == WXK_NUMPAD_SUBTRACT) |
| 947 | { |
| 948 | wxGridCellTextEditor::StartingKey(event); |
| 949 | |
| 950 | // skip Skip() below |
| 951 | return; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | event.Skip(); |
| 956 | } |
| 957 | |
| 958 | void wxGridCellNumberEditor::SetParameters(const wxString& params) |
| 959 | { |
| 960 | if ( !params ) |
| 961 | { |
| 962 | // reset to default |
| 963 | m_min = |
| 964 | m_max = -1; |
| 965 | } |
| 966 | else |
| 967 | { |
| 968 | long tmp; |
| 969 | if ( params.BeforeFirst(_T(',')).ToLong(&tmp) ) |
| 970 | { |
| 971 | m_min = (int)tmp; |
| 972 | |
| 973 | if ( params.AfterFirst(_T(',')).ToLong(&tmp) ) |
| 974 | { |
| 975 | m_max = (int)tmp; |
| 976 | |
| 977 | // skip the error message below |
| 978 | return; |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | wxLogDebug(_T("Invalid wxGridCellNumberEditor parameter string '%s' ignored"), params.c_str()); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | // return the value in the spin control if it is there (the text control otherwise) |
| 987 | wxString wxGridCellNumberEditor::GetValue() const |
| 988 | { |
| 989 | wxString s; |
| 990 | |
| 991 | if( HasRange() ) |
| 992 | { |
| 993 | long value = Spin()->GetValue(); |
| 994 | s.Printf(wxT("%ld"), value); |
| 995 | } |
| 996 | else |
| 997 | { |
| 998 | s = Text()->GetValue(); |
| 999 | } |
| 1000 | return s; |
| 1001 | } |
| 1002 | |
| 1003 | // ---------------------------------------------------------------------------- |
| 1004 | // wxGridCellFloatEditor |
| 1005 | // ---------------------------------------------------------------------------- |
| 1006 | |
| 1007 | wxGridCellFloatEditor::wxGridCellFloatEditor(int width, int precision) |
| 1008 | { |
| 1009 | m_width = width; |
| 1010 | m_precision = precision; |
| 1011 | } |
| 1012 | |
| 1013 | void wxGridCellFloatEditor::Create(wxWindow* parent, |
| 1014 | wxWindowID id, |
| 1015 | wxEvtHandler* evtHandler) |
| 1016 | { |
| 1017 | wxGridCellTextEditor::Create(parent, id, evtHandler); |
| 1018 | |
| 1019 | #if wxUSE_VALIDATORS |
| 1020 | Text()->SetValidator(wxTextValidator(wxFILTER_NUMERIC)); |
| 1021 | #endif // wxUSE_VALIDATORS |
| 1022 | } |
| 1023 | |
| 1024 | void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid) |
| 1025 | { |
| 1026 | // first get the value |
| 1027 | wxGridTableBase *table = grid->GetTable(); |
| 1028 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) |
| 1029 | { |
| 1030 | m_valueOld = table->GetValueAsDouble(row, col); |
| 1031 | } |
| 1032 | else |
| 1033 | { |
| 1034 | m_valueOld = 0.0; |
| 1035 | wxString sValue = table->GetValue(row, col); |
| 1036 | if (! sValue.ToDouble(&m_valueOld) && ! sValue.IsEmpty()) |
| 1037 | { |
| 1038 | wxFAIL_MSG( _T("this cell doesn't have float value") ); |
| 1039 | return; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | DoBeginEdit(GetString()); |
| 1044 | } |
| 1045 | |
| 1046 | bool wxGridCellFloatEditor::EndEdit(int row, int col, |
| 1047 | wxGrid* grid) |
| 1048 | { |
| 1049 | double value = 0.0; |
| 1050 | wxString text(Text()->GetValue()); |
| 1051 | |
| 1052 | if ( (text.IsEmpty() || text.ToDouble(&value)) && (value != m_valueOld) ) |
| 1053 | { |
| 1054 | if (grid->GetTable()->CanSetValueAs(row, col, wxGRID_VALUE_FLOAT)) |
| 1055 | grid->GetTable()->SetValueAsDouble(row, col, value); |
| 1056 | else |
| 1057 | grid->GetTable()->SetValue(row, col, text); |
| 1058 | |
| 1059 | return true; |
| 1060 | } |
| 1061 | return false; |
| 1062 | } |
| 1063 | |
| 1064 | void wxGridCellFloatEditor::Reset() |
| 1065 | { |
| 1066 | DoReset(GetString()); |
| 1067 | } |
| 1068 | |
| 1069 | void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event) |
| 1070 | { |
| 1071 | int keycode = event.GetKeyCode(); |
| 1072 | char tmpbuf[2]; |
| 1073 | tmpbuf[0] = (char) keycode; |
| 1074 | tmpbuf[1] = '\0'; |
| 1075 | bool is_decimal_point = ( wxString(tmpbuf, *wxConvCurrent) == |
| 1076 | wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER) ); |
| 1077 | if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-' |
| 1078 | || is_decimal_point |
| 1079 | || keycode == WXK_NUMPAD0 |
| 1080 | || keycode == WXK_NUMPAD1 |
| 1081 | || keycode == WXK_NUMPAD2 |
| 1082 | || keycode == WXK_NUMPAD3 |
| 1083 | || keycode == WXK_NUMPAD4 |
| 1084 | || keycode == WXK_NUMPAD5 |
| 1085 | || keycode == WXK_NUMPAD6 |
| 1086 | || keycode == WXK_NUMPAD7 |
| 1087 | || keycode == WXK_NUMPAD8 |
| 1088 | || keycode == WXK_NUMPAD9 |
| 1089 | || keycode == WXK_ADD |
| 1090 | || keycode == WXK_NUMPAD_ADD |
| 1091 | || keycode == WXK_SUBTRACT |
| 1092 | || keycode == WXK_NUMPAD_SUBTRACT) |
| 1093 | { |
| 1094 | wxGridCellTextEditor::StartingKey(event); |
| 1095 | |
| 1096 | // skip Skip() below |
| 1097 | return; |
| 1098 | } |
| 1099 | |
| 1100 | event.Skip(); |
| 1101 | } |
| 1102 | |
| 1103 | void wxGridCellFloatEditor::SetParameters(const wxString& params) |
| 1104 | { |
| 1105 | if ( !params ) |
| 1106 | { |
| 1107 | // reset to default |
| 1108 | m_width = |
| 1109 | m_precision = -1; |
| 1110 | } |
| 1111 | else |
| 1112 | { |
| 1113 | long tmp; |
| 1114 | if ( params.BeforeFirst(_T(',')).ToLong(&tmp) ) |
| 1115 | { |
| 1116 | m_width = (int)tmp; |
| 1117 | |
| 1118 | if ( params.AfterFirst(_T(',')).ToLong(&tmp) ) |
| 1119 | { |
| 1120 | m_precision = (int)tmp; |
| 1121 | |
| 1122 | // skip the error message below |
| 1123 | return; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | wxLogDebug(_T("Invalid wxGridCellFloatEditor parameter string '%s' ignored"), params.c_str()); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | wxString wxGridCellFloatEditor::GetString() const |
| 1132 | { |
| 1133 | wxString fmt; |
| 1134 | if ( m_width == -1 ) |
| 1135 | { |
| 1136 | // default width/precision |
| 1137 | fmt = _T("%f"); |
| 1138 | } |
| 1139 | else if ( m_precision == -1 ) |
| 1140 | { |
| 1141 | // default precision |
| 1142 | fmt.Printf(_T("%%%d.f"), m_width); |
| 1143 | } |
| 1144 | else |
| 1145 | { |
| 1146 | fmt.Printf(_T("%%%d.%df"), m_width, m_precision); |
| 1147 | } |
| 1148 | |
| 1149 | return wxString::Format(fmt, m_valueOld); |
| 1150 | } |
| 1151 | |
| 1152 | bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event) |
| 1153 | { |
| 1154 | if ( wxGridCellEditor::IsAcceptedKey(event) ) |
| 1155 | { |
| 1156 | int keycode = event.GetKeyCode(); |
| 1157 | switch ( keycode ) |
| 1158 | { |
| 1159 | case WXK_NUMPAD0: |
| 1160 | case WXK_NUMPAD1: |
| 1161 | case WXK_NUMPAD2: |
| 1162 | case WXK_NUMPAD3: |
| 1163 | case WXK_NUMPAD4: |
| 1164 | case WXK_NUMPAD5: |
| 1165 | case WXK_NUMPAD6: |
| 1166 | case WXK_NUMPAD7: |
| 1167 | case WXK_NUMPAD8: |
| 1168 | case WXK_NUMPAD9: |
| 1169 | case WXK_ADD: |
| 1170 | case WXK_NUMPAD_ADD: |
| 1171 | case WXK_SUBTRACT: |
| 1172 | case WXK_NUMPAD_SUBTRACT: |
| 1173 | case WXK_DECIMAL: |
| 1174 | case WXK_NUMPAD_DECIMAL: |
| 1175 | return true; |
| 1176 | |
| 1177 | default: |
| 1178 | { |
| 1179 | // additionally accept 'e' as in '1e+6', also '-', '+', and '.' |
| 1180 | char tmpbuf[2]; |
| 1181 | tmpbuf[0] = (char) keycode; |
| 1182 | tmpbuf[1] = '\0'; |
| 1183 | bool is_decimal_point = |
| 1184 | ( wxString(tmpbuf, *wxConvCurrent) == |
| 1185 | wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, |
| 1186 | wxLOCALE_CAT_NUMBER) ); |
| 1187 | if ( (keycode < 128) && |
| 1188 | (wxIsdigit(keycode) || tolower(keycode) == 'e' || |
| 1189 | is_decimal_point || keycode == '+' || keycode == '-') ) |
| 1190 | return true; |
| 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
| 1198 | #endif // wxUSE_TEXTCTRL |
| 1199 | |
| 1200 | #if wxUSE_CHECKBOX |
| 1201 | |
| 1202 | // ---------------------------------------------------------------------------- |
| 1203 | // wxGridCellBoolEditor |
| 1204 | // ---------------------------------------------------------------------------- |
| 1205 | |
| 1206 | void wxGridCellBoolEditor::Create(wxWindow* parent, |
| 1207 | wxWindowID id, |
| 1208 | wxEvtHandler* evtHandler) |
| 1209 | { |
| 1210 | m_control = new wxCheckBox(parent, id, wxEmptyString, |
| 1211 | wxDefaultPosition, wxDefaultSize, |
| 1212 | wxNO_BORDER); |
| 1213 | |
| 1214 | wxGridCellEditor::Create(parent, id, evtHandler); |
| 1215 | } |
| 1216 | |
| 1217 | void wxGridCellBoolEditor::SetSize(const wxRect& r) |
| 1218 | { |
| 1219 | bool resize = false; |
| 1220 | wxSize size = m_control->GetSize(); |
| 1221 | wxCoord minSize = wxMin(r.width, r.height); |
| 1222 | |
| 1223 | // check if the checkbox is not too big/small for this cell |
| 1224 | wxSize sizeBest = m_control->GetBestSize(); |
| 1225 | if ( !(size == sizeBest) ) |
| 1226 | { |
| 1227 | // reset to default size if it had been made smaller |
| 1228 | size = sizeBest; |
| 1229 | |
| 1230 | resize = true; |
| 1231 | } |
| 1232 | |
| 1233 | if ( size.x >= minSize || size.y >= minSize ) |
| 1234 | { |
| 1235 | // leave 1 pixel margin |
| 1236 | size.x = size.y = minSize - 2; |
| 1237 | |
| 1238 | resize = true; |
| 1239 | } |
| 1240 | |
| 1241 | if ( resize ) |
| 1242 | { |
| 1243 | m_control->SetSize(size); |
| 1244 | } |
| 1245 | |
| 1246 | // position it in the centre of the rectangle (TODO: support alignment?) |
| 1247 | |
| 1248 | #if defined(__WXGTK__) || defined (__WXMOTIF__) |
| 1249 | // the checkbox without label still has some space to the right in wxGTK, |
| 1250 | // so shift it to the right |
| 1251 | size.x -= 8; |
| 1252 | #elif defined(__WXMSW__) |
| 1253 | // here too, but in other way |
| 1254 | size.x += 1; |
| 1255 | size.y -= 2; |
| 1256 | #endif |
| 1257 | |
| 1258 | int hAlign = wxALIGN_CENTRE; |
| 1259 | int vAlign = wxALIGN_CENTRE; |
| 1260 | if (GetCellAttr()) |
| 1261 | GetCellAttr()->GetAlignment(& hAlign, & vAlign); |
| 1262 | |
| 1263 | int x = 0, y = 0; |
| 1264 | if (hAlign == wxALIGN_LEFT) |
| 1265 | { |
| 1266 | x = r.x + 2; |
| 1267 | #ifdef __WXMSW__ |
| 1268 | x += 2; |
| 1269 | #endif |
| 1270 | y = r.y + r.height/2 - size.y/2; |
| 1271 | } |
| 1272 | else if (hAlign == wxALIGN_RIGHT) |
| 1273 | { |
| 1274 | x = r.x + r.width - size.x - 2; |
| 1275 | y = r.y + r.height/2 - size.y/2; |
| 1276 | } |
| 1277 | else if (hAlign == wxALIGN_CENTRE) |
| 1278 | { |
| 1279 | x = r.x + r.width/2 - size.x/2; |
| 1280 | y = r.y + r.height/2 - size.y/2; |
| 1281 | } |
| 1282 | |
| 1283 | m_control->Move(x, y); |
| 1284 | } |
| 1285 | |
| 1286 | void wxGridCellBoolEditor::Show(bool show, wxGridCellAttr *attr) |
| 1287 | { |
| 1288 | m_control->Show(show); |
| 1289 | |
| 1290 | if ( show ) |
| 1291 | { |
| 1292 | wxColour colBg = attr ? attr->GetBackgroundColour() : *wxLIGHT_GREY; |
| 1293 | CBox()->SetBackgroundColour(colBg); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid) |
| 1298 | { |
| 1299 | wxASSERT_MSG(m_control, |
| 1300 | wxT("The wxGridCellEditor must be Created first!")); |
| 1301 | |
| 1302 | if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) |
| 1303 | m_startValue = grid->GetTable()->GetValueAsBool(row, col); |
| 1304 | else |
| 1305 | { |
| 1306 | wxString cellval( grid->GetTable()->GetValue(row, col) ); |
| 1307 | m_startValue = !( !cellval || (cellval == wxT("0")) ); |
| 1308 | } |
| 1309 | CBox()->SetValue(m_startValue); |
| 1310 | CBox()->SetFocus(); |
| 1311 | } |
| 1312 | |
| 1313 | bool wxGridCellBoolEditor::EndEdit(int row, int col, |
| 1314 | wxGrid* grid) |
| 1315 | { |
| 1316 | wxASSERT_MSG(m_control, |
| 1317 | wxT("The wxGridCellEditor must be Created first!")); |
| 1318 | |
| 1319 | bool changed = false; |
| 1320 | bool value = CBox()->GetValue(); |
| 1321 | if ( value != m_startValue ) |
| 1322 | changed = true; |
| 1323 | |
| 1324 | if ( changed ) |
| 1325 | { |
| 1326 | if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) |
| 1327 | grid->GetTable()->SetValueAsBool(row, col, value); |
| 1328 | else |
| 1329 | grid->GetTable()->SetValue(row, col, value ? _T("1") : wxEmptyString); |
| 1330 | } |
| 1331 | |
| 1332 | return changed; |
| 1333 | } |
| 1334 | |
| 1335 | void wxGridCellBoolEditor::Reset() |
| 1336 | { |
| 1337 | wxASSERT_MSG(m_control, |
| 1338 | wxT("The wxGridCellEditor must be Created first!")); |
| 1339 | |
| 1340 | CBox()->SetValue(m_startValue); |
| 1341 | } |
| 1342 | |
| 1343 | void wxGridCellBoolEditor::StartingClick() |
| 1344 | { |
| 1345 | CBox()->SetValue(!CBox()->GetValue()); |
| 1346 | } |
| 1347 | |
| 1348 | bool wxGridCellBoolEditor::IsAcceptedKey(wxKeyEvent& event) |
| 1349 | { |
| 1350 | if ( wxGridCellEditor::IsAcceptedKey(event) ) |
| 1351 | { |
| 1352 | int keycode = event.GetKeyCode(); |
| 1353 | switch ( keycode ) |
| 1354 | { |
| 1355 | case WXK_MULTIPLY: |
| 1356 | case WXK_NUMPAD_MULTIPLY: |
| 1357 | case WXK_ADD: |
| 1358 | case WXK_NUMPAD_ADD: |
| 1359 | case WXK_SUBTRACT: |
| 1360 | case WXK_NUMPAD_SUBTRACT: |
| 1361 | case WXK_SPACE: |
| 1362 | case '+': |
| 1363 | case '-': |
| 1364 | return true; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | return false; |
| 1369 | } |
| 1370 | |
| 1371 | // return the value as "1" for true and the empty string for false |
| 1372 | wxString wxGridCellBoolEditor::GetValue() const |
| 1373 | { |
| 1374 | bool bSet = CBox()->GetValue(); |
| 1375 | return bSet ? _T("1") : wxEmptyString; |
| 1376 | } |
| 1377 | |
| 1378 | #endif // wxUSE_CHECKBOX |
| 1379 | |
| 1380 | #if wxUSE_COMBOBOX |
| 1381 | |
| 1382 | // ---------------------------------------------------------------------------- |
| 1383 | // wxGridCellChoiceEditor |
| 1384 | // ---------------------------------------------------------------------------- |
| 1385 | |
| 1386 | wxGridCellChoiceEditor::wxGridCellChoiceEditor(const wxArrayString& choices, |
| 1387 | bool allowOthers) |
| 1388 | : m_choices(choices), |
| 1389 | m_allowOthers(allowOthers) { } |
| 1390 | |
| 1391 | wxGridCellChoiceEditor::wxGridCellChoiceEditor(size_t count, |
| 1392 | const wxString choices[], |
| 1393 | bool allowOthers) |
| 1394 | : m_allowOthers(allowOthers) |
| 1395 | { |
| 1396 | if ( count ) |
| 1397 | { |
| 1398 | m_choices.Alloc(count); |
| 1399 | for ( size_t n = 0; n < count; n++ ) |
| 1400 | { |
| 1401 | m_choices.Add(choices[n]); |
| 1402 | } |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | wxGridCellEditor *wxGridCellChoiceEditor::Clone() const |
| 1407 | { |
| 1408 | wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor; |
| 1409 | editor->m_allowOthers = m_allowOthers; |
| 1410 | editor->m_choices = m_choices; |
| 1411 | |
| 1412 | return editor; |
| 1413 | } |
| 1414 | |
| 1415 | void wxGridCellChoiceEditor::Create(wxWindow* parent, |
| 1416 | wxWindowID id, |
| 1417 | wxEvtHandler* evtHandler) |
| 1418 | { |
| 1419 | m_control = new wxComboBox(parent, id, wxEmptyString, |
| 1420 | wxDefaultPosition, wxDefaultSize, |
| 1421 | m_choices, |
| 1422 | m_allowOthers ? 0 : wxCB_READONLY); |
| 1423 | |
| 1424 | wxGridCellEditor::Create(parent, id, evtHandler); |
| 1425 | } |
| 1426 | |
| 1427 | void wxGridCellChoiceEditor::PaintBackground(const wxRect& rectCell, |
| 1428 | wxGridCellAttr * attr) |
| 1429 | { |
| 1430 | // as we fill the entire client area, don't do anything here to minimize |
| 1431 | // flicker |
| 1432 | |
| 1433 | // TODO: It doesn't actually fill the client area since the height of a |
| 1434 | // combo always defaults to the standard... Until someone has time to |
| 1435 | // figure out the right rectangle to paint, just do it the normal way... |
| 1436 | wxGridCellEditor::PaintBackground(rectCell, attr); |
| 1437 | } |
| 1438 | |
| 1439 | void wxGridCellChoiceEditor::BeginEdit(int row, int col, wxGrid* grid) |
| 1440 | { |
| 1441 | wxASSERT_MSG(m_control, |
| 1442 | wxT("The wxGridCellEditor must be Created first!")); |
| 1443 | |
| 1444 | m_startValue = grid->GetTable()->GetValue(row, col); |
| 1445 | |
| 1446 | if (m_allowOthers) |
| 1447 | Combo()->SetValue(m_startValue); |
| 1448 | else |
| 1449 | { |
| 1450 | // find the right position, or default to the first if not found |
| 1451 | int pos = Combo()->FindString(m_startValue); |
| 1452 | if (pos == -1) |
| 1453 | pos = 0; |
| 1454 | Combo()->SetSelection(pos); |
| 1455 | } |
| 1456 | Combo()->SetInsertionPointEnd(); |
| 1457 | Combo()->SetFocus(); |
| 1458 | } |
| 1459 | |
| 1460 | bool wxGridCellChoiceEditor::EndEdit(int row, int col, |
| 1461 | wxGrid* grid) |
| 1462 | { |
| 1463 | wxString value = Combo()->GetValue(); |
| 1464 | if ( value == m_startValue ) |
| 1465 | return false; |
| 1466 | |
| 1467 | grid->GetTable()->SetValue(row, col, value); |
| 1468 | |
| 1469 | return true; |
| 1470 | } |
| 1471 | |
| 1472 | void wxGridCellChoiceEditor::Reset() |
| 1473 | { |
| 1474 | Combo()->SetValue(m_startValue); |
| 1475 | Combo()->SetInsertionPointEnd(); |
| 1476 | } |
| 1477 | |
| 1478 | void wxGridCellChoiceEditor::SetParameters(const wxString& params) |
| 1479 | { |
| 1480 | if ( !params ) |
| 1481 | { |
| 1482 | // what can we do? |
| 1483 | return; |
| 1484 | } |
| 1485 | |
| 1486 | m_choices.Empty(); |
| 1487 | |
| 1488 | wxStringTokenizer tk(params, _T(',')); |
| 1489 | while ( tk.HasMoreTokens() ) |
| 1490 | { |
| 1491 | m_choices.Add(tk.GetNextToken()); |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | // return the value in the text control |
| 1496 | wxString wxGridCellChoiceEditor::GetValue() const |
| 1497 | { |
| 1498 | return Combo()->GetValue(); |
| 1499 | } |
| 1500 | |
| 1501 | #endif // wxUSE_COMBOBOX |
| 1502 | |
| 1503 | // ---------------------------------------------------------------------------- |
| 1504 | // wxGridCellEditorEvtHandler |
| 1505 | // ---------------------------------------------------------------------------- |
| 1506 | |
| 1507 | void wxGridCellEditorEvtHandler::OnKeyDown(wxKeyEvent& event) |
| 1508 | { |
| 1509 | switch ( event.GetKeyCode() ) |
| 1510 | { |
| 1511 | case WXK_ESCAPE: |
| 1512 | m_editor->Reset(); |
| 1513 | m_grid->DisableCellEditControl(); |
| 1514 | break; |
| 1515 | |
| 1516 | case WXK_TAB: |
| 1517 | m_grid->GetEventHandler()->ProcessEvent( event ); |
| 1518 | break; |
| 1519 | |
| 1520 | case WXK_RETURN: |
| 1521 | case WXK_NUMPAD_ENTER: |
| 1522 | if (!m_grid->GetEventHandler()->ProcessEvent(event)) |
| 1523 | m_editor->HandleReturn(event); |
| 1524 | break; |
| 1525 | |
| 1526 | |
| 1527 | default: |
| 1528 | event.Skip(); |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | void wxGridCellEditorEvtHandler::OnChar(wxKeyEvent& event) |
| 1533 | { |
| 1534 | switch ( event.GetKeyCode() ) |
| 1535 | { |
| 1536 | case WXK_ESCAPE: |
| 1537 | case WXK_TAB: |
| 1538 | case WXK_RETURN: |
| 1539 | case WXK_NUMPAD_ENTER: |
| 1540 | break; |
| 1541 | |
| 1542 | default: |
| 1543 | event.Skip(); |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | // ---------------------------------------------------------------------------- |
| 1548 | // wxGridCellWorker is an (almost) empty common base class for |
| 1549 | // wxGridCellRenderer and wxGridCellEditor managing ref counting |
| 1550 | // ---------------------------------------------------------------------------- |
| 1551 | |
| 1552 | void wxGridCellWorker::SetParameters(const wxString& WXUNUSED(params)) |
| 1553 | { |
| 1554 | // nothing to do |
| 1555 | } |
| 1556 | |
| 1557 | wxGridCellWorker::~wxGridCellWorker() |
| 1558 | { |
| 1559 | } |
| 1560 | |
| 1561 | // ============================================================================ |
| 1562 | // renderer classes |
| 1563 | // ============================================================================ |
| 1564 | |
| 1565 | // ---------------------------------------------------------------------------- |
| 1566 | // wxGridCellRenderer |
| 1567 | // ---------------------------------------------------------------------------- |
| 1568 | |
| 1569 | void wxGridCellRenderer::Draw(wxGrid& grid, |
| 1570 | wxGridCellAttr& attr, |
| 1571 | wxDC& dc, |
| 1572 | const wxRect& rect, |
| 1573 | int WXUNUSED(row), int WXUNUSED(col), |
| 1574 | bool isSelected) |
| 1575 | { |
| 1576 | dc.SetBackgroundMode( wxSOLID ); |
| 1577 | |
| 1578 | // grey out fields if the grid is disabled |
| 1579 | if( grid.IsEnabled() ) |
| 1580 | { |
| 1581 | if ( isSelected ) |
| 1582 | { |
| 1583 | dc.SetBrush( wxBrush(grid.GetSelectionBackground(), wxSOLID) ); |
| 1584 | } |
| 1585 | else |
| 1586 | { |
| 1587 | dc.SetBrush( wxBrush(attr.GetBackgroundColour(), wxSOLID) ); |
| 1588 | } |
| 1589 | } |
| 1590 | else |
| 1591 | { |
| 1592 | dc.SetBrush(wxBrush(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE), wxSOLID)); |
| 1593 | } |
| 1594 | |
| 1595 | dc.SetPen( *wxTRANSPARENT_PEN ); |
| 1596 | dc.DrawRectangle(rect); |
| 1597 | } |
| 1598 | |
| 1599 | // ---------------------------------------------------------------------------- |
| 1600 | // wxGridCellStringRenderer |
| 1601 | // ---------------------------------------------------------------------------- |
| 1602 | |
| 1603 | void wxGridCellStringRenderer::SetTextColoursAndFont(wxGrid& grid, |
| 1604 | wxGridCellAttr& attr, |
| 1605 | wxDC& dc, |
| 1606 | bool isSelected) |
| 1607 | { |
| 1608 | dc.SetBackgroundMode( wxTRANSPARENT ); |
| 1609 | |
| 1610 | // TODO some special colours for attr.IsReadOnly() case? |
| 1611 | |
| 1612 | // different coloured text when the grid is disabled |
| 1613 | if( grid.IsEnabled() ) |
| 1614 | { |
| 1615 | if ( isSelected ) |
| 1616 | { |
| 1617 | dc.SetTextBackground( grid.GetSelectionBackground() ); |
| 1618 | dc.SetTextForeground( grid.GetSelectionForeground() ); |
| 1619 | } |
| 1620 | else |
| 1621 | { |
| 1622 | dc.SetTextBackground( attr.GetBackgroundColour() ); |
| 1623 | dc.SetTextForeground( attr.GetTextColour() ); |
| 1624 | } |
| 1625 | } |
| 1626 | else |
| 1627 | { |
| 1628 | dc.SetTextBackground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE)); |
| 1629 | dc.SetTextForeground(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_GRAYTEXT)); |
| 1630 | } |
| 1631 | |
| 1632 | dc.SetFont( attr.GetFont() ); |
| 1633 | } |
| 1634 | |
| 1635 | wxSize wxGridCellStringRenderer::DoGetBestSize(wxGridCellAttr& attr, |
| 1636 | wxDC& dc, |
| 1637 | const wxString& text) |
| 1638 | { |
| 1639 | wxCoord x = 0, y = 0, max_x = 0; |
| 1640 | dc.SetFont(attr.GetFont()); |
| 1641 | wxStringTokenizer tk(text, _T('\n')); |
| 1642 | while ( tk.HasMoreTokens() ) |
| 1643 | { |
| 1644 | dc.GetTextExtent(tk.GetNextToken(), &x, &y); |
| 1645 | max_x = wxMax(max_x, x); |
| 1646 | } |
| 1647 | |
| 1648 | y *= 1 + text.Freq(wxT('\n')); // multiply by the number of lines. |
| 1649 | |
| 1650 | return wxSize(max_x, y); |
| 1651 | } |
| 1652 | |
| 1653 | wxSize wxGridCellStringRenderer::GetBestSize(wxGrid& grid, |
| 1654 | wxGridCellAttr& attr, |
| 1655 | wxDC& dc, |
| 1656 | int row, int col) |
| 1657 | { |
| 1658 | return DoGetBestSize(attr, dc, grid.GetCellValue(row, col)); |
| 1659 | } |
| 1660 | |
| 1661 | void wxGridCellStringRenderer::Draw(wxGrid& grid, |
| 1662 | wxGridCellAttr& attr, |
| 1663 | wxDC& dc, |
| 1664 | const wxRect& rectCell, |
| 1665 | int row, int col, |
| 1666 | bool isSelected) |
| 1667 | { |
| 1668 | wxRect rect = rectCell; |
| 1669 | rect.Inflate(-1); |
| 1670 | |
| 1671 | // erase only this cells background, overflow cells should have been erased |
| 1672 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); |
| 1673 | |
| 1674 | int hAlign, vAlign; |
| 1675 | attr.GetAlignment(&hAlign, &vAlign); |
| 1676 | |
| 1677 | int overflowCols = 0; |
| 1678 | |
| 1679 | if (attr.GetOverflow()) |
| 1680 | { |
| 1681 | int cols = grid.GetNumberCols(); |
| 1682 | int best_width = GetBestSize(grid,attr,dc,row,col).GetWidth(); |
| 1683 | int cell_rows, cell_cols; |
| 1684 | attr.GetSize( &cell_rows, &cell_cols ); // shouldn't get here if <=0 |
| 1685 | if ((best_width > rectCell.width) && (col < cols) && grid.GetTable()) |
| 1686 | { |
| 1687 | int i, c_cols, c_rows; |
| 1688 | for (i = col+cell_cols; i < cols; i++) |
| 1689 | { |
| 1690 | bool is_empty = true; |
| 1691 | for (int j=row; j<row+cell_rows; j++) |
| 1692 | { |
| 1693 | // check w/ anchor cell for multicell block |
| 1694 | grid.GetCellSize(j, i, &c_rows, &c_cols); |
| 1695 | if (c_rows > 0) c_rows = 0; |
| 1696 | if (!grid.GetTable()->IsEmptyCell(j+c_rows, i)) |
| 1697 | { |
| 1698 | is_empty = false; |
| 1699 | break; |
| 1700 | } |
| 1701 | } |
| 1702 | if (is_empty) |
| 1703 | rect.width += grid.GetColSize(i); |
| 1704 | else |
| 1705 | { |
| 1706 | i--; |
| 1707 | break; |
| 1708 | } |
| 1709 | if (rect.width >= best_width) break; |
| 1710 | } |
| 1711 | overflowCols = i - col - cell_cols + 1; |
| 1712 | if (overflowCols >= cols) overflowCols = cols - 1; |
| 1713 | } |
| 1714 | |
| 1715 | if (overflowCols > 0) // redraw overflow cells w/ proper hilight |
| 1716 | { |
| 1717 | hAlign = wxALIGN_LEFT; // if oveflowed then it's left aligned |
| 1718 | wxRect clip = rect; |
| 1719 | clip.x += rectCell.width; |
| 1720 | // draw each overflow cell individually |
| 1721 | int col_end = col+cell_cols+overflowCols; |
| 1722 | if (col_end >= grid.GetNumberCols()) |
| 1723 | col_end = grid.GetNumberCols() - 1; |
| 1724 | for (int i = col+cell_cols; i <= col_end; i++) |
| 1725 | { |
| 1726 | clip.width = grid.GetColSize(i) - 1; |
| 1727 | dc.DestroyClippingRegion(); |
| 1728 | dc.SetClippingRegion(clip); |
| 1729 | |
| 1730 | SetTextColoursAndFont(grid, attr, dc, |
| 1731 | grid.IsInSelection(row,i)); |
| 1732 | |
| 1733 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), |
| 1734 | rect, hAlign, vAlign); |
| 1735 | clip.x += grid.GetColSize(i) - 1; |
| 1736 | } |
| 1737 | |
| 1738 | rect = rectCell; |
| 1739 | rect.Inflate(-1); |
| 1740 | rect.width++; |
| 1741 | dc.DestroyClippingRegion(); |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | // now we only have to draw the text |
| 1746 | SetTextColoursAndFont(grid, attr, dc, isSelected); |
| 1747 | |
| 1748 | grid.DrawTextRectangle(dc, grid.GetCellValue(row, col), |
| 1749 | rect, hAlign, vAlign); |
| 1750 | } |
| 1751 | |
| 1752 | // ---------------------------------------------------------------------------- |
| 1753 | // wxGridCellNumberRenderer |
| 1754 | // ---------------------------------------------------------------------------- |
| 1755 | |
| 1756 | wxString wxGridCellNumberRenderer::GetString(wxGrid& grid, int row, int col) |
| 1757 | { |
| 1758 | wxGridTableBase *table = grid.GetTable(); |
| 1759 | wxString text; |
| 1760 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_NUMBER) ) |
| 1761 | { |
| 1762 | text.Printf(_T("%ld"), table->GetValueAsLong(row, col)); |
| 1763 | } |
| 1764 | else |
| 1765 | { |
| 1766 | text = table->GetValue(row, col); |
| 1767 | } |
| 1768 | |
| 1769 | return text; |
| 1770 | } |
| 1771 | |
| 1772 | void wxGridCellNumberRenderer::Draw(wxGrid& grid, |
| 1773 | wxGridCellAttr& attr, |
| 1774 | wxDC& dc, |
| 1775 | const wxRect& rectCell, |
| 1776 | int row, int col, |
| 1777 | bool isSelected) |
| 1778 | { |
| 1779 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); |
| 1780 | |
| 1781 | SetTextColoursAndFont(grid, attr, dc, isSelected); |
| 1782 | |
| 1783 | // draw the text right aligned by default |
| 1784 | int hAlign, vAlign; |
| 1785 | attr.GetAlignment(&hAlign, &vAlign); |
| 1786 | hAlign = wxALIGN_RIGHT; |
| 1787 | |
| 1788 | wxRect rect = rectCell; |
| 1789 | rect.Inflate(-1); |
| 1790 | |
| 1791 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); |
| 1792 | } |
| 1793 | |
| 1794 | wxSize wxGridCellNumberRenderer::GetBestSize(wxGrid& grid, |
| 1795 | wxGridCellAttr& attr, |
| 1796 | wxDC& dc, |
| 1797 | int row, int col) |
| 1798 | { |
| 1799 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); |
| 1800 | } |
| 1801 | |
| 1802 | // ---------------------------------------------------------------------------- |
| 1803 | // wxGridCellFloatRenderer |
| 1804 | // ---------------------------------------------------------------------------- |
| 1805 | |
| 1806 | wxGridCellFloatRenderer::wxGridCellFloatRenderer(int width, int precision) |
| 1807 | { |
| 1808 | SetWidth(width); |
| 1809 | SetPrecision(precision); |
| 1810 | } |
| 1811 | |
| 1812 | wxGridCellRenderer *wxGridCellFloatRenderer::Clone() const |
| 1813 | { |
| 1814 | wxGridCellFloatRenderer *renderer = new wxGridCellFloatRenderer; |
| 1815 | renderer->m_width = m_width; |
| 1816 | renderer->m_precision = m_precision; |
| 1817 | renderer->m_format = m_format; |
| 1818 | |
| 1819 | return renderer; |
| 1820 | } |
| 1821 | |
| 1822 | wxString wxGridCellFloatRenderer::GetString(wxGrid& grid, int row, int col) |
| 1823 | { |
| 1824 | wxGridTableBase *table = grid.GetTable(); |
| 1825 | |
| 1826 | bool hasDouble; |
| 1827 | double val; |
| 1828 | wxString text; |
| 1829 | if ( table->CanGetValueAs(row, col, wxGRID_VALUE_FLOAT) ) |
| 1830 | { |
| 1831 | val = table->GetValueAsDouble(row, col); |
| 1832 | hasDouble = true; |
| 1833 | } |
| 1834 | else |
| 1835 | { |
| 1836 | text = table->GetValue(row, col); |
| 1837 | hasDouble = text.ToDouble(&val); |
| 1838 | } |
| 1839 | |
| 1840 | if ( hasDouble ) |
| 1841 | { |
| 1842 | if ( !m_format ) |
| 1843 | { |
| 1844 | if ( m_width == -1 ) |
| 1845 | { |
| 1846 | if ( m_precision == -1 ) |
| 1847 | { |
| 1848 | // default width/precision |
| 1849 | m_format = _T("%f"); |
| 1850 | } |
| 1851 | else |
| 1852 | { |
| 1853 | m_format.Printf(_T("%%.%df"), m_precision); |
| 1854 | } |
| 1855 | } |
| 1856 | else if ( m_precision == -1 ) |
| 1857 | { |
| 1858 | // default precision |
| 1859 | m_format.Printf(_T("%%%d.f"), m_width); |
| 1860 | } |
| 1861 | else |
| 1862 | { |
| 1863 | m_format.Printf(_T("%%%d.%df"), m_width, m_precision); |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | text.Printf(m_format, val); |
| 1868 | |
| 1869 | } |
| 1870 | //else: text already contains the string |
| 1871 | |
| 1872 | return text; |
| 1873 | } |
| 1874 | |
| 1875 | void wxGridCellFloatRenderer::Draw(wxGrid& grid, |
| 1876 | wxGridCellAttr& attr, |
| 1877 | wxDC& dc, |
| 1878 | const wxRect& rectCell, |
| 1879 | int row, int col, |
| 1880 | bool isSelected) |
| 1881 | { |
| 1882 | wxGridCellRenderer::Draw(grid, attr, dc, rectCell, row, col, isSelected); |
| 1883 | |
| 1884 | SetTextColoursAndFont(grid, attr, dc, isSelected); |
| 1885 | |
| 1886 | // draw the text right aligned by default |
| 1887 | int hAlign, vAlign; |
| 1888 | attr.GetAlignment(&hAlign, &vAlign); |
| 1889 | hAlign = wxALIGN_RIGHT; |
| 1890 | |
| 1891 | wxRect rect = rectCell; |
| 1892 | rect.Inflate(-1); |
| 1893 | |
| 1894 | grid.DrawTextRectangle(dc, GetString(grid, row, col), rect, hAlign, vAlign); |
| 1895 | } |
| 1896 | |
| 1897 | wxSize wxGridCellFloatRenderer::GetBestSize(wxGrid& grid, |
| 1898 | wxGridCellAttr& attr, |
| 1899 | wxDC& dc, |
| 1900 | int row, int col) |
| 1901 | { |
| 1902 | return DoGetBestSize(attr, dc, GetString(grid, row, col)); |
| 1903 | } |
| 1904 | |
| 1905 | void wxGridCellFloatRenderer::SetParameters(const wxString& params) |
| 1906 | { |
| 1907 | if ( !params ) |
| 1908 | { |
| 1909 | // reset to defaults |
| 1910 | SetWidth(-1); |
| 1911 | SetPrecision(-1); |
| 1912 | } |
| 1913 | else |
| 1914 | { |
| 1915 | wxString tmp = params.BeforeFirst(_T(',')); |
| 1916 | if ( !!tmp ) |
| 1917 | { |
| 1918 | long width; |
| 1919 | if ( tmp.ToLong(&width) ) |
| 1920 | { |
| 1921 | SetWidth((int)width); |
| 1922 | } |
| 1923 | else |
| 1924 | { |
| 1925 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer width parameter string '%s ignored"), params.c_str()); |
| 1926 | } |
| 1927 | |
| 1928 | } |
| 1929 | tmp = params.AfterFirst(_T(',')); |
| 1930 | if ( !!tmp ) |
| 1931 | { |
| 1932 | long precision; |
| 1933 | if ( tmp.ToLong(&precision) ) |
| 1934 | { |
| 1935 | SetPrecision((int)precision); |
| 1936 | } |
| 1937 | else |
| 1938 | { |
| 1939 | wxLogDebug(_T("Invalid wxGridCellFloatRenderer precision parameter string '%s ignored"), params.c_str()); |
| 1940 | } |
| 1941 | |
| 1942 | } |
| 1943 | } |
| 1944 | } |
| 1945 | |
| 1946 | |
| 1947 | // ---------------------------------------------------------------------------- |
| 1948 | // wxGridCellBoolRenderer |
| 1949 | // ---------------------------------------------------------------------------- |
| 1950 | |
| 1951 | wxSize wxGridCellBoolRenderer::ms_sizeCheckMark; |
| 1952 | |
| 1953 | // FIXME these checkbox size calculations are really ugly... |
| 1954 | |
| 1955 | // between checkmark and box |
| 1956 | static const wxCoord wxGRID_CHECKMARK_MARGIN = 2; |
| 1957 | |
| 1958 | wxSize wxGridCellBoolRenderer::GetBestSize(wxGrid& grid, |
| 1959 | wxGridCellAttr& WXUNUSED(attr), |
| 1960 | wxDC& WXUNUSED(dc), |
| 1961 | int WXUNUSED(row), |
| 1962 | int WXUNUSED(col)) |
| 1963 | { |
| 1964 | // compute it only once (no locks for MT safeness in GUI thread...) |
| 1965 | if ( !ms_sizeCheckMark.x ) |
| 1966 | { |
| 1967 | // get checkbox size |
| 1968 | wxCheckBox *checkbox = new wxCheckBox(&grid, wxID_ANY, wxEmptyString); |
| 1969 | wxSize size = checkbox->GetBestSize(); |
| 1970 | wxCoord checkSize = size.y + 2*wxGRID_CHECKMARK_MARGIN; |
| 1971 | |
| 1972 | // FIXME wxGTK::wxCheckBox::GetBestSize() gives "wrong" result |
| 1973 | #if defined(__WXGTK__) || defined(__WXMOTIF__) |
| 1974 | checkSize -= size.y / 2; |
| 1975 | #endif |
| 1976 | |
| 1977 | delete checkbox; |
| 1978 | |
| 1979 | ms_sizeCheckMark.x = ms_sizeCheckMark.y = checkSize; |
| 1980 | } |
| 1981 | |
| 1982 | return ms_sizeCheckMark; |
| 1983 | } |
| 1984 | |
| 1985 | void wxGridCellBoolRenderer::Draw(wxGrid& grid, |
| 1986 | wxGridCellAttr& attr, |
| 1987 | wxDC& dc, |
| 1988 | const wxRect& rect, |
| 1989 | int row, int col, |
| 1990 | bool isSelected) |
| 1991 | { |
| 1992 | wxGridCellRenderer::Draw(grid, attr, dc, rect, row, col, isSelected); |
| 1993 | |
| 1994 | // draw a check mark in the centre (ignoring alignment - TODO) |
| 1995 | wxSize size = GetBestSize(grid, attr, dc, row, col); |
| 1996 | |
| 1997 | // don't draw outside the cell |
| 1998 | wxCoord minSize = wxMin(rect.width, rect.height); |
| 1999 | if ( size.x >= minSize || size.y >= minSize ) |
| 2000 | { |
| 2001 | // and even leave (at least) 1 pixel margin |
| 2002 | size.x = size.y = minSize - 2; |
| 2003 | } |
| 2004 | |
| 2005 | // draw a border around checkmark |
| 2006 | int vAlign, hAlign; |
| 2007 | attr.GetAlignment(& hAlign, &vAlign); |
| 2008 | |
| 2009 | wxRect rectBorder; |
| 2010 | if (hAlign == wxALIGN_CENTRE) |
| 2011 | { |
| 2012 | rectBorder.x = rect.x + rect.width/2 - size.x/2; |
| 2013 | rectBorder.y = rect.y + rect.height/2 - size.y/2; |
| 2014 | rectBorder.width = size.x; |
| 2015 | rectBorder.height = size.y; |
| 2016 | } |
| 2017 | else if (hAlign == wxALIGN_LEFT) |
| 2018 | { |
| 2019 | rectBorder.x = rect.x + 2; |
| 2020 | rectBorder.y = rect.y + rect.height/2 - size.y/2; |
| 2021 | rectBorder.width = size.x; |
| 2022 | rectBorder.height = size.y; |
| 2023 | } |
| 2024 | else if (hAlign == wxALIGN_RIGHT) |
| 2025 | { |
| 2026 | rectBorder.x = rect.x + rect.width - size.x - 2; |
| 2027 | rectBorder.y = rect.y + rect.height/2 - size.y/2; |
| 2028 | rectBorder.width = size.x; |
| 2029 | rectBorder.height = size.y; |
| 2030 | } |
| 2031 | |
| 2032 | bool value; |
| 2033 | if ( grid.GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL) ) |
| 2034 | value = grid.GetTable()->GetValueAsBool(row, col); |
| 2035 | else |
| 2036 | { |
| 2037 | wxString cellval( grid.GetTable()->GetValue(row, col) ); |
| 2038 | value = !( !cellval || (cellval == wxT("0")) ); |
| 2039 | } |
| 2040 | |
| 2041 | if ( value ) |
| 2042 | { |
| 2043 | wxRect rectMark = rectBorder; |
| 2044 | #ifdef __WXMSW__ |
| 2045 | // MSW DrawCheckMark() is weird (and should probably be changed...) |
| 2046 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN/2); |
| 2047 | rectMark.x++; |
| 2048 | rectMark.y++; |
| 2049 | #else // !MSW |
| 2050 | rectMark.Inflate(-wxGRID_CHECKMARK_MARGIN); |
| 2051 | #endif // MSW/!MSW |
| 2052 | |
| 2053 | dc.SetTextForeground(attr.GetTextColour()); |
| 2054 | dc.DrawCheckMark(rectMark); |
| 2055 | } |
| 2056 | |
| 2057 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
| 2058 | dc.SetPen(wxPen(attr.GetTextColour(), 1, wxSOLID)); |
| 2059 | dc.DrawRectangle(rectBorder); |
| 2060 | } |
| 2061 | |
| 2062 | // ---------------------------------------------------------------------------- |
| 2063 | // wxGridCellAttr |
| 2064 | // ---------------------------------------------------------------------------- |
| 2065 | |
| 2066 | void wxGridCellAttr::Init(wxGridCellAttr *attrDefault) |
| 2067 | { |
| 2068 | m_nRef = 1; |
| 2069 | |
| 2070 | m_isReadOnly = Unset; |
| 2071 | |
| 2072 | m_renderer = NULL; |
| 2073 | m_editor = NULL; |
| 2074 | |
| 2075 | m_attrkind = wxGridCellAttr::Cell; |
| 2076 | |
| 2077 | m_sizeRows = m_sizeCols = 1; |
| 2078 | m_overflow = UnsetOverflow; |
| 2079 | |
| 2080 | SetDefAttr(attrDefault); |
| 2081 | } |
| 2082 | |
| 2083 | wxGridCellAttr *wxGridCellAttr::Clone() const |
| 2084 | { |
| 2085 | wxGridCellAttr *attr = new wxGridCellAttr(m_defGridAttr); |
| 2086 | |
| 2087 | if ( HasTextColour() ) |
| 2088 | attr->SetTextColour(GetTextColour()); |
| 2089 | if ( HasBackgroundColour() ) |
| 2090 | attr->SetBackgroundColour(GetBackgroundColour()); |
| 2091 | if ( HasFont() ) |
| 2092 | attr->SetFont(GetFont()); |
| 2093 | if ( HasAlignment() ) |
| 2094 | attr->SetAlignment(m_hAlign, m_vAlign); |
| 2095 | |
| 2096 | attr->SetSize( m_sizeRows, m_sizeCols ); |
| 2097 | |
| 2098 | if ( m_renderer ) |
| 2099 | { |
| 2100 | attr->SetRenderer(m_renderer); |
| 2101 | m_renderer->IncRef(); |
| 2102 | } |
| 2103 | if ( m_editor ) |
| 2104 | { |
| 2105 | attr->SetEditor(m_editor); |
| 2106 | m_editor->IncRef(); |
| 2107 | } |
| 2108 | |
| 2109 | if ( IsReadOnly() ) |
| 2110 | attr->SetReadOnly(); |
| 2111 | |
| 2112 | attr->SetKind( m_attrkind ); |
| 2113 | |
| 2114 | return attr; |
| 2115 | } |
| 2116 | |
| 2117 | void wxGridCellAttr::MergeWith(wxGridCellAttr *mergefrom) |
| 2118 | { |
| 2119 | if ( !HasTextColour() && mergefrom->HasTextColour() ) |
| 2120 | SetTextColour(mergefrom->GetTextColour()); |
| 2121 | if ( !HasBackgroundColour() && mergefrom->HasBackgroundColour() ) |
| 2122 | SetBackgroundColour(mergefrom->GetBackgroundColour()); |
| 2123 | if ( !HasFont() && mergefrom->HasFont() ) |
| 2124 | SetFont(mergefrom->GetFont()); |
| 2125 | if ( !HasAlignment() && mergefrom->HasAlignment() ){ |
| 2126 | int hAlign, vAlign; |
| 2127 | mergefrom->GetAlignment( &hAlign, &vAlign); |
| 2128 | SetAlignment(hAlign, vAlign); |
| 2129 | } |
| 2130 | |
| 2131 | mergefrom->GetSize( &m_sizeRows, &m_sizeCols ); |
| 2132 | |
| 2133 | // Directly access member functions as GetRender/Editor don't just return |
| 2134 | // m_renderer/m_editor |
| 2135 | // |
| 2136 | // Maybe add support for merge of Render and Editor? |
| 2137 | if (!HasRenderer() && mergefrom->HasRenderer() ) |
| 2138 | { |
| 2139 | m_renderer = mergefrom->m_renderer; |
| 2140 | m_renderer->IncRef(); |
| 2141 | } |
| 2142 | if ( !HasEditor() && mergefrom->HasEditor() ) |
| 2143 | { |
| 2144 | m_editor = mergefrom->m_editor; |
| 2145 | m_editor->IncRef(); |
| 2146 | } |
| 2147 | if ( !HasReadWriteMode() && mergefrom->HasReadWriteMode() ) |
| 2148 | SetReadOnly(mergefrom->IsReadOnly()); |
| 2149 | |
| 2150 | if (!HasOverflowMode() && mergefrom->HasOverflowMode() ) |
| 2151 | SetOverflow(mergefrom->GetOverflow()); |
| 2152 | |
| 2153 | SetDefAttr(mergefrom->m_defGridAttr); |
| 2154 | } |
| 2155 | |
| 2156 | void wxGridCellAttr::SetSize(int num_rows, int num_cols) |
| 2157 | { |
| 2158 | // The size of a cell is normally 1,1 |
| 2159 | |
| 2160 | // If this cell is larger (2,2) then this is the top left cell |
| 2161 | // the other cells that will be covered (lower right cells) must be |
| 2162 | // set to negative or zero values such that |
| 2163 | // row + num_rows of the covered cell points to the larger cell (this cell) |
| 2164 | // same goes for the col + num_cols. |
| 2165 | |
| 2166 | // Size of 0,0 is NOT valid, neither is <=0 and any positive value |
| 2167 | |
| 2168 | wxASSERT_MSG( (!((num_rows>0)&&(num_cols<=0)) || |
| 2169 | !((num_rows<=0)&&(num_cols>0)) || |
| 2170 | !((num_rows==0)&&(num_cols==0))), |
| 2171 | wxT("wxGridCellAttr::SetSize only takes two postive values or negative/zero values")); |
| 2172 | |
| 2173 | m_sizeRows = num_rows; |
| 2174 | m_sizeCols = num_cols; |
| 2175 | } |
| 2176 | |
| 2177 | const wxColour& wxGridCellAttr::GetTextColour() const |
| 2178 | { |
| 2179 | if (HasTextColour()) |
| 2180 | { |
| 2181 | return m_colText; |
| 2182 | } |
| 2183 | else if (m_defGridAttr && m_defGridAttr != this) |
| 2184 | { |
| 2185 | return m_defGridAttr->GetTextColour(); |
| 2186 | } |
| 2187 | else |
| 2188 | { |
| 2189 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
| 2190 | return wxNullColour; |
| 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | |
| 2195 | const wxColour& wxGridCellAttr::GetBackgroundColour() const |
| 2196 | { |
| 2197 | if (HasBackgroundColour()) |
| 2198 | return m_colBack; |
| 2199 | else if (m_defGridAttr && m_defGridAttr != this) |
| 2200 | return m_defGridAttr->GetBackgroundColour(); |
| 2201 | else |
| 2202 | { |
| 2203 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
| 2204 | return wxNullColour; |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | |
| 2209 | const wxFont& wxGridCellAttr::GetFont() const |
| 2210 | { |
| 2211 | if (HasFont()) |
| 2212 | return m_font; |
| 2213 | else if (m_defGridAttr && m_defGridAttr != this) |
| 2214 | return m_defGridAttr->GetFont(); |
| 2215 | else |
| 2216 | { |
| 2217 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
| 2218 | return wxNullFont; |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | |
| 2223 | void wxGridCellAttr::GetAlignment(int *hAlign, int *vAlign) const |
| 2224 | { |
| 2225 | if (HasAlignment()) |
| 2226 | { |
| 2227 | if ( hAlign ) *hAlign = m_hAlign; |
| 2228 | if ( vAlign ) *vAlign = m_vAlign; |
| 2229 | } |
| 2230 | else if (m_defGridAttr && m_defGridAttr != this) |
| 2231 | m_defGridAttr->GetAlignment(hAlign, vAlign); |
| 2232 | else |
| 2233 | { |
| 2234 | wxFAIL_MSG(wxT("Missing default cell attribute")); |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | void wxGridCellAttr::GetSize( int *num_rows, int *num_cols ) const |
| 2239 | { |
| 2240 | if ( num_rows ) *num_rows = m_sizeRows; |
| 2241 | if ( num_cols ) *num_cols = m_sizeCols; |
| 2242 | } |
| 2243 | |
| 2244 | // GetRenderer and GetEditor use a slightly different decision path about |
| 2245 | // which attribute to use. If a non-default attr object has one then it is |
| 2246 | // used, otherwise the default editor or renderer is fetched from the grid and |
| 2247 | // used. It should be the default for the data type of the cell. If it is |
| 2248 | // NULL (because the table has a type that the grid does not have in its |
| 2249 | // registry,) then the grid's default editor or renderer is used. |
| 2250 | |
| 2251 | wxGridCellRenderer* wxGridCellAttr::GetRenderer(wxGrid* grid, int row, int col) const |
| 2252 | { |
| 2253 | wxGridCellRenderer *renderer; |
| 2254 | |
| 2255 | if ( m_renderer && this != m_defGridAttr ) |
| 2256 | { |
| 2257 | // use the cells renderer if it has one |
| 2258 | renderer = m_renderer; |
| 2259 | renderer->IncRef(); |
| 2260 | } |
| 2261 | else // no non default cell renderer |
| 2262 | { |
| 2263 | // get default renderer for the data type |
| 2264 | if ( grid ) |
| 2265 | { |
| 2266 | // GetDefaultRendererForCell() will do IncRef() for us |
| 2267 | renderer = grid->GetDefaultRendererForCell(row, col); |
| 2268 | } |
| 2269 | else |
| 2270 | { |
| 2271 | renderer = NULL; |
| 2272 | } |
| 2273 | |
| 2274 | if ( !renderer ) |
| 2275 | { |
| 2276 | if (m_defGridAttr && this != m_defGridAttr ) |
| 2277 | { |
| 2278 | // if we still don't have one then use the grid default |
| 2279 | // (no need for IncRef() here neither) |
| 2280 | renderer = m_defGridAttr->GetRenderer(NULL, 0, 0); |
| 2281 | } |
| 2282 | else // default grid attr |
| 2283 | { |
| 2284 | // use m_renderer which we had decided not to use initially |
| 2285 | renderer = m_renderer; |
| 2286 | if ( renderer ) |
| 2287 | renderer->IncRef(); |
| 2288 | } |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | // we're supposed to always find something |
| 2293 | wxASSERT_MSG(renderer, wxT("Missing default cell renderer")); |
| 2294 | |
| 2295 | return renderer; |
| 2296 | } |
| 2297 | |
| 2298 | // same as above, except for s/renderer/editor/g |
| 2299 | wxGridCellEditor* wxGridCellAttr::GetEditor(wxGrid* grid, int row, int col) const |
| 2300 | { |
| 2301 | wxGridCellEditor *editor; |
| 2302 | |
| 2303 | if ( m_editor && this != m_defGridAttr ) |
| 2304 | { |
| 2305 | // use the cells editor if it has one |
| 2306 | editor = m_editor; |
| 2307 | editor->IncRef(); |
| 2308 | } |
| 2309 | else // no non default cell editor |
| 2310 | { |
| 2311 | // get default editor for the data type |
| 2312 | if ( grid ) |
| 2313 | { |
| 2314 | // GetDefaultEditorForCell() will do IncRef() for us |
| 2315 | editor = grid->GetDefaultEditorForCell(row, col); |
| 2316 | } |
| 2317 | else |
| 2318 | { |
| 2319 | editor = NULL; |
| 2320 | } |
| 2321 | |
| 2322 | if ( !editor ) |
| 2323 | { |
| 2324 | if ( m_defGridAttr && this != m_defGridAttr ) |
| 2325 | { |
| 2326 | // if we still don't have one then use the grid default |
| 2327 | // (no need for IncRef() here neither) |
| 2328 | editor = m_defGridAttr->GetEditor(NULL, 0, 0); |
| 2329 | } |
| 2330 | else // default grid attr |
| 2331 | { |
| 2332 | // use m_editor which we had decided not to use initially |
| 2333 | editor = m_editor; |
| 2334 | if ( editor ) |
| 2335 | editor->IncRef(); |
| 2336 | } |
| 2337 | } |
| 2338 | } |
| 2339 | |
| 2340 | // we're supposed to always find something |
| 2341 | wxASSERT_MSG(editor, wxT("Missing default cell editor")); |
| 2342 | |
| 2343 | return editor; |
| 2344 | } |
| 2345 | |
| 2346 | // ---------------------------------------------------------------------------- |
| 2347 | // wxGridCellAttrData |
| 2348 | // ---------------------------------------------------------------------------- |
| 2349 | |
| 2350 | void wxGridCellAttrData::SetAttr(wxGridCellAttr *attr, int row, int col) |
| 2351 | { |
| 2352 | int n = FindIndex(row, col); |
| 2353 | if ( n == wxNOT_FOUND ) |
| 2354 | { |
| 2355 | // add the attribute |
| 2356 | m_attrs.Add(new wxGridCellWithAttr(row, col, attr)); |
| 2357 | } |
| 2358 | else |
| 2359 | { |
| 2360 | // free the old attribute |
| 2361 | m_attrs[(size_t)n].attr->DecRef(); |
| 2362 | |
| 2363 | if ( attr ) |
| 2364 | { |
| 2365 | // change the attribute |
| 2366 | m_attrs[(size_t)n].attr = attr; |
| 2367 | } |
| 2368 | else |
| 2369 | { |
| 2370 | // remove this attribute |
| 2371 | m_attrs.RemoveAt((size_t)n); |
| 2372 | } |
| 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | wxGridCellAttr *wxGridCellAttrData::GetAttr(int row, int col) const |
| 2377 | { |
| 2378 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; |
| 2379 | |
| 2380 | int n = FindIndex(row, col); |
| 2381 | if ( n != wxNOT_FOUND ) |
| 2382 | { |
| 2383 | attr = m_attrs[(size_t)n].attr; |
| 2384 | attr->IncRef(); |
| 2385 | } |
| 2386 | |
| 2387 | return attr; |
| 2388 | } |
| 2389 | |
| 2390 | void wxGridCellAttrData::UpdateAttrRows( size_t pos, int numRows ) |
| 2391 | { |
| 2392 | size_t count = m_attrs.GetCount(); |
| 2393 | for ( size_t n = 0; n < count; n++ ) |
| 2394 | { |
| 2395 | wxGridCellCoords& coords = m_attrs[n].coords; |
| 2396 | wxCoord row = coords.GetRow(); |
| 2397 | if ((size_t)row >= pos) |
| 2398 | { |
| 2399 | if (numRows > 0) |
| 2400 | { |
| 2401 | // If rows inserted, include row counter where necessary |
| 2402 | coords.SetRow(row + numRows); |
| 2403 | } |
| 2404 | else if (numRows < 0) |
| 2405 | { |
| 2406 | // If rows deleted ... |
| 2407 | if ((size_t)row >= pos - numRows) |
| 2408 | { |
| 2409 | // ...either decrement row counter (if row still exists)... |
| 2410 | coords.SetRow(row + numRows); |
| 2411 | } |
| 2412 | else |
| 2413 | { |
| 2414 | // ...or remove the attribute |
| 2415 | m_attrs.RemoveAt((size_t)n); |
| 2416 | n--; count--; |
| 2417 | } |
| 2418 | } |
| 2419 | } |
| 2420 | } |
| 2421 | } |
| 2422 | |
| 2423 | void wxGridCellAttrData::UpdateAttrCols( size_t pos, int numCols ) |
| 2424 | { |
| 2425 | size_t count = m_attrs.GetCount(); |
| 2426 | for ( size_t n = 0; n < count; n++ ) |
| 2427 | { |
| 2428 | wxGridCellCoords& coords = m_attrs[n].coords; |
| 2429 | wxCoord col = coords.GetCol(); |
| 2430 | if ( (size_t)col >= pos ) |
| 2431 | { |
| 2432 | if ( numCols > 0 ) |
| 2433 | { |
| 2434 | // If rows inserted, include row counter where necessary |
| 2435 | coords.SetCol(col + numCols); |
| 2436 | } |
| 2437 | else if (numCols < 0) |
| 2438 | { |
| 2439 | // If rows deleted ... |
| 2440 | if ((size_t)col >= pos - numCols) |
| 2441 | { |
| 2442 | // ...either decrement row counter (if row still exists)... |
| 2443 | coords.SetCol(col + numCols); |
| 2444 | } |
| 2445 | else |
| 2446 | { |
| 2447 | // ...or remove the attribute |
| 2448 | m_attrs.RemoveAt((size_t)n); |
| 2449 | n--; count--; |
| 2450 | } |
| 2451 | } |
| 2452 | } |
| 2453 | } |
| 2454 | } |
| 2455 | |
| 2456 | int wxGridCellAttrData::FindIndex(int row, int col) const |
| 2457 | { |
| 2458 | size_t count = m_attrs.GetCount(); |
| 2459 | for ( size_t n = 0; n < count; n++ ) |
| 2460 | { |
| 2461 | const wxGridCellCoords& coords = m_attrs[n].coords; |
| 2462 | if ( (coords.GetRow() == row) && (coords.GetCol() == col) ) |
| 2463 | { |
| 2464 | return n; |
| 2465 | } |
| 2466 | } |
| 2467 | |
| 2468 | return wxNOT_FOUND; |
| 2469 | } |
| 2470 | |
| 2471 | // ---------------------------------------------------------------------------- |
| 2472 | // wxGridRowOrColAttrData |
| 2473 | // ---------------------------------------------------------------------------- |
| 2474 | |
| 2475 | wxGridRowOrColAttrData::~wxGridRowOrColAttrData() |
| 2476 | { |
| 2477 | size_t count = m_attrs.Count(); |
| 2478 | for ( size_t n = 0; n < count; n++ ) |
| 2479 | { |
| 2480 | m_attrs[n]->DecRef(); |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | wxGridCellAttr *wxGridRowOrColAttrData::GetAttr(int rowOrCol) const |
| 2485 | { |
| 2486 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; |
| 2487 | |
| 2488 | int n = m_rowsOrCols.Index(rowOrCol); |
| 2489 | if ( n != wxNOT_FOUND ) |
| 2490 | { |
| 2491 | attr = m_attrs[(size_t)n]; |
| 2492 | attr->IncRef(); |
| 2493 | } |
| 2494 | |
| 2495 | return attr; |
| 2496 | } |
| 2497 | |
| 2498 | void wxGridRowOrColAttrData::SetAttr(wxGridCellAttr *attr, int rowOrCol) |
| 2499 | { |
| 2500 | int i = m_rowsOrCols.Index(rowOrCol); |
| 2501 | if ( i == wxNOT_FOUND ) |
| 2502 | { |
| 2503 | // add the attribute |
| 2504 | m_rowsOrCols.Add(rowOrCol); |
| 2505 | m_attrs.Add(attr); |
| 2506 | } |
| 2507 | else |
| 2508 | { |
| 2509 | size_t n = (size_t)i; |
| 2510 | if ( attr ) |
| 2511 | { |
| 2512 | // change the attribute |
| 2513 | m_attrs[n]->DecRef(); |
| 2514 | m_attrs[n] = attr; |
| 2515 | } |
| 2516 | else |
| 2517 | { |
| 2518 | // remove this attribute |
| 2519 | m_attrs[n]->DecRef(); |
| 2520 | m_rowsOrCols.RemoveAt(n); |
| 2521 | m_attrs.RemoveAt(n); |
| 2522 | } |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | void wxGridRowOrColAttrData::UpdateAttrRowsOrCols( size_t pos, int numRowsOrCols ) |
| 2527 | { |
| 2528 | size_t count = m_attrs.GetCount(); |
| 2529 | for ( size_t n = 0; n < count; n++ ) |
| 2530 | { |
| 2531 | int & rowOrCol = m_rowsOrCols[n]; |
| 2532 | if ( (size_t)rowOrCol >= pos ) |
| 2533 | { |
| 2534 | if ( numRowsOrCols > 0 ) |
| 2535 | { |
| 2536 | // If rows inserted, include row counter where necessary |
| 2537 | rowOrCol += numRowsOrCols; |
| 2538 | } |
| 2539 | else if ( numRowsOrCols < 0) |
| 2540 | { |
| 2541 | // If rows deleted, either decrement row counter (if row still exists) |
| 2542 | if ((size_t)rowOrCol >= pos - numRowsOrCols) |
| 2543 | rowOrCol += numRowsOrCols; |
| 2544 | else |
| 2545 | { |
| 2546 | m_rowsOrCols.RemoveAt((size_t)n); |
| 2547 | m_attrs.RemoveAt((size_t)n); |
| 2548 | n--; count--; |
| 2549 | } |
| 2550 | } |
| 2551 | } |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | // ---------------------------------------------------------------------------- |
| 2556 | // wxGridCellAttrProvider |
| 2557 | // ---------------------------------------------------------------------------- |
| 2558 | |
| 2559 | wxGridCellAttrProvider::wxGridCellAttrProvider() |
| 2560 | { |
| 2561 | m_data = (wxGridCellAttrProviderData *)NULL; |
| 2562 | } |
| 2563 | |
| 2564 | wxGridCellAttrProvider::~wxGridCellAttrProvider() |
| 2565 | { |
| 2566 | delete m_data; |
| 2567 | } |
| 2568 | |
| 2569 | void wxGridCellAttrProvider::InitData() |
| 2570 | { |
| 2571 | m_data = new wxGridCellAttrProviderData; |
| 2572 | } |
| 2573 | |
| 2574 | wxGridCellAttr *wxGridCellAttrProvider::GetAttr(int row, int col, |
| 2575 | wxGridCellAttr::wxAttrKind kind ) const |
| 2576 | { |
| 2577 | wxGridCellAttr *attr = (wxGridCellAttr *)NULL; |
| 2578 | if ( m_data ) |
| 2579 | { |
| 2580 | switch(kind) |
| 2581 | { |
| 2582 | case (wxGridCellAttr::Any): |
| 2583 | //Get cached merge attributes. |
| 2584 | // Currenlty not used as no cache implemented as not mutiable |
| 2585 | // attr = m_data->m_mergeAttr.GetAttr(row, col); |
| 2586 | if(!attr) |
| 2587 | { |
| 2588 | //Basicaly implement old version. |
| 2589 | //Also check merge cache, so we don't have to re-merge every time.. |
| 2590 | wxGridCellAttr *attrcell = m_data->m_cellAttrs.GetAttr(row, col); |
| 2591 | wxGridCellAttr *attrrow = m_data->m_rowAttrs.GetAttr(row); |
| 2592 | wxGridCellAttr *attrcol = m_data->m_colAttrs.GetAttr(col); |
| 2593 | |
| 2594 | if((attrcell != attrrow) && (attrrow !=attrcol) && (attrcell != attrcol)){ |
| 2595 | // Two or move are non NULL |
| 2596 | attr = new wxGridCellAttr; |
| 2597 | attr->SetKind(wxGridCellAttr::Merged); |
| 2598 | |
| 2599 | //Order important.. |
| 2600 | if(attrcell){ |
| 2601 | attr->MergeWith(attrcell); |
| 2602 | attrcell->DecRef(); |
| 2603 | } |
| 2604 | if(attrcol){ |
| 2605 | attr->MergeWith(attrcol); |
| 2606 | attrcol->DecRef(); |
| 2607 | } |
| 2608 | if(attrrow){ |
| 2609 | attr->MergeWith(attrrow); |
| 2610 | attrrow->DecRef(); |
| 2611 | } |
| 2612 | //store merge attr if cache implemented |
| 2613 | //attr->IncRef(); |
| 2614 | //m_data->m_mergeAttr.SetAttr(attr, row, col); |
| 2615 | } |
| 2616 | else |
| 2617 | { |
| 2618 | // one or none is non null return it or null. |
| 2619 | if(attrrow) attr = attrrow; |
| 2620 | if(attrcol) attr = attrcol; |
| 2621 | if(attrcell) attr = attrcell; |
| 2622 | } |
| 2623 | } |
| 2624 | break; |
| 2625 | case (wxGridCellAttr::Cell): |
| 2626 | attr = m_data->m_cellAttrs.GetAttr(row, col); |
| 2627 | break; |
| 2628 | case (wxGridCellAttr::Col): |
| 2629 | attr = m_data->m_colAttrs.GetAttr(col); |
| 2630 | break; |
| 2631 | case (wxGridCellAttr::Row): |
| 2632 | attr = m_data->m_rowAttrs.GetAttr(row); |
| 2633 | break; |
| 2634 | default: |
| 2635 | // unused as yet... |
| 2636 | // (wxGridCellAttr::Default): |
| 2637 | // (wxGridCellAttr::Merged): |
| 2638 | break; |
| 2639 | } |
| 2640 | } |
| 2641 | return attr; |
| 2642 | } |
| 2643 | |
| 2644 | void wxGridCellAttrProvider::SetAttr(wxGridCellAttr *attr, |
| 2645 | int row, int col) |
| 2646 | { |
| 2647 | if ( !m_data ) |
| 2648 | InitData(); |
| 2649 | |
| 2650 | m_data->m_cellAttrs.SetAttr(attr, row, col); |
| 2651 | } |
| 2652 | |
| 2653 | void wxGridCellAttrProvider::SetRowAttr(wxGridCellAttr *attr, int row) |
| 2654 | { |
| 2655 | if ( !m_data ) |
| 2656 | InitData(); |
| 2657 | |
| 2658 | m_data->m_rowAttrs.SetAttr(attr, row); |
| 2659 | } |
| 2660 | |
| 2661 | void wxGridCellAttrProvider::SetColAttr(wxGridCellAttr *attr, int col) |
| 2662 | { |
| 2663 | if ( !m_data ) |
| 2664 | InitData(); |
| 2665 | |
| 2666 | m_data->m_colAttrs.SetAttr(attr, col); |
| 2667 | } |
| 2668 | |
| 2669 | void wxGridCellAttrProvider::UpdateAttrRows( size_t pos, int numRows ) |
| 2670 | { |
| 2671 | if ( m_data ) |
| 2672 | { |
| 2673 | m_data->m_cellAttrs.UpdateAttrRows( pos, numRows ); |
| 2674 | |
| 2675 | m_data->m_rowAttrs.UpdateAttrRowsOrCols( pos, numRows ); |
| 2676 | } |
| 2677 | } |
| 2678 | |
| 2679 | void wxGridCellAttrProvider::UpdateAttrCols( size_t pos, int numCols ) |
| 2680 | { |
| 2681 | if ( m_data ) |
| 2682 | { |
| 2683 | m_data->m_cellAttrs.UpdateAttrCols( pos, numCols ); |
| 2684 | |
| 2685 | m_data->m_colAttrs.UpdateAttrRowsOrCols( pos, numCols ); |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | // ---------------------------------------------------------------------------- |
| 2690 | // wxGridTypeRegistry |
| 2691 | // ---------------------------------------------------------------------------- |
| 2692 | |
| 2693 | wxGridTypeRegistry::~wxGridTypeRegistry() |
| 2694 | { |
| 2695 | size_t count = m_typeinfo.Count(); |
| 2696 | for ( size_t i = 0; i < count; i++ ) |
| 2697 | delete m_typeinfo[i]; |
| 2698 | } |
| 2699 | |
| 2700 | |
| 2701 | void wxGridTypeRegistry::RegisterDataType(const wxString& typeName, |
| 2702 | wxGridCellRenderer* renderer, |
| 2703 | wxGridCellEditor* editor) |
| 2704 | { |
| 2705 | wxGridDataTypeInfo* info = new wxGridDataTypeInfo(typeName, renderer, editor); |
| 2706 | |
| 2707 | // is it already registered? |
| 2708 | int loc = FindRegisteredDataType(typeName); |
| 2709 | if ( loc != wxNOT_FOUND ) |
| 2710 | { |
| 2711 | delete m_typeinfo[loc]; |
| 2712 | m_typeinfo[loc] = info; |
| 2713 | } |
| 2714 | else |
| 2715 | { |
| 2716 | m_typeinfo.Add(info); |
| 2717 | } |
| 2718 | } |
| 2719 | |
| 2720 | int wxGridTypeRegistry::FindRegisteredDataType(const wxString& typeName) |
| 2721 | { |
| 2722 | size_t count = m_typeinfo.GetCount(); |
| 2723 | for ( size_t i = 0; i < count; i++ ) |
| 2724 | { |
| 2725 | if ( typeName == m_typeinfo[i]->m_typeName ) |
| 2726 | { |
| 2727 | return i; |
| 2728 | } |
| 2729 | } |
| 2730 | |
| 2731 | return wxNOT_FOUND; |
| 2732 | } |
| 2733 | |
| 2734 | int wxGridTypeRegistry::FindDataType(const wxString& typeName) |
| 2735 | { |
| 2736 | int index = FindRegisteredDataType(typeName); |
| 2737 | if ( index == wxNOT_FOUND ) |
| 2738 | { |
| 2739 | // check whether this is one of the standard ones, in which case |
| 2740 | // register it "on the fly" |
| 2741 | #if wxUSE_TEXTCTRL |
| 2742 | if ( typeName == wxGRID_VALUE_STRING ) |
| 2743 | { |
| 2744 | RegisterDataType(wxGRID_VALUE_STRING, |
| 2745 | new wxGridCellStringRenderer, |
| 2746 | new wxGridCellTextEditor); |
| 2747 | } else |
| 2748 | #endif // wxUSE_TEXTCTRL |
| 2749 | #if wxUSE_CHECKBOX |
| 2750 | if ( typeName == wxGRID_VALUE_BOOL ) |
| 2751 | { |
| 2752 | RegisterDataType(wxGRID_VALUE_BOOL, |
| 2753 | new wxGridCellBoolRenderer, |
| 2754 | new wxGridCellBoolEditor); |
| 2755 | } else |
| 2756 | #endif // wxUSE_CHECKBOX |
| 2757 | #if wxUSE_TEXTCTRL |
| 2758 | if ( typeName == wxGRID_VALUE_NUMBER ) |
| 2759 | { |
| 2760 | RegisterDataType(wxGRID_VALUE_NUMBER, |
| 2761 | new wxGridCellNumberRenderer, |
| 2762 | new wxGridCellNumberEditor); |
| 2763 | } |
| 2764 | else if ( typeName == wxGRID_VALUE_FLOAT ) |
| 2765 | { |
| 2766 | RegisterDataType(wxGRID_VALUE_FLOAT, |
| 2767 | new wxGridCellFloatRenderer, |
| 2768 | new wxGridCellFloatEditor); |
| 2769 | } else |
| 2770 | #endif // wxUSE_TEXTCTRL |
| 2771 | #if wxUSE_COMBOBOX |
| 2772 | if ( typeName == wxGRID_VALUE_CHOICE ) |
| 2773 | { |
| 2774 | RegisterDataType(wxGRID_VALUE_CHOICE, |
| 2775 | new wxGridCellStringRenderer, |
| 2776 | new wxGridCellChoiceEditor); |
| 2777 | } else |
| 2778 | #endif // wxUSE_COMBOBOX |
| 2779 | { |
| 2780 | return wxNOT_FOUND; |
| 2781 | } |
| 2782 | |
| 2783 | // we get here only if just added the entry for this type, so return |
| 2784 | // the last index |
| 2785 | index = m_typeinfo.GetCount() - 1; |
| 2786 | } |
| 2787 | |
| 2788 | return index; |
| 2789 | } |
| 2790 | |
| 2791 | int wxGridTypeRegistry::FindOrCloneDataType(const wxString& typeName) |
| 2792 | { |
| 2793 | int index = FindDataType(typeName); |
| 2794 | if ( index == wxNOT_FOUND ) |
| 2795 | { |
| 2796 | // the first part of the typename is the "real" type, anything after ':' |
| 2797 | // are the parameters for the renderer |
| 2798 | index = FindDataType(typeName.BeforeFirst(_T(':'))); |
| 2799 | if ( index == wxNOT_FOUND ) |
| 2800 | { |
| 2801 | return wxNOT_FOUND; |
| 2802 | } |
| 2803 | |
| 2804 | wxGridCellRenderer *renderer = GetRenderer(index); |
| 2805 | wxGridCellRenderer *rendererOld = renderer; |
| 2806 | renderer = renderer->Clone(); |
| 2807 | rendererOld->DecRef(); |
| 2808 | |
| 2809 | wxGridCellEditor *editor = GetEditor(index); |
| 2810 | wxGridCellEditor *editorOld = editor; |
| 2811 | editor = editor->Clone(); |
| 2812 | editorOld->DecRef(); |
| 2813 | |
| 2814 | // do it even if there are no parameters to reset them to defaults |
| 2815 | wxString params = typeName.AfterFirst(_T(':')); |
| 2816 | renderer->SetParameters(params); |
| 2817 | editor->SetParameters(params); |
| 2818 | |
| 2819 | // register the new typename |
| 2820 | RegisterDataType(typeName, renderer, editor); |
| 2821 | |
| 2822 | // we just registered it, it's the last one |
| 2823 | index = m_typeinfo.GetCount() - 1; |
| 2824 | } |
| 2825 | |
| 2826 | return index; |
| 2827 | } |
| 2828 | |
| 2829 | wxGridCellRenderer* wxGridTypeRegistry::GetRenderer(int index) |
| 2830 | { |
| 2831 | wxGridCellRenderer* renderer = m_typeinfo[index]->m_renderer; |
| 2832 | if (renderer) |
| 2833 | renderer->IncRef(); |
| 2834 | return renderer; |
| 2835 | } |
| 2836 | |
| 2837 | wxGridCellEditor* wxGridTypeRegistry::GetEditor(int index) |
| 2838 | { |
| 2839 | wxGridCellEditor* editor = m_typeinfo[index]->m_editor; |
| 2840 | if (editor) |
| 2841 | editor->IncRef(); |
| 2842 | return editor; |
| 2843 | } |
| 2844 | |
| 2845 | // ---------------------------------------------------------------------------- |
| 2846 | // wxGridTableBase |
| 2847 | // ---------------------------------------------------------------------------- |
| 2848 | |
| 2849 | IMPLEMENT_ABSTRACT_CLASS( wxGridTableBase, wxObject ) |
| 2850 | |
| 2851 | |
| 2852 | wxGridTableBase::wxGridTableBase() |
| 2853 | { |
| 2854 | m_view = (wxGrid *) NULL; |
| 2855 | m_attrProvider = (wxGridCellAttrProvider *) NULL; |
| 2856 | } |
| 2857 | |
| 2858 | wxGridTableBase::~wxGridTableBase() |
| 2859 | { |
| 2860 | delete m_attrProvider; |
| 2861 | } |
| 2862 | |
| 2863 | void wxGridTableBase::SetAttrProvider(wxGridCellAttrProvider *attrProvider) |
| 2864 | { |
| 2865 | delete m_attrProvider; |
| 2866 | m_attrProvider = attrProvider; |
| 2867 | } |
| 2868 | |
| 2869 | bool wxGridTableBase::CanHaveAttributes() |
| 2870 | { |
| 2871 | if ( ! GetAttrProvider() ) |
| 2872 | { |
| 2873 | // use the default attr provider by default |
| 2874 | SetAttrProvider(new wxGridCellAttrProvider); |
| 2875 | } |
| 2876 | return true; |
| 2877 | } |
| 2878 | |
| 2879 | wxGridCellAttr *wxGridTableBase::GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) |
| 2880 | { |
| 2881 | if ( m_attrProvider ) |
| 2882 | return m_attrProvider->GetAttr(row, col, kind); |
| 2883 | else |
| 2884 | return (wxGridCellAttr *)NULL; |
| 2885 | } |
| 2886 | |
| 2887 | void wxGridTableBase::SetAttr(wxGridCellAttr* attr, int row, int col) |
| 2888 | { |
| 2889 | if ( m_attrProvider ) |
| 2890 | { |
| 2891 | attr->SetKind(wxGridCellAttr::Cell); |
| 2892 | m_attrProvider->SetAttr(attr, row, col); |
| 2893 | } |
| 2894 | else |
| 2895 | { |
| 2896 | // as we take ownership of the pointer and don't store it, we must |
| 2897 | // free it now |
| 2898 | wxSafeDecRef(attr); |
| 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | void wxGridTableBase::SetRowAttr(wxGridCellAttr *attr, int row) |
| 2903 | { |
| 2904 | if ( m_attrProvider ) |
| 2905 | { |
| 2906 | attr->SetKind(wxGridCellAttr::Row); |
| 2907 | m_attrProvider->SetRowAttr(attr, row); |
| 2908 | } |
| 2909 | else |
| 2910 | { |
| 2911 | // as we take ownership of the pointer and don't store it, we must |
| 2912 | // free it now |
| 2913 | wxSafeDecRef(attr); |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | void wxGridTableBase::SetColAttr(wxGridCellAttr *attr, int col) |
| 2918 | { |
| 2919 | if ( m_attrProvider ) |
| 2920 | { |
| 2921 | attr->SetKind(wxGridCellAttr::Col); |
| 2922 | m_attrProvider->SetColAttr(attr, col); |
| 2923 | } |
| 2924 | else |
| 2925 | { |
| 2926 | // as we take ownership of the pointer and don't store it, we must |
| 2927 | // free it now |
| 2928 | wxSafeDecRef(attr); |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | bool wxGridTableBase::InsertRows( size_t WXUNUSED(pos), |
| 2933 | size_t WXUNUSED(numRows) ) |
| 2934 | { |
| 2935 | wxFAIL_MSG( wxT("Called grid table class function InsertRows\nbut your derived table class does not override this function") ); |
| 2936 | |
| 2937 | return false; |
| 2938 | } |
| 2939 | |
| 2940 | bool wxGridTableBase::AppendRows( size_t WXUNUSED(numRows) ) |
| 2941 | { |
| 2942 | wxFAIL_MSG( wxT("Called grid table class function AppendRows\nbut your derived table class does not override this function")); |
| 2943 | |
| 2944 | return false; |
| 2945 | } |
| 2946 | |
| 2947 | bool wxGridTableBase::DeleteRows( size_t WXUNUSED(pos), |
| 2948 | size_t WXUNUSED(numRows) ) |
| 2949 | { |
| 2950 | wxFAIL_MSG( wxT("Called grid table class function DeleteRows\nbut your derived table class does not override this function")); |
| 2951 | |
| 2952 | return false; |
| 2953 | } |
| 2954 | |
| 2955 | bool wxGridTableBase::InsertCols( size_t WXUNUSED(pos), |
| 2956 | size_t WXUNUSED(numCols) ) |
| 2957 | { |
| 2958 | wxFAIL_MSG( wxT("Called grid table class function InsertCols\nbut your derived table class does not override this function")); |
| 2959 | |
| 2960 | return false; |
| 2961 | } |
| 2962 | |
| 2963 | bool wxGridTableBase::AppendCols( size_t WXUNUSED(numCols) ) |
| 2964 | { |
| 2965 | wxFAIL_MSG(wxT("Called grid table class function AppendCols\nbut your derived table class does not override this function")); |
| 2966 | |
| 2967 | return false; |
| 2968 | } |
| 2969 | |
| 2970 | bool wxGridTableBase::DeleteCols( size_t WXUNUSED(pos), |
| 2971 | size_t WXUNUSED(numCols) ) |
| 2972 | { |
| 2973 | wxFAIL_MSG( wxT("Called grid table class function DeleteCols\nbut your derived table class does not override this function")); |
| 2974 | |
| 2975 | return false; |
| 2976 | } |
| 2977 | |
| 2978 | |
| 2979 | wxString wxGridTableBase::GetRowLabelValue( int row ) |
| 2980 | { |
| 2981 | wxString s; |
| 2982 | s << row + 1; // RD: Starting the rows at zero confuses users, no matter |
| 2983 | // how much it makes sense to us geeks. |
| 2984 | return s; |
| 2985 | } |
| 2986 | |
| 2987 | wxString wxGridTableBase::GetColLabelValue( int col ) |
| 2988 | { |
| 2989 | // default col labels are: |
| 2990 | // cols 0 to 25 : A-Z |
| 2991 | // cols 26 to 675 : AA-ZZ |
| 2992 | // etc. |
| 2993 | |
| 2994 | wxString s; |
| 2995 | unsigned int i, n; |
| 2996 | for ( n = 1; ; n++ ) |
| 2997 | { |
| 2998 | s += (wxChar) (_T('A') + (wxChar)( col%26 )); |
| 2999 | col = col/26 - 1; |
| 3000 | if ( col < 0 ) break; |
| 3001 | } |
| 3002 | |
| 3003 | // reverse the string... |
| 3004 | wxString s2; |
| 3005 | for ( i = 0; i < n; i++ ) |
| 3006 | { |
| 3007 | s2 += s[n-i-1]; |
| 3008 | } |
| 3009 | |
| 3010 | return s2; |
| 3011 | } |
| 3012 | |
| 3013 | |
| 3014 | wxString wxGridTableBase::GetTypeName( int WXUNUSED(row), int WXUNUSED(col) ) |
| 3015 | { |
| 3016 | return wxGRID_VALUE_STRING; |
| 3017 | } |
| 3018 | |
| 3019 | bool wxGridTableBase::CanGetValueAs( int WXUNUSED(row), int WXUNUSED(col), |
| 3020 | const wxString& typeName ) |
| 3021 | { |
| 3022 | return typeName == wxGRID_VALUE_STRING; |
| 3023 | } |
| 3024 | |
| 3025 | bool wxGridTableBase::CanSetValueAs( int row, int col, const wxString& typeName ) |
| 3026 | { |
| 3027 | return CanGetValueAs(row, col, typeName); |
| 3028 | } |
| 3029 | |
| 3030 | long wxGridTableBase::GetValueAsLong( int WXUNUSED(row), int WXUNUSED(col) ) |
| 3031 | { |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
| 3035 | double wxGridTableBase::GetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col) ) |
| 3036 | { |
| 3037 | return 0.0; |
| 3038 | } |
| 3039 | |
| 3040 | bool wxGridTableBase::GetValueAsBool( int WXUNUSED(row), int WXUNUSED(col) ) |
| 3041 | { |
| 3042 | return false; |
| 3043 | } |
| 3044 | |
| 3045 | void wxGridTableBase::SetValueAsLong( int WXUNUSED(row), int WXUNUSED(col), |
| 3046 | long WXUNUSED(value) ) |
| 3047 | { |
| 3048 | } |
| 3049 | |
| 3050 | void wxGridTableBase::SetValueAsDouble( int WXUNUSED(row), int WXUNUSED(col), |
| 3051 | double WXUNUSED(value) ) |
| 3052 | { |
| 3053 | } |
| 3054 | |
| 3055 | void wxGridTableBase::SetValueAsBool( int WXUNUSED(row), int WXUNUSED(col), |
| 3056 | bool WXUNUSED(value) ) |
| 3057 | { |
| 3058 | } |
| 3059 | |
| 3060 | |
| 3061 | void* wxGridTableBase::GetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), |
| 3062 | const wxString& WXUNUSED(typeName) ) |
| 3063 | { |
| 3064 | return NULL; |
| 3065 | } |
| 3066 | |
| 3067 | void wxGridTableBase::SetValueAsCustom( int WXUNUSED(row), int WXUNUSED(col), |
| 3068 | const wxString& WXUNUSED(typeName), |
| 3069 | void* WXUNUSED(value) ) |
| 3070 | { |
| 3071 | } |
| 3072 | |
| 3073 | ////////////////////////////////////////////////////////////////////// |
| 3074 | // |
| 3075 | // Message class for the grid table to send requests and notifications |
| 3076 | // to the grid view |
| 3077 | // |
| 3078 | |
| 3079 | wxGridTableMessage::wxGridTableMessage() |
| 3080 | { |
| 3081 | m_table = (wxGridTableBase *) NULL; |
| 3082 | m_id = -1; |
| 3083 | m_comInt1 = -1; |
| 3084 | m_comInt2 = -1; |
| 3085 | } |
| 3086 | |
| 3087 | wxGridTableMessage::wxGridTableMessage( wxGridTableBase *table, int id, |
| 3088 | int commandInt1, int commandInt2 ) |
| 3089 | { |
| 3090 | m_table = table; |
| 3091 | m_id = id; |
| 3092 | m_comInt1 = commandInt1; |
| 3093 | m_comInt2 = commandInt2; |
| 3094 | } |
| 3095 | |
| 3096 | |
| 3097 | |
| 3098 | ////////////////////////////////////////////////////////////////////// |
| 3099 | // |
| 3100 | // A basic grid table for string data. An object of this class will |
| 3101 | // created by wxGrid if you don't specify an alternative table class. |
| 3102 | // |
| 3103 | |
| 3104 | WX_DEFINE_OBJARRAY(wxGridStringArray) |
| 3105 | |
| 3106 | IMPLEMENT_DYNAMIC_CLASS( wxGridStringTable, wxGridTableBase ) |
| 3107 | |
| 3108 | wxGridStringTable::wxGridStringTable() |
| 3109 | : wxGridTableBase() |
| 3110 | { |
| 3111 | } |
| 3112 | |
| 3113 | wxGridStringTable::wxGridStringTable( int numRows, int numCols ) |
| 3114 | : wxGridTableBase() |
| 3115 | { |
| 3116 | m_data.Alloc( numRows ); |
| 3117 | |
| 3118 | wxArrayString sa; |
| 3119 | sa.Alloc( numCols ); |
| 3120 | sa.Add( wxEmptyString, numCols ); |
| 3121 | |
| 3122 | m_data.Add( sa, numRows ); |
| 3123 | } |
| 3124 | |
| 3125 | wxGridStringTable::~wxGridStringTable() |
| 3126 | { |
| 3127 | } |
| 3128 | |
| 3129 | int wxGridStringTable::GetNumberRows() |
| 3130 | { |
| 3131 | return m_data.GetCount(); |
| 3132 | } |
| 3133 | |
| 3134 | int wxGridStringTable::GetNumberCols() |
| 3135 | { |
| 3136 | if ( m_data.GetCount() > 0 ) |
| 3137 | return m_data[0].GetCount(); |
| 3138 | else |
| 3139 | return 0; |
| 3140 | } |
| 3141 | |
| 3142 | wxString wxGridStringTable::GetValue( int row, int col ) |
| 3143 | { |
| 3144 | wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
| 3145 | wxEmptyString, |
| 3146 | _T("invalid row or column index in wxGridStringTable") ); |
| 3147 | |
| 3148 | return m_data[row][col]; |
| 3149 | } |
| 3150 | |
| 3151 | void wxGridStringTable::SetValue( int row, int col, const wxString& value ) |
| 3152 | { |
| 3153 | wxCHECK_RET( (row < GetNumberRows()) && (col < GetNumberCols()), |
| 3154 | _T("invalid row or column index in wxGridStringTable") ); |
| 3155 | |
| 3156 | m_data[row][col] = value; |
| 3157 | } |
| 3158 | |
| 3159 | bool wxGridStringTable::IsEmptyCell( int row, int col ) |
| 3160 | { |
| 3161 | wxCHECK_MSG( (row < GetNumberRows()) && (col < GetNumberCols()), |
| 3162 | true, |
| 3163 | _T("invalid row or column index in wxGridStringTable") ); |
| 3164 | |
| 3165 | return (m_data[row][col] == wxEmptyString); |
| 3166 | } |
| 3167 | |
| 3168 | void wxGridStringTable::Clear() |
| 3169 | { |
| 3170 | int row, col; |
| 3171 | int numRows, numCols; |
| 3172 | |
| 3173 | numRows = m_data.GetCount(); |
| 3174 | if ( numRows > 0 ) |
| 3175 | { |
| 3176 | numCols = m_data[0].GetCount(); |
| 3177 | |
| 3178 | for ( row = 0; row < numRows; row++ ) |
| 3179 | { |
| 3180 | for ( col = 0; col < numCols; col++ ) |
| 3181 | { |
| 3182 | m_data[row][col] = wxEmptyString; |
| 3183 | } |
| 3184 | } |
| 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | |
| 3189 | bool wxGridStringTable::InsertRows( size_t pos, size_t numRows ) |
| 3190 | { |
| 3191 | size_t curNumRows = m_data.GetCount(); |
| 3192 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
| 3193 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); |
| 3194 | |
| 3195 | if ( pos >= curNumRows ) |
| 3196 | { |
| 3197 | return AppendRows( numRows ); |
| 3198 | } |
| 3199 | |
| 3200 | wxArrayString sa; |
| 3201 | sa.Alloc( curNumCols ); |
| 3202 | sa.Add( wxEmptyString, curNumCols ); |
| 3203 | m_data.Insert( sa, pos, numRows ); |
| 3204 | if ( GetView() ) |
| 3205 | { |
| 3206 | wxGridTableMessage msg( this, |
| 3207 | wxGRIDTABLE_NOTIFY_ROWS_INSERTED, |
| 3208 | pos, |
| 3209 | numRows ); |
| 3210 | |
| 3211 | GetView()->ProcessTableMessage( msg ); |
| 3212 | } |
| 3213 | |
| 3214 | return true; |
| 3215 | } |
| 3216 | |
| 3217 | bool wxGridStringTable::AppendRows( size_t numRows ) |
| 3218 | { |
| 3219 | size_t curNumRows = m_data.GetCount(); |
| 3220 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
| 3221 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); |
| 3222 | |
| 3223 | wxArrayString sa; |
| 3224 | if ( curNumCols > 0 ) |
| 3225 | { |
| 3226 | sa.Alloc( curNumCols ); |
| 3227 | sa.Add( wxEmptyString, curNumCols ); |
| 3228 | } |
| 3229 | |
| 3230 | m_data.Add( sa, numRows ); |
| 3231 | |
| 3232 | if ( GetView() ) |
| 3233 | { |
| 3234 | wxGridTableMessage msg( this, |
| 3235 | wxGRIDTABLE_NOTIFY_ROWS_APPENDED, |
| 3236 | numRows ); |
| 3237 | |
| 3238 | GetView()->ProcessTableMessage( msg ); |
| 3239 | } |
| 3240 | |
| 3241 | return true; |
| 3242 | } |
| 3243 | |
| 3244 | bool wxGridStringTable::DeleteRows( size_t pos, size_t numRows ) |
| 3245 | { |
| 3246 | size_t curNumRows = m_data.GetCount(); |
| 3247 | |
| 3248 | if ( pos >= curNumRows ) |
| 3249 | { |
| 3250 | wxFAIL_MSG( wxString::Format |
| 3251 | ( |
| 3252 | wxT("Called wxGridStringTable::DeleteRows(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu rows"), |
| 3253 | (unsigned long)pos, |
| 3254 | (unsigned long)numRows, |
| 3255 | (unsigned long)curNumRows |
| 3256 | ) ); |
| 3257 | |
| 3258 | return false; |
| 3259 | } |
| 3260 | |
| 3261 | if ( numRows > curNumRows - pos ) |
| 3262 | { |
| 3263 | numRows = curNumRows - pos; |
| 3264 | } |
| 3265 | |
| 3266 | if ( numRows >= curNumRows ) |
| 3267 | { |
| 3268 | m_data.Clear(); |
| 3269 | } |
| 3270 | else |
| 3271 | { |
| 3272 | m_data.RemoveAt( pos, numRows ); |
| 3273 | } |
| 3274 | if ( GetView() ) |
| 3275 | { |
| 3276 | wxGridTableMessage msg( this, |
| 3277 | wxGRIDTABLE_NOTIFY_ROWS_DELETED, |
| 3278 | pos, |
| 3279 | numRows ); |
| 3280 | |
| 3281 | GetView()->ProcessTableMessage( msg ); |
| 3282 | } |
| 3283 | |
| 3284 | return true; |
| 3285 | } |
| 3286 | |
| 3287 | bool wxGridStringTable::InsertCols( size_t pos, size_t numCols ) |
| 3288 | { |
| 3289 | size_t row, col; |
| 3290 | |
| 3291 | size_t curNumRows = m_data.GetCount(); |
| 3292 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
| 3293 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); |
| 3294 | |
| 3295 | if ( pos >= curNumCols ) |
| 3296 | { |
| 3297 | return AppendCols( numCols ); |
| 3298 | } |
| 3299 | |
| 3300 | for ( row = 0; row < curNumRows; row++ ) |
| 3301 | { |
| 3302 | for ( col = pos; col < pos + numCols; col++ ) |
| 3303 | { |
| 3304 | m_data[row].Insert( wxEmptyString, col ); |
| 3305 | } |
| 3306 | } |
| 3307 | if ( GetView() ) |
| 3308 | { |
| 3309 | wxGridTableMessage msg( this, |
| 3310 | wxGRIDTABLE_NOTIFY_COLS_INSERTED, |
| 3311 | pos, |
| 3312 | numCols ); |
| 3313 | |
| 3314 | GetView()->ProcessTableMessage( msg ); |
| 3315 | } |
| 3316 | |
| 3317 | return true; |
| 3318 | } |
| 3319 | |
| 3320 | bool wxGridStringTable::AppendCols( size_t numCols ) |
| 3321 | { |
| 3322 | size_t row; |
| 3323 | |
| 3324 | size_t curNumRows = m_data.GetCount(); |
| 3325 | #if 0 |
| 3326 | if ( !curNumRows ) |
| 3327 | { |
| 3328 | // TODO: something better than this ? |
| 3329 | // |
| 3330 | wxFAIL_MSG( wxT("Unable to append cols to a grid table with no rows.\nCall AppendRows() first") ); |
| 3331 | return false; |
| 3332 | } |
| 3333 | #endif |
| 3334 | |
| 3335 | for ( row = 0; row < curNumRows; row++ ) |
| 3336 | { |
| 3337 | m_data[row].Add( wxEmptyString, numCols ); |
| 3338 | } |
| 3339 | |
| 3340 | if ( GetView() ) |
| 3341 | { |
| 3342 | wxGridTableMessage msg( this, |
| 3343 | wxGRIDTABLE_NOTIFY_COLS_APPENDED, |
| 3344 | numCols ); |
| 3345 | |
| 3346 | GetView()->ProcessTableMessage( msg ); |
| 3347 | } |
| 3348 | |
| 3349 | return true; |
| 3350 | } |
| 3351 | |
| 3352 | bool wxGridStringTable::DeleteCols( size_t pos, size_t numCols ) |
| 3353 | { |
| 3354 | size_t row; |
| 3355 | |
| 3356 | size_t curNumRows = m_data.GetCount(); |
| 3357 | size_t curNumCols = ( curNumRows > 0 ? m_data[0].GetCount() : |
| 3358 | ( GetView() ? GetView()->GetNumberCols() : 0 ) ); |
| 3359 | |
| 3360 | if ( pos >= curNumCols ) |
| 3361 | { |
| 3362 | wxFAIL_MSG( wxString::Format |
| 3363 | ( |
| 3364 | wxT("Called wxGridStringTable::DeleteCols(pos=%lu, N=%lu)\nPos value is invalid for present table with %lu cols"), |
| 3365 | (unsigned long)pos, |
| 3366 | (unsigned long)numCols, |
| 3367 | (unsigned long)curNumCols |
| 3368 | ) ); |
| 3369 | return false; |
| 3370 | } |
| 3371 | |
| 3372 | if ( numCols > curNumCols - pos ) |
| 3373 | { |
| 3374 | numCols = curNumCols - pos; |
| 3375 | } |
| 3376 | |
| 3377 | for ( row = 0; row < curNumRows; row++ ) |
| 3378 | { |
| 3379 | if ( numCols >= curNumCols ) |
| 3380 | { |
| 3381 | m_data[row].Clear(); |
| 3382 | } |
| 3383 | else |
| 3384 | { |
| 3385 | m_data[row].RemoveAt( pos, numCols ); |
| 3386 | } |
| 3387 | } |
| 3388 | if ( GetView() ) |
| 3389 | { |
| 3390 | wxGridTableMessage msg( this, |
| 3391 | wxGRIDTABLE_NOTIFY_COLS_DELETED, |
| 3392 | pos, |
| 3393 | numCols ); |
| 3394 | |
| 3395 | GetView()->ProcessTableMessage( msg ); |
| 3396 | } |
| 3397 | |
| 3398 | return true; |
| 3399 | } |
| 3400 | |
| 3401 | wxString wxGridStringTable::GetRowLabelValue( int row ) |
| 3402 | { |
| 3403 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) |
| 3404 | { |
| 3405 | // using default label |
| 3406 | // |
| 3407 | return wxGridTableBase::GetRowLabelValue( row ); |
| 3408 | } |
| 3409 | else |
| 3410 | { |
| 3411 | return m_rowLabels[ row ]; |
| 3412 | } |
| 3413 | } |
| 3414 | |
| 3415 | wxString wxGridStringTable::GetColLabelValue( int col ) |
| 3416 | { |
| 3417 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) |
| 3418 | { |
| 3419 | // using default label |
| 3420 | // |
| 3421 | return wxGridTableBase::GetColLabelValue( col ); |
| 3422 | } |
| 3423 | else |
| 3424 | { |
| 3425 | return m_colLabels[ col ]; |
| 3426 | } |
| 3427 | } |
| 3428 | |
| 3429 | void wxGridStringTable::SetRowLabelValue( int row, const wxString& value ) |
| 3430 | { |
| 3431 | if ( row > (int)(m_rowLabels.GetCount()) - 1 ) |
| 3432 | { |
| 3433 | int n = m_rowLabels.GetCount(); |
| 3434 | int i; |
| 3435 | for ( i = n; i <= row; i++ ) |
| 3436 | { |
| 3437 | m_rowLabels.Add( wxGridTableBase::GetRowLabelValue(i) ); |
| 3438 | } |
| 3439 | } |
| 3440 | |
| 3441 | m_rowLabels[row] = value; |
| 3442 | } |
| 3443 | |
| 3444 | void wxGridStringTable::SetColLabelValue( int col, const wxString& value ) |
| 3445 | { |
| 3446 | if ( col > (int)(m_colLabels.GetCount()) - 1 ) |
| 3447 | { |
| 3448 | int n = m_colLabels.GetCount(); |
| 3449 | int i; |
| 3450 | for ( i = n; i <= col; i++ ) |
| 3451 | { |
| 3452 | m_colLabels.Add( wxGridTableBase::GetColLabelValue(i) ); |
| 3453 | } |
| 3454 | } |
| 3455 | |
| 3456 | m_colLabels[col] = value; |
| 3457 | } |
| 3458 | |
| 3459 | |
| 3460 | |
| 3461 | ////////////////////////////////////////////////////////////////////// |
| 3462 | ////////////////////////////////////////////////////////////////////// |
| 3463 | |
| 3464 | IMPLEMENT_DYNAMIC_CLASS( wxGridRowLabelWindow, wxWindow ) |
| 3465 | |
| 3466 | BEGIN_EVENT_TABLE( wxGridRowLabelWindow, wxWindow ) |
| 3467 | EVT_PAINT( wxGridRowLabelWindow::OnPaint ) |
| 3468 | EVT_MOUSEWHEEL( wxGridRowLabelWindow::OnMouseWheel) |
| 3469 | EVT_MOUSE_EVENTS( wxGridRowLabelWindow::OnMouseEvent ) |
| 3470 | EVT_KEY_DOWN( wxGridRowLabelWindow::OnKeyDown ) |
| 3471 | EVT_KEY_UP( wxGridRowLabelWindow::OnKeyUp ) |
| 3472 | END_EVENT_TABLE() |
| 3473 | |
| 3474 | wxGridRowLabelWindow::wxGridRowLabelWindow( wxGrid *parent, |
| 3475 | wxWindowID id, |
| 3476 | const wxPoint &pos, const wxSize &size ) |
| 3477 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE ) |
| 3478 | { |
| 3479 | m_owner = parent; |
| 3480 | } |
| 3481 | |
| 3482 | void wxGridRowLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
| 3483 | { |
| 3484 | wxPaintDC dc(this); |
| 3485 | |
| 3486 | // NO - don't do this because it will set both the x and y origin |
| 3487 | // coords to match the parent scrolled window and we just want to |
| 3488 | // set the y coord - MB |
| 3489 | // |
| 3490 | // m_owner->PrepareDC( dc ); |
| 3491 | |
| 3492 | int x, y; |
| 3493 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
| 3494 | dc.SetDeviceOrigin( 0, -y ); |
| 3495 | |
| 3496 | wxArrayInt rows = m_owner->CalcRowLabelsExposed( GetUpdateRegion() ); |
| 3497 | m_owner->DrawRowLabels( dc , rows ); |
| 3498 | } |
| 3499 | |
| 3500 | |
| 3501 | void wxGridRowLabelWindow::OnMouseEvent( wxMouseEvent& event ) |
| 3502 | { |
| 3503 | m_owner->ProcessRowLabelMouseEvent( event ); |
| 3504 | } |
| 3505 | |
| 3506 | |
| 3507 | void wxGridRowLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
| 3508 | { |
| 3509 | m_owner->GetEventHandler()->ProcessEvent(event); |
| 3510 | } |
| 3511 | |
| 3512 | |
| 3513 | // This seems to be required for wxMotif otherwise the mouse |
| 3514 | // cursor must be in the cell edit control to get key events |
| 3515 | // |
| 3516 | void wxGridRowLabelWindow::OnKeyDown( wxKeyEvent& event ) |
| 3517 | { |
| 3518 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
| 3519 | } |
| 3520 | |
| 3521 | void wxGridRowLabelWindow::OnKeyUp( wxKeyEvent& event ) |
| 3522 | { |
| 3523 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
| 3524 | } |
| 3525 | |
| 3526 | |
| 3527 | |
| 3528 | ////////////////////////////////////////////////////////////////////// |
| 3529 | |
| 3530 | IMPLEMENT_DYNAMIC_CLASS( wxGridColLabelWindow, wxWindow ) |
| 3531 | |
| 3532 | BEGIN_EVENT_TABLE( wxGridColLabelWindow, wxWindow ) |
| 3533 | EVT_PAINT( wxGridColLabelWindow::OnPaint ) |
| 3534 | EVT_MOUSEWHEEL( wxGridColLabelWindow::OnMouseWheel) |
| 3535 | EVT_MOUSE_EVENTS( wxGridColLabelWindow::OnMouseEvent ) |
| 3536 | EVT_KEY_DOWN( wxGridColLabelWindow::OnKeyDown ) |
| 3537 | EVT_KEY_UP( wxGridColLabelWindow::OnKeyUp ) |
| 3538 | END_EVENT_TABLE() |
| 3539 | |
| 3540 | wxGridColLabelWindow::wxGridColLabelWindow( wxGrid *parent, |
| 3541 | wxWindowID id, |
| 3542 | const wxPoint &pos, const wxSize &size ) |
| 3543 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE ) |
| 3544 | { |
| 3545 | m_owner = parent; |
| 3546 | } |
| 3547 | |
| 3548 | void wxGridColLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
| 3549 | { |
| 3550 | wxPaintDC dc(this); |
| 3551 | |
| 3552 | // NO - don't do this because it will set both the x and y origin |
| 3553 | // coords to match the parent scrolled window and we just want to |
| 3554 | // set the x coord - MB |
| 3555 | // |
| 3556 | // m_owner->PrepareDC( dc ); |
| 3557 | |
| 3558 | int x, y; |
| 3559 | m_owner->CalcUnscrolledPosition( 0, 0, &x, &y ); |
| 3560 | dc.SetDeviceOrigin( -x, 0 ); |
| 3561 | |
| 3562 | wxArrayInt cols = m_owner->CalcColLabelsExposed( GetUpdateRegion() ); |
| 3563 | m_owner->DrawColLabels( dc , cols ); |
| 3564 | } |
| 3565 | |
| 3566 | |
| 3567 | void wxGridColLabelWindow::OnMouseEvent( wxMouseEvent& event ) |
| 3568 | { |
| 3569 | m_owner->ProcessColLabelMouseEvent( event ); |
| 3570 | } |
| 3571 | |
| 3572 | void wxGridColLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
| 3573 | { |
| 3574 | m_owner->GetEventHandler()->ProcessEvent(event); |
| 3575 | } |
| 3576 | |
| 3577 | |
| 3578 | // This seems to be required for wxMotif otherwise the mouse |
| 3579 | // cursor must be in the cell edit control to get key events |
| 3580 | // |
| 3581 | void wxGridColLabelWindow::OnKeyDown( wxKeyEvent& event ) |
| 3582 | { |
| 3583 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
| 3584 | } |
| 3585 | |
| 3586 | void wxGridColLabelWindow::OnKeyUp( wxKeyEvent& event ) |
| 3587 | { |
| 3588 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
| 3589 | } |
| 3590 | |
| 3591 | |
| 3592 | |
| 3593 | ////////////////////////////////////////////////////////////////////// |
| 3594 | |
| 3595 | IMPLEMENT_DYNAMIC_CLASS( wxGridCornerLabelWindow, wxWindow ) |
| 3596 | |
| 3597 | BEGIN_EVENT_TABLE( wxGridCornerLabelWindow, wxWindow ) |
| 3598 | EVT_MOUSEWHEEL( wxGridCornerLabelWindow::OnMouseWheel) |
| 3599 | EVT_MOUSE_EVENTS( wxGridCornerLabelWindow::OnMouseEvent ) |
| 3600 | EVT_PAINT( wxGridCornerLabelWindow::OnPaint) |
| 3601 | EVT_KEY_DOWN( wxGridCornerLabelWindow::OnKeyDown ) |
| 3602 | EVT_KEY_UP( wxGridCornerLabelWindow::OnKeyUp ) |
| 3603 | END_EVENT_TABLE() |
| 3604 | |
| 3605 | wxGridCornerLabelWindow::wxGridCornerLabelWindow( wxGrid *parent, |
| 3606 | wxWindowID id, |
| 3607 | const wxPoint &pos, const wxSize &size ) |
| 3608 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS|wxBORDER_NONE|wxFULL_REPAINT_ON_RESIZE ) |
| 3609 | { |
| 3610 | m_owner = parent; |
| 3611 | } |
| 3612 | |
| 3613 | void wxGridCornerLabelWindow::OnPaint( wxPaintEvent& WXUNUSED(event) ) |
| 3614 | { |
| 3615 | wxPaintDC dc(this); |
| 3616 | |
| 3617 | int client_height = 0; |
| 3618 | int client_width = 0; |
| 3619 | GetClientSize( &client_width, &client_height ); |
| 3620 | |
| 3621 | dc.SetPen( wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DDKSHADOW),1, wxSOLID) ); |
| 3622 | dc.DrawLine( client_width-1, client_height-1, client_width-1, 0 ); |
| 3623 | dc.DrawLine( client_width-1, client_height-1, 0, client_height-1 ); |
| 3624 | dc.DrawLine( 0, 0, client_width, 0 ); |
| 3625 | dc.DrawLine( 0, 0, 0, client_height ); |
| 3626 | |
| 3627 | dc.SetPen( *wxWHITE_PEN ); |
| 3628 | dc.DrawLine( 1, 1, client_width-1, 1 ); |
| 3629 | dc.DrawLine( 1, 1, 1, client_height-1 ); |
| 3630 | } |
| 3631 | |
| 3632 | |
| 3633 | void wxGridCornerLabelWindow::OnMouseEvent( wxMouseEvent& event ) |
| 3634 | { |
| 3635 | m_owner->ProcessCornerLabelMouseEvent( event ); |
| 3636 | } |
| 3637 | |
| 3638 | |
| 3639 | void wxGridCornerLabelWindow::OnMouseWheel( wxMouseEvent& event ) |
| 3640 | { |
| 3641 | m_owner->GetEventHandler()->ProcessEvent(event); |
| 3642 | } |
| 3643 | |
| 3644 | // This seems to be required for wxMotif otherwise the mouse |
| 3645 | // cursor must be in the cell edit control to get key events |
| 3646 | // |
| 3647 | void wxGridCornerLabelWindow::OnKeyDown( wxKeyEvent& event ) |
| 3648 | { |
| 3649 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
| 3650 | } |
| 3651 | |
| 3652 | void wxGridCornerLabelWindow::OnKeyUp( wxKeyEvent& event ) |
| 3653 | { |
| 3654 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
| 3655 | } |
| 3656 | |
| 3657 | |
| 3658 | |
| 3659 | ////////////////////////////////////////////////////////////////////// |
| 3660 | |
| 3661 | IMPLEMENT_DYNAMIC_CLASS( wxGridWindow, wxWindow ) |
| 3662 | |
| 3663 | BEGIN_EVENT_TABLE( wxGridWindow, wxWindow ) |
| 3664 | EVT_PAINT( wxGridWindow::OnPaint ) |
| 3665 | EVT_MOUSEWHEEL( wxGridWindow::OnMouseWheel) |
| 3666 | EVT_MOUSE_EVENTS( wxGridWindow::OnMouseEvent ) |
| 3667 | EVT_KEY_DOWN( wxGridWindow::OnKeyDown ) |
| 3668 | EVT_KEY_UP( wxGridWindow::OnKeyUp ) |
| 3669 | EVT_SET_FOCUS( wxGridWindow::OnFocus ) |
| 3670 | EVT_KILL_FOCUS( wxGridWindow::OnFocus ) |
| 3671 | EVT_ERASE_BACKGROUND( wxGridWindow::OnEraseBackground ) |
| 3672 | END_EVENT_TABLE() |
| 3673 | |
| 3674 | wxGridWindow::wxGridWindow( wxGrid *parent, |
| 3675 | wxGridRowLabelWindow *rowLblWin, |
| 3676 | wxGridColLabelWindow *colLblWin, |
| 3677 | wxWindowID id, |
| 3678 | const wxPoint &pos, |
| 3679 | const wxSize &size ) |
| 3680 | : wxWindow( parent, id, pos, size, wxWANTS_CHARS | wxBORDER_NONE | wxCLIP_CHILDREN|wxFULL_REPAINT_ON_RESIZE, |
| 3681 | wxT("grid window") ) |
| 3682 | |
| 3683 | { |
| 3684 | m_owner = parent; |
| 3685 | m_rowLabelWin = rowLblWin; |
| 3686 | m_colLabelWin = colLblWin; |
| 3687 | } |
| 3688 | |
| 3689 | |
| 3690 | wxGridWindow::~wxGridWindow() |
| 3691 | { |
| 3692 | } |
| 3693 | |
| 3694 | |
| 3695 | void wxGridWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
| 3696 | { |
| 3697 | wxPaintDC dc( this ); |
| 3698 | m_owner->PrepareDC( dc ); |
| 3699 | wxRegion reg = GetUpdateRegion(); |
| 3700 | wxGridCellCoordsArray DirtyCells = m_owner->CalcCellsExposed( reg ); |
| 3701 | m_owner->DrawGridCellArea( dc , DirtyCells); |
| 3702 | #if WXGRID_DRAW_LINES |
| 3703 | m_owner->DrawAllGridLines( dc, reg ); |
| 3704 | #endif |
| 3705 | m_owner->DrawGridSpace( dc ); |
| 3706 | m_owner->DrawHighlight( dc , DirtyCells ); |
| 3707 | } |
| 3708 | |
| 3709 | |
| 3710 | void wxGridWindow::ScrollWindow( int dx, int dy, const wxRect *rect ) |
| 3711 | { |
| 3712 | wxWindow::ScrollWindow( dx, dy, rect ); |
| 3713 | m_rowLabelWin->ScrollWindow( 0, dy, rect ); |
| 3714 | m_colLabelWin->ScrollWindow( dx, 0, rect ); |
| 3715 | } |
| 3716 | |
| 3717 | |
| 3718 | void wxGridWindow::OnMouseEvent( wxMouseEvent& event ) |
| 3719 | { |
| 3720 | m_owner->ProcessGridCellMouseEvent( event ); |
| 3721 | } |
| 3722 | |
| 3723 | void wxGridWindow::OnMouseWheel( wxMouseEvent& event ) |
| 3724 | { |
| 3725 | m_owner->GetEventHandler()->ProcessEvent(event); |
| 3726 | } |
| 3727 | |
| 3728 | // This seems to be required for wxMotif/wxGTK otherwise the mouse |
| 3729 | // cursor must be in the cell edit control to get key events |
| 3730 | // |
| 3731 | void wxGridWindow::OnKeyDown( wxKeyEvent& event ) |
| 3732 | { |
| 3733 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
| 3734 | } |
| 3735 | |
| 3736 | void wxGridWindow::OnKeyUp( wxKeyEvent& event ) |
| 3737 | { |
| 3738 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) event.Skip(); |
| 3739 | } |
| 3740 | |
| 3741 | void wxGridWindow::OnEraseBackground( wxEraseEvent& WXUNUSED(event) ) |
| 3742 | { |
| 3743 | } |
| 3744 | |
| 3745 | void wxGridWindow::OnFocus(wxFocusEvent& event) |
| 3746 | { |
| 3747 | if ( !m_owner->GetEventHandler()->ProcessEvent( event ) ) |
| 3748 | event.Skip(); |
| 3749 | } |
| 3750 | |
| 3751 | ////////////////////////////////////////////////////////////////////// |
| 3752 | |
| 3753 | // Internal Helper function for computing row or column from some |
| 3754 | // (unscrolled) coordinate value, using either |
| 3755 | // m_defaultRowHeight/m_defaultColWidth or binary search on array |
| 3756 | // of m_rowBottoms/m_ColRights to speed up the search! |
| 3757 | |
| 3758 | // Internal helper macros for simpler use of that function |
| 3759 | |
| 3760 | static int CoordToRowOrCol(int coord, int defaultDist, int minDist, |
| 3761 | const wxArrayInt& BorderArray, int nMax, |
| 3762 | bool clipToMinMax); |
| 3763 | |
| 3764 | #define internalXToCol(x) CoordToRowOrCol(x, m_defaultColWidth, \ |
| 3765 | m_minAcceptableColWidth, \ |
| 3766 | m_colRights, m_numCols, true) |
| 3767 | #define internalYToRow(y) CoordToRowOrCol(y, m_defaultRowHeight, \ |
| 3768 | m_minAcceptableRowHeight, \ |
| 3769 | m_rowBottoms, m_numRows, true) |
| 3770 | ///////////////////////////////////////////////////////////////////// |
| 3771 | |
| 3772 | #if wxUSE_EXTENDED_RTTI |
| 3773 | WX_DEFINE_FLAGS( wxGridStyle ) |
| 3774 | |
| 3775 | wxBEGIN_FLAGS( wxGridStyle ) |
| 3776 | // new style border flags, we put them first to |
| 3777 | // use them for streaming out |
| 3778 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
| 3779 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) |
| 3780 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) |
| 3781 | wxFLAGS_MEMBER(wxBORDER_RAISED) |
| 3782 | wxFLAGS_MEMBER(wxBORDER_STATIC) |
| 3783 | wxFLAGS_MEMBER(wxBORDER_NONE) |
| 3784 | |
| 3785 | // old style border flags |
| 3786 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
| 3787 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) |
| 3788 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) |
| 3789 | wxFLAGS_MEMBER(wxRAISED_BORDER) |
| 3790 | wxFLAGS_MEMBER(wxSTATIC_BORDER) |
| 3791 | wxFLAGS_MEMBER(wxBORDER) |
| 3792 | |
| 3793 | // standard window styles |
| 3794 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
| 3795 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) |
| 3796 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) |
| 3797 | wxFLAGS_MEMBER(wxWANTS_CHARS) |
| 3798 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
| 3799 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
| 3800 | wxFLAGS_MEMBER(wxVSCROLL) |
| 3801 | wxFLAGS_MEMBER(wxHSCROLL) |
| 3802 | |
| 3803 | wxEND_FLAGS( wxGridStyle ) |
| 3804 | |
| 3805 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxGrid, wxScrolledWindow,"wx/grid.h") |
| 3806 | |
| 3807 | wxBEGIN_PROPERTIES_TABLE(wxGrid) |
| 3808 | wxHIDE_PROPERTY( Children ) |
| 3809 | wxPROPERTY_FLAGS( WindowStyle , wxGridStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
| 3810 | wxEND_PROPERTIES_TABLE() |
| 3811 | |
| 3812 | wxBEGIN_HANDLERS_TABLE(wxGrid) |
| 3813 | wxEND_HANDLERS_TABLE() |
| 3814 | |
| 3815 | wxCONSTRUCTOR_5( wxGrid , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
| 3816 | |
| 3817 | /* |
| 3818 |