Modal vs. Modeless

Modal dialogs disable the AutoCAD window until they are closed. By their nature, modal dialogs are easier to manage because the AutoCAD state remains unchanged for the life of the dialog. Modal dialogs return a dialog status code when they close, and typically the main application code performs additional actions based on the status code returned from the dialog.

Modeless dialogs present additional challenges. Since modeless dialogs can remain open for the lifetime of the AutoCAD application, they must deal with changing AutoCAD state such as documents opening and closing. In addition, since every AutoCAD drawing has its own AutoLISP namespace, activating a new document means that event handlers defined in the deactivated document will no longer be accessible in the newly activated document.

These realities mean that the only way to manage a modeless dialog is by ensuring that the event handlers are defined in every new drawing file. In practice, this usually means that the application lisp file (or files) must be loaded via the acaddoc.lsp file or a similar method that loads the files as part of new document initialization.

Furthermore, since the dialog may already be active when startup code executes (after opening a second drawing file, for example), it must act accordingly. For example, if the application must read some initialization settings from the registry, it likely needs to do that only when the modeless dialog is initially activated. If such initialization is performed in the dialog's OnInitialize event, it will only get executed once when the dialog is first activated. If the application needs to determine at runtime whether the dialog is already active it can use an expression like the following:

(and MyProject-MyForm (dcl-Form-IsActive MyProject-MyForm))

In addition to programming startup code to act appropriately when more than one drawing is opened, modeless dialogs must also behave correctly when all open drawings are closed (resulting in a state referred to as a "no doc state") or when the user switches between multiple active drawings. If the modeless dialog contains controls whose settings or properties depend on the drawing contents, then it must define event handlers for the OnDocActivated and OnEnteringNoDocState events to update the controls as the document state changes.

Modeless dialog event handlers are normally invoked in the AutoCAD application context instead of the document context. AutoLISP code running in the application context cannot call document-dependent functions like the (command) function. To deal with this problem, the control's 'Event Invoke' property may be used to trigger the event handler asynchronously in the document context, thus allowing calls to (command) to succeed. Alternatively, the event handler may be executed synchronously, but instead of calling document-dependent functions within the handler function, it can simply trigger a new command to execute in the document context via the (dcl-SendString) function.