]> Creatis software - creaContours.git/blob - lib/Interface_Icons_NDimensions/interfMenuBar.h
Some fixes for wxString vs std::string pb.
[creaContours.git] / lib / Interface_Icons_NDimensions / interfMenuBar.h
1 #ifndef __interfMENUBARH__
2 #define __interfMENUBARH__
3
4 #include <wx/wx.h>
5 #include <vector>
6 #include <wx/image.h>
7 #include <wx/bitmap.h>
8 #include <wx/bmpbuttn.h>
9 #include <wx/stattext.h>
10 #include <wx/sizer.h>
11
12 //#include "interfMainPanel.h"
13 #include "wxContourEventHandler.h"
14
15
16 class interfMenuBar :
17         public wxPanel
18 {
19 public:
20         interfMenuBar(wxWindow * parent, int sizex, int sizey)
21                 : wxPanel(parent, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_SUNKEN){
22                 //this initialice all the handlers to load any type of image, in this case the png handler is needed
23                 wxInitAllImageHandlers();
24                 
25                                 
26         }
27         //~interfMenuBar();
28
29         virtual void initButtons(wxEvtHandler* evtHandler) = 0;
30
31         /**
32         ** This method add a series of button in a line, it uses the method getbutton to create the button
33         ** and static text to create the text, it also initialice the flexgridsizer to add the buttons and
34         ** the text into the panel
35         ** @param vector path, this parameter contains the path of the images to include in the path
36         ** @param vector nom, this parameter contains the names corresponding to each path of image given
37         **/
38     virtual void addButtons(std::vector<std::string> vpath, std::vector<std::string> vnom){
39                 
40                 int sizex = 80;
41                 int sizey = 80;
42         
43                 flexsizer = new wxFlexGridSizer(2,vpath.size(),2,2);    
44                 this->SetSizer(flexsizer, true);
45                 this->SetAutoLayout( true );
46                 
47                 //first row of the sizer, the buttons are being added
48                 for(int i = 0; i < vpath.size();i++){
49                         std::string p = vpath[i];
50                         wxBitmapButton* bitmapbutton = this->getButton(p, sizex, sizey);
51                         flexsizer->Add(bitmapbutton,wxFIXED_MINSIZE);
52                         
53                 }
54                 //second row of the sizer, the names are being added
55                 
56                 for(int i = 0; i < vnom.size(); i++){
57                         //sizex = vectbutton[i]->GetSize().GetWidth();
58                         //const std::string n = vnom[i];
59                         wxString newstring1(vnom[i].c_str() , wxConvUTF8);
60                         //vectbutton[i]->SetToolTip(wxString(n));
61                         vectbutton[i]->SetToolTip(newstring1);
62                         
63                         //wxStaticText* statictext = getText(n, sizex, 15);
64                         //flexsizer->Add(statictext, wxEXPAND |wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTRE_HORIZONTAL|wxSHAPED);
65                 }
66                 this->Layout();
67         }
68
69         /**
70         **  This method creates the button with the given image and returns it
71         ** @param string imgpath is the string containing the path to the image of the button being created
72         ** @param int x indicates the x coordinate where the button should be located
73         ** @param int y indicates the y coordinate where the button should be located
74         **/
75
76         virtual wxBitmapButton* getButton(std::string imgpath, int sizex, int sizey){
77                 vectimgpath.push_back(imgpath);
78                                 
79                 wxString newstring1( imgpath.c_str() , wxConvUTF8);
80                 //wxBitmap* bitmap = new wxBitmap(imgpath,  wxBITMAP_TYPE_PNG);
81                 wxBitmap* bitmap = new wxBitmap(newstring1,  wxBITMAP_TYPE_PNG);
82                 
83                 //wxSize(sizex,sizey)
84                 wxBitmapButton* bitmapbutton = new wxBitmapButton(this, -1, *bitmap, wxDefaultPosition, wxDefaultSize,
85                                                                                         wxBU_AUTODRAW, wxDefaultValidator, _T(""));
86
87                 vectbutton.push_back(bitmapbutton);
88                 return bitmapbutton;
89         }
90
91         /**
92         **      This method create the static text with the given string and returns it, it also push it back into 
93         **      the vector nom
94         **      @param string nom  text wich will be added to the statictext
95         **      @param int sizex   size of the text
96         **      @param int sizey   size of the text
97         **/
98         virtual wxStaticText* getText(std::string nom, int sizex, int sizey){
99                 vectnom.push_back(nom);
100                 wxString newstring1(nom.c_str() , wxConvUTF8);
101                 return new wxStaticText(this, -1, newstring1 /*wxString(nom)*/, wxDefaultPosition, 
102                                                                 //wxSize(sizex,sizey), wxALIGN_CENTRE, wxString(nom));
103                                                                 wxSize(sizex,sizey), wxALIGN_CENTRE, newstring1);
104         }
105     /**
106         **      Sets the vector funcion, this vector must contain a function for each button created
107         **      they must be ordered in the same way as the vector that contains the image path
108         **      and name of the button. See addButton(<vect>, <vect>)
109         **/
110         virtual void setVectorFunction(std::vector<wxObjectEventFunction> vectf){
111                 for(int i = 0; i < vectf.size();i++){
112                         vectfunct.push_back(vectf[i]);
113                 }               
114         }
115
116         /**
117         **      This method connect the events seted in the vector vectfunct
118         **      each function must have a button to relate with, see function setVectorFunction()
119         **/
120         virtual void connectEvents(wxEvtHandler* evtHandler){   
121
122                 for(int i = 0; i < vectbutton.size();i++){
123                         Connect(vectbutton[i]->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, vectfunct[i],NULL,evtHandler);
124                 }
125         }
126
127         /**
128         **      This method connect the events seted in the vector vectfunct
129         **      each function must have a button to relate with, see function setVectorFunction()
130         **/
131         virtual void connectEvents(){   
132
133                 for(int i = 0; i < vectbutton.size();i++){
134                         Connect(vectbutton[i]->GetId(), wxEVT_COMMAND_BUTTON_CLICKED, vectfunct[i],NULL,vecthand[i]);
135                 }
136         }
137
138         /**
139         **      Sets the name of the button, this is used to relate the name of the button to a specific command
140         **      @params int i   index in the array of buttons use fuction setVectorFuncion(<vect>) to set the vector
141         **                      first
142         **      @params const char      name of the button see wxContour_ActionCommandsID.h to view the commands
143         **/
144         virtual void setButtonName(int i, const char c){
145                 if(i < vectbutton.size()){
146                         vectbutton[i]->SetName(c);
147                 }
148         }
149
150         /**
151         **      Responds to the events of the buttons, when the same panel is responsible for it. It gets the name
152         **      of the button corresponding to the method that has to be executed,
153         **      it uses attribute eventHandler to call the methods define by the application.
154         **      see setEventHandler(wxEventHandler*)
155         **/
156         void onActionButtonPressed( wxCommandEvent& event )
157         {
158                 if(eventHandler!=NULL)
159                 {
160                         std::string theStr = std::string( ((wxButton *)event.GetEventObject())->GetName().ToAscii());
161                         const char * toolCommand = theStr.c_str();
162                         event.SetId( GetId() );
163                         event.SetEventObject( this );
164                         event.SetClientData( (void *) toolCommand);
165                         eventHandler->ProcessEvent( event );
166                 }
167         }
168
169         virtual void setEventHandlers(std::vector<wxEvtHandler*> hand){
170
171                 for(int i = 0; i < hand.size();i++){
172                         vecthand.push_back(hand[i]);
173                 }
174         }
175         
176 private:
177         std::vector<std::string> vectimgpath;
178         std::vector<std::string> vectnom;
179         
180         std::vector<wxButton*> vectbutton;
181         std::vector<wxObjectEventFunction> vectfunct;
182         std::vector<wxEvtHandler*> vecthand;
183
184         wxFlexGridSizer* flexsizer;
185
186 protected:
187         wxEvtHandler* eventHandler;
188         std::string datadir;
189
190         
191
192 };
193
194 #endif