]> Creatis software - creaImageIO.git/blob - src2/creaImageIOWxTreeView.cpp
Linux compat
[creaImageIO.git] / src2 / creaImageIOWxTreeView.cpp
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)
7 {       
8         std::string s1((char*)((long*)item1)[1]);
9         std::string s2((char*)((long*)item2)[1]);
10         if(sortData==1)
11         {
12                 // inverse the order
13                 if (s1 < s2)
14                         return 1;
15                 if (s1 > s2)
16                         return -1;
17
18                 return 0;
19         }
20         else
21         {
22                 if (s1 < s2)
23                         return -1;
24                 if (s1 > s2)
25                         return 1;
26
27                 return 0;
28
29         }
30         
31 }
32
33 ///Comparing function for ordering algorithm. Takes parameters as ints.
34 int wxCALLBACK CompareFunctionInts(long item1, long item2, long sortData)
35 {       
36         int val1=atoi((char*)((long*)item1)[1]);
37         int val2=atoi((char*)((long*)item2)[1]);
38         if(sortData==1)
39         {
40                 // inverse the order
41                 if (val1 < val2)
42                         return 1;
43                 if (val1 > val2)
44                         return -1;
45
46                 return 0;
47         }
48         else
49         {
50                 if (val1 < val2)
51                         return -1;
52                 if (val1 > val2)
53                         return 1;
54
55                 return 0;
56
57         }
58         
59 }
60
61 namespace creaImageIO
62 {
63   //=====================================================================
64   // CTor
65   WxTreeView::WxTreeView(TreeHandler* handler,
66                          GimmickView* gimmick,
67                          wxWindow* parent,
68                          const wxWindowID id)
69     : wxPanel(parent,id),
70       TreeView(handler,gimmick)
71   {
72     GimmickDebugMessage(1,"WxTreeView::WxTreeView"
73                         <<std::endl);
74
75     
76     // Split part below toolbar into notebook for views and panel
77     // for preview, messages...
78     // TO DO : Splitter
79     //    mSplitter = new wxSplitterWindow( this , -1);
80
81     // Global sizer
82     wxBoxSizer  *sizer = new wxBoxSizer(wxHORIZONTAL);
83     
84     int ctrl_style = wxLC_REPORT | wxLC_VRULES;
85     int col_style = wxLIST_FORMAT_LEFT;
86
87     // Creating the ListCtrl for the levels > 0 (not for Root level)
88     for (int i = 1;
89          i < handler->GetTree().GetNumberOfLevels();
90          ++i)
91       {
92
93         GimmickDebugMessage(5,"Creating view for level "<<i
94                             <<std::endl);
95         LevelType level;
96
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;
101
102         level.wxSplitter = new wxSplitterWindow( sparent , -1);
103         //          level.wxSplitter->SetMinimumPaneSize(100);
104         
105         wxListCtrl* ctrl = new wxListCtrl(level.wxSplitter,
106                                           i,
107                                           wxDefaultPosition, 
108                                           wxDefaultSize,
109                                           ctrl_style);
110         level.wxCtrl = ctrl;
111         level.wxSplitter->Initialize(ctrl);
112
113         // Create the columns : one for each attribute of the level
114         int col = 0;
115         
116         tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a;
117         for (a  = handler->GetTree().GetAttributeDescriptorList(i).begin();
118              a != handler->GetTree().GetAttributeDescriptorList(i).end();
119              ++a)
120           {
121                   if(col==0)
122                   {
123                     wxListItem it;
124                     it.SetTextColour(*wxRED);
125                     it.SetText(_T("#C"));
126                 
127                     ctrl->InsertColumn(col,it);
128                     col++;
129                   }
130                   
131                 GimmickDebugMessage(5,"Creating column "<<col<<" : "
132                                 <<a->GetName()
133                                 <<std::endl);
134                 ctrl->InsertColumn(col, 
135                                 crea::std2wx(a->GetName()),
136                                 col_style);
137                 level.key.push_back(a->GetKey());
138                 //          ctrl->SetColumnWidth(col, wxLIST_AUTOSIZE );
139                 col++;
140                 
141                 
142           }
143           
144         mLevelList.push_back(level);
145       }
146     
147
148     /// Initialize the first level splitter
149  
150       sizer->Add( mLevelList[0].wxSplitter ,1, wxGROW  ,0);
151         mColumnSelected=1;
152         mLastSelected=0;
153         mDirection=true;
154     UpdateLevel(1);
155
156     SetSizer( sizer );     
157     SetAutoLayout(true);
158     Layout();
159
160   }
161   //=====================================================================
162
163   //=====================================================================
164   /// Destructor
165   WxTreeView::~WxTreeView()
166   {
167     GimmickDebugMessage(1,"WxTreeView::~WxTreeView"
168                         <<std::endl);
169   }
170   //=====================================================================
171   
172   
173   //=====================================================================
174   struct ItemData
175   {
176     tree::Node* node;
177         int id;
178   };
179  
180   //=====================================================================
181    std::vector<tree::Node*> WxTreeView::GetSelected(int level)
182   {
183     int l = level - 1;
184     // the selection of upper level
185     std::vector<tree::Node*> sel;
186         
187     if (level == 1) 
188       {
189         sel.push_back(GetTreeHandler()->GetTree().GetTree());
190       }
191     else 
192       {
193         int n = GetCtrl(l-1)->GetItemCount();
194         for (int i = 0; i < n; i++)
195           {
196             if ( GetCtrl(l-1)->GetItemState(i,wxLIST_STATE_SELECTED))
197               {
198                 long adr = GetCtrl(l-1)->GetItemData(i);
199                 tree::Node* n = ((ItemData*)adr)->node;
200                         if(mLastSelected==i)
201                         {
202                                 std::vector<tree::Node*>::iterator it;
203                                 it = sel.begin();
204                                 it = sel.insert ( it , n );
205                         }
206                         else
207                         {
208                                 sel.push_back(n);
209                         }
210               }
211           }     
212       }
213         
214           return sel;
215   }
216
217   //=====================================================================
218   
219   ///Removes selected nodes on given level
220   void WxTreeView::RemoveSelected( int level )
221   {
222           std::vector<tree::Node*> sel=GetSelected(level+1);
223           bool erase=false;
224           if (wxMessageBox(_T("Delete file(s) ?"),
225                          _T("Remove Files"),
226                          wxYES_NO,this ) == wxYES)
227           {
228             erase = true;
229           }
230           if(erase)
231           {
232                 std::vector<tree::Node*>::iterator i;
233                 for (i=sel.begin(); i!=sel.end(); ++i)
234                 {
235                         GimmickDebugMessage(2,
236                                         "deleting '"
237                                         <<(*i)->GetLabel()
238                                         <<"'"<<level
239                                         <<std::endl);
240                                 GetTreeHandler()->Remove(*i);
241                 }
242
243                 UpdateLevel(level);
244           }
245           
246   }
247     
248
249   //=====================================================================
250
251  
252   //=====================================================================
253   /// Updates a level of the view (adds or removes children, proganizes, etc.)
254   void WxTreeView::UpdateLevel( int level )
255   {
256     GimmickDebugMessage(1,
257                         GetTreeHandler()->GetTree().GetLabel()
258                         <<" view : updating level "<<level
259                         <<std::endl);
260     
261     wxBusyCursor busy;
262     RecursiveUpdateLevel(level);
263     int i;
264     for (i=0; i<level-1; i++)
265       {
266         if (!GetSplitter(i)->IsSplit()) 
267           GetSplitter(i)->SplitVertically(  GetCtrl(i), GetSplitter(i+1),
268                                             100 );
269       }
270     if (GetSplitter(i)->IsSplit()) GetSplitter(i)->Unsplit();    
271     
272   }
273   //=====================================================================
274   
275   /// Recursive method called upon by UpdateLevel to refresh all windows
276   void WxTreeView::RecursiveUpdateLevel( int level )
277   {
278           GimmickDebugMessage(2,
279                         GetTreeHandler()->GetTree().GetLabel()
280                         <<" view : updating level (recursive)"<<level
281                         <<std::endl);
282
283
284     std::vector<tree::Node*> sel=GetSelected(level);
285
286           int l = level - 1;
287  
288     // to speed up inserting we hide the control temporarily
289     GetCtrl(l)->Hide();
290     GetCtrl(l)->DeleteAllItems();
291     
292     std::vector<tree::Node*>::iterator i;
293     for (i=sel.begin(); i!=sel.end(); ++i)
294       {
295         GimmickDebugMessage(2,
296                             "adding children of '"
297                             <<(*i)->GetLabel()
298                             <<"'"<<level
299                             <<std::endl);
300         int _id=0;
301         //Adds columns
302         GetTreeHandler()->LoadChildren(*i,1);
303         tree::Node::ChildrenListType::reverse_iterator j;
304         for (j = (*i)->GetChildrenList().rbegin(); 
305              j!= (*i)->GetChildrenList().rend(); 
306              ++j)
307           {
308             wxListItem item;
309             item.SetMask(wxLIST_MASK_STATE | 
310                          wxLIST_MASK_TEXT |
311                          //                      wxLIST_MASK_IMAGE |
312                          wxLIST_MASK_DATA |
313                          //                      wxLIST_MASK_WIDTH |
314                          wxLIST_MASK_FORMAT
315                          );
316
317             ItemData* data = new ItemData;
318             
319                 data->node = *j;
320                 item.SetId(_id);
321                 /*
322                 std::string a=(*j)->GetAttribute(mLevelList[l].key[mColumnSelected-1]);
323                 GimmickMessage(1,
324                         "State Check: Att VAL"
325                             <<a
326                             <<std::endl);
327         */
328                 data->id = _id;
329             item.SetData(data);
330             
331                 _id++;
332           
333             long id = GetCtrl(l)->InsertItem(item);
334
335             std::ostringstream oss;
336             int n= GetTreeHandler()->GetNumberOfChildren(*j);
337             
338             oss << n;
339             std::string s(oss.str());
340             
341                 
342             item.SetText( crea::std2wx(s));
343             //      item.SetTextColour(*wxRED);
344             //      item.SetBackgroundColour(*wxBLACK); 
345             item.SetColumn(0);
346
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);
349
350             GetCtrl(l)->SetItem(item);
351                 
352             //      GetCtrl(l)->SetItem(id,0, crea::std2wx(s));
353             //      GetCtrl(l)->SetColumnWidth(0, wxLIST_AUTOSIZE );
354
355           for (int k=1; k<GetCtrl(l)->GetColumnCount(); k++)
356               {
357   
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); 
363                 item.SetColumn(k);
364                 GetCtrl(l)->SetItem(item);
365
366                 //              GetCtrl(l)->SetItem(id,k, crea::std2wx(val));
367                 // GetCtrl(l)->SetColumnWidth(k, wxLIST_AUTOSIZE );
368                 
369               }
370             
371           }
372       }
373     
374     GetCtrl(l)->Show();
375
376     
377     if (level<mLevelList.size()) UpdateLevel(level+1);
378  }
379   //=====================================================================
380
381
382   //================================================================
383   void WxTreeView::OnSelectedChanged(wxListEvent& event)
384   { 
385     GimmickDebugMessage(1,
386                         GetTreeHandler()->GetTree().GetLabel()
387                         <<" view : item selected "
388                         <<std::endl);
389
390     wxListItem info;
391     info.m_itemId = event.m_itemIndex;
392     mLastSelected=event.m_itemIndex;
393
394     // retrieve the level
395     wxObject* obj = event.GetEventObject();   
396     unsigned int level = 0;
397     for (level = 0; level<mLevelList.size(); ++level)
398       {
399         if ( GetCtrl(level) == obj ) break;
400       }
401     GimmickDebugMessage(1,
402                         " Level "<<level+1
403                         <<std::endl);
404     if (level<mLevelList.size()-1) UpdateLevel( level + 2 ); 
405     if (level==mLevelList.size()-1) ValidateSelectedImages ();
406
407   }
408   //================================================================
409
410 //================================================================
411   void WxTreeView::OnColClick(wxListEvent& event)
412   {       
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
417                         <<std::endl);
418
419         if(mColumnSelected!=0)
420         {
421                 wxObject* ctrl = event.GetEventObject(); 
422                 unsigned int level = 0;
423                 for (level = 0; level<mLevelList.size(); ++level)
424                 {
425                 if ( GetCtrl(level) == ctrl ) break;
426                 }
427                 UpdateLevel(level+1);
428                 wxBusyCursor busy;
429          
430                 int l = level - 1;
431
432                 //Sets the data for the items to be sorted
433                 std::string att;
434                 unsigned int ty=0;
435                 int n = GetCtrl(level)->GetItemCount();
436                 for (int i = 0; i < n; i++)
437                 {
438                           
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;
443                         if(i==0)
444                         {
445                                 (*nod).GetAttributeDescriptor(mLevelList[level].key[mColumnSelected-1]).DecodeType(ty);
446                         }
447                         //Obtains the organizing attribute
448                         att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected-1]);
449                         
450                         char* d= new char [att.size()+1];
451                         strcpy (d, att.c_str());
452
453                         //Creates array
454                         long* lp= new long[2];
455                         lp[0]=adr;
456                         lp[1]=(long)d;
457                          
458                         //Sets it as the data
459                         GetCtrl(level)->SetItemData(i,(long)lp);
460                 }       
461                   
462                 if(mDirection)
463                 {
464                         if(ty==1)
465                         {
466                                 GetCtrl(level)->SortItems(CompareFunctionInts, 0);
467                         }
468                         else
469                         {
470                                 GetCtrl(level)->SortItems(CompareFunctionStrings, 0);
471                         }
472                         
473                         mDirection=false;
474                 }
475                 else
476                 {
477                         if(ty==1)
478                         {
479                                 GetCtrl(level)->SortItems(CompareFunctionInts, 1);
480                         }
481                         else
482                         {
483                                 GetCtrl(level)->SortItems(CompareFunctionStrings, 1);
484                         }
485                         mDirection=true;
486                 }
487
488                 //Resets original data
489                         
490                 long it = -1;
491                 for ( ;; )
492                 {
493                         it = GetCtrl(level)->GetNextItem(it,
494                                                                                 wxLIST_NEXT_ALL);
495                         if ( it == -1 )
496                                 break;
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]);
500                         
501                 }
502         }
503         
504    }
505   //================================================================
506
507
508   //================================================================
509   void WxTreeView::ValidateSelectedImages()
510   {
511     GimmickDebugMessage(7,
512                         "WxTreeView::ValidateSelectedImages" 
513                         <<std::endl);
514     std::vector<tree::Node*> sel(GetSelected(mLevelList.size()+1));
515     GetGimmickView()->OnSelectionChange(sel);
516     /*
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 );
523     */
524     // 
525   }
526   //================================================================
527
528
529   //================================================================
530   void WxTreeView::GetNodes(std::vector<tree::Node*>& nodes, bool direction)
531   {
532         long item = mLastSelected;
533         int level=mLevelList.size()-1;
534         //Gets current item data
535         long adr = GetCtrl(level)->GetItemData(item);
536         //Extracts the node
537         tree::Node* nod = ((ItemData*)adr)->node;
538     for ( ;; )
539     {
540                 if(direction)
541                 {
542                         item = GetCtrl(level)->GetNextItem(item,
543                                      wxLIST_NEXT_ABOVE);
544                 }
545                 else
546                 {
547                         item = GetCtrl(level)->GetNextItem(item,
548                                      wxLIST_NEXT_BELOW);
549                 }
550         if ( item == -1 )
551                 {
552             break;
553                 }
554                 if(GetCtrl(level)->GetItemState(item, wxLIST_STATE_SELECTED)==0)
555                 {
556                         adr = GetCtrl(level)->GetItemData(item);
557                         nod = ((ItemData*)adr)->node;
558                         nodes.push_back(nod);
559                 }
560     }
561
562   }
563
564   //================================================================
565   void WxTreeView::GetSelectedAsString(std::vector<std::string>&s)
566   {
567           int level=mLevelList.size();
568         std::vector<tree::Node*> sel=GetSelected(level+1);
569         std::vector<tree::Node*>::iterator i;
570         
571     for (i=sel.begin(); i!=sel.end(); ++i)
572       {
573                   std::string filename=(*i)->GetAttribute("FullFileName");
574                   s.push_back(filename);
575           }
576   }
577   //================================================================
578   BEGIN_EVENT_TABLE(WxTreeView, wxPanel)
579   /*
580     EVT_SIZE(MyFrame::OnSize)
581
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)
592
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)
611
612     EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
613     EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
614 END_EVENT_TABLE()
615
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)
626 #endif
627   */
628     EVT_LIST_ITEM_SELECTED(-1, WxTreeView::OnSelectedChanged)
629   
630     EVT_LIST_ITEM_DESELECTED(-1, WxTreeView::OnSelectedChanged)
631         /*
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)
635
636     EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, MyListCtrl::OnColRightClick)
637         */
638     EVT_LIST_COL_CLICK(-1, WxTreeView::OnColClick)
639         /*
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)
643
644     EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
645
646 #if USE_CONTEXT_MENU
647     EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
648 #endif
649     EVT_CHAR(MyListCtrl::OnChar)
650
651     EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
652   */
653 END_EVENT_TABLE()
654   
655 } // EO namespace creaImageIO
656