]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUIPackageBrowser2.cxx
f80ec6f8586dff56e3d2854c1db5033a782652e6
[bbtk.git] / kernel / src / bbtkWxGUIPackageBrowser2.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkWxGUIPackageBrowser2.cxx,v $
4   Language:  C++
5   Date:      $Date: 2010/04/13 14:20:30 $
6   Version:   $Revision: 1.19 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31
32 /* \brief Short description in one line
33  * 
34  * Long description which 
35  * can span multiple lines
36  */
37  
38 /**
39  * \file 
40  * \brief 
41  */
42 /**
43  * \class bbtk::
44  * \brief 
45  */
46
47
48 #ifdef _USE_WXWIDGETS_
49
50 #define CHECKBOXVIEW 1
51
52 #include "bbtkWxGUIPackageBrowser2.h"
53 //#include "ThirdParty/wx/treemultictrl/wxTreeMultiCtrl.h"
54 #include "bbtkInterpreter.h"
55 #include "bbtkBlackBoxInputDescriptor.h"
56 #include "bbtkBlackBoxOutputDescriptor.h"
57 #include "bbtkWxBlackBox.h"
58 //#include "wx/grid.h"
59 //#include <wx/statline.h>
60
61 #define LIST_CTRL 1000
62
63
64 namespace bbtk
65 {
66
67
68   BEGIN_EVENT_TABLE(WxGUIBlackBoxList, wxListCtrl)
69     EVT_LIST_BEGIN_DRAG(LIST_CTRL, WxGUIBlackBoxList::OnBeginDrag)
70     EVT_LIST_BEGIN_RDRAG(LIST_CTRL, WxGUIBlackBoxList::OnBeginRDrag)
71     EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, WxGUIBlackBoxList::OnBeginLabelEdit)
72     EVT_LIST_END_LABEL_EDIT(LIST_CTRL, WxGUIBlackBoxList::OnEndLabelEdit)
73     EVT_LIST_DELETE_ITEM(LIST_CTRL, WxGUIBlackBoxList::OnDeleteItem)
74     EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, WxGUIBlackBoxList::OnDeleteAllItems)
75 #if WXWIN_COMPATIBILITY_2_4
76     EVT_LIST_GET_INFO(LIST_CTRL, WxGUIBlackBoxList::OnGetInfo)
77     EVT_LIST_SET_INFO(LIST_CTRL, WxGUIBlackBoxList::OnSetInfo)
78 #endif
79     EVT_LIST_ITEM_SELECTED(LIST_CTRL, WxGUIBlackBoxList::OnSelected)
80     EVT_LIST_ITEM_DESELECTED(LIST_CTRL, WxGUIBlackBoxList::OnDeselected)
81     EVT_LIST_KEY_DOWN(LIST_CTRL, WxGUIBlackBoxList::OnListKeyDown)
82     EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, WxGUIBlackBoxList::OnActivated)
83     EVT_LIST_ITEM_FOCUSED(LIST_CTRL, WxGUIBlackBoxList::OnFocused)
84     
85     EVT_LIST_COL_CLICK(LIST_CTRL, WxGUIBlackBoxList::OnColClick)
86     EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, WxGUIBlackBoxList::OnColRightClick)
87     EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, WxGUIBlackBoxList::OnColBeginDrag)
88     EVT_LIST_COL_DRAGGING(LIST_CTRL, WxGUIBlackBoxList::OnColDragging)
89     EVT_LIST_COL_END_DRAG(LIST_CTRL, WxGUIBlackBoxList::OnColEndDrag)
90     
91     EVT_LIST_CACHE_HINT(LIST_CTRL, WxGUIBlackBoxList::OnCacheHint)
92     
93 #if USE_CONTEXT_MENU
94     EVT_CONTEXT_MENU(WxGUIBlackBoxList::OnContextMenu)
95 #endif
96     EVT_CHAR(WxGUIBlackBoxList::OnChar)
97     
98     EVT_RIGHT_DOWN(WxGUIBlackBoxList::OnRightClick)
99     END_EVENT_TABLE()
100     
101     int wxCALLBACK MyCompareFunction(long item1, long item2, long WXUNUSED(sortData))
102   {
103     // inverse the order
104     if (item1 < item2)
105       return -1;
106     if (item1 > item2)
107       return 1;
108     
109     return 0;
110   }
111   
112   // WxGUIBlackBoxList
113   WxGUIBlackBoxList::WxGUIBlackBoxList(wxWindow *parent,
114                                  const wxWindowID id,
115                                  const wxPoint& pos,
116                                  const wxSize& size,
117                                  long style)
118     : wxListCtrl(parent, id, pos, size, style),
119       mUser(0),
120       m_attr(*wxBLUE, *wxLIGHT_GREY, wxNullFont)
121   {
122 #ifdef __POCKETPC__
123     EnableContextMenu();
124 #endif
125   }
126   
127   void WxGUIBlackBoxList::Insert(BlackBoxDescriptor::Pointer d)
128   {
129     if (GetColumnCount()!=3)
130       {
131         InsertColumn( 0, _("Package"), 
132                       wxLIST_FORMAT_LEFT, 60  ); 
133         InsertColumn( 1, _("Box"), 
134                       wxLIST_FORMAT_LEFT, 200 ); 
135         InsertColumn( 2, _("Description"), 
136                       wxLIST_FORMAT_LEFT, 500  ); 
137       }
138     
139     wxListItem kNewItem;
140     kNewItem.SetAlign(wxLIST_FORMAT_LEFT);
141     
142     int nID = this->GetItemCount();
143     kNewItem.SetId(nID);
144     kNewItem.SetMask(wxLIST_MASK_DATA);
145     // TO DO : STORE SMART POINTER ?
146     kNewItem.SetData(d.get());  
147     //    BlackBoxDescriptor::Pointer* d2 = (BlackBoxDescriptor::Pointer*)kNewItem.GetData();
148     //    std::cout << "Descr = "<<d<<" = "<<d2<<std::endl;
149     this->InsertItem(kNewItem);
150     this->SetItem(nID, 0, std2wx(d->GetPackage()->GetName()) );
151     this->SetItem(nID, 1, std2wx(d->GetTypeName()) );
152     this->SetItem(nID, 2, std2wx(d->GetDescription()) );
153     
154     /*
155       if (nID % 2)
156       {
157       kNewItem.SetBackgroundColour(wxColour(192,192,192));
158       this->SetItem(kNewItem);
159       }
160     */
161     
162   }
163   
164   
165   void WxGUIBlackBoxList::OnCacheHint(wxListEvent& event)
166   {
167     //wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
168     //                  event.GetCacheFrom(), event.GetCacheTo() );
169   }
170   
171   void WxGUIBlackBoxList::SetColumnImage(int col, int image)
172   {
173     /*
174       wxListItem item;
175       item.SetMask(wxLIST_MASK_IMAGE);
176       item.SetImage(image);
177       SetColumn(col, item);
178     */
179   }
180
181   void WxGUIBlackBoxList::OnColClick(wxListEvent& event)
182   {
183     int col = event.GetColumn();
184
185     // set or unset image
186     static bool x = false;
187     x = !x;
188     SetColumnImage(col, x ? 0 : -1);
189
190     //    wxLogMessage( wxT("OnColumnClick at %d."), col );
191   }
192
193   void WxGUIBlackBoxList::OnColRightClick(wxListEvent& event)
194   {
195     int col = event.GetColumn();
196     if ( col != -1 )
197       {
198         SetColumnImage(col, -1);
199       }
200
201     // Show popupmenu at position
202     wxMenu menu(wxT("Test"));
203     menu.Append(-1, _T("&About"));
204     PopupMenu(&menu, event.GetPoint());
205
206     //wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() );
207   }
208
209   void WxGUIBlackBoxList::LogColEvent(const wxListEvent& event, const wxChar *name)
210   {
211     //    const int col = event.GetColumn();
212
213     /*
214       wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
215       name,
216       col,
217       event.GetItem().GetWidth(),
218       GetColumnWidth(col));
219     */
220   }
221
222   void WxGUIBlackBoxList::OnColBeginDrag(wxListEvent& event)
223   {
224     LogColEvent( event, wxT("OnColBeginDrag") );
225     /*
226       if ( event.GetColumn() == 0 )
227       {
228       //wxLogMessage(_T("Resizing this column shouldn't work."));
229
230       event.Veto();
231       }
232     */
233   }
234
235   void WxGUIBlackBoxList::OnColDragging(wxListEvent& event)
236   {
237     LogColEvent( event, wxT("OnColDragging") );
238   }
239
240   void WxGUIBlackBoxList::OnColEndDrag(wxListEvent& event)
241   {
242     LogColEvent( event, wxT("OnColEndDrag") );
243   }
244
245   void WxGUIBlackBoxList::OnBeginDrag(wxListEvent& event)
246   {
247           std::cout<<"RaC DRAG TABLE"<<std::endl;
248           
249           wxString text(wxT(this->GetItemText(event.GetIndex())));
250           wxListItem info;
251           info.m_itemId = event.m_itemIndex;
252           info.m_col = 0;
253           info.m_mask = wxLIST_MASK_DATA;
254           wxString sendtext(wxT(""));
255           if ( GetItem(info) )
256           {
257                   // TO DO : STORE SMART PTR ?
258                   BlackBoxDescriptor* d = (BlackBoxDescriptor*)(info.GetData());
259                   if (d!=0)
260                   {
261                           sendtext += wxT(d->GetFullTypeName());
262                   }
263           }
264           else
265           {
266                   wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
267           }
268
269         
270           wxTextDataObject tdo(wxT(sendtext));
271           wxDropSource tds(tdo, this);
272           tds.DoDragDrop();
273           
274          
275
276     //    const wxPoint& pt = event.m_pointDrag;
277
278     //wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
279     //          pt.x, pt.y, HitTest(pt, flags) );
280   }
281
282   void WxGUIBlackBoxList::OnBeginRDrag(wxListEvent& event)
283   {
284     //wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
285     //        event.m_pointDrag.x, event.m_pointDrag.y );
286   }
287
288   void WxGUIBlackBoxList::OnBeginLabelEdit(wxListEvent& event)
289   {
290     //wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
291   }
292
293   void WxGUIBlackBoxList::OnEndLabelEdit(wxListEvent& event)
294   {
295     //wxLogMessage( wxT("OnEndLabelEdit: %s"),
296     //        event.IsEditCancelled() ? _T("[cancelled]")
297     //                                      : event.m_item.m_text.c_str());
298   }
299
300   void WxGUIBlackBoxList::OnDeleteItem(wxListEvent& event)
301   {
302     LogEvent(event, _T("OnDeleteItem"));
303     std::cout << "cannot del"<<std::endl;
304     event.Veto();
305     //wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
306   }
307
308   void WxGUIBlackBoxList::OnDeleteAllItems(wxListEvent& event)
309   {
310     LogEvent(event, _T("OnDeleteAllItems"));
311     event.Veto();
312   }
313
314 #if WXWIN_COMPATIBILITY_2_4
315   void WxGUIBlackBoxList::OnGetInfo(wxListEvent& event)
316   {
317     wxString msg;
318
319     msg << _T("OnGetInfo (") << event.m_item.m_itemId << _T(", ") << event.m_item.m_col << _T(")");
320     if ( event.m_item.m_mask & wxLIST_MASK_STATE )
321       msg << _T(" wxLIST_MASK_STATE");
322     if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
323       msg << _T(" wxLIST_MASK_TEXT");
324     if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
325       msg << _T(" wxLIST_MASK_IMAGE");
326     if ( event.m_item.m_mask & wxLIST_MASK_DATA )
327       msg << _T(" wxLIST_MASK_DATA");
328     if ( event.m_item.m_mask & wxLIST_SET_ITEM )
329       msg << _T(" wxLIST_SET_ITEM");
330     if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
331       msg << _T(" wxLIST_MASK_WIDTH");
332     if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
333       msg << _T(" wxLIST_MASK_WIDTH");
334
335     if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
336       {
337         event.m_item.m_text = _T("My callback text");
338       }
339
340     //wxLogMessage(msg);
341   }
342
343   void WxGUIBlackBoxList::OnSetInfo(wxListEvent& event)
344   {
345     LogEvent(event, _T("OnSetInfo"));
346   }
347 #endif
348
349   void WxGUIBlackBoxList::OnSelected(wxListEvent& event)
350   {
351     if (mUser==0) return;
352
353     wxListItem info;
354     info.m_itemId = event.m_itemIndex;
355     info.m_col = 0;
356     info.m_mask = wxLIST_MASK_DATA;
357     if ( GetItem(info) )
358       {
359         // TO DO : STORE SMART PTR ?
360         BlackBoxDescriptor* d = (BlackBoxDescriptor*)(info.GetData());
361         if (d!=0) mUser->WxGUIBlackBoxListUserOnSelected(d);
362       }
363     else
364       {
365         wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
366       }
367   }
368
369   void WxGUIBlackBoxList::OnDeselected(wxListEvent& event)
370   {
371     LogEvent(event, _T("OnDeselected"));
372   }
373
374   void WxGUIBlackBoxList::OnActivated(wxListEvent& event)
375   {
376     LogEvent(event, _T("OnActivated"));
377   }
378
379   void WxGUIBlackBoxList::OnFocused(wxListEvent& event)
380   {
381     LogEvent(event, _T("OnFocused"));
382
383     event.Skip();
384   }
385
386   void WxGUIBlackBoxList::OnListKeyDown(wxListEvent& event)
387   {
388     long item;
389
390     switch ( event.GetKeyCode() )
391       {
392       case 'c': // colorize
393       case 'C':
394         {
395           wxListItem info;
396           info.m_itemId = event.GetIndex();
397           if ( info.m_itemId == -1 )
398             {
399               // no item
400               break;
401             }
402
403           GetItem(info);
404
405           wxListItemAttr *attr = info.GetAttributes();
406           if ( !attr || !attr->HasTextColour() )
407             {
408               info.SetTextColour(*wxCYAN);
409
410               SetItem(info);
411
412               RefreshItem(info.m_itemId);
413             }
414         }
415         break;
416
417       case 'n': // next
418       case 'N':
419         item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
420         if ( item++ == GetItemCount() - 1 )
421           {
422             item = 0;
423           }
424
425         //wxLogMessage(_T("Focusing item %ld"), item);
426
427         SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
428         EnsureVisible(item);
429         break;
430
431       case 'r': // show bounding Rect
432       case 'R':
433         {
434           item = event.GetIndex();
435           wxRect r;
436           if ( !GetItemRect(item, r) )
437             {
438               //wxLogError(_T("Failed to retrieve rect of item %ld"), item);
439               break;
440             }
441
442           //wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
443           //   item, r.x, r.y, r.x + r.width, r.y + r.height);
444         }
445         break;
446
447       case WXK_DELETE:
448         item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
449         /*
450           while ( item != -1 )
451           {
452           DeleteItem(item);
453
454           //wxLogMessage(_T("Item %ld deleted"), item);
455
456           // -1 because the indices were shifted by DeleteItem()
457           item = GetNextItem(item - 1,
458           wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
459           }
460         */
461         break;
462
463       case WXK_INSERT:
464         if ( GetWindowStyle() & wxLC_REPORT )
465           {
466             if ( GetWindowStyle() & wxLC_VIRTUAL )
467               {
468                 SetItemCount(GetItemCount() + 1);
469               }
470             else // !virtual
471               {
472                 //InsertItemInReportView(event.GetIndex());
473               }
474           }
475         //else: fall through
476
477       default:
478         LogEvent(event, _T("OnListKeyDown"));
479
480         event.Skip();
481       }
482   }
483
484   void WxGUIBlackBoxList::OnChar(wxKeyEvent& event)
485   {
486     //wxLogMessage(_T("Got char event."));
487
488     switch ( event.GetKeyCode() )
489       {
490       case 'n':
491       case 'N':
492       case 'c':
493       case 'C':
494         // these are the keys we process ourselves
495         break;
496
497       default:
498         event.Skip();
499       }
500   }
501
502   void WxGUIBlackBoxList::OnRightClick(wxMouseEvent& event)
503   {
504     if ( !event.ControlDown() )
505       {
506         event.Skip();
507         return;
508       }
509
510     int flags;
511     long subitem;
512     //    long item = 
513     HitTest(event.GetPosition(), flags, &subitem);
514
515     wxString where;
516     switch ( flags )
517       {
518       case wxLIST_HITTEST_ABOVE: where = _T("above"); break;
519       case wxLIST_HITTEST_BELOW: where = _T("below"); break;
520       case wxLIST_HITTEST_NOWHERE: where = _T("nowhere near"); break;
521       case wxLIST_HITTEST_ONITEMICON: where = _T("on icon of"); break;
522       case wxLIST_HITTEST_ONITEMLABEL: where = _T("on label of"); break;
523       case wxLIST_HITTEST_ONITEMRIGHT: where = _T("right on"); break;
524       case wxLIST_HITTEST_TOLEFT: where = _T("to the left of"); break;
525       case wxLIST_HITTEST_TORIGHT: where = _T("to the right of"); break;
526       default: where = _T("not clear exactly where on"); break;
527       }
528
529     //wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
530     //             where.c_str(), item, subitem);
531   }
532
533   void WxGUIBlackBoxList::LogEvent(const wxListEvent& event, const wxChar *eventName)
534   {
535     //wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
536     //            event.GetIndex(), eventName,
537     //              event.GetText().c_str(), event.GetData());
538   }
539
540   wxString WxGUIBlackBoxList::OnGetItemText(long item, long column) const
541   {
542     /*
543       if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS) )
544       {
545       return SMALL_VIRTUAL_VIEW_ITEMS[item][column];
546       }
547       else
548       {
549     */
550     return wxString::Format(_T("Column %ld of item %ld"), column, item);
551     //  }
552   }
553
554   int WxGUIBlackBoxList::OnGetItemColumnImage(long item, long column) const
555   {
556     if (!column)
557       return 0;
558
559     if (!(item %3) && column == 1)
560       return 0;
561
562     return -1;
563   }
564
565   wxListItemAttr *WxGUIBlackBoxList::OnGetItemAttr(long item) const
566   {
567     return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
568   }
569
570
571 #if USE_CONTEXT_MENU
572   void WxGUIBlackBoxList::OnContextMenu(wxContextMenuEvent& event)
573   {
574     wxPoint point = event.GetPosition();
575     // If from keyboard
576     if (point.x == -1 && point.y == -1) {
577       wxSize size = GetSize();
578       point.x = size.x / 2;
579       point.y = size.y / 2;
580     } else {
581       point = ScreenToClient(point);
582     }
583     ShowContextMenu(point);
584   }
585 #endif
586
587   void WxGUIBlackBoxList::ShowContextMenu(const wxPoint& pos)
588   {
589     wxMenu menu;
590   
591     menu.Append(wxID_ABOUT, _T("&About"));
592     menu.AppendSeparator();
593     menu.Append(wxID_EXIT, _T("E&xit"));
594   
595     PopupMenu(&menu, pos.x, pos.y);
596   }
597   //================================================================
598
599
600
601   //================================================================
602   WxGUIBlackBoxInfo::WxGUIBlackBoxInfo(wxWindow* parent)
603     :
604     wxPanel(parent, -1),
605     mDescriptor()
606   {
607     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
608     
609     //    mName = new wxStaticText(this,-1,_T(""));
610     //    sizer->Add(mName,0,wxGROW);
611     mDescription = new wxStaticText(this,-1,_T(""));
612     sizer->Add(mDescription,0,wxGROW);
613     mAuthor = new wxStaticText(this,-1,_T(""));
614     sizer->Add(mAuthor,0,wxGROW);
615     mCategory = new wxStaticText(this,-1,_T(""));
616     sizer->Add(mCategory,0,wxGROW);
617
618
619     wxBoxSizer *inputs = 
620       new wxStaticBoxSizer
621       ( new wxStaticBox(this, wxID_ANY, _T("Inputs")), wxVERTICAL );    
622     mInputList = new wxListCtrl(this, -1,
623                                 wxDefaultPosition,
624                                 wxDefaultSize,
625                                 wxLC_REPORT //wxLC_LIST
626                                 | wxSUNKEN_BORDER);
627     
628     
629     inputs->Add(mInputList,1,wxGROW);
630     sizer->Add(inputs,1,wxGROW);
631
632     wxBoxSizer *outputs = 
633       new wxStaticBoxSizer
634       ( new wxStaticBox(this, wxID_ANY, _T("Outputs")), wxVERTICAL );    
635     mOutputList = new wxListCtrl(this, -1,
636                                  wxDefaultPosition,
637                                  wxDefaultSize,
638                                  wxLC_REPORT //wxLC_LIST
639                                  | wxSUNKEN_BORDER);
640     
641     outputs->Add(mOutputList,1,wxGROW);
642     sizer->Add(outputs,1,wxGROW);
643     
644     
645     SetSizer(sizer);
646     SetAutoLayout(true);
647     Layout();
648   }
649   //================================================================
650
651     
652   //================================================================
653   void WxGUIBlackBoxInfo::UpdateInfo(BlackBoxDescriptor* descr)
654   {
655
656     //    mName->SetLabel(std2wx(descr->GetTypeName()));
657     mDescription->SetLabel(std2wx(descr->GetDescription()));
658     mAuthor->SetLabel(std2wx(descr->GetAuthor()));
659     mCategory->SetLabel(std2wx(descr->GetCategory()));
660     
661     mInputList->ClearAll();
662     mInputList->InsertColumn( 0, _("Name"), 
663                                wxLIST_FORMAT_LEFT, 80  ); 
664     mInputList->InsertColumn( 1, _("Type"), 
665                                wxLIST_FORMAT_LEFT, 100 ); 
666     mInputList->InsertColumn( 2, _("Nature"), 
667                                wxLIST_FORMAT_LEFT, 100  ); 
668     mInputList->InsertColumn( 3, _("Description"), 
669                                wxLIST_FORMAT_LEFT, 500  ); 
670
671     mOutputList->ClearAll();
672     mOutputList->InsertColumn( 0, _("Name"), 
673                                wxLIST_FORMAT_LEFT, 80  ); 
674     mOutputList->InsertColumn( 1, _("Type"), 
675                                wxLIST_FORMAT_LEFT, 100 ); 
676     mOutputList->InsertColumn( 2, _("Nature"), 
677                                wxLIST_FORMAT_LEFT, 100  ); 
678     mOutputList->InsertColumn( 3, _("Description"), 
679                                wxLIST_FORMAT_LEFT, 500  ); 
680     
681
682     std::vector<BlackBoxInputOutputDescriptor*> user_defined;
683     std::vector<BlackBoxInputOutputDescriptor*> ubb_defined;
684     std::vector<BlackBoxInputOutputDescriptor*> wxbb_defined;
685     
686     const BlackBoxDescriptor::InputDescriptorMapType& imap = 
687       descr->GetInputDescriptorMap();
688     BlackBoxDescriptor::InputDescriptorMapType::const_iterator in;
689     for ( in = imap.begin();  in != imap.end(); ++in ) 
690       {
691         int iotype = 0;
692         if (in->second->GetCreatorTypeInfo() == 
693             typeid(AtomicBlackBoxDescriptor))
694           {
695             iotype = 1;
696           }
697         else if (in->second->GetCreatorTypeInfo() == 
698                  typeid(WxBlackBoxDescriptor))
699           {
700             iotype = 2;
701           }
702         if (iotype==0) user_defined.push_back(in->second);
703         else if (iotype==1) ubb_defined.push_back(in->second);
704         else if (iotype==2) wxbb_defined.push_back(in->second);
705         
706       }
707
708     std::vector<BlackBoxInputOutputDescriptor*>::iterator hi;
709     for (hi=user_defined.begin();hi!=user_defined.end();++hi) 
710       {
711         InsertInputOutput(mInputList,*hi);
712       }
713     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) 
714       {
715         InsertInputOutput(mInputList,*hi);
716       }
717     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) 
718       {
719         InsertInputOutput(mInputList,*hi);
720       }
721     
722     // Outputs
723     user_defined.clear();
724     ubb_defined.clear();
725     wxbb_defined.clear();
726     const BlackBoxDescriptor::OutputDescriptorMapType& omap = 
727       descr->GetOutputDescriptorMap();
728     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator out;
729     for ( out = omap.begin();  out != omap.end(); ++out ) 
730       {
731         int iotype = 0;
732         if (out->second->GetCreatorTypeInfo() == 
733             typeid(AtomicBlackBoxDescriptor))
734           {
735             iotype = 1;
736           }
737         else if (out->second->GetCreatorTypeInfo() == 
738                  typeid(WxBlackBoxDescriptor))
739           {
740             iotype = 2;
741           }
742        
743         if (iotype==0) user_defined.push_back(out->second);
744         else if (iotype==1) ubb_defined.push_back(out->second);
745         else if (iotype==2) wxbb_defined.push_back(out->second);
746
747       }
748     for (hi=user_defined.begin();hi!=user_defined.end();++hi) 
749       {
750         InsertInputOutput(mOutputList,*hi);
751       }
752     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) 
753       {
754         InsertInputOutput(mOutputList,*hi);
755       }
756     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) 
757       {
758         InsertInputOutput(mOutputList,*hi);
759       }
760   }
761   //================================================================
762
763
764   //================================================================
765   void WxGUIBlackBoxInfo::InsertInputOutput(wxListCtrl* l,
766                                             BlackBoxInputOutputDescriptor* d)
767   {
768     wxListItem kNewItem;
769     kNewItem.SetAlign(wxLIST_FORMAT_LEFT);
770     int nID = l->GetItemCount();
771     //    std::cout << nID << std::endl;
772     kNewItem.SetId(nID);
773     //    kNewItem.SetMask(wxLIST_MASK_DATA);
774     //    kNewItem.SetData(d);  
775     l->InsertItem(kNewItem);
776     l->SetItem(nID, 0, std2wx(d->GetName()) );
777     l->SetItem(nID, 1, std2wx(d->GetTypeName()) );
778     l->SetItem(nID, 2, std2wx(d->GetNature()) );
779     l->SetItem(nID, 3, std2wx(d->GetDescription()) );
780   }
781   //================================================================
782
783   //================================================================
784   WxGUIBlackBoxInfo::~WxGUIBlackBoxInfo()
785   {
786   }
787   //================================================================
788
789
790  enum 
791     {
792       id_f1,
793       id_f2,
794       id_f3,
795       id_f4,
796       id_f5,
797       id_f6,
798       id_f7,
799       id_f8,
800       id_fc1,
801       id_fc2,
802       id_fc3
803     };
804
805
806   //================================================================
807   WxGUIPackageBrowser2::WxGUIPackageBrowser2( wxWindow *parent,
808                                               WxGUIPackageBrowser2User* user )
809     : wxPanel(parent, -1),
810       mUser(user),
811       mInterpreter()
812   {
813     m_mgr.SetManagedWindow(this);
814
815     //    wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
816     mBoxList = new WxGUIBlackBoxList(this, LIST_CTRL,
817                                      wxDefaultPosition,
818                                      wxDefaultSize,
819                                      wxLC_REPORT //wxLC_LIST
820                                      | wxSUNKEN_BORDER);
821     mBoxList->SetUser(this);
822     mBoxList->SetBackgroundColour(*wxWHITE);
823
824     m_mgr.AddPane(mBoxList,
825                   wxAuiPaneInfo().Name(wxT("BoxList"))
826                   .Caption(wxT("Black Boxes"))
827                   .MinimizeButton(true)
828                   .MaximizeButton(true)
829                   .Center()
830                   .MinSize(wxSize(100,100))
831                   );     
832     //    sizer->Add(mBoxList,5,wxGROW);
833
834     mBoxInfo = new WxGUIBlackBoxInfo(this);
835     //    sizer->Add(mBoxInfo,3,wxGROW);
836
837     m_mgr.AddPane(mBoxInfo,
838                   wxAuiPaneInfo().Name(wxT("Box"))
839                   .Caption(wxT(""))
840                   .MinimizeButton(true)
841                   .MaximizeButton(true)
842                   .Bottom()
843                   .MinSize(wxSize(100,300))
844                   );     
845     
846
847     wxPanel* filters = new wxPanel(this,-1);
848     wxBoxSizer *fsizer = new wxBoxSizer(wxVERTICAL );    
849     wxBoxSizer *fpack = 
850       new wxStaticBoxSizer
851       ( new wxStaticBox(filters, wxID_ANY, _T("Package")), wxHORIZONTAL );    
852     mPackageFilter = new wxTextCtrl(filters,id_f1,_T(""),
853                                     wxDefaultPosition,
854                                     wxDefaultSize,
855                                     wxTE_PROCESS_ENTER);
856     fpack->Add(mPackageFilter,1,wxGROW);
857     fsizer->Add(fpack,0,wxGROW);
858     wxBoxSizer *fname = 
859       new wxStaticBoxSizer
860       ( new wxStaticBox(filters, wxID_ANY, _T("Name")), wxHORIZONTAL );    
861     mNameFilter = new wxTextCtrl(filters,id_f2,_T(""),
862                                     wxDefaultPosition,
863                                     wxDefaultSize,
864                                     wxTE_PROCESS_ENTER);
865     fname->Add(mNameFilter,1,wxGROW);
866     fsizer->Add(fname,0,wxGROW);
867     wxBoxSizer *fdescr = 
868       new wxStaticBoxSizer
869       ( new wxStaticBox(filters, wxID_ANY, _T("Description")), wxHORIZONTAL );
870     mDescriptionFilter = new wxTextCtrl(filters,id_f3,_T(""),
871                                     wxDefaultPosition,
872                                     wxDefaultSize,
873                                     wxTE_PROCESS_ENTER);
874     fdescr->Add(mDescriptionFilter,1,wxGROW);
875     fsizer->Add(fdescr,0,wxGROW);
876     wxBoxSizer *fcat = 
877       new wxStaticBoxSizer
878       ( new wxStaticBox(filters, wxID_ANY, _T("Category")), wxHORIZONTAL );    
879     mCategoryFilter = new wxTextCtrl(filters,id_f4,_T(""),
880                                     wxDefaultPosition,
881                                     wxDefaultSize,
882                                     wxTE_PROCESS_ENTER);
883     fcat->Add(mCategoryFilter,1,wxGROW);
884     fsizer->Add(fcat,0,wxGROW);
885     wxBoxSizer *fintype = 
886       new wxStaticBoxSizer
887       ( new wxStaticBox(filters, wxID_ANY, _T("Input type")), wxHORIZONTAL );
888     mInputTypeFilter = new wxTextCtrl(filters,id_f5,_T(""),
889                                     wxDefaultPosition,
890                                     wxDefaultSize,
891                                     wxTE_PROCESS_ENTER);
892     fintype->Add(mInputTypeFilter,1,wxGROW);
893     fsizer->Add(fintype,0,wxGROW);
894     wxBoxSizer *fouttype = 
895       new wxStaticBoxSizer
896       ( new wxStaticBox(filters, wxID_ANY, _T("Output type")), wxHORIZONTAL );
897     mOutputTypeFilter = new wxTextCtrl(filters,id_f6,_T(""),
898                                     wxDefaultPosition,
899                                     wxDefaultSize,
900                                     wxTE_PROCESS_ENTER);
901     fouttype->Add(mOutputTypeFilter,1,wxGROW);
902     fsizer->Add(fouttype,0,wxGROW);
903     wxBoxSizer *finnat = 
904       new wxStaticBoxSizer
905       ( new wxStaticBox(filters, wxID_ANY, _T("Input nature")),wxHORIZONTAL ); 
906     mInputNatureFilter = new wxTextCtrl(filters,id_f7,_T(""),
907                                         wxDefaultPosition,
908                                         wxDefaultSize,
909                                         wxTE_PROCESS_ENTER);
910     finnat->Add(mInputNatureFilter,1,wxGROW);
911     fsizer->Add(finnat,0,wxGROW);
912     wxBoxSizer *foutnat = 
913       new wxStaticBoxSizer
914       ( new wxStaticBox(filters, wxID_ANY,_T("Output nature")),wxHORIZONTAL ); 
915     mOutputNatureFilter = new wxTextCtrl(filters,id_f8,_T(""),
916                                         wxDefaultPosition,
917                                         wxDefaultSize,
918                                         wxTE_PROCESS_ENTER);
919     foutnat->Add(mOutputNatureFilter,1,wxGROW);
920     fsizer->Add(foutnat,0,wxGROW);
921
922
923     mShowWidgetsFilter = new wxCheckBox(filters,id_fc1,_T("Show widgets"));
924     mShowWidgetsFilter->SetValue(true);
925     fsizer->Add(mShowWidgetsFilter,0,wxGROW);
926
927     mShowAdaptorsFilter = new wxCheckBox(filters,id_fc2,_T("Show adaptors"));
928     mShowAdaptorsFilter->SetValue(false);
929     fsizer->Add(mShowAdaptorsFilter,0,wxGROW);
930
931     mShowGUIsFilter = new wxCheckBox(filters,id_fc3,_T("Show GUIs"));
932     mShowGUIsFilter->SetValue(false);
933     fsizer->Add(mShowGUIsFilter,0,wxGROW);
934
935     filters->SetSizer(fsizer);
936
937     m_mgr.AddPane(filters,
938                   wxAuiPaneInfo().Name(wxT("Filter"))
939                   .Caption(wxT("Filter"))
940                   .MinimizeButton(true)
941                   .MaximizeButton(true)
942                   .Right()
943                   .MinSize(wxSize(100,100)) );
944
945     
946
947     //    sizer->Add(fsizer,1,wxGROW);
948     m_mgr.Update();
949
950     //    SetSizer(sizer);
951     SetAutoLayout(true);
952     Layout();
953     
954   }
955   //================================================================
956
957   //================================================================
958   void WxGUIPackageBrowser2::OnFilter(wxCommandEvent&)
959   {
960     RebuildList();
961   }
962   //================================================================
963
964   //================================================================
965   // User callback when a box is selected in the list 
966   void WxGUIPackageBrowser2::WxGUIBlackBoxListUserOnSelected
967   ( BlackBoxDescriptor* d)
968   {
969         //RaC
970         _actualSelected=d;
971
972     mBoxInfo->UpdateInfo(d);
973     std::string title = d->GetPackage()->GetName()+"::"+d->GetTypeName();
974     m_mgr.GetPane(mBoxInfo).Caption(std2wx(title));
975     m_mgr.Update();
976   }
977   
978   //================================================================
979
980   //================================================================
981   BlackBoxDescriptor* WxGUIPackageBrowser2::GetActualSelected()
982   {
983           return _actualSelected;
984   }
985
986
987   //================================================================
988
989   //================================================================
990   WxGUIPackageBrowser2::~WxGUIPackageBrowser2()
991   {
992     mFactory.reset();
993     mInterpreter.reset();
994     //    Object::PrintObjectListInfo("");
995     m_mgr.UnInit();
996   }
997   //================================================================
998
999   //================================================================
1000   void WxGUIPackageBrowser2::IncludeAll()
1001   {
1002     if (!mInterpreter) mInterpreter =bbtk::Interpreter::New();
1003     mInterpreter->SetCommandLine(true);
1004     std::stringstream* buf = new std::stringstream;
1005     *buf << "exec freeze_no_error" << std::endl;
1006     *buf << "message max 0" << std::endl; 
1007     *buf << "include *" << std::endl;
1008     mInterpreter->InterpretBuffer(buf);
1009
1010     Factory::Pointer F = mInterpreter->GetExecuter()->GetFactory();
1011     BuildFromFactory(F);
1012   }
1013   //================================================================
1014
1015   //================================================================
1016   void WxGUIPackageBrowser2::BuildFromFactory(Factory::Pointer F)
1017   {
1018     mFactory = F;
1019     RebuildList();
1020   }  
1021
1022   //================================================================
1023
1024   //================================================================
1025   void WxGUIPackageBrowser2::RebuildList()
1026   {
1027     mBoxList->Hide();
1028     mBoxList->ClearAll();    
1029     
1030     const Factory::PackageMapType& M = mFactory->GetPackageMap();
1031     Factory::PackageMapType::const_iterator i;
1032     for (i=M.begin();i!=M.end();++i)
1033       {
1034         Package::Pointer P = i->second;
1035         if (P->GetName() == "user") continue;
1036         
1037         Package::DescriptorMapType::iterator j;
1038         for (j=P->GetDescriptorMap().begin();
1039              j!=P->GetDescriptorMap().end();
1040              ++j)
1041           {
1042             //      std::cout << "Insert "<<j->second->GetTypeName()<<std::endl;
1043             if (IsVisible(j->second))
1044               mBoxList->Insert(j->second);
1045           }
1046       }
1047     mBoxList->Show();
1048   }
1049   //================================================================
1050
1051   //================================================================
1052   bool WxGUIPackageBrowser2::IsVisible(BlackBoxDescriptor::Pointer d)
1053   {
1054     //    std::cout << "   '" << wx2std(mPackageFilter->GetValue())<<"'"<<std::endl;
1055     //    std::cout << "vs '" << d->GetPackage()->GetName() << "'"<<std::endl;
1056     if ((!mShowAdaptorsFilter->IsChecked())&&
1057         ((d->GetKind()==BlackBoxDescriptor::ADAPTOR)||
1058          (d->GetKind()==BlackBoxDescriptor::DEFAULT_ADAPTOR)))
1059       return false;
1060     if ((!mShowGUIsFilter->IsChecked())&&
1061         ((d->GetKind()==BlackBoxDescriptor::GUI)||
1062          (d->GetKind()==BlackBoxDescriptor::DEFAULT_GUI)))
1063       return false;
1064     if (d->GetPackage()->GetName().find( wx2std(mPackageFilter->GetValue()) ) 
1065         == std::string::npos ) return false;
1066    if (d->GetTypeName().find( wx2std(mNameFilter->GetValue()) ) 
1067         == std::string::npos ) return false;
1068    if (d->GetDescription().find( wx2std(mDescriptionFilter->GetValue()) ) 
1069         == std::string::npos ) return false;
1070    if (d->GetCategory().find( wx2std(mCategoryFilter->GetValue()) ) 
1071         == std::string::npos ) return false;
1072    if (!mShowWidgetsFilter->IsChecked())
1073      {
1074        bool found = false;
1075        const BlackBoxDescriptor::OutputDescriptorMapType& imap = 
1076          d->GetOutputDescriptorMap();
1077        BlackBoxDescriptor::OutputDescriptorMapType::const_iterator in;
1078        for ( in = imap.begin();  in != imap.end(); ++in ) 
1079          {
1080            if (in->second->GetName() == "Widget" )
1081              {
1082                found = true;
1083                break;
1084              }
1085          }
1086        if (found) return false;
1087      }
1088    if (mInputTypeFilter->GetValue().size()>0)
1089      {
1090        std::string s = wx2std(mInputTypeFilter->GetValue());
1091        bool found = false;
1092        const BlackBoxDescriptor::InputDescriptorMapType& imap = 
1093          d->GetInputDescriptorMap();
1094        BlackBoxDescriptor::InputDescriptorMapType::const_iterator in;
1095        for ( in = imap.begin();  in != imap.end(); ++in ) 
1096          {
1097            if (in->second->GetTypeName().find(s)!=std::string::npos)
1098              {
1099                found = true;
1100                break;
1101              }
1102          }
1103        if (!found) return false;
1104      }
1105    if (mOutputTypeFilter->GetValue().size()>0)
1106      {
1107        std::string s = wx2std(mOutputTypeFilter->GetValue());
1108        bool found = false;
1109        const BlackBoxDescriptor::OutputDescriptorMapType& imap = 
1110          d->GetOutputDescriptorMap();
1111        BlackBoxDescriptor::OutputDescriptorMapType::const_iterator in;
1112        for ( in = imap.begin();  in != imap.end(); ++in ) 
1113          {
1114            if (in->second->GetTypeName().find(s)!=std::string::npos)
1115              {
1116                found = true;
1117                break;
1118              }
1119          }
1120        if (!found) return false;
1121      }
1122    if (mInputNatureFilter->GetValue().size()>0)
1123      {
1124        std::string s = wx2std(mInputNatureFilter->GetValue());
1125        bool found = false;
1126        const BlackBoxDescriptor::InputDescriptorMapType& imap = 
1127          d->GetInputDescriptorMap();
1128        BlackBoxDescriptor::InputDescriptorMapType::const_iterator in;
1129        for ( in = imap.begin();  in != imap.end(); ++in ) 
1130          {
1131            if (in->second->GetNature().find(s)!=std::string::npos)
1132              {
1133                found = true;
1134                break;
1135              }
1136          }
1137        if (!found) return false;
1138      }
1139    if (mOutputNatureFilter->GetValue().size()>0)
1140      {
1141        std::string s = wx2std(mOutputNatureFilter->GetValue());
1142        bool found = false;
1143        const BlackBoxDescriptor::OutputDescriptorMapType& imap = 
1144          d->GetOutputDescriptorMap();
1145        BlackBoxDescriptor::OutputDescriptorMapType::const_iterator in;
1146        for ( in = imap.begin();  in != imap.end(); ++in ) 
1147          {
1148            if (in->second->GetNature().find(s)!=std::string::npos)
1149              {
1150                found = true;
1151                break;
1152              }
1153          }
1154        if (!found) return false;
1155      }
1156
1157     return true;
1158   }
1159   //========================================================================
1160
1161   //========================================================================
1162   BEGIN_EVENT_TABLE(WxGUIPackageBrowser2, wxPanel)
1163     EVT_TEXT_ENTER(id_f1, WxGUIPackageBrowser2::OnFilter )
1164     EVT_TEXT_ENTER(id_f2, WxGUIPackageBrowser2::OnFilter )
1165     EVT_TEXT_ENTER(id_f3, WxGUIPackageBrowser2::OnFilter )
1166     EVT_TEXT_ENTER(id_f4, WxGUIPackageBrowser2::OnFilter )
1167     EVT_TEXT_ENTER(id_f5, WxGUIPackageBrowser2::OnFilter )
1168     EVT_TEXT_ENTER(id_f6, WxGUIPackageBrowser2::OnFilter )
1169     EVT_TEXT_ENTER(id_f7, WxGUIPackageBrowser2::OnFilter )
1170     EVT_TEXT_ENTER(id_f8, WxGUIPackageBrowser2::OnFilter )
1171     EVT_CHECKBOX(  id_fc1, WxGUIPackageBrowser2::OnFilter )
1172     EVT_CHECKBOX(  id_fc2, WxGUIPackageBrowser2::OnFilter )
1173     EVT_CHECKBOX(  id_fc3, WxGUIPackageBrowser2::OnFilter )
1174     END_EVENT_TABLE()
1175   //========================================================================
1176
1177
1178
1179   //================================================================
1180   WxGUIPackageBrowser2Window::WxGUIPackageBrowser2Window( wxWindow *parent, 
1181                                                             wxString title, 
1182                                                             wxSize size)
1183       : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
1184   {     
1185     
1186     
1187     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
1188
1189     mBrowser = new WxGUIPackageBrowser2(this);
1190     mBrowser->IncludeAll();
1191     sizer->Add(mBrowser,1,wxGROW);
1192     
1193     //    WxGUICommand* com = new WxGUICommand(this,this);
1194     //    sizer->Add(com);
1195     
1196     SetSizer(sizer);
1197
1198      // Creates the parent window of all bbtk windows as a child of this
1199 //    Wx::CreateTopWindow(this);
1200     // Add the method OnWxSignal as a Wx::Signal observer 
1201     //bbtkAddWxSignalObserver(WxGUIPackageBrowser2Window::OnWxSignal);
1202  
1203     SetAutoLayout(true);
1204     Layout();
1205   }
1206   //================================================================
1207
1208   //================================================================
1209   WxGUIPackageBrowser2Window::~WxGUIPackageBrowser2Window()
1210   {
1211   }
1212   //================================================================
1213
1214 } // namespace bbtk
1215
1216
1217 #endif //_USE_WXWIDGETS_