home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / df3os2.zip / DFWINDOW.CPP < prev    next >
C/C++ Source or Header  |  1993-09-23  |  7KB  |  302 lines

  1. // ------------ dfwindow.cpp
  2.  
  3. #include "dflatpp.h"
  4. #include "frame.h"
  5. #include "desktop.h"
  6.  
  7. static Color col = {
  8.     WHITE,            // fg
  9.     BLACK,            // bg
  10.     WHITE,            // selected fg
  11.     BLACK,            // selected bg
  12.     WHITE,            // frame fg
  13.     BLACK,            // frame bg
  14.     WHITE,            // highlighted fg
  15.     BLACK            // highlighted bg
  16. };
  17.  
  18. // -------- common constructor initialization code
  19. void DFWindow::InitWindow(int lf, int tp,
  20.                 int ht, int wd, DFWindow *par)
  21. {
  22.     windowtype = DFlatWindow;
  23.     if (lf == -1)
  24.         lf = (desktop.screen().Width()-wd)/2;
  25.     if (tp == -1)
  26.         tp = (desktop.screen().Height()-ht)/2;
  27.     if (ht == -1)
  28.         ht = desktop.screen().Height();
  29.     if (wd == -1)
  30.         wd = desktop.screen().Width();
  31.     attrib = restored_attrib = 0;
  32.     title = 0;
  33.     ctlmenu = 0;
  34.     videosave = 0;
  35.     visible = False;
  36.     prevcapture = 0;
  37.     prevfocus = 0;
  38.     SetParent(par);
  39.     BorderAdj = TopBorderAdj = BottomBorderAdj = 0;
  40.     Rect rcc(lf, tp, lf+wd-1, tp+ht-1);
  41.     restored_rc = rect = rcc;
  42.     clearch = ' ';
  43.     DblBorder = True;
  44.     Enqueue();
  45.     windowstate = OPEN;
  46.     colors = col;
  47. }
  48.  
  49. void DFWindow::SetTitle(const char *ttl)
  50. {
  51.     SetAttribute(TITLEBAR);
  52.     delete title;
  53.     title = new String(ttl);
  54. }
  55.  
  56. void DFWindow::InitWindow(const char *ttl, int lf, int tp,
  57.             int ht, int wd, DFWindow *par)
  58. {
  59.     InitWindow(lf, tp, ht, wd, par);
  60.     SetTitle(ttl);
  61. }
  62.  
  63. void DFWindow::CloseWindow()
  64. {
  65.     windowstate = CLOSING;
  66.     Hide();
  67.     // ------- close window's children
  68.     DFWindow *Wnd = First();
  69.     while (Wnd != 0)    {
  70.         DFWindow *Nxt = Wnd->Next();
  71.         Wnd->CloseWindow();
  72.         Wnd = Nxt;
  73.     }
  74.     // ------ delete this window's memory
  75.     delete title;
  76.     title = 0;
  77.     delete videosave;
  78.     videosave = 0;
  79.     DeleteCtlMenu();
  80.     if (desktop.FocusCapture() == this)
  81.         ReleaseFocus();
  82.     else if (this == desktop.InFocus())    {
  83.         if (Parent() == 0)
  84.             desktop.SetFocus(0);
  85.         else if (Parent()->windowstate == CLOSING)    
  86.             desktop.SetFocus(Parent());
  87.         else     {
  88.                NextSiblingFocus();
  89.                if (this == desktop.InFocus())
  90.                    if (!Parent()->SetFocus())
  91.                        desktop.SetFocus(0);
  92.         }
  93.     }
  94.     Dequeue();
  95.     windowstate = CLOSED;
  96. }
  97.  
  98. // ---------- display the window
  99. void DFWindow::Show()
  100. {
  101.     if (windowstate != CLOSING)    {
  102.         if (attrib & SAVESELF)    {
  103.             Rect rc = ShadowedRect();
  104.             if (videosave == 0)    {
  105.                 int sz = rc.Height() * rc.Width() * 2;
  106.                 videosave = new char[sz];
  107.             }
  108.             if (!visible)
  109.                 desktop.screen().GetBuffer(rc, videosave);
  110.         }
  111.         visible = True;
  112.         Paint();
  113.         Border();
  114.         Shadow();
  115.         // --- show the children of this window
  116.         DFWindow *Wnd = First();
  117.         while (Wnd != 0)    {
  118.             Wnd->Show();
  119.             Wnd = Wnd->Next();
  120.         }
  121.     }
  122. }
  123.  
  124. Rect DFWindow::ShadowedRect()
  125. {
  126.     Rect rc = rect;
  127.     if (attrib & SHADOW)    {
  128.         rc.Right()++;
  129.         rc.Bottom()++;
  130.     }
  131.     return rc;
  132. }
  133.  
  134. void DFWindow::Hide()
  135. {
  136.     if (visible)    {
  137.         visible = False;
  138.         // ----- hide the children
  139.         DFWindow *Wnd = First();
  140.         while (Wnd != 0)    {
  141.             Wnd->Hide();
  142.             Wnd = Wnd->Next();
  143.         }
  144.         Rect rc = ShadowedRect();
  145.         if (videosave != 0)    {
  146.             desktop.screen().PutBuffer(rc, videosave);
  147.             delete videosave;
  148.             videosave = 0;
  149.         }
  150.     }
  151. }
  152.  
  153. Bool DFWindow::isDescendedFrom(DFWindow *ancestor)
  154. {
  155.     DFWindow *wnd = this;
  156.     while (wnd != 0)    {
  157.         if (wnd == ancestor)
  158.             return True;
  159.         wnd = wnd->Parent();
  160.     }
  161.     return False;
  162. }
  163.  
  164. void DFWindow::Keyboard(int key)
  165. {
  166.     switch (key)    {
  167.         case F1:
  168.             HelpFunction();
  169.             return;
  170.         case ALT_F6:
  171.             NextSiblingFocus();
  172.             return;
  173.         case ' ':
  174.             if ((desktop.keyboard().GetShift() & ALTKEY) == 0)
  175.                 break;
  176.             // --- fall through
  177.         case ALT_HYPHEN:
  178.             if (attrib & CONTROLBOX)    {
  179.                 OpenCtlMenu();
  180.                 return;
  181.             }
  182.             break;
  183.         case CTRL_F4:
  184.             if (Parent() && (attrib & CONTROLBOX))    {
  185.                 CloseWindow();
  186.                 return;
  187.             }
  188.             break;
  189.         default:
  190.             break;
  191.     }
  192.     // --- send all unprocessed keystrokes 
  193.     //     to the parent window
  194.     if (Parent() != 0)
  195.         Parent()->Keyboard(key);
  196. }
  197.  
  198. void DFWindow::HelpFunction()
  199. {
  200.     if (Parent() != 0)
  201.         Parent()->HelpFunction();
  202. }
  203.  
  204. void DFWindow::ShiftChanged(int sk)
  205. {
  206.     if (Parent() != 0)
  207.         Parent()->ShiftChanged(sk);
  208. }
  209.  
  210. void DFWindow::DoubleClick(int mx, int my)
  211. {
  212.     if (HitControlBox(mx, my))
  213.         CloseWindow();
  214. }
  215.  
  216. void DFWindow::LeftButton(int mx, int my)
  217. {
  218.     if (my == Top())    {
  219.         // ----- hit the top border
  220.         if (HitControlBox(mx, my) && (attrib & CONTROLBOX))
  221.             // ------- hit the control box
  222.             OpenCtlMenu();
  223.         else if (attrib & MOVEABLE)
  224.             // ---- move the window
  225.             desktop.frame().OpenFrame(this, True, mx);
  226.     }
  227.     else if ((attrib & SIZEABLE) && windowstate == OPEN)
  228.         if (mx == Right() && my == Bottom())
  229.             // --- hit the lower right corner, size the window
  230.             desktop.frame().OpenFrame(this, False, 0);
  231.     prevmouseline = my;
  232.     prevmousecol = mx;
  233. }
  234.  
  235. void DFWindow::ButtonReleased(int, int)
  236. {
  237.     prevmouseline = -1;
  238.     prevmousecol = -1;
  239. }
  240.  
  241. Rect DFWindow::ClientRect()
  242. {
  243.     Rect rc(ClientLeft(), ClientTop(),
  244.         ClientRight(), ClientBottom());
  245.     return rc;
  246. }
  247.  
  248. void DFWindow::ChangePosition(int x, int y)
  249. {
  250.     int ht = Height();
  251.     int wd = Width();
  252.     rect.Left() = x;
  253.     rect.Top() = y;
  254.     rect.Right() = Left()+wd-1;
  255.     rect.Bottom() = Top()+ht-1;
  256.     restored_rc = rect;
  257. }
  258.  
  259. // ------------ move a window
  260. void DFWindow::Move(int x, int y)
  261. {
  262.     int xdif = x - Left();
  263.     int ydif = y - Top();
  264.     if (xdif == 0 && ydif == 0)
  265.         return;
  266.     Bool wasVisible = visible;
  267.     if (wasVisible)
  268.         Hide();
  269.     ChangePosition(x, y);
  270.     DFWindow *Wnd = First();
  271.     while (Wnd != 0)    {
  272.         Wnd->Move(Wnd->Left()+xdif, Wnd->Top()+ydif);
  273.         Wnd = Wnd->Next();
  274.     }
  275.     if (wasVisible)
  276.         Show();
  277. }
  278.  
  279. // ------------ size a window
  280. void DFWindow::Size(int x, int y)
  281. {
  282.     int xdif = x - Right();
  283.     int ydif = y - Bottom();
  284.     if (xdif == 0 && ydif == 0)
  285.         return;
  286.     Bool wasVisible = visible;
  287.     if (wasVisible)
  288.         Hide();
  289.     rect.Right() = x;
  290.     rect.Bottom() = y;
  291.     restored_rc = rect;
  292.  
  293.     DFWindow *Wnd = First();
  294.     while (Wnd != 0)    {
  295.         Wnd->ParentSized(xdif, ydif);
  296.         Wnd = Wnd->Next();
  297.     }
  298.     if (wasVisible)
  299.         Show();
  300. }
  301.  
  302.