public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace) { CompoundHistoryMemento chm = new CompoundHistoryMemento(this.historyName, this.undoImage); if (!historyWorkspace.Selection.IsEmpty) { DeselectFunction df = new DeselectFunction(); EnterCriticalRegion(); HistoryMemento hm = df.Execute(historyWorkspace); chm.PushNewAction(hm); } FlipLayerHistoryMemento flha = new FlipLayerHistoryMemento( null, null, historyWorkspace, this.layerIndex, this.flipType); EnterCriticalRegion(); HistoryMemento flha2 = flha.PerformUndo(); chm.PushNewAction(flha); return chm; }
protected override HistoryMemento OnUndo() { List<HistoryMemento> redoActions = new List<HistoryMemento>(actions.Count); for (int i = 0; i < actions.Count; ++i) { HistoryMemento ha = actions[actions.Count - i - 1]; HistoryMemento rha = null; if (ha != null) { rha = ha.PerformUndo(); } redoActions.Add(rha); } CompoundHistoryMemento cha = new CompoundHistoryMemento(Name, Image, redoActions); return cha; }
public override HistoryMemento PerformAction(DocumentWorkspace documentWorkspace) { HistoryMemento hm = null; int index = documentWorkspace.ActiveLayerIndex; if (index != documentWorkspace.Document.Layers.Count - 1) { SwapLayerFunction slf = new SwapLayerFunction(index, index + 1); HistoryMemento slfhm = slf.Execute(documentWorkspace); hm = new CompoundHistoryMemento( StaticName, StaticImage, new HistoryMemento[] { slfhm }); documentWorkspace.ActiveLayer = (Layer)documentWorkspace.Document.Layers[index + 1]; } return hm; }
protected override HistoryMemento OnUndo() { List <HistoryMemento> redoActions = new List <HistoryMemento>(actions.Count); for (int i = 0; i < actions.Count; ++i) { HistoryMemento ha = actions[actions.Count - i - 1]; HistoryMemento rha = null; if (ha != null) { rha = ha.PerformUndo(); } redoActions.Add(rha); } CompoundHistoryMemento cha = new CompoundHistoryMemento(Name, Image, redoActions); return(cha); }
public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace) { if (this.layerIndex < 1 || this.layerIndex >= historyWorkspace.Document.Layers.Count) { throw new ArgumentException("layerIndex must be greater than or equal to 1, and a valid layer index. layerIndex=" + layerIndex + ", allowableRange=[0," + historyWorkspace.Document.Layers.Count + ")"); } int bottomLayerIndex = this.layerIndex - 1; Rectangle bounds = historyWorkspace.Document.Bounds; PdnRegion region = new PdnRegion(bounds); BitmapHistoryMemento bhm = new BitmapHistoryMemento( null, null, historyWorkspace, bottomLayerIndex, region); BitmapLayer topLayer = (BitmapLayer)historyWorkspace.Document.Layers[this.layerIndex]; BitmapLayer bottomLayer = (BitmapLayer)historyWorkspace.Document.Layers[bottomLayerIndex]; RenderArgs bottomRA = new RenderArgs(bottomLayer.Surface); EnterCriticalRegion(); topLayer.Render(bottomRA, region); bottomLayer.Invalidate(); bottomRA.Dispose(); bottomRA = null; region.Dispose(); region = null; DeleteLayerFunction dlf = new DeleteLayerFunction(this.layerIndex); HistoryMemento dlhm = dlf.Execute(historyWorkspace); CompoundHistoryMemento chm = new CompoundHistoryMemento(StaticName, StaticImage, new HistoryMemento[] { bhm, dlhm }); return chm; }
public void PerformAction(DocumentWorkspace documentWorkspace) { HistoryMemento finalHM; if (documentWorkspace.Selection.IsEmpty) { finalHM = null; } else { CopyToClipboardAction ctca = new CopyToClipboardAction(documentWorkspace); bool result = ctca.PerformAction(); if (!result) { finalHM = null; } else { using (new PushNullToolMode(documentWorkspace)) { EraseSelectionFunction esa = new EraseSelectionFunction(); HistoryMemento hm = esa.Execute(documentWorkspace); CompoundHistoryMemento chm = new CompoundHistoryMemento( StaticName, StaticImage, new HistoryMemento[] { hm }); finalHM = chm; } } } if (finalHM != null) { documentWorkspace.History.PushNewMemento(finalHM); } }
public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace) { if (historyWorkspace.Selection.IsEmpty) { return null; } SelectionHistoryMemento shm = new SelectionHistoryMemento(string.Empty, null, historyWorkspace); PdnRegion region = historyWorkspace.Selection.CreateRegion(); BitmapLayer layer = ((BitmapLayer)historyWorkspace.ActiveLayer); PdnRegion simplifiedRegion = Utility.SimplifyAndInflateRegion(region); HistoryMemento hm = new BitmapHistoryMemento( null, null, historyWorkspace, historyWorkspace.ActiveLayerIndex, simplifiedRegion); HistoryMemento chm = new CompoundHistoryMemento( StaticName, StaticImage, new HistoryMemento[] { shm, hm }); EnterCriticalRegion(); layer.Surface.Clear(region, ColorBgra.FromBgra(255, 255, 255, 0)); layer.Invalidate(simplifiedRegion); historyWorkspace.Document.Invalidate(simplifiedRegion); simplifiedRegion.Dispose(); region.Dispose(); historyWorkspace.Selection.Reset(); return chm; }
public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace) { object savedSelection = null; List<HistoryMemento> actions = new List<HistoryMemento>(); if (!historyWorkspace.Selection.IsEmpty) { savedSelection = historyWorkspace.Selection.Save(); DeselectFunction da = new DeselectFunction(); HistoryMemento hm = da.Execute(historyWorkspace); actions.Add(hm); } ReplaceDocumentHistoryMemento rdha = new ReplaceDocumentHistoryMemento(null, null, historyWorkspace); actions.Add(rdha); CompoundHistoryMemento chm = new CompoundHistoryMemento( StaticName, PdnResources.GetImageResource("Icons.MenuImageFlattenIcon.png"), actions); // TODO: we can save memory here by serializing, then flattening on to an existing layer Document flat = historyWorkspace.Document.Flatten(); EnterCriticalRegion(); historyWorkspace.Document = flat; if (savedSelection != null) { SelectionHistoryMemento shm = new SelectionHistoryMemento(null, null, historyWorkspace); historyWorkspace.Selection.Restore(savedSelection); chm.PushNewAction(shm); } return chm; }
public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace) { int newWidth; int newHeight; // Get new width and Height switch (rotation) { case RotateType.Clockwise90: case RotateType.Clockwise270: case RotateType.CounterClockwise90: case RotateType.CounterClockwise270: newWidth = historyWorkspace.Document.Height; newHeight = historyWorkspace.Document.Width; break; case RotateType.Clockwise180: case RotateType.CounterClockwise180: newWidth = historyWorkspace.Document.Width; newHeight = historyWorkspace.Document.Height; break; default: throw new InvalidEnumArgumentException("invalid RotateType"); } // Figure out which icon and text to use string iconResName; string suffix; switch (rotation) { case RotateType.Clockwise180: iconResName = "Icons.MenuImageRotate180CWIcon.png"; suffix = PdnResources.GetString("RotateAction.180CW"); break; case RotateType.Clockwise270: iconResName = "Icons.MenuImageRotate270CWIcon.png"; suffix = PdnResources.GetString("RotateAction.270CW"); break; case RotateType.Clockwise90: iconResName = "Icons.MenuImageRotate90CWIcon.png"; suffix = PdnResources.GetString("RotateAction.90CW"); break; case RotateType.CounterClockwise180: iconResName = "Icons.MenuImageRotate180CCWIcon.png"; suffix = PdnResources.GetString("RotateAction.180CCW"); break; case RotateType.CounterClockwise270: iconResName = "Icons.MenuImageRotate270CCWIcon.png"; suffix = PdnResources.GetString("RotateAction.270CCW"); break; case RotateType.CounterClockwise90: iconResName = "Icons.MenuImageRotate90CCWIcon.png"; suffix = PdnResources.GetString("RotateAction.90CCW"); break; default: throw new InvalidEnumArgumentException("invalid RotateType"); } // Initialize the new Doc string haNameFormat = PdnResources.GetString("RotateAction.HistoryMementoName.Format"); string haName = string.Format(haNameFormat, StaticName, suffix); ImageResource haImage = ImageResource.Get(iconResName); List<HistoryMemento> actions = new List<HistoryMemento>(); // do the memory allocation now: if this fails, we can still bail out cleanly Document newDoc = new Document(newWidth, newHeight); if (!historyWorkspace.Selection.IsEmpty) { DeselectFunction da = new DeselectFunction(); EnterCriticalRegion(); HistoryMemento hm = da.Execute(historyWorkspace); actions.Add(hm); } ReplaceDocumentHistoryMemento rdha = new ReplaceDocumentHistoryMemento(null, null, historyWorkspace); actions.Add(rdha); newDoc.ReplaceMetaDataFrom(historyWorkspace.Document); // TODO: serialize oldDoc to disk, and let the GC purge it if needed OnProgress(0.0); for (int i = 0; i < historyWorkspace.Document.Layers.Count; ++i) { Layer layer = historyWorkspace.Document.Layers.GetAt(i); double progressStart = 100.0 * ((double)i / (double)historyWorkspace.Document.Layers.Count); double progressEnd = 100.0 * ((double)(i + 1) / (double)historyWorkspace.Document.Layers.Count); if (layer is BitmapLayer) { Layer nl = RotateLayer((BitmapLayer)layer, rotation, newWidth, newHeight, progressStart, progressEnd); newDoc.Layers.Add(nl); } else { throw new InvalidOperationException("Cannot Rotate non-BitmapLayers"); } if (this.PleaseCancel) { break; } OnProgress(progressEnd); } CompoundHistoryMemento chm = new CompoundHistoryMemento( haName, haImage, actions); if (this.PleaseCancel) { chm = null; } else { EnterCriticalRegion(); historyWorkspace.Document = newDoc; } return chm; }
private HistoryMemento ImportOneLayer(DocumentWorkspace documentWorkspace, BitmapLayer layer) { HistoryMemento retHA; List<HistoryMemento> historyMementos = new List<HistoryMemento>(); bool success = true; if (success) { if (!documentWorkspace.Selection.IsEmpty) { HistoryMemento ha = new DeselectFunction().Execute(documentWorkspace); historyMementos.Add(ha); } } if (success) { if (layer.Width > documentWorkspace.Document.Width || layer.Height > documentWorkspace.Document.Height) { HistoryMemento ha = DoCanvasResize(documentWorkspace, layer.Size); if (ha == null) { success = false; } else { historyMementos.Add(ha); } } } if (success) { if (layer.Size != documentWorkspace.Document.Size) { BitmapLayer newLayer; try { using (new WaitCursorChanger(documentWorkspace)) { Utility.GCFullCollect(); newLayer = CanvasSizeAction.ResizeLayer((BitmapLayer)layer, documentWorkspace.Document.Size, AnchorEdge.TopLeft, ColorBgra.White.NewAlpha(0)); } } catch (OutOfMemoryException) { Utility.ErrorBox(documentWorkspace, PdnResources.GetString("ImportFromFileAction.ImportOneLayer.OutOfMemory")); success = false; newLayer = null; } if (newLayer != null) { layer.Dispose(); layer = newLayer; } } } if (success) { NewLayerHistoryMemento nlha = new NewLayerHistoryMemento(string.Empty, null, documentWorkspace, documentWorkspace.Document.Layers.Count); documentWorkspace.Document.Layers.Add(layer); historyMementos.Add(nlha); } if (success) { HistoryMemento[] has = historyMementos.ToArray(); retHA = new CompoundHistoryMemento(string.Empty, null, has); } else { Rollback(historyMementos); retHA = null; } return retHA; }
public HistoryMemento ImportMultipleFiles(DocumentWorkspace documentWorkspace, string[] fileNames) { HistoryMemento retHA = null; List<HistoryMemento> historyMementos = new List<HistoryMemento>(); Rectangle lastLayerBounds = Rectangle.Empty; foreach (string fileName in fileNames) { HistoryMemento ha = ImportOneFile(documentWorkspace, fileName, out lastLayerBounds); if (ha != null) { historyMementos.Add(ha); } else { Rollback(historyMementos); historyMementos.Clear(); break; } } if (lastLayerBounds.Width > 0 && lastLayerBounds.Height > 0) { SelectionHistoryMemento sha = new SelectionHistoryMemento(null, null, documentWorkspace); historyMementos.Add(sha); documentWorkspace.Selection.PerformChanging(); documentWorkspace.Selection.Reset(); documentWorkspace.Selection.SetContinuation(lastLayerBounds, System.Drawing.Drawing2D.CombineMode.Replace); documentWorkspace.Selection.CommitContinuation(); documentWorkspace.Selection.PerformChanged(); } if (historyMementos.Count > 0) { HistoryMemento[] haArray = historyMementos.ToArray(); retHA = new CompoundHistoryMemento(StaticName, StaticImage, haArray); } return retHA; }
private void FlushHistoryMementos(string name) { if (this.currentHistoryMementos.Count > 0) { CompoundHistoryMemento cha = new CompoundHistoryMemento(null, null, this.currentHistoryMementos.ToArray()); string haName; ImageResource image; if (this.didPaste) { haName = PdnResources.GetString("CommonAction.Paste"); image = PdnResources.GetImageResource("Icons.MenuEditPasteIcon.png"); this.didPaste = false; } else { if (name == null) { haName = this.Name; } else { haName = name; } image = this.Image; } CompoundToolHistoryMemento ctha = new CompoundToolHistoryMemento(cha, this.DocumentWorkspace, haName, image); ctha.SeriesGuid = context.seriesGuid; HistoryStack.PushNewMemento(ctha); this.currentHistoryMementos.Clear(); } }
public override HistoryMemento PerformAction(DocumentWorkspace documentWorkspace) { string[] fileNames; string startingDir = Path.GetDirectoryName(documentWorkspace.FilePath); DialogResult result = DocumentWorkspace.ChooseFiles(documentWorkspace, out fileNames, true, startingDir); HistoryMemento retHA = null; if (result == DialogResult.OK) { Type oldToolType = documentWorkspace.GetToolType(); documentWorkspace.SetTool(null); retHA = ImportMultipleFiles(documentWorkspace, fileNames); Type newToolType; if (retHA != null) { CompoundHistoryMemento cha = new CompoundHistoryMemento(StaticName, StaticImage, new HistoryMemento[] { retHA }); retHA = cha; newToolType = typeof(Tools.MoveTool); } else { newToolType = oldToolType; } documentWorkspace.SetToolFromType(newToolType); } return retHA; }
protected override void OnKeyPress(KeyPressEventArgs e) { switch (e.KeyChar) { case (char)13: // Enter if (tracking) { e.Handled = true; } break; case (char)27: // Escape if (tracking) { e.Handled = true; } else { if (mode == EditingMode.Editing) { SaveHistoryMemento(); } else if (mode == EditingMode.EmptyEdit) { RedrawText(false); } if (mode != EditingMode.NotEditing) { e.Handled = true; StopEditing(); } } break; } if (!e.Handled && mode != EditingMode.NotEditing && !tracking) { e.Handled = true; if (mode == EditingMode.EmptyEdit) { mode = EditingMode.Editing; CompoundHistoryMemento cha = new CompoundHistoryMemento(Name, Image, new List<HistoryMemento>()); this.currentHA = cha; HistoryStack.PushNewMemento(cha); } if (!char.IsControl(e.KeyChar)) { InsertCharIntoString(e.KeyChar); textPos++; RedrawText(true); } } base.OnKeyPress (e); }
private void SaveHistoryMemento() { pulseEnabled = false; RedrawText(false); if (saved != null) { PdnRegion hitTest = Selection.CreateRegion(); hitTest.Intersect(saved.Region); if (!hitTest.IsEmpty()) { BitmapHistoryMemento bha = new BitmapHistoryMemento(Name, Image, DocumentWorkspace, ActiveLayerIndex, saved); if (this.currentHA == null) { HistoryStack.PushNewMemento(bha); } else { this.currentHA.PushNewAction(bha); this.currentHA = null; } } hitTest.Dispose(); saved.Dispose(); saved = null; } }
private void FlushHistoryMementos(string name) { if (this.currentHistoryMementos.Count > 0) { CompoundHistoryMemento cha = new CompoundHistoryMemento(null, null, this.currentHistoryMementos.ToArray()); string haName; if (name == null) { haName = this.Name; } else { haName = name; } ImageResource image = this.Image; CompoundToolHistoryMemento ctha = new CompoundToolHistoryMemento(cha, DocumentWorkspace, haName, image); ctha.SeriesGuid = context.seriesGuid; HistoryStack.PushNewMemento(ctha); this.currentHistoryMementos.Clear(); } }
private void CommitGradient() { if (!this.gradientActive) { throw new InvalidOperationException("CommitGradient() called when a gradient was not active"); } RenderGradient(); using (PdnRegion clipRegion = DocumentWorkspace.Selection.CreateRegion()) { BitmapHistoryMemento bhm = new BitmapHistoryMemento( StaticName, StaticImage, DocumentWorkspace, DocumentWorkspace.ActiveLayerIndex, clipRegion, this.ScratchSurface); this.historyMemento.PushNewAction(bhm); // We assume this.historyMemento has already been pushed on to the HistoryStack this.historyMemento = null; } this.startNub.Visible = false; this.endNub.Visible = false; ClearSavedRegion(); ClearSavedMemory(); this.gradientActive = false; SetStatus(this.toolIcon, this.helpTextInitial); }
protected void CommitShape() { OnShapeCommitting(); mouseDown = false; ArrayList has = new ArrayList(); PdnRegion activeRegion = Selection.CreateRegion(); if (outlineSaveRegion != null) { using (PdnRegion clipTest = activeRegion.Clone()) { clipTest.Intersect(outlineSaveRegion); if (!clipTest.IsEmpty()) { BitmapHistoryMemento bha = new BitmapHistoryMemento(Name, Image, this.DocumentWorkspace, ActiveLayerIndex, outlineSaveRegion, this.ScratchSurface); has.Add(bha); outlineSaveRegion.Dispose(); outlineSaveRegion = null; } } } if (interiorSaveRegion != null) { using (PdnRegion clipTest = activeRegion.Clone()) { clipTest.Intersect(interiorSaveRegion); if (!clipTest.IsEmpty()) { BitmapHistoryMemento bha = new BitmapHistoryMemento(Name, Image, this.DocumentWorkspace, ActiveLayerIndex, interiorSaveRegion, this.ScratchSurface); has.Add(bha); interiorSaveRegion.Dispose(); interiorSaveRegion = null; } } } if (has.Count > 0) { CompoundHistoryMemento cha = new CompoundHistoryMemento(Name, Image, (HistoryMemento[])has.ToArray(typeof(HistoryMemento))); if (this.chaAlreadyOnStack == null) { HistoryStack.PushNewMemento(cha); } else { this.chaAlreadyOnStack.PushNewAction(cha); this.chaAlreadyOnStack = null; } } activeRegion.Dispose(); points = null; Update(); this.shapeWasCommited = true; }
public CompoundToolHistoryMemento(CompoundHistoryMemento chm, DocumentWorkspace documentWorkspace, string name, ImageResource image) : base(documentWorkspace, name, image) { this.compoundHistoryMemento = chm; }
protected override void OnStylusUp(StylusEventArgs e) { base.OnStylusUp(e); Cursor = cursorMouseUp; if (moveOriginMode) { moveOriginMode = false; } else if (mouseDown) { bool doCommit = OnShapeEnd(); if (doCommit) { CommitShape(); } else { // place a 'sentinel' history action on the stack that will be filled in later CompoundHistoryMemento cha = new CompoundHistoryMemento(Name, Image, new List<HistoryMemento>()); HistoryStack.PushNewMemento(cha); this.chaAlreadyOnStack = cha; } } }
public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace) { if (historyWorkspace.Selection.IsEmpty) { return null; } else { PdnRegion selectionRegion = historyWorkspace.Selection.CreateRegion(); if (selectionRegion.GetArea() == 0) { selectionRegion.Dispose(); return null; } SelectionHistoryMemento sha = new SelectionHistoryMemento(StaticName, null, historyWorkspace); ReplaceDocumentHistoryMemento rdha = new ReplaceDocumentHistoryMemento(StaticName, null, historyWorkspace); Rectangle boundingBox; Rectangle[] inverseRegionRects = null; boundingBox = Utility.GetRegionBounds(selectionRegion); using (PdnRegion inverseRegion = new PdnRegion(boundingBox)) { inverseRegion.Exclude(selectionRegion); inverseRegionRects = Utility.TranslateRectangles( inverseRegion.GetRegionScansReadOnlyInt(), -boundingBox.X, -boundingBox.Y); } selectionRegion.Dispose(); selectionRegion = null; Document oldDocument = historyWorkspace.Document; // TODO: serialize this to disk so we don't *have* to store the full thing Document newDocument = new Document(boundingBox.Width, boundingBox.Height); // copy the document's meta data over newDocument.ReplaceMetaDataFrom(oldDocument); foreach (Layer layer in oldDocument.Layers) { if (layer is BitmapLayer) { BitmapLayer oldLayer = (BitmapLayer)layer; Surface croppedSurface = oldLayer.Surface.CreateWindow(boundingBox); BitmapLayer newLayer = new BitmapLayer(croppedSurface); ColorBgra clearWhite = ColorBgra.White.NewAlpha(0); foreach (Rectangle rect in inverseRegionRects) { newLayer.Surface.Clear(rect, clearWhite); } newLayer.LoadProperties(oldLayer.SaveProperties()); newDocument.Layers.Add(newLayer); } else { throw new InvalidOperationException("Crop does not support Layers that are not BitmapLayers"); } } CompoundHistoryMemento cha = new CompoundHistoryMemento( StaticName, PdnResources.GetImageResource("Icons.MenuImageCropIcon.png"), new HistoryMemento[] { sha, rdha }); EnterCriticalRegion(); historyWorkspace.Document = newDocument; return cha; } }
protected override void OnMouseDown(MouseEventArgs e) { PointF mousePt = new PointF(e.X, e.Y); MoveNubRenderer mouseCursorNub = PointToNub(mousePt); if (this.mouseButton != MouseButtons.None) { this.shouldMoveBothNubs = !this.shouldMoveBothNubs; } else { bool startNewGradient = true; this.mouseButton = e.Button; if (!this.gradientActive) { this.shouldSwapColors = (this.mouseButton == MouseButtons.Right); } else { this.shouldMoveBothNubs = false; // We are already in the process of drawing or adjusting a gradient. // Determine if they clicked to drag one of the nubs for adjusting. if (mouseCursorNub == null) { // No. Commit the old gradient and begin a new one. CommitGradient(); startNewGradient = true; this.shouldSwapColors = (this.mouseButton == MouseButtons.Right); } else { // Yes. Continue adjusting the old gradient. Cursor = this.handCursorMouseDown; this.mouseNub = mouseCursorNub; this.mouseNub.Location = mousePt; if (this.mouseNub == this.startNub) { this.startPoint = mousePt; } else { this.endPoint = mousePt; } if (this.mouseButton == MouseButtons.Right) { this.shouldSwapColors = !this.shouldSwapColors; } RenderGradient(); startNewGradient = false; } } if (startNewGradient) { // Brand new gradient. Set everything up. this.startPoint = mousePt; this.startNub.Location = mousePt; this.startNub.Visible = true; this.endNub.Location = mousePt; this.endNub.Visible = true; this.endPoint = mousePt; this.mouseNub = mouseCursorNub; Cursor = this.toolMouseDownCursor; this.gradientActive = true; ClearSavedRegion(); RenderGradient(); this.historyMemento = new CompoundHistoryMemento(StaticName, StaticImage); HistoryStack.PushNewMemento(this.historyMemento); // this makes it so they can push Esc to undo } } base.OnMouseDown(e); }