how do I use syntax editor with in mulliple document windows

Docking/MDI for WPF Forum

Posted 12 years ago by David Freibrun
Version: 12.2.0570
Avatar

I want to give user ability to open multiple files and edit them using Syntax Editor control.

I have this tabbedmdihost defined in MainControl.xaml.. It has been simplified below just to give idea.

<docking:TabbedMdiHost x:Name="tabbedMdiHost">
<docking:TabbedMdiContainer>
<docking:DocumentWindow Title="WelcomeDocument.rtf" Description="Rich-text document" FileName="C:\Users\Actipro\My Documents\x.rtf">
  <editor:SyntaxEditor x:Name="editor"
     AreWordWrapGlyphsVisible="True"
    IsLineNumberMarginVisible="True"
    ZoomLevel="{Binding ElementName=scaleSlider, Path=Value}"
    DocumentChanged="OnSyntaxEditorDocumentChanged"
    DocumentIsModifiedChanged="OnSyntaxEditorDocumentIsModifiedChanged"
    DocumentParseDataChanged="OnSyntaxEditorDocumentParseDataChanged"
    IsOverwriteModeActiveChanged="OnSyntaxEditorIsOverwriteModeActiveChanged"
    UserInterfaceUpdate="OnSyntaxEditorUserInterfaceUpdate"
    ViewSelectionChanged="OnSyntaxEditorViewSelectionChanged"
    ViewSplitAdded="OnSyntaxEditorViewSplitAdded"
    ViewSplitMoved="OnSyntaxEditorViewSplitMoved"
    ViewSplitRemoved="OnSyntaxEditorViewSplitRemoved"
    Width="1870"
    Height="880"
    MinWidth="700"
    MinHeight="500"
    MaxWidth="1870"
    MaxHeight="900"
    WordWrapMode="None">
    <editor:EditorDocument xml:space="preserve" FileName="Document1.txt" />
  </editor:SyntaxEditor>
</docking:DocumentWindow>
</docking:TabbedMdiContainer>
</docking:TabbedMdiHost>

 

In my MainControl.xaml.cs I have :

private static void OnNewExecuted(object sender, ExecutedRoutedEventArgs e)

{
     MainControl control = (MainControl)sender;

      control.CreateSyntaxEditorDocumentWindow();
}

private DocumentWindow CreateSyntaxEditorDocumentWindow()
{
string filename = "newachfile.ach";

editor.Document.SetText(String.Empty);

DocumentWindow documentWindow = new DocumentWindow(dockSite, null, System.IO.Path.GetFileName(filename),
new BitmapImage(new Uri("/Resources/Images/TextDocument16.png", UriKind.Relative)), editor);

documentWindow.Description = "New Ach File";
documentWindow.FileName = filename;

documentWindow.Activate();

return documentWindow;
}

When I open new file, it shows up and use the syntax editor object defined in MainControl.xaml, but then when I switch to one of the previously entered files on the tabs,  nothing happens with documentWindow. It seems that I have to create a new syntax editor object for each document window. But how do I set that up dynamically with all the frills that were in MainControl.xaml?  

Comments (9)

Answer - Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi David,

A SyntaxEditor is effectively like a TextBox but with a ton of features for code editing.  So think about it that way.  If you want to have multiple code editor document windows, then you would need a new SyntaxEditor instance for each document window.

You can just set the properties in code and tie up the events using standard event handler add syntax for C# ("+=").  Just be sure that you handle the DockSite.WindowClosed event so that when a window closes, you detach from those event handlers (with "-=" syntax) so you don't have any memory leaks.

On a side note, the Docking/MDI product doesn't support fixed/min/max content sizes for docking windows.  So do not set a Width/Height/MinWidth, etc. on the SyntaxEditor.


Actipro Software Support

Posted 12 years ago by David Freibrun
Avatar

Ok.. I follow what you are saying but I need to translate the XAML definition for editor to assignments in my code. If I take below and try to reproduce it in code

 

<editor:SyntaxEditor x:Name="editor"
AreWordWrapGlyphsVisible="True"
IsLineNumberMarginVisible="True"
ZoomLevel="{Binding ElementName=scaleSlider, Path=Value}"
DocumentChanged="OnSyntaxEditorDocumentChanged"
DocumentIsModifiedChanged="OnSyntaxEditorDocumentIsModifiedChanged"
DocumentParseDataChanged="OnSyntaxEditorDocumentParseDataChanged"
IsOverwriteModeActiveChanged="OnSyntaxEditorIsOverwriteModeActiveChanged"
UserInterfaceUpdate="OnSyntaxEditorUserInterfaceUpdate"
ViewSelectionChanged="OnSyntaxEditorViewSelectionChanged"
ViewSplitAdded="OnSyntaxEditorViewSplitAdded"
ViewSplitMoved="OnSyntaxEditorViewSplitMoved"
ViewSplitRemoved="OnSyntaxEditorViewSplitRemoved"
Width="1870"
Height="880"
MinWidth="700"
MinHeight="500"
MaxWidth="1870"
MaxHeight="900"
WordWrapMode="None">
<editor:EditorDocument xml:space="preserve" FileName="Document1.txt" />
</editor:SyntaxEditor> 

