home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_quik.lha / quickdraw / src / main_calls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-09  |  8.5 KB  |  397 lines

  1. /*--------------------------------- calls.c ----------------------------------*/
  2. /* Copyright 1989 Brown University -- Jeffrey Vogel                           */
  3. /*----------------------------------------------------------------------------*/
  4.  
  5.  
  6. /*--------------------------------- Include  ---------------------------------*/
  7. /*----------------------------------------------------------------------------*/
  8.  
  9. #include "qd_local.h"
  10.  
  11. /*--------------------------------- Statics ----------------------------------*/
  12. /*----------------------------------------------------------------------------*/
  13.  
  14. static XSetWindowAttributes   QDwindowAttributes;
  15.  
  16.  
  17. /*--------------------------------- InitDraw ---------------------------------*/
  18. /*----------------------------------------------------------------------------*/
  19.  
  20. void
  21. InitDraw(width, height)
  22. int         width, height;
  23. {
  24.    XSizeHints  size_hints;
  25.    XEvent      report;
  26.  
  27.    /*** error checks ***/
  28.    if (QDrunning) {
  29.       QDerror("ERROR InitDraw: QuickDraw already initialized.");
  30.       return;
  31.    }
  32.    if ((width < 0) || (height < 0)) {
  33.       QDerror("ERROR InitDraw: Bad size parameters.");
  34.       return;
  35.    }
  36.  
  37.    /*** initializing ***/
  38.    if (!QDdisplay) {
  39.       QDopen(NULL, NULL);
  40.       if (!QDdisplay) {
  41.          QDerror("ERROR InitDraw: Unable to open display.");
  42.          return;
  43.       }
  44.       QDautoDisplay = TRUE;
  45.    }
  46.    else
  47.       QDautoDisplay = FALSE;
  48.  
  49.  
  50.    if ((width >= DisplayWidth(QDdisplay, QDscreen))) {
  51.       QDerror("ERROR InitDraw: Width too big.");
  52.       return;
  53.    }
  54.    if ((height >= DisplayHeight(QDdisplay, QDscreen))) {
  55.       QDerror("ERROR InitDraw: Height too big.");
  56.       return;
  57.    }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.    /*** initialize variables ***/
  68.    QDrunning = TRUE;
  69.    QDwidth   = width;
  70.    QDheight  = height;
  71.    QDx = QDy = 0;
  72.    srandom(1);
  73.  
  74.    /*** set initial attributes ***/
  75.    QDgcValues.line_width = LINE_WIDTH__DEFAULT;
  76.    QDgcValues.fill_style = FillSolid;;
  77.    QDgcValues.line_style = LINE_STYLE__DEFAULT;
  78.    QDgcValues.function = FUNCTION__DEFAULT;;
  79.    QDgcValues.font = QDfonts[FONT__DEFAULT];
  80.    QDgcValues.foreground = BlackPixel(QDdisplay, QDscreen);
  81.    QDgcValues.background = WhitePixel(QDdisplay, QDscreen);
  82.    XChangeGC(QDdisplay, QDgc, 
  83.              GCForeground|GCBackground|GCLineWidth|GCFont|
  84.              GCFunction|GCLineStyle|GCFillStyle, 
  85.              &QDgcValues);
  86.  
  87.    /*** Build window ***/
  88.    QDwindow = XCreateSimpleWindow(QDdisplay, 
  89.                             QDroot,
  90.                             0, 0, (width == 0) ? 100 : width, 
  91.                             (height == 0) ? 100 : height, 
  92.                             2,
  93.                             BlackPixel(QDdisplay, QDscreen),
  94.                             WhitePixel(QDdisplay, QDscreen));
  95.    XSelectInput(QDdisplay, QDwindow, StructureNotifyMask);
  96.  
  97.    /*** size hints ***/
  98.    size_hints.flags = PPosition|PSize;
  99.    size_hints.width = width;
  100.    size_hints.height = height;
  101.    XSetStandardProperties(QDdisplay, QDwindow, "QuickDraw",
  102.                           "QuickDraw", None, NULL, 0, &size_hints);
  103.    
  104.    QDwindowAttributes.backing_store = Always;
  105.    QDwindowAttributes.colormap = QDcolormap;
  106.    XChangeWindowAttributes(QDdisplay, QDwindow, 
  107.                            CWColormap|CWBackingStore,
  108.                            &QDwindowAttributes);
  109.  
  110.    /*** map window ***/      
  111.    XMapRaised(QDdisplay, QDwindow);
  112.    do {
  113.       XNextEvent(QDdisplay, &report);
  114.    } while ((report.type != MapNotify) || (report.xany.window != QDwindow));
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. /*-------------------------------- InitDraw4 ---------------------------------*/
  134. /*----------------------------------------------------------------------------*/
  135.  
  136. void
  137. InitDraw4(width, height, x, y)
  138. int         width, height, x, y;
  139. {
  140.    XSizeHints  size_hints;
  141.    XEvent      report;
  142.  
  143.    /*** error checks ***/
  144.    if (QDrunning) {
  145.       QDerror("ERROR InitDraw: QuickDraw already initialized.");
  146.       return;
  147.    }
  148.    if ((width < 0) || (height < 0)) {
  149.       QDerror("ERROR InitDraw: Bad size parameters.");
  150.       return;
  151.    }
  152.  
  153.    /*** initializing ***/
  154.    if (!QDdisplay) {
  155.       QDopen(NULL, NULL);
  156.       if (!QDdisplay) {
  157.          QDerror("ERROR InitDraw: Unable to open display.");
  158.          return;
  159.       }
  160.       QDautoDisplay = TRUE;
  161.    }
  162.    else
  163.       QDautoDisplay = FALSE;
  164.  
  165.  
  166.    if ((width >= DisplayWidth(QDdisplay, QDscreen))) {
  167.       QDerror("ERROR InitDraw: Width too big.");
  168.       return;
  169.    }
  170.    if ((height >= DisplayHeight(QDdisplay, QDscreen))) {
  171.       QDerror("ERROR InitDraw: Height too big.");
  172.       return;
  173.    }
  174.  
  175.  
  176.    /*** initialize variables ***/
  177.    QDrunning = TRUE;
  178.    QDwidth   = width;
  179.    QDheight  = height;
  180.    QDx = QDy = 0;
  181.    srandom(1);
  182.  
  183.    /*** set initial attributes ***/
  184.    QDgcValues.line_width = LINE_WIDTH__DEFAULT;
  185.    QDgcValues.fill_style = FillSolid;;
  186.    QDgcValues.line_style = LINE_STYLE__DEFAULT;
  187.    QDgcValues.function = FUNCTION__DEFAULT;;
  188.    QDgcValues.font = QDfonts[FONT__DEFAULT];
  189.    QDgcValues.foreground = BlackPixel(QDdisplay, QDscreen);
  190.    QDgcValues.background = WhitePixel(QDdisplay, QDscreen);
  191.    XChangeGC(QDdisplay, QDgc, 
  192.              GCForeground|GCBackground|GCLineWidth|GCFont|
  193.              GCFunction|GCLineStyle|GCFillStyle, 
  194.              &QDgcValues);
  195.  
  196.  
  197.  
  198.  
  199.    /*** Build window ***/
  200.    QDwindow = XCreateSimpleWindow(QDdisplay, 
  201.                             QDroot,
  202.                             0, 0, (width == 0) ? 100 : width, 
  203.                             (height == 0) ? 100 : height, 
  204.                             2,
  205.                             BlackPixel(QDdisplay, QDscreen),
  206.                             WhitePixel(QDdisplay, QDscreen));
  207.    XSelectInput(QDdisplay, QDwindow, StructureNotifyMask);
  208.  
  209.    /*** size hints ***/
  210.    size_hints.flags = USPosition|USSize;
  211.    size_hints.width = width;
  212.    size_hints.height = height;
  213.    size_hints.x = x;
  214.    size_hints.y = y;
  215.    XSetStandardProperties(QDdisplay, QDwindow, "QuickDraw",
  216.                           "QuickDraw", None, NULL, 0, &size_hints);
  217.    
  218.    QDwindowAttributes.backing_store = Always;
  219.    QDwindowAttributes.colormap = QDcolormap;
  220.    XChangeWindowAttributes(QDdisplay, QDwindow, 
  221.                            CWColormap|CWBackingStore,
  222.                            &QDwindowAttributes);
  223.  
  224.    /*** map window ***/      
  225.    XMapRaised(QDdisplay, QDwindow);
  226.    do {
  227.       XNextEvent(QDdisplay, &report);
  228.    } while ((report.type != MapNotify) || (report.xany.window != QDwindow));
  229. }
  230.  
  231.  
  232.  
  233. /*--------------------------------- ClearAll ---------------------------------*/
  234. /*----------------------------------------------------------------------------*/
  235.  
  236. void
  237. ClearAll()
  238. {
  239.    /*** error check ***/
  240.    if (!QDrunning) {
  241.       QDerror("ERROR ClearAll: QuickDraw not initialized.");
  242.       return;
  243.    }
  244.  
  245.    XClearWindow(QDdisplay, QDwindow);
  246.    XFlush(QDdisplay);
  247. }
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265. /*--------------------------------- QuitDraw ---------------------------------*/
  266. /*----------------------------------------------------------------------------*/
  267.  
  268. void
  269. QuitDraw()
  270. {
  271.    register int i;
  272.  
  273.    if (!QDrunning) {
  274.       QDerror("ERROR QuitDraw: Never initialized.");
  275.       return;
  276.    }
  277.       
  278.    XDestroyWindow(QDdisplay, QDwindow);
  279.    QDrunning = FALSE;
  280.    QDwindow = NULL;
  281.    XFlush(QDdisplay);
  282.  
  283.    if (QDautoDisplay) 
  284.       QDclose();
  285. }
  286.  
  287.  
  288.  
  289. /*------------------------------ GetDrawingRect ------------------------------*/
  290. /*----------------------------------------------------------------------------*/
  291.  
  292. void
  293. GetDrawingRect(r)
  294. Rect  *r;
  295. {
  296.    XWindowAttributes   atts;
  297.  
  298.   /*** error check ***/
  299.    if (!QDrunning) {
  300.       QDerror("ERROR GetDrawingRect: QuickDraw not initialized.");
  301.       return;
  302.    }
  303.  
  304.    XGetWindowAttributes(QDdisplay, QDwindow, &atts);
  305.  
  306.    r->left = 0;
  307.    r->top = 0;
  308.    r->right  = atts.width;
  309.    r->bottom = atts.height;
  310. }
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331. /*--------------------------------- DrawGrid ---------------------------------*/
  332. /*----------------------------------------------------------------------------*/
  333.  
  334. void
  335. DrawGrid(spacing)
  336. int  spacing;
  337. {
  338.    XWindowAttributes   atts;
  339.    int                 w, h;
  340.    register int        i;
  341.  
  342.   /*** error check ***/
  343.    if (!QDrunning) {
  344.       QDerror("ERROR DrawGrid: QuickDraw not initialized.");
  345.       return;
  346.    }
  347.  
  348.    XGetWindowAttributes(QDdisplay, QDwindow, &atts);
  349.    w = atts.width;
  350.    h = atts.height;
  351.  
  352.    /*** vertical lines ***/
  353.    for (i = 0; i < (w / spacing); i++)
  354.       XDrawLine(QDdisplay, QDwindow, QDgridgc, 
  355.                 i*spacing, h, i*spacing, 0);
  356.  
  357.    /*** horizontal lines ***/
  358.    for (i = 0; i < (h / spacing); i++)
  359.       XDrawLine(QDdisplay, QDwindow, QDgridgc, 
  360.                 0, i*spacing, w, i*spacing);
  361.  
  362.    XFlush(QDdisplay);
  363. }
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.