diff --git a/uppsrc/GridCtrl/GridBase.cpp b/uppsrc/GridCtrl/GridBase.cpp index 64d18f291..fdcc40daf 100644 --- a/uppsrc/GridCtrl/GridBase.cpp +++ b/uppsrc/GridCtrl/GridBase.cpp @@ -11,11 +11,12 @@ void GridCtrl::Item::SetCtrl(Ctrl& c) { ctrl = &c; ctrl->Hide(); + ctrl->SetFrame(NullFrame()); ctrl->WantFocus(); -// (*edits)[id].ctrl = &ctrl; + ctrl_flag = IC_MANUAL | IC_INIT; } -void GridCtrl::Item::NoCtrl() +void GridCtrl::Item::ClearCtrl() { ctrl = NULL; } @@ -238,16 +239,29 @@ static void MakeOption(One& ctrl) ctrl->WantFocus(); } +GridCtrl::ItemRect& GridCtrl::ItemRect::Ctrls(Callback1&> _factory) +{ + if(!(*edits)[id].factory) + ++parent->genr_ctrls; + (*edits)[id].factory = _factory; + return *this; +} + +GridCtrl::ItemRect& GridCtrl::ItemRect::NoCtrls() +{ + (*edits)[id].factory.Clear(); + --parent->genr_ctrls; + return *this; +} + GridCtrl::ItemRect& GridCtrl::ItemRect::Option() { -// return Ctrls(MakeOption).CtrlAlignHorzCenter(CtrlsImg::O0().GetSize().cx); return Ctrls(MakeOption).CtrlAlignHorzPos().CtrlAlignVertPos(); } GridCtrl::Item& GridCtrl::Item::Editable(bool b) { editable = b; - //SyncCtrls(id); return *this; } @@ -259,7 +273,6 @@ GridCtrl::Item& GridCtrl::Item::NoEditable() GridCtrl::ItemRect& GridCtrl::ItemRect::Editable(bool b) { editable = b; - parent->SyncCtrls(id); return *this; } diff --git a/uppsrc/GridCtrl/GridCtrl.cpp b/uppsrc/GridCtrl/GridCtrl.cpp index ab62fb1a5..f93df3275 100644 --- a/uppsrc/GridCtrl/GridCtrl.cpp +++ b/uppsrc/GridCtrl/GridCtrl.cpp @@ -99,7 +99,7 @@ GridCtrl::GridCtrl() : holder(*this) one_click_edit = false; coloringMode = 0; ctrls = false; - genr_ctrls = false; + genr_ctrls = 0; edit_ctrls = false; sorting = true; sorting_multicol = true; @@ -2421,10 +2421,10 @@ void GridCtrl::DragAndDrop(Point p, PasteClip& d) */ } -Rect GridCtrl::GetItemRect(int r, int c, bool hgrid, bool vgrid, bool ctrlmode) +Rect GridCtrl::GetItemRect(int r, int c, bool hgrid, bool vgrid, bool relw, bool relh) { - int dx = sbx + (ctrlmode ? fixed_width : 0); - int dy = sby + (ctrlmode ? fixed_height : 0); + int dx = sbx + (relw ? fixed_width : 0); + int dy = sby + (relh ? fixed_height : 0); int idx = hitems[c].id; int idy = vitems[r].id; @@ -2677,6 +2677,22 @@ void GridCtrl::Set(const Vector &v, int data_offset /* = 0*/, int column_ Set(r, v, data_offset, column_offset); } +void GridCtrl::SetCtrl(int r, int c, Ctrl& ctrl) +{ + GetItem(r + fixed_rows, c + fixed_cols).SetCtrl(ctrl); + ++genr_ctrls; +} + +void GridCtrl::ClearCtrl(int r, int c) +{ + GridCtrl::Item& item = GetItem(r + fixed_rows, c + fixed_cols); + if(item.ctrl) + { + item.ClearCtrl(); + --genr_ctrls; + } +} + void GridCtrl::SetCtrlValue(int r, int c, const Value &val) { c += fixed_cols; @@ -4548,7 +4564,8 @@ void GridCtrl::UpdateCtrls(int opt /*= UC_CHECK_VIS | UC_SHOW | UC_CURSOR | UC_F bool dorect = false; bool dofocus = false; - bool factory = edits[id].factory; + bool factory_ctrl = edits[id].factory; + bool manual_ctrl = items[vitems[cp.y].id][id].ctrl_flag & IC_MANUAL; if(show) { @@ -4559,12 +4576,12 @@ void GridCtrl::UpdateCtrls(int opt /*= UC_CHECK_VIS | UC_SHOW | UC_CURSOR | UC_F } if(dorect) { - Rect r = GetItemRect(ctrlpos.y, i, horz_grid, vert_grid, true); + Rect r = GetItemRect(ctrlpos.y, i, horz_grid, vert_grid, true, true); if(!r.Intersects(sz)) r.Set(0, 0, 0, 0); - if(!factory) + if(!factory_ctrl && !manual_ctrl) { ctrl->SetRect(AlignRect(r, i)); ctrl->Show(); @@ -4572,7 +4589,7 @@ void GridCtrl::UpdateCtrls(int opt /*= UC_CHECK_VIS | UC_SHOW | UC_CURSOR | UC_F } } - else if(!factory) + else if(!factory_ctrl && !manual_ctrl) { ctrl->SetRect(0, 0, 0, 0); ctrl->Hide(); @@ -4612,26 +4629,10 @@ void GridCtrl::UpdateCtrls(int opt /*= UC_CHECK_VIS | UC_SHOW | UC_CURSOR | UC_F void GridCtrl::SyncCtrls(int row) { - //if(!HasCtrls()) - // return; - - Size sz = GetSize(); - genr_ctrls = false; - - - Vector cols; - - for(int i = 1; i < total_cols; i++) - if(edits[hitems[i].id].factory && hitems[i].editable) - cols.Add(i); - - if(cols.IsEmpty()) + if(!genr_ctrls) return; - - int dy = sby + fixed_height; - int dx = sbx + fixed_width; - Rect r; + Size sz = GetSize(); int js = row < 0 ? 0 : row; int je = row < 0 ? total_rows : row + 1; @@ -4639,31 +4640,40 @@ void GridCtrl::SyncCtrls(int row) for(int j = js; j < je; j++) { int idy = vitems[j].id; - bool fixed = j < fixed_rows; - bool create = !fixed && vitems[j].editable; + bool fixed_row = j < fixed_rows; + bool create_row = !fixed_row && vitems[j].editable; - for(int i = 0; i < cols.GetCount(); i++) + for(int i = 1; i < total_cols; i++) { - int c = cols[i]; - int idx = hitems[c].id; - int oid = idx; + bool fixed_col = i < fixed_cols; + bool create_col = !fixed_col && hitems[i].editable; + + int idx = hitems[i].id; Item *it = &items[idy][idx]; - + if(it->isjoined) { it = &items[it->idy][it->idx]; idx = it->idx; } - if(!it->ctrl && create && it->editable && edits[idx].factory && !edits[idx].nofactory) + bool manual_ctrl = it->ctrl_flag & IC_MANUAL; + + if(!it->ctrl && create_row && create_col && it->editable && edits[idx].factory) { One newctrl; edits[idx].factory(newctrl); - newctrl->SetData(it->val); - newctrl->WhenAction << Proxy(WhenCtrlsAction); it->ctrl = newctrl.Detach(); - holder.AddChild(it->ctrl); + it->ctrl_flag = IC_FACTORY | IC_INIT; + } + + if(it->ctrl && (it->ctrl_flag & IC_INIT)) + { + it->ctrl->SetData(it->val); + it->ctrl->WhenAction << Proxy(WhenCtrlsAction); + holder.AddChild(it->ctrl); + it->ctrl_flag &= ~IC_INIT; } if(it->ctrl) @@ -4671,10 +4681,10 @@ void GridCtrl::SyncCtrls(int row) if(it->isjoined && it->sync_flag == sync_flag) continue; - r = GetItemRect(j, c, horz_grid, vert_grid, true); - AlignRect(r, c); - - if(r.Intersects(sz) && !fixed && !vitems[j].hidden && !hitems[c].hidden) + Rect r = GetItemRect(j, i, horz_grid, vert_grid, true, true); + AlignRect(r, i); + + if(r.Intersects(sz) && !fixed_col && !fixed_row && !vitems[j].hidden && !hitems[i].hidden) { it->ctrl->SetRect(r); it->ctrl->Show(); @@ -4684,13 +4694,10 @@ void GridCtrl::SyncCtrls(int row) it->ctrl->SetRect(0, 0, 0, 0); it->ctrl->Hide(); } - - genr_ctrls = true; } if(it->isjoined) it->sync_flag = sync_flag; - } } sync_flag = 1 - sync_flag; diff --git a/uppsrc/GridCtrl/GridCtrl.h b/uppsrc/GridCtrl/GridCtrl.h index 1ce911afd..405de4c3a 100644 --- a/uppsrc/GridCtrl/GridCtrl.h +++ b/uppsrc/GridCtrl/GridCtrl.h @@ -342,6 +342,13 @@ class GridCtrl : public Ctrl GE_ROW = 0, GE_CELL = 1 }; + + enum GridItemCtrl + { + IC_INIT = BIT(1), + IC_MANUAL = BIT(2), + IC_FACTORY = BIT(3) + }; struct CurState { @@ -372,49 +379,56 @@ class GridCtrl : public Ctrl Ptr ctrl; Convert * convert; Callback1&> factory; - bool nofactory; Edit() { ctrl = NULL; convert = NULL; - nofactory = false; } }; - struct Item : Moveable + class Item : Moveable { - friend class ItemRect; - friend class GridCtrl; - - Item() - { - ctrl = NULL; - convert = NULL; - display = NULL; - - fs = fe = 0; - style = 0; - editable = true; - clickable = true; - idx = 0; - idy = 0; - cx = 0; - cy = 0; - group = -1; - isjoined = false; + public: + friend class ItemRect; + friend class GridCtrl; + + Item() + { + ctrl = NULL; + convert = NULL; + display = NULL; + + fs = fe = 0; + style = 0; + editable = true; + clickable = true; + idx = 0; + idy = 0; + cx = 0; + cy = 0; + group = -1; + isjoined = false; + + rcx = 0; + rcy = 0; + + modified = false; + sync_flag = -1; + paint_flag = -1; + ctrl_flag = 0; + } + ~Item() + { + if(!(ctrl_flag & IC_MANUAL)) + delete ctrl; + } - rcx = 0; - rcy = 0; - - modified = false; - sync_flag = -1; - paint_flag = -1; - } - ~Item() - { - delete ctrl; - } + void SetCtrl(Ctrl& ctrl); + void ClearCtrl(); + + void SetDisplay(GridDisplay& display); + void NoDisplay(); private: @@ -441,19 +455,13 @@ class GridCtrl : public Ctrl bool modified:1; int sync_flag; int paint_flag; + int ctrl_flag; Ptr ctrl; Convert *convert; GridDisplay *display; - void SetCtrl(Ctrl& ctrl); - void NoCtrl(); - - void SetDisplay(GridDisplay& display); - void NoDisplay(); - - public: Value val; @@ -718,10 +726,10 @@ class GridCtrl : public Ctrl ItemRect& Fg(Color c) { fg = c; return *this; } ItemRect& SetImage(const Image& i) { img = i; return *this; } ItemRect& ClearImage() { img = Null; return *this;} - ItemRect& Ctrls(Callback1&> _factory) { (*edits)[id].factory = _factory; return *this; } + ItemRect& Ctrls(Callback1&> _factory); ItemRect& Ctrls(void (*factory)(One&)) { return Ctrls(callback(factory)); } template ItemRect& Ctrls() { return Ctrls(DefaultCtrlFactory()); } - ItemRect& NoCtrls() { (*edits)[id].nofactory = true; return *this; } + ItemRect& NoCtrls(); ItemRect& Option(); ItemRect& Editable(bool b); @@ -885,7 +893,7 @@ class GridCtrl : public Ctrl bool doscroll:1; bool ready:1; bool ctrls:1; - bool genr_ctrls:1; + int genr_ctrls; bool edit_ctrls:1; bool shiftmode:1; bool recalc_cols; @@ -1229,6 +1237,8 @@ class GridCtrl : public Ctrl void Set(int r, const Vector &v, int data_offset = 0, int column_offset = 0); void Set(const Vector &v, int data_offset = 0, int column_offset = 0); + void SetCtrl(int r, int c, Ctrl& ctrl); + void ClearCtrl(int r, int c); void SetCtrlValue(int r, int c, const Value &val); void SetCtrlValue(int c, const Value &val); @@ -1618,7 +1628,7 @@ class GridCtrl : public Ctrl void DrawLine(bool iniLine, bool delLine); - Rect GetItemRect(int r, int c, bool hgrid = false, bool vgrid = false, bool ctrlmode = false); + Rect GetItemRect(int r, int c, bool hgrid = false, bool vgrid = false, bool relw = false, bool relh = false); bool Match(const WString &f, const WString &s, int &fs, int &fe); int ShowMatchedRows(const WString &f); @@ -1632,7 +1642,6 @@ class GridCtrl : public Ctrl int GetMinRowSelected(); int GetMaxRowSelected(); - void CloseGrid(); String RowFormat(const char *s);