]> Creatis software - bbtk.git/blob - kernel/src/bbtkWxGUIPackageBrowser2.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkWxGUIPackageBrowser2.cxx
1 /*=========================================================================
2                                                                                 
3 Program:   bbtk
4 Module:    $RCSfile: bbtkWxGUIPackageBrowser2.cxx,v $
5 Language:  C++
6 Date:      $Date: 2008/04/04 08:04:05 $
7 Version:   $Revision: 1.1 $
8                                                                                 
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12                                                                                 
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*//**
18                                                                             * \brief Short description in one line
19                                                                             * 
20                                                                             * Long description which 
21                                                                             * can span multiple lines
22                                                                             */
23 /**
24  * \file 
25  * \brief 
26  */
27 /**
28  * \class bbtk::
29  * \brief 
30  */
31
32
33 #ifdef _USE_WXWIDGETS_
34
35 #define CHECKBOXVIEW 1
36
37 #include "bbtkWxGUIPackageBrowser2.h"
38 //#include "ThirdParty/wx/treemultictrl/wxTreeMultiCtrl.h"
39 #include "bbtkInterpreter.h"
40 #include "bbtkBlackBoxInputDescriptor.h"
41 #include "bbtkBlackBoxOutputDescriptor.h"
42 #include "bbtkWxBlackBox.h"
43 //#include "wx/grid.h"
44 //#include <wx/statline.h>
45
46 #define LIST_CTRL 1000
47
48
49 namespace bbtk
50 {
51
52
53   BEGIN_EVENT_TABLE(WxGUIBlackBoxList, wxListCtrl)
54     EVT_LIST_BEGIN_DRAG(LIST_CTRL, WxGUIBlackBoxList::OnBeginDrag)
55     EVT_LIST_BEGIN_RDRAG(LIST_CTRL, WxGUIBlackBoxList::OnBeginRDrag)
56     EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, WxGUIBlackBoxList::OnBeginLabelEdit)
57     EVT_LIST_END_LABEL_EDIT(LIST_CTRL, WxGUIBlackBoxList::OnEndLabelEdit)
58     EVT_LIST_DELETE_ITEM(LIST_CTRL, WxGUIBlackBoxList::OnDeleteItem)
59     EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, WxGUIBlackBoxList::OnDeleteAllItems)
60 #if WXWIN_COMPATIBILITY_2_4
61     EVT_LIST_GET_INFO(LIST_CTRL, WxGUIBlackBoxList::OnGetInfo)
62     EVT_LIST_SET_INFO(LIST_CTRL, WxGUIBlackBoxList::OnSetInfo)
63 #endif
64     EVT_LIST_ITEM_SELECTED(LIST_CTRL, WxGUIBlackBoxList::OnSelected)
65     EVT_LIST_ITEM_DESELECTED(LIST_CTRL, WxGUIBlackBoxList::OnDeselected)
66     EVT_LIST_KEY_DOWN(LIST_CTRL, WxGUIBlackBoxList::OnListKeyDown)
67     EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, WxGUIBlackBoxList::OnActivated)
68     EVT_LIST_ITEM_FOCUSED(LIST_CTRL, WxGUIBlackBoxList::OnFocused)
69     
70     EVT_LIST_COL_CLICK(LIST_CTRL, WxGUIBlackBoxList::OnColClick)
71     EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, WxGUIBlackBoxList::OnColRightClick)
72     EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, WxGUIBlackBoxList::OnColBeginDrag)
73     EVT_LIST_COL_DRAGGING(LIST_CTRL, WxGUIBlackBoxList::OnColDragging)
74     EVT_LIST_COL_END_DRAG(LIST_CTRL, WxGUIBlackBoxList::OnColEndDrag)
75     
76     EVT_LIST_CACHE_HINT(LIST_CTRL, WxGUIBlackBoxList::OnCacheHint)
77     
78 #if USE_CONTEXT_MENU
79     EVT_CONTEXT_MENU(WxGUIBlackBoxList::OnContextMenu)
80 #endif
81     EVT_CHAR(WxGUIBlackBoxList::OnChar)
82     
83     EVT_RIGHT_DOWN(WxGUIBlackBoxList::OnRightClick)
84     END_EVENT_TABLE()
85     
86     int wxCALLBACK MyCompareFunction(long item1, long item2, long WXUNUSED(sortData))
87   {
88     // inverse the order
89     if (item1 < item2)
90       return -1;
91     if (item1 > item2)
92       return 1;
93     
94     return 0;
95   }
96   
97   // WxGUIBlackBoxList
98   WxGUIBlackBoxList::WxGUIBlackBoxList(wxWindow *parent,
99                                  const wxWindowID id,
100                                  const wxPoint& pos,
101                                  const wxSize& size,
102                                  long style)
103     : wxListCtrl(parent, id, pos, size, style),
104       mUser(0),
105       m_attr(*wxBLUE, *wxLIGHT_GREY, wxNullFont)
106   {
107 #ifdef __POCKETPC__
108     EnableContextMenu();
109 #endif
110   }
111   
112   void WxGUIBlackBoxList::Insert(BlackBoxDescriptor* d)
113   {
114     if (GetColumnCount()!=3)
115       {
116         InsertColumn( 0, _("Package"), 
117                       wxLIST_FORMAT_LEFT, 40  ); 
118         InsertColumn( 1, _("Box"), 
119                       wxLIST_FORMAT_LEFT, 200 ); 
120         InsertColumn( 2, _("Description"), 
121                       wxLIST_FORMAT_LEFT, 500  ); 
122       }
123     
124     wxListItem kNewItem;
125     kNewItem.SetAlign(wxLIST_FORMAT_LEFT);
126     
127     int nID = this->GetItemCount();
128     kNewItem.SetId(nID);
129     kNewItem.SetMask(wxLIST_MASK_DATA);
130     kNewItem.SetData(d);  
131     //    BlackBoxDescriptor* d2 = (BlackBoxDescriptor*)kNewItem.GetData();
132     //    std::cout << "Descr = "<<d<<" = "<<d2<<std::endl;
133     this->InsertItem(kNewItem);
134     this->SetItem(nID, 0, std2wx(d->GetPackage()->GetName()) );
135     this->SetItem(nID, 1, std2wx(d->GetTypeName()) );
136     this->SetItem(nID, 2, std2wx(d->GetDescription()) );
137     
138     /*
139       if (nID % 2)
140       {
141       kNewItem.SetBackgroundColour(wxColour(192,192,192));
142       this->SetItem(kNewItem);
143       }
144     */
145     
146   }
147   
148   
149   void WxGUIBlackBoxList::OnCacheHint(wxListEvent& event)
150   {
151     //wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
152     //                  event.GetCacheFrom(), event.GetCacheTo() );
153   }
154   
155   void WxGUIBlackBoxList::SetColumnImage(int col, int image)
156   {
157     /*
158       wxListItem item;
159       item.SetMask(wxLIST_MASK_IMAGE);
160       item.SetImage(image);
161       SetColumn(col, item);
162     */
163   }
164
165   void WxGUIBlackBoxList::OnColClick(wxListEvent& event)
166   {
167     int col = event.GetColumn();
168
169     // set or unset image
170     static bool x = false;
171     x = !x;
172     SetColumnImage(col, x ? 0 : -1);
173
174     //    wxLogMessage( wxT("OnColumnClick at %d."), col );
175   }
176
177   void WxGUIBlackBoxList::OnColRightClick(wxListEvent& event)
178   {
179     int col = event.GetColumn();
180     if ( col != -1 )
181       {
182         SetColumnImage(col, -1);
183       }
184
185     // Show popupmenu at position
186     wxMenu menu(wxT("Test"));
187     menu.Append(-1, _T("&About"));
188     PopupMenu(&menu, event.GetPoint());
189
190     //wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() );
191   }
192
193   void WxGUIBlackBoxList::LogColEvent(const wxListEvent& event, const wxChar *name)
194   {
195     const int col = event.GetColumn();
196
197     /*
198       wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
199       name,
200       col,
201       event.GetItem().GetWidth(),
202       GetColumnWidth(col));
203     */
204   }
205
206   void WxGUIBlackBoxList::OnColBeginDrag(wxListEvent& event)
207   {
208     LogColEvent( event, wxT("OnColBeginDrag") );
209     /*
210       if ( event.GetColumn() == 0 )
211       {
212       //wxLogMessage(_T("Resizing this column shouldn't work."));
213
214       event.Veto();
215       }
216     */
217   }
218
219   void WxGUIBlackBoxList::OnColDragging(wxListEvent& event)
220   {
221     LogColEvent( event, wxT("OnColDragging") );
222   }
223
224   void WxGUIBlackBoxList::OnColEndDrag(wxListEvent& event)
225   {
226     LogColEvent( event, wxT("OnColEndDrag") );
227   }
228
229   void WxGUIBlackBoxList::OnBeginDrag(wxListEvent& event)
230   {
231     const wxPoint& pt = event.m_pointDrag;
232
233     int flags;
234     //wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
235     //          pt.x, pt.y, HitTest(pt, flags) );
236   }
237
238   void WxGUIBlackBoxList::OnBeginRDrag(wxListEvent& event)
239   {
240     //wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
241     //        event.m_pointDrag.x, event.m_pointDrag.y );
242   }
243
244   void WxGUIBlackBoxList::OnBeginLabelEdit(wxListEvent& event)
245   {
246     //wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
247   }
248
249   void WxGUIBlackBoxList::OnEndLabelEdit(wxListEvent& event)
250   {
251     //wxLogMessage( wxT("OnEndLabelEdit: %s"),
252     //        event.IsEditCancelled() ? _T("[cancelled]")
253     //                                      : event.m_item.m_text.c_str());
254   }
255
256   void WxGUIBlackBoxList::OnDeleteItem(wxListEvent& event)
257   {
258     LogEvent(event, _T("OnDeleteItem"));
259     std::cout << "cannot del"<<std::endl;
260     event.Veto();
261     //wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
262   }
263
264   void WxGUIBlackBoxList::OnDeleteAllItems(wxListEvent& event)
265   {
266     LogEvent(event, _T("OnDeleteAllItems"));
267     event.Veto();
268   }
269
270 #if WXWIN_COMPATIBILITY_2_4
271   void WxGUIBlackBoxList::OnGetInfo(wxListEvent& event)
272   {
273     wxString msg;
274
275     msg << _T("OnGetInfo (") << event.m_item.m_itemId << _T(", ") << event.m_item.m_col << _T(")");
276     if ( event.m_item.m_mask & wxLIST_MASK_STATE )
277       msg << _T(" wxLIST_MASK_STATE");
278     if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
279       msg << _T(" wxLIST_MASK_TEXT");
280     if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
281       msg << _T(" wxLIST_MASK_IMAGE");
282     if ( event.m_item.m_mask & wxLIST_MASK_DATA )
283       msg << _T(" wxLIST_MASK_DATA");
284     if ( event.m_item.m_mask & wxLIST_SET_ITEM )
285       msg << _T(" wxLIST_SET_ITEM");
286     if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
287       msg << _T(" wxLIST_MASK_WIDTH");
288     if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
289       msg << _T(" wxLIST_MASK_WIDTH");
290
291     if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
292       {
293         event.m_item.m_text = _T("My callback text");
294       }
295
296     //wxLogMessage(msg);
297   }
298
299   void WxGUIBlackBoxList::OnSetInfo(wxListEvent& event)
300   {
301     LogEvent(event, _T("OnSetInfo"));
302   }
303 #endif
304
305   void WxGUIBlackBoxList::OnSelected(wxListEvent& event)
306   {
307     if (mUser==0) return;
308
309     wxListItem info;
310     info.m_itemId = event.m_itemIndex;
311     info.m_col = 0;
312     info.m_mask = wxLIST_MASK_DATA;
313     if ( GetItem(info) )
314       {
315         BlackBoxDescriptor* d = (BlackBoxDescriptor*)(info.GetData());
316         if (d!=0) mUser->WxGUIBlackBoxListUserOnSelected(d);
317       }
318     else
319       {
320         wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
321       }
322   }
323
324   void WxGUIBlackBoxList::OnDeselected(wxListEvent& event)
325   {
326     LogEvent(event, _T("OnDeselected"));
327   }
328
329   void WxGUIBlackBoxList::OnActivated(wxListEvent& event)
330   {
331     LogEvent(event, _T("OnActivated"));
332   }
333
334   void WxGUIBlackBoxList::OnFocused(wxListEvent& event)
335   {
336     LogEvent(event, _T("OnFocused"));
337
338     event.Skip();
339   }
340
341   void WxGUIBlackBoxList::OnListKeyDown(wxListEvent& event)
342   {
343     long item;
344
345     switch ( event.GetKeyCode() )
346       {
347       case 'c': // colorize
348       case 'C':
349         {
350           wxListItem info;
351           info.m_itemId = event.GetIndex();
352           if ( info.m_itemId == -1 )
353             {
354               // no item
355               break;
356             }
357
358           GetItem(info);
359
360           wxListItemAttr *attr = info.GetAttributes();
361           if ( !attr || !attr->HasTextColour() )
362             {
363               info.SetTextColour(*wxCYAN);
364
365               SetItem(info);
366
367               RefreshItem(info.m_itemId);
368             }
369         }
370         break;
371
372       case 'n': // next
373       case 'N':
374         item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
375         if ( item++ == GetItemCount() - 1 )
376           {
377             item = 0;
378           }
379
380         //wxLogMessage(_T("Focusing item %ld"), item);
381
382         SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
383         EnsureVisible(item);
384         break;
385
386       case 'r': // show bounding Rect
387       case 'R':
388         {
389           item = event.GetIndex();
390           wxRect r;
391           if ( !GetItemRect(item, r) )
392             {
393               //wxLogError(_T("Failed to retrieve rect of item %ld"), item);
394               break;
395             }
396
397           //wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
398           //   item, r.x, r.y, r.x + r.width, r.y + r.height);
399         }
400         break;
401
402       case WXK_DELETE:
403         item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
404         /*
405           while ( item != -1 )
406           {
407           DeleteItem(item);
408
409           //wxLogMessage(_T("Item %ld deleted"), item);
410
411           // -1 because the indices were shifted by DeleteItem()
412           item = GetNextItem(item - 1,
413           wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
414           }
415         */
416         break;
417
418       case WXK_INSERT:
419         if ( GetWindowStyle() & wxLC_REPORT )
420           {
421             if ( GetWindowStyle() & wxLC_VIRTUAL )
422               {
423                 SetItemCount(GetItemCount() + 1);
424               }
425             else // !virtual
426               {
427                 //InsertItemInReportView(event.GetIndex());
428               }
429           }
430         //else: fall through
431
432       default:
433         LogEvent(event, _T("OnListKeyDown"));
434
435         event.Skip();
436       }
437   }
438
439   void WxGUIBlackBoxList::OnChar(wxKeyEvent& event)
440   {
441     //wxLogMessage(_T("Got char event."));
442
443     switch ( event.GetKeyCode() )
444       {
445       case 'n':
446       case 'N':
447       case 'c':
448       case 'C':
449         // these are the keys we process ourselves
450         break;
451
452       default:
453         event.Skip();
454       }
455   }
456
457   void WxGUIBlackBoxList::OnRightClick(wxMouseEvent& event)
458   {
459     if ( !event.ControlDown() )
460       {
461         event.Skip();
462         return;
463       }
464
465     int flags;
466     long subitem;
467     long item = HitTest(event.GetPosition(), flags, &subitem);
468
469     wxString where;
470     switch ( flags )
471       {
472       case wxLIST_HITTEST_ABOVE: where = _T("above"); break;
473       case wxLIST_HITTEST_BELOW: where = _T("below"); break;
474       case wxLIST_HITTEST_NOWHERE: where = _T("nowhere near"); break;
475       case wxLIST_HITTEST_ONITEMICON: where = _T("on icon of"); break;
476       case wxLIST_HITTEST_ONITEMLABEL: where = _T("on label of"); break;
477       case wxLIST_HITTEST_ONITEMRIGHT: where = _T("right on"); break;
478       case wxLIST_HITTEST_TOLEFT: where = _T("to the left of"); break;
479       case wxLIST_HITTEST_TORIGHT: where = _T("to the right of"); break;
480       default: where = _T("not clear exactly where on"); break;
481       }
482
483     //wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
484     //             where.c_str(), item, subitem);
485   }
486
487   void WxGUIBlackBoxList::LogEvent(const wxListEvent& event, const wxChar *eventName)
488   {
489     //wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
490     //            event.GetIndex(), eventName,
491     //              event.GetText().c_str(), event.GetData());
492   }
493
494   wxString WxGUIBlackBoxList::OnGetItemText(long item, long column) const
495   {
496     /*
497       if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS) )
498       {
499       return SMALL_VIRTUAL_VIEW_ITEMS[item][column];
500       }
501       else
502       {
503     */
504     return wxString::Format(_T("Column %ld of item %ld"), column, item);
505     //  }
506   }
507
508   int WxGUIBlackBoxList::OnGetItemColumnImage(long item, long column) const
509   {
510     if (!column)
511       return 0;
512
513     if (!(item %3) && column == 1)
514       return 0;
515
516     return -1;
517   }
518
519   wxListItemAttr *WxGUIBlackBoxList::OnGetItemAttr(long item) const
520   {
521     return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
522   }
523
524
525 #if USE_CONTEXT_MENU
526   void WxGUIBlackBoxList::OnContextMenu(wxContextMenuEvent& event)
527   {
528     wxPoint point = event.GetPosition();
529     // If from keyboard
530     if (point.x == -1 && point.y == -1) {
531       wxSize size = GetSize();
532       point.x = size.x / 2;
533       point.y = size.y / 2;
534     } else {
535       point = ScreenToClient(point);
536     }
537     ShowContextMenu(point);
538   }
539 #endif
540
541   void WxGUIBlackBoxList::ShowContextMenu(const wxPoint& pos)
542   {
543     wxMenu menu;
544   
545     menu.Append(wxID_ABOUT, _T("&About"));
546     menu.AppendSeparator();
547     menu.Append(wxID_EXIT, _T("E&xit"));
548   
549     PopupMenu(&menu, pos.x, pos.y);
550   }
551   //================================================================
552
553
554
555   //================================================================
556   WxGUIBlackBoxInfo::WxGUIBlackBoxInfo(wxWindow* parent)
557     :
558     wxPanel(parent, -1),
559     mDescriptor(0)
560   {
561     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
562     
563     mName = new wxStaticText(this,-1,_T(""));
564     sizer->Add(mName,0,wxGROW);
565     mDescription = new wxStaticText(this,-1,_T(""));
566     sizer->Add(mDescription,0,wxGROW);
567     mAuthor = new wxStaticText(this,-1,_T(""));
568     sizer->Add(mAuthor,0,wxGROW);
569     mCategory = new wxStaticText(this,-1,_T(""));
570     sizer->Add(mCategory,0,wxGROW);
571
572
573     wxBoxSizer *inputs = 
574       new wxStaticBoxSizer
575       ( new wxStaticBox(this, wxID_ANY, _T("Inputs")), wxVERTICAL );    
576     mInputList = new wxListCtrl(this, -1,
577                                 wxDefaultPosition,
578                                 wxDefaultSize,
579                                 wxLC_REPORT //wxLC_LIST
580                                 | wxSUNKEN_BORDER);
581     
582     
583     inputs->Add(mInputList,0,wxGROW);
584     sizer->Add(inputs,0,wxGROW);
585
586     wxBoxSizer *outputs = 
587       new wxStaticBoxSizer
588       ( new wxStaticBox(this, wxID_ANY, _T("Outputs")), wxVERTICAL );    
589     mOutputList = new wxListCtrl(this, -1,
590                                  wxDefaultPosition,
591                                  wxDefaultSize,
592                                  wxLC_REPORT //wxLC_LIST
593                                  | wxSUNKEN_BORDER);
594     
595     outputs->Add(mOutputList,0,wxGROW);
596     sizer->Add(outputs,0,wxGROW);
597     
598     
599     SetSizer(sizer);
600     SetAutoLayout(true);
601     Layout();
602   }
603   //================================================================
604
605     
606   //================================================================
607   void WxGUIBlackBoxInfo::UpdateInfo(BlackBoxDescriptor* descr)
608   {
609     std::cout << "UpdateInfo "<<descr<<std::endl;
610     mName->SetLabel(std2wx(descr->GetTypeName()));
611     mDescription->SetLabel(std2wx(descr->GetDescription()));
612     mAuthor->SetLabel(std2wx(descr->GetAuthor()));
613     mCategory->SetLabel(std2wx(descr->GetCategory()));
614     
615     mInputList->ClearAll();
616     mInputList->InsertColumn( 0, _("Name"), 
617                                wxLIST_FORMAT_LEFT, 80  ); 
618     mInputList->InsertColumn( 1, _("Type"), 
619                                wxLIST_FORMAT_LEFT, 100 ); 
620     mInputList->InsertColumn( 2, _("Description"), 
621                                wxLIST_FORMAT_LEFT, 500  ); 
622
623     mOutputList->ClearAll();
624     mOutputList->InsertColumn( 0, _("Name"), 
625                                wxLIST_FORMAT_LEFT, 80  ); 
626     mOutputList->InsertColumn( 1, _("Type"), 
627                                wxLIST_FORMAT_LEFT, 100 ); 
628     mOutputList->InsertColumn( 2, _("Description"), 
629                                wxLIST_FORMAT_LEFT, 500  ); 
630     
631
632     std::vector<BlackBoxInputOutputDescriptor*> user_defined;
633     std::vector<BlackBoxInputOutputDescriptor*> ubb_defined;
634     std::vector<BlackBoxInputOutputDescriptor*> wxbb_defined;
635     
636     const BlackBoxDescriptor::InputDescriptorMapType& imap = 
637       descr->GetInputDescriptorMap();
638     BlackBoxDescriptor::InputDescriptorMapType::const_iterator in;
639     for ( in = imap.begin();  in != imap.end(); ++in ) 
640       {
641         int iotype = 0;
642         if (in->second->GetCreatorTypeInfo() == 
643             typeid(AtomicBlackBoxDescriptor))
644           {
645             iotype = 1;
646           }
647         else if (in->second->GetCreatorTypeInfo() == 
648                  typeid(WxBlackBoxDescriptor))
649           {
650             iotype = 2;
651           }
652         if (iotype==0) user_defined.push_back(in->second);
653         else if (iotype==1) ubb_defined.push_back(in->second);
654         else if (iotype==2) wxbb_defined.push_back(in->second);
655         
656       }
657
658     std::vector<BlackBoxInputOutputDescriptor*>::iterator hi;
659     for (hi=user_defined.begin();hi!=user_defined.end();++hi) 
660       {
661         InsertInputOutput(mInputList,*hi);
662       }
663     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) 
664       {
665         InsertInputOutput(mInputList,*hi);
666       }
667     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) 
668       {
669         InsertInputOutput(mInputList,*hi);
670       }
671     
672     // Outputs
673     user_defined.clear();
674     ubb_defined.clear();
675     wxbb_defined.clear();
676     const BlackBoxDescriptor::OutputDescriptorMapType& omap = 
677       descr->GetOutputDescriptorMap();
678     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator out;
679     for ( out = omap.begin();  out != omap.end(); ++out ) 
680       {
681         int iotype = 0;
682         if (out->second->GetCreatorTypeInfo() == 
683             typeid(AtomicBlackBoxDescriptor))
684           {
685             iotype = 1;
686           }
687         else if (out->second->GetCreatorTypeInfo() == 
688                  typeid(WxBlackBoxDescriptor))
689           {
690             iotype = 2;
691           }
692        
693         if (iotype==0) user_defined.push_back(out->second);
694         else if (iotype==1) ubb_defined.push_back(out->second);
695         else if (iotype==2) wxbb_defined.push_back(out->second);
696
697       }
698     for (hi=user_defined.begin();hi!=user_defined.end();++hi) 
699       {
700         InsertInputOutput(mOutputList,*hi);
701       }
702     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) 
703       {
704         InsertInputOutput(mOutputList,*hi);
705       }
706     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) 
707       {
708         InsertInputOutput(mOutputList,*hi);
709       }
710   }
711   //================================================================
712
713
714   //================================================================
715   void WxGUIBlackBoxInfo::InsertInputOutput(wxListCtrl* l,
716                                             BlackBoxInputOutputDescriptor* d)
717   {
718     wxListItem kNewItem;
719     kNewItem.SetAlign(wxLIST_FORMAT_LEFT);
720     int nID = l->GetItemCount();
721     std::cout << nID << std::endl;
722     kNewItem.SetId(nID);
723     //    kNewItem.SetMask(wxLIST_MASK_DATA);
724     //    kNewItem.SetData(d);  
725     l->InsertItem(kNewItem);
726     l->SetItem(nID, 0, std2wx(d->GetName()) );
727     l->SetItem(nID, 1, std2wx(d->GetTypeName()) );
728     l->SetItem(nID, 2, std2wx(d->GetDescription()) );
729   }
730   //================================================================
731
732   //================================================================
733   WxGUIBlackBoxInfo::~WxGUIBlackBoxInfo()
734   {
735   }
736   //================================================================
737
738
739  enum 
740     {
741       id_f1,
742       id_f2,
743       id_f3,
744       id_f4,
745       id_f5,
746       id_f6
747
748     };
749
750
751   //================================================================
752   WxGUIPackageBrowser2::WxGUIPackageBrowser2( wxWindow *parent,
753                                               WxGUIPackageBrowser2User* user )
754     : wxPanel(parent, -1),
755       mUser(user),
756       mInterpreter(0)
757   {
758
759     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
760     mBoxList = new WxGUIBlackBoxList(this, LIST_CTRL,
761                                      wxDefaultPosition,
762                                      wxDefaultSize,
763                                      wxLC_REPORT //wxLC_LIST
764                                      | wxSUNKEN_BORDER);
765     mBoxList->SetUser(this);
766     mBoxList->SetBackgroundColour(*wxWHITE);
767     sizer->Add(mBoxList,5,wxGROW);
768
769     mBoxInfo = new WxGUIBlackBoxInfo(this);
770     sizer->Add(mBoxInfo,3,wxGROW);
771
772     
773
774     wxBoxSizer *fsizer = new wxStaticBoxSizer
775       ( new wxStaticBox(this, wxID_ANY, _T("Filters")), wxHORIZONTAL );    
776
777     mPackageFilter = new wxTextCtrl(this,id_f1,_T(""),
778                                     wxDefaultPosition,
779                                     wxDefaultSize,
780                                     wxTE_PROCESS_ENTER);
781     fsizer->Add(mPackageFilter,1,wxGROW);
782     mNameFilter = new wxTextCtrl(this,id_f2,_T(""),
783                                     wxDefaultPosition,
784                                     wxDefaultSize,
785                                     wxTE_PROCESS_ENTER);
786     fsizer->Add(mNameFilter,1,wxGROW);
787     mDescriptionFilter = new wxTextCtrl(this,id_f3,_T(""),
788                                     wxDefaultPosition,
789                                     wxDefaultSize,
790                                     wxTE_PROCESS_ENTER);
791     fsizer->Add(mDescriptionFilter,1,wxGROW);
792     mCategoryFilter = new wxTextCtrl(this,id_f4,_T(""),
793                                     wxDefaultPosition,
794                                     wxDefaultSize,
795                                     wxTE_PROCESS_ENTER);
796     fsizer->Add(mCategoryFilter,1,wxGROW);
797     mInputTypeFilter = new wxTextCtrl(this,id_f4,_T(""),
798                                     wxDefaultPosition,
799                                     wxDefaultSize,
800                                     wxTE_PROCESS_ENTER);
801     fsizer->Add(mInputTypeFilter,1,wxGROW);
802     mOutputTypeFilter = new wxTextCtrl(this,id_f4,_T(""),
803                                     wxDefaultPosition,
804                                     wxDefaultSize,
805                                     wxTE_PROCESS_ENTER);
806     fsizer->Add(mOutputTypeFilter,1,wxGROW);
807
808     
809     sizer->Add(fsizer,1,wxGROW);
810
811     SetSizer(sizer);
812     SetAutoLayout(true);
813     Layout();
814     
815   }
816   //================================================================
817
818   //================================================================
819   void WxGUIPackageBrowser2::OnFilter(wxCommandEvent&)
820   {
821     RebuildList();
822   }
823   //================================================================
824
825   //================================================================
826   // User callback when a box is selected in the list 
827   void WxGUIPackageBrowser2::WxGUIBlackBoxListUserOnSelected( BlackBoxDescriptor* d)
828   {
829     mBoxInfo->UpdateInfo(d);
830   }
831   
832   //================================================================
833
834   //================================================================
835   WxGUIPackageBrowser2::~WxGUIPackageBrowser2()
836   {
837     std::cout << "del interpreter" << std::endl;
838     if (mInterpreter) delete mInterpreter;
839     std::cout << "ok" << std::endl;
840   }
841   //================================================================
842
843   //================================================================
844   void WxGUIPackageBrowser2::IncludeAll()
845   {
846     if (!mInterpreter) mInterpreter = new bbtk::Interpreter();
847     mInterpreter->SetCommandLine(true);
848     mInterpreter->InterpretLine("include *");
849
850     Factory* F = mInterpreter->GetExecuter()->GetFactory();
851     BuildFromFactory(F);
852   }
853   //================================================================
854
855   //================================================================
856   void WxGUIPackageBrowser2::BuildFromFactory(Factory* F)
857   {
858     mFactory = F;
859     RebuildList();
860   }  
861   //================================================================
862
863   //================================================================
864   void WxGUIPackageBrowser2::RebuildList()
865   {
866     mBoxList->Hide();
867     mBoxList->ClearAll();    
868     
869     const Factory::PackageMapType& M = mFactory->GetPackageMap();
870     Factory::PackageMapType::const_iterator i;
871     for (i=M.begin();i!=M.end();++i)
872       {
873         Package* P = i->second.mPackage;
874         if (P->GetName() == "user") continue;
875         
876         Package::BlackBoxMapType::iterator j;
877         for (j=P->GetBlackBoxMap().begin();
878              j!=P->GetBlackBoxMap().end();
879              ++j)
880           {
881             //      std::cout << "Insert "<<j->second->GetTypeName()<<std::endl;
882             if (IsVisible(j->second))
883               mBoxList->Insert(j->second);
884           }
885       }
886     mBoxList->Show();
887   }
888   //================================================================
889
890   //================================================================
891   bool WxGUIPackageBrowser2::IsVisible(BlackBoxDescriptor* d)
892   {
893     //    std::cout << "   '" << wx2std(mPackageFilter->GetValue())<<"'"<<std::endl;
894     //    std::cout << "vs '" << d->GetPackage()->GetName() << "'"<<std::endl;
895     if (d->GetPackage()->GetName().find( wx2std(mPackageFilter->GetValue()) ) 
896         == std::string::npos ) return false;
897    if (d->GetTypeName().find( wx2std(mNameFilter->GetValue()) ) 
898         == std::string::npos ) return false;
899    if (d->GetDescription().find( wx2std(mDescriptionFilter->GetValue()) ) 
900         == std::string::npos ) return false;
901    if (d->GetCategory().find( wx2std(mCategoryFilter->GetValue()) ) 
902         == std::string::npos ) return false;
903    if (mInputTypeFilter->GetValue().size()>0)
904      {
905        std::string s = wx2std(mInputTypeFilter->GetValue());
906        bool found = false;
907        const BlackBoxDescriptor::InputDescriptorMapType& imap = 
908          d->GetInputDescriptorMap();
909        BlackBoxDescriptor::InputDescriptorMapType::const_iterator in;
910        for ( in = imap.begin();  in != imap.end(); ++in ) 
911          {
912            if (in->second->GetTypeName().find(s)!=std::string::npos)
913              {
914                found = true;
915                break;
916              }
917          }
918        if (!found) return false;
919      }
920    if (mOutputTypeFilter->GetValue().size()>0)
921      {
922        std::string s = wx2std(mOutputTypeFilter->GetValue());
923        bool found = false;
924        const BlackBoxDescriptor::OutputDescriptorMapType& imap = 
925          d->GetOutputDescriptorMap();
926        BlackBoxDescriptor::OutputDescriptorMapType::const_iterator in;
927        for ( in = imap.begin();  in != imap.end(); ++in ) 
928          {
929            if (in->second->GetTypeName().find(s)!=std::string::npos)
930              {
931                found = true;
932                break;
933              }
934          }
935        if (!found) return false;
936      }
937
938
939     return true;
940   }
941   //========================================================================
942
943   //========================================================================
944   BEGIN_EVENT_TABLE(WxGUIPackageBrowser2, wxPanel)
945     EVT_TEXT_ENTER(id_f1, WxGUIPackageBrowser2::OnFilter )
946     EVT_TEXT_ENTER(id_f2, WxGUIPackageBrowser2::OnFilter )
947     EVT_TEXT_ENTER(id_f3, WxGUIPackageBrowser2::OnFilter )
948     EVT_TEXT_ENTER(id_f4, WxGUIPackageBrowser2::OnFilter )
949     EVT_TEXT_ENTER(id_f5, WxGUIPackageBrowser2::OnFilter )
950     EVT_TEXT_ENTER(id_f6, WxGUIPackageBrowser2::OnFilter )
951     END_EVENT_TABLE()
952   //========================================================================
953
954
955
956   //================================================================
957   WxGUIPackageBrowser2Window::WxGUIPackageBrowser2Window( wxWindow *parent, 
958                                                             wxString title, 
959                                                             wxSize size)
960       : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
961   {     
962     
963     
964     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
965
966     mBrowser = new WxGUIPackageBrowser2(this);
967     mBrowser->IncludeAll();
968     sizer->Add(mBrowser,1,wxGROW);
969     
970     //    WxGUICommand* com = new WxGUICommand(this,this);
971     //    sizer->Add(com);
972     
973     SetSizer(sizer);
974
975     // Creates and sets the parent window of all bbtk windows
976     wxWindow* top = new wxPanel(this,-1);
977     top->Hide();
978     
979     Wx::SetTopWindow(top);
980    
981     SetAutoLayout(true);
982     Layout();
983   }
984   //================================================================
985
986   //================================================================
987   WxGUIPackageBrowser2Window::~WxGUIPackageBrowser2Window()
988   {
989   }
990   //================================================================
991
992 } // namespace bbtk
993
994
995 #endif //_USE_WXWIDGETS_