Ejemplo n.º 1
0
        private List <string> LoadHistory()
        {
            List <string> list = new List <string>();
            string        text = this.historySettings.Value;

            if (!string.IsNullOrWhiteSpace(text))
            {
                try {
                    XmlDocument xml = XmlHelper.LoadXml(text);
                    foreach (XmlNode item in xml.SelectNodes("/List/Item"))
                    {
                        list.Add(item.InnerText);
                    }
                } catch (Exception exception) {
                    Tracer.Report("ScriptConsole.LoadHistory", exception);
                    //Ignore the exception
                }
                int count = list.Count - ScriptConsole.MaxHistoryCount;
                if (0 < count)
                {
                    list.RemoveRange(0, count);
                }
            }
            return(list);
        }
Ejemplo n.º 2
0
 private void RunningMapDoubleClick(object sender, MouseButtonEventArgs e)
 {
     try {
         if (this.Editor != null && this.Editor.Power && e.ChangedButton == MouseButton.Left)
         {
             TreeView treeView = sender as TreeView;
             if (treeView != null && treeView.SelectedItem != null)
             {
                 CircuitMap map = treeView.SelectedItem as CircuitMap;
                 if (map != null)
                 {
                     this.Editor.OpenLogicalCircuit(map);
                     TreeViewItem item = this.Container(treeView, map);
                     if (item != null)
                     {
                         item.IsExpanded = true;
                     }
                     e.Handled = true;
                 }
             }
         }
     } catch (Exception exception) {
         Tracer.Report("MainFrame.RunningMapDoubleClick", exception);
         this.ReportException(exception);
     }
 }
Ejemplo n.º 3
0
        public UserSettings()
        {
            this.fileWatcher = new FileSystemWatcher {
                EnableRaisingEvents = false
            };
            string file = UserSettings.FileName();

            if (!File.Exists(file))
            {
                this.IsFirstRun = true;
                try {
                    this.Save();
                } catch (Exception exception) {
                    Tracer.Report("UserSettings.ctor", exception);
                }
            }
            try {
                this.Load(file);
                this.fileWatcher.Path                = Path.GetDirectoryName(file);
                this.fileWatcher.Filter              = Path.GetFileName(file);
                this.fileWatcher.Changed            += new FileSystemEventHandler(this.FileChanged);
                this.fileWatcher.EnableRaisingEvents = true;
            } catch (Exception exception) {
                Tracer.Report("UserSettings.ctor", exception);
            }
            this.maxRecentFileCount     = new SettingsIntegerCache(this, "Settings.MaxRecentFileCount", 1, 24, 4);
            this.loadLastFileOnStartup  = new SettingsBoolCache(this, "Settings.LoadLastFileOnStartup", true);
            this.createBackupFileOnSave = new SettingsBoolCache(this, "Settings.CreateBackupFileOnSave", false);
            this.gateShape = new SettingsEnumCache <GateShape>(this, "Settings.GateShape",
                                                               EnumHelper.Parse(Properties.Resources.DefaultGateShape, GateShape.Rectangular)
                                                               );
            this.TruncateRecentFile();
        }
