]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxButtonSelectFilesDirectory.cxx
#3475 ButtonSelectFilesDirectory box wx
[bbtk.git] / packages / wx / src / bbwxButtonSelectFilesDirectory.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbwxButtonSelectFilesDirectory.h"
5 #include "bbwxPackage.h"
6
7 #include <wx/filedlg.h>
8 #include <wx/dirdlg.h>
9
10
11 namespace bbwx
12 {
13
14
15    //--------------------------------------------------------------------------
16   class ButtonSelectFilesDirectoryWidget : public wxPanel
17   {
18   public:
19     ButtonSelectFilesDirectoryWidget(ButtonSelectFilesDirectory* box, wxWindow *parent, 
20                         wxString title);
21     ~ButtonSelectFilesDirectoryWidget();
22     void OnButton( wxEvent& );
23     void SetLabel(wxString title);
24     void SetColour(wxColour color);
25         
26   private:
27         void SelectDirectory();
28         void SelectFiles();
29   
30     ButtonSelectFilesDirectory  *mBox;
31     wxButton                                    *mwxButton;
32   }; 
33    
34   //--------------------------------------------------------------------------
35   //--------------------------------------------------------------------------
36
37   ButtonSelectFilesDirectoryWidget::ButtonSelectFilesDirectoryWidget(ButtonSelectFilesDirectory* box,
38                                            wxWindow *parent, 
39                                            wxString title )
40     : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
41       mBox(box)
42   { 
43     wxPanel *panel      = this;
44     mwxButton = new wxButton( panel, -1, title);
45     Connect( mwxButton->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , 
46              (wxObjectEventFunction) 
47              (void (wxPanel::*)(wxEvent&))
48              &ButtonSelectFilesDirectoryWidget::OnButton ); 
49     wxFlexGridSizer *sizer      = new wxFlexGridSizer(1);
50     sizer -> Add( mwxButton,1,wxGROW | wxALL,10 ); 
51     sizer       -> AddGrowableCol(0);
52     panel       -> SetSizer(sizer);
53     panel       -> SetAutoLayout(true);
54     panel       -> Layout();
55   }
56   
57   ButtonSelectFilesDirectoryWidget::~ButtonSelectFilesDirectoryWidget()
58   {
59   }
60   
61   
62   void ButtonSelectFilesDirectoryWidget::SelectFiles(  )
63   {
64   
65         long style;
66     if (mBox->bbGetInputOpenSave()=="Save") 
67     {
68 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
69 #if wxMAJOR_VERSION <= 2
70         style = wxSAVE | wxOVERWRITE_PROMPT;
71 #else
72         style = wxFD_SAVE | wxFD_OVERWRITE_PROMPT;
73 #endif
74       }  else  {
75 //EED 2017-09-16 Migration wxWidgets 2.8 to 3.0
76 #if wxMAJOR_VERSION <= 2
77         style = wxOPEN | wxFILE_MUST_EXIST;
78 #else
79         style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
80 #endif
81       }
82     
83     std::string wc(mBox->bbGetInputWildcard());
84     if (wc=="") 
85     {
86                 wc = "*";  // Any file must be shown
87     }
88     
89     wxFileDialog* FD = new wxFileDialog( 0, 
90                                          bbtk::std2wx(mBox->bbGetInputMessage()),
91                                          bbtk::std2wx(mBox->bbGetInputDefaultDir()),
92                                          bbtk::std2wx(mBox->bbGetInputDefaultFile()),
93                                          bbtk::std2wx(wc),
94                                          style,
95                                          wxDefaultPosition);
96 //EED
97
98     int result_FD = FD->ShowModal();
99
100         // This line is need it by windows //EED
101         FD->SetReturnCode( result_FD );
102
103     if (FD->GetReturnCode()==wxID_OK)
104     {
105                 mBox->bbSetOutputOut( bbtk::wx2std (FD->GetPath()) );
106                 mBox->bbSignalOutputModification(std::string("Out"));
107     } else { 
108                 mBox->bbSetOutputOut(" ");
109                 mBox->bbSignalOutputModification(std::string("Out"));
110     }
111   }   
112   
113 void ButtonSelectFilesDirectoryWidget:: SelectDirectory()
114 {
115         wxDirDialog* FD = new wxDirDialog( 0, 
116                 bbtk::std2wx(mBox->bbGetInputMessage()),
117                 bbtk::std2wx(mBox->bbGetInputDefaultDir()));
118                 
119         if (FD->ShowModal()==wxID_OK)
120         {
121                 mBox->bbSetOutputOut( bbtk::wx2std (FD->GetPath()) );
122                 mBox->bbSignalOutputModification(std::string("Out"));
123                 mBox->bbSetInputDefaultDir( bbtk::wx2std (FD->GetPath()) );
124         } else {  
125                 mBox->bbSetOutputOut("");
126                 mBox->bbSignalOutputModification(std::string("Out"));
127                 mBox->bbSetInputDefaultDir("");
128         }
129
130 }
131  
132 void ButtonSelectFilesDirectoryWidget::OnButton( wxEvent& )
133 {
134   printf("EED ButtonSelectFilesDirectoryWidget::OnButton Start\n");
135           if (mBox->bbGetInputType()==0)
136           {
137                 SelectFiles();
138           } else if (mBox->bbGetInputType()==1){
139                 SelectDirectory();
140           }
141           
142   printf("EED ButtonSelectFilesDirectoryWidget::OnButton End\n");
143           
144 }
145
146   //--------------------------------------------------------------------------  
147   void ButtonSelectFilesDirectoryWidget::SetLabel(wxString title)
148   {
149     mwxButton->SetLabel(title);
150   }
151
152
153
154
155
156
157 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,ButtonSelectFilesDirectory)
158 BBTK_BLACK_BOX_IMPLEMENTATION(ButtonSelectFilesDirectory,bbtk::WxBlackBox);
159 //===== 
160 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
161 //===== 
162 void ButtonSelectFilesDirectory::Process()
163 {
164
165     ButtonSelectFilesDirectoryWidget* w = (ButtonSelectFilesDirectoryWidget*)bbGetOutputWidget();
166     if (w) 
167         {
168                 UpdateLabel();
169         }
170   
171 }
172 //===== 
173 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
174 //===== 
175 void ButtonSelectFilesDirectory::CreateWidget(wxWindow* parent)
176 {
177
178     bbSetOutputWidget
179       ( new ButtonSelectFilesDirectoryWidget ( this, //bbGetWxParent(), 
180                                                                  parent,
181                                                                  bbtk::std2wx(bbGetInputLabel()) ) );
182
183   
184 }
185 //===== 
186 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
187 //===== 
188 void ButtonSelectFilesDirectory::bbUserSetDefaultValues()
189 {
190         bbSetInputType(0);
191         bbSetInputOpenSave("Open");
192 }
193 //===== 
194 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
195 //===== 
196 void ButtonSelectFilesDirectory::bbUserInitializeProcessing()
197 {
198
199 }
200 //===== 
201 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
202 //===== 
203 void ButtonSelectFilesDirectory::bbUserFinalizeProcessing()
204 {
205
206 }
207
208 void ButtonSelectFilesDirectory::UpdateLabel()
209 {
210     ButtonSelectFilesDirectoryWidget* wxwidget = (ButtonSelectFilesDirectoryWidget*)bbGetOutputWidget();
211     wxwidget->SetLabel( bbtk::std2wx( bbGetInputLabel() ) );
212 }   
213
214
215
216 }// EO namespace bbwx
217
218