Skip to content

Commit

Permalink
Fixing Poisition for Popup Menus
Browse files Browse the repository at this point in the history
  • Loading branch information
ShahzaibIbrahim committed Aug 28, 2024
1 parent e10a814 commit 7c89f31
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3694,13 +3694,6 @@ public class SWT {
*/
public static final int COLOR_WIDGET_DISABLED_FOREGROUND = 39;

/**
* Default color light blue (value is 40).
*
* @since 3.127
*/
public static final int COLOR_LIGHT_BLUE = 40;

/**
* Draw constant indicating whether the drawing operation
* should fill the background (value is 1<<0).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,6 @@ public Color getSystemColor (int id) {
case SWT.COLOR_DARK_GREEN: pixel = 0x00008000; break;
case SWT.COLOR_YELLOW: pixel = 0x0000FFFF; break;
case SWT.COLOR_DARK_YELLOW: pixel = 0x00008080; break;
case SWT.COLOR_LIGHT_BLUE: pixel = 0x00FF9933; break;
case SWT.COLOR_BLUE: pixel = 0x00E6D8AD; break;
case SWT.COLOR_DARK_BLUE: pixel = 0x00800000; break;
case SWT.COLOR_MAGENTA: pixel = 0x00FF00FF; break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,20 @@ void _setVisible (boolean visible) {
}
}

void _showMenu (boolean visible, MenuItem menuItem) {
long hwndParent = parent.handle;
if (visible) {
Rectangle r = menuItem.getBounds();
Point p = getDisplay().map(parent, null, new Point(r.x, r.y));
boolean success = OS.TrackPopupMenu (handle, 0, p.x, p.y, 0, hwndParent, null);
if (!success && OS.GetMenuItemCount (handle) == 0) {
OS.SendMessage (hwndParent, OS.WM_MENUSELECT, OS.MAKEWPARAM (0, 0xFFFF), 0);
}
} else {
OS.SendMessage (hwndParent, OS.WM_CANCELMODE, 0, 0);
}
}

/**
* Adds the listener to the collection of listeners who will
* be notified when help events are generated for the control,
Expand Down Expand Up @@ -334,10 +348,36 @@ void createHandle () {
updateBackground ();
}

private static boolean altPressed = false;

void createItem (MenuItem item, int index) {
int count = OS.GetMenuItemCount (handle);
if (!(0 <= index && index <= count)) error (SWT.ERROR_INVALID_RANGE);
display.addMenuItem (item);

if((style & SWT.BAR) != 0 && needsMenuCallback()) {
// this.getShell().addKeyListener(new KeyAdapter() {
// @Override
// public void keyPressed(KeyEvent e) {
// int mnemonicIndex = item.text.indexOf('&');
// if (e.keyCode == SWT.ALT) {
// altPressed = true;
// } else if (altPressed && Character.toUpperCase(e.character) == Character.toUpperCase(item.text.charAt(mnemonicIndex + 1))) {
// // Open the menu item
// setFocusOnMnemonic(e, item);
// altPressed = false; // Reset altPressed state
// }
// }
//
// @Override
// public void keyReleased(KeyEvent e) {
// if (e.keyCode == SWT.ALT) {
// altPressed = false;
// }
// }
// });
this.getShell().addTraverseListener(e -> setFocusOnMnemonic(e, item));
}
/*
* Bug in Windows. For some reason, when InsertMenuItem()
* is used to insert an item without text, it is not possible
Expand Down Expand Up @@ -382,6 +422,38 @@ void createItem (MenuItem item, int index) {
redraw ();
}

private void setFocusOnMnemonic(TraverseEvent e, MenuItem menuItem) {
String text = menuItem.text;
if (text != null) {
int mnemonicIndex = text.indexOf('&');
if (e.detail == SWT.TRAVERSE_MNEMONIC) {
char pressedMnemonic = e.character;
if (Character.toUpperCase(pressedMnemonic) == Character.toUpperCase(text.charAt(mnemonicIndex + 1))) {
this.notifyListeners(SWT.Selection, new org.eclipse.swt.widgets.Event());
if (menuItem.getMenu() != null) {
menuItem.getMenu()._showMenu(true, menuItem);
}
}
}
}
}

private void setFocusOnMnemonic(KeyEvent e, MenuItem menuItem) {
String text = menuItem.text;
if (text != null) {
int mnemonicIndex = text.indexOf('&');
//if (e.detail == SWT.TRAVERSE_MNEMONIC) {
char pressedMnemonic = e.character;
if (Character.toUpperCase(pressedMnemonic) == Character.toUpperCase(text.charAt(mnemonicIndex + 1))) {
this.notifyListeners(SWT.Selection, new org.eclipse.swt.widgets.Event());
if (menuItem.getMenu() != null) {
menuItem.getMenu()._showMenu(true, menuItem);
}
}
//}
}
}

void createWidget () {
checkOrientation (parent);
initThemeColors ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class MenuItem extends Item {
/* Image margin. */
final static int MARGIN_WIDTH = 1;
final static int MARGIN_HEIGHT = 1;

final static int LEFT_TEXT_MARGIN = 20;
static {
DPIZoomChangeRegistry.registerHandler(MenuItem::handleDPIChange, MenuItem.class);
}
Expand Down Expand Up @@ -273,9 +273,11 @@ boolean fillAccel (ACCEL accel) {
accel.key = (short) key;
accel.cmd = (short) id;
accel.fVirt = (byte) fVirt;
if ((accelerator & SWT.ALT) != 0) accel.fVirt |= OS.FALT;
if ((accelerator & SWT.SHIFT) != 0) accel.fVirt |= OS.FSHIFT;
if ((accelerator & SWT.CONTROL) != 0) accel.fVirt |= OS.FCONTROL;
if(!parent.needsMenuCallback()) {
if ((accelerator & SWT.ALT) != 0) accel.fVirt |= OS.FALT;
if ((accelerator & SWT.SHIFT) != 0) accel.fVirt |= OS.FSHIFT;
if ((accelerator & SWT.CONTROL) != 0) accel.fVirt |= OS.FCONTROL;
}
return true;
}

Expand Down Expand Up @@ -1138,16 +1140,13 @@ LRESULT wmDrawChild(long wParam, long lParam) {
int flags = SWT.DRAW_MNEMONIC | SWT.DRAW_DELIMITER;
boolean isInactive = ((struct.itemState & OS.ODS_INACTIVE) != 0);
boolean isSelected = ((struct.itemState & OS.ODS_SELECTED) != 0);
int textMargin = 10; // left margin for x-coordinate spacing

Rectangle bounds = this.getBounds();
gc.setForeground(isInactive? display.getSystemColor(SWT.COLOR_GRAY) : display.getSystemColor(SWT.COLOR_WHITE));
gc.setBackground(isSelected? display.getSystemColor(SWT.COLOR_LIGHT_BLUE) : parent.getBackground());
int xMargin = DPIUtil.scaleDown(x + textMargin , zoom) + (this.image != null? this.image.getBounds().width: 0);
gc.setBackground(isSelected? display.getSystemColor(SWT.COLOR_DARK_GRAY) : parent.getBackground());
int xMargin = DPIUtil.scaleDown(x + LEFT_TEXT_MARGIN, zoom) + (this.image != null? this.image.getBounds().width: 0);
int yMargin = DPIUtil.scaleDown(struct.top + MARGIN_HEIGHT, zoom);

gc.fillRectangle( DPIUtil.scaleDown(x, zoom), DPIUtil.scaleDown(struct.top + MARGIN_HEIGHT, zoom), DPIUtil.scaleDown(bounds.width + (image != null ? image.getBounds().width : 0), zoom), DPIUtil.scaleDown(bounds.height, zoom));

gc.drawText(this.text, xMargin, yMargin, flags);
}
if (image != null) {
Expand All @@ -1157,10 +1156,10 @@ LRESULT wmDrawChild(long wParam, long lParam) {
* item is in a menu bar.
*/
Image image = getEnabled() ? this.image : new Image(display, this.image, SWT.IMAGE_DISABLE);

gc.drawImage(image, DPIUtil.scaleDown(x, zoom) , DPIUtil.scaleDown(struct.top + MARGIN_HEIGHT, zoom));
if (this.image != image)
gc.drawImage(image, DPIUtil.scaleDown(x + LEFT_TEXT_MARGIN, zoom) , DPIUtil.scaleDown(struct.top + MARGIN_HEIGHT, zoom));
if (this.image != image) {
image.dispose();
}
}
gc.dispose();
}
Expand Down

0 comments on commit 7c89f31

Please sign in to comment.