Ejemplo n.º 4
0
 public static void CheckTranslationRequests(Dispatcher dispatcher)
 {
     try {
         string cultureName = App.CurrentCulture.Name;
         if (!cultureName.StartsWith("en", StringComparison.OrdinalIgnoreCase))
         {
             SettingsStringCache checkedVersion = DialogAbout.TranslationRequestVersion();
             if (!Version.TryParse(checkedVersion.Value, out Version version) || version < DialogAbout.CurrentVersion())
             {
                 string text = null;
                 using (WebClient client = new WebClient()) {
                     client.UseDefaultCredentials = true;
                     text = client.DownloadString(new Uri("https://www.LogicCircuit.org/TranslationRequests.txt"));
                 }
                 if (!string.IsNullOrWhiteSpace(text) && text.Contains(cultureName))
                 {
                     dispatcher.BeginInvoke(
                         new Action(() => App.Mainframe.InformationMessage(
                                        "If you can help translating this program to any language you are fluent in please contact me at:\n" +
                                        "<Hyperlink NavigateUri=\"https://www.logiccircuit.org/contact.html\">https://www.logiccircuit.org/contact.html</Hyperlink>"
                                        )),
                         DispatcherPriority.ApplicationIdle
                         );
                 }
                 checkedVersion.Value = DialogAbout.CurrentVersion().ToString();
             }
         }
     } catch (Exception exception) {
         Tracer.Report("DialogAbout.CheckTranslationRequests", exception);
         // ignore all exception here
     }
 }
        public DialogImport(string file, CircuitProject target)
        {
            this.FileName    = file;
            this.DataContext = this;
            this.InitializeComponent();
            Mainframe mainframe = App.Mainframe;
            Thread    thread    = new Thread(new ThreadStart(() => {
                try {
                    CircuitProject import   = CircuitProject.Create(file);
                    List <CircuitInfo> list = new List <CircuitInfo>();
                    foreach (LogicalCircuit circuit in import.LogicalCircuitSet)
                    {
                        list.Add(new CircuitInfo(circuit, target.LogicalCircuitSet.FindByLogicalCircuitId(circuit.LogicalCircuitId) == null));
                    }
                    list.Sort(CircuitDescriptorComparer.Comparer);
                    this.List = list;
                    this.NotifyPropertyChanged(nameof(this.List));
                } catch (SnapStoreException snapStoreException) {
                    Tracer.Report("DialogImport.Load", snapStoreException);
                    mainframe.ErrorMessage(Properties.Resources.ErrorFileCorrupted(file), snapStoreException);
                    mainframe.Dispatcher.BeginInvoke(new Action(() => { this.Close(); }));
                } catch (Exception exception) {
                    Tracer.Report("DialogImport.Load", exception);
                    mainframe.ReportException(exception);
                    mainframe.Dispatcher.BeginInvoke(new Action(() => { this.Close(); }));
                }
            }));

            //TextNote validation will instantiate FlowDocument that in some cases required to happened only on STA.
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Name         = "ImportLoader";
            thread.Priority     = ThreadPriority.AboveNormal;
            thread.Start();
        }
 private void ButtonOkClick(object sender, RoutedEventArgs e)
 {
     try {
         this.DialogResult = true;
     } catch (Exception exception) {
         Tracer.Report("DialogImport.ButtonOkClick", exception);
         App.Mainframe.ReportException(exception);
     }
 }
Ejemplo n.º 7
0
 private static FlowDocument LoadXaml(string text)
 {
     try {
         return(XamlReader.Parse(text) as FlowDocument);
     } catch (Exception exception) {
         Tracer.Report("TextNote.SaveXaml", exception);
     }
     return(null);
 }
Ejemplo n.º 8
0
        private string Suggest(string text)
        {
            string expr = text;

            for (int i = text.Length; 0 < i; i--)
            {
                char c = text[i - 1];
                if (!char.IsLetterOrDigit(c) && c != '.' && c != '_')
                {
                    expr = text.Substring(i);
                    break;
                }
            }
            if (!string.IsNullOrWhiteSpace(expr))
            {
                try {
                    int           index  = expr.LastIndexOf('.');
                    string        prefix = expr.Substring(0, Math.Max(0, index));
                    string        sofar  = (index < 0) ? expr : expr.Substring(index + 1);
                    List <string> list;
                    if (this.suggestionExpr != expr || suggestions == null)
                    {
                        if (string.IsNullOrWhiteSpace(prefix))
                        {
                            list = this.scope.GetVariableNames().Where(name => name.StartsWith(sofar, StringComparison.Ordinal)).OrderBy(name => name.Length).ToList();
                        }
                        else
                        {
                            object obj = this.scriptEngine.CreateScriptSourceFromString(prefix, SourceCodeKind.Expression).Execute(this.scope);
                            list = this.scriptEngine.Operations.GetMemberNames(obj).Where(name => name.StartsWith(sofar, StringComparison.Ordinal)).ToList();
                        }
                        this.lastSuggestion = 0;
                    }
                    else
                    {
                        list = this.suggestions;
                        this.lastSuggestion++;
                        if (list.Count <= this.lastSuggestion)
                        {
                            this.lastSuggestion = 0;
                        }
                    }
                    this.suggestions    = list;
                    this.suggestionExpr = expr;
                    if (list != null && this.lastSuggestion < list.Count)
                    {
                        string current = list[this.lastSuggestion];
                        return(current.Substring(sofar.Length));
                    }
                } catch (Exception exception) {
                    Tracer.Report("IronPythonConsole.Suggest", exception);
                }
            }

            return(string.Empty);
        }
