private void LoadAttachments() { if (loaded) { selected = null; RemoveResizer(); Canvas.Children.Clear(); foreach (string filePath in Manager.GetAttachments(this.Transaction)) { try { string ext = System.IO.Path.GetExtension(filePath); if (string.Compare(ext, ".jpg", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(ext, ".png", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(ext, ".gif", StringComparison.OrdinalIgnoreCase) == 0 || string.Compare(ext, ".bmp", StringComparison.OrdinalIgnoreCase) == 0) { AddItem(new AttachmentDialogImageItem(filePath)); } else if (string.Compare(ext, ".xamlpackage", StringComparison.OrdinalIgnoreCase) == 0) { // load an xamlpackage document. AddItem(new AttachmentDialogDocumentItem(filePath)); } } catch { // todo? } } LayoutContent(); } }
private void OnItemRemoved(AttachmentDialogItem item) { Canvas.Children.Remove(item); if (item == selected) { RemoveResizer(); selected = null; } }
private void MoveResizer(AttachmentDialogItem item, Rect resizerBounds) { // Transform resizer so it is anchored around the selected image. if (resizer != null && item != null) { resizer.LimitBounds = GetScaledBounds(item.ResizeLimit); resizer.Bounds = GetScaledBounds(resizerBounds); resizer.InvalidateArrange(); } }
private void OnItemChanged(AttachmentDialogItem item) { if (item != null) { this.selected.InvalidateArrange(); LayoutContent(); SelectItem(this.selected); // fix up the resizer. this.SetDirty(); } }
/// <summary> /// Add or more the resizer so it is aligned with the given image. /// </summary> private void AddResizer(AttachmentDialogItem item, Rect cropBounds) { if (resizer == null) { resizer = new Resizer(); resizer.BorderBrush = resizer.ThumbBrush = this.resizerBrush; resizer.ThumbSize = ResizerThumbSize; resizer.Resized += OnResized; resizer.Resizing += OnResizing; this.Adorners.Children.Add(resizer); } MoveResizer(item, cropBounds); }
private void Save(object sender, ExecutedRoutedEventArgs e) { try { int index = 0; foreach (UIElement element in Canvas.Children) { AttachmentDialogItem item = element as AttachmentDialogItem; if (item != null) { string fileName = Manager.GetUniqueFileName(this.Transaction, item.FileExtension); string existing = item.FileName; if (!string.IsNullOrEmpty(existing) && string.Compare(fileName, existing, StringComparison.OrdinalIgnoreCase) != 0) { // file name is different, so delete the old file. TempFilesManager.DeleteFile(existing); } AttachmentDialogImageItem img = item as AttachmentDialogImageItem; if (img != null) { if (item == selected && resizer != null) { // crop it. Rect bounds = GetUnscaledBounds(resizer.Bounds); img.Resize(bounds); } } // in case an item was deleted in the middle, we need to re-index the items. item.Save(fileName); item.FileName = fileName; index++; } } ClearSelection(); LoadAttachments(); dirty = false; } catch (Exception ex) { MessageBox.Show(this, "Unexpected error saving new image: " + ex.Message, "Save Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void Delete(object sender, ExecutedRoutedEventArgs e) { try { AttachmentDialogItem item = this.selected; if (item != null) { string filePath = item.FileName; TempFilesManager.DeleteFile(filePath); OnItemRemoved(item); SetDirty(); } } catch (Exception ex) { MessageBox.Show(this, "Error deleting file: " + ex.Message, "Delete Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void Paste(object sender, ExecutedRoutedEventArgs e) { AttachmentDialogItem newItem = null; if (Clipboard.ContainsImage()) { var image = Clipboard.GetImage(); if (image != null) { // for some reason this bitmap doesn't paint unless I save and reload it // the in-memory copy from the clipboard is a bit touchy, probably comes from // another process and so on, so persistence is better strategy here... string path = Path.GetTempFileName(); AttachmentDialogImageItem item = new AttachmentDialogImageItem(image); item.Save(path); TempFilesManager.AddTempFile(path); newItem = new AttachmentDialogImageItem(path); } } else if (Clipboard.ContainsData(DataFormats.XamlPackage) || Clipboard.ContainsData(DataFormats.Rtf) || Clipboard.ContainsData(DataFormats.Text)) { newItem = new AttachmentDialogDocumentItem(Clipboard.GetDataObject()); } if (newItem != null) { AddItem(newItem); LayoutContent(); SelectItem(newItem); SetDirty(); } }
void CanvasGrid_MouseDown(object sender, MouseButtonEventArgs e) { Point pos = e.GetPosition(CanvasGrid); HitTestResult result = VisualTreeHelper.HitTest(CanvasGrid, pos); DependencyObject hit = result.VisualHit; if (hit != null) { AttachmentDialogItem item = WpfHelper.FindAncestor <AttachmentDialogItem>(hit); if (item != null) { SelectItem(item); return; } if (hit == resizer) { return; } } RemoveResizer(); selected = null; }
/// <summary> /// Get the bounds of the content of this item in unscaled coordinates, relative /// to the top left of the item. /// </summary> /// <param name="item">The item whose content bounds we want</param> /// <returns>The bounds</returns> private Rect GetItemContentBounds(AttachmentDialogItem item) { Size size = item.Content.DesiredSize; return(new Rect(0, 0, size.Width, size.Height)); }
private void AddItem(AttachmentDialogItem item) { item.Margin = new Thickness(10); item.ContentChanged += OnContentChanged; Canvas.Children.Add(item); }
private void ClearSelection() { selected = null; RemoveResizer(); }
void SelectItem(AttachmentDialogItem item) { this.selected = item; AddResizer(item, GetItemContentBounds(item)); }