Recent

Author Topic: Using NSToolbar in Lazarus  (Read 2206 times)

CCRDude

  • Hero Member
  • *****
  • Posts: 602
Using NSToolbar in Lazarus
« on: February 19, 2024, 11:20:18 pm »
Not a question, but some code to offer this time :)

Inspired by a post on a German Lazarus forum, I put some effort into making it easier to use a NSToolbar in Lazarus (e.g. the one with the buttons right next to the window title in the title bar).
It supports standard buttons, button groups, search bars, share and menu items. Images can be used from system default images, images that are part of the bundle, or TImageList (with a few bugs remaining). You can assign actions to toolbar items, or direct captions and titles.

It's missing the -WM13.0 specific centeredItemIdentifiers because I cannot compile for that currently, and making a full TComponent out of it, but otherwise quite usable.

https://gitlab.com/ccrdude-pascal/firefly-macos/-/tree/main/source

dbannon

  • Hero Member
  • *****
  • Posts: 2855
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Using NSToolbar in Lazarus
« Reply #1 on: February 20, 2024, 02:17:04 am »
Hey Dud

Is this something that should be mentioned on https://wiki.freepascal.org/FPC_PasCocoa ?

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Hansaplast

  • Hero Member
  • *****
  • Posts: 674
  • Tweaking4All.com
    • Tweaking4All
Re: Using NSToolbar in Lazarus
« Reply #2 on: February 20, 2024, 10:35:39 am »
Thanks for sharing! Going to give it a try right away!  :)

Hansaplast

  • Hero Member
  • *****
  • Posts: 674
  • Tweaking4All.com
    • Tweaking4All
Re: Using NSToolbar in Lazarus
« Reply #3 on: February 20, 2024, 11:05:35 am »
Unfortunately, Lazarus produces and error when trying to compile FireFlyMacOS:


Code: [Select]
NSGraphicsContext.inc(367,13) Error: Illegal type conversion: "Pobjc_object" to "NSCompositingOperation"

In: ./lazarus/lcl/interfaces/carbon/pascocoa/appkit/NSGraphicsContext.inc


I've tried it with Lazarus from trunck:
Lazarus 3.99 (rev main_3_99-1411-g2c81dff5d9) FPC 3.3.1 aarch64-darwin-cocoa (a few weeks old)
Lazarus 3.99 (rev main_3_99-1502-g05af6f9a4f) FPC 3.3.1 aarch64-darwin-cocoa (most recent)


Any tips how to get this to work? 😊

CCRDude

  • Hero Member
  • *****
  • Posts: 602
Re: Using NSToolbar in Lazarus
« Reply #4 on: February 20, 2024, 12:17:08 pm »
I did have this under some circumstances as well (when trying to crate a package out of it). According to a post here in this forum, it's when an old PasCocoa unit is used?

In my project, I enforce macOS 11 using this option:
Project -> Project Options... -> Compiler Options -> Additions and Overrides -> Add -> Custom Option -> "-WM11.0".

Before adding this, it worked as well, but toolbar needed a different init, so there's one IFDEF in there.

Solving this issue would help me as well, since then I could wrap it into a package.

Or is this error occuring when you try to compile the package? Then it's the same I haven't solved yet.
Manually adding the search path and units to the uses part (Firefly.MacOS.Toolbar and Firefly.MacOS.Toolbar.Items) works without this error though :)

CCRDude

  • Hero Member
  • *****
  • Posts: 602
Re: Using NSToolbar in Lazarus
« Reply #5 on: February 20, 2024, 02:19:29 pm »
Illegal type conversion of "Pobjc_object"

The old version still seems to be shipped as well.

Anyway, I just pushed a new commit where the package compiles for me (macOS Ventura, Lazarus trunk).

Hansaplast

  • Hero Member
  • *****
  • Posts: 674
  • Tweaking4All.com
    • Tweaking4All