Ejemplo n.º 9
0
 private void TextBoxGotKeyboardFocus(object sender, RoutedEventArgs e)
 {
     try {
         if (sender is TextBox textBox && !textBox.AcceptsReturn)
         {
             textBox.SelectAll();
         }
     } catch (Exception exception) {
         Tracer.Report("App.TextBoxGotKeyboardFocus", exception);
     }
 }
Ejemplo n.º 10
0
 internal static void DeleteFile(string file)
 {
     if (Mainframe.IsFileExists(file))
     {
         try {
             File.Delete(file);
         } catch (Exception exception) {
             Tracer.Report("Mainframe.DeleteFile", exception);
         }
     }
 }
Ejemplo n.º 11
0
 private void DescriptorMouseMove(object sender, MouseEventArgs e)
 {
     try {
         if (e.LeftButton == MouseButtonState.Pressed && this.Editor != null)
         {
             this.Editor.DescriptorMouseMove((FrameworkElement)sender, e);
         }
     } catch (Exception exception) {
         Tracer.Report("MainFrame.DescriptorMouseMove", exception);
         this.ReportException(exception);
     }
 }
Ejemplo n.º 12
0
 private void DescriptorMouseUp(object sender, MouseButtonEventArgs e)
 {
     try {
         if (e.ChangedButton == MouseButton.Left && this.Editor != null)
         {
             this.Editor.DescriptorMouseUp();
         }
     } catch (Exception exception) {
         Tracer.Report("MainFrame.DescriptorMouseUp", exception);
         this.ReportException(exception);
     }
 }
Ejemplo n.º 13
0
 private void DiagramDrop(object sender, DragEventArgs e)
 {
     try {
         if (this.Editor != null)
         {
             this.Editor.DiagramDrop(e);
         }
     } catch (Exception exception) {
         Tracer.Report("MainFrame.DiagramDrop", exception);
         this.ReportException(exception);
     }
 }
Ejemplo n.º 14
0
 private void WindowKeyUp(object sender, KeyEventArgs e)
 {
     try {
         if (this.Editor != null)
         {
             this.Editor.DiagramKeyUp(e);
         }
     } catch (Exception exception) {
         Tracer.Report("MainFrame.MainFrameKeyUp", exception);
         this.ReportException(exception);
     }
 }
Ejemplo n.º 15
0
 private void DiagramLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
 {
     try {
         if (this.Editor != null)
         {
             this.Editor.DiagramLostFocus();
         }
     } catch (Exception exception) {
         Tracer.Report("MainFrame.DiagramLostKeyboardFocus", exception);
         this.ReportException(exception);
     }
 }
Ejemplo n.º 16
0
 private void DiagramMouseMove(object sender, MouseEventArgs e)
 {
     try {
         Tracer.Assert(sender == this.Diagram);
         if (e.LeftButton == MouseButtonState.Pressed && this.Editor != null)
         {
             this.Editor.DiagramMouseMove(e);
         }
     } catch (Exception exception) {
         Tracer.Report("MainFrame.DiagramMouseMove", exception);
         this.ReportException(exception);
     }
 }
