home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / p / p024 / 12.img / ADS3.LIB / DLGTEST.C next >
Encoding:
C/C++ Source or Header  |  1993-02-17  |  14.3 KB  |  549 lines

  1. /* Next available MSG number is  21 */
  2. /*
  3.                ADS Programmable Dialog Box Test Program
  4.  
  5.       DLGTEST.C
  6.       ¬⌐┼v (C) 1990-1992  Autodesk ñ╜Ñq
  7.  
  8.       Ñ╗│n┼ΘºK╢O¿╤▒z╢iªµÑ⌠ª≤Ñ╬│~╗▌¿D¬║½■¿⌐íB¡╫º∩ñ╬╡oªµ, ª²¼O░╚╜╨┐φ┤`ñU¡z
  9.       ¡∞½h :
  10.  
  11.       1)  ñWªC¬║¬⌐┼v│qºi░╚╗▌ÑX▓{ªb¿Cñ@Ñ≈½■¿⌐∙╪íC
  12.       2)  ¼█├÷¬║╗í⌐·ñσÑ≤ñ]Ñ▓╢╖⌐·╕ⁿ¬⌐┼v│qºiñ╬Ñ╗╢╡│\Ñi│qºiíC
  13.  
  14.       Ñ╗│n┼Θ╢╚┤ú¿╤º@¼░└│Ñ╬ñW¬║░╤ª╥, ª╙Ñ╝┴n⌐·⌐╬┴⌠ºtÑ⌠ª≤½O├╥; ╣∩⌐≤Ñ⌠ª≤»S«φ
  15.       Ñ╬│~ñº╛A║┘⌐╩, ÑHñ╬░╙╖~╛P░Γ⌐╥┴⌠ºtÑX¿π¬║½O├╥, ªbª╣ñ@╖ºñ⌐ÑHº_╗{íC
  16.  
  17.  
  18.  
  19.  
  20.     Programmable Dialog Box Test Program
  21.  
  22.          This program is the ADS counterpart to the LISP test
  23.     program, dlgtest.lsp.  It provides a simple dimensioning
  24.     dialog invoked with the command "dimen" and a simple color
  25.     dialog invoked with the command "setcolor".
  26.  
  27.          The purposes of providing this program:
  28.     1) Demonstrate Programmable Dialog Box use with minimum of code
  29.         to sort through
  30.     2) Demonstrate differences between LISP and ADS dialog
  31.         programming
  32.     3) Use as a starting point to try out new dialog code
  33.  
  34.          Dlgtest uses the file dlgtest.dcl as the DCL (Dialog
  35.     Control Language) file.
  36.  
  37.          ADS functions are associated with dialog buttons with
  38.     the ads_action_tile functions.  These functions are called
  39.     when the user presses buttons during the ads_start_dialog
  40.     function.
  41.  
  42.     Special tile names (keys):
  43.         "accept" - Ok button
  44.         "cancel" - Cancel button
  45.  
  46. */
  47.  
  48.  
  49. #include <stdio.h>
  50. #include <string.h>
  51.  
  52. #include "adslib.h"
  53. #include "adsdlg.h"
  54.  
  55.  
  56. /* All Function Prototypes for DLGTEST.C */
  57.  
  58. void main _((int argc, char *argv[]));
  59. int loadfuncs _((void));
  60. int dofun _((void));
  61. void setdimen _((void));
  62. void get_dimvars _((ads_hdlg hdlg));
  63. void set_dimvars _((ads_hdlg hdlg));
  64. void CALLB dimen_ok   _((ads_callback_packet *cpkt));
  65. void CALLB editcol_cb _((ads_callback_packet *cpkt));
  66. void CALLB listcol_cb _((ads_callback_packet *cpkt));
  67. int setcolor _((void));
  68.  
  69. #ifdef __ZTC__
  70. /* With an old style definition, the short code parameter is promoted to an
  71.    int, and so a short parameter should be prototyped as an int.  This is
  72.    ANSI C. */
  73. void dlg_colortile _((ads_hdlg hdlg, char *key, int color, int borderflag));
  74. void dlg_rect _((int x, int y, int width, int height, int color));
  75. #else
  76. void dlg_colortile _((ads_hdlg hdlg, char *key, short color, int borderflag));
  77. void dlg_rect _((short x, short y, short width, short height, short color));
  78. #endif
  79.  
  80.  
  81. /* Subroutines names to be registered with AutoLisp and provided in this
  82.    application. */
  83.  
  84. static char *exfun[] = {/*MSG0*/"C:dimen", /*MSG0*/"C:setcolor", NULL};
  85.  
  86.  
  87. static char *dimbools[] = {
  88.     /*MSG0*/"dimse1",
  89.     /*MSG0*/"dimse2",
  90.     /*MSG0*/"dimtih",
  91.     /*MSG0*/"dimtoh",
  92.     /*MSG0*/"dimtad",
  93.     /*MSG0*/"dimtol",
  94.     /*MSG0*/"dimlim",
  95.     /*MSG0*/"dimalt",
  96.     /*MSG0*/"dimaso",
  97.     /*MSG0*/"dimsho",
  98.     NULL
  99.    };
  100.  
  101. static char *dimreals[] = {
  102.     /*MSG0*/"dimasz",
  103.     /*MSG0*/"dimtsz",
  104.     /*MSG0*/"dimtxt",
  105.     /*MSG0*/"dimcen",
  106.     /*MSG0*/"dimexo",
  107.     /*MSG0*/"dimexe",
  108.     /*MSG0*/"dimdle",
  109.     NULL
  110.    };
  111.  
  112. #ifndef BLACK
  113. #define    BLACK    0
  114. #define    RED      1
  115. #define    YELLOW   2
  116. #define    GREEN    3
  117. #define    CYAN     4
  118. #define    BLUE     5
  119. #define    MAGENTA  6
  120. #define    WHITE    7
  121. #endif
  122.  
  123. static short color;
  124. #define CLEN 32
  125. static char colorstr[CLEN+1];
  126.  
  127. static char *colorlist[] = {
  128.     /*MSG1*/"black",
  129.     /*MSG2*/"red",
  130.     /*MSG3*/"yellow",
  131.     /*MSG4*/"green",
  132.     /*MSG5*/"cyan",
  133.     /*MSG6*/"blue",
  134.     /*MSG7*/"magenta",
  135.     /*MSG8*/"white"
  136.    };
  137.  
  138.  
  139.  
  140. /* MAIN -- the main routine */
  141. void
  142. /*FCN*/main(argc,argv)
  143.   int argc; char *argv[];
  144. {
  145.     short scode = RSRSLT;
  146.     int stat;
  147.  
  148.     ads_init(argc, argv);
  149.  
  150.     for ( ;; ) {
  151.  
  152.         if ((stat = ads_link(scode)) < 0) {
  153.             printf(/*MSG9*/"TEST: Ñ╤ ads_link() ╢╟ª^¬║ñú¿╬¬║¬¼║A = %d\n", stat);
  154.             fflush(stdout);
  155.             exit(1);
  156.         }
  157.  
  158.         scode = RSRSLT;               /* default return code */
  159.  
  160.         /* Check for AT LEAST the following cases here */
  161.         switch (stat) {
  162.  
  163.             /* Load or define Lisp functions */
  164.         case RQXLOAD:                 /* Load request.  Send function defuns */
  165.             scode = loadfuncs() ? -RSRSLT : -RSERR;
  166.             break;
  167.  
  168.             /* Unload or undefine ALL functions previously defined. */
  169.         case RQXUNLD:                 /* Unload request. Do cleanup */
  170.             /* This defaults to sending an RSRSLT.  If you send an
  171.                RSERR, you can refuse to unload the program, but Lisp
  172.                will still ask you to terminate. */
  173.             break;
  174.  
  175.             /* Execute a "loaded" function that was defined via RQXLOAD */
  176.         case RQSUBR:
  177.             dofun();
  178.             break;
  179.  
  180.         default:
  181.             break;
  182.         }
  183.     }
  184. }
  185.  
  186.  
  187.  
  188. /* LOADFUNCS  --  Define external functions with AutoLISP */
  189. static int
  190. /*FCN*/loadfuncs()
  191. {
  192.     int i;
  193.  
  194.     for (i = 0; exfun[i] != NULL; i++) {
  195.         if (!ads_defun(exfun[i], i))
  196.             return FALSE;
  197.     }
  198.     ads_printf(/*MSG10*/"Ñ\134»α: 1-╝╨╡∙ 2-│]⌐w├CªΓíC\n");
  199.     return TRUE;
  200.  
  201. }
  202.  
  203. /* Execute a defined function. */
  204.  
  205. static int
  206. /*FCN*/dofun()
  207. {
  208.     int id;
  209.     struct resbuf *rb;
  210.  
  211.     /* Get the function code from the combuf */
  212.     if ((id = ads_getfuncode()) < 0)
  213.         return 0;
  214.  
  215.     if ((rb = ads_getargs()) != NULL) {
  216.         ads_printf("ñú└│ª│íuñ▐╝╞ív");
  217.         ads_relrb(rb);
  218.     }
  219.  
  220.     switch (id) {                     /* Which function is called? */
  221.  
  222.     case 0:
  223.         /* dimen -- AutoCAD dimensioning dialog */
  224.         setdimen();
  225.         break;
  226.  
  227.     case 1:
  228.         /* setcolor -- AutoCAD color dialog */
  229.         setcolor();
  230.         break;
  231.  
  232.     default:
  233.         break;
  234.     }
  235.     return 1;
  236. }
  237.  
  238.  
  239. /*
  240.  
  241.                 DIMEN -- Dimensioning Dialog
  242.  
  243. */
  244.  
  245. /* Position of dialog, centered to start */
  246. static int dimx = -1,
  247.            dimy = -1;
  248.  
  249. void
  250. /*FCN*/setdimen()
  251. {
  252.     ads_hdlg hdlg;
  253.     int dlg_status;
  254.     int dcl_id;
  255.  
  256.     ads_load_dialog(/*MSG0*/"dlgtest.dcl", &dcl_id);
  257.     if (dcl_id < 0) {
  258.         ads_printf(/*MSG13*/"╕ⁿñJíudlgtest.dclív┤┴╢í╡oÑ═┐∙╗~\n");
  259.         return;
  260.     }
  261.     /* Display the "dimensions" dialog */
  262.     ads_new_positioned_dialog(/*MSG14*/"dimensions", dcl_id,
  263.                         (CLIENTFUNC)0, dimx, dimy, &hdlg);
  264.     if (hdlg == NULL) {
  265.         ads_printf(/*MSG15*/"íuads_new_dialogívÑ\134»αÑó▒╤\n");
  266.         ads_unload_dialog(dcl_id);
  267.         return;
  268.     }
  269.  
  270.     /* Register dimen_ok function with the OK button */
  271.     ads_action_tile(hdlg, /*MSG0*/"accept", (CLIENTFUNC)dimen_ok);
  272.  
  273.     /* show current values in dialog */
  274.     get_dimvars(hdlg);
  275.  
  276.     /* run the dialog */
  277.     ads_start_dialog(hdlg, &dlg_status);
  278.  
  279.     /* free all memory for dialog */
  280.     ads_unload_dialog(dcl_id);
  281. }
  282.  
  283.  
  284. /* DIMEN_OK -- callback function for OK button of dimension dialog.  */
  285.  
  286. static void CALLB
  287. /*FCN*/dimen_ok(cpkt)
  288.   ads_callback_packet *cpkt;
  289. {
  290.     /* User pressed OK button to end dialog.  Check modified data
  291.        and send to AutoCAD. */
  292.     set_dimvars(cpkt->dialog);
  293.     ads_done_positioned_dialog(cpkt->dialog, 1, &dimx, &dimy);
  294. }
  295.  
  296.  
  297. /* Show current values in dialog */
  298.  
  299. void
  300. /*FCN*/get_dimvars(hdlg)
  301.   ads_hdlg hdlg;
  302. {
  303.     char *bitem;
  304.     char *ritem;
  305.     char **blist = dimbools;
  306.     char **rlist = dimreals;
  307.     char value[80];
  308.     struct resbuf rb;
  309.  
  310.     for ( ; (bitem = *blist) != NULL; ++blist) {
  311.         ads_getvar(bitem, &rb);
  312.         if (rb.restype != RTSHORT) {
  313.             ads_printf(/*MSG17*/"╡Lª╣íuAutoCAD ┼▄╝╞ív: %s\n", bitem);
  314.             if (rb.restype == RTSTR)
  315.                 free(rb.resval.rstring);
  316.             continue;
  317.         }
  318.         sprintf(value, "%d", rb.resval.rint);
  319.         ads_set_tile(hdlg, bitem, value);
  320.     }
  321.     for ( ; (ritem = *rlist) != NULL; ++rlist) {
  322.         ads_getvar(ritem, &rb);
  323.         if (rb.restype != RTREAL) {
  324.             ads_printf(/*MSG18*/"╡Lª╣íuAutoCAD ┼▄╝╞ív: %s\n", ritem);
  325.             if (rb.restype == RTSTR)
  326.                 free(rb.resval.rstring);
  327.             continue;
  328.         }
  329.         ads_rtos(rb.resval.rreal, -1, -1, value);
  330.         ads_set_tile(hdlg, ritem, value);
  331.     }
  332.     ads_set_tile(hdlg, /*MSG0*/"test_item", /*MSG0*/"test_value");
  333. }
  334.  
  335.  
  336. /* set modified dimension variables in AutoCAD */
  337.  
  338. static void
  339. /*FCN*/set_dimvars(hdlg)
  340.   ads_hdlg hdlg;
  341. {
  342.     char *bitem;
  343.     char *ritem;
  344.     char **blist = dimbools;
  345.     char **rlist = dimreals;
  346.     char val[MAX_TILE_STR+1];
  347.     struct resbuf rb;
  348.  
  349.     /* Check all the checkbox tiles for new values */
  350.     for ( ; (bitem = *blist) != NULL; ++blist) {
  351.         /* Get the new value of tiles */
  352.         ads_get_tile(hdlg, bitem, val, MAX_TILE_STR);
  353.  
  354.         rb.restype = RTSHORT;
  355.         rb.resval.rint = atoi(val);
  356.         ads_setvar(bitem, &rb);
  357.     }
  358.     /* Check all the edit box tiles for new values */
  359.     for ( ; (ritem = *rlist) != NULL; ++rlist) {
  360.         /* Get the new value of tiles */
  361.         ads_get_tile(hdlg, ritem, val, MAX_TILE_STR);
  362.         rb.restype = RTREAL;
  363.         ads_distof(val, -1, &rb.resval.rreal);
  364.         ads_setvar(ritem, &rb);
  365.     }
  366.     ads_get_tile(hdlg, /*MSG0*/"test_item", /*MSG0*/"test_value", 50);
  367. }
  368.  
  369.  
  370.  
  371. /*
  372.  
  373.                 SETCOLOR -- Color Dialog
  374.  
  375. */
  376.  
  377. int
  378. /*FCN*/setcolor()
  379. {
  380.     int idx;
  381.     ads_hdlg hdlg;
  382.     int dlg_status;
  383.     struct resbuf rb;
  384.     char *cptr, *ptr;
  385.     short colorsave;
  386.     char cname[10];
  387.     int dcl_id;
  388.  
  389.     /* Load the dialog file */
  390.     ads_load_dialog(/*MSG0*/"dlgtest.dcl", &dcl_id);
  391.     if (dcl_id < 0) {
  392.         ads_printf(/*MSG19*/"╕ⁿñJíudlgtest.dclív┤┴╢í╡oÑ═┐∙╗~\n");
  393.         return -1;
  394.     }
  395.     /* initialize the setcolor dialog, no default callback function */
  396.     ads_new_dialog(/*MSG0*/"setcolor", dcl_id, (CLIENTFUNC)0, &hdlg);
  397.     if (hdlg == NULL) {
  398.         ads_printf(/*MSG20*/"íu│]⌐w├CªΓív╢╡Ñ╪¬║íunew_dialogívÑó▒╤\n");
  399.         ads_unload_dialog(dcl_id);
  400.         return -1;
  401.     }
  402.     /* Get the current color from AutoCAD */
  403.     ads_getvar(/*MSG0*/"CECOLOR", &rb);
  404.     /* AutoCAD  currently returns  "human readable" colour strings
  405.        like "1 (red)" for the standard colours.  Trim  the  string
  406.        at  the  first space to guarantee we have a valid string to
  407.        restore the colour later.  */
  408.     cptr = rb.resval.rstring;
  409.     ptr = strchr(cptr, ' ');
  410.     if (ptr != NULL)
  411.         *ptr = EOS;
  412.     strcpy(colorstr, cptr);
  413.     color = colorsave = atoi(colorstr);
  414.  
  415.     /* Update other tiles when one is changed by using callback
  416.        functions */
  417.     ads_action_tile(hdlg, /*MSG0*/"edit_col", (CLIENTFUNC)editcol_cb);
  418.     ads_action_tile(hdlg, /*MSG0*/"list_col", (CLIENTFUNC)listcol_cb);
  419.  
  420.     /* Use the client data pointer to store the key of each tile,
  421.        for convenient access during callbacks.   We could use
  422.        get_attr_string during the callbacks instead. */
  423.     ads_client_data_tile(hdlg, /*MSG0*/"edit_col", /*MSG0*/"edit_col");
  424.     ads_client_data_tile(hdlg, /*MSG0*/"list_col", /*MSG0*/"list_col");
  425.  
  426.     /* Fill list box */
  427.     ads_start_list(hdlg, /*MSG0*/"list_col", LIST_NEW, 0);
  428.     for (idx = 0; idx < 8; ++idx)
  429.         ads_add_list(colorlist[idx]);
  430.     for (idx = 8; idx < 256; ++idx) {
  431.         sprintf(cname, "%d", idx);
  432.         ads_add_list(cname);
  433.     }
  434.     ads_end_list();
  435.  
  436.     /* Show initial values in edit box, list box, and image tile */
  437.     ads_set_tile(hdlg, /*MSG0*/"edit_col", colorstr);
  438.     ads_set_tile(hdlg, /*MSG0*/"list_col", colorstr);
  439.     dlg_colortile(hdlg, /*MSG0*/"show_image", color, TRUE);
  440.  
  441.     /* Hand control over to the dialog until OK or CANCEL is pressed */
  442.     ads_start_dialog(hdlg, &dlg_status);
  443.  
  444.     /* Dialog ended with OK button, "accept"? */
  445.     if (dlg_status == DLGOK) {
  446.         if (color == 0)
  447.             ads_command(RTSTR, /*MSG0*/"COLOUR", RTSTR, /*MSG0*/"BYLAYER",
  448.                         RTNONE);
  449.         else
  450.             ads_command(RTSTR, /*MSG0*/"COLOUR", RTSHORT, color, RTNONE);
  451.     }
  452.     /* free all memory for dialog */
  453.     ads_unload_dialog(dcl_id);
  454.  
  455.     return (dlg_status == DLGOK) ? color : colorsave;
  456. }
  457.  
  458.  
  459. /* EDITCOL_CB -- ADS callback for color edit box.  */
  460.  
  461. static void CALLB
  462. /*FCN*/editcol_cb(cpkt)
  463.   ads_callback_packet *cpkt;
  464. {
  465.     if (cpkt->value == NULL || cpkt->value[0] == EOS)
  466.         return;
  467.  
  468.     ads_set_tile(cpkt->dialog, /*MSG0*/"list_col", cpkt->value);
  469.     color = atoi(cpkt->value);
  470.     dlg_colortile(cpkt->dialog, /*MSG0*/"show_image", color, TRUE);
  471. }
  472.  
  473. #define MAXKEYLEN 32
  474.  
  475. /* LISTCOL_CB -- ADS callback for color edit box.  */
  476.  
  477. static void CALLB
  478. /*FCN*/listcol_cb(cpkt)
  479.   ads_callback_packet *cpkt;
  480. {
  481.     char akey[MAXKEYLEN];
  482.  
  483.     if (cpkt->value == NULL || cpkt->value[0] == EOS)
  484.         return;
  485.  
  486.     /* Get key */
  487.     ads_get_attr_string(cpkt->tile, /*MSG0*/"key", akey, MAXKEYLEN);
  488.  
  489.     ads_set_tile(cpkt->dialog, /*MSG0*/"edit_col", cpkt->value);
  490.     color = atoi(cpkt->value);
  491.     dlg_colortile(cpkt->dialog, /*MSG0*/"show_image", color, TRUE);
  492. }
  493.  
  494.  
  495.  
  496.  
  497.  
  498. /* DLG_COLORTILE -- Color a tile.  "dialog" can be NULL to use the
  499.                     current dialog.  Values for "color" may be 0 to
  500.                     255.  "color" may be one of the defines in
  501.                     colours.h, such as RED.   Draws border if
  502.                     borderflag is TRUE. */
  503.  
  504. void
  505. /*FCN*/dlg_colortile(hdlg, key, color, borderflag)
  506.   ads_hdlg hdlg;
  507.   char *key;
  508.   short color;
  509.   int borderflag;
  510. {
  511.     short width, height;
  512.  
  513.     ads_dimensions_tile(hdlg, key, &width, &height);
  514.     ads_start_image(hdlg, key);
  515.     ads_fill_image(0, 0, width, height, color);
  516.  
  517.     if (borderflag) {
  518.         /* Put border around color */
  519.         dlg_rect(0, 0, width, height, WHITE);
  520.     }
  521.     ads_end_image();
  522. }
  523.  
  524.  
  525.  
  526. /* DLG_RECT -- Draw a rectangle in an image tile.  Use tile dimensions
  527.                to draw border around tile.  Assumes start_image
  528.                has been called. */
  529.  
  530. void
  531. /*FCN*/dlg_rect(x, y, width, height, color)
  532.   short x;
  533.   short y;
  534.   short width;
  535.   short height;
  536.   short color;
  537. {
  538.     short x2, y2;
  539.  
  540.     x2 = x + width - 1;
  541.     y2 = y + height - 1;
  542.     ads_vector_image(x,  y,  x,  y2, color);
  543.     ads_vector_image(x,  y2, x2, y2, color);
  544.     ads_vector_image(x2, y2, x2, y,  color);
  545.     ads_vector_image(x2, y,  x,  y,  color);
  546. }
  547.  
  548.  
  549.