]> 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/07/23 11:46:11 $
7 Version:   $Revision: 1.10 $
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 = 
469     HitTest(event.GetPosition(), flags, &subitem);
470
471     wxString where;
472     switch ( flags )
473       {
474       case wxLIST_HITTEST_ABOVE: where = _T("above"); break;
475       case wxLIST_HITTEST_BELOW: where = _T("below"); break;
476       case wxLIST_HITTEST_NOWHERE: where = _T("nowhere near"); break;
477       case wxLIST_HITTEST_ONITEMICON: where = _T("on icon of"); break;
478       case wxLIST_HITTEST_ONITEMLABEL: where = _T("on label of"); break;
479       case wxLIST_HITTEST_ONITEMRIGHT: where = _T("right on"); break;
480       case wxLIST_HITTEST_TOLEFT: where = _T("to the left of"); break;
481       case wxLIST_HITTEST_TORIGHT: where = _T("to the right of"); break;
482       default: where = _T("not clear exactly where on"); break;
483       }
484
485     //wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
486     //             where.c_str(), item, subitem);
487   }
488
489   void WxGUIBlackBoxList::LogEvent(const wxListEvent& event, const wxChar *eventName)
490   {
491     //wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
492     //            event.GetIndex(), eventName,
493     //              event.GetText().c_str(), event.GetData());
494   }
495
496   wxString WxGUIBlackBoxList::OnGetItemText(long item, long column) const
497   {
498     /*
499       if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS) )
500       {
501       return SMALL_VIRTUAL_VIEW_ITEMS[item][column];
502       }
503       else
504       {
505     */
506     return wxString::Format(_T("Column %ld of item %ld"), column, item);
507     //  }
508   }
509
510   int WxGUIBlackBoxList::OnGetItemColumnImage(long item, long column) const
511   {
512     if (!column)
513       return 0;
514
515     if (!(item %3) && column == 1)
516       return 0;
517
518     return -1;
519   }
520
521   wxListItemAttr *WxGUIBlackBoxList::OnGetItemAttr(long item) const
522   {
523     return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
524   }
525
526
527 #if USE_CONTEXT_MENU
528   void WxGUIBlackBoxList::OnContextMenu(wxContextMenuEvent& event)
529   {
530     wxPoint point = event.GetPosition();
531     // If from keyboard
532     if (point.x == -1 && point.y == -1) {
533       wxSize size = GetSize();
534       point.x = size.x / 2;
535       point.y = size.y / 2;
536     } else {
537       point = ScreenToClient(point);
538     }
539     ShowContextMenu(point);
540   }
541 #endif
542
543   void WxGUIBlackBoxList::ShowContextMenu(const wxPoint& pos)
544   {
545     wxMenu menu;
546   
547     menu.Append(wxID_ABOUT, _T("&About"));
548     menu.AppendSeparator();
549     menu.Append(wxID_EXIT, _T("E&xit"));
550   
551     PopupMenu(&menu, pos.x, pos.y);
552   }
553   //================================================================
554
555
556
557   //================================================================
558   WxGUIBlackBoxInfo::WxGUIBlackBoxInfo(wxWindow* parent)
559     :
560     wxPanel(parent, -1),
561     mDescriptor()
562   {
563     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
564     
565     //    mName = new wxStaticText(this,-1,_T(""));
566     //    sizer->Add(mName,0,wxGROW);
567     mDescription = new wxStaticText(this,-1,_T(""));
568     sizer->Add(mDescription,0,wxGROW);
569     mAuthor = new wxStaticText(this,-1,_T(""));
570     sizer->Add(mAuthor,0,wxGROW);
571     mCategory = new wxStaticText(this,-1,_T(""));
572     sizer->Add(mCategory,0,wxGROW);
573
574
575     wxBoxSizer *inputs = 
576       new wxStaticBoxSizer
577       ( new wxStaticBox(this, wxID_ANY, _T("Inputs")), wxVERTICAL );    
578     mInputList = new wxListCtrl(this, -1,
579                                 wxDefaultPosition,
580                                 wxDefaultSize,
581                                 wxLC_REPORT //wxLC_LIST
582                                 | wxSUNKEN_BORDER);
583     
584     
585     inputs->Add(mInputList,1,wxGROW);
586     sizer->Add(inputs,1,wxGROW);
587
588     wxBoxSizer *outputs = 
589       new wxStaticBoxSizer
590       ( new wxStaticBox(this, wxID_ANY, _T("Outputs")), wxVERTICAL );    
591     mOutputList = new wxListCtrl(this, -1,
592                                  wxDefaultPosition,
593                                  wxDefaultSize,
594                                  wxLC_REPORT //wxLC_LIST
595                                  | wxSUNKEN_BORDER);
596     
597     outputs->Add(mOutputList,1,wxGROW);
598     sizer->Add(outputs,1,wxGROW);
599     
600     
601     SetSizer(sizer);
602     SetAutoLayout(true);
603     Layout();
604   }
605   //================================================================
606
607     
608   //================================================================
609   void WxGUIBlackBoxInfo::UpdateInfo(BlackBoxDescriptor* descr)
610   {
611
612     //    mName->SetLabel(std2wx(descr->GetTypeName()));
613     mDescription->SetLabel(std2wx(descr->GetDescription()));
614     mAuthor->SetLabel(std2wx(descr->GetAuthor()));
615     mCategory->SetLabel(std2wx(descr->GetCategory()));
616     
617     mInputList->ClearAll();
618     mInputList->InsertColumn( 0, _("Name"), 
619                                wxLIST_FORMAT_LEFT, 80  ); 
620     mInputList->InsertColumn( 1, _("Type"), 
621                                wxLIST_FORMAT_LEFT, 100 ); 
622     mInputList->InsertColumn( 2, _("Nature"), 
623                                wxLIST_FORMAT_LEFT, 100  ); 
624     mInputList->InsertColumn( 3, _("Description"), 
625                                wxLIST_FORMAT_LEFT, 500  ); 
626
627     mOutputList->ClearAll();
628     mOutputList->InsertColumn( 0, _("Name"), 
629                                wxLIST_FORMAT_LEFT, 80  ); 
630     mOutputList->InsertColumn( 1, _("Type"), 
631                                wxLIST_FORMAT_LEFT, 100 ); 
632     mOutputList->InsertColumn( 2, _("Nature"), 
633                                wxLIST_FORMAT_LEFT, 100  ); 
634     mOutputList->InsertColumn( 3, _("Description"), 
635                                wxLIST_FORMAT_LEFT, 500  ); 
636     
637
638     std::vector<BlackBoxInputOutputDescriptor*> user_defined;
639     std::vector<BlackBoxInputOutputDescriptor*> ubb_defined;
640     std::vector<BlackBoxInputOutputDescriptor*> wxbb_defined;
641     
642     const BlackBoxDescriptor::InputDescriptorMapType& imap = 
643       descr->GetInputDescriptorMap();
644     BlackBoxDescriptor::InputDescriptorMapType::const_iterator in;
645     for ( in = imap.begin();  in != imap.end(); ++in ) 
646       {
647         int iotype = 0;
648         if (in->second->GetCreatorTypeInfo() == 
649             typeid(AtomicBlackBoxDescriptor))
650           {
651             iotype = 1;
652           }
653         else if (in->second->GetCreatorTypeInfo() == 
654                  typeid(WxBlackBoxDescriptor))
655           {
656             iotype = 2;
657           }
658         if (iotype==0) user_defined.push_back(in->second);
659         else if (iotype==1) ubb_defined.push_back(in->second);
660         else if (iotype==2) wxbb_defined.push_back(in->second);
661         
662       }
663
664     std::vector<BlackBoxInputOutputDescriptor*>::iterator hi;
665     for (hi=user_defined.begin();hi!=user_defined.end();++hi) 
666       {
667         InsertInputOutput(mInputList,*hi);
668       }
669     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) 
670       {
671         InsertInputOutput(mInputList,*hi);
672       }
673     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) 
674       {
675         InsertInputOutput(mInputList,*hi);
676       }
677     
678     // Outputs
679     user_defined.clear();
680     ubb_defined.clear();
681     wxbb_defined.clear();
682     const BlackBoxDescriptor::OutputDescriptorMapType& omap = 
683       descr->GetOutputDescriptorMap();
684     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator out;
685     for ( out = omap.begin();  out != omap.end(); ++out ) 
686       {
687         int iotype = 0;
688         if (out->second->GetCreatorTypeInfo() == 
689             typeid(AtomicBlackBoxDescriptor))
690           {
691             iotype = 1;
692           }
693         else if (out->second->GetCreatorTypeInfo() == 
694                  typeid(WxBlackBoxDescriptor))
695           {
696             iotype = 2;
697           }
698        
699         if (iotype==0) user_defined.push_back(out->second);
700         else if (iotype==1) ubb_defined.push_back(out->second);
701         else if (iotype==2) wxbb_defined.push_back(out->second);
702
703       }
704     for (hi=user_defined.begin();hi!=user_defined.end();++hi) 
705       {
706         InsertInputOutput(mOutputList,*hi);
707       }
708     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) 
709       {
710         InsertInputOutput(mOutputList,*hi);
711       }
712     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) 
713       {
714         InsertInputOutput(mOutputList,*hi);
715       }
716   }
717   //================================================================
718
719
720   //================================================================
721   void WxGUIBlackBoxInfo::InsertInputOutput(wxListCtrl* l,
722                                             BlackBoxInputOutputDescriptor* d)
723   {
724     wxListItem kNewItem;
725     kNewItem.SetAlign(wxLIST_FORMAT_LEFT);
726     int nID = l->GetItemCount();
727     //    std::cout << nID << std::endl;
728     kNewItem.SetId(nID);
729     //    kNewItem.SetMask(wxLIST_MASK_DATA);
730     //    kNewItem.SetData(d);  
731     l->InsertItem(kNewItem);
732     l->SetItem(nID, 0, std2wx(d->GetName()) );
733     l->SetItem(nID, 1, std2wx(d->GetTypeName()) );
734     l->SetItem(nID, 2, std2wx(d->GetNature()) );
735     l->SetItem(nID, 3, std2wx(d->GetDescription()) );
736   }
737   //================================================================
738
739   //================================================================
740   WxGUIBlackBoxInfo::~WxGUIBlackBoxInfo()
741   {
742   }
743   //================================================================
744
745
746  enum 
747     {
748       id_f1,
749       id_f2,
750       id_f3,
751       id_f4,
752       id_f5,
753       id_f6,
754       id_f7,
755       id_f8,
756       id_fc1,
757       id_fc2,
758       id_fc3
759     };
760
761
762   //================================================================
763   WxGUIPackageBrowser2::WxGUIPackageBrowser2( wxWindow *parent,
764                                               WxGUIPackageBrowser2User* user )
765     : wxPanel(parent, -1),
766       mUser(user),
767       mInterpreter()
768   {
769     m_mgr.SetManagedWindow(this);
770
771     //    wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
772     mBoxList = new WxGUIBlackBoxList(this, LIST_CTRL,
773                                      wxDefaultPosition,
774                                      wxDefaultSize,
775                                      wxLC_REPORT //wxLC_LIST
776                                      | wxSUNKEN_BORDER);
777     mBoxList->SetUser(this);
778     mBoxList->SetBackgroundColour(*wxWHITE);
779
780     m_mgr.AddPane(mBoxList,
781                   wxAuiPaneInfo().Name(wxT("BoxList"))
782                   .Caption(wxT("Black Boxes"))
783                   .MinimizeButton(true)
784                   .MaximizeButton(true)
785                   .Center()
786                   .MinSize(wxSize(100,100))
787                   );     
788     //    sizer->Add(mBoxList,5,wxGROW);
789
790     mBoxInfo = new WxGUIBlackBoxInfo(this);
791     //    sizer->Add(mBoxInfo,3,wxGROW);
792
793     m_mgr.AddPane(mBoxInfo,
794                   wxAuiPaneInfo().Name(wxT("Box"))
795                   .Caption(wxT(""))
796                   .MinimizeButton(true)
797                   .MaximizeButton(true)
798                   .Bottom()
799                   .MinSize(wxSize(100,300))
800                   );     
801     
802
803     wxPanel* filters = new wxPanel(this,-1);
804     wxBoxSizer *fsizer = new wxBoxSizer(wxVERTICAL );    
805     wxBoxSizer *fpack = 
806       new wxStaticBoxSizer
807       ( new wxStaticBox(filters, wxID_ANY, _T("Package")), wxHORIZONTAL );    
808     mPackageFilter = new wxTextCtrl(filters,id_f1,_T(""),
809                                     wxDefaultPosition,
810                                     wxDefaultSize,
811                                     wxTE_PROCESS_ENTER);
812     fpack->Add(mPackageFilter,1,wxGROW);
813     fsizer->Add(fpack,0,wxGROW);
814     wxBoxSizer *fname = 
815       new wxStaticBoxSizer
816       ( new wxStaticBox(filters, wxID_ANY, _T("Name")), wxHORIZONTAL );    
817     mNameFilter = new wxTextCtrl(filters,id_f2,_T(""),
818                                     wxDefaultPosition,
819                                     wxDefaultSize,
820                                     wxTE_PROCESS_ENTER);
821     fname->Add(mNameFilter,1,wxGROW);
822     fsizer->Add(fname,0,wxGROW);
823     wxBoxSizer *fdescr = 
824       new wxStaticBoxSizer
825       ( new wxStaticBox(filters, wxID_ANY, _T("Description")), wxHORIZONTAL );
826     mDescriptionFilter = new wxTextCtrl(filters,id_f3,_T(""),
827                                     wxDefaultPosition,
828                                     wxDefaultSize,
829                                     wxTE_PROCESS_ENTER);
830     fdescr->Add(mDescriptionFilter,1,wxGROW);
831     fsizer->Add(fdescr,0,wxGROW);
832     wxBoxSizer *fcat = 
833       new wxStaticBoxSizer
834       ( new wxStaticBox(filters, wxID_ANY, _T("Category")), wxHORIZONTAL );    
835     mCategoryFilter = new wxTextCtrl(filters,id_f4,_T(""),
836                                     wxDefaultPosition,
837                                     wxDefaultSize,
838                                     wxTE_PROCESS_ENTER);
839     fcat->Add(mCategoryFilter,1,wxGROW);
840     fsizer->Add(fcat,0,wxGROW);
841     wxBoxSizer *fintype = 
842       new wxStaticBoxSizer
843       ( new wxStaticBox(filters, wxID_ANY, _T("Input type")), wxHORIZONTAL );
844     mInputTypeFilter = new wxTextCtrl(filters,id_f5,_T(""),
845                                     wxDefaultPosition,
846                                     wxDefaultSize,
847                                     wxTE_PROCESS_ENTER);
848     fintype->Add(mInputTypeFilter,1,wxGROW);
849     fsizer->Add(fintype,0,wxGROW);
850     wxBoxSizer *fouttype = 
851       new wxStaticBoxSizer
852       ( new wxStaticBox(filters, wxID_ANY, _T("Output type")), wxHORIZONTAL );
853     mOutputTypeFilter = new wxTextCtrl(filters,id_f6,_T(""),
854                                     wxDefaultPosition,
855                                     wxDefaultSize,
856                                     wxTE_PROCESS_ENTER);
857     fouttype->Add(mOutputTypeFilter,1,wxGROW);
858     fsizer->Add(fouttype,0,wxGROW);
859     wxBoxSizer *finnat = 
860       new wxStaticBoxSizer
861       ( new wxStaticBox(filters, wxID_ANY, _T("Input nature")),wxHORIZONTAL ); 
862     mInputNatureFilter = new wxTextCtrl(filters,id_f7,_T(""),
863                                         wxDefaultPosition,
864                                         wxDefaultSize,
865                                         wxTE_PROCESS_ENTER);
866     finnat->Add(mInputNatureFilter,1,wxGROW);
867     fsizer->Add(finnat,0,wxGROW);
868     wxBoxSizer *foutnat = 
869       new wxStaticBoxSizer
870       ( new wxStaticBox(filters, wxID_ANY,_T("Output nature")),wxHORIZONTAL ); 
871     mOutputNatureFilter = new wxTextCtrl(filters,id_f8,_T(""),
872                                         wxDefaultPosition,
873                                         wxDefaultSize,
874                                         wxTE_PROCESS_ENTER);
875     foutnat->Add(mOutputNatureFilter,1,wxGROW);
876     fsizer->Add(foutnat,0,wxGROW);
877
878
879     mShowWidgetsFilter = new wxCheckBox(filters,id_fc1,_T("Show widgets"));
880     mShowWidgetsFilter->SetValue(true);
881     fsizer->Add(mShowWidgetsFilter,0,wxGROW);
882
883     mShowAdaptorsFilter = new wxCheckBox(filters,id_fc2,_T("Show adaptors"));
884     mShowAdaptorsFilter->SetValue(false);
885     fsizer->Add(mShowAdaptorsFilter,0,wxGROW);
886
887     mShowGUIsFilter = new wxCheckBox(filters,id_fc3,_T("Show GUIs"));
888     mShowGUIsFilter->SetValue(false);
889     fsizer->Add(mShowGUIsFilter,0,wxGROW);
890
891     filters->SetSizer(fsizer);
892
893     m_mgr.AddPane(filters,
894                   wxAuiPaneInfo().Name(wxT("Filter"))
895                   .Caption(wxT("Filter"))
896                   .MinimizeButton(true)
897                   .MaximizeButton(true)
898                   .Right()
899                   .MinSize(wxSize(100,100)) );
900
901     
902
903     //    sizer->Add(fsizer,1,wxGROW);
904     m_mgr.Update();
905
906     //    SetSizer(sizer);
907     SetAutoLayout(true);
908     Layout();
909     
910   }
911   //================================================================
912
913   //================================================================
914   void WxGUIPackageBrowser2::OnFilter(wxCommandEvent&)
915   {
916     RebuildList();
917   }
918   //================================================================
919
920   //================================================================
921   // User callback when a box is selected in the list 
922   void WxGUIPackageBrowser2::WxGUIBlackBoxListUserOnSelected
923   ( BlackBoxDescriptor* d)
924   {
925     mBoxInfo->UpdateInfo(d);
926     std::string title = d->GetPackage()->GetName()+"::"+d->GetTypeName();
927     m_mgr.GetPane(mBoxInfo).Caption(std2wx(title));
928     m_mgr.Update();
929   }
930   
931   //================================================================
932
933   //================================================================
934   WxGUIPackageBrowser2::~WxGUIPackageBrowser2()
935   {
936     mFactory.reset();
937     mInterpreter.reset();
938     //    Object::PrintObjectListInfo("");
939     m_mgr.UnInit();
940   }
941   //================================================================
942
943   //================================================================
944   void WxGUIPackageBrowser2::IncludeAll()
945   {
946     if (!mInterpreter) mInterpreter =bbtk::Interpreter::New();
947     mInterpreter->SetCommandLine(true);
948     std::stringstream* buf = new std::stringstream;
949     *buf << "message max 0" << std::endl; 
950     *buf << "include *" << std::endl;
951     mInterpreter->InterpretBuffer(buf);
952
953     Factory::Pointer F = mInterpreter->GetExecuter()->GetFactory();
954     BuildFromFactory(F);
955   }
956   //================================================================
957
958   //================================================================
959   void WxGUIPackageBrowser2::BuildFromFactory(Factory::Pointer F)
960   {
961     mFactory = F;
962     RebuildList();
963   }  
964   //================================================================
965
966   //================================================================
967   void WxGUIPackageBrowser2::RebuildList()
968   {
969     mBoxList->Hide();
970     mBoxList->ClearAll();    
971     
972     const Factory::PackageMapType& M = mFactory->GetPackageMap();
973     Factory::PackageMapType::const_iterator i;
974     for (i=M.begin();i!=M.end();++i)
975       {
976         Package::Pointer P = i->second;
977         if (P->GetName() == "user") continue;
978         
979         Package::BlackBoxMapType::iterator j;
980         for (j=P->GetBlackBoxMap().begin();
981              j!=P->GetBlackBoxMap().end();
982              ++j)
983           {
984             //      std::cout << "Insert "<<j->second->GetTypeName()<<std::endl;
985             if (IsVisible(j->second))
986               mBoxList->Insert(j->second);
987           }
988       }
989     mBoxList->Show();
990   }
991   //================================================================
992
993   //================================================================
994   bool WxGUIPackageBrowser2::IsVisible(BlackBoxDescriptor::Pointer d)
995   {
996     //    std::cout << "   '" << wx2std(mPackageFilter->GetValue())<<"'"<<std::endl;
997     //    std::cout << "vs '" << d->GetPackage()->GetName() << "'"<<std::endl;
998     if ((!mShowAdaptorsFilter->IsChecked())&&
999         ((d->GetKind()==BlackBoxDescriptor::ADAPTOR)||
1000          (d->GetKind()==BlackBoxDescriptor::DEFAULT_ADAPTOR)))
1001       return false;
1002     if ((!mShowGUIsFilter->IsChecked())&&
1003         ((d->GetKind()==BlackBoxDescriptor::GUI)||
1004          (d->GetKind()==BlackBoxDescriptor::DEFAULT_GUI)))
1005       return false;
1006     if (d->GetPackage()->GetName().find( wx2std(mPackageFilter->GetValue()) ) 
1007         == std::string::npos ) return false;
1008    if (d->GetTypeName().find( wx2std(mNameFilter->GetValue()) ) 
1009         == std::string::npos ) return false;
1010    if (d->GetDescription().find( wx2std(mDescriptionFilter->GetValue()) ) 
1011         == std::string::npos ) return false;
1012    if (d->GetCategory().find( wx2std(mCategoryFilter->GetValue()) ) 
1013         == std::string::npos ) return false;
1014    if (!mShowWidgetsFilter->IsChecked())
1015      {
1016        bool found = false;
1017        const BlackBoxDescriptor::OutputDescriptorMapType& imap = 
1018          d->GetOutputDescriptorMap();
1019        BlackBoxDescriptor::OutputDescriptorMapType::const_iterator in;
1020        for ( in = imap.begin();  in != imap.end(); ++in ) 
1021          {
1022            if (in->second->GetName() == "Widget" )
1023              {
1024                found = true;
1025                break;
1026              }
1027          }
1028        if (found) return false;
1029      }
1030    if (mInputTypeFilter->GetValue().size()>0)
1031      {
1032        std::string s = wx2std(mInputTypeFilter->GetValue());
1033        bool found = false;
1034        const BlackBoxDescriptor::InputDescriptorMapType& imap = 
1035          d->GetInputDescriptorMap();
1036        BlackBoxDescriptor::InputDescriptorMapType::const_iterator in;
1037        for ( in = imap.begin();  in != imap.end(); ++in ) 
1038          {
1039            if (in->second->GetTypeName().find(s)!=std::string::npos)
1040              {
1041                found = true;
1042                break;
1043              }
1044          }
1045        if (!found) return false;
1046      }
1047    if (mOutputTypeFilter->GetValue().size()>0)
1048      {
1049        std::string s = wx2std(mOutputTypeFilter->GetValue());
1050        bool found = false;
1051        const BlackBoxDescriptor::OutputDescriptorMapType& imap = 
1052          d->GetOutputDescriptorMap();
1053        BlackBoxDescriptor::OutputDescriptorMapType::const_iterator in;
1054        for ( in = imap.begin();  in != imap.end(); ++in ) 
1055          {
1056            if (in->second->GetTypeName().find(s)!=std::string::npos)
1057              {
1058                found = true;
1059                break;
1060              }
1061          }
1062        if (!found) return false;
1063      }
1064    if (mInputNatureFilter->GetValue().size()>0)
1065      {
1066        std::string s = wx2std(mInputNatureFilter->GetValue());
1067        bool found = false;
1068        const BlackBoxDescriptor::InputDescriptorMapType& imap = 
1069          d->GetInputDescriptorMap();
1070        BlackBoxDescriptor::InputDescriptorMapType::const_iterator in;
1071        for ( in = imap.begin();  in != imap.end(); ++in ) 
1072          {
1073            if (in->second->GetNature().find(s)!=std::string::npos)
1074              {
1075                found = true;
1076                break;
1077              }
1078          }
1079        if (!found) return false;
1080      }
1081    if (mOutputNatureFilter->GetValue().size()>0)
1082      {
1083        std::string s = wx2std(mOutputNatureFilter->GetValue());
1084        bool found = false;
1085        const BlackBoxDescriptor::OutputDescriptorMapType& imap = 
1086          d->GetOutputDescriptorMap();
1087        BlackBoxDescriptor::OutputDescriptorMapType::const_iterator in;
1088        for ( in = imap.begin();  in != imap.end(); ++in ) 
1089          {
1090            if (in->second->GetNature().find(s)!=std::string::npos)
1091              {
1092                found = true;
1093                break;
1094              }
1095          }
1096        if (!found) return false;
1097      }
1098
1099     return true;
1100   }
1101   //========================================================================
1102
1103   //========================================================================
1104   BEGIN_EVENT_TABLE(WxGUIPackageBrowser2, wxPanel)
1105     EVT_TEXT_ENTER(id_f1, WxGUIPackageBrowser2::OnFilter )
1106     EVT_TEXT_ENTER(id_f2, WxGUIPackageBrowser2::OnFilter )
1107     EVT_TEXT_ENTER(id_f3, WxGUIPackageBrowser2::OnFilter )
1108     EVT_TEXT_ENTER(id_f4, WxGUIPackageBrowser2::OnFilter )
1109     EVT_TEXT_ENTER(id_f5, WxGUIPackageBrowser2::OnFilter )
1110     EVT_TEXT_ENTER(id_f6, WxGUIPackageBrowser2::OnFilter )
1111     EVT_TEXT_ENTER(id_f7, WxGUIPackageBrowser2::OnFilter )
1112     EVT_TEXT_ENTER(id_f8, WxGUIPackageBrowser2::OnFilter )
1113     EVT_CHECKBOX(  id_fc1, WxGUIPackageBrowser2::OnFilter )
1114     EVT_CHECKBOX(  id_fc2, WxGUIPackageBrowser2::OnFilter )
1115     EVT_CHECKBOX(  id_fc3, WxGUIPackageBrowser2::OnFilter )
1116     END_EVENT_TABLE()
1117   //========================================================================
1118
1119
1120
1121   //================================================================
1122   WxGUIPackageBrowser2Window::WxGUIPackageBrowser2Window( wxWindow *parent, 
1123                                                             wxString title, 
1124                                                             wxSize size)
1125       : wxFrame((wxFrame *)parent, -1, title, wxDefaultPosition, size)
1126   {     
1127     
1128     
1129     wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
1130
1131     mBrowser = new WxGUIPackageBrowser2(this);
1132     mBrowser->IncludeAll();
1133     sizer->Add(mBrowser,1,wxGROW);
1134     
1135     //    WxGUICommand* com = new WxGUICommand(this,this);
1136     //    sizer->Add(com);
1137     
1138     SetSizer(sizer);
1139
1140      // Creates the parent window of all bbtk windows as a child of this
1141 //    Wx::CreateTopWindow(this);
1142     // Add the method OnWxSignal as a Wx::Signal observer 
1143     //bbtkAddWxSignalObserver(WxGUIPackageBrowser2Window::OnWxSignal);
1144  
1145     SetAutoLayout(true);
1146     Layout();
1147   }
1148   //================================================================
1149
1150   //================================================================
1151   WxGUIPackageBrowser2Window::~WxGUIPackageBrowser2Window()
1152   {
1153   }
1154   //================================================================
1155
1156 } // namespace bbtk
1157
1158
1159 #endif //_USE_WXWIDGETS_