private void ButtonEditEventHandlers_Click(object sender, EventArgs e) { var handlers = GetEventHandlersToEdit(); if (handlers.Count == 0) { return; } var items = new List <KryptonContextMenuItemBase>(); foreach (var handler in handlers) { var text = "\'" + handler.GetPathFromRoot(true) + "\'"; var item = new KryptonContextMenuItem(text, null, delegate(object s, EventArgs e2) { var componentToSelect = (Component)((KryptonContextMenuItem)s).Tag; //if component in the flow graph, then activate flow graph window var parentNode = componentToSelect.Parent as Component_FlowGraphNode; if (parentNode != null) { var graph = parentNode.Parent as Component_FlowGraph; if (graph != null) { EditorAPI.OpenDocumentWindowForObject(Owner.DocumentWindow.Document, graph); } } //select object Owner.DocumentWindow.SelectObjects(new object[] { componentToSelect }); }); item.Tag = handler; items.Add(item); } EditorContextMenuWinForms.Show(items, Owner); }
private void ButtonAddEventHandler_Click(object sender, EventArgs e) { var items = new List <KryptonContextMenuItemBase>(); var subscribeTo = GetOneControlledObject <Component>(); if (subscribeTo == null) { return; } //add handler to C# class { bool enable = false; string className = null; string csharpFileName = null; var fileName = subscribeTo.ParentRoot.HierarchyController?.CreatedByResource?.Owner.Name; if (!string.IsNullOrEmpty(fileName)) { className = subscribeTo.ParentRoot.GetType().Name; try { //find by same class name if (string.Compare(Path.GetFileNameWithoutExtension(fileName), className, true) == 0) //if( Path.GetFileNameWithoutExtension( fileName ) == className ) { csharpFileName = VirtualPathUtility.GetRealPathByVirtual(Path.ChangeExtension(fileName, "cs")); if (File.Exists(csharpFileName)) { enable = true; } } //find by same file name if (!enable) { csharpFileName = VirtualPathUtility.GetRealPathByVirtual(Path.Combine(Path.GetDirectoryName(fileName), className + ".cs")); if (File.Exists(csharpFileName)) { enable = true; } } } catch { } } var item = new KryptonContextMenuItem(Translate("Add Handler to C# File"), null, delegate(object s, EventArgs e2) { AddHandlerToSharpClass(subscribeTo, csharpFileName, className); }); item.Enabled = enable; items.Add(item); } //add handler to C# script { var groupItem = new KryptonContextMenuItem(Translate("Add Handler to C# Script"), null); var childItems = new List <KryptonContextMenuItemBase>(); //create new C# script { var itemsData = new List <(string, Component)>(); itemsData.Add((Translate("Add C# Script in This Component"), subscribeTo)); if (subscribeTo != subscribeTo.ParentRoot) { itemsData.Add((Translate("Add C# Script in the Root Component"), subscribeTo.ParentRoot)); } foreach (var itemData in itemsData) { var childItem = new KryptonContextMenuItem(itemData.Item1, null, delegate(object s, EventArgs e2) { var parent = (Component)((KryptonContextMenuItem)s).Tag; var document = Owner.DocumentWindow.Document; //create script var script = parent.CreateComponent <Component_CSharpScript>(enabled: false); script.Name = script.BaseType.GetUserFriendlyNameForInstance(); script.Code = "class _Temp{\r\n}"; script.Enabled = true; //activate flow graph window var scriptDocumentWindow = EditorAPI.OpenDocumentWindowForObject(document, script) as Component_CSharpScript_DocumentWindow; Owner.DocumentWindow.SelectObjects(new object[] { script }); GetMethodInfo(subscribeTo, out var parameters, out var parametersSignature); var methodNameNotUnique = subscribeTo.Name.Replace(" ", "") + "_" + _event.Name; var methodName = methodNameNotUnique; var methodSignature = $"method:{methodName}({parametersSignature})"; if (!scriptDocumentWindow.ScriptEditorControl.AddMethod(methodName, parameters, out var error)) { Log.Warning("Unable to add a code of the method. " + error); //!!!! } //fix code try { scriptDocumentWindow.ScriptEditorControl.GetCode(out var code); code = code.Replace("class _Temp{", ""); var index = code.LastIndexOf('}'); code = code.Substring(0, index); string newCode = ""; var lines = code.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { newCode += line.Trim() + "\r\n"; } script.Code = newCode; } catch (Exception e3) { Log.Warning("Unable to fix code of the method. " + e3.Message); //!!!! } //create event handler { var handler = script.CreateComponent <Component_EventHandler>(enabled: false); handler.Name = handler.BaseType.GetUserFriendlyNameForInstance() + " " + _event.Name; handler.Event = ReferenceUtility.MakeReference <ReferenceValueType_Event>(null, ReferenceUtility.CalculateThisReference(handler, subscribeTo, _event.Signature)); handler.HandlerMethod = ReferenceUtility.MakeReference <ReferenceValueType_Method>(null, ReferenceUtility.CalculateThisReference(handler, script, methodSignature)); handler.Enabled = true; } //undo var action = new UndoActionComponentCreateDelete(document, new Component[] { script }, true); document.UndoSystem.CommitAction(action); document.Modified = true; }); childItem.Tag = itemData.Item2; childItems.Add(childItem); } } //add handler to one of already created C# scripts foreach (var script in subscribeTo.ParentRoot.GetComponents <Component_CSharpScript>(false, true)) { if (script.TypeSettingsIsPublic()) { var text = string.Format(Translate("Add Handler to \'{0}\'"), script.GetPathFromRoot(true)); var item = new KryptonContextMenuItem(text, null, delegate(object s, EventArgs e2) { var script2 = (Component_CSharpScript)((KryptonContextMenuItem)s).Tag; var document = Owner.DocumentWindow.Document; var oldCode = script2.Code; script2.Code = "class _Temp{\r\n}"; //activate flow graph window var scriptDocumentWindow = EditorAPI.OpenDocumentWindowForObject(document, script2) as Component_CSharpScript_DocumentWindow; Owner.DocumentWindow.SelectObjects(new object[] { script2 }); GetMethodInfo(subscribeTo, out var parameters, out var parametersSignature); //get unique method name string methodName; string methodSignature; { var methodNameNotUnique = subscribeTo.Name.Replace(" ", "") + "_" + _event.Name; for (int n = 1; ; n++) { methodName = methodNameNotUnique; if (n != 1) { methodName += n.ToString(); } methodSignature = $"method:{methodName}({parametersSignature})"; bool found = false; if (script2.CompiledScript != null) { foreach (var m in script2.CompiledScript.Methods) { if (m.Name == methodName) { found = true; } } } if (!found) { break; } //if( subscribeTo.ParentRoot.MetadataGetMemberBySignature( methodSignature ) == null ) // break; } } //GetMethodInfo( subscribeTo, out var parameters, out var methodName, out var methodSignature ); if (!scriptDocumentWindow.ScriptEditorControl.AddMethod(methodName, parameters, out var error)) { Log.Warning("Unable to add a code of the method. " + error); //!!!! } //set Code try { scriptDocumentWindow.ScriptEditorControl.GetCode(out var code); code = code.Replace("class _Temp{", ""); var index = code.LastIndexOf('}'); code = code.Substring(0, index); string newCode = ""; var lines = code.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { newCode += line.Trim() + "\r\n"; } script2.Code = oldCode + "\r\n" + newCode; } catch (Exception e3) { Log.Warning("Unable to fix code of the method. " + e3.Message); //!!!! } //create event handler Component_EventHandler handler; { handler = script2.CreateComponent <Component_EventHandler>(enabled: false); handler.Name = handler.BaseType.GetUserFriendlyNameForInstance() + " " + _event.Name; handler.Event = ReferenceUtility.MakeReference <ReferenceValueType_Event>(null, ReferenceUtility.CalculateThisReference(handler, subscribeTo, _event.Signature)); handler.HandlerMethod = ReferenceUtility.MakeReference <ReferenceValueType_Method>(null, ReferenceUtility.CalculateThisReference(handler, script2, methodSignature)); handler.Enabled = true; } //undo { var property = (Metadata.Property)script2.MetadataGetMemberBySignature("property:Code"); var undoItem = new UndoActionPropertiesChange.Item(script2, property, oldCode, new object[0]); var action1 = new UndoActionPropertiesChange(new UndoActionPropertiesChange.Item[] { undoItem }); var action2 = new UndoActionComponentCreateDelete(document, new Component[] { handler }, true); document.UndoSystem.CommitAction(new UndoMultiAction(new UndoSystem.Action[] { action1, action2 })); document.Modified = true; } }); item.Tag = script; childItems.Add(item); } } groupItem.Items.Add(new KryptonContextMenuItems(childItems.ToArray())); items.Add(groupItem); } //Add Handler to Flow Graph { var groupItem = new KryptonContextMenuItem(Translate("Add Handler to Flow Graph"), null); var childItems = new List <KryptonContextMenuItemBase>(); //create new flow graph { var itemsData = new List <(string, Component)>(); itemsData.Add((Translate("Add Flow Graph in This Component"), subscribeTo)); if (subscribeTo != subscribeTo.ParentRoot) { itemsData.Add((Translate("Add Flow Graph in the Root Component"), subscribeTo.ParentRoot)); } foreach (var itemData in itemsData) { var childItem = new KryptonContextMenuItem(itemData.Item1, null, delegate(object s, EventArgs e2) { var parent = (Component)((KryptonContextMenuItem)s).Tag; //create flow graph var graph = parent.CreateComponent <Component_FlowGraph>(enabled: false); graph.Name = graph.BaseType.GetUserFriendlyNameForInstance(); graph.Enabled = true; //create node with handler var node = AddFlowGraphNode(graph, subscribeTo); node.Position = new Vector2I(-20, -10); //undo var document = Owner.DocumentWindow.Document; var action = new UndoActionComponentCreateDelete(document, new Component[] { graph }, true); document.UndoSystem.CommitAction(action); document.Modified = true; //activate flow graph window EditorAPI.OpenDocumentWindowForObject(document, graph); }); childItem.Tag = itemData.Item2; childItems.Add(childItem); } } //add handler to one of already created flow graph foreach (var graph in subscribeTo.ParentRoot.GetComponents <Component_FlowGraph>(false, true)) { if (graph.TypeSettingsIsPublic()) { var text = string.Format(Translate("Add Handler to \'{0}\'"), graph.GetPathFromRoot(true)); var item = new KryptonContextMenuItem(text, null, delegate(object s, EventArgs e2) { var graph2 = (Component_FlowGraph)((KryptonContextMenuItem)s).Tag; //create node with handler var node = AddFlowGraphNode(graph2, subscribeTo); //undo var document = Owner.DocumentWindow.Document; var action = new UndoActionComponentCreateDelete(document, new Component[] { node }, true); document.UndoSystem.CommitAction(action); document.Modified = true; //activate flow graph window EditorAPI.OpenDocumentWindowForObject(document, graph2); }); item.Tag = graph; childItems.Add(item); } } groupItem.Items.Add(new KryptonContextMenuItems(childItems.ToArray())); items.Add(groupItem); } ////add handler (component only) //{ // var item = new KryptonContextMenuItem( Translate( "Add handler (component only)" ), null, delegate ( object s, EventArgs e2 ) // { // var newObjects = new List<Component>(); // var handler = subscribeTo.CreateComponent<Component_EventHandler>( enable: false ); // handler.Name = handler.BaseType.GetUserFriendlyNameForInstance() + " " + _event.Name; // handler.Event = ReferenceUtility.MakeReference<ReferenceValueType_Event>( null, // ReferenceUtility.CalculateThisReference( handler, subscribeTo, _event.Signature ) ); // handler.Enabled = true; // newObjects.Add( handler ); // var document = Owner.DocumentWindow.Document; // var action = new UndoActionComponentCreateDelete( document, newObjects, true ); // document.UndoSystem.CommitAction( action ); // document.Modified = true; // Owner.DocumentWindow.SelectObjects( newObjects.ToArray() ); // } ); // item.Enabled = GetOneControlledObject<Component>() != null; // items.Add( item ); //} EditorContextMenuWinForms.Show(items, Owner); }
string TranslateContextMenu(string text) { return(EditorContextMenuWinForms.Translate(text)); }
void ShowContextMenu() { var items = new List <KryptonContextMenuItemBase>(); Component oneSelectedComponent = ObjectOfWindow as Component; //Editor { var item = new KryptonContextMenuItem(TranslateContextMenu("Editor"), EditorResourcesCache.Edit, delegate(object s, EventArgs e2) { EditorAPI.OpenDocumentWindowForObject(Document, oneSelectedComponent); }); item.Enabled = oneSelectedComponent != null && EditorAPI.IsDocumentObjectSupport(oneSelectedComponent); items.Add(item); } //Settings { var item = new KryptonContextMenuItem(TranslateContextMenu("Settings"), EditorResourcesCache.Settings, delegate(object s, EventArgs e2) { bool canUseAlreadyOpened = !ModifierKeys.HasFlag(Keys.Shift); EditorAPI.ShowObjectSettingsWindow(Document, oneSelectedComponent, canUseAlreadyOpened); }); item.Enabled = oneSelectedComponent != null; items.Add(item); } items.Add(new KryptonContextMenuSeparator()); //New object { EditorContextMenuWinForms.AddNewObjectItem(items, CanNewObject(out _), delegate(Metadata.TypeInfo type) { TryNewObject(type); }); } //separator items.Add(new KryptonContextMenuSeparator()); //Cut { var item = new KryptonContextMenuItem(TranslateContextMenu("Cut"), EditorResourcesCache.Cut, delegate(object s, EventArgs e2) { //Cut(); }); item.ShortcutKeyDisplayString = EditorActions.GetFirstShortcutKeyString("Cut"); item.Enabled = false; // CanCut(); items.Add(item); } //Copy { var item = new KryptonContextMenuItem(TranslateContextMenu("Copy"), EditorResourcesCache.Copy, delegate(object s, EventArgs e2) { Copy(); }); item.ShortcutKeyDisplayString = EditorActions.GetFirstShortcutKeyString("Copy"); item.Enabled = CanCopy(); items.Add(item); } //Paste { var item = new KryptonContextMenuItem(TranslateContextMenu("Paste"), EditorResourcesCache.Paste, delegate(object s, EventArgs e2) { Paste(); }); item.ShortcutKeyDisplayString = EditorActions.GetFirstShortcutKeyString("Paste"); item.Enabled = CanPaste(out _); items.Add(item); } //Clone { var item = new KryptonContextMenuItem(TranslateContextMenu("Duplicate"), EditorResourcesCache.Clone, delegate(object s, EventArgs e2) { //TryCloneObjects(); }); item.ShortcutKeyDisplayString = EditorActions.GetFirstShortcutKeyString("Duplicate"); item.Enabled = false; // CanCloneObjects( out List<Component> dummy ); items.Add(item); } //separator items.Add(new KryptonContextMenuSeparator()); //Delete { var item = new KryptonContextMenuItem(TranslateContextMenu("Delete"), EditorResourcesCache.Delete, delegate(object s, EventArgs e2) { //TryDeleteObjects(); }); item.Enabled = false; // CanDeleteObjects( out List<Component> dummy ); items.Add(item); } //Rename { var item = new KryptonContextMenuItem(TranslateContextMenu("Rename"), null, delegate(object s, EventArgs e2) { EditorUtility.ShowRenameComponentDialog(oneSelectedComponent); }); item.ShortcutKeyDisplayString = EditorActions.GetFirstShortcutKeyString("Rename"); //!!!!! item.Enabled = oneSelectedComponent != null; items.Add(item); } EditorContextMenuWinForms.AddActionsToMenu(EditorContextMenuWinForms.MenuTypeEnum.Document, items); //, this ); EditorContextMenuWinForms.Show(items, this); }
private void Editor_ContextMenuOpening(object sender, System.Windows.Controls.ContextMenuEventArgs e) { e.Handled = true; var items = new List <KryptonContextMenuItemBase>(); //Find { var item = new KryptonContextMenuItem(TranslateContextMenu("Find and Replace"), null, delegate(object s, EventArgs e2) { SearchReplacePanel?.ShowFindOrReplace(false); }); item.ShortcutKeyDisplayString = EditorActions.ConvertShortcutKeysToString(new Keys[] { Keys.Control | Keys.F }); //item.ShortcutKeyDisplayString = EditorActions.GetFirstShortcutKeyString( "Find" ); items.Add(item); } //separator items.Add(new KryptonContextMenuSeparator()); //Cut { var item = new KryptonContextMenuItem(TranslateContextMenu("Cut"), EditorResourcesCache.Cut, delegate(object s, EventArgs e2) { editor.Cut(); }); item.ShortcutKeyDisplayString = EditorActions.GetFirstShortcutKeyString("Cut"); item.Enabled = !Editor.IsReadOnly; // editor.CanCut(); items.Add(item); } //Copy { var item = new KryptonContextMenuItem(TranslateContextMenu("Copy"), EditorResourcesCache.Copy, delegate(object s, EventArgs e2) { editor.Copy(); }); item.ShortcutKeyDisplayString = EditorActions.GetFirstShortcutKeyString("Copy"); item.Enabled = true; // CanCopy(); items.Add(item); } //Paste { var item = new KryptonContextMenuItem(TranslateContextMenu("Paste"), EditorResourcesCache.Paste, delegate(object s, EventArgs e2) { editor.Paste(); }); item.ShortcutKeyDisplayString = EditorActions.GetFirstShortcutKeyString("Paste"); item.Enabled = !Editor.IsReadOnly; // CanPaste( out _ ); items.Add(item); } EditorContextMenuWinForms.AddActionsToMenu(EditorContextMenuWinForms.MenuTypeEnum.General, items); EditorContextMenuWinForms.Show(items, this); }