Hello, I am trying to build a statusitem component for a Carbon based library. Of course I can only use Cocoa for that, so I need to convert my CFImageRef to a NSImage. Searching in the internet I found code to do that.
I found the following code in the internet: NSImage* image = [[NSImage alloc] initWithSize:rect.size]; [image lockFocus]; CGContextDrawImage([[NSGraphicsContext currentContext] graphicsPort], *(CGRect*)&rect, imageRef); [image unlockFocus]; And I converted and adapted it to Pascal in my full code bellow, but I keep getting an error saying that the context is invalid. Does anyone know why? Here is the debug output: [DBGTGT] :>[TCarbonWSCustomTrayIcon.Show] Jun 5 20:17:03 computador-de-felipe-monteiro-de-carvalho /Users/felipe/Programas/lazarus/examples/trayicon/Wndtray.app/Contents/MacOS/Wndtray: CGContextDrawImage: invalid context [DBGTGT] :<[TCarbonWSCustomTrayIcon.Show] Handle: 01862980 ACGRect.size.width: 2.200000000E+01 ACGRect.size.height: 2.200000000E+01 ACGRect.origin.x: 0.000000000E+00 ACGRect.origin.y: 0.000000000E+00 TCarbonBitmap(ATrayIcon.Icon.Handle).CGImage 01B0BEC0 And my code: class function TCarbonWSCustomTrayIcon.Show(const ATrayIcon: TCustomTrayIcon): Boolean; var APrivateTrayIcon: TPrivateCocoaCarbonTrayIcon; ASize: NSSize; ACGRect: CGRect; AcurrentContext: NSGraphicsContext; begin {$ifdef VerboseCarbonTrayIcon} WriteLn(':>[TCarbonWSCustomTrayIcon.Show]'); {$endif VerboseCarbonTrayIcon} Result := False; { Creates the handle } APrivateTrayIcon := TPrivateCocoaCarbonTrayIcon.Create; APrivateTrayIcon.bar := NSStatusBar.systemStatusBar(); ATrayIcon.Handle := PtrInt(APrivateTrayIcon); { Convert our CFImageRef to a NSImage } ASize.width := TCarbonBitmap(ATrayIcon.Icon.Handle).Width; ASize.height := TCarbonBitmap(ATrayIcon.Icon.Handle).Height; ACGRect.size.width := ASize.width; ACGRect.size.height := ASize.height; ACGRect.origin.x := 0; ACGRect.origin.y := 0; APrivateTrayIcon.image := NSImage.initWithSize(ASize); APrivateTrayIcon.image.lockFocus; AcurrentContext := NSGraphicsContext.currentContext(); CGContextDrawImage(AcurrentContext.graphicsPort, ACGRect, TCarbonBitmap(ATrayIcon.Icon.Handle).CGImage); APrivateTrayIcon.image.unlockFocus; { Shows the icon } if APrivateTrayIcon.item <> nil then Exit(True); APrivateTrayIcon.item := APrivateTrayIcon.bar.statusItemWithLength(NSSquareStatusItemLength); APrivateTrayIcon.item.retain(); APrivateTrayIcon.item.setImage(APrivateTrayIcon.image); Result := True; {$ifdef VerboseCarbonTrayIcon} WriteLn(':<[TCarbonWSCustomTrayIcon.Show]', ' Handle: ', IntToHex(ATrayIcon.Handle, 8), ' ACGRect.size.width: ', ACGRect.size.width, ' ACGRect.size.height: ', ACGRect.size.height, ' ACGRect.origin.x: ', ACGRect.origin.x, ' ACGRect.origin.y: ', ACGRect.origin.y, ' TCarbonBitmap(ATrayIcon.Icon.Handle).CGImage ', IntToHex(Int64(TCarbonBitmap(ATrayIcon.Icon.Handle).CGImage), 8) ); {$endif VerboseCarbonTrayIcon} end; thanks, -- Felipe Monteiro de Carvalho _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]