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 = + (pos*(mSpacing+mFixedSize/mAspectRatio));
00182 }
00183
00184 ScrollBar* GetScrollBar()
00185 {
00186 return mScrollBar;
00187 }
00188
00189 void SetScrollBarRange()
00190 {
00191 int pageSize;
00192 if (mHorizontal)
00193 pageSize = W()/(mSpacing+mFixedSize*mAspectRatio);
00194 else
00195 pageSize = H()/(mSpacing+mFixedSize/mAspectRatio);
00196 mScrollBar->SetRange(mNrIm, pageSize>0 ? pageSize : 1);
00197 }
00198
00199 virtual void PlaceView(OGLVIEW* oglView)
00200 {
00201 OGLIMAGE* im = oglView->im;
00202 int imW = (im ? im->w : mFixedSize*mAspectRatio);
00203 int imH = (im ? im->h : mFixedSize/mAspectRatio);
00204 int vX, vY, vW, vH;
00205
00206 vH = vW = mFixedSize;
00207 vX = vY = mFixedPos;
00208
00209 if (mHorizontal)
00210 {
00211 vW = mAspectRatio * mFixedSize;
00212 vX = mNextViewPos;
00213 mNextViewPos += vW + mSpacing;
00214 }
00215 else
00216 {
00217 vH = mFixedSize / mAspectRatio;
00218 vY = mNextViewPos;
00219 mNextViewPos -= vH + mSpacing;
00220 }
00221 viewSys.SetDimensions(oglView,vX,vY,vW,vH);
00222 if(oglView->im && mFitImages)
00223 viewSys.SetZoom(oglView, vW/(float)imW, vH/(float)imH);
00224 }
00225
00226 View* AddImage(OGLIMAGE *im, ViewListener *listener = 0)
00227 {
00228 View *view;
00229 OGLVIEW *oV;
00230
00231 if(!im) return 0;
00232
00233 if(listener == 0 && mViewListener != 0)
00234 listener = mViewListener;
00235
00236 view = new View(mOglWnd, im, 0, 0, 10, 10,listener);
00237 oV = view->GetOGLVIEW2D();
00238 if (oV->im && mAspectRatio <= 0)
00239 mAspectRatio = oV->im->w / (float) oV->im->h;
00240 PlaceView(oV);
00241 mNrIm++;
00242
00243 viewSys.SetColor(oV, OGL_BORDER, oglBLACK);
00244
00245 view->SetTags(mViewTags);
00246 SetScrollBarRange();
00247 return view;
00248 }
00249
00250 virtual int SetState(int nState)
00251 {
00252 mScrollBar->SetState(nState);
00253 return Window::SetState(nState);
00254 }
00255
00256 void LayoutViews()
00257 {
00258 LIST* obj;
00259 int i = 0;
00260
00261 mFixedPos = mMargin + (mHorizontal ? (mStdSide ? mExt+1:3) :
00262 (mStdSide ? 3:mExt+1));
00263 mFixedSize = (mHorizontal ? H() : W()) - (mExt+4) - 2*mMargin;
00264 mNextViewPos = (mHorizontal ? mMargin + 2 : H() - (mFixedSize+mMargin+2));
00265 ForAllElements(obj, mOglWnd->objectList)
00266 {
00267 OGLVIEW* view = (OGLVIEW *) obj->info;
00268 PlaceView(view);
00269 i++;
00270 }
00271 mNrIm = i;
00272 SetScrollBarRange();
00273
00274 if (mNrIm)
00275 OnScroll(0, mScrollBar->GetValue(), 0);
00276 }
00277
00278 virtual void ReshapeFunc(int w, int h)
00279 {
00280 Window::ReshapeFunc(w, h);
00281 LayoutViews();
00282 }
00283
00284 virtual void MouseFunc(INT msg, INT but, INT state, INT x, INT y)
00285 {
00286 Window::MouseFunc(msg, but, state, x, y);
00287
00288 int incr = mScrollBar->LineIncrement();
00289 if(msg == oglMouseWheelUp) mScrollBar->ChangePos(-incr);
00290 if(msg == oglMouseWheelDown) mScrollBar->ChangePos(incr);
00291
00292 if ((msg==oglMouseDown || msg==oglMouseDblClick) && mImListener)
00293 {
00294 OGLVIEW *view = viewSys.FindView(mOglWnd, x, y);
00295 if (view) {
00296 int id = FindViewIndex(view);
00297 mImListener
00298 ->ImageSelectionEvent(this,id,msg,but,state,mListenerData);
00299 }
00300 }
00301 }
00302
00303 virtual void KeyboardFunc(INT c, int state)
00304 {
00305 Window::KeyboardFunc(c, state);
00306 if(mScrollBar)
00307 mScrollBar->ScrollKeys(c, state);
00308 }
00309
00310 private:
00311
00312 void Init(int w, int h, bool horizontal, bool stdSide )
00313 {
00314 mHorizontal = horizontal;
00315 mNrIm = 0;
00316 mAspectRatio = 0;
00317 mSpacing = 2;
00318 mMargin = 2;
00319 mExt = 16;
00320
00321 mViewTags = FlexViewTags;
00322
00323 mStdSide = stdSide;
00324 oglSys.SetAllowCameraMove(mOglWnd, 0);
00325 SetBorderType(BEV_ETCHED);
00326 mScrollBar = new WindowScrollBar(this, horizontal, stdSide);
00327 mScrollBar->SetScrollBarListener(this, 0);
00328 mScrollBar->SetLineIncrement(1);
00329
00330 mViewListener = 0;
00331 mImListener = 0;
00332 mFitImages = true;
00333 LayoutViews();
00334 }
00335
00336 WindowScrollBar* mScrollBar;
00337 bool mHorizontal;
00338 bool mStdSide;
00339 bool mFitImages;
00340 float mAspectRatio;
00341 int mNrIm;
00342 int mFixedSize;
00343 int mFixedPos;
00344 int mNextViewPos;
00345 int mExt;
00346 int mMargin;
00347 int mSpacing;
00348
00349 int mViewTags;
00350
00351 ViewListener* mViewListener;
00352 ImagesListener* mImListener;
00353 void* mListenerData;
00354
00355 };
00356
00357 }
00358 #endif