Bug 210430 - Shell.setMenuBar() does not work
Summary: Shell.setMenuBar() does not work
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.4   Edit
Hardware: PC Mac OS X
: P3 normal (vote)
Target Milestone: 3.4 M6   Edit
Assignee: Kevin Barnes CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-11-20 13:13 EST by Steve Northover CLA
Modified: 2009-06-04 14:53 EDT (History)
7 users (show)

See Also:


Attachments
loading NIB method (3.37 KB, patch)
2008-02-22 02:15 EST, Sky Yan CLA
Silenio_Quarti: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Steve Northover CLA 2007-11-20 13:13:45 EST
 
Comment 1 Steve Northover CLA 2007-11-20 13:17:19 EST
It doesn't work so Eclipse has no menu bar.  We put some code in place that we thought should work and it doesn't seem to do anything.  It seems amazing to me that menu bars don't work in cocoa, given that cocoa is supposed to replace carbon.  We must be doing something dumb.
Comment 2 Bryan Hunt CLA 2007-11-20 14:37:17 EST
Does this do the trick?

	application.setMainMenu(menuBar.nsMenu);

Comment 3 Steve Northover CLA 2007-11-20 14:44:13 EST
Does it do the trick for you?  I believe we have commented out code in Display.setMenuBar() that already tried this.
Comment 4 Bryan Hunt CLA 2007-11-20 14:48:51 EST
I'm looking for a simple example to test this right now.  

My suggestion was not one of the choices commented out.
Comment 5 Bryan Hunt CLA 2007-11-20 14:51:45 EST
Found the file viewer example and it doesn't seem to work ... bummer
Comment 6 Bryan Hunt CLA 2007-11-20 14:59:41 EST
I debugged a bit further and found that inside Shell.setMenuBar()

display.getActiveShell() == this

returns false so the menu bar is never set on the display.
Comment 7 Steve Northover CLA 2007-11-20 15:33:45 EST
Comment out the test.  Does your API work?
Comment 8 Bryan Hunt CLA 2007-11-20 15:42:35 EST
Sigh, no it doesn't.

Did a little research and found this:

http://lapcatsoftware.com/blog/2007/05/16/working-without-a-nib-part-1/

It seems there are problems running without a nib including setting the main menu.  The article has some suggested hacks around the problem.

Is standalone SWT required on OS X?  Can we use a native launcher to properly initialize the menu bar, or is it time to read about hacking?
Comment 9 Steve Northover CLA 2007-11-20 15:56:35 EST
Let's see what Scott suggests.
Comment 10 Bryan Hunt CLA 2007-11-20 16:32:11 EST
Here's some more interesting info

http://www.cocoabuilder.com/archive/message/cocoa/2007/11/4/192216
Comment 11 Scott Kovatch CLA 2007-11-22 15:13:33 EST
In the AWT we have a bare-bones nib installed in the Java VM framework. It has a basic menu bar with the application menu and a Window menu. Then, when an AWT app starts up, we have an application delegate class that overrides finishLaunching. Inside that we call

[NSBundle loadNibFile:defaultNibFile externalNameTable: [NSDictionary dictionaryWithObject:self forKey:@"NSOwner"] withZone:nil];

That sets up the menu bar for the application. You really don't want to start down the road of using undocumented SPI, even though it's so easy to do in Objective-C. AFAIK, there are no non-nib-based ways to set the main menu in Cocoa, but I will ask -- things may have changed in the 4 years since we moved the AWT to Cocoa.

You can get a simple nib by creating one in Interface Builder. Launch IB, then choose the MainMenu template. Delete all of the other menus except the one named NewApplication and save it. You could also make a copy of the one in /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Resources/English.lproj/DefaultApp.nib.

(I'm technically on vacation this week for US Thanksgiving, so I may not read again until Monday.)
Comment 12 Carolyn MacLeod CLA 2008-01-29 22:39:55 EST
Pinging Scott: Did you have a chance to check with the Cocoa folks to see if there are any non-nib-based ways to set the main menu these days?
Comment 13 Scott Kovatch CLA 2008-01-30 01:05:12 EST
It's not possible to create a Cocoa application without a nib. Recent changes were made that will make it possible, but not on Leopard. Sorry! 

As soon as I get the go-ahead from our lawyers I will be able to do something here.
Comment 14 Scott Kovatch CLA 2008-01-31 12:56:05 EST
See bug #211359. Feel free to use the shared nib file in JavaVM.framework to set up the initial menu bar.
Comment 15 Sky Yan CLA 2008-02-22 02:15:00 EST
Created attachment 90448 [details]
loading NIB method

As Bug 211359 has been fixed with loading NIB method. I create this patch to set menu bar.

In Display.setMenuBar(), it removes all items except application menu item, then add all items of Menu object to menu bar object. There is one thing I found that if the menu bar item has no sub menu, it can not be shown in menu bar. In this situation, I create an empty menu as the sub menu.
Comment 16 Steve Northover CLA 2008-02-22 09:49:06 EST
Carbon had a bug like this one, only it would crash.  Need to check the carbon code to see what we did.
Comment 17 Scott Kovatch CLA 2008-02-22 12:14:55 EST
(In reply to comment #15)

> In Display.setMenuBar(), it removes all items except application menu item,
> then add all items of Menu object to menu bar object. There is one thing I
> found that if the menu bar item has no sub menu, it can not be shown in menu
> bar. In this situation, I create an empty menu as the sub menu.

This is correct. In Cocoa the menu bar is treated like an NSMenu, and each of the top-level menus are NSMenuItems with a sub-menu corresponding to the NSMenu you want to add to the menu bar.

It is an odd way to think about it, but it's all internally consistent and you don't have to introduce the separate notion of a menu bar like you had in Carbon.
Comment 18 Steve Northover CLA 2008-02-22 12:16:43 EST
Not so odd.  Windows (win32) does it this way too.
Comment 19 Scott Kovatch CLA 2008-02-22 12:26:40 EST
Given the NextStep background of Cocoa that doesn't surprise me.
Comment 20 Kevin Barnes CLA 2008-02-25 14:35:08 EST
Thanks again Sky Yan.
fixed > 20080225