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