home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ascii11.zip / ascii.c next >
Text File  |  1998-04-17  |  18KB  |  524 lines

  1. // ASCII Table 1.1
  2. // Copyright 1998 by D.J. van Enckevort
  3. // This program and sourcecode is released as freeware
  4. // You can use this code for your own programs
  5. // If you use it, please keep a notice of this copyright
  6.  
  7. #define INCL_WIN
  8. #define INCL_PM
  9. #include <os2.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "ascii.h"
  14.  
  15. // declaration of the hab
  16. HAB hab;
  17.  
  18. // Program main function
  19. // Arguments:
  20. // argc number of commandline arguments;
  21. // argv commandline arguments
  22. // Note argv[0] is the name of the program
  23. // no return code
  24. void main(int argc, char *argv[])
  25. {
  26. HMQ hmq;
  27. HWND hwnd = NULLHANDLE;
  28. QMSG qmsg;
  29. char Path[CCHMAXPATH], *Program = NULL, FullName[CCHMAXPATH];
  30. HINI Profile;
  31.  
  32. // Do the usual initialization
  33. hab=WinInitialize(0);
  34. hmq=WinCreateMsgQueue(hab, 0);
  35.  
  36. // Open ASCII.INI
  37. strcpy(Path, argv[0]);
  38. strlwr(Path);
  39. Program=strstr(Path, "ascii.exe");
  40. Program[0]=0;
  41. strcpy(FullName, Path);
  42. strcat(FullName, "ascii.ini");
  43. Profile=PrfOpenProfile(hab, FullName);
  44.  
  45. // Load the main window from the executable resources
  46. hwnd=WinLoadDlg(HWND_DESKTOP, HWND_DESKTOP, WinProc, (HMODULE)0, DLG_ASCII, &Profile);
  47.  
  48. // Start the message queue
  49. while (WinGetMsg(hab, &qmsg, 0L, 0, 0))
  50.   WinDispatchMsg(hab, &qmsg);
  51.  
  52. // Finish the program and exit
  53. PrfCloseProfile(Profile);
  54. WinDestroyWindow(hwnd);
  55. WinDestroyMsgQueue(hmq);
  56. WinTerminate(hab);
  57. }
  58.  
  59. // Window procedure for the main window
  60. // Is called by the system
  61. MRESULT EXPENTRY WinProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  62. {
  63. // Several variables
  64. // Used in WM_COMMAND and WM_CONTROL (NotifyCode)
  65. USHORT ControlID, NotifyCode;
  66. // Used in WM_CONTROL to store text from value set
  67. VSTEXT vsText;
  68. // Used in WM_INITDLG
  69. USHORT row, col;
  70. // Used in WM_COMMAND
  71. // Used for the Font Dialog
  72. static FONTDLG fd;
  73. HWND hwndFD;
  74. HPS hps;
  75. // Used for Copy & Clear
  76. IPT Len;
  77. // CurrentFont used for fontselection & fontset
  78. // ch used in WM_INITDLG
  79. char ch[2], temp[11];
  80. static char CurrentFont[FACESIZE];
  81. // Pointsize of the font
  82. static int PointSize;
  83. // static to remember the value of this variable between different calls
  84. // Used in UM_RESIZE to size the controls
  85. static SWP oldframe, initialframe;
  86. SWP newframe;
  87. // Pointer the the handle to the .INI file
  88. static HINI *pProfile;
  89. ULONG Size;
  90. switch (msg)
  91.    {
  92.    case WM_INITDLG:
  93.       // Initialize window
  94.       // ch[0] contains the character to display in the cell of the value set
  95.       // ch[1] is the termination zero of the string
  96.       ch[0]=0;
  97.       ch[1]=0;
  98.       // Fill the value set with the ASCII table, from character 0 to 255
  99.       // value sets start with row 1 and column 1
  100.       // 16 rows * 16 cols = 256 cells
  101.       for (row=1; row<=16; row++)
  102.          {
  103.          for (col=1; col<=16; col++)
  104.             {
  105.             // Set the contents of the cell
  106.             WinSendMsg(WinWindowFromID(hwnd, SET_ASCII), VM_SETITEM, MPFROM2SHORT(row, col), MPFROMP((VOID *)ch));
  107.             // Raise ch[0] with one, which in effect is the same as copying the next character to ch[0]
  108.             ch[0]++;
  109.             }
  110.          }
  111.       // Get the handle to the .INI file
  112.       pProfile=(HINI*)PVOIDFROMMP(mp2);
  113.       // Get the initial size of the window
  114.       WinQueryWindowPos(hwnd, &initialframe);
  115.       // also in oldframe for sizing
  116.       oldframe.cx=initialframe.cx;
  117.       oldframe.cy=initialframe.cy;
  118.       // Read the size
  119.       newframe.cx=PrfQueryProfileInt(*pProfile, "Size", "x", initialframe.cx);
  120.       newframe.cy=PrfQueryProfileInt(*pProfile, "Size", "y", initialframe.cy);
  121.       // Read the position
  122.       newframe.x=PrfQueryProfileInt(*pProfile, "Position", "x", initialframe.x);
  123.       newframe.y=PrfQueryProfileInt(*pProfile, "Position", "y", initialframe.y);
  124.       WinSetWindowPos(hwnd, NULL, newframe.x, newframe.y, newframe.cx, newframe.cy,
  125.                          SWP_ACTIVATE | SWP_SHOW | SWP_SIZE | SWP_MOVE);
  126.       // Read the font dialog data
  127.       // Set default pointsize
  128.       PointSize=10;
  129.       // Set all values in the FONTDLG structure fd to zero
  130.       memset(&fd, 0, sizeof(FONTDLG));
  131.       // Fill the structure with the necessary data
  132.       // set default fAttrs values
  133.       fd.fAttrs.usRecordLength=sizeof(FATTRS);
  134.       fd.fAttrs.fsSelection=0;
  135.       fd.fAttrs.lMatch=0;
  136.       fd.fAttrs.idRegistry=0;
  137.       fd.fAttrs.usCodePage=850;
  138.       fd.fAttrs.lMaxBaselineExt=0;
  139.       fd.fAttrs.lAveCharWidth=0;
  140.       fd.fAttrs.fsType=0;
  141.       fd.fAttrs.fsFontUse=0;
  142.       // read fAttrs values
  143.       PrfQueryProfileData(*pProfile, "FontAttr", "PointSize", &PointSize, &Size);
  144.       PrfQueryProfileData(*pProfile, "FontAttr", "fsSelection", &fd.fAttrs.fsSelection, &Size);
  145.       PrfQueryProfileData(*pProfile, "FontAttr", "lMatch", &fd.fAttrs.lMatch, &Size);
  146.       PrfQueryProfileString(*pProfile, "FontAttr", "Facename", "Helv", fd.fAttrs.szFacename, FACESIZE);
  147.       PrfQueryProfileData(*pProfile, "FontAttr", "isRegistry", &fd.fAttrs.idRegistry, &Size);
  148.       PrfQueryProfileData(*pProfile, "FontAttr", "usCodePage", &fd.fAttrs.usCodePage, &Size);
  149.       PrfQueryProfileData(*pProfile, "FontAttr", "lMaxBaselineExt", &fd.fAttrs.lMaxBaselineExt, &Size);
  150.       PrfQueryProfileData(*pProfile, "FontAttr", "fAveCharWidth", &fd.fAttrs.lAveCharWidth, &Size);
  151.       PrfQueryProfileData(*pProfile, "FontAttr", "fsType", &fd.fAttrs.fsType, &Size);
  152.       PrfQueryProfileData(*pProfile, "FontAttr", "fsFontUse", &fd.fAttrs.fsFontUse, &Size);
  153.       // size of the structure
  154.       fd.cbSize=sizeof(FONTDLG);
  155.       // Current font
  156.       CurrentFont[0]=0;
  157.       fd.pszFamilyname=CurrentFont;
  158.       // Preferred point size
  159.       fd.fxPointSize=MAKEFIXED(PointSize,0);
  160.       // Size of buffer for fontfamily name
  161.       fd.usFamilyBufLen=FACESIZE;
  162.       // Default foreground colour
  163.       fd.clrFore=SYSCLR_WINDOWTEXT;
  164.       // Default background colour
  165.       fd.clrBack=SYSCLR_WINDOW;
  166.       // Dialog flags
  167.       // Center dialog, and use font attributes
  168.       fd.fl=FNTS_CENTER | FNTS_INITFROMFATTRS;
  169.       // Set the font through the presentation parameters
  170.       sprintf(CurrentFont, "%i.%s", PointSize, fd.fAttrs.szFacename);
  171.       // in the value set
  172.       WinSetPresParam(WinWindowFromID(hwnd, SET_ASCII), PP_FONTNAMESIZE, sizeof(CurrentFont), CurrentFont);
  173.       // in the mle (multi line entryfield)
  174.       WinSetPresParam(WinWindowFromID(hwnd, MLE_TEXT), PP_FONTNAMESIZE, sizeof(CurrentFont), CurrentFont);
  175.       // set the text of this static text control to the name and pointsize of the font
  176.       WinSetWindowText(WinWindowFromID(hwnd, ST_FONT), CurrentFont);
  177.       break;
  178.    case WM_COMMAND:
  179.       // Commands raising from the controls
  180.       // mp1 contains the resource ID of the control
  181.       ControlID=SHORT1FROMMP(mp1);
  182.       switch (ControlID)
  183.          {
  184.          case PB_CLEAR:
  185.             // Clear pushbutton
  186.             // Get the number of bytes in MLE
  187.             Len=LONGFROMMR(WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_QUERYTEXTLENGTH, 0L, 0L));
  188.             // Delete the contents of the MLE
  189.             WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_DELETE, 0L, MPFROMLONG(Len));
  190.             break;
  191.          case PB_COPY:
  192.             // copy pushbutton
  193.             // Get the number of bytes in MLE
  194.             Len=LONGFROMMR(WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_QUERYTEXTLENGTH, 0L, 0L));
  195.             // select all of the MLE contents
  196.             WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_SETSEL, 0L, MPFROMLONG(Len));
  197.             // copy selected text to the clipboard
  198.             WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_COPY, 0L, 0L);
  199.             break;
  200.          case PB_FONT:
  201.             // Font pushbutton
  202.             // Open a presentation space (required for the font dialog
  203.             hps=WinGetPS(hwnd);
  204.             // presentation space
  205.             fd.hpsScreen=hps;
  206.             // Open the fonddialog
  207.             hwndFD=WinFontDlg(HWND_DESKTOP, hwnd, &fd);
  208.             if ((hwndFD) && (fd.lReturn==DID_OK))
  209.                {
  210.                // User selected a font and pressed Ok
  211.                // Create a complete fontname
  212.                PointSize=fd.fxPointSize/65536;
  213.                sprintf(CurrentFont, "%i.%s", PointSize, fd.fAttrs.szFacename);
  214.                // and set the presentation parameters
  215.                // for the value set
  216.                WinSetPresParam(WinWindowFromID(hwnd, SET_ASCII), PP_FONTNAMESIZE, sizeof(CurrentFont), CurrentFont);
  217.                // for the MLE
  218.                WinSetPresParam(WinWindowFromID(hwnd, MLE_TEXT), PP_FONTNAMESIZE, sizeof(CurrentFont), CurrentFont);
  219.                // Update the text of the static text control
  220.                WinSetWindowText(WinWindowFromID(hwnd, ST_FONT), CurrentFont);
  221.                }
  222.             // release presentation space
  223.             WinReleasePS(hps);
  224.             break;
  225.          case PB_ABOUT:
  226.             // about pushbutton
  227.             // Load and display the about dialog, we don't load it modally, because I hate that
  228.             WinLoadDlg(HWND_DESKTOP, HWND_DESKTOP, AboutProc, (HMODULE)0, DLG_ABOUT, NULL);
  229.             break;
  230.          }
  231.       break;
  232.    case WM_CONTROL:
  233.       // Messages from controls
  234.       // Control resource ID
  235.       ControlID=SHORT1FROMMP(mp1);
  236.       // Notify code
  237.       NotifyCode=SHORT2FROMMP(mp1);
  238.       switch (ControlID)
  239.          {
  240.          case SET_ASCII:
  241.             // Value set
  242.             if ((NotifyCode==VN_SELECT) || (NotifyCode==VN_ENTER))
  243.                {
  244.                // A Cell was selected, or enter was pressed in the selected cell
  245.                // structure to get the text from the valueset
  246.                // buffer size of zero, to let them know we want to query the needed size
  247.                vsText.ulBufLen=0;
  248.                // Send message to query buffer size
  249.                WinSendMsg(WinWindowFromID(hwnd, SET_ASCII), VM_QUERYITEM, mp2, &vsText);
  250.                // Increase buffer size by one to accomodate the trailing zero (I don't know whether it is
  251.                // needed, some messages include it, others not)
  252.                vsText.ulBufLen++;
  253.                // Allocate the memory
  254.                DosAllocMem((void**)&vsText.pszItemText, vsText.ulBufLen, PAG_COMMIT | PAG_READ | PAG_WRITE);
  255.                // Query the text
  256.                WinSendMsg(WinWindowFromID(hwnd, SET_ASCII), VM_QUERYITEM, mp2, &vsText);
  257.                // Insert the text in the MLE
  258.                WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_INSERT, MPFROMP(vsText.pszItemText), 0L);
  259.                // Release the allocated memory
  260.                DosFreeMem(vsText.pszItemText);
  261.                }
  262.             break;
  263.          }
  264.       break;
  265.    case WM_PAINT:
  266.       // Window need to be repainted
  267.       // Call Default Dialog Window Procedure to repaint the window
  268.       WinDefDlgProc(hwnd, msg, mp1, mp2);
  269.       // Do a resize of the controls (should check first whether it is needed)
  270.       WinPostMsg(hwnd, UM_RESIZE, 0L, 0L);
  271.       break;
  272.    case UM_RESIZE:
  273.       // Requested to resize the controls
  274.       // Query new size of the window
  275.       WinQueryWindowPos(hwnd, &newframe);
  276.       // Check whether the new size isn't to small
  277.       if ((newframe.cx<initialframe.cx) || (newframe.cy<initialframe.cy))
  278.          {
  279.          // window is to small, restore it to the initial size
  280.          // Copy the initial sizes to the newframe structure
  281.          // So we use the initial size, but the new position
  282.          newframe.cx=initialframe.cx;
  283.          newframe.cy=initialframe.cy;
  284.          // Make it so
  285.          WinSetWindowPos(hwnd, NULL, newframe.x, newframe.y, newframe.cx, newframe.cy,
  286.                          SWP_ACTIVATE | SWP_SHOW | SWP_SIZE | SWP_MOVE);
  287.          // And break out, we've finished the resize (and don't want to do more unneeded work)
  288.          break;
  289.          }
  290.       // These controls we don't want to size, they have their proper size already, just have to move them
  291.       // to their new positions
  292.       // Move the static text with the font name
  293.       Move(hwnd, oldframe, newframe, ST_FONT);
  294.       // Move the copy button
  295.       Move(hwnd, oldframe, newframe, PB_COPY);
  296.       // Move the clear button
  297.       Move(hwnd, oldframe, newframe, PB_CLEAR);
  298.       // move the font button
  299.       Move(hwnd, oldframe, newframe, PB_FONT);
  300.       // move the about button
  301.       Move(hwnd, oldframe, newframe, PB_ABOUT);
  302.       // These controls have to be resized and moved
  303.       // The MLE
  304.       Resize(hwnd, oldframe, newframe, MLE_TEXT);
  305.       // The value set
  306.       Resize(hwnd, oldframe, newframe, SET_ASCII);
  307.       // Query the size of the window again, to store in oldframe
  308.       // (could have copied it from newframe too)
  309.       WinQueryWindowPos(hwnd, &oldframe);
  310.       break;
  311.    case WM_CLOSE:
  312.       // User has selected to close the program
  313.       // (Could ask for confirmation here)
  314.       // Save the size & position
  315.       WinQueryWindowPos(hwnd, &newframe);
  316.       sprintf(temp, "%li", newframe.cx);
  317.       PrfWriteProfileString(*pProfile, "Size", "x", temp);
  318.       sprintf(temp, "%li", newframe.cy);
  319.       PrfWriteProfileString(*pProfile, "Size", "y", temp);
  320.       sprintf(temp, "%li", newframe.x);
  321.       PrfWriteProfileString(*pProfile, "Position", "x", temp);
  322.       sprintf(temp, "%li", newframe.y);
  323.       PrfWriteProfileString(*pProfile, "Position", "y", temp);
  324.       // Save the font dialog values
  325.       PrfWriteProfileData(*pProfile, "FontAttr", "PointSize", &PointSize, sizeof(INT));
  326.       PrfWriteProfileData(*pProfile, "FontAttr", "fsSelection", &fd.fAttrs.fsSelection, sizeof(USHORT));
  327.       PrfWriteProfileData(*pProfile, "FontAttr", "lMatch", &fd.fAttrs.lMatch, sizeof(LONG));
  328.       PrfWriteProfileString(*pProfile, "FontAttr", "Facename", fd.fAttrs.szFacename);
  329.       PrfWriteProfileData(*pProfile, "FontAttr", "isRegistry", &fd.fAttrs.idRegistry, sizeof(USHORT));
  330.       PrfWriteProfileData(*pProfile, "FontAttr", "usCodePage", &fd.fAttrs.usCodePage, sizeof(USHORT));
  331.       PrfWriteProfileData(*pProfile, "FontAttr", "lMaxBaselineExt", &fd.fAttrs.lMaxBaselineExt, sizeof(LONG));
  332.       PrfWriteProfileData(*pProfile, "FontAttr", "fAveCharWidth", &fd.fAttrs.lAveCharWidth, sizeof(LONG));
  333.       PrfWriteProfileData(*pProfile, "FontAttr", "fsType", &fd.fAttrs.fsType, sizeof(USHORT));
  334.       PrfWriteProfileData(*pProfile, "FontAttr", "fsFontUse", &fd.fAttrs.fsFontUse, sizeof(USHORT));
  335.       // Dismiss the window
  336.       WinDismissDlg(hwnd, TRUE);
  337.       // Let the program know we are ready to quit
  338.       WinPostMsg(hwnd, WM_QUIT, 0L, 0L);
  339.       break;
  340.    default:
  341.       // We didn't handle the message, let the default window procedure handle it
  342.       return WinDefDlgProc(hwnd, msg, mp1, mp2);
  343.    }
  344. // We have handled all messages
  345. return (MRESULT)FALSE;
  346. }
  347.  
  348. // Window procedure for the about dialogbox
  349. // Is called by the system
  350. MRESULT EXPENTRY AboutProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  351. {
  352. // Declaration of variables
  353. USHORT ControlID;
  354. switch (msg)
  355.    {
  356.    case WM_INITDLG:
  357.       // Initialization
  358.       // Nothing
  359.       break;
  360.    case WM_COMMAND:
  361.       // Commands from controls
  362.       // Resource ID of control
  363.       ControlID=SHORT1FROMMP(mp1);
  364.       switch (ControlID)
  365.          {
  366.          case PB_OK:
  367.             // Pushbutton pressed
  368.             // Close the About dialogbox
  369.             // Send the WM_CLOSE message
  370.             WinSendMsg(hwnd, WM_CLOSE, 0L, 0L);
  371.             break;
  372.          }
  373.       break;
  374.    case WM_CLOSE:
  375.       // Close the About dialogbox
  376.       // User pressed the Ok button
  377.       // Dismiss the dialogbox
  378.       WinDismissDlg(hwnd, TRUE);
  379.       // Note we now do not send a WM_QUIT message, if we did so, we would close all of the application
  380.       break;
  381.    default:
  382.       // Let the default window procedure handle messages we didn't handle
  383.       return WinDefDlgProc(hwnd, msg, mp1, mp2);
  384.    }
  385. // All messages are handled
  386. return (MRESULT)FALSE;
  387. }
  388.  
  389. // Function to resize and move controls
  390. // Parameters:
  391. // hwnd Window handler of the main window
  392. // oldframe & newframe SWP structures which contain the old & new sizes of the main window
  393. //   used to calculate the new size and position of the client window
  394. // ID Resource ID of the client window
  395. void Resize(HWND hwnd, SWP oldframe, SWP newframe, ULONG ID)
  396. {
  397. // structure to store the current position and size
  398. SWP swp;
  399. // Flags to resize and move
  400. ULONG flags=SWP_ACTIVATE | SWP_SHOW | SWP_SIZE | SWP_MOVE;
  401. // Query current position and size
  402. WinQueryWindowPos(WinWindowFromID(hwnd, ID), &swp);
  403. // Calculate the new position and size
  404. swp.x=(swp.x*newframe.cx)/oldframe.cx;
  405. swp.y=(swp.y*newframe.cy)/oldframe.cy;
  406. swp.cx=(swp.cx*newframe.cx)/oldframe.cx;
  407. swp.cy=(swp.cy*newframe.cy)/oldframe.cy;
  408. // Move and resize the control
  409. WinSetWindowPos(WinWindowFromID(hwnd, ID), NULL, swp.x, swp.y, swp.cx, swp.cy, flags);
  410. }
  411.  
  412. // Function to move controls
  413. // Parameters:
  414. // hwnd Window handler of the main window
  415. // oldframe & newframe SWP structures which contain the old & new sizes of the main window
  416. // ID Resource ID of the client window
  417. void Move(HWND hwnd, SWP oldframe, SWP newframe, ULONG ID)
  418. {
  419. // structure to store the current position and size
  420. SWP swp;
  421. // Flags to move
  422. ULONG flags=SWP_ACTIVATE | SWP_SHOW | SWP_MOVE;
  423. // Query current position and size
  424. WinQueryWindowPos(WinWindowFromID(hwnd, ID), &swp);
  425. // Calculate the new position
  426. swp.x=(swp.x*newframe.cx)/oldframe.cx;
  427. swp.y=(swp.y*newframe.cy)/oldframe.cy;
  428. // Move the control
  429. WinSetWindowPos(WinWindowFromID(hwnd, ID), NULL, swp.x, swp.y, swp.cx, swp.cy, flags);
  430. }
  431.  
  432. // That was all
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480. // Really?
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523. // Yes, really.
  524.