00001
00002 #ifndef Impala_Visualization_ShowNextFromControl_h
00003 #define Impala_Visualization_ShowNextFromControl_h
00004
00005 #include "Visualization/ShowNextFromControlListener.h"
00006
00007 #ifndef OglGui_Button_h
00008 #include "OglGui/Button.h"
00009 #endif
00010
00011 #ifndef OglGui_TextEdit_h
00012 #include "OglGui/TextEdit.h"
00013 #endif
00014
00015 namespace Impala {
00016 namespace Visualization {
00017
00018 class ShowNextFromControl : public OglGui::Window,
00019 public OglGui::ButtonListener,
00020 public OglGui::WindowListener
00021 {
00022 public:
00023 typedef OglGui::Window Window;
00024 typedef OglGui::StaticText StaticText;
00025 typedef OglGui::TextEdit TextEdit;
00026 typedef OglGui::Button Button;
00027
00028 ShowNextFromControl(Window* parent,int w,int h,int nrShow,int margin=0) :
00029 Window(parent, w, h)
00030 {
00031 Init(w,h,nrShow,margin);
00032 }
00033
00034 void SetListener(ShowNextFromControlListener* l, void* lData=0)
00035 {
00036 mListener = l;
00037 mListenerData = lData;
00038 }
00039
00040 int NrToShow() { return mNrToShow; }
00041 void NrToShow(int n) { mNrToShow = n; }
00042 int StartVal() { return mStartVal; }
00043 void StartVal(int v) { mStartVal = v; ShowNextFromChanged(); }
00044 int MaxVal() { return mMax; }
00045
00046 void MaxVal(int m)
00047 {
00048 mMax = m;
00049 mTotalText->SetText("Total:"+MakeString(m));
00050 if (mStartVal > mMax && (mStartVal = mMax - mNrToShow)<-1)
00051 mStartVal = 0;
00052 UpdateStartValText();
00053 }
00054
00055
00056 StaticText* TotalText() { return mTotalText; }
00057 StaticText* ShowFromText() { return mShowFromText; }
00058 TextEdit* ShowNrTextEdit() { return mShowNrTextEdit; }
00059 TextEdit* ValTextEdit() { return mValTextEdit; }
00060 Button* PlusButton() { return mPlusButton; }
00061 Button* MinButton() { return mMinButton; }
00062
00063 void ChangeValText(TextEdit* txtEdit, int val)
00064 {
00065 txtEdit->SetText(MakeString(val));
00066 txtEdit->SetCaretMark(100,100);
00067 }
00068
00069 void Clamp(int& ref, int minVal, int maxVal)
00070 {
00071 if (ref < minVal) ref = minVal;
00072 if (ref > maxVal) ref = maxVal;
00073 }
00074
00075 void UpdateStartValText()
00076 {
00077 Clamp(mStartVal,0,mMax ? mMax-1 : 0);
00078 ChangeValText(mValTextEdit,mStartVal);
00079 }
00080
00081 void UpdateShowNrText()
00082 {
00083 Clamp(mNrToShow,1,2000);
00084 ChangeValText(mShowNrTextEdit,mNrToShow);
00085 }
00086
00087 void ShowNextFromChanged()
00088 {
00089 if (mListener)
00090 mListener->ShowNextFromChanged(this, mListenerData);
00091 }
00092
00093 virtual void
00094 WindowKeyboardEvent(OglGui::Window *src, int c, int state, void *userData)
00095 {
00096 if (c == '\r')
00097 {
00098 if (src == mValTextEdit)
00099 {
00100 mStartVal = atoi(mValTextEdit->GetText().c_str());
00101 UpdateStartValText();
00102 }
00103 if (src == mShowNrTextEdit)
00104 {
00105 mNrToShow = atoi(mShowNrTextEdit->GetText().c_str());
00106 UpdateShowNrText();
00107 }
00108 ShowNextFromChanged();
00109 }
00110 }
00111
00112 virtual void ButtonSelectionEvent(OglGui::Button *src, void *userData)
00113 {
00114 if (src == mMinButton || src == mPlusButton)
00115 {
00116 mStartVal += (((src==mPlusButton) ? mNrToShow : -mNrToShow));
00117 UpdateStartValText();
00118 ShowNextFromChanged();
00119 }
00120 }
00121
00122 private:
00123 void Init(int w, int h, int nrShow, int m)
00124 {
00125 int x = m;
00126 int mH = h - 2 * m;
00127
00128 mListener = 0;
00129 mMax = 0;
00130 mStartVal = 0;
00131 mNrToShow = nrShow;
00132
00133 mTotalText = new StaticText(this,x,m,110,mH,"Total:");
00134 mTotalText->SetAlign(oglLeftAlign);
00135 x += 112;
00136
00137 mShowNrTextEdit = new TextEdit(this,x,m,50,mH,"",0);
00138 UpdateShowNrText();
00139 mShowNrTextEdit->SingleLine(true);
00140 mShowNrTextEdit->SetWindowListener(this);
00141 x += 52;
00142
00143 mShowFromText = new StaticText(this,x,m,88,mH,"Showing from:");
00144 mShowFromText->SetAlign(oglLeftAlign);
00145 x += 90;
00146
00147 mValTextEdit = new TextEdit(this,x,m,60,mH,"0",0);
00148 UpdateStartValText();
00149 mValTextEdit->SingleLine(true);
00150 mValTextEdit->SetWindowListener(this);
00151 x += 62;
00152
00153 mMinButton = new OglGui::Button(this,x,m,16,mH,"-",4,true);
00154 mMinButton->SetButtonListener(this);
00155 x += 18;
00156
00157 mPlusButton = new OglGui::Button(this,x,m,16,mH,"+",4,true);
00158 mPlusButton->SetButtonListener(this);
00159 x += 16 + m;
00160
00161 SetDimensions(x,h);
00162 ScaleChildren();
00163 SetAllowChildScaling(false);
00164 SetDimensions(w,h);
00165 }
00166
00167 ShowNextFromControlListener* mListener;
00168 void* mListenerData;
00169
00170 StaticText* mTotalText;
00171 StaticText* mShowFromText;
00172 TextEdit* mValTextEdit;
00173 TextEdit* mShowNrTextEdit;
00174 Button* mPlusButton;
00175 Button* mMinButton;
00176
00177 int mStartVal;
00178 int mMax;
00179 int mNrToShow;
00180 };
00181
00182 }
00183 }
00184
00185 #endif