Events
If you want to write you own script plugin, there is no way to do it if you are not familiar with HippoEDIT events.
Events are the callbacks (same as handlers) you can assign to some plugs to be called when specific action has been executed.
Basics
There are no magic in events design (as you need to define “special” named functions etc).
To subscribe on event, you just assign your handler function to appropriate named plug from engine (plugs are members of Application object). Something like this:
- doc_save_handler.js
function myDocumentSaveHandler() { alert("Some document was Saved!"); } Application.onNewDocument = myDocumentSaveHandler;
The code above will show message box, as far as any document saved.
Handlers always have this object, that represents source of event and can have parameters (can be checked in API documentation), which forward additional information to handler.
To receive them you need to define input parameters of your functions in the sequence as it defined by API. If you do not need some trailing parameters, you can skip their definition in handler signature (even if you catch no parameters it will work). And of course, you can always access Application object - it is global.
- on_file_rename.js
Application.onDocumentNameChange = function(old_name, new_name, rename) { Output().writeln("onDocumentNameChange : old_name = " + old_name + ", new_name = " + new_name + ", rename = " + rename); }
The above code will be called on file rename and shall output old and new file name together with rename indicator passed as event parameters.
Following code executed when a document is modified or saved and uses this object, that represent a document here, to get Title text. The type of this pointer for every handler can be checked in the documentation, but normally it is clear from handler purpose.
- on_file_modified.js
Application.onModifiedChanged = function (modified) { Output().writeln("onModifiedChanged : document = " + this.Title + ", modified = " + modified); }
In-line and Unnamed Handlers
If you have some simple handler function and do not need parameters except of this, you can directly assign handler code to a event plug - HippoEDIT will convert it to function handler internally:
Application.onDocumentListUpdate = "Output().writeln(\"onDocumentListUpdate\")";
or, if scripting engine support this (JavaScript supports) do in-place assignment of unnamed function to event plug:
onModifiedChanged = function (modified) { Output().writeln("onModifiedChanged : document = " + this.Title + ", modified = " + modified); }
as Application is global object, you can avoid it and directly write event plug name (on example above).
Unregistering handlers
To unregister event handler, reset plug by assigning to it null object.
onModifiedChanged = function (modified) { Output().writeln("onModifiedChanged : document = " + this.Title + ", modified = " + modified); onModifiedChanged = null; }
the code above will execute handler only once, because event plug is reseted after first handler execution.
Event Hubs
Sometimes you want to connect more than one event handler to same event plug. Of course, you can do it also in code by creating one hub handler and dispatching the event to every single handler, but there is the easier way. You can use dedicated functions from HippoEDIT scripting framework:
- boolean attachEvent(string EventName, object eventHandler) - helper to attach more than one handler to event plug
- boolean detachEvent(string EventName, object eventHandler) - helper to detach handler from event plug
//////////////////////////////////////////////////////////////////////// // attach/detach event tests // this is extended technique if you want to have more than one handler // attached to same event from same script and normally is not needed, // while you can also copy previous handler and call it inside of your // handler as an alternative function onDocumentListUpdate2() { Output().writeln("onDocumentListUpdate2"); } function onDocumentListUpdate3() { Output().writeln("onDocumentListUpdate3"); if (Application.detachEvent("onDocumentListUpdate", onDocumentListUpdate3)) Output().writeln("onDocumentListUpdate3 detached"); } if (Application.attachEvent("onDocumentListUpdate", onDocumentListUpdate2)) Output().writeln("onDocumentListUpdate2 attached"); if (Application.attachEvent("onDocumentListUpdate", onDocumentListUpdate3)) Output().writeln("onDocumentListUpdate3 attached");
Commands
The easiest way to integrate your functionality in HippoEDIT scripting infrastructure is to model you functionality as command objects. If you have command object it is automatically visible in command list in keyboard settings, you can add to menu, ToolBar or StatusBar.
UI Events
There is no way to modify UI directly from any part of code. UI can be changed only during dedicated events, that provide you helper objects and ensure that UI modifications are done in right sequence, objects you want to modify already available and actions you did in code can be undone.
You can use following UI initialization events for modifications:
- void onInitMainMenu(boolean bUpdate) - Main Menu initialization
- void onMainSubmenuUpdate() - update of Main Menu sub-menus
- void onInitToolbars() - ToolBars initialization
- void onInitContextMenu(eCMS eStyle, object view, integer line, integer pos) - Context Menu initialization
- void onInitStatusBar() - Status Bar initialization
- void onMenuCreate(string command) - creation of drop-down in menu or in status bar
- void onPopupCreate(string command) - creation for drop-down window in menu or in status bar
- void onUICreate() - called when framework is ready for any UI adjustment
List Pane events
- void onListContextMenu(object pane, object item) - context menu initialization in List Pane
- void onListDoubleClick(object item) - double click on item of List Pane
- void onListCheck(object item) - item in List Pane was checked
Service Events
In addition to UI events, there are of course, normal, functional events informing you about internal processes as: changing of cursor position, changing of active document, closing of the workspace, change of global settings etc.
Here is list of events you can subscribe:
Document Events
- void onDocumentSwitch() - called when active document swtiched
- void onDocumentListUpdate() - list of open document has been changed
- void onDocumentStateUpdate() - document state has been changed
- void onDocumentNameChange(string oldName, string newName, boolean rename) - called of document name has been changed
- void onModifiedChange(boolean modified) - modified state of the document has been changed
- void onDocumentClose() - document is to be closed
- void onDocumentOpen() - document has been opened
- void onDocumentSave() - document is to be saved
- void onNewDocument() - new document created
- void onSyntaxChange(object oldSyntax) - document syntax has been changed
- void onEditOperation(HE_ACTION action) - some edit operation was done on document
- void onTextInsert(object position, string text, HE_ACTION action) - text was inserted into document
View Events
- void onCursorPosChange(object view) - called if cursor position in active view changed
- void onFocusSet() - focus set to view
- void onFocusLost() - focus removed from active view
- void onScroll(unsigned integer scrollX, unsigned integer scrollY) - current view was scrolled
- void onSelectionChange(object selection, boolean blockMode) - selection on current view was changed
Workspace Events
- void onWorkspaceOpen(boolean saveState) - called after workspace has been opened
- void onWorkspaceClose(boolean saveState) - called before workspace closed
Project Events
- void onProjectLoad(object pProject) - called after project loaded
- void onProjectSave(object pProject) - called before project saved
- void onProjectChanged(object pProject) - called when project changed
Framework Events
- boolean onCanCloseWorkspace() - called by framework to determine, if workspace can be closed
- boolean onCanCloseApplication() - called by framework to determine, if application can be closed
- void onSettingsChange() - called by framework if global settings has been changed
- void onJobFinished(string jobID, integer lineFrom, integer lineTo) - called when one of background document jobs has been finished
- void onIdle() - called on idle time of application