Re: Using NSToolbar in Lazarus
« Reply #6 on: February 20, 2024, 07:46:56 pm »
Seems I have to wait a minute or so, since Github is still showing me the old 377 line version of that file. (NSGraphicsContext.inc)
I will try again tomorrow - looking forward to see this work 😁

CCRDude

  • Hero Member
  • *****
  • Posts: 602
Re: Using NSToolbar in Lazarus
« Reply #7 on: February 20, 2024, 11:01:17 pm »
The Lazarus GitHub still has this file, no idea why it's included in Lazarus if it's old and a new one exists?
My message was referring to my GitLab repository, where I updated the package file.

It seems both versions exist:
fpcsrc/packages/cocoaint/src/appkit/NSGraphicsContext.inc (new)
lazarus/lcl/interfaces/carbon/pascocoa/appkit/NSGraphicsContext.inc (old)

Since the second speaks about the Carbon interface, and my project settings refer to the Cocoa interface, how about this change to your project:

Project -> Project Options... -> Compiler Options -> Additions and Overrides -> Add -> IDE Macro: LCLWidgetType:=cocoa

Have you tried the example project?

I posted in the existing thread about this issue...
« Last Edit: February 20, 2024, 11:12:27 pm by CCRDude »

Hansaplast

  • Hero Member
  • *****
  • Posts: 674
  • Tweaking4All.com
    • Tweaking4All
Re: Using NSToolbar in Lazarus
« Reply #8 on: February 24, 2024, 11:52:29 am »

Apologies for the delayed response.


I just tried the latest version you have on Gitlab, and have tried the included example.
I'm no longer getting the compiler error, but the example unfortunately it keeps crashing. Trying to debug didn't show my anything helpful either. Adding the Cocoa widgetset and adding -WM11.0 (which I use for most of my projects now anyway) manually didn't make a difference.
For reference: Lazarus 3.99 (rev main_3_99-1510-g805f05ea99) FPC 3.3.1 aarch64-darwin-cocoa

CCRDude

  • Hero Member
  • *****
  • Posts: 602
Re: Using NSToolbar in Lazarus
« Reply #9 on: February 25, 2024, 08:13:41 pm »
To debug, you probably would have to remove the package from the example code, and add the source files to the project, or at least the source folder to it settings.

Do you mean by "keeps crashing" that the same error in NSGraphicsContext.inc is showing, or something else?

I'm just thinking... I'm not sure if I've embedded the app bundle in the repository, but the example also loads graphical resources there. Will have to check that.

CCRDude

  • Hero Member
  • *****
  • Posts: 602
Re: Using NSToolbar in Lazarus
« Reply #10 on: February 26, 2024, 08:56:40 am »
Just verified that the app bundle was missing in the demo folder in the repository, since I basically had the whole bin folder in the gitignore. My apologies!

Hansaplast

  • Hero Member
  • *****
  • Posts: 674
  • Tweaking4All.com
    • Tweaking4All
Re: Using NSToolbar in Lazarus
« Reply #11 on: February 26, 2024, 03:38:27 pm »
No worries - I'm already grateful somebody is doing an effort to make NSToolbar available in Lazarus 😁
Just downloaded the repository, compiled the package (no errors), but the example still crashes (did a clean+build for the example project).


Just being curious: will this ever become a visual component? Or is that becoming too much of a pain to implement?

CCRDude

  • Hero Member
  • *****
  • Posts: 602
Re: Using NSToolbar in Lazarus
« Reply #12 on: February 26, 2024, 03:54:12 pm »
I've checked out the code, and had an error message about compiling into the specified path, so I had to go into settings and click "create bundle" again, then compile. This works. for me.
Can you pinpoint any further where it crashes? Breakpoint at the TMacToolbar.Attach method begin, going through it?

What macOS version are you using?

Visual component in terms of a WYSIWYG view in the editor, no.
A component that allows to edit items using a non-visual component was my plan. Needs all that streaming stuff for saving component properties, I do not have much experience there, so there's not much more there.
I have another non-visual component in the package that would simply convert the first (or a specified) TToolbar into a NSToolbar where possible.

 

TinyPortal © 2005-2018