[futurebasic] Re: [FB] Make push-button auto-repeat?

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

From: Bernie <fblist.bw@...>
Date: Mon, 3 May 2010 15:51:37 +0100
I wrote:

> rc wrote:
> 
>> I have two controls, kControlPushButtonProc, which I need to somehow give an auto-repeat behavior. (They are Plus and Minus buttons,   like calculator buttons in appearance spaced a few pixels apart ) I am seeking single click behavior when just clicked once, auto-repeat when held down.
>> 
>> I searched all the examples for an auto-repeat like this, but did not find anything. Tried the little arrows process, but that makes a single button into two halves click wise apparently, so I guess that won't work.
> 
> Not much use to you but in Cocoa it is simply a click of a checkbox in IB:

This is of no use to Robert (yet) because it requires OS X 10.5 but posted here for everyone's amusement.
'---------------
/*
   ContinuousButton

   Bernie Wylde 20100503

   Requirements:
   OS X 10.5 or later
   Compile as Objective-C
*/

include "Tlbx HIView.incl"

BeginCFunction
#import <Cocoa/Cocoa.h>

typedef OSStatus (*CallBackType)(HIViewRef);

@interface ContinuousButtonController : NSObjectController {
 NSButton  *button;
 CallBackType _callBack;
 HIViewRef  hiView;
}
@end

static ContinuousButtonController *sContinuousButtonController;

@implementation ContinuousButtonController

- (void)setCallBack:(CallBackType)callBack {
    _callBack = callBack;
}

- (void)buttonPressed:(id)sender {
 if ( _callBack != nil ) (*_callBack)( hiView );
}

- (void)setHIView:(HIViewRef)view {
 hiView = view;
}

- (void)initButtonWithFrame:(NSRect)r title:(NSString *)title hiView:(HIViewRef)view callback:(CallBackType)callBack {
 button = [[NSButton alloc] initWithFrame:r];
 [button setButtonType:NSMomentaryPushInButton];
 [button setBezelStyle:NSRoundedBezelStyle];
 [button setTitle:title];
 [button setContinuous:YES];
 [button setTarget:self];
 [button setAction:@selector(buttonPressed:)];
 [self setHIView:view];
 [self setCallBack:callBack];
 HICocoaViewSetView( view, button );
}
@end

HIViewRef ContinuousButtonInit( WindowRef w, SInt32 id, HIRect r, CFStringRef title, OSStatus (*callBack)(HIViewRef) )
{
 NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
 NSApplicationLoad();
 
 HIObjectRef obj;
 HIViewRef contentView;
 HIViewFindByID( HIViewGetRoot( w ), kHIViewWindowContentID, &contentView );
 HIObjectCreate( CFSTR( "com.apple.HICocoaView" ), 0, &obj );
 HIViewRef view = (HIViewRef)obj;
 HIViewSetFrame( view, &r );
 HIViewAddSubview( contentView, view );
 HIViewSetVisible( view, true );
 HIViewID viewID = {0,id};
 HIViewSetID( view, viewID );
 HIViewSetText( view, title );// for demo only (see fn ContinuousButtonCallback)

 sContinuousButtonController = [[ContinuousButtonController alloc] init];
 [sContinuousButtonController initButtonWithFrame:NSMakeRect( r.origin.x, r.origin.y, r.size.width, r.size.height ) title:(NSString *)title hiView:view callback:callBack];
 
 [localPool release];
 return view;
}
EndC
toolbox fn ContinuousButtonInit( WindowRef w, SInt32 id, HIRect r, CFStringRef title, pointer callback ) = HIViewRef


local fn ContinuousButtonCallback( view as HIViewRef )
'~'1
dim as CFStringRef title
dim as Str255 s

title = fn HIViewCopyText( view )
fn CFStringGetPascalString( title, @s, 256, _kCFStringEncodingMacRoman )
CFRelease( title )
print s;
end fn

dim as HIRect r
dim as HIViewRef view
 
appearance window 1,,(0,0)-(400,400), _kDocumentWindowClass, _kWindowCompositingAttribute

r.origin.x = 20.0 : r.origin.y = 360.0 : r.size.width = 40.0 : r.size.height = 32.0
fn ContinuousButtonInit( window( _wndRef), 1, r, @"+", @fn ContinuousButtonCallback )

r.origin.x = 55.0
fn ContinuousButtonInit( window( _wndRef), 2, r, @"-", @fn ContinuousButtonCallback )

RunApplicationEventLoop()
'---------------

Bernie