Ejemplo n.º 17
0
 private void PowerButtonMouseDown(object sender, MouseButtonEventArgs e)
 {
     try {
         Editor current = this.Editor;
         if (e.ChangedButton == MouseButton.Left && current != null)
         {
             current.Power = !current.Power;
         }
     } catch (Exception exception) {
         Tracer.Report("MainFrame.PowerButtonMouseDown", exception);
         this.ReportException(exception);
     }
 }
Ejemplo n.º 18
0
 private void FileChanged(object sender, FileSystemEventArgs e)
 {
     try {
         if (e.ChangeType == WatcherChangeTypes.Changed)
         {
             this.Merge();
         }
         this.NotifyRecentFilesChanged();
     } catch (Exception exception) {
         Tracer.Report("UserSettings.fileChanged", exception);
         //swallow all exceptions here
     }
 }
Ejemplo n.º 19
0
 private void DiagramMouseUp(object sender, MouseButtonEventArgs e)
 {
     try {
         Tracer.Assert(sender == this.Diagram);
         if (e.ChangedButton == MouseButton.Left && this.Editor != null)
         {
             this.Editor.DiagramMouseUp(e);
         }
     } catch (Exception exception) {
         Tracer.Report("MainFrame.DiagramMouseUp", exception);
         this.ReportException(exception);
     }
 }
Ejemplo n.º 20
0
 private void TextBoxPreviewMouseDown(object sender, RoutedEventArgs e)
 {
     try {
         if (sender is TextBox textBox && !textBox.AcceptsReturn && !textBox.IsFocused)
         {
             textBox.Focus();
             textBox.SelectAll();
             e.Handled = true;
         }
     } catch (Exception exception) {
         Tracer.Report("App.TextBoxPreviewMouseDown", exception);
     }
 }
 private void ButtonUncheckAllClick(object sender, RoutedEventArgs e)
 {
     try {
         if (this.List != null)
         {
             foreach (CircuitInfo info in this.List)
             {
                 info.SetImport(false);
             }
         }
     } catch (Exception exception) {
         Tracer.Report("DialogImport.ButtonCheckAllClick", exception);
         App.Mainframe.ReportException(exception);
     }
 }
Ejemplo n.º 22
0
        private void SetMessage(string text)
        {
            try {
                List <string> list      = new List <string>();
                int           start     = 0;
                Regex         hyperlink = new Regex("<Hyperlink.*?</Hyperlink>", RegexOptions.Compiled | RegexOptions.Multiline);
                foreach (Match m in hyperlink.Matches(text))
                {
                    if (0 < m.Index - start)
                    {
                        list.Add(text.Substring(start, m.Index - start));
                    }
                    list.Add(text.Substring(m.Index, m.Length));
                    start = m.Index + m.Length;
                }
                if (start < text.Length)
                {
                    list.Add(text.Substring(start));
                }

                List <Inline> inlines = new List <Inline>();
                Regex         parts   = new Regex("NavigateUri=\"(?<uri>.*)\">(?<text>.*)</Hyperlink>", RegexOptions.Compiled | RegexOptions.Multiline);
                foreach (string s in list)
                {
                    if (hyperlink.IsMatch(s))
                    {
                        Match     m    = parts.Match(s);
                        string    uri  = m.Groups["uri"].Value;
                        string    txt  = m.Groups["text"].Value;
                        Hyperlink link = new Hyperlink(new Run(txt))
                        {
                            NavigateUri = new Uri(uri)
                        };
                        this.message.Inlines.Add(link);
                    }
                    else
                    {
                        this.message.Inlines.Add(new Run(s));
                    }
                }
                this.message.Inlines.AddRange(inlines);
                return;
            } catch (Exception exception) {
                Tracer.Report("DialogMessage.SetMessage", exception);
            }
            this.message.Inlines.Clear();
            this.message.Text = text;
        }
