home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 06 / dflat3 / applicat.c next >
Text File  |  1991-05-19  |  14KB  |  567 lines

  1. /* ------------- applicat.c ------------- */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <dos.h>
  7. #include "dflat.h"
  8.  
  9. static WINDOW mnu = NULLWND;
  10. static int ScreenHeight;
  11.  
  12. extern DBOX TabStops;
  13. extern DBOX DisplayCGA;
  14. extern DBOX DisplayEGA;
  15. extern DBOX DisplayVGA;
  16. extern DBOX Windows;
  17. extern DBOX Log;
  18.  
  19. static DBOX *Display;
  20.  
  21. static void CreateMenu(WINDOW);
  22. static void ChooseWindow(WINDOW, int);
  23. static void CloseAll(WINDOW);
  24. static void SelectColors(void);
  25. static void SetScreenHeight(WINDOW, int);
  26. #ifdef INCLUDE_MULTIDOCS
  27. static void SelectTexture(void);
  28. static void SelectBorder(WINDOW);
  29. static void SelectTitle(WINDOW);
  30. #endif
  31. #ifdef INCLUDE_DIALOG_BOXES
  32. static void SelectLines(WINDOW);
  33. #endif
  34.  
  35. /* ----- compute the Y coordinate for the menu bar ---- */
  36. #define MenuY(wnd)    ((TestAttribute(wnd, HASBORDER) &&         \
  37.                     !TestAttribute(wnd, TITLEBAR)) ? -1 : 0)
  38.  
  39. int ApplicationProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  40. {
  41.     int rtn;
  42.  
  43.     switch (msg)    {
  44.         case CREATE_WINDOW:
  45.             ScreenHeight = SCREENHEIGHT;
  46.             if (!LoadConfig())
  47.                 cfg.ScreenLines = ScreenHeight;
  48.  
  49.             if (isVGA())
  50.                 Display = &DisplayVGA;
  51.             else if (isEGA())
  52.                 Display = &DisplayEGA;
  53.             else 
  54.                 Display = &DisplayCGA;
  55.  
  56.             if (cfg.InsertMode)
  57.                 SetCommandToggle(MainMenu, ID_INSERT);
  58.             if (cfg.WordWrap)
  59.                 SetCommandToggle(MainMenu, ID_WRAP);
  60. #ifdef INCLUDE_DIALOG_BOXES
  61.             if (cfg.Border)
  62.                 SetCheckBox(Display, ID_BORDER);
  63.             if (cfg.Title)
  64.                 SetCheckBox(Display, ID_TITLE);
  65.             if (cfg.Texture)
  66.                 SetCheckBox(Display, ID_TEXTURE);
  67.             if (cfg.mono == 1)
  68.                 PushRadioButton(Display, ID_MONO);
  69.             else if (cfg.mono == 2)
  70.                 PushRadioButton(Display, ID_REVERSE);
  71.             else
  72.                 PushRadioButton(Display, ID_COLOR);
  73.             if (cfg.ScreenLines == 25)
  74.                 PushRadioButton(Display, ID_25LINES);
  75.             else if (cfg.ScreenLines == 43)
  76.                 PushRadioButton(Display, ID_43LINES);
  77.             else if (cfg.ScreenLines == 50)
  78.                 PushRadioButton(Display, ID_50LINES);
  79. #endif
  80.             if (SCREENHEIGHT != cfg.ScreenLines)
  81.                 SetScreenHeight(wnd, cfg.ScreenLines);
  82.             SelectColors();
  83. #ifdef INCLUDE_MULTIDOCS
  84.             SelectBorder(wnd);
  85.             SelectTitle(wnd);
  86. #endif
  87.             rtn = BaseWndProc(APPLICATION, wnd, msg, p1, p2);
  88.             if (wnd->extension != NULL)    {
  89.                 CreateMenu(wnd);
  90.                 SendMessage(wnd, SHOW_WINDOW, 0, 0);
  91.             }
  92. #ifdef INCLUDE_CLOCK
  93.             if (TestAttribute(wnd, TITLEBAR))
  94.                 PostMessage(wnd, CAPTURE_CLOCK, 0, 0);
  95. #endif
  96.             return rtn;
  97.         case SIZE:
  98.             BaseWndProc(APPLICATION, wnd, msg, p1, p2);
  99.             SendMessage(mnu, HIDE_WINDOW, 0, 0);
  100.             SendMessage(mnu, SIZE, GetRight(wnd)-1, GetTop(wnd)+1);
  101.             SendMessage(mnu, BUILDMENU, LPARAM(wnd->extension), 0);
  102.             SendMessage(mnu, SHOW_WINDOW, 0, 0);
  103.             return TRUE;
  104.         case KEYBOARD:
  105. #ifdef INCLUDE_SYSTEM_MENUS
  106.             if (WindowSizing || WindowMoving)
  107.                 break;
  108. #endif
  109.             PostMessage(mnu, msg, p1, p2);
  110.             return TRUE;
  111.         case SETFOCUS:
  112.             return FALSE;
  113.         case PAINT:
  114.             if (isVisible(wnd))
  115.                 ClearWindow(wnd, (RECT *)p1, cfg.Texture ?
  116.                     APPLCHAR : ' ');
  117.             return TRUE;
  118.         case COMMAND:
  119.             switch ((int)p1)    {
  120. #ifdef INCLUDE_LOGGING
  121.                 case ID_LOG:    {
  122.                     MessageLog(wnd);
  123.                     if (CheckBoxSetting(&Log, ID_LOGGING))
  124.                         SetCommandToggle(MainMenu, ID_LOG);
  125.                     else
  126.                         ClearCommandToggle(MainMenu, ID_LOG);
  127.                     break;
  128.                 }
  129. #endif
  130.                 case ID_EXIT:
  131.                 case ID_SYSCLOSE:
  132.                     PostMessage(wnd, CLOSE_WINDOW, 0, 0);
  133.                     break;
  134. #ifdef INCLUDE_DIALOG_BOXES
  135.                 case ID_DISPLAY:
  136.                     if (DialogBox(wnd, Display, NULL))    {
  137.                         SendMessage(wnd, HIDE_WINDOW, 0, 0);
  138.                         SelectColors();
  139.                         SelectLines(wnd);
  140. #ifdef INCLUDE_MULTIDOCS
  141.                         SelectBorder(wnd);
  142.                         SelectTitle(wnd);
  143.                         SelectTexture();
  144. #endif
  145.                         SendMessage(mnu, CLOSE_WINDOW, 0, 0);
  146.                         ClearAttribute(wnd, HASMENUBAR);
  147.                         CreateMenu(wnd);
  148. #ifdef INCLUDE_CLOCK
  149.                         if (TestAttribute(wnd, TITLEBAR))
  150.                             SendMessage(wnd, CAPTURE_CLOCK, 0, 0);
  151.                         else
  152.                             SendMessage(NULLWND, RELEASE_CLOCK, 0, 0);
  153. #endif
  154.                         PostMessage(wnd, SHOW_WINDOW, 0, 0);
  155.                     }
  156.                     break;
  157.                 case ID_TABS:
  158.                     switch (cfg.Tabs)    {
  159.                         case 2:
  160.                             PushRadioButton(&TabStops, ID_TAB2);
  161.                             break;
  162.                         case 4:
  163.                             PushRadioButton(&TabStops, ID_TAB4);
  164.                             break;
  165.                         case 6:
  166.                             PushRadioButton(&TabStops, ID_TAB6);
  167.                             break;
  168.                         case 8:
  169.                             PushRadioButton(&TabStops, ID_TAB8);
  170.                             break;
  171.                         default:
  172.                             break;
  173.                     }
  174.                     if (DialogBox(wnd, &TabStops, NULL))    {
  175.                         if (RadioButtonSetting(&TabStops, ID_TAB2))
  176.                             cfg.Tabs = 2;
  177.                         if (RadioButtonSetting(&TabStops, ID_TAB4))
  178.                             cfg.Tabs = 4;
  179.                         if (RadioButtonSetting(&TabStops, ID_TAB6))
  180.                             cfg.Tabs = 6;                    
  181.                         if (RadioButtonSetting(&TabStops, ID_TAB8))
  182.                             cfg.Tabs = 8;
  183.                     }
  184.                     break;
  185. #endif
  186.                 case ID_SAVEOPTIONS:
  187.                     SaveConfig();
  188.                     break;
  189.                 case ID_WINDOW:
  190.                     ChooseWindow(wnd, (int)p2-2);
  191.                     break;
  192.                 case ID_CLOSEALL:
  193.                     CloseAll(wnd);
  194.                     break;
  195.                 case ID_SYSRESTORE:
  196.                 case ID_SYSMOVE:
  197.                 case ID_SYSSIZE:
  198.                 case ID_SYSMINIMIZE:
  199.                 case ID_SYSMAXIMIZE:
  200.                     return BaseWndProc(APPLICATION, wnd, msg, p1, p2);
  201.                 default:
  202.                     if (inFocus != mnu)
  203.                         PostMessage(inFocus, msg, p1, p2);
  204.                     break;
  205.             }
  206.             return TRUE;
  207. #ifdef INCLUDE_SYSTEM_MENUS
  208.         case LEFT_BUTTON:    {
  209.             WINDOW wnd1 = wnd;
  210.             int mx, my;
  211.             if (WindowSizing || WindowMoving)
  212.                 return FALSE;
  213.             if (SendMessage(wnd, INSIDE_WINDOW, p1, p2))    {
  214.                 if (inFocus && inFocus != mnu)
  215.                     if (SendMessage(inFocus, INSIDE_WINDOW, p1, p2))
  216.                         wnd1 = inFocus;
  217.                 mx = (int) p1 - GetLeft(wnd1);
  218.                 my = (int) p2 - GetTop(wnd1);
  219.                 if (HitControlBox(wnd1, mx, my))    {
  220.                     BuildSystemMenu(wnd1);
  221.                     return TRUE;
  222.                 }
  223.             }
  224.             break;
  225.         }
  226. #endif
  227. #ifdef INCLUDE_CLOCK
  228.         case CLOCKTICK:    {
  229.             WINDOW wnd1 = Focus.LastWindow;
  230.             int x = GetLeft(wnd)+WindowWidth(wnd)-9;
  231.             while (wnd1 != NULLWND && wnd1 != wnd)    {
  232.                 if (SendMessage(wnd1, INSIDE_WINDOW, x, GetTop(wnd)))
  233.                     return TRUE;
  234.                 if (SendMessage(wnd1, INSIDE_WINDOW, x+5, GetTop(wnd)))
  235.                     return TRUE;
  236.                 wnd1 = PrevWindow(wnd1);
  237.             }
  238.             foreground = cfg.clr.TitleFG;
  239.             background = cfg.clr.TitleBG;
  240.             wputs(wnd, (char *)p1, WindowWidth(wnd)-9, 0);
  241.             return TRUE;
  242.         }
  243. #endif
  244.         case CLOSE_WINDOW:
  245.             CloseAll(wnd);
  246. #ifdef INCLUDE_CLOCK
  247.             SendMessage(NULLWND, RELEASE_CLOCK, 0, 0);
  248. #endif
  249.             PostMessage(NULLWND, STOP, 0, 0);
  250.             rtn = BaseWndProc(APPLICATION, wnd, msg, p1, p2);
  251.             if (ScreenHeight != SCREENHEIGHT)
  252.                 SetScreenHeight(wnd, ScreenHeight);
  253.             return rtn;
  254.         default:
  255.             break;
  256.     }
  257.     return BaseWndProc(APPLICATION, wnd, msg, p1, p2);
  258. }
  259.  
  260. static void CreateMenu(WINDOW wnd)
  261. {
  262.     mnu = CreateWindow(MENUBAR,
  263.                         NULL,
  264.                         GetClientLeft(wnd),
  265.                         GetClientTop(wnd)+MenuY(wnd),
  266.                         1,
  267.                         ClientWidth(wnd),
  268.                         NULL,
  269.                         wnd,
  270.                         NULL,
  271.                         0);
  272.     AddAttribute(wnd, HASMENUBAR);
  273.     SendMessage(mnu, BUILDMENU, LPARAM(wnd->extension), 0);
  274.     SendMessage(mnu, SETFOCUS, TRUE, TRUE);
  275. }
  276.  
  277. void PrepEditMenu(void *wnd, struct Menu *mnu)
  278. {
  279.     struct PopDown *pd = mnu->Selections;
  280.     while (pd->SelectionTitle != NULL)    {
  281.         if (*pd->SelectionTitle != LINE)    {
  282.             switch (pd->ActionId)    {
  283.                 case ID_CUT:
  284.                 case ID_COPY:
  285.                 case ID_CLEAR:
  286.                 case ID_DELETETEXT:
  287.                     if (GetClass(inFocus) == EDITBOX &&
  288.                         isMultiLine(inFocus) &&
  289.                             BlockMarked(inFocus))
  290.                         pd->Attrib &= ~INACTIVE;
  291.                     else
  292.                         pd->Attrib |= INACTIVE;
  293.                     break;
  294.                 case ID_PARAGRAPH:
  295.                     if (GetClass(inFocus) == EDITBOX &&
  296.                             isMultiLine(inFocus))
  297.                         pd->Attrib &= ~INACTIVE;
  298.                     else
  299.                         pd->Attrib |= INACTIVE;
  300.                     break;
  301.                 case ID_PASTE:
  302.                     if (GetClass(inFocus) == EDITBOX &&
  303.                             isMultiLine(inFocus) &&
  304.                                 !TestAttribute(inFocus, READONLY) &&
  305.                                     Clipboard != NULL)
  306.                         pd->Attrib &= ~INACTIVE;
  307.                     else
  308.                         pd->Attrib |= INACTIVE;
  309.                     break;
  310.                 case ID_UNDO:
  311.                     if (GetClass(inFocus) == EDITBOX &&
  312.                             inFocus->DeletedText != NULL)
  313.                         pd->Attrib &= ~INACTIVE;
  314.                     else
  315.                         pd->Attrib |= INACTIVE;
  316.                     break;
  317.                 default:
  318.                     break;
  319.             }
  320.         }
  321.         pd++;
  322.     }
  323. }
  324.  
  325. static char *Menus[9] = {
  326.     "~1.                      ",
  327.     "~2.                      ",
  328.     "~3.                      ",
  329.     "~4.                      ",
  330.     "~5.                      ",
  331.     "~6.                      ",
  332.     "~7.                      ",
  333.     "~8.                      ",
  334.     "~9.                      "
  335. };
  336.  
  337. #ifdef INCLUDE_MULTIDOCS
  338.  
  339. static WINDOW oldFocus;
  340. static int WindowSel;
  341.  
  342. void PrepWindowMenu(void *wnd, struct Menu *mnu)
  343. {
  344.     struct PopDown *p0 = mnu->Selections;
  345.     struct PopDown *pd = mnu->Selections + 2;
  346.     struct PopDown *ca = mnu->Selections + 13;
  347.     int MenuNo = 0;
  348.     WINDOW wnd1 = Built.FirstWindow;
  349.     mnu->Selection = 0;
  350.     oldFocus = inFocus;
  351.     while (wnd1 != NULLWND && MenuNo < 9)    {
  352.         if (GetClass(wnd1) != MENUBAR && GetParent(wnd1) == wnd)    {
  353.             strncpy(Menus[MenuNo]+4, GetTitle(wnd1), 20);
  354.             pd->SelectionTitle = Menus[MenuNo];
  355.             if (wnd1 == inFocus)    {
  356.                 pd->Attrib |= CHECKED;
  357.                 mnu->Selection = MenuNo+2;
  358.             }
  359.             else
  360.                 pd->Attrib &= ~CHECKED;
  361.             pd++;
  362.             MenuNo++;
  363.         }
  364.         wnd1 = NextWindowBuilt(wnd1);
  365.     }
  366.     if (MenuNo)
  367.         p0->SelectionTitle = "~Close all";
  368.     else
  369.         p0->SelectionTitle = NULL;
  370.     if (wnd1 != NULLWND)    {
  371.         *pd++ = *ca;
  372.         if (mnu->Selection == 0)
  373.             mnu->Selection = 11;
  374.     }
  375.     pd->SelectionTitle = NULL;
  376. }
  377. #endif
  378.  
  379. #ifdef INCLUDE_DIALOG_BOXES
  380. static int WindowPrep(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  381. {
  382.     switch (msg)    {
  383.         case INITIATE_DIALOG:    {
  384.             WINDOW wnd1 = Built.FirstWindow;
  385.             WINDOW cwnd = ControlWindow(&Windows, ID_WINDOWLIST);
  386.             int sel = 0;
  387.             if (cwnd == NULLWND)
  388.                 return FALSE;
  389.             while (wnd1 != NULLWND)    {
  390.                 if (GetClass(wnd1) != MENUBAR &&
  391.                         GetParent(wnd1) == GetParent(wnd) &&
  392.                             wnd1 != wnd)    {
  393.                     if (wnd1 == oldFocus)
  394.                         WindowSel = sel;
  395.                     SendMessage(cwnd, ADDTEXT, LPARAM(GetTitle(wnd1)), 0);
  396.                     sel++;
  397.                 }
  398.                 wnd1 = NextWindowBuilt(wnd1);
  399.             }
  400.             SendMessage(cwnd, LB_SETSELECTION, WindowSel, 0);
  401. #ifdef INCLUDE_SCROLLBARS
  402.             AddAttribute(cwnd, VSCROLLBAR);
  403. #endif
  404.             PostMessage(cwnd, SHOW_WINDOW, 0, 0);
  405.             break;
  406.         }
  407.         case COMMAND:
  408.             switch ((int) p1)    {
  409.                 case ID_OK:
  410.                     WindowSel = SendMessage(ControlWindow(&Windows,
  411.                                         ID_WINDOWLIST),
  412.                                             LB_CURRENTSELECTION, 0, 0);
  413.                     break;
  414.                 case ID_WINDOWLIST:
  415.                     if ((int) p2 == LB_CHOOSE)
  416.                         SendMessage(wnd, COMMAND, ID_OK, 0);
  417.                     break;
  418.                 default:
  419.                     break;
  420.             }
  421.             break;
  422.         default:
  423.             break;
  424.     }
  425.     return DefaultWndProc(wnd, msg, p1, p2);
  426. }
  427. #endif
  428.  
  429. static void ChooseWindow(WINDOW wnd, int WindowNo)
  430. {
  431.     WINDOW wnd1 = GetFirstChild(wnd);
  432.     if (WindowNo == 9)    {
  433. #ifdef INCLUDE_DIALOG_BOXES
  434.         if (DialogBox(wnd, &Windows, WindowPrep))
  435.             WindowNo = WindowSel;
  436.         else
  437. #endif
  438.             return;
  439.     }
  440.     while (wnd1 != NULLWND)    {
  441.         if (GetClass(wnd1) != MENUBAR)
  442.             if (WindowNo-- == 0)
  443.                 break;
  444.         wnd1 = GetNextChild(wnd);
  445.     }
  446.     SendMessage(wnd1, SETFOCUS, TRUE, 0);
  447.     if (wnd1->condition == ISMINIMIZED)
  448.         SendMessage(wnd1, RESTORE, 0, 0);
  449. }
  450.  
  451.  
  452. static void SelectColors(void)
  453. {
  454. #ifdef INCLUDE_DIALOG_BOXES
  455.     if (RadioButtonSetting(Display, ID_MONO))
  456.         cfg.mono = 1;
  457.     else if (RadioButtonSetting(Display, ID_REVERSE))
  458.         cfg.mono = 2;
  459.     else
  460.         cfg.mono = 0;
  461. #endif
  462.     if ((ismono() || video_mode == 2) && cfg.mono == 0)
  463.         cfg.mono = 1;
  464.  
  465.     if (cfg.mono == 1)
  466.         cfg.clr = bw;
  467.     else if (cfg.mono == 2)
  468.         cfg.clr = reverse;
  469.     else
  470.         cfg.clr = color;
  471. }
  472.  
  473. #ifdef INCLUDE_DIALOG_BOXES
  474. static void SelectLines(WINDOW wnd)
  475. {
  476.     cfg.ScreenLines = 25;
  477.     if (isEGA() || isVGA())    {
  478.         if (RadioButtonSetting(Display, ID_43LINES))
  479.             cfg.ScreenLines = 43;
  480.         else if (RadioButtonSetting(Display, ID_50LINES))
  481.             cfg.ScreenLines = 50;
  482.     }
  483.     if (SCREENHEIGHT != cfg.ScreenLines)
  484.         SetScreenHeight(wnd, cfg.ScreenLines);
  485. }
  486. #endif
  487.  
  488. static void SetScreenHeight(WINDOW wnd, int height)
  489. {
  490.     if (isEGA() || isVGA())    {
  491.         int FullScreen = WindowHeight(wnd) == SCREENHEIGHT;
  492.         SendMessage(NULLWND, SAVE_CURSOR, 0, 0);
  493.         switch (height)    {
  494.             case 25:
  495.                 Set25();
  496.                 break;
  497.             case 43:
  498.                 Set43();
  499.                 break;
  500.             case 50:
  501.                 Set50();
  502.                 break;
  503.             default:
  504.                 break;
  505.         }
  506.         SendMessage(NULLWND, RESTORE_CURSOR, 0, 0);
  507.         resetmouse();
  508.         SendMessage(NULLWND, SHOW_MOUSE, 0, 0);
  509.         if (FullScreen || SCREENHEIGHT-1 < GetBottom(wnd))
  510.             PostMessage(wnd, SIZE, LPARAM(GetRight(wnd)),
  511.                 SCREENHEIGHT-1);
  512.     }
  513. }
  514.  
  515. #ifdef INCLUDE_MULTIDOCS
  516.  
  517.  
  518. static void SelectTexture(void)
  519. {
  520. #ifdef INCLUDE_DIALOG_BOXES
  521.     cfg.Texture = CheckBoxSetting(Display, ID_TEXTURE);
  522. #endif
  523. }
  524.  
  525. static void SelectBorder(WINDOW wnd)
  526. {
  527. #ifdef INCLUDE_DIALOG_BOXES
  528.     cfg.Border = CheckBoxSetting(Display, ID_BORDER);
  529. #endif
  530.     if (cfg.Border)
  531.         AddAttribute(wnd, HASBORDER);
  532.     else
  533.         ClearAttribute(wnd, HASBORDER);
  534. }
  535.  
  536. static void SelectTitle(WINDOW wnd)
  537. {
  538. #ifdef INCLUDE_DIALOG_BOXES
  539.     cfg.Title = CheckBoxSetting(Display, ID_TITLE);
  540. #endif
  541.     if (cfg.Title)
  542.         AddAttribute(wnd, TITLEBAR);
  543.     else
  544.         ClearAttribute(wnd, TITLEBAR);
  545. }
  546. #endif
  547.  
  548. static void CloseAll(WINDOW wnd)
  549. {
  550.     int closing = TRUE;
  551.     ClearAttribute(wnd, VISIBLE);
  552.     while (closing)    {
  553.         WINDOW wnd1 = GetLastChild(wnd);
  554.         closing = FALSE;
  555.         while (wnd1 != NULLWND)    {
  556.             if (GetClass(wnd1) != MENUBAR && wnd1 == inFocus)    {
  557.                 SendMessage(wnd1, CLOSE_WINDOW, 0, 0);
  558.                 closing = TRUE;
  559.                 break;
  560.             }
  561.             wnd1 = GetPrevChild(wnd);
  562.         }
  563.     }
  564.     AddAttribute(wnd, VISIBLE);
  565.     SendMessage(wnd, PAINT, 0, 0);
  566. }
  567.