void RenameOrCreateFileOrFolder() { var fileItem = TreeFolderBrowser.SelectedItem as PathItem; if (string.IsNullOrEmpty(fileItem?.EditName)) { return; } string oldFilename = Path.GetFileName(fileItem.FullPath); string oldPath = Path.GetDirectoryName(fileItem.FullPath); string newPath = Path.Combine(oldPath, fileItem.EditName); if (newPath != fileItem.FullPath) { if (fileItem.IsFolder) { try { if (Directory.Exists(fileItem.FullPath)) { Directory.Move(fileItem.FullPath, newPath); } else { Directory.CreateDirectory(newPath); } } catch { MessageBox.Show("Unable to rename or create folder:\r\n" + newPath, "Path Creation Error", MessageBoxButton.OK, MessageBoxImage.Warning); } } else { try { if (File.Exists(fileItem.FullPath)) { if (!File.Exists(newPath)) { File.Move(fileItem.FullPath, newPath); } else { File.Copy(fileItem.FullPath, newPath, true); } } else { File.WriteAllText(newPath, ""); } // check if tab was open and if so rename the tab var tab = Window.GetTabFromFilename(fileItem.FullPath); if (tab != null) { Window.CloseTab(fileItem.FullPath); WindowUtilities.DoEvents(); Window.OpenTab(newPath); WindowUtilities.DoEvents(); //var doc = (MarkdownDocumentEditor) tab.Tag; //doc.MarkdownDocument.Load(newPath); //tab.Tag = doc; //Window.BindTabHeaders(); } } catch { MessageBox.Show("Unable to rename or create file:\r\n" + newPath, "File Creation Error", MessageBoxButton.OK, MessageBoxImage.Warning); } // Open the document // HandleItemSelection(); } } fileItem.FullPath = newPath; fileItem.IsEditing = false; }
private void TreeViewItem_Drop(object sender, DragEventArgs e) { if (sender is TreeView) { // dropped into treeview open space return; //targetItem = ActivePathItem; } string rawFilename = null; if (e.Data.GetDataPresent(DataFormats.FileDrop)) { var tkens = e.Data.GetData(DataFormats.FileDrop) as string[]; if (tkens == null) { return; } rawFilename = tkens[0]; } IsDragging = false; FavoriteItem targetItem; e.Handled = true; targetItem = (e.OriginalSource as FrameworkElement)?.DataContext as FavoriteItem; if (targetItem == null) { return; } string path = rawFilename; if (string.IsNullOrEmpty(rawFilename)) { // "path|title" path = e.Data.GetData(DataFormats.UnicodeText) as string; } if (string.IsNullOrEmpty(path)) { return; } FavoriteItem sourceItem = null; ObservableCollection <FavoriteItem> parentList = null; var tokens = path.Split('|'); if (tokens.Length == 1) { // just a filename var newItem = new FavoriteItem { File = path, Title = System.IO.Path.GetFileName(path) }; sourceItem = FavoritesModel.FindFavoriteByFilenameAndTitle(FavoritesModel.Favorites, newItem.Filename, newItem.File); if (sourceItem == null) { sourceItem = newItem; } } else { sourceItem = FavoritesModel.FindFavoriteByFilenameAndTitle(FavoritesModel.Favorites, tokens[0], tokens[1]); } if (sourceItem == null) { return; } parentList = sourceItem.Parent?.Items; if (parentList == null) { parentList = FavoritesModel.Favorites; } parentList.Remove(sourceItem); parentList = null; if (targetItem.IsFolder && !sourceItem.IsFolder) { // dropped on folder: Add below parentList = targetItem.Items; sourceItem.Parent = targetItem; } else { // Dropped on file: Add after parentList = targetItem.Parent?.Items; if (parentList == null) { parentList = FavoritesModel.Favorites; sourceItem.Parent = null; } else { sourceItem.Parent = targetItem.Parent; } } var index = parentList.IndexOf(targetItem); if (index < 0) { index = 0; } else { index++; } if (index >= parentList.Count) { parentList.Add(sourceItem); } else { parentList.Insert(index, sourceItem); } FavoritesModel.SaveFavorites(); WindowUtilities.DoEvents(); }
/// <summary> /// Saves an image loaded from clipboard to disk OR if base64 is checked /// creates the base64 content. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_SaveImage(object sender, RoutedEventArgs e) { string imagePath = null; var bitmapSource = ImagePreview.Source as BitmapSource; if (bitmapSource == null) { MessageBox.Show("Unable to convert bitmap source.", "Bitmap conversion error", MessageBoxButton.OK, MessageBoxImage.Warning); return; } using (var bitMap = WindowUtilities.BitmapSourceToBitmap(bitmapSource)) { if (bitMap == null) { return; } imagePath = AddinManager.Current.RaiseOnSaveImage(bitMap); if (PasteAsBase64Content) { Base64EncodeImage(bitMap); IsMemoryImage = false; return; } if (!string.IsNullOrEmpty(imagePath)) { TextImage.Text = imagePath; IsMemoryImage = false; return; } string initialFolder = null; string documentFolder = null; if (!string.IsNullOrEmpty(Document.Filename) && Document.Filename != "untitled") { documentFolder = Path.GetDirectoryName(Document.Filename); if (!string.IsNullOrEmpty(Document.LastImageFolder)) { initialFolder = Document.LastImageFolder; } else { initialFolder = documentFolder; } } var sd = new SaveFileDialog { Filter = "Image files (*.png;*.jpg;*.gif;)|*.png;*.jpg;*.jpeg;*.gif|All Files (*.*)|*.*", FilterIndex = 1, Title = "Save Image from Clipboard as", InitialDirectory = initialFolder, CheckFileExists = false, OverwritePrompt = true, CheckPathExists = true, RestoreDirectory = true }; var result = sd.ShowDialog(); if (result != null && result.Value) { imagePath = sd.FileName; try { var ext = Path.GetExtension(imagePath)?.ToLower(); if (ext == ".jpg" || ext == ".jpeg") { ImageUtils.SaveJpeg(bitMap, imagePath, mmApp.Configuration.JpegImageCompressionLevel); } else { using (var fileStream = new FileStream(imagePath, FileMode.Create)) { BitmapEncoder encoder = null; if (ext == ".png") { encoder = new PngBitmapEncoder(); } else if (ext == ".gif") { encoder = new GifBitmapEncoder(); } encoder.Frames.Add(BitmapFrame.Create(ImagePreview.Source as BitmapSource)); encoder.Save(fileStream); if (ext == ".png" || ext == ".jpeg") { mmFileUtils.OptimizeImage(sd.FileName); // async } } } } catch (Exception ex) { MessageBox.Show("Couldn't save file: \r\n" + ex.Message, mmApp.ApplicationName); return; } string relPath = Path.GetDirectoryName(sd.FileName); Document.LastImageFolder = relPath; if (documentFolder != null) { try { relPath = FileUtils.GetRelativePath(sd.FileName, documentFolder); } catch (Exception ex) { mmApp.Log($"Failed to get relative path.\r\nFile: {sd.FileName}, Path: {imagePath}", ex); } imagePath = relPath; } if (imagePath.Contains(":\\")) { imagePath = "file:///" + imagePath; } imagePath = imagePath.Replace("\\", "/"); Image = imagePath; IsMemoryImage = false; } } }
private void RepopulateChildren(ObservableCollection <ObservableCollection <CellContent> > data) { //Debug.WriteLine("RepopulateChildren called."); bool origRecursiveUpdates = PreventRecursiveUpdates; PreventRecursiveUpdates = true; Children.Clear(); RowDefinitions.Clear(); ColumnDefinitions.Clear(); var contextMenu = ParentWindow.Resources["ColumnContextMenu"] as ContextMenu; //var rect = new Rectangle { Fill = Brushes.Gray }; //Grid.SetColumnSpan(rect, 10000); //Children.Add(rect); if (data.Count < 1) { return; } RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); var columnCounter = 0; foreach (var header in data[0]) { ColumnDefinitions.Add(new ColumnDefinition { }); var columnText = NewTextBox(); columnText.Background = new BrushConverter().ConvertFromString("#777") as Brush; columnText.Tag = new TablePosition { Column = columnCounter, Row = 0 }; columnText.ContextMenu = contextMenu; var binding = new Binding("Text") { Source = header }; columnText.SetBinding(TextBox.TextProperty, binding); Grid.SetColumn(columnText, columnCounter); Children.Add(columnText); columnCounter++; } if (data.Count > 50) { mmApp.Model.Window.ShowStatusProgress("Binding table data..."); } var rowCount = 1; foreach (var row in data.Skip(1)) { RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); columnCounter = 0; foreach (var column in row) { var columnText = NewTextBox(); columnText.Tag = new TablePosition { Column = columnCounter, Row = rowCount }; var binding = new Binding("Text") { Source = column, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, Mode = BindingMode.TwoWay }; columnText.SetBinding(TextBox.TextProperty, binding); //columnText.Text = column.Text; columnText.ContextMenu = contextMenu; Grid.SetColumn(columnText, columnCounter); Grid.SetRow(columnText, rowCount); Children.Add(columnText); columnCounter++; } rowCount++; if (rowCount % 8 == 0) { PreventRecursiveUpdates = false; WindowUtilities.DoEvents(); PreventRecursiveUpdates = true; } } var lastText = Children.OfType <TextBox>().LastOrDefault(); if (lastText != null) { lastText.PreviewKeyDown += (s, e) => { if (e.Key == Key.Tab) { if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) { return; } var newRow = new ObservableCollection <CellContent>(); foreach (var header in TableSource[0]) { newRow.Add(new CellContent(string.Empty)); } var textBox = s as TextBox; var pos = textBox.Tag as TablePosition; TableSource.Add(newRow); SelectColumn(pos.Row + 1, 0); } }; } PreventRecursiveUpdates = origRecursiveUpdates; if (!PreventRecursiveUpdates) { InvalidateVisual(); } }
void RenameOrCreateFileOrFolder() { var fileItem = TreeFolderBrowser.SelectedItem as PathItem; if (string.IsNullOrEmpty(fileItem?.EditName)) { return; } string oldFile = fileItem.FullPath; string oldPath = Path.GetDirectoryName(fileItem.FullPath); string newPath = Path.Combine(oldPath, fileItem.EditName); if (fileItem.IsFolder) { try { if (Directory.Exists(fileItem.FullPath)) { Directory.Move(fileItem.FullPath, newPath); } else { if (Directory.Exists(newPath)) { AppModel.Window.ShowStatusError($"Can't create folderOrFilePath {newPath} because it exists already."); return; } fileItem.IsEditing = false; var parent = fileItem.Parent; parent.Files.Remove(fileItem); fileItem.FullPath = newPath; FolderStructure.InsertPathItemInOrder(fileItem, parent); Dispatcher.Invoke(() => { Directory.CreateDirectory(newPath); fileItem.UpdateGitFileStatus(); }, DispatcherPriority.ApplicationIdle); } } catch { MessageBox.Show("Unable to rename or create folderOrFilePath:\r\n" + newPath, "Path Creation Error", MessageBoxButton.OK, MessageBoxImage.Warning); return; } } else { try { if (File.Exists(fileItem.FullPath)) { if (!File.Exists(newPath)) { File.Move(fileItem.FullPath, newPath); } else { File.Copy(fileItem.FullPath, newPath, true); } } else { if (File.Exists(newPath)) { AppModel.Window.ShowStatusError($"Can't create file {newPath} because it exists already."); return; } fileItem.IsEditing = false; fileItem.FullPath = newPath; // force assignment so file watcher doesn't add another File.WriteAllText(newPath, ""); fileItem.UpdateGitFileStatus(); var parent = fileItem.Parent; fileItem.Parent.Files.Remove(fileItem); FolderStructure.InsertPathItemInOrder(fileItem, parent); } // If tab was open - close it and re-open new file var tab = Window.GetTabFromFilename(oldFile); if (tab != null) { Window.CloseTab(oldFile); WindowUtilities.DoEvents(); Window.OpenFile(newPath, isPreview: true); WindowUtilities.DoEvents(); } } catch (Exception ex) { MessageBox.Show("Unable to rename or create file:\r\n" + newPath + "\r\n" + ex.Message, "File Creation Error", MessageBoxButton.OK, MessageBoxImage.Warning); } } fileItem.FullPath = newPath; fileItem.IsEditing = false; }
private void TreeViewItem_Drop(object sender, DragEventArgs e) { // avoid double drop events? if (!IsDragging) { return; } IsDragging = false; FavoriteItem targetItem; e.Handled = true; if (sender is TreeView) { // dropped into treeview open space return; //targetItem = ActivePathItem; } targetItem = (e.OriginalSource as FrameworkElement)?.DataContext as FavoriteItem; if (targetItem == null) { return; } // "path|title" var path = e.Data.GetData(DataFormats.UnicodeText) as string; if (string.IsNullOrEmpty(path)) { return; } var tokens = path.Split('|'); var sourceItem = FavoritesModel.FindFavoriteByFilenameAndTitle(FavoritesModel.Favorites, tokens[0], tokens[1]); if (sourceItem == null) { return; } var parentList = sourceItem.Parent?.Items; if (parentList == null) { parentList = FavoritesModel.Favorites; } parentList.Remove(sourceItem); parentList = null; if (targetItem.IsFolder && !sourceItem.IsFolder) { // dropped on folder: Add below parentList = targetItem.Items; sourceItem.Parent = targetItem; } else { // Dropped on file: Add after parentList = targetItem.Parent?.Items; if (parentList == null) { parentList = FavoritesModel.Favorites; sourceItem.Parent = null; } else { sourceItem.Parent = targetItem.Parent; } } var index = parentList.IndexOf(targetItem); if (index < 0) { index = 0; } else { index++; } if (index >= parentList.Count) { parentList.Add(sourceItem); } else { parentList.Insert(index, sourceItem); } FavoritesModel.SaveFavorites(); WindowUtilities.DoEvents(); }