Ejemplo n.º 23
0
 public static bool IsDirectoryPathValid(string path)
 {
     if (path != null && path.Length > 0)
     {
         try {
             if (Directory.Exists(Path.GetFullPath(path)))
             {
                 return(true);
             }
         } catch (Exception exception) {
             Tracer.Report("Mainframe.IsPathValid", exception);
             App.Mainframe.ReportException(exception);
         }
     }
     return(false);
 }
Ejemplo n.º 24
0
 private static FlowDocument LoadPackage(string text)
 {
     if (!string.IsNullOrEmpty(text))
     {
         try {
             using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(text))) {
                 FlowDocument document = new FlowDocument();
                 TextRange    range    = new TextRange(document.ContentStart, document.ContentEnd);
                 range.Load(stream, DataFormats.XamlPackage);
                 return(document);
             }
         } catch (Exception exception) {
             Tracer.Report("TextNote.LoadPackage", exception);
         }
     }
     return(null);
 }
Ejemplo n.º 25
0
 public void AutoSave()
 {
     try {
         if (!string.IsNullOrEmpty(this.File) && this.HasChanges && this.autoSavedVersion != this.CircuitProject.Version)
         {
             string file = Mainframe.AutoSaveFile(this.File);
             if (!string.IsNullOrEmpty(file))
             {
                 Mainframe.DeleteFile(file);
                 this.CircuitProject.SaveSnapshot(file);
                 Mainframe.Hide(file);
             }
             this.autoSavedVersion = this.CircuitProject.Version;
         }
     } catch (Exception exception) {
         Tracer.Report("Editor.AutoSave", exception);
     }
 }
Ejemplo n.º 26
0
 protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
 {
     base.OnClosing(e);
     try {
         if (!this.EnsureSaved())
         {
             e.Cancel = true;
         }
         else
         {
             IronPythonConsole.Stop();
             Settings.User.Save();
             this.autoSaveTimer?.Dispose();
             this.autoSaveTimer = null;
         }
     } catch (Exception exception) {
         Tracer.Report("Mainframe.OnClosing", exception);
         this.ReportException(exception);
     }
 }
Ejemplo n.º 27
0
 private void RunningMapTreeViewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e)
 {
     try {
         if (sender == e.OriginalSource && e.NewValue != null && !object.ReferenceEquals(e.OldValue, e.NewValue))
         {
             CircuitMap map = e.NewValue as CircuitMap;
             if (map != null)
             {
                 TreeViewItem item = this.Container((TreeView)sender, map);
                 if (item != null)
                 {
                     item.IsExpanded = true;
                     item.BringIntoView();
                 }
             }
         }
     } catch (Exception exception) {
         Tracer.Report("MainFrame.RunningMapTreeViewSelectedItemChanged", exception);
         this.ReportException(exception);
     }
 }
Ejemplo n.º 28
0
 protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
 {
     base.OnPropertyChanged(e);
     try {
         if (!this.changing)
         {
             this.changing = true;
             if (e.Property == SpectrumSlider.ValueProperty)
             {
                 this.Hue = (double)e.NewValue;
             }
             else if (e.Property == SpectrumSlider.HueProperty)
             {
                 this.Value = (double)e.NewValue;
             }
         }
     } catch (Exception exception) {
         Tracer.Report("SpectrumSlider.OnPropertyChanged", exception);
     } finally {
         this.changing = false;
     }
 }
Ejemplo n.º 29
0
        private void Edit(string file)
        {
            this.ResetAutoSaveTimer();
            Editor circuitEditor;

            try {
                circuitEditor = new Editor(this, file);
            } catch (SnapStoreException snapStoreException) {
                Tracer.Report("Mainframe.Edit", snapStoreException);
                throw new CircuitException(Cause.CorruptedFile, snapStoreException, Properties.Resources.ErrorFileCorrupted(file));
            }
            if (this.Editor != null)
            {
                this.Editor.Power = false;
            }
            if (circuitEditor.File != null)
            {
                Settings.User.AddRecentFile(circuitEditor.File);
            }
            this.Dispatcher.BeginInvoke(new Action(() => this.ScrollOffset = new Point(0, 0)), System.Windows.Threading.DispatcherPriority.Normal);
            this.Editor = circuitEditor;
            this.Status = Properties.Resources.Ready;
        }
