]> Creatis software - creaImageIO.git/blob - src2/creaImageIOWxTreeView.cpp
Modified dynamic validation of last level
[creaImageIO.git] / src2 / creaImageIOWxTreeView.cpp
1 #include <creaImageIOWxTreeView.h>
2 #include <creaImageIOGimmickView.h>
3 #include <creaImageIOSystem.h>
4 #include <wx/splitter.h>
5 #include <wx/gdicmn.h>
6 #include <boost/date_time/gregorian/gregorian.hpp>
7
8 const std::string empty_string("");
9
10 //=====================================================================
11 namespace creaImageIO
12 {
13   //=====================================================================
14   /// Data stored by the list items
15   struct ItemData
16   {
17     ItemData() : node(0), id(-1), attr(&empty_string) {}
18     // The corresponding Node
19     tree::Node* node;
20     // The id ?
21     int id;
22     // The pointer on the current attribute string to sort on
23     const std::string* attr;
24     // Was the item previously selected ?
25     // Useful for reselecting the item after sort
26     bool selected;
27   };
28   //=====================================================================
29 }
30 //=====================================================================
31
32 //=====================================================================
33 ///Comparing function for ordering algorithm. Takes parameters as strings.
34 int wxCALLBACK CompareFunctionStrings(long item1, long item2, long sortData)
35 {       
36   creaImageIO::ItemData* data1 = (creaImageIO::ItemData*)item1;
37   creaImageIO::ItemData* data2 = (creaImageIO::ItemData*)item2;
38
39   const std::string& s1(*(data1->attr));
40   const std::string& s2(*(data2->attr));
41   if(sortData==1)
42     {
43       // inverse the order
44       if (s1 < s2)
45         return 1;
46       if (s1 > s2)
47         return -1;
48       
49       return 0;
50     }
51   else
52     {
53       if (s1 < s2)
54         return -1;
55       if (s1 > s2)
56         return 1;
57       
58       return 0;
59       
60     }
61 }
62 //=====================================================================
63
64 //=====================================================================
65 ///Comparing function for ordering algorithm. Takes parameters as ints.
66 int wxCALLBACK CompareFunctionInts(long item1, long item2, long sortData)
67 {       
68   creaImageIO::ItemData* data1 = (creaImageIO::ItemData*)item1;
69   creaImageIO::ItemData* data2 = (creaImageIO::ItemData*)item2;
70
71   const std::string& s1(*(data1->attr));
72   const std::string& s2(*(data2->attr));
73
74   int val1=atoi(s1.c_str());
75   int val2=atoi(s2.c_str());
76
77   if(sortData==1)
78     {
79       // inverse the order
80       if (val1 < val2)
81         return 1;
82       if (val1 > val2)
83         return -1;
84       
85       return 0;
86     }
87   else
88     {
89       if (val1 < val2)
90         return -1;
91       if (val1 > val2)
92         return 1;
93
94       return 0;
95       
96     }
97   
98 }
99
100 //=====================================================================
101
102
103 //=====================================================================
104 namespace creaImageIO
105 {
106   //=====================================================================
107   // CTor
108   WxTreeView::WxTreeView(TreeHandler* handler,
109                          GimmickView* gimmick,
110                          wxWindow* parent,
111                          const wxWindowID id)
112     : wxPanel(parent,id),
113       TreeView(handler, gimmick)
114   {
115     GimmickDebugMessage(1,"WxTreeView::WxTreeView"
116                         <<std::endl);
117
118     
119     // Split part below toolbar into notebook for views and panel
120     // for preview, messages...
121     // TO DO : Splitter
122     //    mSplitter = new wxSplitterWindow( this , -1);
123
124     // Global sizer
125     wxBoxSizer  *sizer = new wxBoxSizer(wxHORIZONTAL);
126     
127     int ctrl_style = wxLC_REPORT | wxLC_VRULES;
128     int col_style = wxLIST_FORMAT_LEFT;
129
130     // Creating the ListCtrl for the levels > 0 (not for Root level)
131     for (int i = 0;
132          i < handler->GetTree().GetNumberOfLevels() -1;
133          ++i)
134       {
135         GimmickDebugMessage(5,"Creating view for level "<<i
136                             <<std::endl);
137         LevelType level;
138         level.SelectedUpToDate = true;
139         level.SortColumn = 0;
140
141         // If the first level : parent = this
142         wxWindow* sparent = this;
143         // else parent = last splitter
144         if (i>0) 
145                 sparent = mLevelList[i-1].wxSplitter;
146
147         level.wxSplitter = new wxSplitterWindow( sparent , -1);
148         if(i!=0)
149         {
150         level.wxSplitter->Show(false);
151         }
152         //          level.wxSplitter->SetMinimumPaneSize(100);
153         
154         wxListCtrl* ctrl = new wxListCtrl(level.wxSplitter,
155                                           i,
156                                           wxDefaultPosition, 
157                                           wxDefaultSize,
158                                           ctrl_style);
159         level.wxCtrl = ctrl;
160         level.wxSplitter->Initialize(ctrl);
161    
162         // Create the columns : one for each attribute of the level
163         int col = 0;
164         std::string title;
165
166         tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a;
167         for (a  = handler->GetTree().GetAttributeDescriptorList(i+1).begin();
168              a != handler->GetTree().GetAttributeDescriptorList(i+1).end();
169              ++a)
170
171 {
172         
173             GimmickDebugMessage(5,"Creating column "<<col<<" : "
174                                 <<a->GetName()
175                                 <<std::endl);
176             
177             if(a->GetFlags()!=creaImageIO::tree::AttributeDescriptor::PRIVATE)
178               {
179                 
180                 if(a->GetName()=="UNKNOWN")
181                   {
182                     title = "#";
183                     title += handler->GetTree().GetLevelDescriptor(i+1).GetName();
184                     if (title[title.size()-1]!='s')
185                       title += "s";
186                     
187                   }
188                 else
189                   {
190                     title=a->GetName();
191                   }
192                   std::string temp = a->GetKey();
193                   if (temp.compare("ID") != 0)
194                   {
195                 
196                 ctrl->InsertColumn(col, 
197                                    crea::std2wx(title),
198                                    col_style);
199                 col++;
200                   }
201                 level.key.push_back(a->GetKey());
202               }
203                 
204           }
205           
206         mLevelList.push_back(level);
207       }
208     
209 #if wxUSE_MENUS
210
211          // Column Menu
212     menu =new wxMenu;
213         wxMenuItem* m1=menu->Append(wxID_ANY, _T("&Sort ascending"));
214         wxMenuItem* m2=menu->Append(wxID_ANY, _T("&Sort descending"));
215         wxMenuItem* m3=menu->Append(wxID_ANY, _T("&Filter"));
216         mAscendingID=m1->GetId();
217         mDescendingID=m2->GetId();
218         mFilterID=m3->GetId();
219         Connect( mAscendingID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupSort) );
220         Connect( mDescendingID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupSort) );
221         Connect( mFilterID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnPopupFilter) );
222
223
224
225         //ItemMenu
226         menuItem =new wxMenu;
227         wxMenuItem* m1Item=menuItem->Append(wxID_ANY, _T("&Anonymize"));
228         wxMenuItem* m2Item=menuItem->Append(wxID_ANY, _T("&Local Copy"));
229         
230         mAnonymizingID=m1Item->GetId();
231         mLocalCopyID=m2Item->GetId();
232         //Connect( mAnonymizingID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnAnonymize) );
233         Connect( mLocalCopyID, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(WxTreeView::OnLocalCopy) );
234         
235         
236 #endif // wxUSE_MENUS
237         /// Initialize the first level splitter
238           
239         sizer->Add( mLevelList[0].wxSplitter ,1, wxGROW  ,0);
240         //      mColumnSelected=1;
241         mLastSelected=0;
242         mLastLevel=0;
243         //      mDirection=true;
244
245         mIgnoreSelectedChanged = false;
246
247         //CreateColorPalette();
248     UpdateLevel(1);
249
250     SetSizer( sizer );     
251     SetAutoLayout(true);
252     Layout();
253
254   }
255   //=====================================================================
256
257   //=====================================================================
258   /// Destructor
259   WxTreeView::~WxTreeView()
260   {
261     GimmickDebugMessage(1,"WxTreeView::~WxTreeView"
262                         <<std::endl);
263   }
264   //=====================================================================
265   
266   
267   
268   //=====================================================================
269   const std::vector<tree::Node*>& WxTreeView::GetSelected(int level)
270   {
271     //  if (GetSelectedUpToDate(level)) 
272     int l = level - 1;
273     // the selection of upper level
274         std::vector<tree::Node*>& sel(mLevelList[l].Selected);
275         std::cout<<l<<std::endl;
276     sel.clear();
277           if (level == 1) 
278       {
279         sel.push_back(GetTreeHandler()->GetTree().GetTree());
280       }
281           else if (level < mLevelList.size()+2 ) 
282     {
283                 long item = -1;
284                 for ( ;; )
285                 {
286                         item = GetCtrl(l-1)->GetNextItem(item,
287                                                                                  wxLIST_NEXT_ALL,
288                                                                                  wxLIST_STATE_SELECTED);
289                         if ( item == -1 )
290                                 break;
291                         long adr = GetCtrl(l-1)->GetItemData(item);
292                         tree::Node* n = ((ItemData*)adr)->node;
293                         if(mLastSelected==item)
294                         {
295                                 std::vector<tree::Node*>::iterator it;
296                                 it = sel.begin();
297                                 it = sel.insert ( it , n );
298                         }
299                         else
300                         {
301                                 
302                                 sel.push_back(n);
303                         }                       
304                         
305                 }
306                 /*int n = GetCtrl(l-1)->GetItemCount();
307                 for (int i = 0; i<n; i++)
308                 {
309                         std::cout<<GetCtrl(l-1)->GetItemState(i,wxLIST_STATE_SELECTED)<<std::endl;
310                         if ( GetCtrl(l-1)->GetItemState(i,wxLIST_STATE_SELECTED))
311                         {
312                                 long adr = GetCtrl(l-1)->GetItemData(i);
313                                 tree::Node* n = ((ItemData*)adr)->node;
314                                 if(mLastSelected==i)
315                                 {
316                                         std::vector<tree::Node*>::iterator it;
317                                         it = sel.begin();
318                                         it = sel.insert ( it , n );
319                                 }
320                                 else
321                                 {
322                                         
323                                         sel.push_back(n);
324                                 }
325                         }
326               }*/
327           }
328         else
329         {
330                 // NOTHING
331         }   
332          
333     //    return mLevelList[level-1].Selected;
334     return sel;
335   }
336
337   //=====================================================================
338   
339   //=====================================================================
340   ///Removes selected nodes on last selected level
341    // NOT SPECIFIC 
342   void WxTreeView::RemoveSelected()
343   {
344           unsigned int tempLevel = mLastLevel;
345     mLastLevel+=1;
346     const std::vector<tree::Node*>& sel=GetSelected(mLastLevel+1);
347         // if no selection, no remove action.
348     if(sel.size() != 0)
349         {
350             bool erase=false;
351             std::stringstream out;
352             std::string levelName=GetTreeHandler()->GetTree().GetLevelDescriptor(mLastLevel).GetName();
353             out<<"Delete ";
354             out<<sel.size();
355             if(sel.size()>1&&levelName.at(levelName.size()-1)!='s')
356               {
357                 out<<" "<<levelName;
358                 out<<"s?";
359               }
360             else
361               {
362                 out<<" "<<GetTreeHandler()->GetTree().GetLevelDescriptor(mLastLevel).GetName()<<"?";
363               }
364             if (wxMessageBox(crea::std2wx(out.str()),
365                              _T("Remove Files"),
366                              wxYES_NO,this ) == wxYES)
367               {
368                 erase = true;
369               }
370             if(erase)
371                   {
372             GetGimmickView()->modifyValidationSignal(false);
373                     bool needRefresh=false;
374                     std::vector<tree::Node*>::const_iterator i;
375                     for (i=sel.begin(); i!=sel.end(); ++i)
376                       {
377                         GimmickMessage(1,
378                                        "deleting '"
379                                        <<(*i)->GetLabel()
380                                        <<"'"<<mLastLevel
381                                        <<std::endl);
382                         if((*i)->GetParent()->GetNumberOfChildren()<2)
383                           {
384                             needRefresh=true;
385                           }
386                           tree::Node* n= (tree::Node*)(*i);
387                           GetTreeHandler()->LoadChildren((*i),4);
388                           GetGimmickView()->AddIgnoreFile(n);
389                           GetTreeHandler()->Remove(*i);
390                       }
391                     
392                     if(needRefresh && mLastLevel>1)
393                       {
394                         UpdateLevel(mLastLevel-2);
395                       }
396                     else if(mLastLevel>1)
397                       {
398                         UpdateLevel(mLastLevel-1);
399                       }
400                     else
401                       {
402                         UpdateLevel(mLastLevel);
403                       }
404                   }
405         }
406         else
407         {
408                 // no need to incremente level
409                 mLastLevel = tempLevel;
410         }
411     
412   }
413   
414   
415   //=====================================================================
416   /// Updates a level of the view (adds or removes children, etc.)
417   void WxTreeView::UpdateLevel( int level )
418   {
419     GimmickDebugMessage(1,
420                         GetTreeHandler()->GetTree().GetLabel()
421                         <<"WxTreeView::UpdateLevel(level "
422                         <<level
423                         <<")"
424                         <<std::endl);
425     
426     wxBusyCursor busy;
427     RecursiveUpdateLevel(level);
428     int i;
429     for (i=0; i<level-1; i++)
430       {
431         if (!GetSplitter(i)->IsSplit()) 
432           GetSplitter(i)->SplitVertically(  GetCtrl(i), GetSplitter(i+1),
433                                             100 );
434       }
435     if (GetSplitter(i)->IsSplit()) GetSplitter(i)->Unsplit();    
436     
437   }
438   //=====================================================================
439   
440   //=====================================================================
441   /// Recursive method called upon by UpdateLevel to refresh all windows
442   void WxTreeView::RecursiveUpdateLevel( int level )
443   {
444     GimmickDebugMessage(1,
445                         GetTreeHandler()->GetTree().GetLabel()
446                         <<"WxTreeView::RecursiveUpdateLevel(level "
447                         <<level
448                         <<")"<<std::endl);
449     
450     
451     const std::vector<tree::Node*>& sel(GetSelected(level));
452     
453     int l = level - 1;
454     
455     // to speed up inserting we hide the control temporarily
456     GetCtrl(l)->Hide();
457     GetCtrl(l)->DeleteAllItems();
458     
459     std::vector<tree::Node*>::const_iterator i;
460     
461     for (i=sel.begin(); i!=sel.end(); ++i)
462       {
463         GimmickDebugMessage(1,
464                             "adding children of '"
465                             <<(*i)->GetLabel()
466                             <<"'"
467                             <<std::endl);
468         int _id=0;
469         
470         //Adds items and sets their attributes 
471         
472         GetTreeHandler()->LoadChildren(*i,1);
473         tree::Node::ChildrenListType::reverse_iterator j;
474         for (j = (*i)->GetChildrenList().rbegin(); 
475              j!= (*i)->GetChildrenList().rend(); 
476              ++j)
477           {
478             GimmickDebugMessage(1,
479                                 "adding children "
480                                 <<(*j)->GetLabel()
481                                 <<"'"
482                                 <<std::endl);
483             
484             wxListItem item;
485             item.SetMask(wxLIST_MASK_STATE | 
486                          wxLIST_MASK_TEXT |
487                          //                      wxLIST_MASK_IMAGE |
488                          wxLIST_MASK_DATA |
489                          //                      wxLIST_MASK_WIDTH |
490                          wxLIST_MASK_FORMAT
491                          );
492             
493             ItemData* data = new ItemData;
494             data->node = *j;
495             data->id = _id;
496             
497             item.SetId(_id);
498             item.SetData(data);
499             
500             _id++;
501             GetCtrl(l)->InsertItem(item);
502             
503             //Setting attributes
504             for (int k=0; k<GetCtrl(l)->GetColumnCount(); ++k)                          
505               {
506                 std::string val;
507                 //  Temporary correction : it works but no explanation about the problem FCY
508                 
509                 if(k==0 && level <3)
510                 {
511                   val = (*j)->GetAttribute("NumberOfChildren");
512                 }
513                 else
514                   val = (*j)->GetAttribute(mLevelList[l].key[k]);
515                 if(((*j)->GetAttributeDescriptor(mLevelList[l].key[k])).isDateEntry()) // Date
516                   {
517                     //                                    std::cout << "["<<val<< "]" << std::endl;
518                     std::string valtmp(val);
519                     try
520                       {
521                         boost::gregorian::date d1(boost::gregorian::from_undelimited_string(val));                                 
522                         val = to_iso_extended_string(d1);
523                       }
524                     catch (...)
525                       {
526                         val =  valtmp;
527                       }
528                     //                                    std::cout << "["<<val<< "]" << std::endl;     
529                   }
530                 else if(((*j)->GetAttributeDescriptor(mLevelList[l].key[k])).isTimeEntry()) // Time
531                   {
532                     if ((val.size()>6) && 
533                         (val != "" || val != " "))
534                       val = val.substr(0,2) + " : " 
535                         + val.substr(2,2) + " : " 
536                         + val.substr(4,2);
537                   }
538                 else
539                   {
540                     
541                   }
542                 if (val.size()==0) val = "?";
543                 item.SetText( crea::std2wx(val));
544                 item.SetColumn(k);
545                 GetCtrl(l)->SetItem(item);
546               } 
547             
548           }
549       }
550     
551     SortLevel(l);
552     GetCtrl(l)->Show();
553   }
554   //=====================================================================
555   
556   
557   //================================================================
558   void WxTreeView::OnItemDeSelected(wxListEvent& event)
559   { 
560     GimmickDebugMessage(1,
561                         GetTreeHandler()->GetTree().GetLabel()
562                         <<" WxTreeView::OnItemDeselected"<<std::endl);
563     // retrieve the level
564     wxObject* obj = event.GetEventObject();   
565     unsigned int level = 0;
566     for (level = 0; level<mLevelList.size(); ++level)
567       {
568         if ( GetCtrl(level) == obj ) break;
569       } 
570     SetSelectedUpToDate(level,false);
571     // to allow a first selection in images TreeView
572     if (level==mLevelList.size()-1) 
573       OnItemSelected(event);
574   }
575   //================================================================
576   
577   //================================================================
578   void WxTreeView::OnItemSelected(wxListEvent& event)
579   { 
580           
581     GimmickDebugMessage(1,
582                         GetTreeHandler()->GetTree().GetLabel()
583                         <<" WxTreeView::OnItemSelected"<<std::endl);
584
585     if (mIgnoreSelectedChanged) 
586       {
587         GimmickDebugMessage(1,
588                             " mIgnoreSelectedChanged true: returning"
589                             <<std::endl);
590         return;
591       }
592     
593
594     
595     wxListItem info;
596     info.m_itemId = event.m_itemIndex;
597     mLastSelected = event.m_itemIndex;
598     // retrieve the level
599     wxObject* obj = event.GetEventObject();   
600     unsigned int level = 0;
601     for (level = 0; level<mLevelList.size(); ++level)
602       {
603         if ( GetCtrl(level) == obj ) break;
604       }
605         mLastLevel=level;
606     GimmickDebugMessage(1,
607                         " Level "<<level+1
608                         <<std::endl);
609     
610     // Update the children level (if selection not at last level)
611     if (level<mLevelList.size()-1) 
612       {
613                 
614         UpdateLevel( level + 2 ); 
615         // Reset the viewer setting the default image
616         GetGimmickView()->ClearSelection();
617       }
618     // Select all images if the selection is at series level
619     if (level==mLevelList.size()-2) SelectAll(level+1);
620     // Validate selected images if the selection is at image level
621     if (level==(mLevelList.size()-1)) //&&mProcess) 
622       {
623         if(event.GetEventType()==wxEVT_COMMAND_LIST_ITEM_SELECTED)
624           {
625                   ValidateSelectedImages (true);
626           }
627         else
628           {
629                   ValidateSelectedImages (false);
630           }
631       }
632     
633   }
634   //================================================================
635
636   //================================================================
637   void WxTreeView::SelectAll(int level)
638   {
639     long item = -1;
640     //    int level=mLevelList.size()-1;
641     for ( ;; )
642       {
643         item = GetCtrl(level)->GetNextItem(item,
644                                            wxLIST_NEXT_ALL);
645         if ( item == -1 )
646           break;
647         
648         if(item==(GetCtrl(level)->GetItemCount()-1))
649           {
650             mIgnoreSelectedChanged = false;//mProcess=true;
651           }
652         else
653           {
654             mIgnoreSelectedChanged = true;//    mProcess=false;
655           }
656         GetCtrl(level)->SetItemState(item,wxLIST_STATE_SELECTED, wxLIST_MASK_STATE 
657                                      | wxLIST_MASK_TEXT |wxLIST_MASK_IMAGE | wxLIST_MASK_DATA | wxLIST_MASK_WIDTH | wxLIST_MASK_FORMAT);
658       }
659   }
660
661   //================================================================
662   //================================================================
663
664   void WxTreeView::OnColClick(wxListEvent& event)
665   { 
666     mColumnSelected = event.m_col;
667     wxPoint clientpt;
668     clientpt.x = wxGetMousePosition().x - this->GetScreenPosition().x;
669     clientpt.y = wxGetMousePosition().y - this->GetScreenPosition().y;
670     senderCtrl = event.GetEventObject(); 
671     unsigned int level = 0;
672     for (level = 0; level<mLevelList.size(); ++level)
673       {
674         if ( GetCtrl(level) == senderCtrl ) break;
675       }
676     PopupMenu(menu, clientpt);
677     
678   }
679
680   void WxTreeView::OnItemMenu(wxListEvent &event)
681   {
682          wxPoint clientpt;
683     clientpt.x = wxGetMousePosition().x - this->GetScreenPosition().x;
684     clientpt.y = wxGetMousePosition().y - this->GetScreenPosition().y;
685     senderCtrl = event.GetEventObject(); 
686     unsigned int level = 0;
687     for (level = 0; level<mLevelList.size(); ++level)
688       {
689                 if ( GetCtrl(level) == senderCtrl ) break;
690       }
691     PopupMenu(menuItem, clientpt);
692     
693   }
694   
695   //================================================================
696   //================================================================
697
698   void WxTreeView::OnPopupFilter(wxCommandEvent& event)
699   {
700     wxBusyCursor busy;
701     GimmickDebugMessage(7,
702                         "WxTreeView::OnEndLabelEdit" 
703                         <<std::endl);
704     unsigned int level = 0;
705     for (level = 0; level<mLevelList.size(); ++level)
706       {
707         if ( GetCtrl(level) == senderCtrl ) break;
708       }
709     std::string filter = crea::wx2std(wxGetTextFromUser(_T("Enter the filter to apply"), _T("Filter On Column")));
710     
711     std::string att;
712     
713     long it = -1;
714     UpdateLevel(level+1);
715     
716     std::vector<long> items;
717     bool in=false;
718     int del=0;
719     for ( ;; )
720       {
721         it = GetCtrl(level)->GetNextItem(it,
722                                          wxLIST_NEXT_ALL);
723         if ( it == -1 )
724           break;
725         
726         long adr = GetCtrl(level)->GetItemData(it);
727         tree::Node* nod = ((ItemData*)adr)->node;
728         att=(*nod).GetAttribute(mLevelList[level].key[mColumnSelected]);
729         
730         
731         if(att.find(filter)>900)
732           {
733             
734             if(!in)
735               {
736                 in=true;
737               }
738             else
739               {
740                 del+=1;
741               }
742             
743             items.push_back(it-del);
744           }
745         
746       }
747     std::vector<long>::iterator iter;
748     for(iter=items.begin();iter!=items.end();++iter)
749       {
750         GetCtrl(level)->DeleteItem(*iter);
751       }
752     GetGimmickView()->ClearSelection();
753   }
754   //================================================================
755   
756   //================================================================
757   void WxTreeView::OnPopupSort(wxCommandEvent& event)
758   {
759     wxBusyCursor busy;
760     unsigned int level = 0;
761     for (level = 0; level<mLevelList.size(); ++level)
762       {
763         if ( GetCtrl(level) == senderCtrl ) break;
764       }
765     mLevelList[level].SortColumn = mColumnSelected;
766
767     if(event.GetId()==mAscendingID)
768       {
769         mLevelList[level].SortAscending = true;
770       }
771     else if(event.GetId()==mDescendingID)
772       {
773         mLevelList[level].SortAscending = false;
774       }
775           
776     SortLevel(level);
777   }
778   //================================================================
779
780   //================================================================
781   void WxTreeView::OnLocalCopy(wxCommandEvent& event)
782   {
783     wxBusyCursor busy;
784     
785         unsigned int tempLevel = mLastLevel;
786     mLastLevel+=1;
787     const std::vector<tree::Node*>& sel=GetSelected(mLastLevel+1);
788         
789     if(sel.size() != 0)
790         {
791             bool copy=false;
792             std::stringstream out;
793             std::string levelName=GetTreeHandler()->GetTree().GetLevelDescriptor(mLastLevel).GetName();
794             out<<"Copy ";
795             out<<sel.size();
796             if(sel.size()>1&&levelName.at(levelName.size()-1)!='s')
797               {
798                 out<<" "<<levelName;
799                 out<<"s to .gimmick?";
800               }
801             else
802               {
803                 out<<" "<<GetTreeHandler()->GetTree().GetLevelDescriptor(mLastLevel).GetName()<<" to .gimmick?";
804               }
805             if (wxMessageBox(crea::std2wx(out.str()),
806                              _T("Remove Files"),
807                              wxYES_NO,this ) == wxYES)
808               {
809                 copy = true;
810               }
811             if(copy)
812                   {
813                         std::vector<std::string> s;
814                         GetFilenamesAsString(sel,s);
815             GetGimmickView()->CopyFiles(s);
816                   }
817         }
818         else
819         {
820                 mLastLevel = tempLevel;
821         }
822     
823     
824   }
825   //================================================================
826
827   //================================================================
828   void WxTreeView::SortLevel(int level)
829   {       
830     GimmickDebugMessage(1,
831                         "WxTreeView::SortLevel(" 
832                         <<level<<")"
833                         <<std::endl);  
834     //Obtain the column name and the level that needs to be organized
835     
836     //    int l = level - 1;
837     //Sets the data for the items to be sorted
838     //    std::string att;
839     unsigned int ty=0;
840     int nbselected = 0;
841     int n = GetCtrl(level)->GetItemCount();
842     for (int i = 0; i < n; i++)
843       {
844         
845         //Gets current item data
846         ItemData* data = (ItemData*)GetCtrl(level)->GetItemData(i);
847         
848         //Extracts the node and the type of attribute   
849         tree::Node* nod = data->node;
850         if(i==0)
851           {
852             (*nod).GetAttributeDescriptor
853               (mLevelList[level].key[mLevelList[level].SortColumn])
854               .DecodeType( ty );
855           }
856         //Obtains the organizing attribute
857         data->attr = & (*nod).GetAttribute
858           (mLevelList[level].key[mLevelList[level].SortColumn]);
859         //Selected ?
860         data->selected = false;
861         if (GetCtrl(level)->GetItemState(i,wxLIST_STATE_SELECTED)>0)
862           {
863             data->selected = true;
864             nbselected++;
865           }
866
867       } 
868     GimmickDebugMessage(1,
869                         "WxTreeView::OnSort : " 
870                         <<nbselected<<" selected before sorting"
871                         <<std::endl);  
872
873     mIgnoreSelectedChanged = true; 
874     // 
875     if (mLevelList[level].SortAscending)
876       {
877         
878         if(ty==1)
879           {
880             GetCtrl(level)->SortItems(CompareFunctionInts, 0);
881           }
882         else
883           {
884             GetCtrl(level)->SortItems(CompareFunctionStrings, 0);
885           }
886         
887       }
888     else
889       {
890         if(ty==1)
891           {
892             GetCtrl(level)->SortItems(CompareFunctionInts, 1);
893           }
894         else
895           {
896             GetCtrl(level)->SortItems(CompareFunctionStrings, 1);
897           }
898       }
899  
900
901     // Reselects the unselected 
902     n = GetCtrl(level)->GetItemCount();
903     int after = 0;
904     for (int i = 0; i < n; i++)
905       {
906         
907         //Gets current item data
908         ItemData* data = (ItemData*)GetCtrl(level)->GetItemData(i);
909   
910         //  long item = -1;
911         //    for ( ;; )
912         //      {
913         //      item = GetCtrl(level)->GetNextItem(item,wxLIST_NEXT_ALL);
914         //      if ( item == -1 ) break;
915         //Gets current item data
916         //      ItemData* data = (ItemData*)GetCtrl(level)->GetItemData(item);
917         // was selected ?
918         
919         if (data->selected)
920           {
921             nbselected--;
922             if (nbselected==0)
923               {
924                 // if it is the last one we must process the selection
925                 mIgnoreSelectedChanged = false;
926               }
927             GetCtrl(level)->SetItemState(i,
928                                          wxLIST_STATE_SELECTED, 
929                                          wxLIST_MASK_STATE 
930                                          | wxLIST_MASK_TEXT 
931                                          | wxLIST_MASK_IMAGE 
932                                          | wxLIST_MASK_DATA 
933                                          | wxLIST_MASK_WIDTH 
934                                          | wxLIST_MASK_FORMAT);   
935           }
936         if (GetCtrl(level)->GetItemState(i,wxLIST_STATE_SELECTED)>0)
937           {
938             after++;
939           }
940
941         
942       }
943     mIgnoreSelectedChanged = false; 
944      GimmickDebugMessage(1,
945                         "WxTreeView::SortLevel : " 
946                         <<after<<" selected after sorting"
947                         <<std::endl);  
948   
949   }
950   //================================================================
951
952   
953   //================================================================
954   void WxTreeView::ValidateSelectedImages(bool isSelection)
955   {
956     GimmickDebugMessage(7,
957                         "WxTreeView::ValidateSelectedImages" 
958                         <<std::endl);
959     const std::vector<tree::Node*>& sel(GetSelected(mLevelList.size()+1));
960     GetGimmickView()->OnSelectionChange(sel,
961                                         isSelection,(mLastSelected-1),
962                                         !mIgnoreSelectedChanged);
963  
964   }
965   //================================================================
966
967
968   //================================================================
969   void WxTreeView::GetNodes(std::vector<tree::Node*>& nodes, bool direction)
970   {
971         long item = mLastSelected;
972         int level=mLevelList.size()-1;
973         //Gets current item data
974         long adr = GetCtrl(level)->GetItemData(item);
975         //Extracts the node
976         tree::Node* nod = ((ItemData*)adr)->node;
977     for ( ;; )
978     {
979                 if(direction)
980                 {
981                         item = GetCtrl(level)->GetNextItem(item,
982                                      wxLIST_NEXT_ABOVE);
983                 }
984                 else
985                 {
986                         item = GetCtrl(level)->GetNextItem(item,
987                                      wxLIST_NEXT_BELOW);
988                 }
989         if ( item == -1 || item==0  )
990                 {
991             break;
992                 }
993                 if(GetCtrl(level)->GetItemState(item, wxLIST_STATE_SELECTED)==0 )
994                 {
995
996                         adr = GetCtrl(level)->GetItemData(item);
997                         nod = ((ItemData*)adr)->node;
998                         nodes.push_back(nod);
999                 }
1000     }
1001
1002   }
1003   //================================================================
1004    //=================================================
1005   void WxTreeView::OnKeyDown(wxListEvent &event)
1006   {
1007           if(event.GetKeyCode() == WXK_DELETE)
1008           {
1009                    wxBusyCursor busy;
1010                   
1011                    RemoveSelected();
1012                    GetGimmickView()->ClearSelection();
1013           }
1014                   
1015   }
1016   //================================================================
1017
1018   //================================================================
1019   // Should be in another place : not specific !
1020   void WxTreeView::GetSelectedAsString(std::vector<std::string>&s)
1021   {
1022     int level=mLevelList.size();
1023     const std::vector<tree::Node*>& sel=GetSelected(level+1);
1024     std::vector<tree::Node*>::const_iterator i;
1025     
1026     for (i=sel.begin(); i!=sel.end(); ++i)
1027       {
1028         std::string filename=(*i)->GetAttribute("FullFileName");
1029         s.push_back(filename);
1030       }
1031   }
1032
1033   //================================================================
1034   void WxTreeView::GetFilenamesAsString(const std::vector<tree::Node*>& nodes, std::vector<std::string>&s)
1035   {
1036     std::vector<tree::Node*>::const_iterator i;
1037     
1038     for (i=nodes.begin(); i!=nodes.end(); ++i)
1039       {
1040                   if((*i)->GetLevel()<mLevelList.size())
1041                   {
1042                          GetTreeHandler()->LoadChildren(*i,0);
1043                          GetFilenamesAsString((*i)->GetChildrenList(),s);
1044                   }
1045                   else
1046                   {
1047                         std::string filename=(*i)->GetAttribute("FullFileName");
1048                         s.push_back(filename);
1049                   }
1050       }
1051   }
1052
1053    //================================================================
1054   void WxTreeView::SetColor(int l, int item)
1055   {
1056           int colorId=12;
1057           GetCtrl(l)->SetItemTextColour(item, wxColourDatabase().Find
1058                    (crea::std2wx(mColorPalette[colorId])));
1059           GetCtrl(l)->SetItemState(item,wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED);  /*
1060           int colorId=0;
1061           //Setting the color according to the parent
1062                 if(l==0)
1063                 {
1064                 item.SetBackgroundColour
1065                   (wxColourDatabase().Find
1066                    (crea::std2wx(mColorPalette[colorId]))); 
1067                 mColorMap.insert
1068                   (NodeColorPair
1069                    (*j,wxColourDatabase().Find
1070                     (crea::std2wx(mColorPalette[colorId]))));
1071                 if(colorId<64)
1072                   {
1073                     colorId++;
1074                   }
1075                 else
1076                         {
1077                           colorId=0;
1078                         }
1079                 }
1080                 else if(l!=mLevelList.size()-1)
1081                   {
1082                     item.SetBackgroundColour(mColorMap[*i]); 
1083                         mColorMap.insert(NodeColorPair(*j,mColorMap[*i]));
1084                 }
1085                 else
1086                 {
1087                         item.SetBackgroundColour(mColorMap[*i]); 
1088                 }*/
1089   }
1090   //================================================================
1091   void WxTreeView::CreateColorPalette()
1092   {
1093   GimmickDebugMessage(6,"WxTreeView::CreateColorPalette");
1094   mColorPalette.push_back("WHITE");
1095   mColorPalette.push_back("LIGHT GREY");
1096   mColorPalette.push_back("AQUAMARINE");
1097   mColorPalette.push_back("MEDIUM FOREST GREEN");
1098   mColorPalette.push_back("INDIAN RED");
1099   mColorPalette.push_back("KHAKI");
1100   mColorPalette.push_back("ORANGE");
1101   mColorPalette.push_back("LIGHT BLUE");
1102   mColorPalette.push_back("LIGHT STEEL BLUE");
1103   mColorPalette.push_back("PINK");
1104   mColorPalette.push_back("PLUM");
1105   mColorPalette.push_back("PURPLE");
1106   mColorPalette.push_back("RED");
1107   mColorPalette.push_back("SEA GREEN");
1108   mColorPalette.push_back("SIENNA");
1109   mColorPalette.push_back("SKY BLUE");
1110   mColorPalette.push_back("SLATE BLUE");
1111   mColorPalette.push_back("SPRING GREEN");
1112   mColorPalette.push_back("TAN");
1113   mColorPalette.push_back("THISTLE");
1114   mColorPalette.push_back("TURQUOISE");
1115   mColorPalette.push_back("VIOLET");
1116   mColorPalette.push_back("VIOLET RED");
1117   mColorPalette.push_back("WHEAT");
1118   mColorPalette.push_back("YELLOW");
1119   mColorPalette.push_back("YELLOW GREEN");
1120   mColorPalette.push_back("BLUE");
1121   mColorPalette.push_back("BLUE VIOLET");
1122   mColorPalette.push_back("BROWN");
1123   mColorPalette.push_back("CADET BLUE");
1124   mColorPalette.push_back("CORAL");
1125   mColorPalette.push_back("CORNFLOWER BLUE");
1126   mColorPalette.push_back("CYAN");
1127   mColorPalette.push_back("DARK GREY");
1128   mColorPalette.push_back("DARK GREEN");
1129   mColorPalette.push_back("DARK OLIVE GREEN");
1130   mColorPalette.push_back("DARK ORCHID");
1131   mColorPalette.push_back("DARK SLATE BLUE");
1132   mColorPalette.push_back("DARK SLATE GREY");
1133   mColorPalette.push_back("DARK TURQUOISE");
1134   mColorPalette.push_back("FIREBRICK");
1135   mColorPalette.push_back("FOREST GREEN");
1136   mColorPalette.push_back("GOLD");
1137   mColorPalette.push_back("GOLDENROD");
1138   mColorPalette.push_back("GREY");
1139   mColorPalette.push_back("GREEN");
1140   mColorPalette.push_back("GREEN YELLOW");
1141   mColorPalette.push_back("LIME GREEN");
1142   mColorPalette.push_back("MAGENTA");
1143   mColorPalette.push_back("MAROON");
1144   mColorPalette.push_back("MEDIUM AQUAMARINE");
1145   mColorPalette.push_back("MEDIUM BLUE");
1146   mColorPalette.push_back("MEDIUM GOLDENROD");
1147   mColorPalette.push_back("MEDIUM ORCHID");
1148   mColorPalette.push_back("MEDIUM SEA GREEN");
1149   mColorPalette.push_back("MEDIUM SLATE BLUE");
1150   mColorPalette.push_back("MEDIUM SPRING GREEN");
1151   mColorPalette.push_back("MEDIUM TURQUOISE");
1152   mColorPalette.push_back("MEDIUM VIOLET RED");
1153   mColorPalette.push_back("MIDNIGHT BLUE");
1154   mColorPalette.push_back("NAVY");
1155   mColorPalette.push_back("ORANGE RED");
1156   mColorPalette.push_back("ORCHID, PALE GREEN");
1157   mColorPalette.push_back("STEEL BLUE");
1158   mColorPalette.push_back("BLACK");
1159
1160
1161   }
1162   //================================================================
1163   BEGIN_EVENT_TABLE(WxTreeView, wxPanel)   
1164   /*
1165     EVT_SIZE(MyFrame::OnSize)
1166
1167     EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
1168     EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
1169     EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
1170     EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
1171     EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
1172     EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
1173     EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
1174     EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
1175     EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
1176     EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView)
1177
1178     EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
1179     EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
1180     EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
1181     EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
1182     EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
1183     EVT_MENU(LIST_ADD, MyFrame::OnAdd)
1184     EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
1185     EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
1186     EVT_MENU(LIST_SORT, MyFrame::OnSort)
1187     EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
1188     EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
1189     EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
1190     EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
1191     EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
1192     EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
1193     EVT_MENU(LIST_THAW, MyFrame::OnThaw)
1194     EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
1195     EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
1196
1197     EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
1198     EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
1199 END_EVENT_TABLE()
1200
1201 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
1202     EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
1203     EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
1204         
1205     EVT_LIST_BEGIN_LABEL_EDIT(-1, WxTreeView::OnBeginLabelEdit)
1206     EVT_LIST_END_LABEL_EDIT(-1, WxTreeView::OnEndLabelEdit)
1207         
1208     EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
1209     EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
1210 #if WXWIN_COMPATIBILITY_2_4
1211     EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
1212     EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
1213 #endif
1214   */
1215     EVT_LIST_KEY_DOWN(-1, WxTreeView::OnKeyDown)
1216     EVT_LIST_ITEM_SELECTED(-1, WxTreeView::OnItemSelected)
1217         EVT_LIST_ITEM_RIGHT_CLICK(-1, WxTreeView::OnItemMenu)
1218     EVT_LIST_ITEM_DESELECTED(-1, WxTreeView::OnItemDeSelected)
1219         /*
1220     EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
1221     EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
1222     EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
1223 */
1224     EVT_LIST_COL_RIGHT_CLICK(-1, WxTreeView::OnColClick)
1225         
1226     EVT_LIST_COL_CLICK(-1, WxTreeView::OnColClick)
1227
1228         //EVT_LEFT_DOWN(WxTreeView::OnMouseClick)
1229         /*
1230     EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
1231     EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
1232     EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
1233
1234     EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
1235
1236 #if USE_CONTEXT_MENU
1237     EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
1238 #endif
1239     EVT_CHAR(MyListCtrl::OnChar)
1240
1241     EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
1242   */
1243 END_EVENT_TABLE()
1244   
1245 } // EO namespace creaImageIO
1246