[futurebasic] Re: [FB] Drawing Question

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2010 : Group Archive : Group : All Groups

From: Pierre Zippi <paz@...>
Date: Sun, 24 Oct 2010 17:07:22 -0500
There must be a language barrier here...

> From: SVanVoorst <svanvoorst@...>
> Reply-To: <futurebasic@...>
> Date: Sun, 24 Oct 2010 17:26:35 -0400
> To: <futurebasic@...>
> Subject: Re: [FB] Drawing Question
> 
> 
> <<I would like to see a list guru translate this simple example without using
> Quickdraw based graphics. That would help me start the transition to CG.>>
>  
> 
> /*
>   Creates a moveable alignment line drawn into a user pane.
>   Written by S.Van Voorst
> */
> 
> 
> Include "Tlbx CoreGraphics.Incl"
> Include "Tlbx HIView.Incl"
> 
> 
> begin enum 1'Control constants
> _pane
> end enum
> 
> 
> _nLengths = 2'For dashed line
> 
> 
> _wndW = 500'Window width
> _wndH = 400'Window height
> 
> 
> begin globals
> dim as short gMenuBarH'Menu Bar height
> dim as HIPoint   gDrag'Drag point
> dim as boolean gMouseDown, gMouseDragged
> end globals
> 
> 
> local fn WindowEventHandler( nextHandler as EventHandlerCallRef, ¬
> theEvent as EventRef, userData as Ptr ) as OSStatus
> '~'1
> dim as WindowRef   @ w
> dim as UInt32       eventClass, eventKind
> dim as OSStatus      result, ignore, err
> dim as HIPoint    @ pt
> 
> 
> result = _eventNotHandledErr
> 
> 
> eventKind  = fn GetEventKind( theEvent )
> eventClass  =  fn GetEventClass( theEvent )
> 
> 
> select eventClass
> 
> 
> case _kEventClassWindow
> 
> 
> ignore = fn GetEventParameter( theEvent, _kEventParamDirectObject, ¬
> _typeWindowRef, #0, sizeof( WindowRef ), #0, @w )
> 
> 
> select eventKind
> 
> 
> case _kEventWindowActivated
> 
> 
> case _kEventWindowClose
> end
> 
> 
> end select
> 
> 
> case _kEventClassMouse
> 
> 
> ignore = fn GetEventParameter( theEvent, _kEventParamWindowMouseLocation, ¬
> _typeHIPoint, #0, sizeof( pt ), #0, @pt )
> 
> 
> //Mouse coordinates start at top of window, not top of user pane.
> //Therefore, must subtract width of menuBar from "y" mouse coordinate value
> 
> 
> pt.y -= gMenuBarH'pt.y = pt.y - (gMenuBarH )
> 
> 
> select eventKind
> 
> 
> case _kEventMouseDown
> 
> 
> gMouseDown = _true
> gDrag = pt
> err = fn HIViewSetNeedsDisplay( button&(_pane), _true)
> 
> 
> case _kEventMouseUp
> // Reset flags
> gMouseDragged = _false : gMouseDown = _false
> err = fn HIViewSetNeedsDisplay( button&(_pane), _true)
> 
> 
> case _kEventMouseDragged
> 
> 
> gMouseDragged = _true
> gDrag = pt
> 
> 
> long if gMouseDown
> err = fn HIViewSetNeedsDisplay( button&(_pane), _true)
> end if
> 
> 
> end select
> 
> 
> end select
> 
> 
> end fn = result
> 
> 
> local fn InstallWindowHandler( w as WindowRef )
> '~'1
> _nWindEventKinds = 5
> 
> 
> dim as EventTypeSpec  events(_nWindEventKinds - 1)
> 
> 
> events.eventClass(0) = _kEventClassWindow
> events.eventKind(0)  = _kEventWindowActivated
> 
> 
> events.eventClass(1) = _kEventClassWindow
> events.eventKind(1)  = _kEventWindowClose
> 
> 
> events.eventClass(2) = _kEventClassMouse
> events.eventKind(2)  = _kEventMouseDown
> 
> 
> events.eventClass(3) = _kEventClassMouse
> events.eventKind(3)  = _kEventMouseUp
> 
> 
> events.eventClass(4) = _kEventClassMouse
> events.eventKind(4)  = _kEventMouseDragged
> 
> 
> end fn = fn InstallEventHandler( fn GetWindowEventTarget( w ), @fn
> WindowEventHandler, ¬
> _nWindEventKinds, @events(0), #0, #0 )
> 
> 
> 
> 
> local fn PaneDrawHandler( nextHandler as EventHandlerCallRef, ¬
> theEvent as EventRef, userData as Ptr ) as OSStatus
> '~'1
> dim as ControlRef   @ c
> dim as CGContextRef  @ ctx
> dim as OSStatus       ignore
> dim as HIRect         bounds
> dim as CGRect    rect1, rect2
> dim as single lengths(_nLengths - 1)
> 
> 
> // Create ContextRef
> ignore = fn GetEventParameter( theEvent, _kEventParamCGContextRef, ¬
> _typeCGContextRef, #0, sizeof( CGContextRef ), #0, @ctx )
> 
> 
> // get the ControlRef
> ignore = fn GetEventParameter( theEvent, _kEventParamDirectObject, ¬
> _typeControlRef, #0, sizeof( c ), #0, @c )
> 
> 
> // get the view-centric bounds of the control
> ignore = fn HIViewGetBounds( c, @bounds )
> 
> 
> // Puts blue border around user pane
> CGContextSetLineWidth( ctx, 4.0 )
> CGContextSetRGBStrokeColor( ctx, 0.0, 0.0, 1.0, 1.0 )
> CGContextStrokeRect( ctx, bounds )
> 
> 
> // ** CTM was neither scaled nor translated ** //
> 
> 
> CGContextSaveGState( ctx )
> 
> 
> rect1 = fn CGRectMake( 80, 30, 50, 50)
> CGContextSetRGBStrokeColor( ctx, 0.0, 0, 0, 1 )
> CGContextFillRect( ctx, rect1 )
> 
> 
> rect2 = fn CGRectMake( 80, 130, 50, 50)
> CGContextSetRGBStrokeColor( ctx, 0.0, 0, 0, 1 )
> CGContextFillRect( ctx, rect2 )
> 
> 
> long if gMouseDown or gMouseDragged
> CGContextMoveToPoint( ctx, gDrag.x , _wndH )
> CGContextAddLineToPoint( ctx, gDrag.x, 0.0 )
> CGContextSetLineWidth( ctx, 1.5 )
> // Dashed line
> lengths(0) = 6 : lengths(1) = 3
> CGContextSetLineDash( ctx, 0.0, @lengths(0), _nLengths )
> CGContextSetRGBStrokeColor( ctx, 1.0, 0.0, 0.0, 1.0 )
> CGContextStrokePath( ctx )
> end if
> 
> 
> CGContextRestoreGState( ctx )
> 
> 
> end fn = _noErr
> 
> 
> local fn InstallPaneDrawHandler( c as ControlRef )
> '~'1
> dim as EventTypeSpec  events
> 
> 
> events.eventClass = _kEventClassControl
> events.eventKind  = _kEventControlDraw
> 
> 
> end fn = fn InstallEventHandler( fn GetControlEventTarget( c ), @fn
> PaneDrawHandler, 1, @events, #0, #0 )
> 
> 
> local fn BuildWindow
> '~'1
> dim as Rect        r
> dim as WindowRef @ wRef
> dim as WindowAttributes  attr
> dim as short mBarH
> dim as OSStatus err
> 
> 
> attr  = _kWindowStandardFloatingAttributes
> attr += _kWindowStandardHandlerAttribute
> attr += _kWindowCompositingAttribute
> 
> 
> // ----- Initialize flags ----- //
> gMouseDragged = _false
> gMouseDown = _false
> 
> 
> SetRect( r, 0, 0, _wndW, _wndH )
> appearance window -1, "Mouse down for line",@r, _kDocumentWindowClass, attr
> 
> 
> get window 1, wRef
> fn InstallWindowHandler( wRef )'Intercept some window CarbonEvents
> 
> 
> err = fn GetThemeMenuBarHeight(mBarH)
> gMenuBarH = mBarH'To be used later to calculate "y" coord.
> 
> 
> SetRect( r, 0, 0, _wndW, _wndH )
> appearance Button _pane,,,,,,@r,_kControlUserPaneProc
> fn InstallPaneDrawHandler(  button&(_pane) )
> 
> 
> appearance window  1
> 
> 
> end fn
> 
> 
> fn BuildWindow
> 
> 
> do
> HandleEvents
> until (gFBQuit)
> 
> 
> 
> 
> 
> 
> 
> 
> 
>  
> 
> --
> To unsubscribe, send ANY message to: futurebasic-unsubscribe@associate
> .com
>