Skip to content

Commit

Permalink
Attempt to handle key events on canvas first regardless of focus
Browse files Browse the repository at this point in the history
  • Loading branch information
don-mccomb committed Dec 1, 2012
1 parent a3d31f1 commit 476eb86
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
18 changes: 6 additions & 12 deletions Pinta.Gui.Widgets/Widgets/Canvas/PintaCanvas.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ public PintaCanvas ()
if (PintaCore.Tools.CurrentTool != null) if (PintaCore.Tools.CurrentTool != null)
PintaCore.Tools.CurrentTool.DoMouseMove (sender, e, point); PintaCore.Tools.CurrentTool.DoMouseMove (sender, e, point);
}; };

// Handle key press/release events
KeyPressEvent += new KeyPressEventHandler (PintaCanvas_KeyPressEvent);
KeyReleaseEvent += new KeyReleaseEventHandler (PintaCanvas_KeyReleaseEvent);
} }


#region Protected Methods #region Protected Methods
Expand Down Expand Up @@ -183,14 +179,7 @@ private void SetRequisition (Size size)
QueueResize (); QueueResize ();
} }


[GLib.ConnectBefore] public void DoKeyPressEvent (object o, KeyPressEventArgs e)
private void PintaCanvas_KeyReleaseEvent (object o, KeyReleaseEventArgs e)
{
PintaCore.Tools.CurrentTool.DoKeyRelease (this, e);
}

[GLib.ConnectBefore]
private void PintaCanvas_KeyPressEvent (object o, KeyPressEventArgs e)
{ {
// Give the current tool a chance to handle the key press // Give the current tool a chance to handle the key press
PintaCore.Tools.CurrentTool.DoKeyPress (this, e); PintaCore.Tools.CurrentTool.DoKeyPress (this, e);
Expand All @@ -201,6 +190,11 @@ private void PintaCanvas_KeyPressEvent (object o, KeyPressEventArgs e)
PintaCore.Tools.SetCurrentTool (e.Event.Key); PintaCore.Tools.SetCurrentTool (e.Event.Key);
} }


public void DoKeyReleaseEvent (object o, KeyReleaseEventArgs e)
{
PintaCore.Tools.CurrentTool.DoKeyRelease (this, e);
}

/// <summary> /// <summary>
/// Filters out all modifier keys except Ctrl/Shift/Alt. This prevents Caps Lock, Num Lock, etc /// Filters out all modifier keys except Ctrl/Shift/Alt. This prevents Caps Lock, Num Lock, etc
/// from appearing as active modifier keys. /// from appearing as active modifier keys.
Expand Down
8 changes: 1 addition & 7 deletions Pinta.Tools/Tools/CloneStampTool.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -66,13 +66,7 @@ protected override void OnBuildToolBar(Gtk.Toolbar tb)
base.OnBuildToolBar(tb); base.OnBuildToolBar(tb);


// Change the cursor when the BrushWidth is changed. // Change the cursor when the BrushWidth is changed.
brush_width.ComboBox.Changed += HandleBrushWidthComboBoxChanged; brush_width.ComboBox.Changed += (sender, e) => SetCursor (DefaultCursor);
}

void HandleBrushWidthComboBoxChanged (object sender, EventArgs e)
{
SetCursor (DefaultCursor);
PintaCore.Chrome.Canvas.GrabFocus ();
} }


protected override void OnMouseDown (Gtk.DrawingArea canvas, Gtk.ButtonPressEventArgs args, Cairo.PointD point) protected override void OnMouseDown (Gtk.DrawingArea canvas, Gtk.ButtonPressEventArgs args, Cairo.PointD point)
Expand Down
46 changes: 44 additions & 2 deletions Pinta/MainWindow.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public class MainWindow
ScrolledWindow sw; ScrolledWindow sw;
DockFrame dock; DockFrame dock;
Menu show_pad; Menu show_pad;


CanvasPad canvas_pad;

ActionHandlers dialog_handlers; ActionHandlers dialog_handlers;


public MainWindow () public MainWindow ()
Expand Down Expand Up @@ -80,12 +82,52 @@ public MainWindow ()
window_shell.DeleteEvent += MainWindow_DeleteEvent; window_shell.DeleteEvent += MainWindow_DeleteEvent;
window_shell.DragDataReceived += MainWindow_DragDataReceived; window_shell.DragDataReceived += MainWindow_DragDataReceived;


window_shell.KeyPressEvent += HandleWindowShellKeyPressEvent;
window_shell.KeyReleaseEvent += HandleWindow_shellKeyReleaseEvent;

// TODO: These need to be [re]moved when we redo zoom support // TODO: These need to be [re]moved when we redo zoom support
PintaCore.Actions.View.ZoomToWindow.Activated += new EventHandler (ZoomToWindow_Activated); PintaCore.Actions.View.ZoomToWindow.Activated += new EventHandler (ZoomToWindow_Activated);
PintaCore.Actions.View.ZoomToSelection.Activated += new EventHandler (ZoomToSelection_Activated); PintaCore.Actions.View.ZoomToSelection.Activated += new EventHandler (ZoomToSelection_Activated);
PintaCore.Workspace.ActiveDocumentChanged += ActiveDocumentChanged; PintaCore.Workspace.ActiveDocumentChanged += ActiveDocumentChanged;
} }


void HandleWindowShellKeyPressEvent (object o, KeyPressEventArgs e)
{
// Give the Canvas (and by extension the tools)
// first shot at handling the event if
// the mouse pointer is on the canvas
if (IsMouseOnCanvas())
{
canvas_pad.Canvas.DoKeyPressEvent (o, e);
}
}

void HandleWindow_shellKeyReleaseEvent (object o, KeyReleaseEventArgs e)
{
// Give the Canvas (and by extension the tools)
// first shot at handling the event if
// the mouse pointer is on the canvas
if (IsMouseOnCanvas())
{
canvas_pad.Canvas.DoKeyReleaseEvent (o, e);
}
}

// Check if the mouse pointer is on the canvas
private bool IsMouseOnCanvas()
{
int x = 0;
int y = 0;

// Get the position of the mouse pointer relative
// to canvas scrolled window top-left corner
sw.GetPointer (out x, out y);

// Check if the pointer is on the canvas
return (x > 0) && (x < sw.Allocation.Width) &&
(y > 0) && (y < sw.Allocation.Height);
}

// Called when an extension node is added or removed // Called when an extension node is added or removed
private void OnExtensionChanged (object s, ExtensionNodeEventArgs args) private void OnExtensionChanged (object s, ExtensionNodeEventArgs args)
{ {
Expand Down Expand Up @@ -227,7 +269,7 @@ private void CreateDockAndPads (HBox container)
palettepad.Initialize (dock, show_pad); palettepad.Initialize (dock, show_pad);


// Canvas pad // Canvas pad
var canvas_pad = new CanvasPad (); canvas_pad = new CanvasPad ();
canvas_pad.Initialize (dock, show_pad); canvas_pad.Initialize (dock, show_pad);


sw = canvas_pad.ScrolledWindow; sw = canvas_pad.ScrolledWindow;
Expand Down

0 comments on commit 476eb86

Please sign in to comment.