home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / gfx / misc / imagefx_sdk / sas / examples / hooks / gedsample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-15  |  15.6 KB  |  427 lines

  1. /*
  2.  * GedSample
  3.  *
  4.  * Sample ImageFX hook that uses the Ged system for an interface.
  5.  * The hook really doesn't do anything; this just shows how to
  6.  * use the Ged system to make an interface.
  7.  *
  8.  * Written by Thomas Krehbiel, 04-Nov-93
  9.  *
  10.  */
  11.  
  12. #include <scan/hooks.h>
  13. #include <clib/dos_protos.h>
  14. #include <clib/intuition_protos.h>
  15. #include <clib/graphics_protos.h>
  16.  
  17.  
  18. /* This information is required by the "start.o" startup code. */
  19. char *HookName = "GedSample";
  20.  
  21. /* A version string so the Version command works on us. */
  22. char *HookVersion = "\0$VER: GedSample 2.0.2 (15.2.95)";
  23.  
  24.  
  25. /*******************************************************************
  26.  *
  27.  *  Defines
  28.  *
  29.  *******************************************************************/
  30.  
  31. #define WINDOW_WIDTH    (300)
  32. #define WINDOW_HEIGHT   (100)
  33.  
  34.  
  35. /*******************************************************************
  36.  *
  37.  *  Text Array and Indexes
  38.  *
  39.  *******************************************************************/
  40.  
  41. #include "gedsample_strings.h"
  42.  
  43. #if 0
  44.  
  45. char *DefaultStrings[] = {
  46.    "Dummy",
  47.  
  48.    "GED Sample Window",
  49.    "_Okay",
  50.    "_Cancel",
  51.    "Int:",
  52.    "Str:",
  53. };
  54.  
  55. enum {
  56.    G_Dummy = 0,
  57.    G_Title,       /* gadget labels must start from 1! */
  58.    G_Okay,
  59.    G_Cancel,
  60.    G_Integer,
  61.    G_String,
  62.    TXT_COUNT
  63. };
  64.  
  65. /* This information is required by the "start.o" startup code. */
  66. char *HookText = "Hook_GedSample";
  67. int   HookTextCount = TXT_COUNT;
  68.  
  69. #endif
  70.  
  71. /*******************************************************************
  72.  *
  73.  *  These are filled in by the ImageFX Ged system
  74.  *
  75.  *******************************************************************/
  76.  
  77. LONG OutsideArea[4];             /* will contain border size,
  78.                                     [0] = leftedge,
  79.                                     [1] = topedge,
  80.                                     [2] = width,
  81.                                     [3] = height */
  82.  
  83. LONG InsideArea[4];              /* will contain border size,
  84.                                     [0] = leftedge,
  85.                                     [1] = topedge,
  86.                                     [2] = width,
  87.                                     [3] = height */
  88.  
  89. LONG IntegerValue;
  90. char StringValue[80];            /* makes a copy of the string here */
  91.  
  92. /*******************************************************************
  93.  *
  94.  *  Gadget ID's
  95.  *
  96.  *******************************************************************/
  97.  
  98. enum {
  99.    ID_Okay = 300,                /* start high to avoid conflicts */
  100.    ID_Cancel,                    /*    with ImageFX gadgets */
  101.    ID_Integer,
  102.    ID_String
  103. };
  104.  
  105. /*******************************************************************
  106.  *
  107.  *  Gadget callbacks - these are called when the gadget associated
  108.  *  with the callback is "fiddled" with.
  109.  *
  110.  *  Be sure to use __saveds for all callbacks!
  111.  *
  112.  *  Any callback that returns non-zero will cause GedWin() to
  113.  *  return immediately.
  114.  *
  115.  *******************************************************************/
  116.  
  117. int __saveds IntegerCode (GedIntegerProto)
  118. {
  119.    /*
  120.     * Check to see if we're within 0 - 100.  If not, beep and
  121.     * reset ourselves to those bounds.
  122.     *
  123.     * 'w' and 'val' are found in the GedIntegerProto define.
  124.     * 'w' is a pointer to the window containing the gadget.
  125.     * 'val' is the current value of the gadget.
  126.     */
  127.  
  128.    if (val < 0) {
  129.       DisplayBeep(w->WScreen);
  130.       Ged_Set(w, ID_Integer, 0);
  131.    }
  132.    else if (val > 100) {
  133.       DisplayBeep(w->WScreen);
  134.       Ged_Set(w, ID_Integer, 100);
  135.    }
  136.  
  137.    return(0);
  138. }
  139.  
  140. /*******************************************************************
  141.  *
  142.  *  NewGad array - defines elements of our interface
  143.  *
  144.  *******************************************************************/
  145.  
  146. struct NewGad newGads[] = {
  147.  
  148.    { Scale_ID },                 /* voodoo magic */
  149.  
  150.    /* okay gadget */
  151.    { Button_ID,                  /* style */
  152.          ID_Okay,                /* gadget id */
  153.          8,                      /* left */
  154.          WINDOW_HEIGHT - 16,     /* top */
  155.          100,                    /* width */
  156.          12,                     /* height - 12's a good height for buttons */
  157.          G_Okay,                 /* label - index into text array */
  158.          NULL,                   /* code - none needed */
  159.          0,                      /* userdata - none needed */
  160.          NULL,                   /* pointer - not used */
  161.          ID_Okay,                /* data1 - exit GedWin with this return code
  162.                                        when user clicks this gadget */
  163.          0,                      /* data2 - unused */
  164.          0,                      /* data3 - unused */
  165.          0,                      /* data4 - unused */
  166.          0,                      /* data5 - unused */
  167.          0,                      /* data6 - unused */
  168.          0,                      /* data7 - unused */
  169.          0                       /* data8 - unused */
  170.    },
  171.  
  172.    /* cancel gadget */
  173.    { Button_ID,                  /* style */
  174.          ID_Cancel,              /* gadget id */
  175.          WINDOW_WIDTH - 108,     /* left */
  176.          WINDOW_HEIGHT - 16,     /* top */
  177.          100,                    /* width */
  178.          12,                     /* height - 12's a good height for buttons */
  179.          G_Cancel,               /* label - index into text array */
  180.          NULL,                   /* code - none needed */
  181.          0,                      /* userdata - none needed */
  182.          NULL,                   /* pointer - not used */
  183.          ID_Cancel,              /* data1 - exit GedWin with this return code
  184.                                        when user clicks this gadget */
  185.          0,                      /* data2 - unused */
  186.          0,                      /* data3 - unused */
  187.          0,                      /* data4 - unused */
  188.          0,                      /* data5 - unused */
  189.          0,                      /* data6 - unused */
  190.          0,                      /* data7 - unused */
  191.          0                       /* data8 - unused */
  192.    },
  193.  
  194.    /* integer gadget */
  195.    { Integer_ID,                 /* style */
  196.          ID_Integer,             /* gadget id */
  197.          50,                     /* left */
  198.          20,                     /* top */
  199.          40,                     /* width */
  200.          14,                     /* height - 14's a good height for strings */
  201.          G_Integer,              /* label - index into text array */
  202.          IntegerCode,            /* code - callback function */
  203.          0,                      /* userdata - none needed */
  204.          &IntegerValue,          /* pointer - where to store value */
  205.          0,                      /* data1 - lowest value (not implemented) */
  206.          50,                     /* data2 - initial value */
  207.          100,                    /* data3 - highest value (not implemented) */
  208.          ID_String,              /* data4 - next gadget id to activate */
  209.          1,                      /* data5 - justification
  210.                                        (0=left, 1=right, 2=center) */
  211.          0,                      /* data6 - unused */
  212.          0,                      /* data7 - unused */
  213.          0                       /* data8 - unused */
  214.    },
  215.  
  216.    /* string gadget */
  217.    { String_ID,                  /* style */
  218.          ID_String,              /* gadget id */
  219.          WINDOW_WIDTH/2+40,      /* left */
  220.          20,                     /* top */
  221.          80,                     /* width */
  222.          14,                     /* height - 14's a good height for strings */
  223.          G_String,               /* label - index into text array */
  224.          NULL,                   /* code - none needed */
  225.          0,                      /* userdata - none needed */
  226.          (long *)StringValue,    /* pointer - where to store value */
  227.          80,                     /* data1 - max chars */
  228.          (long)"Testing",        /* data2 - initial string */
  229.          0,                      /* data3 - unused */
  230.          ID_Integer,             /* data4 - next gadget id to activate */
  231.          2,                      /* data5 - justification
  232.                                        (0=left, 1=right, 2=center) */
  233.          0,                      /* data6 - unused */
  234.          0,                      /* data7 - unused */
  235.          0                       /* data8 - unused */
  236.    },
  237.  
  238.    /* window title */
  239.    { Text_ID,                    /* style */
  240.          0,                      /* id - none needed */
  241.          WINDOW_WIDTH/2,         /* left */
  242.          4,                      /* top */
  243.          0,                      /* width - unneeded */
  244.          0,                      /* height - unneeded */
  245.          G_Title,                /* label - index into text array */
  246.          NULL,                   /* code - none needed */
  247.          0,                      /* userdata - none needed */
  248.          NULL,                   /* pointer - unused */
  249.          2,                      /* data1 - drawing pen (2=white) */
  250.          0,                      /* data2 - unused */
  251.          2,                      /* data3 - justification
  252.                                        (0=left, 1=right, 2=center) */
  253.          0,                      /* data4 - unused */
  254.          0,                      /* data5 - unused */
  255.          0,                      /* data6 - unused */
  256.          0,                      /* data7 - unused */
  257.          0                       /* data8 - unused */
  258.    },
  259.  
  260.    /* window border */
  261.    { Border_ID,                  /* style */
  262.          0,                      /* id - none needed */
  263.          0,                      /* left */
  264.          0,                      /* top */
  265.          WINDOW_WIDTH,           /* width */
  266.          WINDOW_HEIGHT,          /* height */
  267.          0,                      /* label - none needed */
  268.          NULL,                   /* code - none needed */
  269.          0,                      /* userdata - none needed */
  270.          OutsideArea,            /* pointer - (optional) - fill in actual
  271.                                     size of the border when created;
  272.                                     useful for drawing stuff inside
  273.                                     the border so we know where the
  274.                                     actual border coords are.  Use NULL
  275.                                     if you don't need this.  This is just
  276.                                     here as an example. */
  277.          FALSE,                  /* data1 - recessed border? */
  278.          TRUE,                   /* data2 - double thick? */
  279.          0,                      /* data3 - unused */
  280.          0,                      /* data4 - unused */
  281.          0,                      /* data5 - unused */
  282.          0,                      /* data6 - unused */
  283.          0,                      /* data7 - unused */
  284.          0                       /* data8 - unused */
  285.    },
  286.  
  287.    /* inside border */
  288.    { Border_ID,                  /* style */
  289.          0,                      /* id - none needed */
  290.          8,                      /* left */
  291.          14,                     /* top */
  292.          WINDOW_WIDTH-16,        /* width */
  293.          WINDOW_HEIGHT-14-18,    /* height */
  294.          0,                      /* label - none needed */
  295.          NULL,                   /* code - none needed */
  296.          0,                      /* userdata - none needed */
  297.          InsideArea,             /* pointer - (optional) - fill in actual
  298.                                     size of the border when created;
  299.                                     useful for drawing stuff inside
  300.                                     the border so we know where the
  301.                                     actual border coords are.  Use NULL
  302.                                     if you don't need this.  This is just
  303.                                     here as an example. */
  304.          TRUE,                   /* data1 - recessed border? */
  305.          FALSE,                  /* data2 - double thick? */
  306.          0,                      /* data3 - unused */
  307.          0,                      /* data4 - unused */
  308.          0,                      /* data5 - unused */
  309.          0,                      /* data6 - unused */
  310.          0,                      /* data7 - unused */
  311.          0                       /* data8 - unused */
  312.    },
  313.  
  314.    { End_ID }                    /* must end with End_ID! */
  315. };
  316.  
  317. /*******************************************************************
  318.  *
  319.  *  NewWindow - defines the window (flags and such)
  320.  *
  321.  *******************************************************************/
  322.  
  323. struct NewWindow newWindow = {
  324.  
  325.    0, 0,                         /* left, top */
  326.    WINDOW_WIDTH, WINDOW_HEIGHT,  /* width, height */
  327.     0, 1,                         /* 1.3 stuff... :) */
  328.     IDCMP_GADGETUP,               /* IDCMP flags (must set correctly!) */
  329.     WFLG_BORDERLESS |             /* Window flags */
  330.        WFLG_SMART_REFRESH |
  331.        WFLG_NOCAREREFRESH |
  332.        WFLG_RMBTRAP |
  333.        WFLG_ACTIVATE,
  334.     NULL,                         /* Firstgadget - unused */
  335.     NULL,
  336.     NULL,
  337.     NULL,                         /* Screen - set by ImageFX in PrepareNW */
  338.     NULL,
  339.     0,0,0,0,                      /* Min/max size */
  340.     WBENCHSCREEN                  /* Type - set by ImageFX in PrepareNW */
  341.  
  342. };
  343.  
  344.  
  345. /*******************************************************************
  346.  *
  347.  * Ginit:
  348.  *
  349.  * GedWin initialization callback.  This is called twice during
  350.  * initialization; once before the window is opened and once after
  351.  * the window is opened and the gadgets are added.  You can tell which
  352.  * case you're dealing with by examining the arguments.  If win != NULL
  353.  * then the window is opened and the gadgets are added, otherwise
  354.  * the window isn't open and the gadgets aren't added (in this case
  355.  * you can fiddle with the NewGad array passed in here).
  356.  *
  357.  * Generally this is used to do extra rendering into the window
  358.  * that can't be done with gadgets and such.  It can also be
  359.  * used to initialize gadget states, although this would better
  360.  * be done by initializing the NewGad array beforehand.
  361.  *
  362.  * Make sure you use __saveds for all callbacks!
  363.  *
  364.  *******************************************************************/
  365.  
  366. int __saveds Ginit (struct Window *win, struct NewGad *ng)
  367. {
  368.    if (win) {
  369.  
  370.       /* this is just an example - it does nothing useful (except
  371.          demonstrate how to find Border_ID positions) */
  372.  
  373.       SetDrMd(win->RPort, COMPLEMENT);
  374.       RectFill(win->RPort,    /* recall that InsideArea[] will contain */
  375.          InsideArea[0]+2,     /*  the coords of a Border_ID */
  376.          InsideArea[1]+1,
  377.          InsideArea[0]+InsideArea[2]-3,
  378.          InsideArea[1]+InsideArea[3]-2);
  379.       Delay(10);
  380.       RectFill(win->RPort,    /* recall that InsideArea[] will contain */
  381.          InsideArea[0]+2,     /*  the coords of a Border_ID */
  382.          InsideArea[1]+1,
  383.          InsideArea[0]+InsideArea[2]-3,
  384.          InsideArea[1]+InsideArea[3]-2);
  385.  
  386.    }
  387.  
  388.    return(1);     /* always return 1 */
  389. }
  390.  
  391.  
  392. /*******************************************************************
  393.  *
  394.  *  Hook entry point is here.
  395.  *
  396.  *******************************************************************/
  397.  
  398. void hook_main (int argc, char **argv)
  399. {
  400.    int returncode;
  401.  
  402.    /*
  403.     * PrepareNW() fills in the NewWindow structure with appropriate
  404.     * values to open on the ImageFX screen (or on Workbench).
  405.     */
  406.    PrepareNW(&newWindow,
  407.       WINDOW_WIDTH, WINDOW_HEIGHT, TRUE);
  408.  
  409.    /*
  410.     * GedWin() opens the window, processes events, and returns when
  411.     * the window is closed.
  412.     */
  413.    returncode = GedWin(&newWindow,  /* newwindow */
  414.                   newGads,          /* newgad array */
  415.                   ID_Integer,       /* id of first string gad to activate */
  416.                   Ginit,            /* init function callback */
  417.                   NULL,             /* cleanup function callback */
  418.                   HookTextArray);   /* text array - from start.o */
  419.  
  420.    if (returncode == ID_Okay) {
  421.       InfoRequest("You hit okay!");
  422.    }
  423.    else if (returncode == ID_Cancel) {
  424.       Errorf("You cancelled, ya bum!");
  425.    }
  426. }
  427.