private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
 {
     TextPointer start = this._rtb.Document.ContentStart,
                 end = this._rtb.Document.ContentEnd;
     TextRange tr = new TextRange(start, end);
     tr.Select(start, end);
     if (tr.CanSave(DataFormats.Xaml))
     {
         FileInfo fi = new FileInfo("proba.xaml");
         if (fi.Exists)
             fi.Delete();
         FileStream fs = fi.Create();
         tr.Save(fs, DataFormats.Xaml);
         fs.Flush();
         fs.Close();
     }
 }
 private void Paste_RequestNavigate(object sender, RequestNavigateEventArgs e)
 {
     TextPointer start = this._rtb.Document.ContentStart,
                 end = this._rtb.Document.ContentEnd;
     TextRange tr = new TextRange(start, end);
     tr.Select(start, end);
     MemoryStream ms;
     StringBuilder sb = new StringBuilder();
     foreach (String dataFormat in _listOfFormats)
     {
         if (tr.CanSave(dataFormat))
         {
             ms = new MemoryStream();
             tr.Save(ms, dataFormat);
             ms.Seek(0, SeekOrigin.Begin);
             sb.AppendLine(dataFormat);
             foreach (char c in ms.ToArray().Select<byte, char>((b) => (char)b))
             {
                 sb.Append(c);
             }
             sb.AppendLine();
         }
         //_tb.Text = sb.ToString();
     }
 }
        internal string Format(CharPosition start, CharPosition end)
        {
            Paragraph paragraph = new Paragraph();
            for (int lineIndex = start.CharacterY; lineIndex <= end.CharacterY; ++lineIndex)
            {
                string lineContent = textBuffer.GetLineContent(lineIndex);
                if (string.IsNullOrEmpty(lineContent))
                    continue;

                int charStart = 0, lineLength = lineContent.Length;
                if (lineIndex == start.CharacterY) // Starting line.
                    charStart = start.CharacterX;
                if (lineIndex == end.CharacterY) // Ending line.
                    lineLength = end.CharacterX;

                for (int charIndex = charStart; charIndex < lineLength; ++charIndex)
                {
                    CodeFragment fragment = null;
                    int fragmentWidth = codeFragmentManager.GetFragment(
                        charIndex, lineIndex, out fragment);

                    if (null == fragment)
                    {
                        // We have encountered a whitespace character (e.g. space, tab, etc),
                        // scan forward until we get a non-whitespace character, then flush
                        // whatever we have found (whitespaces) into the paragraph at one go.
                        //
                        int startIndex = charIndex;
                        while (charIndex < lineLength)
                        {
                            fragmentWidth = codeFragmentManager.GetFragment(
                                charIndex, lineIndex, out fragment);

                            if (fragmentWidth > 0 && (null != fragment))
                            {
                                string text = lineContent.Substring(startIndex, charIndex - startIndex);
                                FormatFragment(null, text, paragraph);
                                break;
                            }

                            charIndex = charIndex + 1;
                        }

                        if (charIndex >= lineLength)
                        {
                            // If there was no fragment at the time we reached the
                            // end of line, then flush out the remaining whitespaces,
                            // if there is any.
                            //
                            if (null == fragment && (startIndex < charIndex))
                            {
                                FormatFragment(null, lineContent.Substring(startIndex,
                                    charIndex - startIndex), paragraph);
                            }

                            break; // Reach the end of line.
                        }
                    }

                    int offsetToNextChar = fragmentWidth - 1;
                    if ((charIndex + fragmentWidth) > (lineLength - 1))
                    {
                        fragmentWidth = (lineLength - 1) - charIndex;
                        offsetToNextChar = fragmentWidth - 1;
                    }

                    // Initialize the text store.
                    string textContent = string.Empty;
                    textContent = lineContent.Substring(charIndex,
                        ((fragmentWidth == 0) ? 1 : fragmentWidth));

                    FormatFragment(fragment, textContent, paragraph);
                    if (fragmentWidth > 0)
                        charIndex += offsetToNextChar;
                }
            }

            FlowDocument flowDocument = new FlowDocument();
            flowDocument.TextAlignment = TextAlignment.Left;
            flowDocument.FontSize = 11;
            flowDocument.FontFamily = new FontFamily("Consolas");
            flowDocument.FlowDirection = FlowDirection.LeftToRight;
            flowDocument.TextAlignment = TextAlignment.Left;
            flowDocument.Blocks.Add(paragraph);

            TextRange range = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
            if (range.CanSave(DataFormats.Rtf))
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    range.Save(stream, DataFormats.Rtf);
                    stream.Flush();
                    return (Encoding.UTF8.GetString(stream.ToArray()));
                }
            }

            return string.Empty;
        }
Beispiel #4
0
        public void Save(TextRange storyText, string storyFileName)
        {
            // Data format for the story file.
            string dataFormat = DataFormats.Rtf;

            // Absolute path to the application folder
            string appLocation = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                App.ApplicationFolderName);

            // Absolute path to the stories folder
            string storiesLocation = Path.Combine(appLocation, Const.StoriesFolderName);

            // Fully qualified path to the new story file
            storyFileName = GetSafeFileName(storyFileName);
            string storyAbsolutePath = Path.Combine(storiesLocation, storyFileName);

            try
            {
                // Create the stories directory if it doesn't exist
                if (!Directory.Exists(storiesLocation))
                    Directory.CreateDirectory(storiesLocation);

                // Open the file for writing the richtext
                using (FileStream stream = File.Create(storyAbsolutePath))
                {
                    // Store the relative path to the story
                    this.relativePath = Path.Combine(Const.StoriesFolderName, storyFileName);

                    // Save the story to disk
                    if (storyText.CanSave(dataFormat))
                        storyText.Save(stream, dataFormat);
                }
            }
            catch
            {
                // Could not save the story. Handle all exceptions
                // the same, ignore and continue.
            }
        }
