[Mono-osx] [MonoMac] Add a toolbar programatically

Alain Bocherens alain at slide-effect.com
Thu Jul 7 05:15:38 EDT 2011


Hi guys,

I am trying to add programatically a toolbar to a window using Monomac. 
I don't want to use IB because i am trying to have almost the same code 
on windows and mac.

My code is based on one of the first version of monomac.windows.forms 
wrapper (https://github.com/mcneel/MonoMac.Windows.Form).

Anyway, I tried to add a toolbar to a simple window. The toolbar is 
displayed (but really small) and it display no buttons. If I use the 
customize option I can see the button palette and even drag and drop 
toolbar button on the toolbar (the green (+) symbol appear) but no 
button are added to the toolbar.

Here is a screenshot of what I mean:
http://www.slide-effect.com/tmp/toolbar-mac.png

  Here follow a snippet of my code:

The toolbar class I wrote:
-----------------------------------------------
using System;
using MonoMac.AppKit;
using MonoMac.ObjCRuntime;
using MonoMac.Foundation;
using System.Drawing;
namespace SEForms.Forms
{

     [MonoMac.Foundation.Register("Toolbar")]
     public partial class Toolbar : NSToolbar
     {
         public Toolbar () : base()
         {
             this.WillInsertItem += myinsertItems;
             this.AllowedItemIdentifiers += mytoolbarItems;
             this.DefaultItemIdentifiers += mytoolbarItems;
             this.SelectableItemIdentifiers += mytoolbarItems;

             this.DisplayMode = NSToolbarDisplayMode.IconAndLabel;
             this.Visible = true;
             this.AllowsUserCustomization = true;
             this.ValidateVisibleItems();
         }

         public MonoMac.AppKit.NSToolbarItem myinsertItems 
(MonoMac.AppKit.NSToolbar toolbar, string itemIdentifier, bool 
willBeInserted)
         {
             if(itemIdentifier=="test_tralala")
             {
                 NSToolbarItem item = new NSToolbarItem("test_tralala");
                 item.ToolTip = "ich bin ein Berliner";
                 item.Label = "Test Tralala!";
                 item.PaletteLabel = "Test Tralala!";
                 NSImage img = new NSImage("/Library/Application 
Support/Apple/iChat Icons/Flowers/Ixia.gif");
                 item.Image = img;
                 item.Target = this;
                 item.Enabled = true;
                 return item;
             }
             else
             {
                 return null;
             }
         }

         public string[] mytoolbarItems (MonoMac.AppKit.NSToolbar toolbar)
         {
             string[] v = new string[8];
             v[0] = NSToolbarSeparatorItemIdentifier;
             v[1] = NSToolbarSpaceItemIdentifier;
             v[2] = NSToolbarFlexibleSpaceItemIdentifier;
             v[3] = NSToolbarShowColorsItemIdentifier;
             v[4] = NSToolbarShowFontsItemIdentifier;
             v[5] = NSToolbarCustomizeToolbarItemIdentifier;
             v[6] = NSToolbarPrintItemIdentifier;
             v[7] = "test_tralala";
             return v;
         }



     }
}
-----------------------------------------------


In my NSView:
-----------------------------------------------
Toolbar toolbar = new Toolbar();
this.Toolbar = toolbar;
-----------------------------------------------

You can find the full project here: 
http://www.slide-effect.com/tmp/SEForms.zip

Any idea how to make it work?

thanks.

-- 
Alain Bocherens

Slide Effect :: Create Amazing Presentations
http://www.slide-effect.com
Banner Effect :: Flash Banners Made Simple.
http://www.banner-effect.com



More information about the Mono-osx mailing list