From: Kevin B. <krb...@ma...> - 2005-06-01 16:04:59
|
There's two problems with your code, both in the toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar method. The first is that when the method is called, itemid is an NSCFString, not a Ruby String, and it doesn't get implicitly converted. Just inserting 'itemid = itemid.to_s' will fix that though. (If you step through that method, you might notice that neither branch of the if is being called, because the comparison fails.) The other problem is that you're passing a string to NSToolbarItem#setImage, which expects an NSImage. You can fix this easily: tbi.setImage "NSApplicationIcon" becomes: tbi.setImage( NSImage.imageNamed( "NSApplicationIcon" ) ) and assuming there's an NSApplicationIcon.{tif,icns,...} in your app bundle, it'll work. Hope this helps. Let me know if anything is unclear. Pacem in terris / Mir / Shanti / Salaam / Heiwa Kevin R. Bullock > To: rub...@li... > From: Mark Hubbart <di...@ma...> > Date: Tue, 31 May 2005 07:59:04 -0700 > Subject: [Rubycocoa-talk] anyone work with toolbars? > Reply-To: rub...@li... > > Hi, > The main reasons I want to work with RubyCocoa is the ability to use > all the nifty cocoa features like toolbars, drawers, and sheets. I've > been fighting to figure out the whole toolbar thing, but I've gotten > a bit stuck. I've stuck what I have so far at the end of the message > (edited somewhat for brevity). > > It doesn't work. There is nothing being shown in the toolbar, and it > seems that the toolbaritems I'm creating never get added to the > toolbar; calling @main_window.toolbar.items returns an empty NSArray. > > Has anyone worked with toolbars under RubyCocoa, who could offer any > advice (or sample code!)? I'm a bit stuck here at the moment, so > *any* help would be appreciated. > > Here's the code. The calls to breakpoint() are from the ruby- > breakpoint library, which has helped a lot in figuring out what I > have. > > ----- > class AppController < OSX::NSObject > include OSX > > ib_outlets :main_window > > def awakeFromNib > tb = NSToolbar.alloc.initWithIdentifier "main window toolbar" > tb.setAllowsUserCustomization true > tb.setAutosavesConfiguration true > tb.setDisplayMode NSToolbarDisplayModeIconOnly > > tb.setDelegate self > @main_window.setToolbar tb > > breakpoint > end > > def nullActionOfDoom(*args) > puts "Null action of doom!!!!" > p args > end > > # --- delegate for toolbar > > def toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar > (toolbar, itemid, insert) > if itemid == "install item toolbar item" > tbi = NSToolbarItem.alloc.initWithItemIdentifier itemid > tbi.setLabel "Install" > tbi.setPaletteLabel "Install" > tbi.setToolTip "Install this item" > tbi.setImage "NSApplicationIcon" > > tbi.setTarget self > tbi.setAction :nullActionOfDoom # is this how you replace the > selector call? > > elsif itemid == "remove item toolbar item" > tbi = NSToolbarItem.alloc.initWithItemIdentifier itemid > tbi.setLabel "Remove" > tbi.setPaletteLabel "Remove" > tbi.setToolTip "Remove this item" > tbi.setImage "NSApplicationIcon" > > tbi.setTarget self > tbi.setAction :nullActionOfDoom > end > breakpoint > tbi > end > > def toolbarAllowedItemIdentifiers(toolbar) > breakpoint > ["install item toolbar item", "remove item toolbar item"] > end > > def toolbarDefaultItemIdentifiers(toolbar) > breakpoint > ["install item toolbar item", "remove item toolbar item"] > end > > def toolbarDidRemoveItem(notification) breakpoint end > def toolbarSelectableItemIdentifiers(toolbar) breakpoint end > def toolbarWillAddItem(toolbar) breakpoint end > end |