]> 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/06/10 19:19:42 $
7 Version:   $Revision: 1.9 $
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::Pointer d)
113   {
114     if (GetColumnCount()!=3)
115       {
116         InsertColumn( 0, _("Package"), 
117                       wxLIST_FORMAT_LEFT, 60  ); 
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     // TO DO : STORE SMART POINTER ?
131     kNewItem.SetData(d.get());  
132     //    BlackBoxDescriptor::Pointer* d2 = (BlackBoxDescriptor::Pointer*)kNewItem.GetData();
133     //    std::cout << "Descr = "<<d<<" = "<<d2<<std::endl;
134     this->InsertItem(kNewItem);
135     this->SetItem(nID, 0, std2wx(d->GetPackage()->GetName()) );
136     this->SetItem(nID, 1, std2wx(d->GetTypeName()) );
137     this->SetItem(nID, 2, std2wx(d->GetDescription()) );
138     
139     /*
140       if (nID % 2)
141       {
142       kNewItem.SetBackgroundColour(wxColour(192,192,192));
143       this->SetItem(kNewItem);
144       }
145     */
146     
147   }
148   
149   
150   void WxGUIBlackBoxList::OnCacheHint(wxListEvent& event)
151   {
152     //wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
153     //                  event.GetCacheFrom(), event.GetCacheTo() );
154   }
155   
156   void WxGUIBlackBoxList::SetColumnImage(int col, int image)
157   {
158     /*
159       wxListItem item;
160       item.SetMask(wxLIST_MASK_IMAGE);
161       item.SetImage(image);
162       SetColumn(col, item);
163     */
164   }
165
166   void WxGUIBlackBoxList::OnColClick(wxListEvent& event)
167   {
168     int col = event.GetColumn();
169
170     // set or unset image
171     static bool x = false;
172     x = !x;
173     SetColumnImage(col, x ? 0 : -1);
174
175     //    wxLogMessage( wxT("OnColumnClick at %d."), col );
176   }
177
178   void WxGUIBlackBoxList::OnColRightClick(wxListEvent& event)
179   {
180     int col = event.GetColumn();
181     if ( col != -1 )
182       {
183         SetColumnImage(col, -1);
184       }
185
186     // Show popupmenu at position
187     wxMenu menu(wxT("Test"));
188     menu.Append(-1, _T("&About"));
189     PopupMenu(&menu, event.GetPoint());
190
191     //wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() );
192   }
193
194   void WxGUIBlackBoxList::LogColEvent(const wxListEvent& event, const wxChar *name)
195   {
196     const int col = event.GetColumn();
197
198     /*
199       wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
200       name,
201       col,
202       event.GetItem().GetWidth(),
203       GetColumnWidth(col));
204     */
205   }
206
207   void WxGUIBlackBoxList::OnColBeginDrag(wxListEvent& event)
208   {
209     LogColEvent( event, wxT("OnColBeginDrag") );
210     /*
211       if ( event.GetColumn() == 0 )
212       {
213       //wxLogMessage(_T("Resizing this column shouldn't work."));
214
215       event.Veto();
216       }
217     */
218   }
219
220   void WxGUIBlackBoxList::OnColDragging(wxListEvent& event)
221   {
222     LogColEvent( event, wxT("OnColDragging") );
223   }
224
225   void WxGUIBlackBoxList::OnColEndDrag(wxListEvent& event)
226   {
227     LogColEvent( event, wxT("OnColEndDrag") );
228   }
229
230   void WxGUIBlackBoxList::OnBeginDrag(wxListEvent& event)
231   {
232     const wxPoint& pt = event.m_pointDrag;
233
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         // TO DO : STORE SMART PTR ?
316         BlackBoxDescriptor* d = (BlackBoxDescriptor*)(info.GetData());
317         if (d!=0) mUser->WxGUIBlackBoxListUserOnSelected(d);
318       }
319     else
320       {
321         wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
322       }
323   }
324
325   void WxGUIBlackBoxList::OnDeselected(wxListEvent& event)
326   {
327     LogEvent(event, _T("OnDeselected"));
328   }
329
330   void WxGUIBlackBoxList::OnActivated(wxListEvent& event)
331   {
332     LogEvent(event, _T("OnActivated"));
333   }
334
335   void WxGUIBlackBoxList::OnFocused(wxListEvent& event)
336   {
337     LogEvent(event, _T("OnFocused"));
338
339     event.Skip();
340   }
341
342   void WxGUIBlackBoxList::OnListKeyDown(wxListEvent& event)
343   {
344     long item;
345
346     switch ( event.GetKeyCode() )
347       {
348       case 'c': // colorize
349       case 'C':
350         {
351           wxListItem info;
352           info.m_itemId = event.GetIndex();
353           if ( info.m_itemId == -1 )
354             {
355               // no item
356               break;
357             }
358
359           GetItem(info);
360
361           wxListItemAttr *attr = info.GetAttributes();
362           if ( !attr || !attr->HasTextColour() )
363             {
364               info.SetTextColour(*wxCYAN);
365
366               SetItem(info);
367
368               RefreshItem(info.m_itemId);
369             }
370         }
371         break;
372
373       case 'n': // next
374       case 'N':
375         item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
376         if ( item++ == GetItemCount() - 1 )
377           {
378             item = 0;
379           }
380
381         //wxLogMessage(_T("Focusing item %ld"), item);
382
383         SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
384         EnsureVisible(item);
385         break;
386
387       case 'r': // show bounding Rect
388       case 'R':
389         {
390           item = event.GetIndex();
391           wxRect r;
392           if ( !GetItemRect(item, r) )
393             {
394               //wxLogError(_T("Failed to retrieve rect of item %ld"), item);
395               break;
396             }
397
398           //wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
399           //   item, r.x, r.y, r.x + r.width, r.y + r.height);
400         }
401         break;
402
403       case WXK_DELETE:
404         item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
405         /*
406           while ( item != -1 )
407           {
408           DeleteItem(item);
409
410           //wxLogMessage(_T("Item %ld deleted"), item);
411
412           // -1 because the indices were shifted by DeleteItem()
413           item = GetNextItem(item - 1,
414           wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
415           }
416         */
417         break;
418
419       case WXK_INSERT:
420         if ( GetWindowStyle() & wxLC_REPORT )
421           {
422             if ( GetWindowStyle() & wxLC_VIRTUAL )
423               {
424                 SetItemCount(GetItemCount() + 1);
425               }
426             else // !virtual
427               {
428                 //InsertItemInReportView(event.GetIndex());
429               }
430           }
431         //else: fall through
432
433       default:
434         LogEvent(event, _T("OnListKeyDown"));
435
436         event.Skip();
437       }
438   }
439
440   void WxGUIBlackBoxList::OnChar(wxKeyEvent& event)
441   {
442     //wxLogMessage(_T("Got char event."));
443
444     switch ( event.GetKeyCode() )
445       {
446       case 'n':
447       case 'N':
448       case 'c':
449       case 'C':
450         // these are the keys we process ourselves
451         break;
452
453       default:
454         event.Skip();
455       }
456   }
457
458   void WxGUIBlackBoxList::OnRightClick(wxMouseEvent& event)
459   {
460     if ( !event.ControlDown() )
461       {
462         event.Skip();
463         return;
464       }
465
466     int flags;
467     long subitem;
468     long item = HitTest(event.GetPosition(), flags, &subitem);
469
470     wxString where;
471     switch ( flags )
472       {
473       case wxLIST_HITTEST_ABOVE: where = _T("above"); break;
474       case wxLIST_HITTEST_BELOW: where = _T("below"); break;
475       case wxLIST_HITTEST_NOWHERE: where = _T("nowhere near"); break;
476       case wxLIST_HITTEST_ONITEMICON: where = _T("on icon of"); break;
477       case wxLIST_HITTEST_ONITEMLABEL: where = _T("on label of"); break;
478       case wxLIST_HITTEST_ONITEMRIGHT: where = _T("right on"); break;
479       case wxLIST_HITTEST_TOLEFT: where = _T("to the left of"); break;
480       case wxLIST_HITTEST_TORIGHT: where = _T("to the right of"); break;
481       default: where = _T("not clear exactly where on"); break;
482       }
483
484     //wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
485     //             where.c_str(), item, subitem);
486   }
487
488   void WxGUIBlackBoxList::LogEvent(const wxListEvent& event, const wxChar *eventName)
489   {
490     //wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
491     //            event.GetIndex(), eventName,
492     //              event.GetText().c_str(), event.GetData());
493   }
494
495   wxString WxGUIBlackBoxList::OnGetItemText(long item, long column) const
496   {
497     /*
498       if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS) )
499       {
500       return SMALL_VIRTUAL_VIEW_ITEMS[item][column];
501       }
502       else
503       {
504     */
505     return wxString::Format(_T("Column %ld of item %ld"), column, item);
506     //  }
507   }
508
509   int WxGUIBlackBoxList::OnGetItemColumnImage(long item, long column) const
510   {
511     if (!column)
512       return 0;
513
514     if (!(item %3) && column == 1)
515       return 0;
516
517     return -1;
518   }
519
520   wxListItemAttr *WxGUIBlackBoxList::OnGetItemAttr(long item) const
521   {
522     return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
523   }
524
525
526 #if USE_CONTEXT_MENU
527   void WxGUIBlackBoxList::OnContextMenu(wxContextMenuEvent& event)
528   {
529     wxPoint point = event.GetPosition();
530     // If from keyboard
531     if (point.x == -1 && point.y == -1) {
532       wxSize size = GetSize();
533       point.x = size.x / 2;
534       point.y = size.y / 2;
535     } else {
536       point = ScreenToClient(point);
537     }
538     ShowContextMenu(point);
539   }
540 #endif
541
542   void WxGUIBlackBoxList::ShowContextMenu(const wxPoint& pos)
543   {
544     wxMenu menu;
545   
546     menu.Append(wxID_ABOUT, _T("&About"));
547     menu.AppendSeparator();
548     menu.Append(wxID_EXIT, _T("E&xit"));
549   
550     PopupMenu(&menu, pos.x, pos.y);
551   }
552   //================================================================
553
554
555
556   //================================================================
557   WxGUIBlackBoxInfo::WxGUIBlackBoxInfo(wxWindow* parent)
558     :
559     wxPanel(parent, -1),
560     mDescriptor()
561   {
562     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
563     
564     //    mName = new wxStaticText(this,-1,_T(""));
565     //    sizer->Add(mName,0,wxGROW);
566     mDescription = new wxStaticText(this,-1,_T(""));
567     sizer->Add(mDescription,0,wxGROW);
568     mAuthor = new wxStaticText(this,-1,_T(""));
569     sizer->Add(mAuthor,0,wxGROW);
570     mCategory = new wxStaticText(this,-1,_T(""));
571     sizer->Add(mCategory,0,wxGROW);
572
573
574     wxBoxSizer *inputs = 
575       new wxStaticBoxSizer
576       ( new wxStaticBox(this, wxID_ANY, _T("Inputs")), wxVERTICAL );    
577     mInputList = new wxListCtrl(this, -1,
578                                 wxDefaultPosition,
579                                 wxDefaultSize,
580                                 wxLC_REPORT //wxLC_LIST
581                                 | wxSUNKEN_BORDER);
582     
583     
584     inputs->Add(mInputList,1,wxGROW);
585     sizer->Add(inputs,1,wxGROW);
586
587     wxBoxSizer *outputs = 
588       new wxStaticBoxSizer
589       ( new wxStaticBox(this, wxID_ANY, _T("Outputs")), wxVERTICAL );    
590     mOutputList = new wxListCtrl(this, -1,
591                                  wxDefaultPosition,
592                                  wxDefaultSize,
593                                  wxLC_REPORT //wxLC_LIST
594                                  | wxSUNKEN_BORDER);
595     
596     outputs->Add(mOutputList,1,wxGROW);
597     sizer->Add(outputs,1,wxGROW);
598     
599     
600     SetSizer(sizer);
601     SetAutoLayout(true);
602     Layout();
603   }
604   //================================================================
605
606     
607   //================================================================
608   void WxGUIBlackBoxInfo::UpdateInfo(BlackBoxDescriptor* descr)
609   {
610
611     //    mName->SetLabel(std2wx(descr->GetTypeName()));
612     mDescription->SetLabel(std2wx(descr->GetDescription()));
613     mAuthor->SetLabel(std2wx(descr->GetAuthor()));
614     mCategory->SetLabel(std2wx(descr->GetCategory()));
615     
616     mInputList->ClearAll();
617     mInputList->InsertColumn( 0, _("Name"), 
618                                wxLIST_FORMAT_LEFT, 80  ); 
619     mInputList->InsertColumn( 1, _("Type"), 
620                                wxLIST_FORMAT_LEFT, 100 ); 
621     mInputList->InsertColumn( 2, _("Nature"), 
622                                wxLIST_FORMAT_LEFT, 100  ); 
623     mInputList->InsertColumn( 3, _("Description"), 
624                                wxLIST_FORMAT_LEFT, 500  ); 
625
626     mOutputList->ClearAll();
627     mOutputList->InsertColumn( 0, _("Name"), 
628                                wxLIST_FORMAT_LEFT, 80  ); 
629     mOutputList->InsertColumn( 1, _("Type"), 
630                                wxLIST_FORMAT_LEFT, 100 ); 
631     mOutputList->InsertColumn( 2, _("Nature"), 
632                                wxLIST_FORMAT_LEFT, 100  ); 
633     mOutputList->InsertColumn( 3, _("Description"), 
634                                wxLIST_FORMAT_LEFT, 500  ); 
635     
636
637     std::vector<BlackBoxInputOutputDescriptor*> user_defined;
638     std::vector<BlackBoxInputOutputDescriptor*> ubb_defined;
639     std::vector<BlackBoxInputOutputDescriptor*> wxbb_defined;
640     
641     const BlackBoxDescriptor::InputDescriptorMapType& imap = 
642       descr->GetInputDescriptorMap();
643     BlackBoxDescriptor::InputDescriptorMapType::const_iterator in;
644     for ( in = imap.begin();  in != imap.end(); ++in ) 
645       {
646         int iotype = 0;
647         if (in->second->GetCreatorTypeInfo() == 
648             typeid(AtomicBlackBoxDescriptor))
649           {
650             iotype = 1;
651           }
652         else if (in->second->GetCreatorTypeInfo() == 
653                  typeid(WxBlackBoxDescriptor))
654           {
655             iotype = 2;
656           }
657         if (iotype==0) user_defined.push_back(in->second);
658         else if (iotype==1) ubb_defined.push_back(in->second);
659         else if (iotype==2) wxbb_defined.push_back(in->second);
660         
661       }
662
663     std::vector<BlackBoxInputOutputDescriptor*>::iterator hi;
664     for (hi=user_defined.begin();hi!=user_defined.end();++hi) 
665       {
666         InsertInputOutput(mInputList,*hi);
667       }
668     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) 
669       {
670         InsertInputOutput(mInputList,*hi);
671       }
672     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) 
673       {
674         InsertInputOutput(mInputList,*hi);
675       }
676     
677     // Outputs
678     user_defined.clear();
679     ubb_defined.clear();
680     wxbb_defined.clear();
681     const BlackBoxDescriptor::OutputDescriptorMapType& omap = 
682       descr->GetOutputDescriptorMap();
683     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator out;
684     for ( out = omap.begin();  out != omap.end(); ++out ) 
685       {
686         int iotype = 0;
687         if (out->second->GetCreatorTypeInfo() == 
688             typeid(AtomicBlackBoxDescriptor))
689           {
690             iotype = 1;
691           }
692         else if (out->second->GetCreatorTypeInfo() == 
693                  typeid(WxBlackBoxDescriptor))
694           {
695             iotype = 2;
696           }
697        
698         if (iotype==0) user_defined.push_back(out->second);
699         else if (iotype==1) ubb_defined.push_back(out->second);
700         else if (iotype==2) wxbb_defined.push_back(out->second);
701
702       }
703     for (hi=user_defined.begin();hi!=user_defined.end();++hi) 
704       {
705         InsertInputOutput(mOutputList,*hi);
706       }
707     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) 
708       {
709         InsertInputOutput(mOutputList,*hi);
710       }
711     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) 
712       {
713         InsertInputOutput(mOutputList,*hi);
714       }
715   }
716   //================================================================
717
718
719   //================================================================
720   void WxGUIBlackBoxInfo::InsertInputOutput(wxListCtrl* l,
721                                             BlackBoxInputOutputDescriptor* d)
722   {
723     wxListItem kNewItem;
724     kNewItem.SetAlign(wxLIST_FORMAT_LEFT);
725     int nID = l->GetItemCount();
726     //    std::cout << nID << std::endl;
727     kNewItem.SetId(nID);
728     //    kNewItem.SetMask(wxLIST_MASK_DATA);
729     //    kNewItem.SetData(d);  
730     l->InsertItem(kNewItem);
731     l->SetItem(nID, 0, std2wx(d->GetName()) );
732     l->SetItem(nID, 1, std2wx(d->GetTypeName()) );
733     l->SetItem(nID, 2, std2wx(d->GetNature()) );
734     l->SetItem(nID, 3, std2wx(d->GetDescription()) );
735   }
736   //================================================================
737
738   //================================================================
739   WxGUIBlackBoxInfo::~WxGUIBlackBoxInfo()
740   {
741   }
742   //================================================================
743
744
745  enum 
746     {
747       id_f1,
748       id_f2,
749       id_f3,
750       id_f4,
751       id_f5,
752       id_f6,
753       id_f7,
754       id_f8,
755       id_fc1,
756       id_fc2,
757       id_fc3
758     };
759
760
761   //================================================================
762   WxGUIPackageBrowser2::WxGUIPackageBrowser2( wxWindow *parent,
763                                               WxGUIPackageBrowser2User* user )
764     : wxPanel(parent, -1),
765       mUser(user),
766       mInterpreter()
767   {
768     m_mgr.SetManagedWindow(this);
769
770     //    wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
771     mBoxList = new WxGUIBlackBoxList(this, LIST_CTRL,
772                                      wxDefaultPosition,
773                                      wxDefaultSize,
774                                      wxLC_REPORT //wxLC_LIST
775                                      | wxSUNKEN_BORDER);
776     mBoxList->SetUser(this);
777     mBoxList->SetBackgroundColour(*wxWHITE);
778
779     m_mgr.AddPane(mBoxList,
780                   wxAuiPaneInfo().Name(wxT("BoxList"))
781                   .Caption(wxT("Black Boxes"))
782                   .MinimizeButton(true)
783                   .MaximizeButton(true)
784                   .Center()
785                   .MinSize(wxSize(100,100))
786                   );     
787     //    sizer->Add(mBoxList,5,wxGROW);
788
789     mBoxInfo = new WxGUIBlackBoxInfo(this);
790     //    sizer->Add(mBoxInfo,3,wxGROW);
791
792     m_mgr.AddPane(mBoxInfo,
793                   wxAuiPaneInfo().Name(wxT("Box"))
794                   .Caption(wxT(""))
795                   .MinimizeButton(true)
796                   .MaximizeButton(true)
797                   .Bottom()
798                   .MinSize(wxSize(100,300))
799                   );     
800     
801
802     wxPanel* filters = new wxPanel(this,-1);
803     wxBoxSizer *fsizer = new wxBoxSizer(wxVERTICAL );    
804     wxBoxSizer *fpack = 
805       new wxStaticBoxSizer
806       ( new wxStaticBox(filters, wxID_ANY, _T("Package")), wxHORIZONTAL );    
807     mPackageFilter = new wxTextCtrl(filters,id_f1,_T(""),
808                                     wxDefaultPosition,
809                                     wxDefaultSize,
810                                     wxTE_PROCESS_ENTER);
811     fpack->Add(mPackageFilter,1,wxGROW);
812     fsizer->Add(fpack,0,wxGROW);
813     wxBoxSizer *fname = 
814       new wxStaticBoxSizer
815       ( new wxStaticBox(filters, wxID_ANY, _T("Name")), wxHORIZONTAL );    
816     mNameFilter = new wxTextCtrl(filters,id_f2,_T(""),
817                                     wxDefaultPosition,
818                                     wxDefaultSize,
819                                     wxTE_PROCESS_ENTER);
820     fname->Add(mNameFilter,1,wxGROW);
821     fsizer->Add(fname,0,wxGROW);
822     wxBoxSizer *fdescr = 
823       new wxStaticBoxSizer
824       ( new wxStaticBox(filters, wxID_ANY, _T("Description")), wxHORIZONTAL );
825     mDescriptionFilter = new wxTextCtrl(filters,id_f3,_T(""),
826                                     wxDefaultPosition,
827                                     wxDefaultSize,
828                                     wxTE_PROCESS_ENTER);
829     fdescr->Add(mDescriptionFilter,1,wxGROW);
830     fsizer->Add(fdescr,0,wxGROW);
831     wxBoxSizer *fcat = 
832       new wxStaticBoxSizer
833       ( new wxStaticBox(filters, wxID_ANY, _T("Category")), wxHORIZONTAL );    
834     mCategoryFilter = new wxTextCtrl(filters,id_f4,_T(""),
835                                     wxDefaultPosition,
836                                     wxDefaultSize,
837                                     wxTE_PROCESS_ENTER);
838     fcat->Add(mCategoryFilter,1,wxGROW);
839     fsizer->Add(fcat,0,wxGROW);
840     wxBoxSizer *fintype = 
841       new wxStaticBoxSizer
842       ( new wxStaticBox(filters, wxID_ANY, _T("Input type")), wxHORIZONTAL );
843     mInputTypeFilter = new wxTextCtrl(filters,id_f5,_T(""),
844                                     wxDefaultPosition,
845                                     wxDefaultSize,
846                                     wxTE_PROCESS_ENTER);
847     fintype->Add(mInputTypeFilter,1,wxGROW);
848     fsizer->Add(fintype,0,wxGROW);
849     wxBoxSizer *fouttype = 
850       new wxStaticBoxSizer
851       ( new wxStaticBox(filters, wxID_ANY, _T("Output type")), wxHORIZONTAL );
852     mOutputTypeFilter = new wxTextCtrl(filters,id_f6,_T(""),
853                                     wxDefaultPosition,
854                                     wxDefaultSize,
855                                     wxTE_PROCESS_ENTER);
856     fouttype->Add(mOutputTypeFilter,1,wxGROW);
857     fsizer->Add(fouttype,0,wxGROW);
858     wxBoxSizer *finnat = 
859       new wxStaticBoxSizer
860       ( new wxStaticBox(filters, wxID_ANY, _T("Input nature")),wxHORIZONTAL ); 
861     mInputNatureFilter = new wxTextCtrl(filters,id_f7,_T(""),
862                                         wxDefaultPosition,
863                                         wxDefaultSize,
864                                         wxTE_PROCESS_ENTER);
865     finnat->Add(mInputNatureFilter,1,wxGROW);
866     fsizer->Add(finnat,0,wxGROW);
867     wxBoxSizer *foutnat = 
868       new wxStaticBoxSizer
869       ( new wxStaticBox(filters, wxID_ANY,_T("Output nature")),wxHORIZONTAL ); 
870     mOutputNatureFilter = new wxTextCtrl(filters,id_f8,_T(""),
871                                         wxDefaultPosition,
872                                         wxDefaultSize,
873                                         wxTE_PROCESS_ENTER);
874     foutnat->Add(mOutputNatureFilter,1,wxGROW);
875     fsizer->Add(foutnat,0,wxGROW);
876
877
878     mShowWidgetsFilter = new wxCheckBox(filters,id_fc1,_T("Show widgets"));
879     mShowWidgetsFilter->SetValue(true);
880     fsizer->Add(mShowWidgetsFilter,0,wxGROW);
881
882     mShowAdaptorsFilter = new wxCheckBox(filters,id_fc2,_T("Show adaptors"));
883     mShowAdaptorsFilter->SetValue(false);
884     fsizer->Add(mShowAdaptorsFilter,0,wxGROW);
885
886     mShowGUIsFilter = new wxCheckBox(filters,id_fc3,_T("Show GUIs"));
887     mShowGUIsFilter->SetValue(false);
888     fsizer->Add(mShowGUIsFilter,0,wxGROW);
889
890     filters->SetSizer(fsizer);
891
892     m_mgr.AddPane(filters,
893                   wxAuiPaneInfo().Name(wxT("Filter"))
894                   .Caption(wxT("Filter"))
895                   .MinimizeButton(true)
896                   .MaximizeButton(true)
897                   .Right()
898                   .MinSize(wxSize(100,100)) );
899
900     
901
902     //    sizer->Add(fsizer,1,wxGROW);
903     m_mgr.Update();
904
905     //    SetSizer(sizer);
906     SetAutoLayout(true);
907     Layout();
908     
909   }
910   //================================================================
911
912   //================================================================
913   void WxGUIPackageBrowser2::OnFilter(wxCommandEvent&)
914   {
915     RebuildList();
916   }
917   //================================================================
918
919   //================================================================
920   // User callback when a box is selected in the list 
921   void WxGUIPackageBrowser2::WxGUIBlackBoxListUserOnSelected
922   ( BlackBoxDescriptor* d)
923   {
924     mBoxInfo->UpdateInfo(d);
925     std::string title = d->GetPackage()->GetName()+"::"+d->GetTypeName();
926     m_mgr.GetPane(mBoxInfo).Caption(std2wx(title));
927     m_mgr.Update();
928   }
929   
930   //================================================================
931
932   //================================================================
933   WxGUIPackageBrowser2::~WxGUIPackageBrowser2()
934   {
935     mFactory.reset();
936     mInterpreter.reset();
937     //    Object::PrintObjectListInfo("");
938     m_mgr.UnInit();
939   }
940   //================================================================
941
942   //================================================================
943   void WxGUIPackageBrowser2::IncludeAll()
944   {
945     if (!mInterpreter) mInterpreter =bbtk::Interpreter::New();
946     mInterpreter->SetCommandLine(true);
947     std::stringstream* buf = new std::stringstream;
948     *buf << "message max 0" << std::endl; 
949     *buf << "include *" << std::endl;
950     mInterpreter->InterpretBuffer(buf);
951
952     Factory::Pointer F = mInterpreter->GetExecuter()->GetFactory();
953     BuildFromFactory(F);
954   }
955   //================================================================
956
957   //================================================================
958   void WxGUIPackageBrowser2::BuildFromFactory(Factory::Pointer F)
959   {
960     mFactory = F;
961     RebuildList();
962   }  
963   //================================================================
964
965   //================================================================
966   void WxGUIPackageBrowser2::RebuildList()
967   {
968     mBoxList->Hide();
969     mBoxList->ClearAll();    
970     
971     const Factory::PackageMapType& M = mFactory->GetPackageMap();
972     Factory::PackageMapType::const_iterator i;
973     for (i=M.begin();i!=M.end();++i)
974       {
975         Package::Pointer P = i->second;
976         if (P->GetName() == "user") continue;
977         
978         Package::BlackBoxMapType::iterator j;
979         for (j=P->GetBlackBoxMap().begin();
980              j!=P->GetBlackBoxMap().end();
981              ++j)
982           {
983             //      std::cout << "Insert "<<j->second->GetTypeName()<<std::endl;
984             if (IsVisible(j->second))
985               mBoxList->Insert(j->second);
986           }
987       }
988     mBoxList->Show();
989   }
990   //================================================================
991
992   //================================================================
993   bool WxGUIPackageBrowser2::IsVisible(BlackBoxDescriptor::Pointer d)
994   {
995     //    std::cout << "   '" << wx2std(mPackageFilter->GetValue())<<"'"<<std::endl;
996     //    std::cout << "vs '" << d->GetPackage()->GetName() << "'"<<std::endl;
997     if ((!mShowAdaptorsFilter->IsChecked())&&
998         ((d->GetKind()==BlackBoxDescriptor::ADAPTOR)||
999          (d->GetKind()==BlackBoxDescriptor::DEFAULT_ADAPTOR)))
1000       return false;
1001     if ((!mShowGUIsFilter->IsChecked())&&
1002         ((d->GetKind()==BlackBoxDescriptor::GUI)||
1003          (d->GetKind()==BlackBoxDescriptor::DEFAULT_GUI)))
1004       return false;
1005     if (d->GetPackage()->GetName().find( wx2std(mPackageFilter->GetValue()) ) 
1006         == std::string::npos ) return false;
1007    if (d->GetTypeName().find( wx2std(mNameFilter->GetValue()) ) 
1008         == std::string::npos ) return false;
1009    if (d->GetDescription().find( wx2std(mDescriptionFilter->GetValue()) ) 
1010         == std::string::npos ) return false;
1011    if (d->GetCategory().find( wx2std(mCategoryFilter->GetValue()) ) 
1012         == std::string::npos ) return false;
1013    if (!mShowWidgetsFilter->IsChecked())
1014      {
1015        bool found = false;
1016        const BlackBoxDescriptor::OutputDescriptorMapType& imap = 
1017          d->GetOutputDescriptorMap();
1018        BlackBoxDescriptor::OutputDescriptorMapType::const_iterator in;
1019        for ( in = imap.begin();  in != imap.end(); ++in ) 
1020          {
1021            if (in->second->GetName() == "Widget" )
1022              {
1023                found = true;
1024                break;
1025              }
1026          }
1027        if (found) return false;
1028      }
1029    if (mInputTypeFilter->GetValue().size()>0)
1030      {
1031        std::string s = wx2std(mInputTypeFilter->GetValue());
1032        bool found = false;
1033        const BlackBoxDescriptor::InputDescriptorMapType& imap = 
1034          d->GetInputDescriptorMap();
1035        BlackBoxDescriptor::InputDescriptorMapType::const_iterator in;
1036        for ( in = imap.begin();  in != imap.end(); ++in ) 
1037          {
1038            if (in->second->GetTypeName().find(s)!=std::string::npos)
1039              {
1040                found = true;
1041                break;
1042              }
1043          }
1044        if (!found) return false;
1045      }
1046    if (mOutputTypeFilter->GetValue().size()>0)
1047      {
1048        std::string s = wx2std(mOutputTypeFilter->GetValue());
1049        bool found = false;
1050        const BlackBoxDescriptor::OutputDescriptorMapType& imap = 
1051          d->GetOutputDescriptorMap();
1052        BlackBoxDescriptor::OutputDescriptorMapType::const_iterator in;
1053        for ( in = imap.begin();  in != imap.end(); ++in ) 
1054          {
1055            if (in->second->GetTypeName().find(s)!=std::string::npos)
1056              {
1057                found = true;
1058                break;
1059              }
1060          }
1061        if (!found) return false;
1062      }
1063    if (mInputNatureFilter->GetValue().size()>0)
1064      {
1065        std::string s = wx2std(mInputNatureFilter->GetValue());
1066        bool found = false;
1067        const BlackBoxDescriptor::InputDescriptorMapType& imap = 
1068          d->GetInputDescriptorMap();
1069        BlackBoxDescriptor::InputDescriptorMapType::const_iterator in;
1070        for ( in = imap.begin();  in != imap.end(); ++in ) 
1071          {
1072            if (in->second->GetNature().find(s)!=std::string::npos)
1073              {
1074                found = true;
1075                break;
1076              }
1077          }
1078        if (!found) return false;
1079      }
1080    if (mOutputNatureFilter->GetValue().size()>0)
1081      {
1082        std::string s = wx2std(mOutputNatureFilter->GetValue());
1083        bool found = false;
1084        const BlackBoxDescriptor::OutputDescriptorMapType& imap = 
1085          d->GetOutputDescriptorMap();
1086        BlackBoxDescriptor::OutputDescriptorMapType::const_iterator in;
1087        for ( in = imap.begin();  in != imap.end(); ++in ) 
1088          {
1089            if (in->second->GetNature().find(s)!=std::string::npos)
1090              {
1091                found = true;
1092                break;
1093              }
1094          }
1095        if (!found) return false;
1096      }
1097
1098     return true;
1099   }
1100   //========================================================================
1101
1102   //========================================================================
1103   BEGIN_EVENT_TABLE(WxGUIPackageBrowser2, wxPanel)
1104     EVT_TEXT_ENTER(id_f1, WxGUIPackageBrowser2::OnFilter )
1105     EVT_TEXT_ENTER(id_f2, WxGUIPackageBrowser2::OnFilter )
1106     EVT_TEXT_ENTER(id_f3, WxGUIPackageBrowser2::OnFilter )
1107     EVT_TEXT_ENTER(id_f4, WxGUIPackageBrowser2::OnFilter )
1108     EVT_TEXT_ENTER(id_f5, WxGUIPackageBrowser2::OnFilter )
1109     EVT_TEXT_ENTER(id_f6, WxGUIPackageBrowser2::OnFilter )
1110     EVT_TEXT_ENTER(id_f7, WxGUIPackageBrowser2::OnFilter )
1111     EVT_TEXT_ENTER(id_f8, WxGUIPackageBrowser2::OnFilter )
1112     EVT_CHECKBOX(  id_fc1, WxGUIPackageBrowser2::OnFilter )
1113     EVT_CHECKBOX(  id_fc2, WxGUIPackageBrowser2::OnFilter )
1114     EVT_CHECKBOX(  id_fc3, WxGUIPackageBrowser2::OnFilter )
1115     END_EVENT_TABLE()
1116   //========================================================================
1117
1118
1119
1120   //================================================================
1121   WxGUIPackageBrowser2Window::WxGUIPackageBrowser2Window( wxWindow *parent, 
1122                                                             wxString title, 
1123                                                             wxSize size)
1124       : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
1125   {     
1126     
1127     
1128     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
1129
1130     mBrowser = new WxGUIPackageBrowser2(this);
1131     mBrowser->IncludeAll();
1132     sizer->Add(mBrowser,1,wxGROW);
1133     
1134     //    WxGUICommand* com = new WxGUICommand(this,this);
1135     //    sizer->Add(com);
1136     
1137     SetSizer(sizer);
1138
1139      // Creates the parent window of all bbtk windows as a child of this
1140 //    Wx::CreateTopWindow(this);
1141     // Add the method OnWxSignal as a Wx::Signal observer 
1142     //bbtkAddWxSignalObserver(WxGUIPackageBrowser2Window::OnWxSignal);
1143  
1144     SetAutoLayout(true);
1145     Layout();
1146   }
1147   //================================================================
1148
1149   //================================================================
1150   WxGUIPackageBrowser2Window::~WxGUIPackageBrowser2Window()
1151   {
1152   }
1153   //================================================================
1154
1155 } // namespace bbtk
1156
1157
1158 #endif //_USE_WXWIDGETS_