Ejemplo n.º 30
0
        public Mainframe()
        {
            App.Mainframe     = this;
            this.ProjectWidth = new SettingsGridLengthCache(Settings.User, "Mainframe.ProjectWidth", "0.25*");
            this.DiagramWidth = new SettingsGridLengthCache(Settings.User, "Mainframe.DiagramWidth", "0.75*");

            // Create this command here as it used multiple times not like all other commands only onces when menu is created.
            this.CommandOpenRecent = new LambdaUICommand(Properties.Resources.CommandFileOpenRecent, file => this.OpenRecent(file as string));

            this.DataContext = this;
            this.InitializeComponent();

            this.autoSaveTimer = new Timer(o => this.Editor?.AutoSave(), null, Timeout.Infinite, Timeout.Infinite);

            Thread thread = new Thread(new ThreadStart(() => {
                try {
                    string file = App.CurrentApp.FileToOpen;
                    if (string.IsNullOrEmpty(file) || !File.Exists(file))
                    {
                        if (Settings.User.LoadLastFileOnStartup)
                        {
                            file = Settings.User.RecentFile();
                        }
                    }
                    if (!string.IsNullOrEmpty(file) && File.Exists(file))
                    {
                        this.Edit(file);
                    }
                    else
                    {
                        this.Edit(null);
                    }
                    if (!string.IsNullOrEmpty(App.CurrentApp.CommandLineErrors))
                    {
                        this.ShowErrorMessage(App.CurrentApp.CommandLineErrors, null);
                    }
                    else if (!string.IsNullOrEmpty(App.CurrentApp.ScriptToRun))
                    {
                        this.Dispatcher.BeginInvoke(new Action(() => IronPythonConsole.Run(this, App.CurrentApp.ScriptToRun)));
                    }
                    else
                    {
                        // Reuse this thread to check if there are any translations requests are pending
                        DialogAbout.CheckTranslationRequests(this.Dispatcher);
                    }
                } catch (Exception exception) {
                    Tracer.Report("Mainframe.PostLoaded", exception);
                    this.ReportException(exception);
                    this.Edit(null);
                }
            }));

            //TextNote validator will instantiate FlowDocument that in some cases required to happened only on STA.
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Name         = "ProjectLoader";
            thread.Priority     = ThreadPriority.AboveNormal;
            thread.Start();

            // Check for new available version
            Thread versionThread = new Thread(new ThreadStart(() => DialogAbout.CheckVersion(this.Dispatcher)))
            {
                IsBackground = true,
                Name         = "CheckVersion",
                Priority     = ThreadPriority.BelowNormal
            };

            versionThread.Start();

                        #if DEBUG && false
            this.Loaded += (object sender, RoutedEventArgs e) => {
                Menu   menu = (Menu)((Grid)this.Content).Children[0];
                string text = "Test";
                menu.Items.Add(new MenuItem()
                {
                    Header = text, Command = new LambdaUICommand(text, o => {
                        DialogMessage.Show(this,
                                           "hello",
                                           "world <Hyperlink NavigateUri=\"http://www.rbc.ru\">link 1</Hyperlink> or <Hyperlink NavigateUri=\"http://www.lenta.ru\">link 2</Hyperlink> hello <Hyperlink NavigateUri=\"http://www.cnn.com\">and link 3</Hyperlink> end",
                                           "details",
                                           MessageBoxImage.Error,
                                           MessageBoxButton.OKCancel
                                           );
                    })
                });
            };
                        #endif
        }