Author Topic: mode_tile - How it works  (Read 8151 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Swamp Rat
  • Posts: 812
mode_tile - How it works
« on: January 22, 2017, 04:57:22 PM »
Hi guys,
The last days I was thinking about putting some practice over the mode_tile function.
The idea of this example won't go out of my head so just now I had some spare time to assemble it (my goal was to "visualise" how it works) :

Code - Auto/Visual Lisp: [Select]
  1. ; mode_tile Practice/Visualisation
  2.  
  3. ; (mode_tile key mode)
  4. ; mode ; Type: Integer
  5. ; A numeric value that can be one of the following:
  6. ; 0 -- Enable tile
  7. ; 1 -- Disable tile
  8. ; 2 -- Set focus to tile
  9. ; 3 -- Select edit box contents
  10. ; 4 -- Flip image highlighting on or off
  11.  
  12. (defun C:test ( / *error* dcl des dch dcf rtn )
  13.  
  14.   (defun *error* ( msg )
  15.     (and (< 0 dch) (unload_dialog dch))
  16.     (and (eq 'FILE (type des)) (close des))
  17.     (and (eq 'STR (type dcl)) (findfile dcl) (vl-file-delete dcl))
  18.     (and msg (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")) (princ (strcat "\nError: " msg)) )
  19.     (princ)
  20.   ); defun *error*
  21.  
  22.   (cond
  23.     (
  24.       (not
  25.         (and
  26.           (setq dcl (vl-filename-mktemp nil nil ".dcl"))
  27.           (setq des (open dcl "w"))
  28.           (write-line ; princ will work too
  29.             (strcat
  30.               "ModeTile : dialog"
  31.               "{"
  32.               "  label = \"Mode Tile Practice\";"
  33.               "  : column"
  34.               "  {"
  35.               "    : edit_box"
  36.               "    { label = \"BoxLabel\"; key = \"Box\"; value = \"BoxValue\"; edit_width = 16; width = 22; fixed_width = true; alignment = centered; }"
  37.               "    spacer;"
  38.               "    : button { label = \"Enable tile\"; key = \"Enable\"; edit_width = 36; width = 36; fixed_width = true; }"
  39.               "    : button { label = \"Disable tile\"; key = \"Disable\"; edit_width = 36; width = 36; fixed_width = true; }"
  40.               "    : button { label = \"Set focus to tile\"; key = \"Focus\"; edit_width = 36; width = 36; fixed_width = true; }"
  41.               "    : button { label = \"Select edit box contents\"; key = \"Select\"; edit_width = 36; width = 36; fixed_width = true; }"
  42.               "    : button { label = \"Flip image highlighting\"; key = \"Flip\"; edit_width = 36; width = 36; fixed_width = true; } "
  43.               "    spacer;"
  44.               "    ok_cancel;"
  45.               "  }"
  46.               "}"              
  47.             ); strcat
  48.             des
  49.           ); write-line / princ
  50.           (not (setq des (close des)))
  51.           (< 0 (setq dch (load_dialog dcl)))
  52.         ); and
  53.       ); not
  54.       (princ "\nUnable to write or load the DCL file.")
  55.     )
  56.     (
  57.       (not
  58.         (progn
  59.           (new_dialog "ModeTile" dch)
  60.           (setq rtn (get_tile "Box"))
  61.           (or ; actions for mode_tile
  62.             (foreach x '(("Enable" 0) ("Disable" 1) ("Focus" 2) ("Select" 3) ("Flip" 4))
  63.               ; (action_tile (car x) (vl-prin1-to-string '(mode_tile "Box" (cadr x)))) ; This won't work, why?
  64.               (action_tile (car x) (strcat "(mode_tile \"Box\"" (itoa (cadr x)) ")"))
  65.             ); foreach
  66.             (progn ; This progn block is redundant, just leaving it to see how (action_tile) is normally used, instead of the above (foreach)
  67.               (action_tile "Enable" "(mode_tile \"Box\" 0)") ; check
  68.               (action_tile "Disable" "(mode_tile \"Box\" 1)") ; check
  69.               (action_tile "Focus" "(mode_tile \"Box\" 2)") ; check
  70.               (action_tile "Select" "(mode_tile \"Box\" 3)") ; what this does ?
  71.               (action_tile "Flip" "(mode_tile \"Box\" 4)") ; what this does ?
  72.             ); progn
  73.           ); or
  74.           (foreach x '(("Box" (setq rtn $value)) ("accept" (done_dialog 1))) ; other actions
  75.             (action_tile (car x) (vl-prin1-to-string (cadr x)))
  76.           ); foreach
  77.           (if (= 1 (setq dcf (start_dialog)))
  78.             (alert (strcat "\nBox's value: " rtn))
  79.           )
  80.         ); progn
  81.       ); not
  82.       (princ "\nUnable to display the dialog")
  83.     )
  84.   ); cond
  85.   (*error* nil)
  86.   (princ)
  87. ); defun C:test

Obviously I used a DCL template thats from Lee Mac's DCL examples, because IMO it can't be written more perfectly otherwise.
( Even the variable names are matching - just wanted to make sure we talk on the same language  :lol:  )

So.. back to the questions:

1. I had trouble understanding what this means:
; 3 -- Select edit box contents
; 4 -- Flip image highlighting on or off

Or atleast this part of the code "didn't worked/visualised well":
Code - Auto/Visual Lisp: [Select]
  1. (action_tile "Select" "(mode_tile \"Box\" 3)") ; what this does ?
  2. (action_tile "Flip" "(mode_tile \"Box\" 4)") ; what this does ?


2. This action_tile part didn't worked correctly, why?
Code - Auto/Visual Lisp: [Select]
The code errors out, then I go to the last break source and in a new source window displays:
Code - Auto/Visual Lisp: [Select]
  1. ;;; Copied to window at 11:49 PM 1/22/17
  2.  
  3. (lambda ($KEY $VALUE $DATA $REASON $X $Y)
  4.   (MODE_TILE "Box" (CADR X))
  5.   )
  6.  
  7. ;;; End of text
  8.  
I assumed that the trouble might be because of the strcasing:
Code - Auto/Visual Lisp: [Select]
  1. "(MODE_TILE \"Box\" 1)"
  2. _$
But this works?! :
Code - Auto/Visual Lisp: [Select]
  1. (action_tile (car x) (strcat "(MODE_TILE \"Box\"" (itoa (cadr x)) ")"))
:thinking:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: mode_tile - How it works
« Reply #1 on: January 22, 2017, 05:39:34 PM »
Quick answer to your second question:

2. This action_tile part didn't worked correctly, why?
Code - Auto/Visual Lisp: [Select]

Because the mode_tile expression is quoted and hence (cadr x) is not evaluated - the resulting action_tile expression would look something like:
Code - Auto/Visual Lisp: [Select]
  1. (action_tile "key1" "(mode_tile \"Box\" (cadr x))")

In which (cadr x) no longer has meaning.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: mode_tile - How it works
« Reply #2 on: January 22, 2017, 06:35:19 PM »
Because the mode_tile expression is quoted and hence (cadr x) is not evaluated - the resulting action_tile expression would look something like:
Code - Auto/Visual Lisp: [Select]
  1. (action_tile "key1" "(mode_tile \"Box\" (cadr x))")
In which (cadr x) no longer has meaning.

Oooh I see now, thanks for the heads up Lee!
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg