core/vgui/wx/wxSliderPanel.cxx
Go to the documentation of this file.
00001 // This is core/vgui/wx/wxSliderPanel.cxx
00002 #include "wxSliderPanel.h"
00003 //:
00004 // \file
00005 
00006 //: For compilers that support precompilation, includes "wx/wx.h".
00007 #include <wx/wxprec.h>
00008 
00009 #ifdef __BORLANDC__
00010 #pragma hdrstop
00011 #endif
00012 
00013 #ifndef WX_PRECOMP
00014 #include <wx/wx.h>
00015 #endif
00016 
00017 #include <vcl_cassert.h>
00018 #include <vgui/vgui_message.h>
00019 
00020 
00021 IMPLEMENT_DYNAMIC_CLASS( wxSliderPanel, wxScrolledWindow )
00022 
00023 
00024 //: Event Table
00025 BEGIN_EVENT_TABLE( wxSliderPanel, wxPanel )
00026   EVT_SCROLL_THUMBTRACK( wxSliderPanel::OnSliderTrack )
00027   EVT_SCROLL_PAGEDOWN( wxSliderPanel::OnSliderTrack )
00028   EVT_SCROLL_PAGEUP( wxSliderPanel::OnSliderTrack )
00029   EVT_SCROLL_LINEDOWN( wxSliderPanel::OnSliderTrack )
00030   EVT_SCROLL_LINEUP( wxSliderPanel::OnSliderTrack )
00031   EVT_SCROLL_CHANGED( wxSliderPanel::OnSliderChange )
00032   EVT_TEXT( wxID_ANY, wxSliderPanel::OnChangeText )
00033   EVT_TEXT_ENTER( wxID_ANY, wxSliderPanel::OnEnterText )
00034 END_EVENT_TABLE()
00035 
00036 
00037 const char wxSliderPanel::update[] = "";
00038 const char wxSliderPanel::enter[] = "";
00039 
00040 
00041 //: Constructor - Default
00042 wxSliderPanel::wxSliderPanel()
00043 : base_id_(10100),
00044   send_messages_(true)
00045 {
00046   Init();
00047 }
00048 
00049 //: Constructor
00050 wxSliderPanel::wxSliderPanel(wxWindow* parent,
00051                              wxWindowID id,
00052                              wxWindowID base_id,
00053                              const wxPoint& pos,
00054                              const wxSize& size,
00055                              long style,
00056                              const wxString& name)
00057   : send_messages_(true)
00058 {
00059   Init();
00060   Create(parent, id, base_id, pos, size, style, name);
00061 }
00062 
00063 
00064 //: Creator
00065 bool wxSliderPanel::Create(wxWindow* parent,
00066                            wxWindowID id,
00067                            wxWindowID base_id,
00068                            const wxPoint& pos,
00069                            const wxSize& size,
00070                            long style,
00071                            const wxString& name)
00072 {
00073   base_id_ = base_id;
00074   wxScrolledWindow::Create(parent, id, pos, size, style, name);
00075   return true;
00076 }
00077 
00078 
00079 //: Destructor
00080 wxSliderPanel::~wxSliderPanel()
00081 {
00082 }
00083 
00084 
00085 //: Initialization
00086 void wxSliderPanel::Init()
00087 {
00088 }
00089 
00090 //: Convert slider position to a double value
00091 double wxSliderPanel::sp_to_val(unsigned int idx, int sp) const
00092 {
00093   return (sp/1000.0) * (max_vals_[idx] - min_vals_[idx]) + min_vals_[idx];
00094 }
00095 
00096 //: Convert a double value to a slider position
00097 int wxSliderPanel::val_to_sp(unsigned int idx, double val) const
00098 {
00099   return static_cast<int>((1000.0*(val -min_vals_[idx])
00100                                  /(max_vals_[idx]-min_vals_[idx])));
00101 }
00102 
00103 
00104 //: Create the controls
00105 void wxSliderPanel::CreateControls()
00106 {
00107   wxFlexGridSizer* itemFlexGridSizer = new wxFlexGridSizer(vals_.size(), 3, 0, 0);
00108   itemFlexGridSizer->AddGrowableCol(1);
00109   this->SetSizer(itemFlexGridSizer);
00110 
00111   itemFlexGridSizer->Add(0,3,0,wxGROW,0);
00112   itemFlexGridSizer->Add(0,3,0,wxGROW,0);
00113   itemFlexGridSizer->Add(0,3,0,wxGROW,0);
00114   for (unsigned int i=0; i<vals_.size(); ++i)
00115   {
00116     wxStaticText* itemLabel = new wxStaticText(this, wxID_STATIC,
00117                                                wxString::Format(wxT("%u"),i+1),
00118                                                wxDefaultPosition, wxDefaultSize, 0 );
00119     itemFlexGridSizer->Add(itemLabel, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_TOP|wxLEFT, 3);
00120 
00121     int pos = val_to_sp(i,vals_[i]);
00122     wxSlider* itemSlider = new wxSlider(this, base_id_+2*i, pos, 0, 1000,
00123                                         wxDefaultPosition, wxSize(100, -1),
00124                                         wxSL_HORIZONTAL );
00125     itemFlexGridSizer->Add(itemSlider, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxLEFT, 3);
00126 
00127     wxTextCtrl* itemTextCtrl = new wxTextCtrl(this, base_id_+2*i+1,
00128                                               wxString::Format(wxT("%g"),vals_[i]),
00129                                               wxDefaultPosition, wxSize(70, -1), wxTE_PROCESS_ENTER );
00130     itemFlexGridSizer->Add(itemTextCtrl, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT, 3);
00131     // Numeric text validator
00132     itemTextCtrl->SetValidator( wxTextValidator(wxFILTER_NUMERIC) );
00133   }
00134   itemFlexGridSizer->Add(0,3,0,wxGROW,0);
00135   itemFlexGridSizer->Add(0,3,0,wxGROW,0);
00136   itemFlexGridSizer->Add(0,3,0,wxGROW,0);
00137 }
00138 
00139 void wxSliderPanel::CreateSliders(const vcl_vector<double>& init_vals,
00140                                   const vcl_vector<double>& min_vals,
00141                                   const vcl_vector<double>& max_vals)
00142 {
00143   assert(init_vals.size() == min_vals.size());
00144   assert(init_vals.size() == max_vals.size());
00145   vals_ = init_vals;
00146   min_vals_ = min_vals;
00147   max_vals_ = max_vals;
00148 
00149 
00150   Freeze();
00151   DestroyChildren();
00152   wxScrolledWindow::Create(GetParent(), GetId(), GetPosition(), GetSize(), GetWindowStyleFlag(), GetName());
00153   CreateControls();
00154   FitInside();
00155   SetScrollRate(0,20);
00156   Thaw();
00157   Refresh();
00158 }
00159 
00160 
00161 bool wxSliderPanel::ShowToolTips()
00162 {
00163   return true;
00164 }
00165 
00166 
00167 //: Used by event handles to validate and lookup widgets
00168 int wxSliderPanel::GetWidgets(const wxEvent& event,
00169                               wxSlider*& slider,
00170                               wxTextCtrl*& text)
00171 {
00172   slider = NULL;
00173   text = NULL;
00174   int idx = (event.GetId()-base_id_)/2;
00175   if (idx < 0 || idx >= static_cast<int>(vals_.size()))
00176     return -1;
00177 
00178   text = dynamic_cast<wxTextCtrl*>(FindWindowById(base_id_+2*idx+1));
00179   slider = dynamic_cast<wxSlider*>(FindWindowById(base_id_+2*idx));
00180   return idx;
00181 }
00182 
00183 
00184 //: Handle Slider Tracking (dragging)
00185 void wxSliderPanel::OnSliderTrack( wxScrollEvent& event )
00186 {
00187   wxSlider* slider;
00188   wxTextCtrl* text;
00189   int idx = GetWidgets(event, slider, text);
00190   if (!slider || !text){
00191     event.Skip();
00192     return;
00193   }
00194 
00195   int spos = event.GetInt();
00196   double val = sp_to_val(idx,spos);
00197   text->SetValue(wxString::Format(wxT("%g"),val));
00198 }
00199 
00200 //: Handle Slider Release (stop dragging)
00201 void wxSliderPanel::OnSliderChange( wxScrollEvent& event )
00202 {
00203   wxSlider* slider;
00204   wxTextCtrl* text;
00205   int idx = GetWidgets(event, slider, text);
00206   if (!slider || !text){
00207     event.Skip();
00208     return;
00209   }
00210 
00211   if (send_messages_)
00212   {
00213     vgui_message m;
00214     m.from = this;
00215     m.user = wxSliderPanel::enter;
00216     m.data = &idx;
00217     notify(m);
00218   }
00219 }
00220 
00221 
00222 //: Handle Textbox value change (typing)
00223 void wxSliderPanel::OnChangeText( wxCommandEvent& event )
00224 {
00225   wxSlider* slider;
00226   wxTextCtrl* text;
00227   int idx = GetWidgets(event, slider, text);
00228   if (!slider || !text){
00229     event.Skip();
00230     return;
00231   }
00232 
00233   event.GetString().ToDouble(&vals_[idx]);
00234   int spos = val_to_sp(idx,vals_[idx]);
00235   slider->SetValue(spos);
00236   event.Skip();
00237 
00238   if (send_messages_)
00239   {
00240     vgui_message m;
00241     m.from = this;
00242     m.user = wxSliderPanel::update;
00243     m.data = &idx;
00244     notify(m);
00245   }
00246 }
00247 
00248 
00249 //: Handle Textbox press of Enter key
00250 void wxSliderPanel::OnEnterText( wxCommandEvent& event )
00251 {
00252   wxSlider* slider;
00253   wxTextCtrl* text;
00254   int idx = GetWidgets(event, slider, text);
00255   if (!slider || !text){
00256     event.Skip();
00257     return;
00258   }
00259 
00260   if (send_messages_)
00261   {
00262     vgui_message m;
00263     m.from = this;
00264     m.user = wxSliderPanel::enter;
00265     m.data = &idx;
00266     notify(m);
00267   }
00268 }
00269 
00270 
00271 //: Update the data
00272 void wxSliderPanel::update_data(vcl_vector<double>& data,
00273                                 bool send_messages)
00274 {
00275   for (unsigned int i=0; i<data.size() && i<vals_.size(); ++i)
00276   {
00277     update_data(i,data[i],send_messages);
00278   }
00279 }
00280 
00281 //: Update a single value
00282 void wxSliderPanel::update_data(unsigned int i, double val,
00283                                 bool send_messages)
00284 {
00285   vals_[i] = val;
00286   int id = base_id_+2*i+1; // id of text control
00287   wxTextCtrl* text = dynamic_cast<wxTextCtrl*>(FindWindowById(id));
00288 
00289   // optionally disable sending messages about this update
00290   send_messages_ = send_messages;
00291   text->SetValue(wxString::Format(wxT("%g"),val));
00292   send_messages_ = true;
00293 }