X
2717

How to add an OS native menubar to a top-level base widget?

With IDL it is possible to create an OS native drop down menu bar within a widget dialog. These menu bars give the widget application more of a native look and feel across IDL’s supported platforms. 

Discussion

Windows and UNIX Platforms

To create a menubar at the top of the widget dialog, set the MBAR keyword equal to a named variable when creating the top-level base widget. The menubar is itself a special kind of base widget that can only have WIDGET_BUTTON or CW_PDMENU widgets as children. Upon return, the named variable contains the widget ID of the new menubar base. This widget ID will then be used to as the parent widget ID for WIDGET_BUTTON and/or CW_PDMENU widgets that make up the menubar. The code in the Example Code section below demonstrates how to add such a menubar to a widget. The widget creation commands first create a top-level base (wTLB) with a menubar (mbar), then populates the menubar with two WIDGET_BUTTON widgets (wFile_menu and wHelp_menu) that will form the root buttons of the drop down menus. Then a couple of submenu buttons are added to each of the root buttons. Note, CW_PDMENU could also have been used to construct the pulldown menu.

For additional information, please refer to WIDGET_BASE in the IDL documentation.

Example:

;Create a top-level base and set the MBAR keyword
;   equal to a named variable.
wTLB = WIDGET_BASE(TITLE = 'Example', MBAR=mbar)
  
   ;Add the first button ("File") to the menubar
   ;   Notice that the parent of the first button
   ;   is the mbar variable
   wFile_menu = WIDGET_BUTTON(mbar, VALUE='File', /MENU)
      ;Add sub-buttons to the "File" menu button
      wFile_bttn1=WIDGET_BUTTON(wFile_menu, VALUE='File Sub 1')
      wFile_bttn2=WIDGET_BUTTON(wFile_menu, VALUE='File Sub 2')
  
   ;Add another button to the menubar
   wHelp_menu = WIDGET_BUTTON(mbar, VALUE='Help', /MENU)
      ;Add a sub-button to the second menubar button
      wHelp_bttn1=WIDGET_BUTTON(wHelp_menu, VALUE='Help Sub 1')
      wHelp_bttn2=WIDGET_BUTTON(wHelp_menu, VALUE='Help Sub 2')
  
   ;Add a text widget to the top-level base
   wTextBox = WIDGET_TEXT(wTLB, VALUE = "Sample text.", $
            XSIZE = 30, YSIZE=10, /EDITABLE)

;Realize the widget
WIDGET_CONTROL, wTLB, /REALIZE

;Continue your code...