Recent

Author Topic: How Create and show cocoa control inside of TWindowControl?  (Read 3141 times)

Soner

  • Sr. Member
  • ****
  • Posts: 305
How Create and show cocoa control inside of TWindowControl?
« on: February 02, 2018, 12:40:28 am »
Does anyone know how to create and show a cocoa control in TWindowscontrol (website)?
Here is what I did, but it isn`t visible, I think something is missing but what?

The Component:
Code: Pascal  [Select][+][-]
  1. //cocoa-code is from Phils(lazarusforum) webbrowser-example
  2. unit ccbrowser;
  3.  
  4. {$mode objfpc}{$H+}
  5. {$modeswitch objectivec1}
  6.  
  7. interface
  8.  
  9. uses
  10.   Classes, SysUtils,
  11.    Controls, CocoaAll, CocoaPrivate, WebKit;
  12. type
  13.  
  14.   { TWBrowser }
  15.  
  16.   TWBrowser = class(TWinControl)
  17.   private
  18.     fWebView: WebView;
  19.   protected
  20.     procedure CreateWnd; override;
  21.     procedure DestroyWnd; override;
  22.     procedure Resize; override;
  23.   public
  24.     constructor Create(TheOwner: TComponent); override;
  25.     destructor Destroy; override;
  26.     procedure Navigate(AUrl: string);
  27.   end;
  28.  
  29. implementation
  30.  
  31. { TWBrowser }
  32.  
  33. constructor TWBrowser.Create(TheOwner: TComponent);
  34. begin
  35.   inherited Create(TheOwner);
  36.   Color:=$00ffff; //test
  37. end;
  38.  
  39. destructor TWBrowser.Destroy;
  40. begin
  41.   inherited Destroy;
  42. end;
  43.  
  44.  
  45. procedure TWBrowser.CreateWnd;
  46. var aRect: NSRect;
  47. begin
  48.   inherited CreateWnd;
  49.   if (Handle<>0) then begin
  50.     aRect.origin.x:=0;
  51.     aRect.origin.y:=0;
  52.     aRect.size.width:=100;//Width;
  53.     aRect.size.height:=100;//Height;
  54.     fWebView:= WebView(NSView(WebView.alloc).initWithFrame(aRect));
  55.     //I think here is somethin missed to place fwebview in twbrowser-control
  56.   end
  57.   else if Assigned(fWebView) then begin
  58.     NSObject(fWebView).release;
  59.     fWebView:=nil;
  60.   end;
  61. end;
  62.  
  63. procedure TWBrowser.DestroyWnd;
  64. begin
  65.   if Assigned(fWebView) then begin
  66.     NSObject(fWebView).release;
  67.     fWebView:=nil;
  68.   end;
  69.   inherited DestroyWnd;
  70. end;
  71.  
  72. procedure TWBrowser.Resize;
  73. begin
  74.   inherited Resize;
  75.   if Assigned(fWebView) then
  76.     NSObject(fWebView).lclSetFrame(Bounds(0,0,Width,Height));
  77.  
  78. end;
  79.  
  80. procedure TWBrowser.Navigate(AUrl: string);
  81. var aNSURL : NSURL;
  82.     aNSReq : NSURLRequest;
  83. begin
  84.   aNSURL := NSURL.URLWithString(NSSTR(PAnsiChar(AUrl)).
  85.               stringByAddingPercentEscapesUsingEncoding(NSASCIIStringEncoding));
  86.   ANSReq := NSURLRequest.RequestWithURL(ANSURL);
  87.   WebView(fWebView).mainFrame.loadRequest(ANSReq);
  88. end;
  89.  
  90. end.
  91.  

Edit:
the question corrected



« Last Edit: February 02, 2018, 02:19:08 am by Soner »

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: How Create and show cocoa control inside of TWindowControl?
« Reply #1 on: February 02, 2018, 02:11:23 am »
Does anyone know how to create and show a website?

Use the TWebBrowser control:

https://macpgmr.github.io/


Soner

  • Sr. Member
  • ****
  • Posts: 305
Re: How Create and show cocoa control inside of TWindowControl?
« Reply #2 on: February 02, 2018, 02:30:09 am »
Does anyone know how to create and show a website?

Use the TWebBrowser control:

https://macpgmr.github.io/

Sorry, I asked wrong question or I forget to change question in google translate.

I know your component, but it is not what I search, because your control doesn't work for InternetExplorer-Control.
I am creating crossplatform full featured browser-component, also I created QT(QtWebview) and Win32(InternetExplorer) part.
Now I am working on Cocoa-Webview because LCLQt on MacOs does not look good, too much design bugs.
I know good enough about GTK, QT, X, WIN32,  Linux, Windows, C and C++ Programming, but nothing about Mac, Cocoa and ObjectiveC or Objective Pascal.
 
I want only know how to place Cocoa-Controls in LCL-TWinControl. It is easy with QT, GTK or Win32 but with cocoa and mac it is hards.
Thanks again.

 

TinyPortal © 2005-2018