00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef OglGui_ViewStrip_h
00035 #define OglGui_ViewStrip_h
00036
00037 #include "OglGui/View.h"
00038 #include "OglGui/WindowScrollBar.h"
00039 #include "OglGui/ImagesListener.h"
00040
00041 namespace OglGui
00042 {
00043
00044 class ViewStrip : public Window, public ScrollBarListener
00045 {
00046 public:
00047
00048 ViewStrip(int x, int y, int w, int h, bool horizontal=true,
00049 bool stdSide=true) :
00050 Window(x, y, w, h, true)
00051 {
00052 Init(w, h, horizontal, stdSide);
00053 }
00054
00055 ViewStrip(Window* parent, int x, int y, int w, int h, bool horizontal=true,
00056 bool stdSide=true) :
00057 Window(parent, x, y, w, h, true)
00058 {
00059 Init(w, h, horizontal, stdSide);
00060 }
00061
00062 ViewStrip(Window* parent, int w, int h, bool horizontal=true,
00063 bool stdSide=true ) :
00064 Window(parent, w, h, true)
00065 {
00066 Init(w, h, horizontal, stdSide);
00067 }
00068
00069 void SetImagesListener(ImagesListener* listener, void* listenerData = 0)
00070 {
00071 mImListener = listener;
00072 mListenerData = listenerData;
00073 }
00074
00075
00076
00077 void SetViewListener(ViewListener* listener)
00078 {
00079 mViewListener = listener;
00080 }
00081
00082 void AspectRatio(float ratio)
00083 {
00084 mAspectRatio = ratio;
00085 }
00086
00087 void SetFitImages(bool mode)
00088 {
00089 mFitImages = mode;
00090
00091 if(mode) LayoutViews();
00092 }
00093
00094 bool GetFitImages()
00095 {
00096 return mFitImages;
00097 }
00098
00099 void SetExtraSpace(int nPix)
00100 {
00101 mExt = nPix;
00102 }
00103
00104 void Spacing(int nPix)
00105 {
00106 mSpacing = nPix;
00107 }
00108
00109 void SetMargin(int nPix)
00110 {
00111 mMargin = nPix;
00112 }
00113
00114 void SetViewTags(int tags)
00115 {
00116 mViewTags = tags;
00117 }
00118
00119 int Size()
00120 {
00121 return mNrIm;
00122 }
00123
00124 OGLVIEW* FindViewWithImage(OGLIMAGE* im)
00125 {
00126 LIST *obj;
00127
00128 ForAllElements(obj, mOglWnd->objectList)
00129 {
00130 OGLVIEW* view = (OGLVIEW *) obj->info;
00131 if(im == view->im)
00132 return view;
00133 }
00134 return 0;
00135 }
00136
00137
00138 int FindViewIndex(OGLVIEW *view)
00139 {
00140 LIST *obj;
00141 int ind = 0;
00142
00143 ForAllElements(obj, mOglWnd->objectList)
00144 {
00145 if(view == (OGLVIEW *) obj->info)
00146 return ind;
00147 ind++;
00148 }
00149 return -1;
00150 }
00151
00152
00153 OGLVIEW* FindIndexView(int ind)
00154 {
00155 LIST *obj;
00156 int n=0;
00157
00158 if (ind < 0)
00159 return NULL;
00160
00161 ForAllElements(obj, mOglWnd->objectList)
00162 {
00163 if(n++ == ind)
00164 return (OGLVIEW *) obj->info;
00165 }
00166 return NULL;
00167 }
00168
00169 View* ViewFromOGLVIEW(OGLVIEW* oglView)
00170 {
00171 if (!oglView)
00172 return 0;
00173 return (View*) viewSys.GetSysData(oglView,1);
00174 }
00175
00176 void OnScroll(ScrollBar *src, int pos, void* data)
00177 {
00178 if(mHorizontal)
00179 mOglWnd->docX = -pos * (mSpacing+mFixedSize*mAspectRatio);
00180 else
00181 mOglWnd->docY = H() + (pos*(mSpacing+mFixedSize/mAspectRatio));
00182 }
00183
00184 ScrollBar* GetScrollBar()
00185 {
00186 return mScrollBar;
00187 }
00188
00189 void SetScrollBarRange()
00190 {
00191 if (mHorizontal)
00192 mScrollBar->SetRange(mNrIm, W()/(mSpacing+mFixedSize*mAspectRatio));
00193 else
00194 mScrollBar->SetRange(mNrIm, H()/(mSpacing+mFixedSize/mAspectRatio));
00195 }
00196
00197 virtual void PlaceView(OGLVIEW* oglView)
00198 {
00199 OGLIMAGE* im = oglView->im;
00200 int imW = (im ? im->w : mFixedSize*mAspectRatio);
00201 int imH = (im ? im->h : mFixedSize/mAspectRatio);
00202 int vX, vY, vW, vH;
00203
00204 vH = vW = mFixedSize;
00205 vX = vY = mFixedPos;
00206
00207 if (mHorizontal)
00208 {
00209 vW = mAspectRatio * mFixedSize;
00210 vX = mNextViewPos;
00211 mNextViewPos += vW + mSpacing;
00212 }
00213 else
00214 {
00215 vH = mFixedSize / mAspectRatio;
00216 vY = -mNextViewPos;
00217 mNextViewPos += vH + mSpacing;
00218 }
00219 viewSys.SetDimensions(oglView,vX,vY,vW,vH);
00220 if(oglView->im && mFitImages)
00221 viewSys.SetZoom(oglView, vW/(float)imW, vH/(float)imH);
00222 }
00223
00224 View* AddImage(OGLIMAGE *im, ViewListener *listener = 0)
00225 {
00226 View *view;
00227 OGLVIEW *oV;
00228
00229 if(!im) return 0;
00230
00231 if(listener == 0 && mViewListener != 0)
00232 listener = mViewListener;
00233
00234 view = new View(mOglWnd, im, 0, 0, 10, 10,listener);
00235 oV = view->GetOGLVIEW2D();
00236 if (oV->im && mAspectRatio <= 0)
00237 mAspectRatio = oV->im->w / (float) oV->im->h;
00238 PlaceView(oV);
00239 mNrIm++;
00240
00241 viewSys.SetColor(oV, OGL_BORDER, oglBLACK);
00242
00243 view->SetTags(mViewTags);
00244 SetScrollBarRange();
00245 return view;
00246 }
00247
00248 virtual int SetState(int nState)
00249 {
00250 mScrollBar->SetState(nState);
00251 return Window::SetState(nState);
00252 }
00253
00254 void LayoutViews()
00255 {
00256 LIST* obj;
00257 int i = 0;
00258
00259 mFixedPos = mMargin + (mHorizontal ? (mStdSide ? mExt+1:3) :
00260 (mStdSide ? 3:mExt+1));
00261 mFixedSize = (mHorizontal ? H() : W()) - (mExt+4) - 2*mMargin;
00262 mNextViewPos = (mHorizontal ? mMargin + 2 : H() - (mFixedSize+mMargin+2));
00263
00264 ForAllElements(obj, mOglWnd->objectList)
00265 {
00266 OGLVIEW* view = (OGLVIEW *) obj->info;
00267 PlaceView(view);
00268 i++;
00269 }
00270 mNrIm = i;
00271 SetScrollBarRange();
00272 }
00273
00274 virtual void ReshapeFunc(int w, int h)
00275 {
00276 Window::ReshapeFunc(w, h);
00277 LayoutViews();
00278 }
00279
00280 virtual void MouseFunc(INT msg, INT but, INT state, INT x, INT y)
00281 {
00282 Window::MouseFunc(msg, but, state, x, y);
00283
00284 int incr = mScrollBar->LineIncrement();
00285 if(msg == oglMouseWheelUp) mScrollBar->ChangePos(-incr);
00286 if(msg == oglMouseWheelDown) mScrollBar->ChangePos(incr);
00287
00288 if ((msg==oglMouseDown || msg==oglMouseDblClick) && mImListener)
00289 {
00290 OGLVIEW *view = viewSys.FindView(mOglWnd, x, y);
00291 if (view) {
00292 int id = FindViewIndex(view);
00293 mImListener
00294 ->ImageSelectionEvent(this,id,msg,but,state,mListenerData);
00295 }
00296 }
00297 }
00298
00299 virtual void KeyboardFunc(INT c, int state)
00300 {
00301 Window::KeyboardFunc(c, state);
00302 if(mScrollBar)
00303 mScrollBar->ScrollKeys(c, state);
00304 }
00305
00306 private:
00307
00308 void Init(int w, int h, bool horizontal, bool stdSide )
00309 {
00310 mHorizontal = horizontal;
00311 mNrIm = 0;
00312 mAspectRatio = 0;
00313 mSpacing = 2;
00314 mMargin = 2;
00315 mExt = 16;
00316
00317 mViewTags = FlexViewTags;
00318
00319 mStdSide = stdSide;
00320 oglSys.SetAllowCameraMove(mOglWnd, 0);
00321 SetBorderType(BEV_ETCHED);
00322 mScrollBar = new WindowScrollBar(this, horizontal, stdSide);
00323 mScrollBar->SetScrollBarListener(this, 0);
00324 mScrollBar->SetLineIncrement(1);
00325
00326 mViewListener = 0;
00327 mImListener = 0;
00328 mFitImages = true;
00329 LayoutViews();
00330 }
00331
00332 WindowScrollBar* mScrollBar;
00333 bool mHorizontal;
00334 bool mStdSide;
00335 bool mFitImages;
00336 float mAspectRatio;
00337 int mNrIm;
00338 int mFixedSize;
00339 int mFixedPos;
00340 int mNextViewPos;
00341 int mExt;
00342 int mMargin;
00343 int mSpacing;
00344
00345 int mViewTags;
00346
00347 ViewListener* mViewListener;
00348 ImagesListener* mImListener;
00349 void* mListenerData;
00350
00351 };
00352
00353 }
00354 #endif