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