;;; TextEditFix.lsp
;;; Temporary workaround for a BricsCAD V27 bug: the in-place text editor opens oversized (about
;;; the annotation scale factor times too big) when annotative text is edited and the annotation
;;; scale is not 1:1. TEXTED does not change it (same at 0, 1 and 2).
;;;
;;; While a text-edit command runs, the current annotation scale is switched to 1:1 and put back
;;; when the command ends, is cancelled or fails. ANNOAUTOSCALE is turned off for the swap so no
;;; 1:1 scale is added to annotative objects. Standalone: no other files needed.
;;;
;;; Commands:  TEFIX      toggle the workaround on/off (it turns on when this file loads)
;;;            TEFIXLOG   toggle printing of every command name, to find the one a double-click starts
;;; Add command names to *tef-cmds* if a double-click starts a command that is not listed.
;;; Delete this file's load line once Bricsys fixes the bug.

(setq *tef-cmds* '("TEXTEDIT" "-TEXTEDIT" "DDEDIT" "MTEDIT" "EDITTEXT" "MTEXTEDIT"))

(defun tef:restore (/ s)
  (if *tef-saved*
    (progn
      (setq s *tef-saved*
	    *tef-saved* nil
      ) ;_end setq
      (vl-catch-all-apply 'setvar (list "CANNOSCALE" (car s)))
      (if (cadr s)
	(vl-catch-all-apply 'setvar (list "ANNOAUTOSCALE" (cadr s)))
      ) ;_end if
    ) ;_end progn
  ) ;_end if
) ;_end defun

(defun tef:onStart (rea params / cmd cur auto)
  (setq cmd (strcase (car params)))
  (if *tef-log*
    (princ (strcat "\n[TEF] command: " cmd))
  ) ;_end if
  (if (and (member cmd *tef-cmds*) (not *tef-saved*))
    (progn
      (setq cur  (getvar "CANNOSCALE")
	    auto (vl-catch-all-apply 'getvar (list "ANNOAUTOSCALE"))
	    auto (if (numberp auto) auto nil)
      ) ;_end setq
      (if (and cur (/= cur "1:1"))
	(progn
	  (setq *tef-saved* (list cur auto))
	  (if auto
	    (vl-catch-all-apply 'setvar (list "ANNOAUTOSCALE" 0))
	  ) ;_end if
	  (if (vl-catch-all-error-p (vl-catch-all-apply 'setvar (list "CANNOSCALE" "1:1")))
	    (tef:restore)
	  ) ;_end if
	) ;_end progn
      ) ;_end if
    ) ;_end progn
  ) ;_end if
  (princ)
) ;_end defun

(defun tef:onEnd (rea params)
  (tef:restore)
  (princ)
) ;_end defun

(defun tef:install ()
  (tef:remove)
  (setq *tef-reactor*
	 (vl-catch-all-apply
	   'vlr-command-reactor
	   (list nil
		 '((:vlr-commandWillStart . tef:onStart)
		   (:vlr-commandEnded . tef:onEnd)
		   (:vlr-commandCancelled . tef:onEnd)
		   (:vlr-commandFailed . tef:onEnd)
		  )
	   ) ;_end list
	 ) ;_end vl-catch-all-apply
  ) ;_end setq
  (if (vl-catch-all-error-p *tef-reactor*)
    (progn
      (princ (strcat "\nTextEditFix: reactor not installed: " (vl-catch-all-error-message *tef-reactor*)))
      (setq *tef-reactor* nil)
    ) ;_end progn
  ) ;_end if
) ;_end defun

(defun tef:remove ()
  (tef:restore)
  (if *tef-reactor*
    (progn
      (vl-catch-all-apply 'vlr-remove (list *tef-reactor*))
      (setq *tef-reactor* nil)
    ) ;_end progn
  ) ;_end if
) ;_end defun

(defun c:TEFIX ()
  (if *tef-reactor*
    (progn (tef:remove) (princ "\nTextEditFix is OFF."))
    (progn (tef:install) (princ (if *tef-reactor* "\nTextEditFix is ON." "\nTextEditFix could not start.")))
  ) ;_end if
  (princ)
) ;_end defun

(defun c:TEFIXLOG ()
  (setq *tef-log* (not *tef-log*))
  (princ (if *tef-log* "\nTextEditFix logging is ON." "\nTextEditFix logging is OFF."))
  (princ)
) ;_end defun

(tef:install)
(princ "\nTextEditFix loaded (BricsCAD V27 annotative text editor workaround). TEFIX toggles it.")
(princ)
