(if (= (type vl-load-com) 'SUBR)
  (vl-load-com)
)

(command "opendcl")

(dcl_project_load "texthook" t)

(defun c:TextHook ( / )

  (defun get_width (gw_text_string / gw_text_entity gw_text_object gw_text_entity
                    gw_10_point gw_11_point gw_text_width
                   )
    (setq gw_text_entity (list                                       ; create a temporary text entity
                              '(0 . "TEXT")
                              '(100 . "AcDbEntity")
                              '(67 . 0)
                              '(410 . "Model")
                              (cons 8 "0" )
                              '(100 . "AcDbText")
                              (cons 10 (list 0 0 0))
                              (cons 11 (list 0 0 0))
                              (cons 40 text_height)
                              (cons 1 gw_text_string)
                              (cons 7 text_style)
                              '(71 . 0)
                              '(72 . 2)
                              '(73 . 0)
                            )
    )
    (entmake gw_text_entity)
    (setq gw_text_object (entlast)
          gw_text_entity (entget gw_text_object)                     ; get the entity
          gw_10_point (cdr (assoc 10 gw_text_entity))
          gw_11_point (cdr (assoc 11 gw_text_entity))
          gw_text_width (distance gw_10_point gw_11_point)           ; character width
    )
    (entdel gw_text_object)                                          ; delete the entity
    gw_text_width                                                    ; return the width
  )

  (defun draw_cursor ( / )  
    (setq dc_point_10 (cdr (assoc 10 text_entity)))
    (setq dc_text_height (cdr (assoc 40 text_entity)))
    (setq dc_text_rotation (cdr (assoc 50 text_entity)))
    (setq dc_text_string (cdr (assoc 1 text_entity)))
    (if (= new_cursor 0)
      (setq x_offset (* text_height -0.166))
      (progn
        (setq x_offset (get_width (substr dc_text_string 1 new_cursor)))
        (if (= new_cursor (strlen dc_text_string))
          (setq x_offset (+ x_offset (* text_height 0.166)))
        )
      )  
    )    
    (setq new_x (+ (car dc_point_10) x_offset))
    (setq new_y1 (- (cadr dc_point_10) (/ dc_text_height 2.0)))
    (setq new_y2 (+ (cadr dc_point_10) (* (/ dc_text_height 2.0) 2.5)))
    (setq point1 (list new_x new_y1 0.0))
    (setq point2 (list new_x new_y2 0.0))
    (if (/= text_rotation 0.0)
      (progn
        (setq point1 (polar dc_point_10 (+ (angle dc_point_10 point1) dc_text_rotation) (distance dc_point_10 point1)))
        (setq point2 (polar dc_point_10 (+ (angle dc_point_10 point2) dc_text_rotation) (distance dc_point_10 point2)))
      )
    )
    (setq cursor_entity (subst (cons 10 point1) (assoc 10 cursor_entity) cursor_entity))
    (setq cursor_entity (subst (cons 11 point2) (assoc 11 cursor_entity) cursor_entity))
    (entmod cursor_entity)
    (princ)
  )
    
  (defun c:texthook_TextHook_TextBox_OnEditChanged (NewValue /)
    (setq text_entity (subst (cons 1 NewValue) (assoc 1 text_entity) text_entity))
    (entmod text_entity)
    (setq a (dcl_TextBox_GetSel texthook_TextHook_TextBox))
    (setq new_cursor (car a))
    (if (/= old_cursor new_cursor)
      (progn
        (draw_cursor)
      )
    )
    (princ)
  )

(defun c:TextHook_TextHook_TextBox_OnUpdate (NewValue /)
  (print "ou")
  (print newvalue)
)

  (defun c:TextHook_TextHook_TextBox_OnKeyDown (Character RepeatCount Flags /)
    (if (and (= flags 331)
             (> new_cursor 0)
        )
      (progn  
        (setq new_cursor (1- new_cursor))
        (draw_cursor)        
      )  
    )
    (if (and (= flags 333)
             (< newcursor (strlen (cdr (assoc 1 text_entity))))
        )     
      (progn
        (setq new_cursor (1+ new_cursor))
        (draw_cursor)        
      )  
    )
    (princ)
  )  
    
  (defun c:TextHook_TextHook_TextBox_OnReturnPressed (/)
    (setvar "nomutt" 0)
    (setvar "cmdecho" old_cmdecho)
    (entdel cursor_object)
    (if (dcl_form_isactive texthook_TextHook)
      (dcl_form_close texthook_TextHook)
    )
    (princ)
  )

  (setq insertion_point (list 10 10 0))
  (setq new_string " ")

  (setq old_cmdecho (getvar "cmdecho"))
  (setq old_string new_string)
  (setq text_style (getvar "textstyle"))
  (setq text_height (getvar "textsize"))
  (setq dxf72 0)
  (setq dxf73 0)
  (setq text_rotation 0.0)
  (setq text_layer (getvar "clayer"))
  (setq edit_color 7)
  (setq new_cursor 0)
  (setq old_cursor new_cursor)
  (setq text_entity (list                                      ; set up a text entity
                      '(0 . "TEXT")
                      (cons  8 text_layer)
                      (cons  7 text_style)
                      (cons 40 text_height)
                      (cons 62 edit_color)
                      (cons 50 text_rotation)
                      (cons 10 insertion_point)
                      (cons 11 insertion_point)
                      (cons 72 dxf72)
                      (cons 73 dxf73)
                      (if (not new_string)
                        (cons 1 "")
                        (cons 1 new_string)
                      )
                    )
  )
  (setq text_object (entmakex text_entity))                    ; create a text object then edit it
  (setq text_entity (entget text_object))
  (setq new_x (car insertion_point))
  (setq new_y1 (- (cadr insertion_point) (/ text_height 2.0)))
  (setq new_y2 (+ (cadr insertion_point) (* (/ text_height 2.0) 2.5)))
  (setq point1 (list new_x new_y1 0.0))
  (setq point2 (list new_x new_y2 0.0))
  (if (/= text_rotation 0.0)
    (progn
      (setq point1 (polar insertion_point (+ (angle insertion_point point1) dc_text_rotation) (distance insertion_point point1)))
      (setq point2 (polar insertion_point (+ (angle insertion_point point2) dc_text_rotation) (distance insertion_point point2)))
    )
  )
  (setq cursor_entity (list                                                   ; create the line
                        '(0 . "LINE")
                        '(100 . "AcDbEntity")
                        '(100 . "AcDbLine")
                        '(67 . 0)
                        '(62 . 7)                                             ; color = white
                        (cons 8 "power")
                        (cons 10 point1)
                        (cons 11 point2)
                      )
  )
  (setq cursor_object (entmakex cursor_entity))                    ; create a text object then edit it
  (setq cursor_entity (entget cursor_object))
  (setvar "nomutt" 1)
  (setvar "cmdecho" 0)
  (dcl_form_show texthook_TextHook 600 400)
  (dcl_Control_SetText TextHook_TextHook_TextBox "")  
  (dcl_TextBox_SetSel texthook_TextHook_TextBox 0 0)
  (dcl_Control_SetFocus texthook_TextHook_TextBox)

  (princ)
)
