Detect CTRL+Click on SWT ToolItem - swt

Is there a way to detect a CTRL-click on a ToolItem? I want to distinguish between CTRL+Click and normal mouse click.
ToolBar toolbar= new ToolBar(parent, SWT.NONE);
ToolItem saveToolItem = new ToolItem(toolbar, SWT.PUSH);
...
saveToolItem.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> {
// if CTRL+Click {
// specialSave();
// } else
normalSave();
}));

The SelectionEvent passed to the event (in e in your code) has a stateMask field including the modifier keys being pressed. The SWT.CTRL constant for Ctrl.
So:
if ((e.stateMask & SWT.CTRL) == SWT.CTRL)
Tests for the Ctrl key being pressed

Related

Is there any key binding for press CTRL and Mouse click together?

I have editor.Now I want to enable key binding while press CTRL and Mouse click. Is there any key binding Sequence available in Eclipse RCP.
Mouse clicks don't generate key events so you can use key bindings for this.
If you have a SWT mouse event listener you can test the state of the 'modifier' keys in the listener by checking the stateMask in the MouseEvent.
For example:
#Override
public void mouseDown(MouseEvent event) {
boolean isMod1 = (event.stateMask & SWT.MOD1) != 0;
...
}
SWT.MOD1 is Ctrl on most platforms ('Command' on macOS).

Context menu gets clipped on the right side

I show a context menu on right click but for every second click the right side of the menu gets clipped (about 1 to 2 character width) The basics that I can put here:
void initialise(Handler eventHandler) {
addMenuItem(eventHandler, "New", new NewAction(shell),false);
addMenuItem(eventHandler, "Edit", new EditAction(shell),false);
menuItems.add(new SeparatorMenuItem());
... more items
}
void addMenuItem(Handler eventHandler, String text, Action action, boolean isCheck) {
actions.add(action);
MenuItem it = isCheck ? new CheckMenuItem() : new MenuItem();
it.setText(text);
it.setData(action);
it.setDisable(true);
menuItems.add(it);
}
....
menu = new ContextMenu();
menu.getItems().clear();
menu.getItems().addAll(getMenuItems(getSelection()));
menu.setAutoHide(true);
...
What I've figured out is that it happens on every second right click and although the menu occupy the same rectangle, the drawn part is shifted by 12 pixels, giving the appearance that it's been clipped
You need to set the effect style to null in the code or the css file

How to display a context menu on Gtk.TreeView right click?

I am trying to show a custom context menu when I right click on a row in a TreeView.
treeView.button_press_event.connect ((event) => {
if (event.type == EventType.BUTTON_PRESS && event.button == 3) {
Gtk.Menu menu = new Gtk.Menu ();
Gtk.MenuItem menu_item = new Gtk.MenuItem.with_label ("Add file");
menu.add (menu_item);
menu.show ();
}
});
It doesn't show anything. If I debug a message there I can see the block is being executed when right clicking on a row in the TreeView. I tried show_all () with no success either. popup_at_pointer () is available only on Gtk+ 3.22 and later versions. I am using Gtk+ 3.18.
Is there any way to show a custom menu when right clicking a row on a Gtk.TreeView?
Found out one has to attach the Gtk.Menu to a widget using attach_to_widget () and then use show_all () before calling the only method to show the menu available in Gtk+ 3.18 which is popup (...). popup (...) is deprecated since Gtk+ 3.22, but it is the only method available in Gtk+ 3.18.
Here is the code
treeView.button_press_event.connect ((event) => {
if (event.type == EventType.BUTTON_PRESS && event.button == 3) {
Gtk.Menu menu = new Gtk.Menu ();
Gtk.MenuItem menu_item = new Gtk.MenuItem.with_label ("Add file");
menu.attach_to_widget (treeView, null);
menu.add (menu_item);
menu.show_all ();
menu.popup (null, null, null, event.button, event.time);
}
});
Relevant source: https://developer.gnome.org/gtkmm-tutorial/stable/sec-treeview-examples.html.en#treeview-popup-menu-example
I think you need the older style gtk_menu_popup.

How to add MouseOver handler to a VectorFeature in GWT-Openlayers

