[12762] | 1 | using System;
|
---|
| 2 | using System.IO;
|
---|
| 3 | using System.Xml;
|
---|
| 4 | using System.ComponentModel;
|
---|
| 5 | using System.Collections.Generic;
|
---|
| 6 |
|
---|
| 7 | using System.Windows;
|
---|
| 8 | using System.Windows.Input;
|
---|
| 9 | using System.Windows.Shapes;
|
---|
| 10 | using System.Windows.Controls;
|
---|
| 11 | using System.Windows.Controls.Primitives;
|
---|
| 12 | using System.Windows.Media;
|
---|
| 13 | using System.Windows.Media.Imaging;
|
---|
| 14 |
|
---|
| 15 | using Microsoft.Win32;
|
---|
| 16 |
|
---|
| 17 | using IoPath = System.IO.Path;
|
---|
| 18 | using FolderBrowserDialog = System.Windows.Forms.FolderBrowserDialog;
|
---|
| 19 |
|
---|
| 20 | namespace WpfTestSvgSample
|
---|
| 21 | {
|
---|
| 22 | /// <summary>
|
---|
| 23 | /// Interaction logic for MainWindow.xaml
|
---|
| 24 | /// </summary>
|
---|
| 25 | public partial class MainWindow : Window
|
---|
| 26 | {
|
---|
| 27 | #region Private Fields
|
---|
| 28 |
|
---|
| 29 | private delegate void FileChangedToUIThread(FileSystemEventArgs e);
|
---|
| 30 |
|
---|
| 31 | private string _titleBase;
|
---|
| 32 |
|
---|
| 33 | private bool _leftSplitterChanging;
|
---|
| 34 |
|
---|
| 35 | private string _drawingDir;
|
---|
| 36 |
|
---|
| 37 | private bool _canDeleteXaml;
|
---|
| 38 |
|
---|
| 39 | private string _svgFilePath;
|
---|
| 40 | private string _xamlFilePath;
|
---|
| 41 |
|
---|
| 42 | private FileSystemWatcher _fileWatcher;
|
---|
| 43 |
|
---|
| 44 | private SvgPage _svgPage;
|
---|
| 45 | private XamlPage _xamlPage;
|
---|
| 46 | private DrawingPage _drawingPage;
|
---|
| 47 |
|
---|
| 48 | private BitmapImage _folderClose;
|
---|
| 49 | private BitmapImage _folderOpen;
|
---|
| 50 | private BitmapImage _fileThumbnail;
|
---|
| 51 |
|
---|
| 52 | #endregion
|
---|
| 53 |
|
---|
| 54 | #region Constructors and Destructor
|
---|
| 55 |
|
---|
| 56 | public MainWindow()
|
---|
| 57 | {
|
---|
| 58 | InitializeComponent();
|
---|
| 59 |
|
---|
| 60 | _titleBase = this.Title;
|
---|
| 61 |
|
---|
| 62 | leftExpander.Expanded += new RoutedEventHandler(OnLeftExpanderExpanded);
|
---|
| 63 | leftExpander.Collapsed += new RoutedEventHandler(OnLeftExpanderCollapsed);
|
---|
| 64 | leftSplitter.MouseMove += new MouseEventHandler(OnLeftSplitterMove);
|
---|
| 65 |
|
---|
| 66 | this.Loaded += new RoutedEventHandler(OnWindowLoaded);
|
---|
| 67 | //this.Unloaded += new RoutedEventHandler(OnWindowUnloaded);
|
---|
| 68 | this.Closing += new CancelEventHandler(OnWindowClosing);
|
---|
| 69 |
|
---|
| 70 | _drawingDir = IoPath.Combine(IoPath.GetDirectoryName(
|
---|
| 71 | System.Reflection.Assembly.GetExecutingAssembly().Location), "XamlDrawings");
|
---|
| 72 |
|
---|
| 73 | if (!Directory.Exists(_drawingDir))
|
---|
| 74 | {
|
---|
| 75 | Directory.CreateDirectory(_drawingDir);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | try
|
---|
| 79 | {
|
---|
| 80 | _folderClose = new BitmapImage();
|
---|
| 81 | _folderClose.BeginInit();
|
---|
| 82 | _folderClose.UriSource = new Uri("Images/FolderClose.png", UriKind.Relative);
|
---|
| 83 | _folderClose.EndInit();
|
---|
| 84 |
|
---|
| 85 | _folderOpen = new BitmapImage();
|
---|
| 86 | _folderOpen.BeginInit();
|
---|
| 87 | _folderOpen.UriSource = new Uri("Images/FolderOpen.png", UriKind.Relative);
|
---|
| 88 | _folderOpen.EndInit();
|
---|
| 89 |
|
---|
| 90 | _fileThumbnail = new BitmapImage();
|
---|
| 91 | _fileThumbnail.BeginInit();
|
---|
| 92 | _fileThumbnail.UriSource = new Uri("Images/Thumbnail.png", UriKind.Relative);
|
---|
| 93 | _fileThumbnail.EndInit();
|
---|
| 94 | }
|
---|
| 95 | catch (Exception ex)
|
---|
| 96 | {
|
---|
| 97 | _folderClose = null;
|
---|
| 98 | _folderOpen = null;
|
---|
| 99 |
|
---|
| 100 | MessageBox.Show(ex.ToString(), "Wpf-Svg Test Sample",
|
---|
| 101 | MessageBoxButton.OK, MessageBoxImage.Error);
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | #endregion
|
---|
| 106 |
|
---|
| 107 | #region Protected Methods
|
---|
| 108 |
|
---|
| 109 | protected override void OnInitialized(EventArgs e)
|
---|
| 110 | {
|
---|
| 111 | base.OnInitialized(e);
|
---|
| 112 |
|
---|
| 113 | double width = SystemParameters.PrimaryScreenWidth;
|
---|
| 114 | double height = SystemParameters.PrimaryScreenHeight;
|
---|
| 115 |
|
---|
| 116 | this.Width = Math.Min(1600, width) * 0.85;
|
---|
| 117 | this.Height = height * 0.85;
|
---|
| 118 |
|
---|
| 119 | this.Left = (width - this.Width) / 2.0;
|
---|
| 120 | this.Top = (height - this.Height) / 2.0;
|
---|
| 121 |
|
---|
| 122 | this.WindowStartupLocation = WindowStartupLocation.Manual;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | #endregion
|
---|
| 126 |
|
---|
| 127 | #region Private Event Handlers
|
---|
| 128 |
|
---|
| 129 | private void OnWindowLoaded(object sender, RoutedEventArgs e)
|
---|
| 130 | {
|
---|
| 131 | leftExpander.IsExpanded = false;
|
---|
| 132 |
|
---|
| 133 | // Retrieve the display pages...
|
---|
| 134 | _svgPage = frameSvgInput.Content as SvgPage;
|
---|
| 135 | _xamlPage = frameXamlOutput.Content as XamlPage;
|
---|
| 136 | _drawingPage = frameDrawing.Content as DrawingPage;
|
---|
| 137 |
|
---|
| 138 | if (_drawingPage != null)
|
---|
| 139 | {
|
---|
| 140 | _drawingPage.XamlDrawingDir = _drawingDir;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | this.txtSvgPath.Text = IoPath.GetFullPath(@".\Samples");
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | private void OnWindowUnloaded(object sender, RoutedEventArgs e)
|
---|
| 147 | {
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | private void OnWindowClosing(object sender, CancelEventArgs e)
|
---|
| 151 | {
|
---|
| 152 | if (_canDeleteXaml && !String.IsNullOrEmpty(_xamlFilePath) && File.Exists(_xamlFilePath))
|
---|
| 153 | {
|
---|
| 154 | File.Delete(_xamlFilePath);
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | private void OnBrowseForSvgPath(object sender, RoutedEventArgs e)
|
---|
| 159 | {
|
---|
| 160 | FolderBrowserDialog dlg = new FolderBrowserDialog();
|
---|
| 161 | dlg.ShowNewFolderButton = false;
|
---|
| 162 | dlg.Description = "Select the location of the SVG Files";
|
---|
| 163 |
|
---|
| 164 | string curDir = txtSvgPath.Text;
|
---|
| 165 | if (!String.IsNullOrEmpty(curDir) && Directory.Exists(curDir))
|
---|
| 166 | {
|
---|
| 167 | dlg.SelectedPath = curDir;
|
---|
| 168 | }
|
---|
| 169 | dlg.RootFolder = Environment.SpecialFolder.MyComputer;
|
---|
| 170 |
|
---|
| 171 | if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
|
---|
| 172 | {
|
---|
| 173 | txtSvgPath.Text = dlg.SelectedPath;
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | private void OnBrowseForSvgFile(object sender, RoutedEventArgs e)
|
---|
| 178 | {
|
---|
| 179 | OpenFileDialog dlg = new OpenFileDialog();
|
---|
| 180 | dlg.Multiselect = false;
|
---|
| 181 | dlg.Filter = "SVG Files|*.svg;*.svgz"; ;
|
---|
| 182 | dlg.FilterIndex = 1;
|
---|
| 183 |
|
---|
| 184 | bool? isSelected = dlg.ShowDialog();
|
---|
| 185 |
|
---|
| 186 | if (isSelected != null && isSelected.Value)
|
---|
| 187 | {
|
---|
| 188 | this.LoadFile(dlg.FileName);
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | private void OnSvgPathTextChanged(object sender, TextChangedEventArgs e)
|
---|
| 193 | {
|
---|
| 194 | string selectePath = txtSvgPath.Text;
|
---|
| 195 | if (selectePath != null)
|
---|
| 196 | {
|
---|
| 197 | selectePath = selectePath.Trim();
|
---|
| 198 | }
|
---|
| 199 | if (String.IsNullOrEmpty(selectePath) || !Directory.Exists(selectePath))
|
---|
| 200 | {
|
---|
| 201 | return;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | this.FillTreeView(selectePath);
|
---|
| 205 |
|
---|
| 206 | this.CloseFile();
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | private void OnFillSvgInputChecked(object sender, RoutedEventArgs e)
|
---|
| 210 | {
|
---|
| 211 | if (_svgPage == null)
|
---|
| 212 | {
|
---|
| 213 | return;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | try
|
---|
| 217 | {
|
---|
| 218 | this.Cursor = Cursors.Wait;
|
---|
| 219 | this.ForceCursor = true;
|
---|
| 220 |
|
---|
| 221 | if (File.Exists(_svgFilePath) &&
|
---|
| 222 | (fillSvgInput.IsChecked != null && fillSvgInput.IsChecked.Value))
|
---|
| 223 | {
|
---|
| 224 | _svgPage.LoadDocument(_svgFilePath);
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | catch (Exception ex)
|
---|
| 228 | {
|
---|
| 229 | MessageBox.Show(ex.ToString(), "Wpf-Svg Test Sample",
|
---|
| 230 | MessageBoxButton.OK, MessageBoxImage.Error);
|
---|
| 231 | }
|
---|
| 232 | finally
|
---|
| 233 | {
|
---|
| 234 | this.Cursor = Cursors.Arrow;
|
---|
| 235 | this.ForceCursor = false;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | private void OnFillXamlOutputChecked(object sender, RoutedEventArgs e)
|
---|
| 240 | {
|
---|
| 241 | if (_xamlPage == null || String.IsNullOrEmpty(_xamlFilePath))
|
---|
| 242 | {
|
---|
| 243 | return;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | try
|
---|
| 247 | {
|
---|
| 248 | this.Cursor = Cursors.Wait;
|
---|
| 249 | this.ForceCursor = true;
|
---|
| 250 |
|
---|
| 251 | if (!File.Exists(_xamlFilePath) && _drawingPage != null)
|
---|
| 252 | {
|
---|
| 253 | if (!_drawingPage.SaveDocument(_xamlFilePath))
|
---|
| 254 | {
|
---|
| 255 | return;
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | if (File.Exists(_xamlFilePath) &&
|
---|
| 260 | (fillXamlOutput.IsChecked != null && fillXamlOutput.IsChecked.Value))
|
---|
| 261 | {
|
---|
| 262 | _xamlPage.LoadDocument(_xamlFilePath);
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 | catch (Exception ex)
|
---|
| 266 | {
|
---|
| 267 | MessageBox.Show(ex.ToString(), "Wpf-Svg Test Sample",
|
---|
| 268 | MessageBoxButton.OK, MessageBoxImage.Error);
|
---|
| 269 | }
|
---|
| 270 | finally
|
---|
| 271 | {
|
---|
| 272 | this.Cursor = Cursors.Arrow;
|
---|
| 273 | this.ForceCursor = false;
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | private void OnTabItemGotFocus(object sender, RoutedEventArgs e)
|
---|
| 278 | {
|
---|
| 279 | if (sender == tabDrawing)
|
---|
| 280 | {
|
---|
| 281 | if (_drawingPage != null)
|
---|
| 282 | {
|
---|
| 283 | _drawingPage.PageSelected(true);
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 | else if (sender == tabXamlOutput)
|
---|
| 287 | {
|
---|
| 288 | if (_xamlPage != null)
|
---|
| 289 | {
|
---|
| 290 | _xamlPage.PageSelected(true);
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 | else if (sender == tabSvgInput)
|
---|
| 294 | {
|
---|
| 295 | if (_svgPage != null)
|
---|
| 296 | {
|
---|
| 297 | _svgPage.PageSelected(true);
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | #region TreeView Event Handlers
|
---|
| 303 |
|
---|
| 304 | private void OnTreeViewItemSelected(object sender, RoutedEventArgs e)
|
---|
| 305 | {
|
---|
| 306 | TreeViewItem selItem = treeView.SelectedItem as TreeViewItem;
|
---|
| 307 | if (selItem == null || selItem.Tag == null)
|
---|
| 308 | {
|
---|
| 309 | return;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | string svgFilePath = selItem.Tag as string;
|
---|
| 313 | if (String.IsNullOrEmpty(svgFilePath) || !File.Exists(svgFilePath))
|
---|
| 314 | {
|
---|
| 315 | return;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | e.Handled = true;
|
---|
| 319 |
|
---|
| 320 | this.CloseFile();
|
---|
| 321 |
|
---|
| 322 | try
|
---|
| 323 | {
|
---|
| 324 | this.Cursor = Cursors.Wait;
|
---|
| 325 | this.ForceCursor = true;
|
---|
| 326 |
|
---|
| 327 | this.LoadFile(svgFilePath);
|
---|
| 328 | }
|
---|
| 329 | catch (Exception ex)
|
---|
| 330 | {
|
---|
| 331 | MessageBox.Show(ex.ToString(), "Wpf-Svg Test Sample",
|
---|
| 332 | MessageBoxButton.OK, MessageBoxImage.Error);
|
---|
| 333 | }
|
---|
| 334 | finally
|
---|
| 335 | {
|
---|
| 336 | this.Cursor = Cursors.Arrow;
|
---|
| 337 | this.ForceCursor = false;
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | private void OnTreeViewItemUnselected(object sender, RoutedEventArgs e)
|
---|
| 342 | {
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | private void OnTreeViewItemCollapsed(object sender, RoutedEventArgs e)
|
---|
| 346 | {
|
---|
| 347 | if (_folderClose == null)
|
---|
| 348 | {
|
---|
| 349 | return;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | TreeViewItem treeItem = e.OriginalSource as TreeViewItem;
|
---|
| 353 | if (treeItem == null)
|
---|
| 354 | {
|
---|
| 355 | return;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | BulletDecorator decorator = treeItem.Header as BulletDecorator;
|
---|
| 359 | if (decorator == null)
|
---|
| 360 | {
|
---|
| 361 | return;
|
---|
| 362 | }
|
---|
| 363 | Image headerImage = decorator.Bullet as Image;
|
---|
| 364 | if (headerImage == null)
|
---|
| 365 | {
|
---|
| 366 | return;
|
---|
| 367 | }
|
---|
| 368 | headerImage.Source = _folderClose;
|
---|
| 369 |
|
---|
| 370 | e.Handled = true;
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | private void OnTreeViewItemExpanded(object sender, RoutedEventArgs e)
|
---|
| 374 | {
|
---|
| 375 | if (_folderOpen == null)
|
---|
| 376 | {
|
---|
| 377 | return;
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | TreeViewItem treeItem = e.OriginalSource as TreeViewItem;
|
---|
| 381 | if (treeItem == null)
|
---|
| 382 | {
|
---|
| 383 | return;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | BulletDecorator decorator = treeItem.Header as BulletDecorator;
|
---|
| 387 | if (decorator == null)
|
---|
| 388 | {
|
---|
| 389 | return;
|
---|
| 390 | }
|
---|
| 391 | Image headerImage = decorator.Bullet as Image;
|
---|
| 392 | if (headerImage == null)
|
---|
| 393 | {
|
---|
| 394 | return;
|
---|
| 395 | }
|
---|
| 396 | headerImage.Source = _folderOpen;
|
---|
| 397 |
|
---|
| 398 | e.Handled = true;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | #endregion
|
---|
| 402 |
|
---|
| 403 | #region Drag/Drop Methods
|
---|
| 404 |
|
---|
| 405 | private void OnDragEnter(object sender, DragEventArgs de)
|
---|
| 406 | {
|
---|
| 407 | if (de.Data.GetDataPresent(DataFormats.Text) ||
|
---|
| 408 | de.Data.GetDataPresent(DataFormats.FileDrop))
|
---|
| 409 | {
|
---|
| 410 | de.Effects = DragDropEffects.Copy;
|
---|
| 411 | }
|
---|
| 412 | else
|
---|
| 413 | {
|
---|
| 414 | de.Effects = DragDropEffects.None;
|
---|
| 415 | }
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | private void OnDragLeave(object sender, DragEventArgs e)
|
---|
| 419 | {
|
---|
| 420 |
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | private void OnDragDrop(object sender, DragEventArgs de)
|
---|
| 424 | {
|
---|
| 425 | string fileName = "";
|
---|
| 426 | if (de.Data.GetDataPresent(DataFormats.Text))
|
---|
| 427 | {
|
---|
| 428 | fileName = (string)de.Data.GetData(DataFormats.Text);
|
---|
| 429 | }
|
---|
| 430 | else if (de.Data.GetDataPresent(DataFormats.FileDrop))
|
---|
| 431 | {
|
---|
| 432 | string[] fileNames;
|
---|
| 433 | fileNames = (string[])de.Data.GetData(DataFormats.FileDrop);
|
---|
| 434 | fileName = fileNames[0];
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | if (!String.IsNullOrEmpty(fileName))
|
---|
| 438 | {
|
---|
| 439 | }
|
---|
| 440 | if (String.IsNullOrEmpty(fileName) || !File.Exists(fileName))
|
---|
| 441 | {
|
---|
| 442 | return;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | this.CloseFile();
|
---|
| 446 |
|
---|
| 447 | try
|
---|
| 448 | {
|
---|
| 449 | this.Cursor = Cursors.Wait;
|
---|
| 450 | this.ForceCursor = true;
|
---|
| 451 |
|
---|
| 452 | this.LoadFile(fileName);
|
---|
| 453 | }
|
---|
| 454 | catch (Exception ex)
|
---|
| 455 | {
|
---|
| 456 | MessageBox.Show(ex.ToString(), "Wpf-Svg Test Sample",
|
---|
| 457 | MessageBoxButton.OK, MessageBoxImage.Error);
|
---|
| 458 | }
|
---|
| 459 | finally
|
---|
| 460 | {
|
---|
| 461 | this.Cursor = Cursors.Arrow;
|
---|
| 462 | this.ForceCursor = false;
|
---|
| 463 | }
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | #endregion
|
---|
| 467 |
|
---|
| 468 | #region LeftExpander/Splitter Event Handlers
|
---|
| 469 |
|
---|
| 470 | private void OnLeftExpanderCollapsed(object sender, RoutedEventArgs e)
|
---|
| 471 | {
|
---|
| 472 | if (_leftSplitterChanging)
|
---|
| 473 | {
|
---|
| 474 | return;
|
---|
| 475 | }
|
---|
| 476 |
|
---|
| 477 | ColumnDefinition rowExpander = mainGrid.ColumnDefinitions[0];
|
---|
| 478 | rowExpander.Width = new GridLength(24, GridUnitType.Pixel);
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | private void OnLeftExpanderExpanded(object sender, RoutedEventArgs e)
|
---|
| 482 | {
|
---|
| 483 | if (_leftSplitterChanging)
|
---|
| 484 | {
|
---|
| 485 | return;
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | ColumnDefinition rowExpander = mainGrid.ColumnDefinitions[0];
|
---|
| 489 | rowExpander.Width = new GridLength(280, GridUnitType.Pixel);
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 | private void OnLeftSplitterMove(object sender, MouseEventArgs e)
|
---|
| 493 | {
|
---|
| 494 | _leftSplitterChanging = true;
|
---|
| 495 |
|
---|
| 496 | ColumnDefinition rowExpander = mainGrid.ColumnDefinitions[0];
|
---|
| 497 |
|
---|
| 498 | leftExpander.IsExpanded = rowExpander.ActualWidth > 24;
|
---|
| 499 |
|
---|
| 500 | _leftSplitterChanging = false;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | #endregion
|
---|
| 504 |
|
---|
| 505 | #region FileSystemWatcher Event Handlers
|
---|
| 506 |
|
---|
| 507 | private void OnFileChangedToUIThread(FileSystemEventArgs e)
|
---|
| 508 | {
|
---|
| 509 | // Stop watching.
|
---|
| 510 | _fileWatcher.EnableRaisingEvents = false;
|
---|
| 511 |
|
---|
| 512 | try
|
---|
| 513 | {
|
---|
| 514 | this.Cursor = Cursors.Wait;
|
---|
| 515 | this.ForceCursor = true;
|
---|
| 516 |
|
---|
| 517 | this.LoadFile(e.FullPath);
|
---|
| 518 | }
|
---|
| 519 | catch (Exception ex)
|
---|
| 520 | {
|
---|
| 521 | MessageBox.Show(ex.ToString(), "Wpf-Svg Test Sample",
|
---|
| 522 | MessageBoxButton.OK, MessageBoxImage.Error);
|
---|
| 523 | }
|
---|
| 524 | finally
|
---|
| 525 | {
|
---|
| 526 | this.Cursor = Cursors.Arrow;
|
---|
| 527 | this.ForceCursor = false;
|
---|
| 528 | }
|
---|
| 529 | }
|
---|
| 530 |
|
---|
| 531 | // Define the event handlers.
|
---|
| 532 | private void OnFileChanged(object source, FileSystemEventArgs e)
|
---|
| 533 | {
|
---|
| 534 | if (e.ChangeType == WatcherChangeTypes.Changed)
|
---|
| 535 | {
|
---|
| 536 | this.Dispatcher.BeginInvoke(new FileChangedToUIThread(OnFileChangedToUIThread),
|
---|
| 537 | System.Windows.Threading.DispatcherPriority.Normal, e);
|
---|
| 538 | }
|
---|
| 539 | }
|
---|
| 540 |
|
---|
| 541 | private void OnFileRenamed(object source, RenamedEventArgs e)
|
---|
| 542 | {
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | #endregion
|
---|
| 546 |
|
---|
| 547 | #endregion
|
---|
| 548 |
|
---|
| 549 | #region Private Methods
|
---|
| 550 |
|
---|
| 551 | private void LoadFile(string fileName)
|
---|
| 552 | {
|
---|
| 553 | string fileExt = IoPath.GetExtension(fileName);
|
---|
| 554 | if (String.IsNullOrEmpty(fileExt))
|
---|
| 555 | {
|
---|
| 556 | return;
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | if (_fileWatcher != null)
|
---|
| 560 | {
|
---|
| 561 | _fileWatcher.EnableRaisingEvents = false;
|
---|
| 562 | }
|
---|
| 563 |
|
---|
| 564 | bool generateXaml = (fillXamlOutput.IsChecked != null &&
|
---|
| 565 | fillXamlOutput.IsChecked.Value);
|
---|
| 566 |
|
---|
| 567 | if (String.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase) ||
|
---|
| 568 | String.Equals(fileExt, ".svg", StringComparison.OrdinalIgnoreCase))
|
---|
| 569 | {
|
---|
| 570 | txtSvgFile.Text = fileName;
|
---|
| 571 | txtSvgFileLabel.Text = "Selected SVG File:";
|
---|
| 572 |
|
---|
| 573 | _svgFilePath = fileName;
|
---|
| 574 |
|
---|
| 575 | if (_svgPage != null &&
|
---|
| 576 | (fillSvgInput.IsChecked != null && fillSvgInput.IsChecked.Value))
|
---|
| 577 | {
|
---|
| 578 | _svgPage.LoadDocument(fileName);
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | if (_drawingPage == null)
|
---|
| 582 | {
|
---|
| 583 | return;
|
---|
| 584 | }
|
---|
| 585 | _drawingPage.SaveXaml = generateXaml;
|
---|
| 586 |
|
---|
| 587 | try
|
---|
| 588 | {
|
---|
| 589 | if (_drawingPage.LoadDocument(fileName))
|
---|
| 590 | {
|
---|
| 591 | this.Title = _titleBase + " - " + IoPath.GetFileName(fileName);
|
---|
| 592 |
|
---|
| 593 | if (_xamlPage != null && !String.IsNullOrEmpty(_drawingDir))
|
---|
| 594 | {
|
---|
| 595 | string xamlFilePath = IoPath.Combine(_drawingDir,
|
---|
| 596 | IoPath.GetFileNameWithoutExtension(fileName) + ".xaml");
|
---|
| 597 |
|
---|
| 598 | _xamlFilePath = xamlFilePath;
|
---|
| 599 | _canDeleteXaml = true;
|
---|
| 600 |
|
---|
| 601 | if (File.Exists(xamlFilePath) &&
|
---|
| 602 | (fillXamlOutput.IsChecked != null && fillXamlOutput.IsChecked.Value))
|
---|
| 603 | {
|
---|
| 604 | _xamlPage.LoadDocument(xamlFilePath);
|
---|
| 605 |
|
---|
| 606 | // Delete the file after loading it...
|
---|
| 607 | File.Delete(xamlFilePath);
|
---|
| 608 | }
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | if (_fileWatcher == null)
|
---|
| 612 | {
|
---|
| 613 | // Create a new FileSystemWatcher and set its properties.
|
---|
| 614 | _fileWatcher = new FileSystemWatcher();
|
---|
| 615 | // Watch for changes in LastAccess and LastWrite times
|
---|
| 616 | _fileWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite;
|
---|
| 617 |
|
---|
| 618 | _fileWatcher.IncludeSubdirectories = false;
|
---|
| 619 |
|
---|
| 620 | // Add event handlers.
|
---|
| 621 | _fileWatcher.Changed += new FileSystemEventHandler(OnFileChanged);
|
---|
| 622 | _fileWatcher.Created += new FileSystemEventHandler(OnFileChanged);
|
---|
| 623 | _fileWatcher.Deleted += new FileSystemEventHandler(OnFileChanged);
|
---|
| 624 | _fileWatcher.Renamed += new RenamedEventHandler(OnFileRenamed);
|
---|
| 625 | }
|
---|
| 626 |
|
---|
| 627 | _fileWatcher.Path = IoPath.GetDirectoryName(fileName);
|
---|
| 628 | // Only watch current file
|
---|
| 629 | _fileWatcher.Filter = IoPath.GetFileName(fileName);
|
---|
| 630 | // Begin watching.
|
---|
| 631 | _fileWatcher.EnableRaisingEvents = true;
|
---|
| 632 | }
|
---|
| 633 | }
|
---|
| 634 | catch
|
---|
| 635 | {
|
---|
| 636 | // Try loading the XAML, if generated but the rendering failed...
|
---|
| 637 | if (_xamlPage != null && !String.IsNullOrEmpty(_drawingDir))
|
---|
| 638 | {
|
---|
| 639 | string xamlFilePath = IoPath.Combine(_drawingDir,
|
---|
| 640 | IoPath.GetFileNameWithoutExtension(fileName) + ".xaml");
|
---|
| 641 |
|
---|
| 642 | _xamlFilePath = xamlFilePath;
|
---|
| 643 | _canDeleteXaml = true;
|
---|
| 644 |
|
---|
| 645 | if (File.Exists(xamlFilePath) &&
|
---|
| 646 | (fillXamlOutput.IsChecked != null && fillXamlOutput.IsChecked.Value))
|
---|
| 647 | {
|
---|
| 648 | _xamlPage.LoadDocument(xamlFilePath);
|
---|
| 649 |
|
---|
| 650 | // Delete the file after loading it...
|
---|
| 651 | File.Delete(xamlFilePath);
|
---|
| 652 | }
|
---|
| 653 | }
|
---|
| 654 | throw;
|
---|
| 655 | }
|
---|
| 656 | }
|
---|
| 657 | else if (String.Equals(fileExt, ".xaml", StringComparison.OrdinalIgnoreCase) ||
|
---|
| 658 | String.Equals(fileExt, ".zaml", StringComparison.OrdinalIgnoreCase))
|
---|
| 659 | {
|
---|
| 660 | txtSvgFile.Text = fileName;
|
---|
| 661 | txtSvgFileLabel.Text = "Selected XAML File:";
|
---|
| 662 |
|
---|
| 663 | if (_svgPage != null)
|
---|
| 664 | {
|
---|
| 665 | _svgPage.UnloadDocument();
|
---|
| 666 | _svgFilePath = null;
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | try
|
---|
| 670 | {
|
---|
| 671 | if (_drawingPage.LoadDocument(fileName))
|
---|
| 672 | {
|
---|
| 673 | this.Title = _titleBase + " - " + IoPath.GetFileName(fileName);
|
---|
| 674 |
|
---|
| 675 | _xamlFilePath = fileName;
|
---|
| 676 | _canDeleteXaml = false;
|
---|
| 677 |
|
---|
| 678 | if (_xamlPage != null)
|
---|
| 679 | {
|
---|
| 680 | _xamlPage.LoadDocument(fileName);
|
---|
| 681 | }
|
---|
| 682 | }
|
---|
| 683 | }
|
---|
| 684 | catch
|
---|
| 685 | {
|
---|
| 686 | if (_xamlPage != null)
|
---|
| 687 | {
|
---|
| 688 | _xamlFilePath = fileName;
|
---|
| 689 | _canDeleteXaml = false;
|
---|
| 690 |
|
---|
| 691 | _xamlPage.LoadDocument(fileName);
|
---|
| 692 | }
|
---|
| 693 |
|
---|
| 694 | throw;
|
---|
| 695 | }
|
---|
| 696 | }
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | private void CloseFile()
|
---|
| 700 | {
|
---|
| 701 | try
|
---|
| 702 | {
|
---|
| 703 | if (_svgPage != null)
|
---|
| 704 | {
|
---|
| 705 | _svgPage.UnloadDocument();
|
---|
| 706 | }
|
---|
| 707 | if (_xamlPage != null)
|
---|
| 708 | {
|
---|
| 709 | _xamlPage.UnloadDocument();
|
---|
| 710 | }
|
---|
| 711 | if (_drawingPage != null)
|
---|
| 712 | {
|
---|
| 713 | _drawingPage.UnloadDocument();
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | if (_canDeleteXaml && !String.IsNullOrEmpty(_xamlFilePath) && File.Exists(_xamlFilePath))
|
---|
| 717 | {
|
---|
| 718 | File.Delete(_xamlFilePath);
|
---|
| 719 | }
|
---|
| 720 |
|
---|
| 721 | _svgFilePath = null;
|
---|
| 722 | _xamlFilePath = null;
|
---|
| 723 | _canDeleteXaml = false;
|
---|
| 724 | }
|
---|
| 725 | catch
|
---|
| 726 | {
|
---|
| 727 | }
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | #region FillTreeView Methods
|
---|
| 731 |
|
---|
| 732 | private void FillTreeView(string sourceDir)
|
---|
| 733 | {
|
---|
| 734 | if (String.IsNullOrEmpty(sourceDir) || !Directory.Exists(sourceDir))
|
---|
| 735 | {
|
---|
| 736 | return;
|
---|
| 737 | }
|
---|
| 738 |
|
---|
| 739 | string[] svgFiles = Directory.GetFiles(sourceDir, "*.svg", SearchOption.TopDirectoryOnly);
|
---|
| 740 |
|
---|
| 741 | treeView.BeginInit();
|
---|
| 742 | treeView.Items.Clear();
|
---|
| 743 |
|
---|
| 744 | DirectoryInfo directoryInfo = new DirectoryInfo(sourceDir);
|
---|
| 745 |
|
---|
| 746 | TextBlock headerText = new TextBlock();
|
---|
| 747 | headerText.Text = directoryInfo.Name;
|
---|
| 748 | headerText.Margin = new Thickness(3, 0, 0, 0);
|
---|
| 749 |
|
---|
| 750 | BulletDecorator decorator = new BulletDecorator();
|
---|
| 751 | if (_folderClose != null)
|
---|
| 752 | {
|
---|
| 753 | Image image = new Image();
|
---|
| 754 | image.Source = _folderClose;
|
---|
| 755 |
|
---|
| 756 | decorator.Bullet = image;
|
---|
| 757 | }
|
---|
| 758 | else
|
---|
| 759 | {
|
---|
| 760 | Ellipse bullet = new Ellipse();
|
---|
| 761 | bullet.Height = 16;
|
---|
| 762 | bullet.Width = 16;
|
---|
| 763 | bullet.Fill = Brushes.Goldenrod;
|
---|
| 764 | bullet.Stroke = Brushes.DarkGray;
|
---|
| 765 | bullet.StrokeThickness = 1;
|
---|
| 766 |
|
---|
| 767 | decorator.Bullet = bullet;
|
---|
| 768 | }
|
---|
| 769 | decorator.Margin = new Thickness(0, 0, 10, 0);
|
---|
| 770 | decorator.Child = headerText;
|
---|
| 771 |
|
---|
| 772 | TreeViewItem categoryItem = new TreeViewItem();
|
---|
| 773 | categoryItem.Tag = String.Empty;
|
---|
| 774 | categoryItem.Header = decorator;
|
---|
| 775 | categoryItem.Margin = new Thickness(0);
|
---|
| 776 | categoryItem.Padding = new Thickness(3);
|
---|
| 777 | categoryItem.FontSize = 14;
|
---|
| 778 | categoryItem.FontWeight = FontWeights.Bold;
|
---|
| 779 |
|
---|
| 780 | treeView.Items.Add(categoryItem);
|
---|
| 781 |
|
---|
| 782 | FillTreeView(sourceDir, svgFiles, categoryItem);
|
---|
| 783 |
|
---|
| 784 | categoryItem.IsExpanded = true;
|
---|
| 785 |
|
---|
| 786 | treeView.EndInit();
|
---|
| 787 |
|
---|
| 788 | leftExpander.IsExpanded = true;
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | private void FillTreeView(string sourceDir, string[] svgFiles, TreeViewItem treeItem)
|
---|
| 792 | {
|
---|
| 793 | if (svgFiles != null && svgFiles.Length != 0)
|
---|
| 794 | {
|
---|
| 795 | for (int i = 0; i < svgFiles.Length; i++)
|
---|
| 796 | {
|
---|
| 797 | string svgFile = svgFiles[i];
|
---|
| 798 |
|
---|
| 799 | TextBlock itemText = new TextBlock();
|
---|
| 800 | itemText.Text = String.Format("({0:D3}) - {1}", i, IoPath.GetFileName(svgFile));
|
---|
| 801 | itemText.Margin = new Thickness(3, 0, 0, 0);
|
---|
| 802 |
|
---|
| 803 | BulletDecorator fileItem = new BulletDecorator();
|
---|
| 804 | if (_fileThumbnail != null)
|
---|
| 805 | {
|
---|
| 806 | Image image = new Image();
|
---|
| 807 | image.Source = _fileThumbnail;
|
---|
| 808 | image.Height = 16;
|
---|
| 809 | image.Width = 16;
|
---|
| 810 |
|
---|
| 811 | fileItem.Bullet = image;
|
---|
| 812 | }
|
---|
| 813 | else
|
---|
| 814 | {
|
---|
| 815 | Ellipse bullet = new Ellipse();
|
---|
| 816 | bullet.Height = 16;
|
---|
| 817 | bullet.Width = 16;
|
---|
| 818 | bullet.Fill = Brushes.Goldenrod;
|
---|
| 819 | bullet.Stroke = Brushes.DarkGray;
|
---|
| 820 | bullet.StrokeThickness = 1;
|
---|
| 821 |
|
---|
| 822 | fileItem.Bullet = bullet;
|
---|
| 823 | }
|
---|
| 824 | fileItem.Margin = new Thickness(0, 0, 10, 0);
|
---|
| 825 | fileItem.Child = itemText;
|
---|
| 826 |
|
---|
| 827 | TreeViewItem item = new TreeViewItem();
|
---|
| 828 | item.Tag = svgFile;
|
---|
| 829 | item.Header = fileItem;
|
---|
| 830 | item.Margin = new Thickness(0);
|
---|
| 831 | item.Padding = new Thickness(2);
|
---|
| 832 | item.FontSize = 12;
|
---|
| 833 | item.FontWeight = FontWeights.Normal;
|
---|
| 834 |
|
---|
| 835 | treeItem.Items.Add(item);
|
---|
| 836 | }
|
---|
| 837 | }
|
---|
| 838 |
|
---|
| 839 | if (String.IsNullOrEmpty(sourceDir) || !Directory.Exists(sourceDir))
|
---|
| 840 | {
|
---|
| 841 | return;
|
---|
| 842 | }
|
---|
| 843 |
|
---|
| 844 | string[] directories = Directory.GetDirectories(sourceDir);
|
---|
| 845 | if (directories != null && directories.Length != 0)
|
---|
| 846 | {
|
---|
| 847 | for (int i = 0; i < directories.Length; i++)
|
---|
| 848 | {
|
---|
| 849 | string directory = directories[i];
|
---|
| 850 | svgFiles = Directory.GetFiles(directory, "*.svg", SearchOption.TopDirectoryOnly);
|
---|
| 851 | {
|
---|
| 852 | DirectoryInfo directoryInfo = new DirectoryInfo(directory);
|
---|
| 853 |
|
---|
| 854 | TextBlock headerText = new TextBlock();
|
---|
| 855 | headerText.Text = directoryInfo.Name;
|
---|
| 856 | headerText.Margin = new Thickness(3, 0, 0, 0);
|
---|
| 857 |
|
---|
| 858 | BulletDecorator decorator = new BulletDecorator();
|
---|
| 859 | if (_folderClose != null)
|
---|
| 860 | {
|
---|
| 861 | Image image = new Image();
|
---|
| 862 | image.Source = _folderClose;
|
---|
| 863 |
|
---|
| 864 | decorator.Bullet = image;
|
---|
| 865 | }
|
---|
| 866 | else
|
---|
| 867 | {
|
---|
| 868 | Ellipse bullet = new Ellipse();
|
---|
| 869 | bullet.Height = 16;
|
---|
| 870 | bullet.Width = 16;
|
---|
| 871 | bullet.Fill = Brushes.Goldenrod;
|
---|
| 872 | bullet.Stroke = Brushes.DarkGray;
|
---|
| 873 | bullet.StrokeThickness = 1;
|
---|
| 874 |
|
---|
| 875 | decorator.Bullet = bullet;
|
---|
| 876 | }
|
---|
| 877 | decorator.Margin = new Thickness(0, 0, 10, 0);
|
---|
| 878 | decorator.Child = headerText;
|
---|
| 879 |
|
---|
| 880 | TreeViewItem categoryItem = new TreeViewItem();
|
---|
| 881 | categoryItem.Tag = String.Empty;
|
---|
| 882 | categoryItem.Header = decorator;
|
---|
| 883 | categoryItem.Margin = new Thickness(0);
|
---|
| 884 | categoryItem.Padding = new Thickness(3);
|
---|
| 885 | categoryItem.FontSize = 14;
|
---|
| 886 | categoryItem.FontWeight = FontWeights.Bold;
|
---|
| 887 |
|
---|
| 888 | treeItem.Items.Add(categoryItem);
|
---|
| 889 |
|
---|
| 890 | FillTreeView(directory, svgFiles, categoryItem);
|
---|
| 891 |
|
---|
| 892 | if (!categoryItem.HasItems)
|
---|
| 893 | {
|
---|
| 894 | treeItem.Items.Remove(categoryItem);
|
---|
| 895 | }
|
---|
| 896 | }
|
---|
| 897 | }
|
---|
| 898 | }
|
---|
| 899 | }
|
---|
| 900 |
|
---|
| 901 | #endregion
|
---|
| 902 |
|
---|
| 903 | #endregion
|
---|
| 904 | }
|
---|
| 905 | }
|
---|