It is currently Thu May 23, 2013 11:24 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: API call help
PostPosted: Tue Aug 09, 2011 12:44 am 
Offline
Enthusiast
Enthusiast

Joined: Thu Jul 02, 2009 5:42 am
Posts: 170
I am trying to call an API (NSOpenPanel) but running into problems.

I want to replace OpenFileRequester with NSOPenPanel. Any API gurus here :D


Top
 Profile  
 
 Post subject: Re: API call help
PostPosted: Tue Aug 09, 2011 10:44 am 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 8876
Location: France
Unfortunately, NSOPenPanel is Cocoa (objectiveC) and this is not accessible trough PB. The 'easier' is your really need it is to write a small wrapper in ObjectiveC which expose a C function and then call it from PB


Top
 Profile  
 
 Post subject: Re: API call help
PostPosted: Tue Aug 09, 2011 4:47 pm 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 1085
Location: Netherlands
Fred is right, it's Cocoa.


Last edited by wilbert on Sun Sep 23, 2012 12:07 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: API call help
PostPosted: Tue Aug 09, 2011 5:05 pm 
Offline
Addict
Addict

Joined: Tue May 06, 2003 5:07 pm
Posts: 2258
Location: UK
By the way... ;)

How would you convert a PB string to a NSString?

This is what I do:
Code:
void function (const char* pbstring)
{
NSString string = [NSString stringWithUTF8String: string];
}


When PB is in non unicode mode, it works if there aren't any special character in the PB String (ie éèë...)
When PB is in unicode mode, it only gets the first letter of the PB string into the NSString.

Any solution? :)


Top
 Profile  
 
 Post subject: Re: API call help
PostPosted: Tue Aug 09, 2011 5:12 pm 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 1085
Location: Netherlands
Polo wrote:
How would you convert a PB string to a NSString?

It's easier to use

NSString * string = [NSString stringWithFormat:@"%S", pbstring];

when unicode is used and

NSString * string = [NSString stringWithFormat:@"%s", pbstring];

when unicode is not used. That way you can use a static variable you set once that contains the format ( @"%S" or @"%s" ) and use the same convert function all the time.


Top
 Profile  
 
 Post subject: Re: API call help
PostPosted: Tue Aug 09, 2011 5:13 pm 
Offline
Addict
Addict

Joined: Tue May 06, 2003 5:07 pm
Posts: 2258
Location: UK
Using Cocoa panels in PB, that's quite easy, just link your app with cocoa

ImportC "/System/Library/Frameworks/Cocoa.framework/Cocoa"
EndImport
ImportC "/System/Library/Frameworks/AppKit.framework/AppKit"
EndImport

(last one is just in case :) )

Create a static library with Xcode with the ObjC code you need, like this for instance, and import the C function in PB.
(you'd still need to handle the string conversion to make the panel customisable, which i didn't in the example below)

Code:
void mOpenFileRequester()
{
NSString *title = "Requester title";
NSString *directory = "";
NSString *file = "";
NSString *extensions = "";

currentwin=[NSApp keyWindow];
if ( !currentwin ) [NSApp activateIgnoringOtherApps:YES];
   
NSOpenPanel *panel=[NSOpenPanel openPanel];
[panel setTitle:title];
      
if ( [panel runModalForDirectory:directory file:file types:extensions]==NSFileHandlingPanelOKButton )
{
  NSString *fileselected=[[panel filename] UTF8String];
}
   
if ( currentwin ) [currentwin makeKeyWindow];
}


Top
 Profile  
 
 Post subject: Re: API call help
PostPosted: Tue Aug 09, 2011 5:20 pm 
Offline
Addict
Addict

Joined: Tue May 06, 2003 5:07 pm
Posts: 2258
Location: UK
Cheers wilbert, works great!


Top
 Profile  
 
 Post subject: Re: API call help
PostPosted: Wed Jan 04, 2012 8:14 pm 
Offline
User
User

Joined: Sat Nov 13, 2010 11:22 pm
Posts: 23
I'd like to do the same (calling a Cocoa API). I need to access CoreMIDI to do some real-time MIDI. I used the WinAPI on Windows but it looks like a different game on MacOS X. Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye