/// <summary> /// Draws the text. /// </summary> /// <param name="showCursor">Whether or not to show the mouse cursor in the drawing.</param> /// <param name="useTextLayer">Whether or not to use the TextLayer (as opposed to the Userlayer).</param> private void RedrawText(bool showCursor, bool useTextLayer) { Rectangle r = CurrentTextLayout.GetLayoutBounds(); r.Inflate(10 + OutlineWidth, 10 + OutlineWidth); InflateAndInvalidate(r); CurrentTextBounds = r; Rectangle cursorBounds = Rectangle.Zero; Cairo.ImageSurface surf; if (!useTextLayer) { //Draw text on the current UserLayer's surface as finalized text. surf = PintaCore.Workspace.ActiveDocument.Layers.CurrentUserLayer.Surface; } else { //Draw text on the current UserLayer's TextLayer's surface as re-editable text. surf = PintaCore.Workspace.ActiveDocument.Layers.CurrentUserLayer.TextLayer.Layer.Surface; ClearTextLayer(); } using (var g = new Cairo.Context(surf)) { g.Save(); // Show selection if on text layer if (useTextLayer) { // Selected Text Cairo.Color c = new Cairo.Color(0.7, 0.8, 0.9, 0.5); foreach (Rectangle rect in CurrentTextLayout.SelectionRectangles) { g.FillRectangle(rect.ToCairoRectangle(), c); } } if (selection != null) { selection.Clip(g); } g.MoveTo(new Cairo.PointD(CurrentTextEngine.Origin.X, CurrentTextEngine.Origin.Y)); g.SetSourceColor(PintaCore.Palette.PrimaryColor); //Fill in background if (BackgroundFill) { using (var g2 = new Cairo.Context(surf)) { if (selection != null) { selection.Clip(g2); } g2.FillRectangle(CurrentTextLayout.GetLayoutBounds().ToCairoRectangle(), PintaCore.Palette.SecondaryColor); } } // Draw the text if (FillText) { Pango.CairoHelper.ShowLayout(g, CurrentTextLayout.Layout); } if (FillText && StrokeText) { g.SetSourceColor(PintaCore.Palette.SecondaryColor); g.LineWidth = OutlineWidth; Pango.CairoHelper.LayoutPath(g, CurrentTextLayout.Layout); g.Stroke(); } else if (StrokeText) { g.SetSourceColor(PintaCore.Palette.PrimaryColor); g.LineWidth = OutlineWidth; Pango.CairoHelper.LayoutPath(g, CurrentTextLayout.Layout); g.Stroke(); } if (showCursor) { var loc = CurrentTextLayout.GetCursorLocation(); var color = PintaCore.Palette.PrimaryColor; g.Antialias = Cairo.Antialias.None; g.DrawLine(new Cairo.PointD(loc.X, loc.Y), new Cairo.PointD(loc.X, loc.Y + loc.Height), color, 1); cursorBounds = Rectangle.Inflate(loc, 2, 10); } g.Restore(); if (useTextLayer && (is_editing || ctrlKey) && !CurrentTextEngine.IsEmpty()) { //Draw the text edit rectangle. g.Save(); g.Translate(.5, .5); using (Cairo.Path p = g.CreateRectanglePath(CurrentTextBounds.ToCairoRectangle())) { g.AppendPath(p); } g.LineWidth = 1; g.SetSourceColor(new Cairo.Color(1, 1, 1)); g.StrokePreserve(); g.SetDash(new double[] { 2, 4 }, 0); g.SetSourceColor(new Cairo.Color(1, .1, .2)); g.Stroke(); g.Restore(); } } InflateAndInvalidate(PintaCore.Workspace.ActiveDocument.Layers.CurrentUserLayer.previousTextBounds); PintaCore.Workspace.Invalidate(old_cursor_bounds); InflateAndInvalidate(r); PintaCore.Workspace.Invalidate(cursorBounds); old_cursor_bounds = cursorBounds; }
protected override void OnMouseDown(Document document, ToolMouseEventArgs e) { ctrlKey = e.IsControlPressed; //Store the mouse position. Point pt = e.PointDouble.ToGdkPoint(); // Grab focus so we can get keystrokes imContext.FocusIn(); if (selection != null) { selection.Dispose(); } selection = document.Selection.Clone(); // A right click allows you to move the text around if (e.MouseButton == MouseButton.Right) { //The user is dragging text with the right mouse button held down, so track the mouse as it moves. tracking = true; //Remember the position of the mouse before the text is dragged. startMouseXY = e.PointDouble; startClickPoint = clickPoint; //Change the cursor to indicate that the text is being dragged. SetCursor(cursor_hand); return; } // The user clicked the left mouse button if (e.MouseButton == MouseButton.Left) { // If the user is [editing or holding down Ctrl] and clicked //within the text, move the cursor to the click location if ((is_editing || ctrlKey) && CurrentTextBounds.ContainsCorrect(pt)) { StartEditing(); //Change the position of the cursor to where the mouse clicked. TextPosition p = CurrentTextLayout.PointToTextPosition(pt); CurrentTextEngine.SetCursorPosition(p, true); //Redraw the text with the new cursor position. RedrawText(true, true); return; } // We're already editing and the user clicked outside the text, // commit the user's work, and start a new edit switch (CurrentTextEngine.State) { // We were editing, save and stop case TextMode.Uncommitted: StopEditing(true); break; // We were editing, but nothing had been // keyed. Stop editing. case TextMode.Unchanged: StopEditing(false); break; } if (ctrlKey) { //Go through every UserLayer. foreach (UserLayer ul in document.Layers.UserLayers) { //Check each UserLayer's editable text boundaries to see if they contain the mouse position. if (ul.textBounds.ContainsCorrect(pt)) { //The mouse clicked on editable text. //Change the current UserLayer to the Layer that contains the text that was clicked on. document.Layers.SetCurrentUserLayer(ul); //The user is editing text now. is_editing = true; //Set the cursor in the editable text where the mouse was clicked. TextPosition p = CurrentTextLayout.PointToTextPosition(pt); CurrentTextEngine.SetCursorPosition(p, true); //Redraw the editable text with the cursor. RedrawText(true, true); //Don't check any more UserLayers - stop at the first UserLayer that has editable text containing the mouse position. return; } } } else { if (CurrentTextEngine.State == TextMode.NotFinalized) { //The user is making a new text and the old text hasn't been finalized yet. FinalizeText(); } if (!is_editing) { // Start editing at the cursor location clickPoint = pt; CurrentTextEngine.Clear(); UpdateFont(); clickPoint.Offset(0, -CurrentTextLayout.FontHeight / 2); CurrentTextEngine.Origin = clickPoint; StartEditing(); RedrawText(true, true); } } } }
protected override void OnMouseDown(DrawingArea canvas, ButtonPressEventArgs args, Cairo.PointD point) { ctrlKey = (args.Event.State & ModifierType.ControlMask) != 0; //Store the mouse position. Point pt = point.ToGdkPoint(); // Grab focus so we can get keystrokes PintaCore.Chrome.Canvas.GrabFocus(); if (selection != null) { selection.DisposeSelection(); } selection = PintaCore.Workspace.ActiveDocument.Selection.Clone(); // A right click allows you to move the text around if (args.Event.Button == 3) { //The user is dragging text with the right mouse button held down, so track the mouse as it moves. tracking = true; //Remember the position of the mouse before the text is dragged. startMouseXY = point; startClickPoint = clickPoint; //Change the cursor to indicate that the text is being dragged. SetCursor(cursor_hand); return; } // The user clicked the left mouse button if (args.Event.Button == 1) { // If the user is [editing or holding down Ctrl] and clicked //within the text, move the cursor to the click location if ((is_editing || ctrlKey) && CurrentTextBounds.ContainsCorrect(pt)) { StartEditing(); //Change the position of the cursor to where the mouse clicked. Position p = CurrentTextEngine.PointToTextPosition(pt); CurrentTextEngine.SetCursorPosition(p, true); //Redraw the text with the new cursor position. RedrawText(true, true); return; } // We're already editing and the user clicked outside the text, // commit the user's work, and start a new edit switch (CurrentTextEngine.EditMode) { // We were editing, save and stop case EditingMode.Editing: StopEditing(true); break; // We were editing, but nothing had been // keyed. Stop editing. case EditingMode.EmptyEdit: StopEditing(false); break; } if (ctrlKey) { //Go through every UserLayer. foreach (UserLayer ul in PintaCore.Workspace.ActiveDocument.UserLayers) { //Check each UserLayer's editable text boundaries to see if they contain the mouse position. if (ul.textBounds.ContainsCorrect(pt)) { //The mouse clicked on editable text. //Change the current UserLayer to the Layer that contains the text that was clicked on. PintaCore.Workspace.ActiveDocument.SetCurrentUserLayer(ul); //The user is editing text now. is_editing = true; //Set the cursor in the editable text where the mouse was clicked. Position p = CurrentTextEngine.PointToTextPosition(pt); CurrentTextEngine.SetCursorPosition(p, true); //Redraw the editable text with the cursor. RedrawText(true, true); //Don't check any more UserLayers - stop at the first UserLayer that has editable text containing the mouse position. return; } } } else { if (CurrentTextEngine.textMode == TextMode.NotFinalized) { //The user is making a new text and the old text hasn't been finalized yet. FinalizeText(); } if (!is_editing) { // Start editing at the cursor location clickPoint = pt; CurrentTextEngine.Clear(); clickPoint.Offset(0, -CurrentTextEngine.FontHeight / 2); CurrentTextEngine.Origin = clickPoint; StartEditing(); RedrawText(true, true); } } } }