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