Cocoa Declares

Hi,

I want to capture the MouseDown event and the Mouse location within the active window, including the Windows ToolBar region.
The normal Window MouseDown event is not fireing if you click the mouse within the ToolBar or the window TitleBar.

I used a MBS Plugin in RS 2012r2, (CarbonWindowEventsMBS) but this Plugin is only working with Carbon builts.

Before I buy another MBS Plugin, I’m wondering if there are Cocoa Declares I can use?

They should capture the MouseDown event and return the Mouse location of the active window.
Thanks for any help

What are you trying to accomplish? What for do you need the mouse click location within the toolbar?

I can’t speak for Hanspeter, but I wanted to get right-clicks (contextual clicks) in the toolbar to implement the “Show/Hide toolbar” contextual menu - as of 10.8, apple removed the “tic tac” control in the upper-right of the window that would show/hide the toolbar. Apple’s strategy seems to be to allow right-clicks in the toolbar as an alternative way to accomplish this.

Sadly, I made a FR for this which was closed.

Hi Lukas,
I’m working on a custom control working and behaves simular like a ComboBox but with a lot of
features not available in the Xojo ComboBox.
To close the popup Listbox I need to capture the MousDown event anywhere on the window.

I made a custom ComboBox under RS 2012r2 / Carbon, which is working as expected - but not under Cocoa.

To Hanspeter: use the MouseExit event of the window, it fires when the mouse leaves the content part of the window.

To Michael:
it seems that you can add your own NSView (or NSControl) to an NSWindow’s -> contentView -> superView. Here is an example I just found through Google (but I didn’t try it):
http://fredandrandall.com/blog/2011/09/14/adding-a-button-or-view-to-the-nswindow-title-bar/
Of course you will have to translate it to Xojo declares.

The MouseExit event is not what I’m looking for.
As with the original ComboBox, the popup Listbox should close only when you click a Mousebutton.

I belive the only way to capture the MouseDown event anywhere on the active window is via Declares.

I was able to use Declares to build a class for animated WindowResizing but I’m struggeling with the MouseDown events.
Any Ideas?

Then close the ComboBox in the Action events of all toolbar items. User can then drag the window with the open ComboBox but as soon as they click on a ToolItem, it will close.

Anything else will get complicated, because NSToolbar is not an NSResponder. You would have to look at NSApplication and hook into the event queue and filter out the mouse clicks for the toolbar (and only the toolbar).

Lukas - any idea how Apple gets mouse clicks in their toolbars? It seems pretty consistent that most (all?) Apple-built apps have a contextual menu to hide (or customize) the toolbar.

  • (void)setAllowsUserCustomization:(BOOL)allowsCustomization

Hmm. I tried it:

  dim h as integer = tb1.Handle
  
  declare sub setAllowsUserCustomization lib "Cocoa" selector "setAllowsUserCustomization:" (NSToolbar as integer, value as boolean)
  setAllowsUserCustomization(h,true)

But it fails, as toolbar.handle is 0 in cocoa, which is a bug I reported over a year ago :frowning:

<https://xojo.com/issue/17116>

[quote=25565:@Michael Diehr]But it fails, as toolbar.handle is 0 in cocoa, which is a bug I reported over a year ago :frowning:

<https://xojo.com/issue/17116>[/quote]

As an easy workaround, you can get the NSToolbar by getting the Toolbar’s Window’s Handle and then declaring to “toolbar”.

Thanks, Joe, but I’m having trouble translating that into code, I tried this :

declare sub setAllowsUserCustomization lib "Cocoa" selector "setAllowsUserCustomization:" (NSToolbar as integer, value as boolean)
dim h as integer = tb1.Window.Handle
setAllowsUserCustomization(h,true)

but it triggers an ObjectiveC exception

Ok, figured it out:

  declare sub setAllowsUserCustomization lib "Cocoa" selector "setAllowsUserCustomization:" (NSToolbar as integer, value as boolean)
  
  dim h as integer = tb1.Handle
  if h = 0 then
    ' due to bug, we get the toolbar using cocoa declares  see [https://xojo.com/issue/17116](https://xojo.com/issue/17116) 
    dim h2 as integer = tb1.Window.handle
    declare function GetNSToolbar lib "Cocoa" selector "toolbar" (NSWindow as integer) as integer
    h = GetNSToolbar(h2)
    setAllowsUserCustomization(h,true)
  else
    setAllowsUserCustomization(h,true)
  end if
   

I’ll add that to the feedback case so others can use it

[quote=25570:@Michael Diehr]Ok, figured it out:

  declare sub setAllowsUserCustomization lib "Cocoa" selector "setAllowsUserCustomization:" (NSToolbar as integer, value as boolean)
  
  dim h as integer = tb1.Handle
  if h = 0 then
    ' due to bug, we get the toolbar using cocoa declares  see [https://xojo.com/issue/17116](https://xojo.com/issue/17116) 
    dim h2 as integer = tb1.Window.handle
    declare function GetNSToolbar lib "Cocoa" selector "toolbar" (NSWindow as integer) as integer
    h = GetNSToolbar(h2)
    setAllowsUserCustomization(h,true)
  else
    setAllowsUserCustomization(h,true)
  end if
   

I’ll add that to the feedback case so others can use it[/quote]

I’m not sure customization will work at all as you want (it’s not supported), but the way to get the handle is good.

The result of setAllowsUserCustomization = true is that right-clicking brings up a menu with the following options:

  • Icon and Text
  • Icon Only
  • Text Only
  • Hide Toolbar
  • Use Small Size
  • Customize Toolbar…

If I could keep everything but “Customize Toolbar…” that would be cool.

In fact, the effect of choosing “Customize Toolbar…” is not that bad, as it gives you only one choice (which is your default set of ToolbarButtons) so it seems relatively harmless. Having the other features work a little more like other OS X cocoa apps is nice, too.

It would. I looked into it for the IDE and there’s no public API for it.

Michael, please take a look at my fork of macoslib here .
It’s a little bit outdated since I no longer have time to maintain it, but here you will find a complete implementation of the NSToolbar using declares.

[quote=25570:@Michael Diehr]Ok, figured it out:

  declare sub setAllowsUserCustomization lib "Cocoa" selector "setAllowsUserCustomization:" (NSToolbar as integer, value as boolean)
  
  dim h as integer = tb1.Handle
  if h = 0 then
    ' due to bug, we get the toolbar using cocoa declares  see [https://xojo.com/issue/17116](https://xojo.com/issue/17116) 
    dim h2 as integer = tb1.Window.handle
    declare function GetNSToolbar lib "Cocoa" selector "toolbar" (NSWindow as integer) as integer
    h = GetNSToolbar(h2)
    setAllowsUserCustomization(h,true)
  else
    setAllowsUserCustomization(h,true)
  end if
   

I’ll add that to the feedback case so others can use it[/quote]
Great!
Using this, is there a way to save the users toolbar settings and use them in the next app launch?
If the user wants only text in the toolbar for example.