I want to show a custom tooltip (popup) when user hovers over a Vector Feature on the GWT-openlayers map. I know that SelectFeature.setHover() will allow me to do this but that will also select the feature which i dont want to have.
it is like, when the user hovers, tooltip must be shown and when he clicks on the feature, then selection muse happen.
how can this be achieved?
Regards
Jatin
Check out this code.
This will also be added to our showcase but we have a little problem with the server at the moment. Will give a shout here when it is uploaded.
Note that the order in which you add the SelectFeature controls is important.
public void buildPanel() {
// create some MapOptions
MapOptions defaultMapOptions = new MapOptions();
defaultMapOptions.setNumZoomLevels(16);
// Create a MapWidget
final MapWidget mapWidget = new MapWidget("500px", "500px",
defaultMapOptions);
// Create an EmptyLayer as base layer
EmptyLayer.Options emptyLayerOptions = new EmptyLayer.Options();
emptyLayerOptions.setAttribution("EmptyLayer (c) GWT-Openlayers");
emptyLayerOptions.setIsBaseLayer(true); // make it a baselayer.
EmptyLayer emptyLayer = new EmptyLayer("Empty layer", emptyLayerOptions);
mapWidget.getMap().addLayer(emptyLayer);
// Add a clickable vectors to the map
// create a layer to add the vectors to
final Vector vectorLayer = new Vector("Vectorlayer");
mapWidget.getMap().addLayer(vectorLayer);
// SelectFeature control to capture clicks on the vectors
final SelectFeature selectFeature = new SelectFeature(vectorLayer);
selectFeature.setAutoActivate(true);
// SelectFeature control to capture hover on the vectors
SelectFeatureOptions selectFeatureHoverOptions = new SelectFeatureOptions();
// use the tempory style to be defined in the StyleMap
selectFeatureHoverOptions.setRenderIntent(RenderIntent.TEMPORARY);
selectFeatureHoverOptions.setHighlightOnly(true);
selectFeatureHoverOptions.setHover();
SelectFeature selectHoverFeature = new SelectFeature(vectorLayer,
selectFeatureHoverOptions);
selectHoverFeature.setClickOut(false);
selectHoverFeature.setAutoActivate(true);
mapWidget.getMap().addControl(selectHoverFeature);
mapWidget.getMap().addControl(selectFeature);
// Define a style for the vectors
Style style = new Style();
style.setFillColor("red");
style.setStrokeColor("green");
style.setStrokeWidth(2);
style.setFillOpacity(0.9);
style.setPointRadius(30);
Style selectedStyle = new Style();
selectedStyle.setFillColor("yellow");
selectedStyle.setStrokeColor("yellow");
selectedStyle.setStrokeWidth(2);
selectedStyle.setFillOpacity(0.9);
selectedStyle.setPointRadius(30);
Style hoverStyle = new Style();
hoverStyle.setFillColor("blue");
hoverStyle.setStrokeColor("pink");
hoverStyle.setStrokeWidth(2);
hoverStyle.setFillOpacity(0.9);
hoverStyle.setPointRadius(30);
StyleMap styleMap = new StyleMap(style, selectedStyle, hoverStyle);
vectorLayer.setStyleMap(styleMap);
// Add a point
Point point = new Point(146.7, -41.8);
final VectorFeature pointFeature = new VectorFeature(point);
vectorLayer.addFeature(pointFeature);
// capture clicks on the vectorlayer
vectorLayer
.addVectorFeatureSelectedListener(new VectorFeatureSelectedListener() {
public void onFeatureSelected(
FeatureSelectedEvent eventObject) {
Window.alert("The vector is now selected.\nIt will get de-selected when closing this popup.");
selectFeature.unSelect(eventObject.getVectorFeature());
}
});
// Attach a popup to the point, we use null as size cause we set
// autoSize to true
// Note that we use FramedCloud... This extends a normal popup and
// creates is styled as a baloon
// We want to display this popup on hover, and hide it when hovering
// ends
final Popup popup = new FramedCloud("id1",
pointFeature.getCenterLonLat(), null,
"<h1>Some popup text</H1><BR/>And more text", null, false);
popup.setPanMapIfOutOfView(true); // this set the popup in a strategic
// way, and pans the map if needed.
popup.setAutoSize(true);
pointFeature.setPopup(popup);
// capture hover by adding a listener to the control, and display the
// popup
selectHoverFeature
.addFeatureHighlightedListener(new FeatureHighlightedListener() {
public void onFeatureHighlighted(VectorFeature vectorFeature) {
mapWidget.getMap().addPopup(vectorFeature.getPopup());
}
});
// capture unhover, and remove popup
selectHoverFeature
.addFeatureUnhighlightedListener(new FeatureUnhighlightedListener() {
public void onFeatureUnhighlighted(
VectorFeature vectorFeature) {
mapWidget.getMap()
.removePopup(vectorFeature.getPopup());
}
});
// Center and zoom to a location
mapWidget.getMap().setCenter(new LonLat(146.7, -41.8), 6);
contentPanel
.add(new HTML(
"<p>This example shows how to add a Vector (point) to map, and do some action when hovering, and another when clicking.</p>"
+ "<p>"
+ "<LI>Hover over the point. This will cause a popup to show, and change the style of the point to the temporary style.</LI>"
+ "<LI>Then when you click the Vector gets selected, gets another style, and a Window.alert is displayed.</LI>"
+ "<LI>When closing the Window.alert, the Vector gets de-selected.</p>"));
contentPanel.add(mapWidget);
initWidget(contentPanel);
mapWidget.getElement().getFirstChildElement().getStyle().setZIndex(0);
}
You must indeed use a SelectFeature to achieve this.
The secret is that you must pass SelectFeatureOptions with highlight only enabled.
Something like
SelectFeatureOptions selectFeatureOptions = new SelectFeatureOptions();
selectFeatureOptions.setHighlightOnly(true);
SelectFeature selectFeature = new SelectFeature(vectorLayer,selectFeatureOptions);

How To Bind Mouse action?

I have Added following code to add Mouse Gestures in command binding but it looks like Keyboard shortcuts are working but Mouse shortcut isn't working:
gestures = new InputGestureCollection
{
new KeyGesture(Key.Enter, ModifierKeys.Control), new MouseGesture(MouseAction.MiddleClick, ModifierKeys.Control)
};
zoomChartToFitCommand = new RoutedUICommand("Zoom To Fit", "zoomChartToFit", typeof(ZoomCommands), gestures);