]> 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         } // if OK
125 }
126  
127 void ButtonSelectFilesDirectoryWidget::OnButton( wxEvent& )
128 {
129           if (mBox->bbGetInputType()==0)
130           {
131                 SelectFiles();
132           } else if (mBox->bbGetInputType()==1){
133                 SelectDirectory();
134           }
135 }
136
137   //--------------------------------------------------------------------------  
138   void ButtonSelectFilesDirectoryWidget::SetLabel(wxString title)
139   {
140     mwxButton->SetLabel(title);
141   }
142
143
144
145
146
147
148 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,ButtonSelectFilesDirectory)
149 BBTK_BLACK_BOX_IMPLEMENTATION(ButtonSelectFilesDirectory,bbtk::WxBlackBox);
150 //===== 
151 // 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)
152 //===== 
153 void ButtonSelectFilesDirectory::Process()
154 {
155
156     ButtonSelectFilesDirectoryWidget* w = (ButtonSelectFilesDirectoryWidget*)bbGetOutputWidget();
157     if (w) 
158         {
159                 UpdateLabel();
160         }
161   
162 }
163 //===== 
164 // 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)
165 //===== 
166 void ButtonSelectFilesDirectory::CreateWidget(wxWindow* parent)
167 {
168
169     bbSetOutputWidget
170       ( new ButtonSelectFilesDirectoryWidget ( this, //bbGetWxParent(), 
171                                                                  parent,
172                                                                  bbtk::std2wx(bbGetInputLabel()) ) );
173
174   
175 }
176 //===== 
177 // 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)
178 //===== 
179 void ButtonSelectFilesDirectory::bbUserSetDefaultValues()
180 {
181         bbSetInputType(0);
182         bbSetInputOpenSave("Open");
183 }
184 //===== 
185 // 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)
186 //===== 
187 void ButtonSelectFilesDirectory::bbUserInitializeProcessing()
188 {
189
190 }
191 //===== 
192 // 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)
193 //===== 
194 void ButtonSelectFilesDirectory::bbUserFinalizeProcessing()
195 {
196
197 }
198
199 void ButtonSelectFilesDirectory::UpdateLabel()
200 {
201     ButtonSelectFilesDirectoryWidget* wxwidget = (ButtonSelectFilesDirectoryWidget*)bbGetOutputWidget();
202     wxwidget->SetLabel( bbtk::std2wx( bbGetInputLabel() ) );
203 }   
204
205
206
207 }// EO namespace bbwx
208
209