Beispiel #5
0
        public void Save(string storyText, string storyFileName)
        {
            // Data format for the story file.
            string dataFormat = DataFormats.Rtf;

            // Absolute path to the application folder
            string appLocation = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                App.ApplicationFolderName);

            // Absolute path to the stories folder
            string storiesLocation = Path.Combine(appLocation, Const.StoriesFolderName);

            // Fully qualified path to the new story file
            storyFileName = GetSafeFileName(storyFileName);
            string storyAbsolutePath = Path.Combine(storiesLocation, storyFileName);

            // Convert the text into a TextRange.  This will allow saving the story to disk as RTF.
            TextBlock block = new System.Windows.Controls.TextBlock();
            block.Text = storyText;
            TextRange range = new TextRange(block.ContentStart, block.ContentEnd);

            try
            {
                // Create the stories directory if it doesn't exist
                if (!Directory.Exists(storiesLocation))
                    Directory.CreateDirectory(storiesLocation);

                // Open the file for writing the richtext
                using (FileStream stream = File.Create(storyAbsolutePath))
                {
                    // Store the relative path to the story
                    this.relativePath = Path.Combine(Const.StoriesFolderName, storyFileName);

                    // Save the story to disk
                    if (range.CanSave(dataFormat))
                        range.Save(stream, dataFormat);
                }
            }
            catch
            {
                // Could not save the story. Handle all exceptions
                // the same, ignore and continue.
            }
        }
Beispiel #6
0
        private void MenuItemSpeichern_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (SaveTxtChanges())
                {
                    SaveFileDialog save = new SaveFileDialog();
                    save.Filter = "Earthdawncharakterarchiv (*.edz)|*.edz";
                    if (save.ShowDialog() == true)
                    {
                        Directory.CreateDirectory(save.FileName.Replace(save.SafeFileName, "") + "\\TmpED\\");
                        using(ZipArchive zip = ZipFile.Open(save.FileName, ZipArchiveMode.Update))
                        {
                            ZipArchiveEntry bin = zip.GetEntry("Charakterdaten.bin");
                            ZipArchiveEntry xaml = zip.GetEntry("Charakternotizen.xaml");
                            if (bin != null)
                            {
                                bin.Delete();
                            }
                            if (xaml != null)
                            {
                                xaml.Delete();
                            }

                            FileStream sfs = new FileStream(save.FileName.Replace(save.SafeFileName, "") + "\\TmpED\\Charakterdaten.bin", FileMode.Create, FileAccess.ReadWrite);
                            BinaryFormatter formatter = new BinaryFormatter();
                            formatter.Serialize(sfs, aktChar);
                            sfs.Close();
                            zip.CreateEntryFromFile(save.FileName.Replace(save.SafeFileName, "") + "\\TmpED\\Charakterdaten.bin", "Charakterdaten.bin");

                            FileStream nfs = new FileStream(save.FileName.Replace(save.SafeFileName, "") + "\\TmpED\\Charakternotizen.xaml", FileMode.Create, FileAccess.ReadWrite);
                            TextRange rng = new TextRange(Notizen.ContentStart, Notizen.ContentEnd);
                            if (rng.CanSave(DataFormats.Xaml))
                            {
                                rng.Save(nfs, DataFormats.Xaml);
                            }
                            nfs.Close();
                            zip.CreateEntryFromFile(save.FileName.Replace(save.SafeFileName, "") + "\\TmpED\\Charakternotizen.xaml", "Charakternotizen.xaml");
                        }
                        Directory.Delete(save.FileName.Replace(save.SafeFileName, "") + "\\TmpED\\", true);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            } 
        }
Beispiel #7
0
        //public event ScriptParsedHandler OnScriptParsed;
        //private void ScriptParsed()
        //{
        //    if (OnScriptParsed != null)
        //        OnScriptParsed(this, new ScriptParsedEventArgs());
        //}

        //public event ModelChangedHandler OnModelChangedByScript;
        //private void ModelChangedByScript(XbimModel newModel)
        //{
        //    if (OnModelChangedByScript != null)
        //        OnModelChangedByScript(this, new ModelChangedEventArgs(newModel));
        //}


        private void OnClick_SaveScript(object sender, RoutedEventArgs e)
        {
            string dataFormat = "Text";
            TextRange txtRange = new TextRange(ScriptText.Document.ContentStart, ScriptText.Document.ContentEnd);
            string script = txtRange.Text.Trim();
            if (String.IsNullOrEmpty(script))
            {
                System.Windows.MessageBox.Show("There is no script to save.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            System.Windows.Forms.SaveFileDialog dlg = new System.Windows.Forms.SaveFileDialog();
            dlg.AddExtension = true;
            dlg.DefaultExt = ".bql";
            dlg.Filter = "Building Query Language|*.bql";
            dlg.FilterIndex = 0;
            dlg.Title = "Set file name of the script";
            dlg.OverwritePrompt = true;
            dlg.ValidateNames = true;
            dlg.FileOk += delegate(object s, System.ComponentModel.CancelEventArgs eArg)
            {
                var name = dlg.FileName;
                FileStream file = null;
                try
                {
                    file = new FileStream(name, FileMode.Create);
                    if (txtRange.CanSave(dataFormat))
                    {
                        txtRange.Save(file, dataFormat);
                    }

                    this.Title = "BQL Console - " + System.IO.Path.GetFileName(name);
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show(string.Format("Saving script to file failed with message : {0}.", ex.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                finally
                {
                    if (file != null)
                        file.Close();
                }
            };
            dlg.ShowDialog();
        }