home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / winsrc.zip / MATHTOOL.C < prev    next >
C/C++ Source or Header  |  1990-12-21  |  6KB  |  213 lines

  1. #include "windows.h"
  2. #include "winfract.h"
  3. #include "mathtool.h"
  4. #include "math.h"
  5.  
  6. static char MTClassName[] =     "FFWMathTools";
  7. static char MTWindowTitle[] =   "Math Tools";
  8. HWND hFractalWnd, hMathToolsWnd, hCoordBox;
  9. HANDLE hThisInst;
  10. long FAR PASCAL MTWndProc(HWND, WORD, WORD, DWORD);
  11. int MTWindowOpen = 0, CoordBoxOpen = 0, KillCoordBox = 0;
  12. static double Pi =  3.14159265359;
  13.  
  14. WORD CoordFormat = IDM_RECT;
  15. WORD AngleFormat = IDM_DEGREES;
  16.  
  17. extern unsigned xdots, ydots;
  18. extern double xxmin, yymin, xxmax, yymax, delxx, delyy;
  19.  
  20. void CheckMathTools(void) {
  21.    if(KillCoordBox) {
  22.       DestroyWindow(hCoordBox);
  23.       KillCoordBox = 0;
  24.    }
  25. }
  26.  
  27. BOOL RegisterMathWindows(HANDLE hInstance) {
  28.     WNDCLASS  wc;
  29.  
  30.     hThisInst = hInstance;
  31.  
  32.     wc.style = CS_OWNDC;
  33.     wc.lpfnWndProc = MTWndProc;
  34.     wc.cbClsExtra = 0;
  35.     wc.cbWndExtra = 0;
  36.     wc.hInstance = hInstance;
  37.     wc.hIcon = LoadIcon(hInstance, "MathToolIcon");
  38.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  39.     wc.hbrBackground = GetStockObject(BLACK_BRUSH);
  40.     wc.lpszMenuName =  "MathToolsMenu";
  41.     wc.lpszClassName = MTClassName;
  42.  
  43.     return (RegisterClass(&wc));
  44. }
  45.  
  46. BOOL OpenMTWnd(void) {
  47.     hMathToolsWnd = CreateWindow(
  48.         MTClassName,
  49.         MTWindowTitle,
  50.         WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
  51.         CW_USEDEFAULT, CW_USEDEFAULT,
  52.         xdots, ydots,
  53.         hFractalWnd,
  54.         NULL,
  55.         hThisInst,
  56.         NULL
  57.     );
  58.     if(!hMathToolsWnd)
  59.         return(FALSE);
  60.  
  61.     ShowWindow(hMathToolsWnd, SW_SHOWNORMAL);
  62.     UpdateWindow(hMathToolsWnd);
  63.  
  64.     return(TRUE);
  65. }
  66.  
  67. void MathToolBox(HWND hWnd) {
  68.     hFractalWnd = hWnd;
  69.     if(MTWindowOpen)
  70.        DestroyWindow(hMathToolsWnd);
  71.     else {
  72.       if(!OpenMTWnd())
  73.          MessageBox(hWnd, "Error Opening Math Tools Window", NULL,
  74.                     MB_ICONEXCLAMATION | MB_OK);
  75.     }
  76. }
  77.  
  78. BOOL FAR PASCAL CoordBoxDlg(HWND hDlg, WORD Message, WORD wp, DWORD dp) {
  79.    HMENU hDlgMenu;
  80.  
  81.    hDlgMenu = GetMenu(hDlg);
  82.    switch(Message) {
  83.       case WM_INITDIALOG:
  84.          CoordBoxOpen = 1;
  85.          CheckMenuItem(GetMenu(hFractalWnd), IDM_COORD, MF_CHECKED);
  86.          hCoordBox = hDlg;
  87.          return(TRUE);
  88.       case WM_CLOSE:
  89.          KillCoordBox = 1;
  90.          break;
  91.       case WM_DESTROY:
  92.          CheckMenuItem(GetMenu(hFractalWnd), IDM_COORD, MF_UNCHECKED);
  93.          CoordBoxOpen = 0;
  94.          break;
  95.       case WM_COMMAND:
  96.          CheckMenuItem(hDlgMenu, AngleFormat, MF_UNCHECKED);
  97.          CheckMenuItem(hDlgMenu, CoordFormat, MF_UNCHECKED);
  98.          switch(wp) {
  99.             case IDM_RADIANS:
  100.             case IDM_GRAD:
  101.             case IDM_DEGREES:
  102.                AngleFormat = wp;
  103.                break;
  104.             case IDM_POLAR:
  105.             case IDM_RECT:
  106.             case IDM_PIXEL:
  107.                CoordFormat = wp;
  108.                break;
  109.          }
  110.          CheckMenuItem(hDlgMenu, AngleFormat, MF_CHECKED);
  111.          CheckMenuItem(hDlgMenu, CoordFormat, MF_CHECKED);
  112.          if(CoordFormat == IDM_POLAR) {
  113.             SetDlgItemText(hDlg, ID_X_NAME, "|z|");
  114.             SetDlgItemText(hDlg, ID_Y_NAME, "\xD8");
  115.             EnableMenuItem(hDlgMenu, IDM_DEGREES, MF_ENABLED);
  116.             EnableMenuItem(hDlgMenu, IDM_RADIANS, MF_ENABLED);
  117.             EnableMenuItem(hDlgMenu, IDM_GRAD, MF_ENABLED);
  118.          }
  119.          else {
  120.             SetDlgItemText(hDlg, ID_X_NAME, "x");
  121.             SetDlgItemText(hDlg, ID_Y_NAME, "y");
  122.             EnableMenuItem(hDlgMenu, IDM_DEGREES, MF_DISABLED | MF_GRAYED);
  123.             EnableMenuItem(hDlgMenu, IDM_RADIANS, MF_DISABLED | MF_GRAYED);
  124.             EnableMenuItem(hDlgMenu, IDM_GRAD, MF_DISABLED | MF_GRAYED);
  125.          }
  126.    }
  127.    return(FALSE);
  128. }
  129.  
  130. void UpdateCoordBox(DWORD dw) {
  131.    unsigned xPixel, yPixel;
  132.    double xd, yd, Angle, Modulus;
  133.    char xStr[40], yStr[40];
  134.  
  135.    xPixel = (unsigned)dw;
  136.    yPixel = (unsigned)(dw >> 16);
  137.    xd = xxmin + (delxx * xPixel);
  138.    yd = yymax - (delyy * yPixel);
  139.    switch(CoordFormat) {
  140.       case IDM_PIXEL:
  141.          sprintf(xStr, "%d", xPixel);
  142.          sprintf(yStr, "%d", yPixel);
  143.          break;
  144.       case IDM_RECT:
  145.          sprintf(xStr, "%+.8g", xd);
  146.          sprintf(yStr, "%+.8g", yd);
  147.          break;
  148.       case IDM_POLAR:
  149.          Modulus = (xd*xd) + (yd*yd);
  150.          if(Modulus > 1E-20) {
  151.             Modulus = sqrt(Modulus);
  152.             Angle = atan2(yd, xd);
  153.             switch(AngleFormat) {
  154.                case IDM_DEGREES:
  155.                   Angle = (Angle / Pi) * 180;
  156.                   break;
  157.                case IDM_GRAD:
  158.                   Angle = (Angle / Pi) * 200;
  159.                case IDM_RADIANS:
  160.                   break;
  161.             }
  162.          }
  163.          else {
  164.             Modulus = 0.0;
  165.             Angle = 0.0;
  166.          }
  167.          sprintf(xStr, "%+.8g", Modulus);
  168.          sprintf(yStr, "%+.8g", Angle);
  169.          break;
  170.    }
  171.    SetDlgItemText(hCoordBox, ID_X_COORD, xStr);
  172.    SetDlgItemText(hCoordBox, ID_Y_COORD, yStr);
  173. }
  174.  
  175. void CoordinateBox(HWND hWnd) {
  176.    FARPROC lpCoordBox;
  177.  
  178.    hFractalWnd = hWnd;
  179.    if(CoordBoxOpen)
  180.       DestroyWindow(hCoordBox);
  181.    else {
  182.       if(lpCoordBox = MakeProcInstance(CoordBoxDlg, hThisInst)) {
  183.          if(CreateDialog(hThisInst, "CoordBox", hWnd, lpCoordBox))
  184.             return;
  185.       }
  186.       MessageBox(hWnd, "Error Opening Coordinate Box",
  187.                  NULL, MB_ICONEXCLAMATION | MB_OK);
  188.    }
  189. }
  190.  
  191. long FAR PASCAL MTWndProc(HWND hWnd, unsigned Message, WORD wParam, DWORD lParam) {
  192.    switch (Message) {
  193.       case WM_CREATE:
  194.          CheckMenuItem(GetMenu(hFractalWnd), IDM_MATH_TOOLS, MF_CHECKED);
  195.          MTWindowOpen = 1;
  196.          break;
  197.       case WM_COMMAND:
  198.          switch(wParam) {
  199.             case IDM_EXIT:
  200.                DestroyWindow(hWnd);
  201.                break;
  202.          }
  203.          break;
  204.       case WM_DESTROY:
  205.          CheckMenuItem(GetMenu(hFractalWnd), IDM_MATH_TOOLS, MF_UNCHECKED);
  206.          MTWindowOpen = 0;
  207.          break;
  208.       default:
  209.          return(DefWindowProc(hWnd, Message, wParam, lParam));
  210.     }
  211.     return(0L);
  212. }
  213.