1 #include <creaImageIOWxTreeView.h>
2 #include <creaImageIOGimmickView.h>
3 #include <creaImageIOSystem.h>
4 #include <wx/splitter.h>
6 ///Comparing function for ordering algorithm. Takes parameters as strings.
7 int wxCALLBACK CompareFunctionStrings(long item1, long item2, long sortData)
9 std::string s1((char*)((long*)item1)[1]);
10 std::string s2((char*)((long*)item2)[1]);
34 ///Comparing function for ordering algorithm. Takes parameters as ints.
35 int wxCALLBACK CompareFunctionInts(long item1, long item2, long sortData)
37 int val1=atoi((char*)((long*)item1)[1]);
38 int val2=atoi((char*)((long*)item2)[1]);
64 //=====================================================================
66 WxTreeView::WxTreeView(TreeHandler* handler,
71 TreeView(handler,gimmick)
73 GimmickDebugMessage(1,"WxTreeView::WxTreeView"
77 // Split part below toolbar into notebook for views and panel
78 // for preview, messages...
80 // mSplitter = new wxSplitterWindow( this , -1);
83 wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
85 int ctrl_style = wxLC_REPORT | /*wxLC_VRULES |*/ wxLC_EDIT_LABELS;
86 int col_style = wxLIST_FORMAT_LEFT;
88 // Creating the ListCtrl for the levels > 0 (not for Root level)
90 i < handler->GetTree().GetNumberOfLevels();
94 GimmickDebugMessage(5,"Creating view for level "<<i
98 // If the first level : parent = this
99 wxWindow* sparent = this;
100 // else parent = last splitter
101 if (i>1) sparent = mLevelList[i-2].wxSplitter;
103 level.wxSplitter = new wxSplitterWindow( sparent , -1);
104 // level.wxSplitter->SetMinimumPaneSize(100);
106 wxListCtrl* ctrl = new wxListCtrl(level.wxSplitter,
112 level.wxSplitter->Initialize(ctrl);
114 // Create the first column : number of children
116 std::string title = "#";
117 if (i<handler->GetTree().GetNumberOfLevels()-1)
119 title += handler->GetTree().GetLevelDescriptor(i+1).GetName();
120 if (title[title.size()-1]!='s')
123 ctrl->InsertColumn(0,crea::std2wx(title),col_style);
126 // Create the columns : one for each attribute of the level
128 tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a;
129 for (a = handler->GetTree().GetAttributeDescriptorList(i).begin();
130 a != handler->GetTree().GetAttributeDescriptorList(i).end();
134 GimmickDebugMessage(5,"Creating column "<<col<<" : "
137 ctrl->InsertColumn(col,
138 crea::std2wx(a->GetName()),
140 level.key.push_back(a->GetKey());
141 // ctrl->SetColumnWidth(col, wxLIST_AUTOSIZE );
147 mLevelList.push_back(level);
151 /// Initialize the first level splitter
153 sizer->Add( mLevelList[0].wxSplitter ,1, wxGROW ,0);
157 mSelectionMade=false;
158 CreateColorPalette();
166 //=====================================================================
168 //=====================================================================
170 WxTreeView::~WxTreeView()
172 GimmickDebugMessage(1,"WxTreeView::~WxTreeView"
175 //=====================================================================
178 //=====================================================================
185 //=====================================================================
186 std::vector<tree::Node*> WxTreeView::GetSelected(int level)
189 // the selection of upper level
190 std::vector<tree::Node*> sel;
194 sel.push_back(GetTreeHandler()->GetTree().GetTree());
198 int n = GetCtrl(l-1)->GetItemCount();
199 for (int i = 0; i < n; i++)
201 if ( GetCtrl(l-1)->GetItemState(i,wxLIST_STATE_SELECTED))
203 long adr = GetCtrl(l-1)->GetItemData(i);
204 tree::Node* n = ((ItemData*)adr)->node;
207 std::vector<tree::Node*>::iterator it;
209 it = sel.insert ( it , n );
222 //=====================================================================
224 ///Removes selected nodes on given level
225 void WxTreeView::RemoveSelected( int level )
227 std::vector<tree::Node*> sel=GetSelected(level+1);
229 if (wxMessageBox(_T("Delete file(s) ?"),
231 wxYES_NO,this ) == wxYES)
237 std::vector<tree::Node*>::iterator i;
238 for (i=sel.begin(); i!=sel.end(); ++i)
240 GimmickDebugMessage(2,
245 GetTreeHandler()->Remove(*i);
254 //=====================================================================
257 //=====================================================================
258 /// Updates a level of the view (adds or removes children, proganizes, etc.)
259 void WxTreeView::UpdateLevel( int level )
261 GimmickDebugMessage(1,
262 GetTreeHandler()->GetTree().GetLabel()
263 <<" view : updating level "<<level
267 RecursiveUpdateLevel(level);
269 for (i=0; i<level-1; i++)
271 if (!GetSplitter(i)->IsSplit())
272 GetSplitter(i)->SplitVertically( GetCtrl(i), GetSplitter(i+1),
275 if (GetSplitter(i)->IsSplit()) GetSplitter(i)->Unsplit();
278 //=====================================================================
280 /// Recursive method called upon by UpdateLevel to refresh all windows
281 void WxTreeView::RecursiveUpdateLevel( int level )
283 GimmickDebugMessage(2,
284 GetTreeHandler()->GetTree().GetLabel()
285 <<" view : updating level (recursive)"<<level
289 std::vector<tree::Node*> sel=GetSelected(level);
293 // to speed up inserting we hide the control temporarily
295 GetCtrl(l)->DeleteAllItems();
297 std::vector<tree::Node*>::iterator i;
298 //Adds the first item (filter)
299 GetCtrl(l)->InsertItem(0, _T("Filter:"));
300 for (i=sel.begin(); i!=sel.end(); ++i)
302 GimmickDebugMessage(2,
303 "adding children of '"
312 //Adds items (other than the first) and sets their attributes
313 GetTreeHandler()->LoadChildren(*i,1);
314 tree::Node::ChildrenListType::reverse_iterator j;
315 for (j = (*i)->GetChildrenList().rbegin();
316 j!= (*i)->GetChildrenList().rend();
320 item.SetMask(wxLIST_MASK_STATE |
322 // wxLIST_MASK_IMAGE |
324 // wxLIST_MASK_WIDTH |
328 ItemData* data = new ItemData;
337 long id=GetCtrl(l)->InsertItem(item);
339 //Setting first column (number of children)
340 std::ostringstream oss;
341 int n= GetTreeHandler()->GetNumberOfChildren(*j);
343 std::string s(oss.str());
344 item.SetText( crea::std2wx(s));
346 //Setting the color according to the parent
349 item.SetBackgroundColour
350 (wxColourDatabase().Find
351 (crea::std2wx(mColorPalette[colorId])));
354 (*j,wxColourDatabase().Find
355 (crea::std2wx(mColorPalette[colorId]))));
365 else if(l!=mLevelList.size()-1)
367 item.SetBackgroundColour(mColorMap[*i]);
368 mColorMap.insert(NodeColorPair(*j,mColorMap[*i]));
372 item.SetBackgroundColour(mColorMap[*i]);
377 GetCtrl(l)->SetItem(item);
379 //Setting other attributes
380 for (int k=1; k<GetCtrl(l)->GetColumnCount(); k++)
383 std::string val = (*j)->GetAttribute(mLevelList[l].key[k-1]);
384 if (val.size()==0) val = "?";
385 item.SetText( crea::std2wx(val));
387 GetCtrl(l)->SetItem(item);
395 if (level<mLevelList.size()) UpdateLevel(level+1);
398 //=====================================================================
401 //================================================================
402 void WxTreeView::OnSelectedChanged(wxListEvent& event)
404 GimmickDebugMessage(1,
405 GetTreeHandler()->GetTree().GetLabel()
406 <<" view : item selected "
410 info.m_itemId = event.m_itemIndex;
411 mLastSelected=event.m_itemIndex;
412 // retrieve the level
413 wxObject* obj = event.GetEventObject();
414 unsigned int level = 0;
415 for (level = 0; level<mLevelList.size(); ++level)
417 if ( GetCtrl(level) == obj ) break;
419 GimmickDebugMessage(1,
423 if(event.m_itemIndex!=0)
425 if(level<mLevelList.size()-1)
427 mSelected=GetSelected(level+2);
431 mLastLevelSelected=GetSelected(level+2);
434 if (level<mLevelList.size()-1)
436 UpdateLevel( level + 2 );
437 GetGimmickView()->ClearSelection();
439 if (level==mLevelList.size()-2) SelectLowerLevel();
440 if (level==mLevelList.size()-1) ValidateSelectedImages ();
444 if(event.GetEventType()==10145)
447 GetCtrl(level)->SetItemText(0,crea::std2wx(""));
448 GetCtrl(level)->EditLabel(event.m_itemIndex);
454 //================================================================
456 //================================================================
457 void WxTreeView::SelectLowerLevel()
460 int level=mLevelList.size()-1;
463 item = GetCtrl(level)->GetNextItem(item,
469 GetCtrl(level)->SetItemState(item,wxLIST_STATE_SELECTED, wxLIST_MASK_STATE
470 | wxLIST_MASK_TEXT |wxLIST_MASK_IMAGE | wxLIST_MASK_DATA | wxLIST_MASK_WIDTH | wxLIST_MASK_FORMAT);
477 //================================================================
479 //================================================================
480 void WxTreeView::OnColClick(wxListEvent& event)
482 //Obtain the column name and the level that needs to be organized
483 mColumnSelected=event.m_col;
484 GimmickDebugMessage(1,
485 " Column selected: " <<event.m_col
488 if(mColumnSelected!=0)
490 wxObject* ctrl = event.GetEventObject();
491 unsigned int level = 0;
492 for (level = 0; level<mLevelList.size(); ++level)
494 if ( GetCtrl(level) == ctrl ) break;
500 GetCtrl(level)->DeleteItem(0);
501 //Sets the data for the items to be sorted
504 int n = GetCtrl(level)->GetItemCount();
505 for (int i = 0; i < n; i++)
508 //Gets current item data
509 long adr = GetCtrl(level)->GetItemData(i);
510 //Extracts the node and the type of attribute
511 tree::Node* nod = ((ItemData*)adr)->node;
514 (*nod).GetAttributeDescriptor(mLevelList[level].key[mColumnSelected-1]).DecodeType(ty);
516 //Obtains the organizing attribute
517 att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected-1]);
519 char* d= new char [att.size()+1];
520 strcpy (d, att.c_str());
523 long* lp= new long[2];
527 //Sets it as the data
528 GetCtrl(level)->SetItemData(i,(long)lp);
535 GetCtrl(level)->SortItems(CompareFunctionInts, 0);
539 GetCtrl(level)->SortItems(CompareFunctionStrings, 0);
548 GetCtrl(level)->SortItems(CompareFunctionInts, 1);
552 GetCtrl(level)->SortItems(CompareFunctionStrings, 1);
557 //Resets original data
558 std::vector<tree::Node*>::iterator selection;
559 std::vector<long> change;
563 it = GetCtrl(level)->GetNextItem(it,
568 //Gets current item data, extracts the node and resets it
569 long item = GetCtrl(level)->GetItemData(it);
570 GetCtrl(level)->SetItemData(it,((long*)item)[0]);
571 tree::Node* n= ((ItemData*)((long*)item)[0])->node;
572 if(level<mLevelList.size()-1)
574 for(selection=mSelected.begin();selection!=mSelected.end();++selection)
576 if((*selection)->GetAttribute("ID").compare(n->GetAttribute("ID"))==0)
578 change.push_back(it);
584 for(selection=mLastLevelSelected.begin();selection!=mLastLevelSelected.end();++selection)
586 if((*selection)->GetAttribute("ID").compare(n->GetAttribute("ID"))==0)
588 change.push_back(it);
595 //Resets the selected items
596 std::vector<long>::iterator selectedIts;
597 for(selectedIts=change.begin();selectedIts!=change.end();++selectedIts)
599 GetCtrl(level)->SetItemState(*selectedIts,wxLIST_STATE_SELECTED, wxLIST_MASK_STATE
600 | wxLIST_MASK_TEXT |wxLIST_MASK_IMAGE | wxLIST_MASK_DATA | wxLIST_MASK_WIDTH | wxLIST_MASK_FORMAT);
603 GetCtrl(level)->InsertItem(0,_T("Filter:"));
607 //================================================================
608 void WxTreeView::OnBeginLabelEdit(wxListEvent& event)
610 GimmickDebugMessage(7,
611 "WxTreeView::OnBeginLabelEdit"
613 if(event.m_itemIndex!=0)
620 //================================================================
621 void WxTreeView::OnEndLabelEdit(wxListEvent& event)
623 GimmickDebugMessage(7,
624 "WxTreeView::OnEndLabelEdit"
626 wxObject* ctrl = event.GetEventObject();
627 unsigned int level = 0;
628 for (level = 0; level<mLevelList.size(); ++level)
630 if ( GetCtrl(level) == ctrl ) break;
632 std::string filter = crea::wx2std(event.m_item.m_text.c_str());
637 UpdateLevel(level+1);
642 it = GetCtrl(level)->GetNextItem(it,
648 long adr = GetCtrl(level)->GetItemData(it);
649 for (int j=1;j<GetCtrl(level)->GetColumnCount()-1&&!contains;j++)
651 tree::Node* nod = ((ItemData*)adr)->node;
652 att=(*nod).GetAttribute(mLevelList[level].key[j-1]);
654 if(att.find(filter)<900)
661 GetCtrl(level)->DeleteItem(it);
665 GetGimmickView()->ClearSelection();
666 //GetCtrl(level)->DeleteAllItems();
669 //================================================================
670 void WxTreeView::ValidateSelectedImages()
672 GimmickDebugMessage(7,
673 "WxTreeView::ValidateSelectedImages"
675 std::vector<tree::Node*> sel(GetSelected(mLevelList.size()+1));
676 GetGimmickView()->OnSelectionChange(sel);
679 //================================================================
682 //================================================================
683 void WxTreeView::GetNodes(std::vector<tree::Node*>& nodes, bool direction)
685 long item = mLastSelected;
686 int level=mLevelList.size()-1;
687 //Gets current item data
688 long adr = GetCtrl(level)->GetItemData(item);
690 tree::Node* nod = ((ItemData*)adr)->node;
695 item = GetCtrl(level)->GetNextItem(item,
700 item = GetCtrl(level)->GetNextItem(item,
707 if(GetCtrl(level)->GetItemState(item, wxLIST_STATE_SELECTED)==0 && item!=0)
709 adr = GetCtrl(level)->GetItemData(item);
710 nod = ((ItemData*)adr)->node;
711 nodes.push_back(nod);
717 //================================================================
718 void WxTreeView::GetSelectedAsString(std::vector<std::string>&s)
720 int level=mLevelList.size();
721 std::vector<tree::Node*> sel=GetSelected(level+1);
722 std::vector<tree::Node*>::iterator i;
724 for (i=sel.begin(); i!=sel.end(); ++i)
726 std::string filename=(*i)->GetAttribute("FullFileName");
727 s.push_back(filename);
731 //================================================================
732 void WxTreeView::CreateColorPalette()
734 GimmickDebugMessage(6,"WxTreeView::CreateColorPalette");
735 mColorPalette.push_back("WHITE");
736 mColorPalette.push_back("LIGHT GREY");
737 mColorPalette.push_back("AQUAMARINE");
738 mColorPalette.push_back("MEDIUM FOREST GREEN");
739 mColorPalette.push_back("INDIAN RED");
740 mColorPalette.push_back("KHAKI");
741 mColorPalette.push_back("ORANGE");
742 mColorPalette.push_back("LIGHT BLUE");
743 mColorPalette.push_back("LIGHT STEEL BLUE");
744 mColorPalette.push_back("PINK");
745 mColorPalette.push_back("PLUM");
746 mColorPalette.push_back("PURPLE");
747 mColorPalette.push_back("RED");
748 mColorPalette.push_back("SEA GREEN");
749 mColorPalette.push_back("SIENNA");
750 mColorPalette.push_back("SKY BLUE");
751 mColorPalette.push_back("SLATE BLUE");
752 mColorPalette.push_back("SPRING GREEN");
753 mColorPalette.push_back("TAN");
754 mColorPalette.push_back("THISTLE");
755 mColorPalette.push_back("TURQUOISE");
756 mColorPalette.push_back("VIOLET");
757 mColorPalette.push_back("VIOLET RED");
758 mColorPalette.push_back("WHEAT");
759 mColorPalette.push_back("YELLOW");
760 mColorPalette.push_back("YELLOW GREEN");
761 mColorPalette.push_back("BLUE");
762 mColorPalette.push_back("BLUE VIOLET");
763 mColorPalette.push_back("BROWN");
764 mColorPalette.push_back("CADET BLUE");
765 mColorPalette.push_back("CORAL");
766 mColorPalette.push_back("CORNFLOWER BLUE");
767 mColorPalette.push_back("CYAN");
768 mColorPalette.push_back("DARK GREY");
769 mColorPalette.push_back("DARK GREEN");
770 mColorPalette.push_back("DARK OLIVE GREEN");
771 mColorPalette.push_back("DARK ORCHID");
772 mColorPalette.push_back("DARK SLATE BLUE");
773 mColorPalette.push_back("DARK SLATE GREY");
774 mColorPalette.push_back("DARK TURQUOISE");
775 mColorPalette.push_back("FIREBRICK");
776 mColorPalette.push_back("FOREST GREEN");
777 mColorPalette.push_back("GOLD");
778 mColorPalette.push_back("GOLDENROD");
779 mColorPalette.push_back("GREY");
780 mColorPalette.push_back("GREEN");
781 mColorPalette.push_back("GREEN YELLOW");
782 mColorPalette.push_back("LIME GREEN");
783 mColorPalette.push_back("MAGENTA");
784 mColorPalette.push_back("MAROON");
785 mColorPalette.push_back("MEDIUM AQUAMARINE");
786 mColorPalette.push_back("MEDIUM BLUE");
787 mColorPalette.push_back("MEDIUM GOLDENROD");
788 mColorPalette.push_back("MEDIUM ORCHID");
789 mColorPalette.push_back("MEDIUM SEA GREEN");
790 mColorPalette.push_back("MEDIUM SLATE BLUE");
791 mColorPalette.push_back("MEDIUM SPRING GREEN");
792 mColorPalette.push_back("MEDIUM TURQUOISE");
793 mColorPalette.push_back("MEDIUM VIOLET RED");
794 mColorPalette.push_back("MIDNIGHT BLUE");
795 mColorPalette.push_back("NAVY");
796 mColorPalette.push_back("ORANGE RED");
797 mColorPalette.push_back("ORCHID, PALE GREEN");
798 mColorPalette.push_back("STEEL BLUE");
799 mColorPalette.push_back("BLACK");
803 //================================================================
804 BEGIN_EVENT_TABLE(WxTreeView, wxPanel)
806 EVT_SIZE(MyFrame::OnSize)
808 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
809 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
810 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
811 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
812 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
813 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
814 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
815 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
816 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
817 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView)
819 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
820 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
821 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
822 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
823 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
824 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
825 EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
826 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
827 EVT_MENU(LIST_SORT, MyFrame::OnSort)
828 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
829 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
830 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
831 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
832 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
833 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
834 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
835 EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
836 EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
838 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
839 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
842 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
843 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
844 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
846 EVT_LIST_BEGIN_LABEL_EDIT(-1, WxTreeView::OnBeginLabelEdit)
847 EVT_LIST_END_LABEL_EDIT(-1, WxTreeView::OnEndLabelEdit)
849 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
850 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
851 #if WXWIN_COMPATIBILITY_2_4
852 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
853 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
856 EVT_LIST_ITEM_SELECTED(-1, WxTreeView::OnSelectedChanged)
858 EVT_LIST_ITEM_DESELECTED(-1, WxTreeView::OnSelectedChanged)
860 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
861 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
862 EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
864 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, MyListCtrl::OnColRightClick)
866 EVT_LIST_COL_CLICK(-1, WxTreeView::OnColClick)
868 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
869 EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
870 EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
872 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
875 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
877 EVT_CHAR(MyListCtrl::OnChar)
879 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
883 } // EO namespace creaImageIO