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,
406 mSelected=GetSelected(level+2);
408 if (level<mLevelList.size()-1) UpdateLevel( level + 2 );
410 if (level==mLevelList.size()-1) ValidateSelectedImages ();
413 //================================================================
415 //================================================================
416 void WxTreeView::OnColClick(wxListEvent& event)
418 //Obtain the column name and the level that needs to be organized
419 mColumnSelected=event.m_col;
420 GimmickDebugMessage(1,
421 " Column selected: " <<event.m_col
424 if(mColumnSelected!=0)
426 wxObject* ctrl = event.GetEventObject();
427 unsigned int level = 0;
428 for (level = 0; level<mLevelList.size(); ++level)
430 if ( GetCtrl(level) == ctrl ) break;
433 UpdateLevel(level+1);
439 //Sets the data for the items to be sorted
442 int n = GetCtrl(level)->GetItemCount();
443 for (int i = 0; i < n; i++)
446 //Gets current item data
447 long adr = GetCtrl(level)->GetItemData(i);
448 //Extracts the node and the type of attribute
449 tree::Node* nod = ((ItemData*)adr)->node;
452 (*nod).GetAttributeDescriptor(mLevelList[level].key[mColumnSelected-1]).DecodeType(ty);
454 //Obtains the organizing attribute
455 att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected-1]);
457 char* d= new char [att.size()+1];
458 strcpy (d, att.c_str());
461 long* lp= new long[2];
465 //Sets it as the data
466 GetCtrl(level)->SetItemData(i,(long)lp);
473 GetCtrl(level)->SortItems(CompareFunctionInts, 0);
477 GetCtrl(level)->SortItems(CompareFunctionStrings, 0);
486 GetCtrl(level)->SortItems(CompareFunctionInts, 1);
490 GetCtrl(level)->SortItems(CompareFunctionStrings, 1);
495 //Resets original data
496 std::vector<tree::Node*>::iterator selection;
497 std::vector<long> change;
501 it = GetCtrl(level)->GetNextItem(it,
505 //Gets current item data, extracts the node and resets it
506 long item = GetCtrl(level)->GetItemData(it);
507 GetCtrl(level)->SetItemData(it,((long*)item)[0]);
508 tree::Node* n= ((ItemData*)((long*)item)[0])->node;
509 for(selection=mSelected.begin();selection!=mSelected.end();++selection)
511 if((*selection)->GetAttribute("ID").compare(n->GetAttribute("ID"))==0)
513 change.push_back(it);
518 std::vector<long>::iterator selectedIts;
519 for(selectedIts=change.begin();selectedIts!=change.end();++selectedIts)
521 GetCtrl(level)->SetItemState(*selectedIts,wxLIST_STATE_SELECTED, wxLIST_MASK_STATE
522 | wxLIST_MASK_TEXT |wxLIST_MASK_IMAGE | wxLIST_MASK_DATA | wxLIST_MASK_WIDTH | wxLIST_MASK_FORMAT);
528 //================================================================
531 //================================================================
532 void WxTreeView::ValidateSelectedImages()
534 GimmickDebugMessage(7,
535 "WxTreeView::ValidateSelectedImages"
537 std::vector<tree::Node*> sel(GetSelected(mLevelList.size()+1));
538 GetGimmickView()->OnSelectionChange(sel);
540 //Send an event telling wether the selection is valid or not
541 wxCommandEvent event( 0, GetId() );
542 event.SetEventObject( this );
543 std::vector<tree::Node*> sel=GetSelected((mLevelList.size()+1));
544 event.SetClientData(&sel);
545 GetEventHandler()->ProcessEvent( event );
549 //================================================================
552 //================================================================
553 void WxTreeView::GetNodes(std::vector<tree::Node*>& nodes, bool direction)
555 long item = mLastSelected;
556 int level=mLevelList.size()-1;
557 //Gets current item data
558 long adr = GetCtrl(level)->GetItemData(item);
560 tree::Node* nod = ((ItemData*)adr)->node;
565 item = GetCtrl(level)->GetNextItem(item,
570 item = GetCtrl(level)->GetNextItem(item,
577 if(GetCtrl(level)->GetItemState(item, wxLIST_STATE_SELECTED)==0)
579 adr = GetCtrl(level)->GetItemData(item);
580 nod = ((ItemData*)adr)->node;
581 nodes.push_back(nod);
587 //================================================================
588 void WxTreeView::GetSelectedAsString(std::vector<std::string>&s)
590 int level=mLevelList.size();
591 std::vector<tree::Node*> sel=GetSelected(level+1);
592 std::vector<tree::Node*>::iterator i;
594 for (i=sel.begin(); i!=sel.end(); ++i)
596 std::string filename=(*i)->GetAttribute("FullFileName");
597 s.push_back(filename);
600 //================================================================
601 BEGIN_EVENT_TABLE(WxTreeView, wxPanel)
603 EVT_SIZE(MyFrame::OnSize)
605 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
606 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
607 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
608 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
609 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
610 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
611 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
612 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
613 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
614 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView)
616 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
617 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
618 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
619 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
620 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
621 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
622 EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
623 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
624 EVT_MENU(LIST_SORT, MyFrame::OnSort)
625 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
626 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
627 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
628 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
629 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
630 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
631 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
632 EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
633 EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
635 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
636 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
639 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
640 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
641 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
642 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
643 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
644 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
645 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
646 #if WXWIN_COMPATIBILITY_2_4
647 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
648 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
651 EVT_LIST_ITEM_SELECTED(-1, WxTreeView::OnSelectedChanged)
653 EVT_LIST_ITEM_DESELECTED(-1, WxTreeView::OnSelectedChanged)
655 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
656 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
657 EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
659 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, MyListCtrl::OnColRightClick)
661 EVT_LIST_COL_CLICK(-1, WxTreeView::OnColClick)
663 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
664 EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
665 EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
667 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
670 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
672 EVT_CHAR(MyListCtrl::OnChar)
674 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
678 } // EO namespace creaImageIO