home *** CD-ROM | disk | FTP | other *** search
- /* XGStdScroll.cpp
- *
- * The scroll bar handler code
- */
-
- /* YAAF - Yet another application framework
- * Copyright (C) 1997 William Edward Woody and In Phase Consulting
- *
- * This library is free software; you can redistribute it
- * and/or modify it under the terms of the GNU Library
- * General Public License as published by the Free Software
- * Foundation; either version 2 of the License, or any
- * later version.
- *
- * This library is distributed in the hope that it will be
- * useful, but WITHOUT ANY WARRANTY; without even the implied
- * warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU Library General Public License for
- * more details.
- *
- * You should have received a copy of the GNU Library General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- *
- * To contact the author, either e-mail me at
- * woody@alumni.caltech.edu, or write to us at
- *
- * William Edward Woody
- * In Phase Consulting
- * 1545 Ard Eevin Avenue
- * Glendale, CA 91202
- */
-
- #include <XApplication.h>
- #include <XDataUtil.h>
- #include <XStdScroll.h>
- #include <XWindow.h>
- #include <XError.h>
-
- /************************************************************************/
- /* */
- /* Globals */
- /* */
- /************************************************************************/
-
- #if OPT_MACOS == 1
- ControlActionUPP XGStdScroll::gProc = NULL;
- #endif
-
- /************************************************************************/
- /* */
- /* Construction/Destruction */
- /* */
- /************************************************************************/
-
- /* XGStdScroll::XGStdScroll
- *
- * create a push button
- */
-
- XGStdScroll::XGStdScroll(XGView *view, XGArgStream &stream) :
- XGView(view,stream,true)
- {
- short min,max,val,page;
-
- page = stream.GetInteger();
- min = stream.GetInteger();
- max = stream.GetInteger();
- val = stream.GetInteger();
- Init(page,min,max,val);
- }
-
- XGStdScroll::XGStdScroll(XGView *view, XGSScrollInitRecord &i) :
- XGView(view,i.v,true)
- {
- Init(i.page,i.min,i.max,i.val);
- }
-
- /* XGStdScroll::~XGStdScroll
- *
- * Delete me
- */
-
- XGStdScroll::~XGStdScroll()
- {
- #if OPT_MACOS == 1
- if (fControl) ::DisposeControl(fControl);
- #endif
- }
-
- /************************************************************************/
- /* */
- /* Control management */
- /* */
- /************************************************************************/
-
- /* XGStdScroll::DoDrawView
- *
- * Draw my control. This draws the control in a wierd way.
- */
-
- void XGStdScroll::DoDrawView(Rect)
- {
- #if OPT_MACOS == 1
-
- if (fControl && IsActive()) {
- XGDraw draw(this,false);
- ::Draw1Control(fControl);
- } else {
- Rect r;
- XGDraw draw(this);
-
- r = GetContentRect(); // Just erase the rectangle
- FrameRect(&r);
- InsetRect(&r,1,1);
- EraseRect(&r);
- }
- #endif
- }
-
- /* XGStdScroll::DoActivate
- *
- * Activate/deactivate scrollbars
- */
-
- void XGStdScroll::DoActivate(bool active)
- {
- #if OPT_MACOS == 1
- XGDraw draw(this,false);
-
- if (fControl) {
- if (active) ::ShowControl(fControl);
- else {
- Rect r;
-
- ::HideControl(fControl); // reduce flash
-
- r = GetContentRect();
- ViewToGlobal(&r); // (kludge to keep
- FrameRect(&r); // the coordinate systems
- InsetRect(&r,1,1); // correct)
- EraseRect(&r);
- }
- }
- #endif
-
- #if OPT_WINOS == 1
- #pragma unused(active)
- #endif
- }
-
-
- /* XGStdScroll::DoMouseDown
- *
- * Handle mouse click stuff ###TODO
- */
-
- bool XGStdScroll::DoMouseDown(Point pt, short)
- {
- #if OPT_MACOS == 1
- short i;
- XGSScrollEvent e;
- ControlHandle c;
-
- /*
- * Do the prescroll
- */
-
- if (!fControl) return false;
-
- /*
- * Handle the track control (in it's own drawing context)
- */
-
- SetControlReference(fControl,(long)(void *)this);
- {
- XGDraw draw(this,false);
-
- // Where in the control?
- ViewToGlobal(&pt);
- i = ::FindControl(pt,GetWindow()->_GetGrafPtr(),&c);
- if (i == kControlIndicatorPart) {
- // In the thumb. Handle thumb scrolling
- e.oldvalue = GetValue();
- ReceiveDispatch(KEventPreScroll,GetViewID(),(void *)&this);
- i = ::TrackControl(fControl,pt,NULL);
- e.scroll = this;
- ReceiveDispatch(KEventScroll,GetViewID(),(void *)&e);;
- } else {
- // In the rest--handle scrolling bar
- i = ::TrackControl(fControl,pt,gProc);
- }
- }
- #endif
-
- #if OPT_WINOS == 1
- #pragma unused(pt)
- #endif
-
- return false;
- }
-
- /* XGStdScroll::DoSizeView
- *
- * This handles the resize event
- */
-
- void XGStdScroll::DoSizeView(void)
- {
- #if OPT_MACOS == 1
- XGDraw draw(this,false);
- Rect r;
-
- r = GetContentRect();
- ViewToGlobal(&r);
- if (fControl) ::SizeControl(fControl,r.right-r.left,r.bottom-r.top);
- #endif
- }
-
- /* XGStdScroll::DoMoveView
- *
- * Move this view to the new location
- */
-
- void XGStdScroll::DoMoveView(void)
- {
- #if OPT_MACOS == 1
- XGDraw draw(this,false);
- Rect r;
-
- r = GetContentRect();
- ViewToGlobal(&r);
- if (fControl) ::MoveControl(fControl,r.left,r.top);
- #endif
- }
-
- /* XGStdScroll::ShowView
- *
- * This also shows the control
- */
-
- void XGStdScroll::ShowView()
- {
- XGView::ShowView();
-
- #if OPT_MACOS == 1
- if (fControl) {
- XGDraw draw(this,false);
- ::ShowControl(fControl);
- }
- #endif
- }
-
- /* XGStdScroll::HideView
- *
- * This also hides the control
- */
-
- void XGStdScroll::HideView()
- {
- XGView::HideView();
-
- #if OPT_MACOS == 1
- if (fControl) {
- XGDraw draw(this,false);
- ::HideControl(fControl);
- }
- #endif
- }
-
- /* XGStdScroll::EnableView
- *
- * This also enables the control.
- */
-
- void XGStdScroll::EnableView()
- {
- XGView::EnableView();
-
- #if OPT_MACOS == 1
- if (fControl && IsActive()) {
- XGDraw draw(this,false);
- ::HiliteControl(fControl,255);
- }
- #endif
- }
-
- /* XGStdScroll::DisableView
- *
- * This also disables the control
- */
-
- void XGStdScroll::DisableView()
- {
- XGView::DisableView();
-
- #if OPT_MACOS == 1
- if (fControl && IsActive()) {
- XGDraw draw(this,false);
- ::HiliteControl(fControl,0);
- }
- #endif
- }
-
- /************************************************************************/
- /* */
- /* Message Dispatch */
- /* */
- /************************************************************************/
-
- /* XGStdScroll::ReceiveDispatch
- *
- * Receive messages. Pass them upwards
- */
-
- long XGStdScroll::ReceiveDispatch(long msg, long arg, void *parg)
- {
- #if OPT_WINOS == 1
- XGSWInternalRecord *w;
-
- if (msg == KEventWInternal) {
- /*
- * Process the BN_CLICKED command message, turning this
- * into a KEventButton message.
- */
-
- w = (XGSWInternalRecord *)parg;
- if ((w->msg == WM_HSCROLL) || (w->msg == WM_VSCROLL)) {
- short notify;
- short v = GetValue();
-
- notify = LOWORD(w->wp);
- switch (notify) {
- case SB_BOTTOM:
- v = GetMaxValue();
- break;
- case SB_LINEDOWN:
- v++;
- break;
- case SB_LINEUP:
- v--;
- break;
- case SB_PAGEDOWN:
- v += fPageValue;
- break;
- case SB_PAGEUP:
- v -= fPageValue;
- break;
- case SB_THUMBPOSITION:
- case SB_THUMBTRACK:
- v = HIWORD(w->wp);
- break;
- case SB_TOP:
- v = GetMinValue();
- break;
- default:
- return 0; /* Not handled */
- }
-
- // Lazy--just call the SetValue() method
- SetValue(v);
- return 0;
- }
- return 1;
- }
- #endif
-
- return XGView::ReceiveDispatch(msg,arg,parg);
- }
-
- /************************************************************************/
- /* */
- /* Button Values */
- /* */
- /************************************************************************/
-
- /* XGStdScroll:GetValue
- *
- * Get value
- */
-
- short XGStdScroll::GetValue(void)
- {
- #if OPT_MACOS == 1
- return ::GetControlValue(fControl);
- #endif
-
- #if OPT_WINOS == 1
- SCROLLINFO s;
-
- s.cbSize = sizeof(s);
- s.fMask = SIF_POS;
- ::GetScrollInfo(_GetHWND(),SB_CTL,&s);
- return s.nPos;
- #endif
- }
-
- /* XGStdScroll::SetValue
- *
- * Set the value of this thing
- */
-
- void XGStdScroll::SetValue(short x)
- {
- XGSScrollEvent e;
- short ov,nv;
-
- // Find the new value (if it changes)
- ov = GetValue();
- nv = x;
- if (nv < GetMinValue()) nv = GetMinValue();
- if (nv > GetMaxValue()) nv = GetMaxValue();
- if (nv == ov) return; /* Value doesn't change */
-
- // Send the change value messages
- ReceiveDispatch(KEventPreScroll,GetViewID(),(void *)this);
-
- // Now change the value
- #if OPT_MACOS == 1
- {
- XGDraw draw(this,false); // make sure set up right
- ::SetControlValue(fControl,nv);
- }
- #endif
-
- #if OPT_WINOS == 1
- SCROLLINFO s;
-
- s.cbSize = sizeof(s);
- s.fMask = SIF_POS;
- s.nPos = nv;
- ::SetScrollInfo(_GetHWND(),SB_CTL,&s,true);
- #endif
-
- // Send the new value message
- e.oldvalue = ov;
- e.scroll = this;
- ReceiveDispatch(KEventScroll,GetViewID(),(void *)&e);
- }
-
-
- /* XGStdScroll:GetMinValue
- *
- * Get value
- */
-
- short XGStdScroll::GetMinValue(void)
- {
- #if OPT_MACOS == 1
- return ::GetControlMinimum(fControl);
- #endif
-
- #if OPT_WINOS == 1
- SCROLLINFO s;
-
- s.cbSize = sizeof(s);
- s.fMask = SIF_RANGE;
- ::GetScrollInfo(_GetHWND(),SB_CTL,&s);
- return s.nMin;
- #endif
- }
-
- /* XGStdScroll::SetMinValue
- *
- * Set the value of this thing
- */
-
- void XGStdScroll::SetMinValue(short x)
- {
- XGSScrollEvent e;
- short ov,nv;
-
- // Find the new value (if it changes)
- ov = GetValue();
- nv = ov;
- if (nv < x) nv = x;
-
- // If the value changed, send the change value messages
- if (nv != ov) ReceiveDispatch(KEventPreScroll,GetViewID(),(void *)this);
-
- // Now change the value
- #if OPT_MACOS == 1
- {
- XGDraw draw(this,false); // make sure set up right
- ::SetControlMinimum(fControl,x);
- }
- #endif
-
- #if OPT_WINOS == 1
- SCROLLINFO s;
-
- s.cbSize = sizeof(s);
- s.fMask = SIF_RANGE;
- ::GetScrollInfo(_GetHWND(),SB_CTL,&s);
- s.nMin = x;
- ::SetScrollInfo(_GetHWND(),SB_CTL,&s,true);
- #endif
-
- // If the value changed, send the new value message
- if (nv != ov) {
- e.oldvalue = ov;
- e.scroll = this;
- ReceiveDispatch(KEventScroll,GetViewID(),(void *)&e);
- }
- }
-
-
- /* XGStdScroll:GetMaxValue
- *
- * Get value
- */
-
- short XGStdScroll::GetMaxValue(void)
- {
- #if OPT_MACOS == 1
- return ::GetControlMaximum(fControl);
- #endif
-
- #if OPT_WINOS == 1
- SCROLLINFO s;
-
- s.cbSize = sizeof(s);
- s.fMask = SIF_RANGE;
- ::GetScrollInfo(_GetHWND(),SB_CTL,&s);
- return s.nMax;
- #endif
- }
-
- /* XGStdScroll::SetMaxValue
- *
- * Set the value of this thing
- */
-
- void XGStdScroll::SetMaxValue(short x)
- {
- XGSScrollEvent e;
- short ov,nv;
-
- // Find the new value (if it changes)
- ov = GetValue();
- nv = ov;
- if (nv > x) nv = x;
-
- // If the value changed, send the change value messages
- if (nv != ov) ReceiveDispatch(KEventPreScroll,GetViewID(),(void *)this);
-
- // Now change the value
- #if OPT_MACOS == 1
- {
- XGDraw draw(this,false); // make sure set up right
- ::SetControlMaximum(fControl,x);
- }
- #endif
-
- #if OPT_WINOS == 1
- SCROLLINFO s;
-
- s.cbSize = sizeof(s);
- s.fMask = SIF_PAGE | SIF_RANGE;
- ::GetScrollInfo(_GetHWND(),SB_CTL,&s);
- s.nMax = x;
- if (s.nPage > 0) s.nMax += s.nPage - 1;
- ::SetScrollInfo(_GetHWND(),SB_CTL,&s,true);
- #endif
-
- // If the value changed, send the new value message
- if (nv != ov) {
- e.oldvalue = ov;
- e.scroll = this;
- ReceiveDispatch(KEventScroll,GetViewID(),(void *)&e);
- }
- }
-
- /* XGStdScroll:GetPageValue
- *
- * Get value
- */
-
- short XGStdScroll::GetPageValue(void)
- {
- return fPageValue;
- }
-
- /* XGStdScroll::SetPageValue
- *
- * Set the value of this thing
- */
-
- void XGStdScroll::SetPageValue(short x)
- {
- fPageValue = x;
-
- #if OPT_WINOS == 1
- SCROLLINFO s;
-
- s.cbSize = sizeof(s);
- s.fMask = SIF_PAGE | SIF_RANGE;
- ::GetScrollInfo(_GetHWND(),SB_CTL,&s);
-
- /* Adjust min/max in page value */
- if (s.nPage > 0) s.nMax = s.nMax - s.nPage + 1; /* Actual max */
- if (x > 0) s.nMax = s.nMax + x - 1;
-
- s.nPage = x;
- ::SetScrollInfo(_GetHWND(),SB_CTL,&s,true);
- #endif
- }
-
- /************************************************************************/
- /* */
- /* Action Proc Stuff */
- /* */
- /************************************************************************/
-
- #if OPT_MACOS == 1
-
- /* XGStdScroll::ActionProc
- *
- * The action proc for scrolling the scrollbar
- */
-
- pascal void XGStdScroll::ActionProc(ControlHandle c, short part)
- {
- XGStdScroll *s = (XGStdScroll *)GetControlReference(c);
- short ov,nv;
- short min,max;
- XGSScrollEvent e;
-
- ov = s->GetValue();
- min = s->GetMinValue();
- max = s->GetMaxValue();
-
- switch (part) {
- default:
- return;
-
- case kControlUpButtonPart:
- nv = ov - 1;
- break;
-
- case kControlDownButtonPart:
- nv = ov + 1;
- break;
-
- case kControlPageUpPart:
- nv = ov - s->GetPageValue();
- break;
-
- case kControlPageDownPart:
- nv = ov + s->GetPageValue();
- break;
- }
-
- if (nv < min) nv = min;
- if (nv > max) nv = max;
- if (nv == ov) return; /* No change */
-
- /*
- * The scrollbar has changed. This sends the appropriate
- * messages
- */
-
- e.oldvalue = ov;
- e.scroll = s;
- s->ReceiveDispatch(KEventPreScroll,s->GetViewID(),(void *)&s);
- ::SetControlValue(s->fControl,nv); /* Change directly */
- s->ReceiveDispatch(KEventScroll,s->GetViewID(),(void *)&e);
- }
-
- #endif
-
- /************************************************************************/
- /* */
- /* Construction */
- /* */
- /************************************************************************/
-
- /* XGStdScroll::Init
- *
- * Initialization central
- */
-
- void XGStdScroll::Init(short page, short min, short max, short val)
- {
- fPageValue = page;
-
- #if OPT_MACOS == 1
- XGDraw draw(this,false);
- Rect r;
-
- /*
- * Create the control at this location
- */
-
- r = GetContentRect();
- ViewToGlobal(&r);
- fControl = NewControl(GetWindow()->_GetGrafPtr(), // window owner
- &r, // where
- "\p", // title
- true, // visible
- val, // initial value
- min, // min value
- max, // max value
- scrollBarProc, // push button proc
- 0); // refcon
- if (fControl == NULL) {
- throw XPostError("Unable to make scroll bar");
- }
-
- if (gProc == NULL) gProc = NewControlActionProc(ActionProc);
- #endif
-
- #if OPT_WINOS == 1
- Rect r;
- HWND w;
- long f;
-
- /*
- * Get the location of the control in the parent's coordinate
- * system
- */
-
- r = GetContentRect();
- if (GetParent()) {
- ViewToGlobal(&r);
- GetParent()->GlobalToView(&r);
- }
-
- /*
- * Create the control window
- */
-
- if (r.right - r.left > r.bottom - r.top) f = SBS_HORZ;
- else f = SBS_VERT;
-
- w = CreateWindow("SCROLLBAR", // control class
- "", // control title
- WS_CHILD | f, // control window flags
- r.left,
- r.top,
- r.right-r.left,
- r.bottom-r.top, // control location
- GetParent()->_GetHWND(), // container window
- NULL, // no menu
- _GInstance, // My app instance
- NULL); // no additional args
- if (w == NULL) {
- throw XPostError("Unable to make scroll bar");
- }
-
- _SetHWND(w); // set me as the window
- SetProp(w,_GViewClass,(HANDLE)this);
-
- SetMinValue(min); // scroll bar parameters
- SetMaxValue(max);
- SetValue(val);
- SetPageValue(fPageValue);
-
- if (IsVisible()) ShowWindow(w,SW_SHOW);
- EnableWindow(w,IsEnabled()); // set me up
- #endif
- }
-
-