Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

#315 - Extends NSOutlineView for proper handling mouse events #319

Closed
wants to merge 1 commit into from

2 participants

@Strnadj

Iam referencing my Issue #315 from thursday, I have to fix this issue, cause I need it ;) It's something like "example solution", Iam new in Mono / Mono Mac / c#, but it works... I try to find mouse handler for List / TreeView but I was no able to find it... I have to extend NSOutlineView, like "Hywel Thomas" does it in "WidgetView".

@sevoku
Collaborator

Isn't this the same as #328 but for TreeViews? Could you move the sample from the main window to the TreeViews sample? And it needs a rebase...

@Strnadj

@sevoku Yes its actually is, Iam now in middle of sth, I accidentally deleted my fork of mono/xwt. I will move examples to treeview, rebase it and create new PR, but probably tomorrow..

@sevoku
Collaborator

Sure, no problem! Take the time you need!

@Strnadj

Close this, accidentaly deleted XWT branch...

@Strnadj Strnadj closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
40 TestApps/Samples/MainWindow.cs
@@ -13,7 +13,9 @@ public class MainWindow: Window
VBox sampleBox;
Label title;
Widget currentSample;
-
+ Menu contextMenu;
+ Label treeViewLabel;
+
DataField<string> nameCol = new DataField<string> ();
DataField<Sample> widgetCol = new DataField<Sample> ();
DataField<Image> iconCol = new DataField<Image> ();
@@ -126,8 +128,15 @@ public MainWindow ()
AddSample (null, "Screens", typeof (ScreensSample));
samplesTree.DataSource = store;
-
- box.Panel1.Content = samplesTree;
+
+
+ VBox leftPanel = new VBox ();
+
+ treeViewLabel = new Label ("");
+
+ leftPanel.PackStart (samplesTree, true, true);
+ leftPanel.PackStart (treeViewLabel);
+ box.Panel1.Content = leftPanel;
sampleBox = new VBox ();
title = new Label ("Sample:");
@@ -142,6 +151,31 @@ public MainWindow ()
samplesTree.SelectionChanged += HandleSamplesTreeSelectionChanged;
CloseRequested += HandleCloseRequested;
+
+ // Create context menu
+ contextMenu = new Menu ();
+ contextMenu.Items.Add (new MenuItem ("Context menu #1"));
+ contextMenu.Items.Add (new MenuItem ("Context menu #2"));
+ contextMenu.Items.Add (new MenuItem ("Context menu #3"));
+ samplesTree.ButtonPressed += HandleContextMenu;
+
+ samplesTree.MouseEntered += delegate(object sender, EventArgs e) {
+ treeViewLabel.Text = "Try right-button context menu";
+ };
+ samplesTree.MouseExited += delegate(object sender, EventArgs e) {
+ treeViewLabel.Text = "";
+ };
+ }
+
+ void HandleContextMenu(object sender, ButtonEventArgs e) {
+ if (e.Button == PointerButton.Right) {
+ TreePosition tmpTreePos;
+ RowDropPosition tmpRowDrop;
+ if (samplesTree.GetDropTargetRow (e.X, e.Y, out tmpRowDrop, out tmpTreePos)) {
+ samplesTree.SelectRow (tmpTreePos);
+ contextMenu.Popup (samplesTree, e.X, e.Y);
+ }
+ }
}
void HandleCloseRequested (object sender, CloseRequestedEventArgs args)
View
3  Xwt.Mac/Xwt.Mac.csproj
@@ -9,6 +9,8 @@
<AssemblyName>Xwt.Mac</AssemblyName>
<SuppressXamMacMigration>True</SuppressXamMacMigration>
<UseMSBuildEngine>False</UseMSBuildEngine>
+ <ProductVersion>12.0.0</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>True</DebugSymbols>
@@ -127,6 +129,7 @@
<Compile Include="Xwt.Mac\ScrollControlBackend.cs" />
<Compile Include="Xwt.Mac.CellViews\CellViewBackend.cs" />
<Compile Include="Xwt.Mac\WidgetView.cs" />
+ <Compile Include="Xwt.Mac\OutlineViewBackend.cs" />
</ItemGroup>
<Import Project="..\BuildHelpers.targets" />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
View
152 Xwt.Mac/Xwt.Mac/OutlineViewBackend.cs
@@ -0,0 +1,152 @@
+//
+// OutlineViewBackend.cs
+//
+// Author:
+// Lluis Sanchez <lluis@xamarin.com>
+// Hywel Thomas <hywel.w.thomas@gmail.com>
+// strnadj <jan.strnadek@gmail.com>
+//
+// Copyright (c) 2014 strnadj
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+using System;
+using Xwt.Backends;
+using MonoMac.AppKit;
+using MonoMac.CoreGraphics;
+
+namespace Xwt.Mac
+{
+ public class OutlineViewBackend : NSOutlineView
+ {
+ ITreeViewEventSink eventSink;
+ protected ApplicationContext context;
+ NSTrackingArea trackingArea;
+
+ public OutlineViewBackend (ITreeViewEventSink eventSink, ApplicationContext context)
+ {
+ this.context = context;
+ this.eventSink = eventSink;
+ }
+
+ public NSOutlineView View {
+ get { return this; }
+ }
+
+ public override void UpdateTrackingAreas ()
+ {
+ if (trackingArea != null) {
+ RemoveTrackingArea (trackingArea);
+ trackingArea.Dispose ();
+ }
+ System.Drawing.RectangleF viewBounds = this.Bounds;
+ var options = NSTrackingAreaOptions.MouseMoved | NSTrackingAreaOptions.ActiveInKeyWindow | NSTrackingAreaOptions.MouseEnteredAndExited;
+ trackingArea = new NSTrackingArea (viewBounds, options, this, null);
+ AddTrackingArea (trackingArea);
+ }
+
+ public override void RightMouseDown (NSEvent theEvent)
+ {
+ base.RightMouseUp (theEvent);
+ var p = ConvertPointFromView (theEvent.LocationInWindow, null);
+ ButtonEventArgs args = new ButtonEventArgs ();
+ args.X = p.X;
+ args.Y = p.Y;
+ args.Button = PointerButton.Right;
+ context.InvokeUserCode (delegate {
+ eventSink.OnButtonPressed (args);
+ });
+ }
+
+ public override void RightMouseUp (NSEvent theEvent)
+ {
+ base.RightMouseUp (theEvent);
+ var p = ConvertPointFromView (theEvent.LocationInWindow, null);
+ ButtonEventArgs args = new ButtonEventArgs ();
+ args.X = p.X;
+ args.Y = p.Y;
+ args.Button = PointerButton.Right;
+ context.InvokeUserCode (delegate {
+ eventSink.OnButtonReleased (args);
+ });
+ }
+
+ public override void MouseDown (NSEvent theEvent)
+ {
+ base.MouseDown (theEvent);
+ var p = ConvertPointFromView (theEvent.LocationInWindow, null);
+ ButtonEventArgs args = new ButtonEventArgs ();
+ args.X = p.X;
+ args.Y = p.Y;
+ args.Button = PointerButton.Left;
+ context.InvokeUserCode (delegate {
+ eventSink.OnButtonPressed (args);
+ });
+ }
+
+ public override void MouseUp (NSEvent theEvent)
+ {
+ base.MouseUp (theEvent);
+ var p = ConvertPointFromView (theEvent.LocationInWindow, null);
+ ButtonEventArgs args = new ButtonEventArgs ();
+ args.X = p.X;
+ args.Y = p.Y;
+ args.Button = (PointerButton) theEvent.ButtonNumber + 1;
+ context.InvokeUserCode (delegate {
+ eventSink.OnButtonReleased (args);
+ });
+ }
+
+ public override void MouseEntered (NSEvent theEvent)
+ {
+ base.MouseEntered (theEvent);
+ context.InvokeUserCode (delegate {
+ eventSink.OnMouseEntered ();
+ });
+ }
+
+ public override void MouseExited (NSEvent theEvent)
+ {
+ base.MouseExited (theEvent);
+ context.InvokeUserCode (delegate {
+ eventSink.OnMouseExited ();
+ });
+ }
+
+ public override void MouseMoved (NSEvent theEvent)
+ {
+ base.MouseMoved (theEvent);
+ var p = ConvertPointFromView (theEvent.LocationInWindow, null);
+ MouseMovedEventArgs args = new MouseMovedEventArgs ((long) TimeSpan.FromSeconds (theEvent.Timestamp).TotalMilliseconds, p.X, p.Y);
+ context.InvokeUserCode (delegate {
+ eventSink.OnMouseMoved (args);
+ });
+ }
+
+ public override void MouseDragged (NSEvent theEvent)
+ {
+ base.MouseDragged (theEvent);
+ var p = ConvertPointFromView (theEvent.LocationInWindow, null);
+ MouseMovedEventArgs args = new MouseMovedEventArgs ((long) TimeSpan.FromSeconds (theEvent.Timestamp).TotalMilliseconds, p.X, p.Y);
+ context.InvokeUserCode (delegate {
+ eventSink.OnMouseMoved (args);
+ });
+ }
+ }
+}
+
View
17 Xwt.Mac/Xwt.Mac/TreeViewBackend.cs
@@ -59,7 +59,7 @@ public override void ItemWillExpand (NSNotification notification)
protected override NSTableView CreateView ()
{
- var t = new NSOutlineView ();
+ var t = new OutlineViewBackend (EventSink, ApplicationContext);
t.Delegate = new TreeDelegate () { Backend = this };
return t;
}
@@ -170,8 +170,23 @@ public void ExpandToRow (TreePosition pos)
public bool GetDropTargetRow (double x, double y, out RowDropPosition pos, out TreePosition nodePosition)
{
+ // Get row
+ int row = Tree.GetRow (new System.Drawing.PointF ((float)x, (float)y));
+
+ // Set position
pos = RowDropPosition.Into;
+
+ // Set node position for return
nodePosition = null;
+
+ // Row found
+ if (row >= 0) {
+ // Return item
+ nodePosition = ((TreeItem)Tree.ItemAtRow (row)).Position;
+ return true;
+ }
+
+ // Nothing found
return false;
}
Something went wrong with that request. Please try again.