1 #include <creaImageIOWxTreeView.h>
2 #include <creaImageIOGimmickView.h>
3 #include <creaImageIOSystem.h>
4 #include <wx/splitter.h>
5 ///Comparing function for ordering algorithm. Takes parameters as strings.
6 int wxCALLBACK CompareFunctionStrings(long item1, long item2, long sortData)
8 std::string s1((char*)((long*)item1)[1]);
9 std::string s2((char*)((long*)item2)[1]);
33 ///Comparing function for ordering algorithm. Takes parameters as ints.
34 int wxCALLBACK CompareFunctionInts(long item1, long item2, long sortData)
36 int val1=atoi((char*)((long*)item1)[1]);
37 int val2=atoi((char*)((long*)item2)[1]);
63 //=====================================================================
65 WxTreeView::WxTreeView(TreeHandler* handler,
70 TreeView(handler,gimmick)
72 GimmickDebugMessage(1,"WxTreeView::WxTreeView"
76 // Split part below toolbar into notebook for views and panel
77 // for preview, messages...
79 // mSplitter = new wxSplitterWindow( this , -1);
82 wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
84 int ctrl_style = wxLC_REPORT | wxLC_VRULES;
85 int col_style = wxLIST_FORMAT_LEFT;
87 // Creating the ListCtrl for the levels > 0 (not for Root level)
89 i < handler->GetTree().GetNumberOfLevels();
93 GimmickDebugMessage(5,"Creating view for level "<<i
97 // If the first level : parent = this
98 wxWindow* sparent = this;
99 // else parent = last splitter
100 if (i>1) sparent = mLevelList[i-2].wxSplitter;
102 level.wxSplitter = new wxSplitterWindow( sparent , -1);
103 // level.wxSplitter->SetMinimumPaneSize(100);
105 wxListCtrl* ctrl = new wxListCtrl(level.wxSplitter,
111 level.wxSplitter->Initialize(ctrl);
113 // Create the columns : one for each attribute of the level
116 tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a;
117 for (a = handler->GetTree().GetAttributeDescriptorList(i).begin();
118 a != handler->GetTree().GetAttributeDescriptorList(i).end();
124 it.SetTextColour(*wxRED);
125 it.SetText(_T("#C"));
127 ctrl->InsertColumn(col,it);
131 GimmickDebugMessage(5,"Creating column "<<col<<" : "
134 ctrl->InsertColumn(col,
135 crea::std2wx(a->GetName()),
137 level.key.push_back(a->GetKey());
138 // ctrl->SetColumnWidth(col, wxLIST_AUTOSIZE );
144 mLevelList.push_back(level);
148 /// Initialize the first level splitter
150 sizer->Add( mLevelList[0].wxSplitter ,1, wxGROW ,0);
161 //=====================================================================
163 //=====================================================================
165 WxTreeView::~WxTreeView()
167 GimmickDebugMessage(1,"WxTreeView::~WxTreeView"
170 //=====================================================================
173 //=====================================================================
180 //=====================================================================
181 std::vector<tree::Node*> WxTreeView::GetSelected(int level)
184 // the selection of upper level
185 std::vector<tree::Node*> sel;
189 sel.push_back(GetTreeHandler()->GetTree().GetTree());
193 int n = GetCtrl(l-1)->GetItemCount();
194 for (int i = 0; i < n; i++)
196 if ( GetCtrl(l-1)->GetItemState(i,wxLIST_STATE_SELECTED))
198 long adr = GetCtrl(l-1)->GetItemData(i);
199 tree::Node* n = ((ItemData*)adr)->node;
202 std::vector<tree::Node*>::iterator it;
204 it = sel.insert ( it , n );
217 //=====================================================================
219 ///Removes selected nodes on given level
220 void WxTreeView::RemoveSelected( int level )
222 std::vector<tree::Node*> sel=GetSelected(level+1);
224 if (wxMessageBox(_T("Delete file(s) ?"),
226 wxYES_NO,this ) == wxYES)
232 std::vector<tree::Node*>::iterator i;
233 for (i=sel.begin(); i!=sel.end(); ++i)
235 GimmickDebugMessage(2,
240 GetTreeHandler()->Remove(*i);
249 //=====================================================================
252 //=====================================================================
253 /// Updates a level of the view (adds or removes children, proganizes, etc.)
254 void WxTreeView::UpdateLevel( int level )
256 GimmickDebugMessage(1,
257 GetTreeHandler()->GetTree().GetLabel()
258 <<" view : updating level "<<level
262 RecursiveUpdateLevel(level);
264 for (i=0; i<level-1; i++)
266 if (!GetSplitter(i)->IsSplit())
267 GetSplitter(i)->SplitVertically( GetCtrl(i), GetSplitter(i+1),
270 if (GetSplitter(i)->IsSplit()) GetSplitter(i)->Unsplit();
273 //=====================================================================
275 /// Recursive method called upon by UpdateLevel to refresh all windows
276 void WxTreeView::RecursiveUpdateLevel( int level )
278 GimmickDebugMessage(2,
279 GetTreeHandler()->GetTree().GetLabel()
280 <<" view : updating level (recursive)"<<level
284 std::vector<tree::Node*> sel=GetSelected(level);
288 // to speed up inserting we hide the control temporarily
290 GetCtrl(l)->DeleteAllItems();
292 std::vector<tree::Node*>::iterator i;
293 for (i=sel.begin(); i!=sel.end(); ++i)
295 GimmickDebugMessage(2,
296 "adding children of '"
302 GetTreeHandler()->LoadChildren(*i,1);
303 tree::Node::ChildrenListType::reverse_iterator j;
304 for (j = (*i)->GetChildrenList().rbegin();
305 j!= (*i)->GetChildrenList().rend();
309 item.SetMask(wxLIST_MASK_STATE |
311 // wxLIST_MASK_IMAGE |
313 // wxLIST_MASK_WIDTH |
317 ItemData* data = new ItemData;
322 std::string a=(*j)->GetAttribute(mLevelList[l].key[mColumnSelected-1]);
324 "State Check: Att VAL"
333 long id = GetCtrl(l)->InsertItem(item);
335 std::ostringstream oss;
336 int n= GetTreeHandler()->GetNumberOfChildren(*j);
339 std::string s(oss.str());
342 item.SetText( crea::std2wx(s));
343 // item.SetTextColour(*wxRED);
344 // item.SetBackgroundColour(*wxBLACK);
347 //Sets the last level as selecte....How to make it select only the first time?
348 //if (level==mLevelList.size()) item.SetState(wxLIST_STATE_SELECTED);
350 GetCtrl(l)->SetItem(item);
352 // GetCtrl(l)->SetItem(id,0, crea::std2wx(s));
353 // GetCtrl(l)->SetColumnWidth(0, wxLIST_AUTOSIZE );
355 for (int k=1; k<GetCtrl(l)->GetColumnCount(); k++)
358 std::string val = (*j)->GetAttribute(mLevelList[l].key[k-1]);
359 if (val.size()==0) val = "?";
360 item.SetText( crea::std2wx(val));
361 // item.SetTextColour(*wxBLACK);
362 // item.SetBackgroundColour(*wxWHITE);
364 GetCtrl(l)->SetItem(item);
366 // GetCtrl(l)->SetItem(id,k, crea::std2wx(val));
367 // GetCtrl(l)->SetColumnWidth(k, wxLIST_AUTOSIZE );
377 if (level<mLevelList.size()) UpdateLevel(level+1);
379 //=====================================================================
382 //================================================================
383 void WxTreeView::OnSelectedChanged(wxListEvent& event)
385 GimmickDebugMessage(1,
386 GetTreeHandler()->GetTree().GetLabel()
387 <<" view : item selected "
391 info.m_itemId = event.m_itemIndex;
392 mLastSelected=event.m_itemIndex;
394 // retrieve the level
395 wxObject* obj = event.GetEventObject();
396 unsigned int level = 0;
397 for (level = 0; level<mLevelList.size(); ++level)
399 if ( GetCtrl(level) == obj ) break;
401 GimmickDebugMessage(1,
404 if (level<mLevelList.size()-1) UpdateLevel( level + 2 );
405 if (level==mLevelList.size()-1) ValidateSelectedImages ();
408 //================================================================
410 //================================================================
411 void WxTreeView::OnColClick(wxListEvent& event)
413 //Obtain the column name and the level that needs to be organized
414 mColumnSelected=event.m_col;
415 GimmickDebugMessage(1,
416 " Column selected: " <<event.m_col
419 if(mColumnSelected!=0)
421 wxObject* ctrl = event.GetEventObject();
422 unsigned int level = 0;
423 for (level = 0; level<mLevelList.size(); ++level)
425 if ( GetCtrl(level) == ctrl ) break;
427 UpdateLevel(level+1);
432 //Sets the data for the items to be sorted
435 int n = GetCtrl(level)->GetItemCount();
436 for (int i = 0; i < n; i++)
439 //Gets current item data
440 long adr = GetCtrl(level)->GetItemData(i);
441 //Extracts the node and the type of attribute
442 tree::Node* nod = ((ItemData*)adr)->node;
445 (*nod).GetAttributeDescriptor(mLevelList[level].key[mColumnSelected-1]).DecodeType(ty);
447 //Obtains the organizing attribute
448 att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected-1]);
450 char* d= new char [att.size()+1];
451 strcpy (d, att.c_str());
454 long* lp= new long[2];
458 //Sets it as the data
459 GetCtrl(level)->SetItemData(i,(long)lp);
466 GetCtrl(level)->SortItems(CompareFunctionInts, 0);
470 GetCtrl(level)->SortItems(CompareFunctionStrings, 0);
479 GetCtrl(level)->SortItems(CompareFunctionInts, 1);
483 GetCtrl(level)->SortItems(CompareFunctionStrings, 1);
488 //Resets original data
493 it = GetCtrl(level)->GetNextItem(it,
497 //Gets current item data, extracts the node and resets it
498 long item = GetCtrl(level)->GetItemData(it);
499 GetCtrl(level)->SetItemData(it,((long*)item)[0]);
505 //================================================================
508 //================================================================
509 void WxTreeView::ValidateSelectedImages()
511 GimmickDebugMessage(7,
512 "WxTreeView::ValidateSelectedImages"
514 std::vector<tree::Node*> sel(GetSelected(mLevelList.size()+1));
515 GetGimmickView()->OnSelectionChange(sel);
517 //Send an event telling wether the selection is valid or not
518 wxCommandEvent event( 0, GetId() );
519 event.SetEventObject( this );
520 std::vector<tree::Node*> sel=GetSelected((mLevelList.size()+1));
521 event.SetClientData(&sel);
522 GetEventHandler()->ProcessEvent( event );
526 //================================================================
529 //================================================================
530 void WxTreeView::GetNodes(std::vector<tree::Node*>& nodes, bool direction)
532 long item = mLastSelected;
533 int level=mLevelList.size()-1;
534 //Gets current item data
535 long adr = GetCtrl(level)->GetItemData(item);
537 tree::Node* nod = ((ItemData*)adr)->node;
542 item = GetCtrl(level)->GetNextItem(item,
547 item = GetCtrl(level)->GetNextItem(item,
554 if(GetCtrl(level)->GetItemState(item, wxLIST_STATE_SELECTED)==0)
556 adr = GetCtrl(level)->GetItemData(item);
557 nod = ((ItemData*)adr)->node;
558 nodes.push_back(nod);
564 //================================================================
565 void WxTreeView::GetSelectedAsString(std::vector<std::string>&s)
567 int level=mLevelList.size();
568 std::vector<tree::Node*> sel=GetSelected(level+1);
569 std::vector<tree::Node*>::iterator i;
571 for (i=sel.begin(); i!=sel.end(); ++i)
573 std::string filename=(*i)->GetAttribute("FullFileName");
574 s.push_back(filename);
577 //================================================================
578 BEGIN_EVENT_TABLE(WxTreeView, wxPanel)
580 EVT_SIZE(MyFrame::OnSize)
582 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
583 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
584 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
585 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
586 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
587 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
588 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
589 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
590 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
591 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView)
593 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
594 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
595 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
596 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
597 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
598 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
599 EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
600 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
601 EVT_MENU(LIST_SORT, MyFrame::OnSort)
602 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
603 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
604 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
605 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
606 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
607 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
608 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
609 EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
610 EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
612 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
613 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
616 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
617 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
618 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
619 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
620 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
621 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
622 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
623 #if WXWIN_COMPATIBILITY_2_4
624 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
625 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
628 EVT_LIST_ITEM_SELECTED(-1, WxTreeView::OnSelectedChanged)
630 EVT_LIST_ITEM_DESELECTED(-1, WxTreeView::OnSelectedChanged)
632 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
633 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
634 EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
636 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, MyListCtrl::OnColRightClick)
638 EVT_LIST_COL_CLICK(-1, WxTreeView::OnColClick)
640 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
641 EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
642 EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
644 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
647 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
649 EVT_CHAR(MyListCtrl::OnChar)
651 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
655 } // EO namespace creaImageIO