the best I could come up with is this

 

editor.AreWordWrapGlyphsVisible = true;
editor.IsLineNumberMarginVisible = true;
// editor.ZoomLevel = "{Binding ElementName=scaleSlider, Path=Value}";
editor.DocumentChanged += new EventHandler<EditorDocumentChangedEventArgs>(OnSyntaxEditorDocumentChanged);
editor.DocumentIsModifiedChanged += new EventHandler<>(OnSyntaxEditorDocumentIsModifiedChanged);
editor.DocumentParseDataChanged += new EventHandler<>(OnSyntaxEditorDocumentParseDataChanged);
editor.IsOverwriteModeActiveChanged += new EventHandler<>(OnSyntaxEditorIsOverwriteModeActiveChanged);
editor.UserInterfaceUpdate += new EventHandler<>(OnSyntaxEditorUserInterfaceUpdate);
editor.ViewSelectionChanged += new EventHandler<>(OnSyntaxEditorViewSelectionChanged);
editor.ViewSplitAdded += new EventHandler<>(OnSyntaxEditorViewSplitAdded);
editor.ViewSplitMoved += new EventHandler<>(OnSyntaxEditorViewSplitMoved);
editor.ViewSplitRemoved += new EventHandler<>(OnSyntaxEditorViewSplitRemoved);
//editor.Width = 1870;
//editor.Height = 880;
//editor.MinWidth = 700;
//editor.MinHeight = 500;
//editor.MaxWidth = 1870;
//editor.MaxHeight = 900;
editor.WordWrapMode = WordWrapMode.None;

I don't know what "type" will work in the <>.. It doesn't seem to like what is available and something needs to be there. The only one that worked was .<EditorDocumentChangedEventArgs>. Also I don't know how to set editor.ZoomLevel to reproduce what was in the xaml.

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

HI David,

You can look at our documentation that comes with the product.  It includes a full class library where you can look up what delegate types each event is.

For bindings, there are a lot of resources on the web about programmatically creating a WPF binding.  Just Google something like that phrase to find details.


Actipro Software Support

Posted 12 years ago by David Freibrun
Avatar

I have looked at the class library and on the web i don't see anything that works.

 

For example,

 

how would you translate 

 

ZoomLevel="{Binding ElementName=scaleSlider, Path=Value}"

to code?

editor.ZoomLevel = ?;

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi David,

This web page gives a good discussion on bindings and shows an example of how to programmatically create one, similar to what you are trying to do:

http://blogs.msdn.com/b/wpfsdk/archive/2006/10/19/wpf-basic-data-binding-faq.aspx


Actipro Software Support

Posted 12 years ago by David Freibrun
Avatar

That actually worked. I used the following

Binding myBinding = new Binding("value");

myBinding.ElementName = "scalesSlider";

editor.SetBinding(SyntaxEditor.ZoomLevelProperty, myBinding);

Still can't figure out how to assing delegate types and eventhandler though.

Example, in XAML

ViewSplitAdded = "OnSyntaxEditorViewSplitAdded"

but how is this expressed in code? I tried this

editor.ViewSplitAdded += new EventHandler<EditorViewSplitEventArgs>(OnSyntaxEditorViewSplitAdded)

but it doesn't work. Am I missing something? It says I might be missing an assembly reference, but I'm not finding any that work. and looking in the class library definition.

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi David,

First, if you use Visual Studio it will automatically generate the right side of += for you if you let its Intellisense work by pressing spacebar then Tab after the +=.  But anyhow, the class library in our documentation file gives the definition of this event as a RoutedEventHandler.  Or you can even hover over the ViewSplitAdded identifier to see that in the VS quick info.


Actipro Software Support

Posted 12 years ago by David Freibrun
Avatar

Thank you. Another problem solved.

Lastly, I'm defining SyntaxEditor editor objects inside DocumentWindow and it looks good. However, the various services that are registered with my syntax language are not working inside the new DocumentWindow. I still see them working on my document that was opened in the maincontrol constructor. So I have this defined in the MainControl()

var language = new xxxx.Text.Languages.XYZ.XYZSyntaxLanguage();

this.LoadLanguage(language);

new DisplayItemClassificationTypeProvider().RegisterAll();

this.Loaded += new RoutedEventHandler(OnLoaded);

So it seems it applies all the syntax language services to the SyntaxEditor editor object defined in MainControl.xaml. How can get these services to work in each new document window opened? I tried to reinstantiate and load the language but it doesn't work.

Posted 12 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi David,

It's hard to know without seeing your code but I suspect that based on what you are saying, certain portions of your code aren't getting called in the case where you have it in a document window.  SyntaxEditor will perform the same in a document window or main window as long as you have the same language wired up and same options set.

Also note that you can share language instances amongst documents.  So if you only have one language your app uses, create it at app startup and use the same global instance of it on each SyntaxEditor document.

Other than that, I'd suggest you debug any event handlers you have to make sure you are hitting them properly in the bad scenario.


Actipro Software Support

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.