Recent

Author Topic: cocoa programming without objective-pascal mode  (Read 5233 times)

bee

  • Sr. Member
  • ****
  • Posts: 393
cocoa programming without objective-pascal mode
« on: November 02, 2014, 02:12:24 pm »
Hi,

Is it possible to do Cocoa GUI programming on Mac/OSX using FreePascal but without objective-pascal mode? Just pure object pascal or delphi mode. How do I access the Cocoa SDK (foundation, appkit, etc) from pascal code? GUI programming without GUI IDE is fine with me as long as I could use Cocoa SDK from pascal. I don't want to use Lazarus since it only supports Carbon (32 bit) which will be deprecated in the next few years.

And, are FreePascal internal libraries (packages) e.g. fcl-web, sql-db, etc. applicable on Mac/OSX/Cocoa platform?

Any hints for those questions are very much appreciated. Thank you.

Regards,

-Bee
-Bee-

A long time pascal lover.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8732
  • FPC developer.
Re: cocoa programming without objective-pascal mode
« Reply #1 on: November 02, 2014, 03:03:18 pm »
COCOA uses objective C and its object model which is based on different principles, so no.

To solve that  Free Pascal went the objective pascal dialect way, Delphi later did it slightly different using a late binding bridge approach based on olevariant iirc. The Delphi way is less intrusive, but more limited (doesn't allow inheritance of derivative classes iirc)

There is a start on Lazarus with COCOA, I'm not sure how active that part of the project is currently. The last that I heard (about an year ago) was that several basic widgets were working.

I'm not aware of any efforts or 3rd party packages to use cocoa not through objective pascal atm.

bee

  • Sr. Member
  • ****
  • Posts: 393
Re: cocoa programming without objective-pascal mode
« Reply #2 on: November 02, 2014, 04:54:36 pm »
I'm not a compiler expert whatsoever. I don't understand what kind of principles that makes FreePascal requires a new strange platform locked-in dialect to work with Cocoa. I don't see this kind of approach on any other languages that I know of.

For example, RemObject's Oxygene (another pascal variant) could work with many different kind of OSes and frameworks without a special dialect for any platforms. Oxygene could work with OSX/Cocoa, iOS/CocoaTouch, Windows/.Net, Linux/Mono, Android/Dalvik, and Java. Though some language features aren't always available on some particular platforms due the platform specific behavior, it doesn't break its general language principles.

If other language could do that, why FreePascal couldn't? I don't really understand. I'd be very grateful if someone could explain it to me. Thank you.
-Bee-

A long time pascal lover.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8732
  • FPC developer.
Re: cocoa programming without objective-pascal mode
« Reply #3 on: November 02, 2014, 06:13:15 pm »
I'm not a compiler expert whatsoever. I don't understand what kind of principles that makes FreePascal requires a new strange platform locked-in dialect to work with Cocoa.

Cocoa is defined purely on objective C level.  The objective C language has an object format that is incompatible with pretty much everything. 

Afaik accessing objects on pure C level is possible by instrumenting the objective C language runtime, but is hard and Objective C runtime version dependent because it bindings are. You'd need header translations (from plain C to Pascal for that of the objective runtime, and helper headers for the frameworks you want to approach). From that interface on plain C level you would then have to rebuild something object oriented.

The objective Pascal support under the hood accesses objective C/Cocoa that way too.  But it is tedious, and if the structure of this kind of interfacing seeps into your code, changes made by Apple (and these happen every year with the new versions of iOS and OS/X, though I understood the severity varies) could possibly break all your code. Objective Pascal is an attempt to abstract that.

Quote
I don't see this kind of approach on any other languages that I know of.

I'm no Maccer anymore, but make sure those actually access Cocoa, and not work via QT (if so, you can use Lazarus via QT too)

Quote
For example, RemObject's Oxygene (another pascal variant) could work with many different kind of OSes and frameworks without a special dialect for any platforms. Oxygene could work with OSX/Cocoa, iOS/CocoaTouch, Windows/.Net, Linux/Mono, Android/Dalvik, and Java.

That is marketing talk that means nothing. All those targets have wildly differing interfacing options, and might use various techniques, interface libraries and other layers to achieve that. It might be as simple as compiling oxygene to either JVM or .NET bytecode depending on target, since they all seem to have that in common.  Totally different world, totally different problems.

Quote
Though some language features aren't always available on some particular platforms due the platform specific behavior, it doesn't break its general language principles.

My guess enter the system from a totally different direction, by being a managed language. That has other difficulties.

I'm not interested enough in Oxygene to try and find out (IMHO it is not Pascal) But if you are happy with Oxygene and don't see limitations why not simply use it?

Quote
If other language could do that, why FreePascal couldn't? I don't really understand. I'd be very grateful if someone could explain it to me. Thank you.

One example doesn't create a pattern, specially if you don't know the details of the example.

Anyway IMHO you have several options, aside from carrying on with Carbon as long as possible (it has been dying for quite some while now, but is still not dead)

- Use QT with Lazarus or standalone. QT interfaces to Cocoa, not native but I'm told fairly decent quality.
- Work on Lazarus/Cocoa
- Try to build something yourself by using objective Pascal only in a few units (to interface Cocoa), and build an own abstraction using small native FPC/Delphi classes lib from that. That's what Lazarus/Cocoa does also, but Lazarus is more complicated due to the LCL-widgetset separation.

bee

  • Sr. Member
  • ****
  • Posts: 393
Re: cocoa programming without objective-pascal mode
« Reply #4 on: November 03, 2014, 02:22:43 am »
Thank you for the explanation, Marco. I really appreciate it.

That is marketing talk that means nothing. All those targets have wildly differing interfacing options, and might use various techniques, interface libraries and other layers to achieve that. It might be as simple as compiling oxygene to either JVM or .NET bytecode depending on target, since they all seem to have that in common. Totally different world, totally different problems.

I know it isn't marketting talk. It does what it said. You better know what you're talking before judging anything. Of course all those targets have wildly differing interfacing options, and might use various techniques, interface libraries, and other layers to achieve that. Isn't that what high level language for? An abstraction language for many different low level techniques. Just like FreePascal can still use the same pascal dialect even for different processor and architecture (which requires different technique on each of them). For Java-based and .Net environment, Oxygene does generate managed intermediate code (CIL or Java byte code) because that's what the target platform requirement. But for Cocoa, they are as native and fast as pure Cocoa code. Oxygene code can even talk to Cocoa-written library/class directly.

I'm not interested enough in Oxygene to try and find out (IMHO it is not Pascal) But if you are happy with Oxygene and don't see limitations why not simply use it?

Why is it always "my way or the highway" attitude on this pascal community? I saw this kind of attitude on Delphi community years ago when I told them about FreePascal. And now, I see this similar attitude on FreePascal community when I tell them about Oxygene. I simply don't understand. :( I brought this discussion to spread new information from the pascal world. I never meant to say that FreePascal is inferior, or Oxygene is superior, or the other way around, far from that. I was like "Hey, look... there's a new kid with a new toy over there! Can't we play along together?"

I've been using pascal for more than 20 years using many kind of pascal language variants. Oxygene's pascal is as pascal as Delphi's pascal or Turbo Pascal's pascal or GNU's pascal can be. Oxygene differences with FreePascal is like the way Delphi is different from Turbo Pascal which Delphi is the more modern pascal variant. They are all pascal language variants after all. There's no true, the one and only, pascal variant. The 'pascal' part of FreePascal name never means it's the true pascal whatsoever. And most --if not all-- (object) pascal language principles and philosophies are still maintained and ensured by Oxygene. It even still supports the old-fashion classic pascal's 'function' and 'procedure' syntax (though isn't encourage, because the new 'method' syntax is more elegant). I dare to say that objective-pascal dialect is the one that is NOT pascal. It's like objective-C spirit in pascal syntax. It looks weird and alien to me.

And yes I'm using Oxygene, although still on testing phase. I accept Oxygene just like the way I accepted Delphi when I used to be a Turbo Pascal programmers many years ago. It's a fresh, new, and modern variant of pascal. I think it's great to all of us (pascal lovers) to have some pascal variants that are being active developed with the same high passion and dedication. We should learn from each other. Don't be arrogant and underestimate the others. If FreePascal could accomodate new Delphi's pascal syntaxes (closures, generics, etc) then why can't it accomodate Oxygene's pascal syntaxes as well?

And yes I'm also still using FreePascal. FreePascal is even still my main pascal variant of choice. But as I got more obstacles to use it on Apple's platform --especially the debugger support and Cocoa access-- I'm considering other alternatives. For now, Oxygene is on my top list. Apple's Swift is on the second rank. :)

One example doesn't create a pattern, specially if you don't know the details of the example.

I mentioned Oxygene simply because I considered it as the closest example to FreePascal. Another example is Delphi. We know it could target OSX, iOS, and .Net without ruining its syntax. FYI, Oxygene was called Delphi Prism --was treated as a Delphi variant-- that at the time only target .Net platform. Then it became Oxygene, a new cross platform pascal language variant. Though I don't know the internal details of Oxygene --as I said I'm not a compiler guy-- but I know how it works.

Anyway IMHO you have several options, aside from carrying on with Carbon as long as possible (it has been dying for quite some while now, but is still not dead)

It's not dead, but is clearly dying. It's like a person with a cancer disease on stadium 4. As Apple goes 64-bit on all of its OSes, Apple officially stated that Carbon is deprecated since OSX Leopard and will be completely obsoleted in the next few years (maybe 3-5 years from now). So, it's not an option for me.

- Use QT with Lazarus or standalone. QT interfaces to Cocoa, not native but I'm told fairly decent quality.

Non native interface isn't an option for me. I'm using FreePascal (and considering Oxygene) because it's native to the target OS. No additional execution layer, no additional interface layer, no additional framework layer, etc. They're as native as the recommended technique by the OS creator. Well, except FreePascal on Cocoa. :D

- Work on Lazarus/Cocoa
- Try to build something yourself by using objective Pascal only in a few units (to interface Cocoa), and build an own abstraction using small native FPC/Delphi classes lib from that. That's what Lazarus/Cocoa does also, but Lazarus is more complicated due to the LCL-widgetset separation.

It requires to use objective-pascal dialect which I hate and don't really understand.

So, my current options are (1) stay with FreePascal with its ugly objective-pascal dialect and limited support for Apple platform; (2) try Oxygene as it's the only closest pascal variant to FreePascal but with better support for Apple platform; or (3) completely move to new Apple's Swift language which I think a lot better than objective-C (which I also hate) but losing the cross platform feature. Well... I don't take any decision yet. :)

Any hint or thought about this is very much welcome and appreciated. Thank you.
-Bee-

A long time pascal lover.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: cocoa programming without objective-pascal mode
« Reply #5 on: November 03, 2014, 03:26:02 pm »
Note/FYI: bee cross posted on FPC mailing list where responses indicated his view of things (i.e. having to use Objective Pascal in OSX development) was a bit one-sided...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

dieselnutjob

  • Full Member
  • ***
  • Posts: 176
Re: cocoa programming without objective-pascal mode
« Reply #6 on: November 03, 2014, 06:37:21 pm »
Programming without Lazarus isn't the same as programming without objective C.

I have also been trying all weekend to compile a Cocoa app without Lazarus but I can't quite do it.  More specifically I can write an app and it runs, but I can't get the window to appear.

This is what I did.

I based the code on this
http://viewsourcecode.org/why/hackety.org/2008/06/25/callingCocoaCommandline.html

Create a folder called Documents\project6.app
Create a folder called Documents\project6.app\Contents
Create a folder called Documents\project6.app\Contents\MacOS
stole an Info.plist file from a Lazarus Carbon project and put it in Documents\project6.app\Contents\Info.plist
edit the Info.plist file and changed the Executable file to project6 and the bundle name to project6

Wrote the following code
Code: [Select]
program project6;
{$mode objfpc}{$H+}
{$modeswitch objectivec2}

uses
  CocoaAll;

var
  myapp: NSApplication;
  mywindow: NSWindow;
  myrect: NSRect;
  myrect2: NSRect;
  mytextfield: NSTextField;
begin
  myapp:=NSApplication.sharedApplication;
  myrect.size.height:=140;
  myrect.size.width:=340;
  myrect2.size.height:=100;
  myrect2.size.width:=260;
  mywindow.initWithContentRect_styleMask_backing_defer(myrect, NSTitledWindowMask OR NSClosableWindowMask OR NSMiniaturizableWIndowMask, NSBackingStoreBuffered, false);
  mytextfield.InitWithFrame(myrect2);
  mywindow.center;
  mywindow.orderFront(nil);
  myapp.run;
end.

compile it
copy the executable to Documents\project6.app\Contents\MacOS\project6

double click on the project6.app icon in Finder

What happens is that the app runs and creates an empty title bar, and you can right click on the app in the dock and quit it

but where is the window?

dieselnutjob

  • Full Member
  • ***
  • Posts: 176
Re: cocoa programming without objective-pascal mode
« Reply #7 on: November 03, 2014, 07:17:35 pm »
I noticed there is a function called NSApplication.mainWindow: NSWindow;
so I tried mywindow:=myapp.mainWindow before teh InitWithContect line
no difference

dieselnutjob

  • Full Member
  • ***
  • Posts: 176
Re: cocoa programming without objective-pascal mode
« Reply #8 on: November 05, 2014, 05:18:37 pm »
The code here
http://www.cocoawithlove.com/2010/09/minimalist-cocoa-programming.html
works.
I will have a go at pascalising it.

 

TinyPortal © 2005-2018