home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 215 / DDJ9206.ZIP / DFLT12.ZIP / WINDOW.C < prev   
Text File  |  1992-04-17  |  15KB  |  496 lines

  1. /* ---------- window.c ------------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. WINDOW inFocus = NULL;
  6.  
  7. int foreground, background;   /* current video colors */
  8.  
  9. static void TopLine(WINDOW, int, RECT);
  10.  
  11. /* --------- create a window ------------ */
  12. WINDOW CreateWindow(
  13.     CLASS class,              /* class of this window       */
  14.     char *ttl,                /* title or NULL              */
  15.     int left, int top,        /* upper left coordinates     */
  16.     int height, int width,    /* dimensions                 */
  17.     void *extension,          /* pointer to additional data */
  18.     WINDOW parent,            /* parent of this window      */
  19.     int (*wndproc)(struct window *,enum messages,PARAM,PARAM),
  20.     int attrib)               /* window attribute           */
  21. {
  22.     WINDOW wnd = DFcalloc(1, sizeof(struct window));
  23.     get_videomode();
  24.     if (wnd != NULL)    {
  25.         int base;
  26.         /* ----- height, width = -1: fill the screen ------- */
  27.         if (height == -1)
  28.             height = SCREENHEIGHT;
  29.         if (width == -1)
  30.             width = SCREENWIDTH;
  31.         /* ----- coordinates -1, -1 = center the window ---- */
  32.         if (left == -1)
  33.             wnd->rc.lf = (SCREENWIDTH-width)/2;
  34.         else
  35.             wnd->rc.lf = left;
  36.         if (top == -1)
  37.             wnd->rc.tp = (SCREENHEIGHT-height)/2;
  38.         else
  39.             wnd->rc.tp = top;
  40.         wnd->attrib = attrib;
  41.         if (ttl != NULL)
  42.             AddAttribute(wnd, HASTITLEBAR);
  43.         if (wndproc == NULL)
  44.             wnd->wndproc = classdefs[class].wndproc;
  45.         else
  46.             wnd->wndproc = wndproc;
  47.         /* ---- derive attributes of base classes ---- */
  48.         base = class;
  49.         while (base != -1)    {
  50.             AddAttribute(wnd, classdefs[base].attrib);
  51.             base = classdefs[base].base;
  52.         }
  53.         if (parent && !TestAttribute(wnd, NOCLIP))    {
  54.             /* -- keep upper left within borders of parent - */
  55.             wnd->rc.lf = max(wnd->rc.lf,GetClientLeft(parent));
  56.             wnd->rc.tp = max(wnd->rc.tp,GetClientTop(parent));
  57.         }
  58.         wnd->class = class;
  59.         wnd->extension = extension;
  60.         wnd->rc.rt = GetLeft(wnd)+width-1;
  61.         wnd->rc.bt = GetTop(wnd)+height-1;
  62.         wnd->ht = height;
  63.         wnd->wd = width;
  64.         if (ttl != NULL)
  65.             InsertTitle(wnd, ttl);
  66.         wnd->nextfocus = wnd->prevfocus = wnd->dFocus = NULL;
  67.         wnd->parent = parent;
  68.         wnd->oldcondition = wnd->condition = ISRESTORED;
  69.         wnd->RestoredRC = wnd->rc;
  70.         wnd->PrevKeyboard = wnd->PrevMouse = NULL;
  71.         SendMessage(wnd, CREATE_WINDOW, 0, 0);
  72.         InitWindowColors(wnd);
  73.         if (isVisible(wnd))
  74.             SendMessage(wnd, SHOW_WINDOW, 0, 0);
  75.     }
  76.     return wnd;
  77. }
  78.  
  79. /* -------- add a title to a window --------- */
  80. void AddTitle(WINDOW wnd, char *ttl)
  81. {
  82.     InsertTitle(wnd, ttl);
  83.     SendMessage(wnd, BORDER, 0, 0);
  84. }
  85.  
  86. /* ----- insert a title into a window ---------- */
  87. void InsertTitle(WINDOW wnd, char *ttl)
  88. {
  89.     wnd->title=DFrealloc(wnd->title,strlen(ttl)+1);
  90.     strcpy(wnd->title, ttl);
  91. }
  92.  
  93. static unsigned char line[300];
  94.  
  95. /* ------ write a line to video window client area ------ */
  96. void writeline(WINDOW wnd, char *str, int x, int y, BOOL pad)
  97. {
  98.     char *cp;
  99.     int len;
  100.     int dif;
  101.     char wline[200];
  102.  
  103.     memset(wline, 0, 200);
  104.     len = LineLength(str);
  105.     dif = strlen(str) - len;
  106.     strncpy(wline, str, ClientWidth(wnd) + dif);
  107.     if (pad)    {
  108.         cp = wline+strlen(wline);
  109.         while (len++ < ClientWidth(wnd)-x)
  110.             *cp++ = ' ';
  111.     }
  112.     wputs(wnd, wline, x, y);
  113. }
  114.  
  115. RECT AdjustRectangle(WINDOW wnd, RECT rc)
  116. {
  117.     /* -------- adjust the rectangle ------- */
  118.     if (TestAttribute(wnd, HASBORDER))    {
  119.         if (RectLeft(rc) == 0)
  120.             --rc.rt;
  121.         else if (RectLeft(rc) < RectRight(rc) &&
  122.                 RectLeft(rc) < WindowWidth(wnd)+1)
  123.             --rc.lf;
  124.     }
  125.     if (TestAttribute(wnd, HASBORDER | HASTITLEBAR))    {
  126.         if (RectTop(rc) == 0)
  127.             --rc.bt;
  128.         else if (RectTop(rc) < RectBottom(rc) &&
  129.                 RectTop(rc) < WindowHeight(wnd)+1)
  130.             --rc.tp;
  131.     }
  132.     RectRight(rc) = max(RectLeft(rc),
  133.                         min(RectRight(rc),WindowWidth(wnd)));
  134.     RectBottom(rc) = max(RectTop(rc),
  135.                         min(RectBottom(rc),WindowHeight(wnd)));
  136.     return rc;
  137. }
  138.  
  139. /* -------- display a window's title --------- */
  140. void DisplayTitle(WINDOW wnd, RECT *rcc)
  141. {
  142.     if (GetTitle(wnd) != NULL)    {
  143.         int tlen = min(strlen(GetTitle(wnd)), WindowWidth(wnd)-2);
  144.         int tend = WindowWidth(wnd)-3-BorderAdj(wnd);
  145.         RECT rc;
  146.  
  147.         if (rcc == NULL)
  148.             rc = RelativeWindowRect(wnd, WindowRect(wnd));
  149.         else
  150.             rc = *rcc;
  151.         rc = AdjustRectangle(wnd, rc);
  152.  
  153.         if (SendMessage(wnd, TITLE, (PARAM) rcc, 0))    {
  154.             if (wnd == inFocus)    {
  155.                 foreground = cfg.clr[TITLEBAR] [HILITE_COLOR] [FG];
  156.                 background = cfg.clr[TITLEBAR] [HILITE_COLOR] [BG];
  157.             }
  158.             else    {
  159.                 foreground = cfg.clr[TITLEBAR] [STD_COLOR] [FG];
  160.                 background = cfg.clr[TITLEBAR] [STD_COLOR] [BG];
  161.             }
  162.             memset(line,' ',WindowWidth(wnd));
  163. #ifdef INCLUDE_MINIMIZE
  164.             if (wnd->condition != ISMINIMIZED)
  165. #endif
  166.                 strncpy(line + ((WindowWidth(wnd)-2 - tlen) / 2),
  167.                     wnd->title, tlen);
  168.             if (TestAttribute(wnd, CONTROLBOX))
  169.                 line[2-BorderAdj(wnd)] = CONTROLBOXCHAR;
  170.             if (TestAttribute(wnd, MINMAXBOX))    {
  171.                 switch (wnd->condition)    {
  172.                     case ISRESTORED:
  173. #ifdef INCLUDE_MAXIMIZE
  174.                         line[tend+1] = MAXPOINTER;
  175. #endif
  176. #ifdef INCLUDE_MINIMIZE
  177.                         line[tend]   = MINPOINTER;
  178. #endif
  179.                         break;
  180. #ifdef INCLUDE_MINIMIZE
  181.                     case ISMINIMIZED:
  182.                         line[tend+1] = MAXPOINTER;
  183.                         break;
  184. #endif
  185. #ifdef INCLUDE_MAXIMIZE
  186.                     case ISMAXIMIZED:
  187. #ifdef INCLUDE_MINIMIZE
  188.                         line[tend]   = MINPOINTER;
  189. #endif
  190. #ifdef INCLUDE_RESTORE
  191.                         line[tend+1] = RESTOREPOINTER;
  192. #endif
  193.                         break;
  194. #endif
  195.                     default:
  196.                         break;
  197.                 }
  198.             }
  199.             line[RectRight(rc)+1] = line[tend+3] = '\0';
  200.             writeline(wnd, line+RectLeft(rc),
  201.                            RectLeft(rc)+BorderAdj(wnd),
  202.                            0,
  203.                            FALSE);
  204.         }
  205.     }
  206. }
  207.  
  208. /* --- display right border shadow character of a window --- */
  209. static void near shadow_char(WINDOW wnd, int y)
  210. {
  211.     int fg = foreground;
  212.     int bg = background;
  213.     int x = WindowWidth(wnd);
  214.     int c = videochar(GetLeft(wnd)+x, GetTop(wnd)+y);
  215.  
  216.     if (TestAttribute(wnd, SHADOW) == 0 || cfg.mono)
  217.         return;
  218.     foreground = LIGHTGRAY;
  219.     background = BLACK;
  220.     wputch(wnd, c, x, y);
  221.     foreground = fg;
  222.     background = bg;
  223. }
  224.  
  225. /* --- display the bottom border shadow line for a window -- */
  226. static void near shadowline(WINDOW wnd, RECT rc)
  227. {
  228.     int i;
  229.     int y = GetBottom(wnd)+1;
  230.     int fg = foreground;
  231.     int bg = background;
  232.  
  233.     if ((TestAttribute(wnd, SHADOW)) == 0 || cfg.mono)
  234.         return;
  235.     for (i = 0; i < WindowWidth(wnd)+1; i++)
  236.         line[i] = videochar(GetLeft(wnd)+i, y);
  237.     line[i] = '\0';
  238.     foreground = LIGHTGRAY;
  239.     background = BLACK;
  240.     line[RectRight(rc)+1] = '\0';
  241.     if (RectLeft(rc) == 0)
  242.         rc.lf++;
  243.     ClipString++;
  244.     wputs(wnd, line+RectLeft(rc), RectLeft(rc),
  245.         WindowHeight(wnd));
  246.     --ClipString;
  247.     foreground = fg;
  248.     background = bg;
  249. }
  250.  
  251. static RECT ParamRect(WINDOW wnd, RECT *rcc)
  252. {
  253.     RECT rc;
  254.     if (rcc == NULL)    {
  255.         rc = RelativeWindowRect(wnd, WindowRect(wnd));
  256.         if (TestAttribute(wnd, SHADOW))    {
  257.             rc.rt++;
  258.             rc.bt++;
  259.         }
  260.     }
  261.     else
  262.         rc = *rcc;
  263.     return rc;
  264. }
  265.  
  266. void PaintShadow(WINDOW wnd)
  267. {
  268.     int y;
  269.     RECT rc = ParamRect(wnd, NULL);
  270.     for (y = 1; y < WindowHeight(wnd); y++)
  271.         shadow_char(wnd, y);
  272.     shadowline(wnd, rc);
  273. }
  274.  
  275. /* ------- display a window's border ----- */
  276. void RepaintBorder(WINDOW wnd, RECT *rcc)
  277. {
  278.     int y;
  279.     unsigned int lin, side, ne, nw, se, sw;
  280.     RECT rc, clrc;
  281.  
  282.     if (!TestAttribute(wnd, HASBORDER))
  283.         return;
  284.     rc = ParamRect(wnd, rcc);
  285.     clrc = AdjustRectangle(wnd, rc);
  286.  
  287.     if (wnd == inFocus)    {
  288.         lin  = FOCUS_LINE;
  289.         side = FOCUS_SIDE;
  290.         ne   = FOCUS_NE;
  291.         nw   = FOCUS_NW;
  292.         se   = FOCUS_SE;
  293.         sw   = FOCUS_SW;
  294.     }
  295.     else    {
  296.         lin  = LINE;
  297.         side = SIDE;
  298.         ne   = NE;
  299.         nw   = NW;
  300.         se   = SE;
  301.         sw   = SW;
  302.     }
  303.     line[WindowWidth(wnd)] = '\0';
  304.     /* ---------- window title ------------ */
  305.     if (TestAttribute(wnd, HASTITLEBAR))
  306.         if (RectTop(rc) == 0)
  307.             if (RectLeft(rc) < WindowWidth(wnd)-BorderAdj(wnd))
  308.                 DisplayTitle(wnd, &rc);
  309.     foreground = FrameForeground(wnd);
  310.     background = FrameBackground(wnd);
  311.     /* -------- top frame corners --------- */
  312.     if (RectTop(rc) == 0)    {
  313.         if (RectLeft(rc) == 0)
  314.             wputch(wnd, nw, 0, 0);
  315.         if (RectLeft(rc) < WindowWidth(wnd))    {
  316.             if (RectRight(rc) >= WindowWidth(wnd)-1)
  317.                 wputch(wnd, ne, WindowWidth(wnd)-1, 0);
  318.             TopLine(wnd, lin, clrc);
  319.         }
  320.     }
  321.  
  322.     /* ----------- window body ------------ */
  323.     for (y = RectTop(rc); y <= RectBottom(rc); y++)    {
  324.         int ch;
  325.         if (y == 0 || y >= WindowHeight(wnd)-1)
  326.             continue;
  327.         if (RectLeft(rc) == 0)
  328.             wputch(wnd, side, 0, y);
  329.         if (RectLeft(rc) < WindowWidth(wnd) &&
  330.                 RectRight(rc) >= WindowWidth(wnd)-1)    {
  331.             if (TestAttribute(wnd, VSCROLLBAR))
  332.                 ch = (    y == 1 ? UPSCROLLBOX      :
  333.                           y == WindowHeight(wnd)-2  ?
  334.                                 DOWNSCROLLBOX       :
  335.                           y-1 == wnd->VScrollBox    ?
  336.                                 SCROLLBOXCHAR       :
  337.                           SCROLLBARCHAR );
  338.             else
  339.                 ch = side;
  340.             wputch(wnd, ch, WindowWidth(wnd)-1, y);
  341.         }
  342.         if (RectRight(rc) == WindowWidth(wnd))
  343.             shadow_char(wnd, y);
  344.     }
  345.  
  346.     if (RectTop(rc) <= WindowHeight(wnd)-1 &&
  347.             RectBottom(rc) >= WindowHeight(wnd)-1)    {
  348.         /* -------- bottom frame corners ---------- */
  349.         if (RectLeft(rc) == 0)
  350.             wputch(wnd, sw, 0, WindowHeight(wnd)-1);
  351.         if (RectLeft(rc) < WindowWidth(wnd) &&
  352.                 RectRight(rc) >= WindowWidth(wnd)-1)
  353.             wputch(wnd, se, WindowWidth(wnd)-1,
  354.                 WindowHeight(wnd)-1);
  355.  
  356.  
  357.         if (wnd->StatusBar == NULL)    {
  358.             /* ----------- bottom line ------------- */
  359.             memset(line,lin,WindowWidth(wnd)-1);
  360.             if (TestAttribute(wnd, HSCROLLBAR))    {
  361.                 line[0] = LEFTSCROLLBOX;
  362.                 line[WindowWidth(wnd)-3] = RIGHTSCROLLBOX;
  363.                 memset(line+1, SCROLLBARCHAR, WindowWidth(wnd)-4);
  364.                 line[wnd->HScrollBox] = SCROLLBOXCHAR;
  365.             }
  366.             line[WindowWidth(wnd)-2] = line[RectRight(rc)] = '\0';
  367.             if (RectLeft(rc) != RectRight(rc) ||
  368.             (RectLeft(rc) && RectLeft(rc) < WindowWidth(wnd)-1))
  369.                 writeline(wnd,
  370.                     line+(RectLeft(clrc)),
  371.                     RectLeft(clrc)+1,
  372.                     WindowHeight(wnd)-1,
  373.                     FALSE);
  374.         }
  375.         if (RectRight(rc) == WindowWidth(wnd))
  376.             shadow_char(wnd, WindowHeight(wnd)-1);
  377.     }
  378.     if (RectBottom(rc) == WindowHeight(wnd))
  379.         /* ---------- bottom shadow ------------- */
  380.         shadowline(wnd, rc);
  381. }
  382.  
  383. static void TopLine(WINDOW wnd, int lin, RECT rc)
  384. {
  385.     if (TestAttribute(wnd, HASMENUBAR))
  386.         return;
  387.     if (TestAttribute(wnd, HASTITLEBAR) && GetTitle(wnd))
  388.         return;
  389.     if (RectLeft(rc) == 0)    {
  390.         RectLeft(rc) += BorderAdj(wnd);
  391.         RectRight(rc) += BorderAdj(wnd);
  392.     }
  393.     if (RectRight(rc) < WindowWidth(wnd)-1)
  394.         RectRight(rc)++;
  395.  
  396.     if (RectLeft(rc) < RectRight(rc))    {
  397.         /* ----------- top line ------------- */
  398.         memset(line,lin,WindowWidth(wnd)-1);
  399.         if (TestAttribute(wnd, CONTROLBOX))    {
  400.             strncpy(line+1, "   ", 3);
  401.             *(line+2) = CONTROLBOXCHAR;
  402.         }
  403.         line[RectRight(rc)] = '\0';
  404.         writeline(wnd, line+RectLeft(rc),
  405.             RectLeft(rc), 0, FALSE);
  406.     }
  407. }
  408.  
  409. /* ------ clear the data space of a window -------- */
  410. void ClearWindow(WINDOW wnd, RECT *rcc, int clrchar)
  411. {
  412.     if (isVisible(wnd))    {
  413.         int y;
  414.         RECT rc;
  415.  
  416.         if (rcc == NULL)
  417.             rc = RelativeWindowRect(wnd, WindowRect(wnd));
  418.         else
  419.             rc = *rcc;
  420.  
  421.         if (RectLeft(rc) == 0)
  422.             RectLeft(rc) = BorderAdj(wnd);
  423.         if (RectRight(rc) > WindowWidth(wnd)-1)
  424.             RectRight(rc) = WindowWidth(wnd)-1;
  425.         SetStandardColor(wnd);
  426.         memset(line, clrchar, sizeof line);
  427.         line[RectRight(rc)+1] = '\0';
  428.         for (y = RectTop(rc); y <= RectBottom(rc); y++)    {
  429.             if (y < TopBorderAdj(wnd) ||
  430.                     y > ClientHeight(wnd)+
  431.                         (TestAttribute(wnd, HASMENUBAR) ? 1 : 0))
  432.                 continue;
  433.             writeline(wnd,
  434.                 line+(RectLeft(rc)),
  435.                 RectLeft(rc),
  436.                 y,
  437.                 FALSE);
  438.         }
  439.     }
  440. }
  441.  
  442. /* ------ compute the logical line length of a window ------ */
  443. int LineLength(char *ln)
  444. {
  445.     int len = strlen(ln);
  446.     char *cp = ln;
  447.     while ((cp = strchr(cp, CHANGECOLOR)) != NULL)    {
  448.         cp++;
  449.         len -= 3;
  450.     }
  451.     cp = ln;
  452.     while ((cp = strchr(cp, RESETCOLOR)) != NULL)    {
  453.         cp++;
  454.         --len;
  455.     }
  456.     return len;
  457. }
  458.  
  459. void InitWindowColors(WINDOW wnd)
  460. {
  461.     int fbg,col;
  462.     int cls = GetClass(wnd);
  463.     /* window classes without assigned colors inherit parent's colors */
  464.     if (cfg.clr[cls][0][0] == 0xff && GetParent(wnd) != NULL)
  465.         cls = GetClass(GetParent(wnd));
  466.     /* ---------- set the colors ---------- */
  467.     for (fbg = 0; fbg < 2; fbg++)
  468.         for (col = 0; col < 4; col++)
  469.             wnd->WindowColors[col][fbg] = cfg.clr[cls][col][fbg];
  470. }
  471.  
  472. void PutWindowChar(WINDOW wnd, int c, int x, int y)
  473. {
  474.     if (x < ClientWidth(wnd) && y < ClientHeight(wnd))
  475.         wputch(wnd, c, x+BorderAdj(wnd), y+TopBorderAdj(wnd));
  476. }
  477.  
  478. void PutWindowLine(WINDOW wnd, void *s, int x, int y)
  479. {
  480.     int saved = FALSE, sv;
  481.     if (x < ClientWidth(wnd) && y < ClientHeight(wnd))    {
  482.         char *en = (char *)s+ClientWidth(wnd)-x;
  483.         if (strlen(s)+x > ClientWidth(wnd))    {
  484.             sv = *en;
  485.             *en = '\0';
  486.             saved = TRUE;
  487.         }
  488.         ClipString++;
  489.         wputs(wnd, s, x+BorderAdj(wnd), y+TopBorderAdj(wnd));
  490.         --ClipString;
  491.         if (saved)
  492.             *en = sv;
  493.     }
  494. }
  495.  
  496.