blob: ce8f52cd1da10e0d12ee951364e7712158862d78 [file] [log] [blame]
package org.eclipse.swt.widgets;
/*
* Copyright (c) 2000, 2002 IBM Corp. All rights reserved.
* This file is made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*/
import org.eclipse.swt.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.gtk.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.events.*;
/**
* Instances of this class represent a selectable user interface object
* that issues notification when pressed and released.
* <dl>
* <dt><b>Styles:</b></dt>
* <dd>CHECK, CASCADE, PUSH, RADIO, SEPARATOR</dd>
* <dt><b>Events:</b></dt>
* <dd>Arm, Help, Selection</dd>
* </dl>
* <p>
* Note: Only one of the styles CHECK, CASCADE, PUSH, RADIO, and SEPARATOR
* may be specified.
* </p><p>
* IMPORTANT: This class is <em>not</em> intended to be subclassed.
* </p>
*/
public class MenuItem extends Item {
Menu parent, menu;
int accelerator;
/**
* Constructs a new instance of this class given its parent
* (which must be a <code>Menu</code>) and a style value
* describing its behavior and appearance. The item is added
* to the end of the items maintained by its parent.
* <p>
* The style value is either one of the style constants defined in
* class <code>SWT</code> which is applicable to instances of this
* class, or must be built by <em>bitwise OR</em>'ing together
* (that is, using the <code>int</code> "|" operator) two or more
* of those <code>SWT</code> style constants. The class description
* for all SWT widget classes should include a comment which
* describes the style constants which are applicable to the class.
* </p>
*
* @param parent a composite control which will be the parent of the new instance (cannot be null)
* @param style the style of control to construct
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
* <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
* </ul>
*
* @see SWT
* @see Widget#checkSubclass
* @see Widget#getStyle
*/
public MenuItem (Menu parent, int style) {
super (parent, checkStyle (style));
this.parent = parent;
createWidget (parent.getItemCount ());
}
/**
* Constructs a new instance of this class given its parent
* (which must be a <code>Menu</code>), a style value
* describing its behavior and appearance, and the index
* at which to place it in the items maintained by its parent.
* <p>
* The style value is either one of the style constants defined in
* class <code>SWT</code> which is applicable to instances of this
* class, or must be built by <em>bitwise OR</em>'ing together
* (that is, using the <code>int</code> "|" operator) two or more
* of those <code>SWT</code> style constants. The class description
* for all SWT widget classes should include a comment which
* describes the style constants which are applicable to the class.
* </p>
*
* @param parent a composite control which will be the parent of the new instance (cannot be null)
* @param style the style of control to construct
* @param index the index to store the receiver in its parent
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
* <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
* </ul>
*
* @see SWT
* @see Widget#checkSubclass
* @see Widget#getStyle
*/
public MenuItem (Menu parent, int style, int index) {
super (parent, checkStyle (style));
this.parent = parent;
int count = parent.getItemCount ();
if (!(0 <= index && index <= count)) {
error (SWT.ERROR_ITEM_NOT_ADDED);
}
createWidget (index);
}
void addAccelerator (int accel_group) {
if (accel_group == 0) return;
if (accelerator == 0) return;
byte [] activate = Converter.wcsToMbcs (null, "activate", true);
int mask = 0;
if ((accelerator & SWT.CONTROL) != 0) mask |= OS.GDK_CONTROL_MASK;
if ((accelerator & SWT.ALT) != 0) mask |= OS.GDK_MOD1_MASK;
if ((accelerator & SWT.SHIFT) != 0) mask |= OS.GDK_SHIFT_MASK;
int keysym = accelerator & ~(SWT.ALT | SWT.SHIFT | SWT.CTRL);
int newKey = Display.untranslateKey (keysym);
if (newKey != 0) {
keysym = newKey;
} else {
keysym = wcsToMbcs ((char) keysym);
}
OS.gtk_widget_add_accelerator (handle, activate, accel_group, keysym, mask, OS.GTK_ACCEL_VISIBLE);
}
/**
* Adds the listener to the collection of listeners who will
* be notified when the arm events are generated for the control, by sending
* it one of the messages defined in the <code>ArmListener</code>
* interface.
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see ArmListener
* @see #removeArmListener
*/
public void addArmListener (ArmListener listener) {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
TypedListener typedListener = new TypedListener (listener);
addListener (SWT.Arm, typedListener);
}
/**
* Adds the listener to the collection of listeners who will
* be notified when the help events are generated for the control, by sending
* it one of the messages defined in the <code>HelpListener</code>
* interface.
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see HelpListener
* @see #removeHelpListener
*/
public void addHelpListener (HelpListener listener) {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
TypedListener typedListener = new TypedListener (listener);
addListener (SWT.Help, typedListener);
}
/**
* Adds the listener to the collection of listeners who will
* be notified when the control is selected, by sending
* it one of the messages defined in the <code>SelectionListener</code>
* interface.
* <p>
* When <code>widgetSelected</code> is called, the stateMask field of the event object is valid.
* <code>widgetDefaultSelected</code> is not called.
* </p>
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see SelectionListener
* @see #removeSelectionListener
* @see SelectionEvent
*/
public void addSelectionListener (SelectionListener listener) {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
TypedListener typedListener = new TypedListener(listener);
addListener (SWT.Selection,typedListener);
addListener (SWT.DefaultSelection,typedListener);
}
static int checkStyle (int style) {
return checkBits (style, SWT.PUSH, SWT.CHECK, SWT.RADIO, SWT.SEPARATOR, SWT.CASCADE, 0);
}
void createHandle (int index) {
state |= HANDLE;
byte [] buffer = new byte [1];
int bits = SWT.CHECK | SWT.RADIO | SWT.PUSH | SWT.SEPARATOR;
switch (style & bits) {
case SWT.SEPARATOR:
handle = OS.gtk_menu_item_new ();
break;
case SWT.RADIO:
// handle = OS.gtk_radio_menu_item_new_with_label (0, buffer);
// break;
case SWT.CHECK:
handle = OS.gtk_check_menu_item_new_with_label (buffer);
break;
case SWT.PUSH:
default:
handle = OS.gtk_menu_item_new_with_label (buffer);
break;
}
if (handle == 0) error (SWT.ERROR_NO_HANDLES);
if ((style & (SWT.CHECK | SWT.RADIO)) != 0) {
OS.gtk_check_menu_item_set_show_toggle (handle, true);
}
if ((parent.style & SWT.BAR) != 0) {
OS.gtk_menu_bar_insert (parent.handle, handle, index);
} else {
OS.gtk_menu_insert (parent.handle, handle, index);
}
OS.gtk_widget_show (handle);
}
/**
* Return the widget accelerator. An accelerator is the bit-wise
* OR of zero or more modifier masks and a key. Examples:
* <code>SWT.CONTROL | SWT.SHIFT | 'T', SWT.ALT | SWT.F2</code>.
*
* @return the accelerator
*
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public int getAccelerator () {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
return accelerator;
}
public Display getDisplay () {
Menu parent = this.parent;
if (parent == null) error (SWT.ERROR_WIDGET_DISPOSED);
return parent.getDisplay ();
}
/**
* Returns <code>true</code> if the receiver is enabled, and
* <code>false</code> otherwise. A disabled control is typically
* not selectable from the user interface and draws with an
* inactive or "grayed" look.
*
* @return the receiver's enabled state
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public boolean getEnabled () {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
GtkWidget widget = new GtkWidget ();
OS.memmove (widget, handle, GtkWidget.sizeof);
return (widget.flags & OS.GTK_SENSITIVE) != 0;
}
/**
* Returns the receiver's cascade menu if it has one or null
* if it does not. Only <code>CASCADE</code> menu items can have
* a pull down menu. The sequence of key strokes, button presses
* and/or button releases that are used to request a pull down
* menu is platform specific.
*
* @return the receiver's menu
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public Menu getMenu () {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
return menu;
}
/**
* Returns the receiver's parent, which must be a <code>Menu</code>.
*
* @return the receiver's parent
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public Menu getParent () {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
return parent;
}
/**
* Returns <code>true</code> if the receiver is selected,
* and false otherwise.
* <p>
* When the receiver is of type <code>CHECK</code> or <code>RADIO</code>,
* it is selected when it is checked.
*
* @return the selection state
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public boolean getSelection () {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
if ((style & (SWT.CHECK | SWT.RADIO)) == 0) return false;
GtkCheckMenuItem menuItem = new GtkCheckMenuItem ();
OS.memmove (menuItem, handle, GtkCheckMenuItem.sizeof);
return menuItem.active != 0;
}
void hookEvents () {
super.hookEvents ();
Display display = getDisplay ();
int windowProc2 = display.windowProc2;
byte [] activate_event = Converter.wcsToMbcs (null, "activate", true);
OS.gtk_signal_connect (handle, activate_event, windowProc2, SWT.Selection);
}
/**
* Returns <code>true</code> if the receiver is enabled, and
* <code>false</code> otherwise. A disabled control is typically
* not selectable from the user interface and draws with an
* inactive or "grayed" look.
*
* @return the receiver's enabled state
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public boolean isEnabled () {
return getEnabled ();
}
int processSelection (int int0, int int1, int int2) {
postEvent (SWT.Selection);
return 0;
}
void releaseChild () {
super.releaseChild ();
if (menu != null) menu.dispose ();
menu = null;
}
void releaseWidget () {
if (menu != null) {
menu.releaseWidget ();
menu.releaseHandle ();
}
menu = null;
super.releaseWidget ();
int accel_group = parent.getShell ().accelGroup;
removeAccelerator (accel_group);
accelerator = 0;
parent = null;
}
void removeAccelerator (int accel_group) {
if (accel_group == 0) return;
if (accelerator == 0) return;
int mask = 0;
if ((accelerator & SWT.CONTROL) != 0) mask |= OS.GDK_CONTROL_MASK;
if ((accelerator & SWT.ALT) != 0) mask |= OS.GDK_MOD1_MASK;
if ((accelerator & SWT.SHIFT) != 0) mask |= OS.GDK_SHIFT_MASK;
int keysym = accelerator & ~(SWT.ALT | SWT.SHIFT | SWT.CTRL);
int newKey = Display.untranslateKey (keysym);
if (newKey != 0) {
keysym = newKey;
} else {
keysym = wcsToMbcs ((char) keysym);
}
OS.gtk_widget_remove_accelerator (handle, accel_group, keysym, mask);
accelerator = 0;
}
/**
* Removes the listener from the collection of listeners who will
* be notified when the arm events are generated for the control.
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see ArmListener
* @see #addArmListener
*/
public void removeArmListener (ArmListener listener) {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
if (eventTable == null) return;
eventTable.unhook (SWT.Arm, listener);
}
/**
* Removes the listener from the collection of listeners who will
* be notified when the help events are generated for the control.
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see HelpListener
* @see #addHelpListener
*/
public void removeHelpListener (HelpListener listener) {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
if (eventTable == null) return;
eventTable.unhook (SWT.Help, listener);
}
/**
* Removes the listener from the collection of listeners who will
* be notified when the control is selected.
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see SelectionListener
* @see #addSelectionListener
*/
public void removeSelectionListener (SelectionListener listener) {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
if (eventTable == null) return;
eventTable.unhook (SWT.Selection, listener);
eventTable.unhook (SWT.DefaultSelection,listener);
}
/**
* Sets the widget accelerator. An accelerator is the bit-wise
* OR of zero or more modifier masks and a key. Examples:
* <code>SWT.CONTROL | SWT.SHIFT | 'T', SWT.ALT | SWT.F2</code>.
*
* @param accelerator an integer that is the bit-wise OR of masks and a key
*
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public void setAccelerator (int accelerator) {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
int accel_group = parent.getShell ().accelGroup;
if (accelerator != 0) removeAccelerator (accel_group);
this.accelerator = accelerator;
if (accelerator != 0) addAccelerator (accel_group);
}
/**
* Enables the receiver if the argument is <code>true</code>,
* and disables it otherwise. A disabled control is typically
* not selectable from the user interface and draws with an
* inactive or "grayed" look.
*
* @param enabled the new enabled state
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public void setEnabled (boolean enabled) {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
OS.gtk_widget_set_sensitive (handle, enabled);
}
/**
* Sets the receiver's pull down menu to the argument.
* Only <code>CASCADE</code> menu items can have a
* pull down menu. The sequence of key strokes, button presses
* and/or button releases that are used to request a pull down
* menu is platform specific.
*
* @param menu the new pull down menu
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_MENU_NOT_DROP_DOWN - if the menu is not a drop down menu</li>
* <li>ERROR_MENUITEM_NOT_CASCADE - if the menu item is not a <code>CASCADE</code></li>
* <li>ERROR_INVALID_ARGUMENT - if the menu has been disposed</li>
* <li>ERROR_INVALID_PARENT - if the menu is not in the same widget tree</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public void setMenu (Menu menu) {
checkWidget ();
/* Check to make sure the new menu is valid */
if ((style & SWT.CASCADE) == 0) {
error (SWT.ERROR_MENUITEM_NOT_CASCADE);
}
if (menu != null) {
if ((menu.style & SWT.DROP_DOWN) == 0) {
error (SWT.ERROR_MENU_NOT_DROP_DOWN);
}
if (menu.parent != parent.parent) {
error (SWT.ERROR_INVALID_PARENT);
}
}
/* Assign the new menu */
Menu oldMenu = this.menu;
if (oldMenu == menu) return;
if (oldMenu != null) {
oldMenu.cascade = null;
/*
* Add a reference to the menu we are about
* to replace or GTK will destroy it.
*/
OS.gtk_object_ref (oldMenu.handle);
OS.gtk_menu_item_remove_submenu (handle);
}
if ((this.menu = menu) != null) {
menu.cascade = this;
OS.gtk_menu_item_set_submenu (handle, menu.handle);
}
}
/**
* Sets the selection state of the receiver.
* <p>
* When the receiver is of type <code>CHECK</code> or <code>RADIO</code>,
* it is selected when it is checked.
*
* @param selected the new selection state
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
public void setSelection (boolean selected) {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
if ((style & (SWT.CHECK | SWT.RADIO)) == 0) return;
OS.gtk_signal_handler_block_by_data (handle, SWT.Selection);
OS.gtk_check_menu_item_set_active (handle, selected);
OS.gtk_signal_handler_unblock_by_data (handle, SWT.Selection);
}
public void setText (String string) {
if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED);
if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
if ((style & SWT.SEPARATOR) != 0) return;
text = string;
if ((style & SWT.ARROW) != 0) return;
int length = string.length ();
int index = string.indexOf('\t');
if (index != -1) length = index;
char [] text = new char [length + 1];
char [] pattern = new char [length + 1];
string.getChars (0, length, text, 0);
int i = 0, j = 0;
while (i < length) {
pattern [j] = ' ';
if (text [i] == '&') {
i++;
if (i < length && text [i] != '&') {
pattern [j] = '_';
}
}
text [j++] = text [i++];
}
while (j < i) {
text [j] = pattern [j] = '\0';
j++;
}
int list = OS.gtk_container_children (handle);
int label = OS.g_list_nth_data (list, 0);
byte [] buffer1 = Converter.wcsToMbcs (null, text);
OS.gtk_label_set_text (label, buffer1);
byte [] buffer2 = Converter.wcsToMbcs (null, pattern);
OS.gtk_label_set_pattern (label, buffer2);
}
}