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

  1. // ------------- menubar.cpp
  2.  
  3. #include <ctype.h>
  4. #include "desktop.h"
  5. #include "menubar.h"
  6. #include "menusel.h"
  7.  
  8. // -------- construct a menubar item
  9. MenuBarItem::MenuBarItem(const char *Title, MenuSelection **Ms,
  10.                          void (*MenuPrep)())
  11. {
  12.     if (Title != 0)
  13.         title = new String(Title);
  14.     else
  15.         title = 0;
  16.     ms = Ms;
  17.     menuprep = MenuPrep;
  18.     popdown = 0;
  19. }
  20.  
  21. static Color col = {
  22.     BLACK,                // fg
  23.     LIGHTGRAY,            // bg
  24.     BLACK,                // selected fg
  25.     CYAN,                // selected bg
  26.     BLACK,                // frame fg
  27.     LIGHTGRAY,            // frame bg
  28.     BLACK,                // highlighted fg
  29.     LIGHTGRAY            // highlighted bg
  30. };
  31.  
  32. // -------- construct a menubar
  33. MenuBar::MenuBar( MenuBarItem *MenuItems, DFWindow *par) :
  34.             TextBox( par->ClientLeft(), par->ClientTop(),
  35.                         1, par->ClientWidth(), par)
  36. {
  37.     windowtype = MenubarWindow;
  38.     menuitems = MenuItems;
  39.     selection = -1;
  40.     ispoppeddown = False;
  41.     MenuBarItem *menu = menuitems;
  42.     menucount = 0;
  43.     oldfocus = 0;
  44.     int off = 2;
  45.     SetTextLength(desktop.screen().Width()*2);
  46.     colors = col;
  47.     shortcutfg = RED;
  48.     while (menu->title != 0)    {
  49.         int len = menu->title->Strlen()-1;
  50.         menu->x1 = off;
  51.         menu->x2 = off+len-1;
  52.         off += len+2;
  53.         String ttl("  ");
  54.         ttl += *(menu->title);
  55.         AddText(ttl);
  56.         int n = text->Strlen()-1;
  57.         (*text)[n] = '\0';
  58.         menu->popdown = new PopDown(this, menu->ms);
  59.         menu++;
  60.         menucount++;
  61.     }
  62.     par->SetAttribute(MENUBAR);
  63. }
  64.  
  65. // -------- menubar destructor
  66. MenuBar::~MenuBar()
  67. {
  68.     MenuBarItem *menu = menuitems;
  69.     while (menu->title != 0)    {
  70.         delete menu->popdown;
  71.         menu++;
  72.     }
  73.     TextBox::CloseWindow();
  74. }
  75.  
  76. // ---- menubar gets the focus
  77. Bool MenuBar::SetFocus()
  78. {
  79.     if (oldfocus == 0)
  80.         if (desktop.InFocus() != 0)
  81.             if (desktop.InFocus()->State() != CLOSING)
  82.                 oldfocus = desktop.InFocus();
  83.     return TextBox::SetFocus();
  84. }
  85.  
  86. // ---- menubar loses the focus
  87. void MenuBar::ResetFocus()
  88. {
  89.     if (!ispoppeddown)    {
  90.         SetSelection(-1);
  91.         oldfocus = 0;
  92.     }
  93.     TextBox::ResetFocus();
  94. }
  95.  
  96. // -------- paint the menubar
  97. void MenuBar::Paint()
  98. {
  99.     WriteShortcutLine(0, colors.fg, colors.bg);
  100.     if (selection != -1)    {
  101.         int x = menuitems[selection].x1;
  102.         int len = menuitems[selection].x2-x+2;
  103.         String sel = text->mid(len, x+selection);
  104.         DisplayShortcutField(sel,x,0,colors.sfg,colors.sbg);
  105.     }
  106. }
  107.  
  108. // ------- left mouse button is pressed
  109. void MenuBar::LeftButton(int mx, int)
  110. {
  111.     mx -= Left();
  112.     MenuBarItem *menu = menuitems;
  113.     int sel = 0;
  114.     while (menu->title != 0)    {
  115.         if (mx >= menu->x1 && mx <= menu->x2)    {
  116.             if (selection != sel || !ispoppeddown)    {
  117.                 if (ispoppeddown)    {
  118.                     PopDown *pd = menuitems[selection].popdown;
  119.                     if (pd->isOpen())
  120.                         pd->CloseMenu(False);
  121.                 }
  122.                 Select(sel);
  123.             }
  124.             return;
  125.         }
  126.         sel++;
  127.         menu++;
  128.     }
  129.     if (selection == -1)
  130.         SetSelection(0);
  131. }
  132.  
  133. void MenuBar::SetSelection(int sel)
  134. {
  135.     selection = sel;
  136.     Paint();
  137. }
  138.  
  139. // ----- programmed selection
  140. void MenuBar::Select(int sel)    {
  141.     selection = sel;
  142.     Select();
  143. }
  144.  
  145. // ------- user selection
  146. void MenuBar::Select()
  147. {
  148.     Paint();
  149.     ispoppeddown = True;
  150.     MenuBarItem &mb = *(menuitems+selection);
  151.     int lf = Left() + mb.x1;
  152.     int tp = Top()+1;
  153.     if (mb.menuprep != 0)
  154.         (*mb.menuprep)();
  155.     mb.popdown->OpenMenu(lf, tp);
  156. }
  157.  
  158. // ------ test for popdown accelerator key
  159. Bool MenuBar::AcceleratorKey(int key)
  160. {
  161.     MenuBarItem *menu = menuitems;
  162.     while (menu->title != 0)    {
  163.         PopDown *pd = menu->popdown;
  164.         if (pd->AcceleratorKey(key))
  165.             return True;
  166.         menu++;
  167.     }
  168.     return False;
  169. }
  170.  
  171. // ------ test for menubar shortcut key
  172. Bool MenuBar::ShortcutKey(int key)
  173. {
  174.     int altkey = desktop.keyboard().AltConvert(key);
  175.     MenuBarItem *menu = menuitems;
  176.     int sel = 0;
  177.     while (menu->title != 0)    {
  178.         int off = menu->title->FindChar(SHORTCUTCHAR);
  179.         if (off != -1)    {
  180.             String &cp = *(menu->title);
  181.             int c = cp[off+1];
  182.             if (tolower(c) == altkey)    {
  183.                 SetFocus();
  184.                 Select(sel);
  185.                 return True;
  186.             }
  187.         }
  188.         sel++;
  189.         menu++;
  190.     }
  191.     return False;
  192. }
  193.  
  194. // -------- keystroke while menubar has the focus
  195. void MenuBar::Keyboard(int key)
  196. {
  197.     if (AcceleratorKey(key))
  198.         return;
  199.     if (!ispoppeddown && ShortcutKey(key))
  200.         return;
  201.     switch (key)    {
  202.         case F10:
  203.             if (ispoppeddown)
  204.                 break;
  205.             if (this != desktop.InFocus())    {
  206.                 if (selection == -1)
  207.                     selection = 0;
  208.                 SetFocus();
  209.                 break;
  210.             }
  211.             // ------ fall through
  212.         case ESC:
  213.             ispoppeddown = False;
  214.             SetSelection(-1);
  215.             if (oldfocus != 0)
  216.                 oldfocus->SetFocus();
  217.             else
  218.                 Parent()->SetFocus();
  219.             break;
  220.         case FWD:
  221.             selection++;
  222.             if (selection == menucount)
  223.                 selection = 0;
  224.             if (ispoppeddown)
  225.                 Select();
  226.             else
  227.                 Paint();
  228.             break;
  229.         case BS:
  230.             if (selection == 0)
  231.                 selection = menucount;
  232.             --selection;
  233.             if (ispoppeddown)
  234.                 Select();
  235.             else
  236.                 Paint();
  237.             break;
  238.         case '\r':
  239.             if (selection != -1)
  240.                 Select();
  241.             break;
  242.         case ALT_F6:
  243.             TextBox::Keyboard(key);
  244.             break;
  245.         default:
  246.             break;
  247.     }
  248. }
  249.  
  250. // ---- resize the menubar when the application window resizes
  251. void MenuBar::ParentSized(int xdif, int)
  252. {
  253.     Size(Right()+xdif, Bottom());
  254. }
  255.  
  256.  
  257.