home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / asci20b1.zip / ascii.c < prev    next >
Text File  |  1999-05-21  |  25KB  |  594 lines

  1. // ASCII Table 2.0
  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_WINHOOKS
  9. #define INCL_PM
  10. #define INCL_GPI
  11. #include <os2.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "ascii.h"
  16. #include "tool.h"
  17.  
  18. // declaration of the hab
  19. HAB hab;
  20. HWND hwndMain = NULLHANDLE;
  21.  
  22. // Program main function
  23. // Arguments:
  24. // argc number of commandline arguments;
  25. // argv commandline arguments
  26. // Note argv[0] is the name of the program
  27. // no return code
  28. void main(int argc, char *argv[])
  29. {
  30. HMQ hmq;
  31. QMSG qmsg;
  32. char Path[CCHMAXPATH], *Program = NULL, FullName[CCHMAXPATH];
  33. HINI Profile;
  34. HWND hwndMenu;
  35. HACCEL hAccel;
  36. HPOINTER Icon;
  37.  
  38. // Do the usual initialization
  39. hab=WinInitialize(0);
  40. hmq=WinCreateMsgQueue(hab, 0);
  41.  
  42. WinSetHook(hab, HMQ_CURRENT, HK_INPUT, (PFN)MessageHook, NULLHANDLE);
  43. // Open ASCII.INI
  44. strcpy(Path, argv[0]);
  45. strlwr(Path);
  46. Program=strstr(Path, "ascii.exe");
  47. Program[0]=0;
  48. strcpy(FullName, Path);
  49. strcat(FullName, "ascii.ini");
  50. Profile=PrfOpenProfile(hab, FullName);
  51.  
  52. // Load the main window from the executable resources
  53. hwndMain=WinLoadDlg(HWND_DESKTOP, HWND_DESKTOP, WinProc, (HMODULE)0, DLG_ASCII, &Profile);
  54. // Load menu
  55. hwndMenu = WinLoadMenu(hwndMain, (HMODULE)0, ID_MENU);
  56. // Load icon & assign it to the main window
  57. Icon = WinLoadPointer( HWND_DESKTOP, NULLHANDLE, ID_ICON);
  58. WinSendMsg(hwndMain, WM_SETICON, MPFROMLONG(Icon), (MPARAM)NULL);
  59. // Load Accellerator table and make it active
  60. hAccel = WinLoadAccelTable(hab, (HMODULE)0, ID_ACCELTABLE);
  61. WinSetAccelTable(hab, hAccel, hwndMain);
  62. // update window
  63. WinSendMsg(hwndMain, WM_UPDATEFRAME, MPFROMLONG(ID_MENU), (MPARAM)NULL);
  64.  
  65. // Start the message queue
  66. while (WinGetMsg(hab, &qmsg, 0L, 0, 0))
  67.   WinDispatchMsg(hab, &qmsg);
  68.  
  69. // Finish the program and exit
  70. PrfCloseProfile(Profile);
  71. WinDestroyWindow(hwndMain);
  72. // Release hook
  73. WinReleaseHook(hab, HMQ_CURRENT, HK_INPUT, (PFN)MessageHook, NULLHANDLE);
  74. WinDestroyMsgQueue(hmq);
  75. WinTerminate(hab);
  76. }
  77.  
  78. BOOL EXPENTRY MessageHook(HAB hab, PQMSG pqmsg, ULONG fs)
  79. {
  80. SHORT x, y, dx, dy, col, row;
  81. MRESULT mr;
  82. char title[25];
  83. SWP swp;
  84. // Do we have a WM_MOUSEMOVE for SET_ASCII?
  85.  
  86. if (pqmsg->msg==WM_MOUSEMOVE)
  87.    {
  88.    if (WinWindowFromID(hwndMain, SET_ASCII)==pqmsg->hwnd)
  89.       {
  90.       // Mouse is moved
  91.       // Position of the mouse
  92.       x=SHORT1FROMMP(pqmsg->mp1);
  93.       y=SHORT2FROMMP(pqmsg->mp1);
  94.       // Size of the control
  95.       WinQueryWindowPos(pqmsg->hwnd, &swp);
  96.       // Calculate division factors
  97.       dx=swp.cx/32;
  98.       dy=swp.cy/8;
  99.       // calculate column and row
  100.       col=(x)/dx;
  101.       row=7-(y)/dy;
  102.       // is it within range?
  103.       if (((row>=0) && (row<=7)) && ((col>=0) && (col<=31)))
  104.          {
  105.          // display the alt-code in the titlebar
  106.          sprintf(title, "ASCII Table - alt-%i", row*32+col);
  107.          WinSetWindowText(hwndMain, title);
  108.          }
  109.       else
  110.          {
  111.          // Set Window title to normal
  112.          WinSetWindowText(hwndMain, "ASCII Table");
  113.          }
  114.       }
  115.    else
  116.       {
  117.       // Set Window title to normal
  118.       WinSetWindowText(hwndMain, "ASCII Table");
  119.       }
  120.    }
  121. if (fs==PM_REMOVE)
  122.    {
  123.    return FALSE;
  124.    }
  125. return TRUE;
  126. }
  127.  
  128. // Window procedure for the main window
  129. // Is called by the system
  130. MRESULT EXPENTRY WinProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  131. {
  132. // Several variables
  133. // Used for replies
  134. USHORT Reply;
  135. // Used in WM_COMMAND and WM_CONTROL (NotifyCode)
  136. USHORT ControlID, NotifyCode;
  137. // Used in WM_CONTROL to store text from value set
  138. // Used in WM_INITDLG
  139. USHORT row, col;
  140. // Used in WM_COMMAND
  141. // Used for the Font Dialog
  142. static FONTDLG fd;
  143. // hwndFD = font dialog
  144. // hwndFileDlg = file dialog
  145. // hwndDT = direct type static omdat deze langer bewaard moet blijven
  146. // menu = menu
  147. HWND hwndFD, hwndFileDlg, menu;
  148. static HWND hwndDT;
  149. // Used for the File Dialog
  150. FILEDLG FileDlg;
  151. // used for file open / file save / file save as
  152. FILE *f;
  153. size_t Read, Written;
  154. HPS hps;
  155. // Used for Copy & Clear
  156. IPT Len, StartingPoint;
  157. // CurrentFont used for fontselection & fontset
  158. // ch used in WM_INITDLG
  159. char ch[CHAR_BUFFER], temp[TEMP_BUFFER], buffer[MAX_BUFFER];
  160. static char CurrentFont[FACESIZE];
  161. // Pointsize of the font
  162. static int PointSize;
  163. static USHORT usCodePage;
  164. // static to remember the value of this variable between different calls
  165. // Used in UM_RESIZE to size the controls
  166. static SWP oldframe, initialframe;
  167. SWP newframe;
  168. // Pointer the the handle to the .INI file
  169. static HINI *pProfile;
  170. ULONG Size;
  171. MRESULT mr;
  172. static char FileName[CCHMAXPATH];
  173. POWNERITEM OwnerItem;
  174. static char PtSizeList[80]="1 2 4 6 8 9 10 11 12 14 16 18 20 24 30 36 40 42 48 72 80 96";
  175. // DirectType is set if the user has activated DirectType
  176. // (Three states: Off/Waiting for User Input/On) (DT_OFF/DT_WAIT/DT_ON)
  177. static char DirectType;
  178. switch (msg)
  179.    {
  180.    case WM_INITDLG:
  181.       // Initialize window
  182.       // No filename
  183.       FileName[0]=0;
  184.       usCodePage=850;
  185.       // Create Import/Export buffer
  186.       memset(buffer, 0, MAX_BUFFER);
  187.       // Set MLE Text format
  188.       WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_FORMAT, MPFROMSHORT(MLFIE_NOTRANS), MPFROMLONG(0L));
  189.       // Set Import/Export buffer
  190.       WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_SETIMPORTEXPORT, MPFROMP(buffer), MPFROMLONG(8192));
  191.       // Register this window as a viewer for the clipboard so we will be
  192.       // notified when there is data on the clipboard.
  193.       WinSetClipbrdViewer(hab, hwnd);
  194.       // Set all valueset cells to VIA_OWNERDRAW
  195.       for (row=1; row<=MAX_ROW; row++)
  196.          {
  197.          for (col=1; col<=MAX_COLUMN; col++)
  198.             {
  199.             // Set the contents of the cell
  200.             WinSendMsg(WinWindowFromID(hwnd, SET_ASCII), VM_SETITEMATTR, MPFROM2SHORT(row, col), MPFROM2SHORT(VIA_TEXT | VIA_OWNERDRAW, TRUE));
  201.             }
  202.          }
  203.       // Get the handle to the .INI file
  204.       pProfile=(HINI*)PVOIDFROMMP(mp2);
  205.       // Get the initial size of the window
  206.       WinQueryWindowPos(hwnd, &initialframe);
  207.       // also in oldframe for sizing
  208.       oldframe.cx=initialframe.cx;
  209.       oldframe.cy=initialframe.cy;
  210.       // Read the size
  211.       newframe.cx=PrfQueryProfileInt(*pProfile, "Size", "x", initialframe.cx);
  212.       newframe.cy=PrfQueryProfileInt(*pProfile, "Size", "y", initialframe.cy);
  213.       // Read the position
  214.       newframe.x=PrfQueryProfileInt(*pProfile, "Position", "x", initialframe.x);
  215.       newframe.y=PrfQueryProfileInt(*pProfile, "Position", "y", initialframe.y);
  216.       WinSetWindowPos(hwnd, NULL, newframe.x, newframe.y, newframe.cx, newframe.cy,
  217.                          SWP_ACTIVATE | SWP_SHOW | SWP_SIZE | SWP_MOVE);
  218.       PrfLoadFont(pProfile, &fd);
  219.       // Set the font through the presentation parameters
  220.       PointSize=fd.fxPointSize/65536;
  221.       fd.pszPtSizeList=PtSizeList;
  222.       sprintf(CurrentFont, "%i.%s", PointSize, fd.fAttrs.szFacename);
  223.       // in the value set
  224.       WinSetPresParam(WinWindowFromID(hwnd, SET_ASCII), PP_FONTNAMESIZE, sizeof(CurrentFont), CurrentFont);
  225.       // in the mle (multi line entryfield)
  226.       WinSetPresParam(WinWindowFromID(hwnd, MLE_TEXT), PP_FONTNAMESIZE, sizeof(CurrentFont), CurrentFont);
  227.       // set the text of this static text control to the name and pointsize of the font
  228.       WinSetWindowText(WinWindowFromID(hwnd, ST_FONT), CurrentFont);
  229.       WinPostMsg(hwnd, UM_SETMENUSTATE, MPFROMSHORT(MI_SAVE), MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED));
  230.       WinPostMsg(hwnd, UM_SETMENUSTATE, MPFROMSHORT(MI_PASTE), MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED));
  231.       WinPostMsg(hwnd, UM_ADDCPMENU, 0L, 0L);
  232.       break;
  233.    case WM_COMMAND:
  234.       // Commands raising from the controls
  235.       // mp1 contains the resource ID of the control
  236.       ControlID=SHORT1FROMMP(mp1);
  237.       switch (ControlID)
  238.          {
  239.          case MI_CLEAR:
  240.             // Clear menu
  241.             // Get the number of bytes in MLE
  242.             Len=LONGFROMMR(WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_QUERYTEXTLENGTH, 0L, 0L));
  243.             // Delete the contents of the MLE
  244.             WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_DELETE, 0L, MPFROMLONG(Len));
  245.             break;
  246.          case MI_COPY:
  247.             // copy menu
  248.             WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_COPY, 0L, 0L);
  249.             break;
  250.          case MI_CUT:
  251.             // cut menu
  252.             // cut & copy selected text to the clipboard
  253.             WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_CUT, 0L, 0L);
  254.             break;
  255.          case MI_PASTE:
  256.             WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_PASTE, 0L, 0L);
  257.             break;
  258.          case MI_MLE:
  259.             break;
  260.          case MI_DIRECTTYPE:
  261.             menu=HWNDFROMMP(mp2);
  262.             if (DirectType==DT_OFF)
  263.                { // Ask the user for the program for DirectType
  264.                DirectType=DT_WAIT;
  265.                WinLoadDlg(HWND_DESKTOP, HWND_DESKTOP, DirectTypeProc, (HMODULE)0, DLG_ABOUT, NULL);
  266.                WinCheckMenuItem(menu, ControlID, FALSE);
  267.                }
  268.             if (DirectType==DT_ON)
  269.                { // Switch off DirectType
  270.                DirectType=DT_OFF;
  271.                hwndDT=NULL_HANDLE;
  272.                WinCheckMenuItem(menu, ControlID, FALSE);
  273.                }
  274.             if (DirectType==DT_WAIT):
  275.                { // The window is still waiting for user input, switch to the window
  276.                // Probably have to use WinFocusChange, but need to know HWND
  277.                }
  278.             break;
  279.          case MI_FONT:
  280.             // Font menu
  281.             // Open a presentation space (required for the font dialog
  282.             hps=WinGetPS(hwnd);
  283.             // presentation space
  284.             fd.hpsScreen=hps;
  285.             // Open the fonddialog
  286.             hwndFD=WinFontDlg(HWND_DESKTOP, hwnd, &fd);
  287.             if ((hwndFD) && (fd.lReturn==DID_OK))
  288.                {
  289.                // User selected a font and pressed Ok
  290.                // Create a complete fontname
  291.                PointSize=fd.fxPointSize/65536;
  292.                sprintf(CurrentFont, "%i.%s", PointSize, fd.fAttrs.szFacename);
  293.                fd.fAttrs.usCodePage=usCodePage;
  294.                // and set the presentation parameters
  295.                // for the value set
  296.                WinSetPresParam(WinWindowFromID(hwnd, SET_ASCII), PP_FONTNAMESIZE, sizeof(CurrentFont), CurrentFont);
  297.                // for the MLE
  298.                WinSetPresParam(WinWindowFromID(hwnd, MLE_TEXT), PP_FONTNAMESIZE, sizeof(CurrentFont), CurrentFont);
  299.                // Update the text of the static text control
  300.                WinSetWindowText(WinWindowFromID(hwnd, ST_FONT), CurrentFont);
  301.                }
  302.             // release presentation space
  303.             WinReleasePS(hps);
  304.             break;
  305.          case MI_OPEN:
  306.             memset(&FileDlg, 0, sizeof(FILEDLG));
  307.             FileDlg.cbSize=sizeof(FILEDLG);
  308.             FileDlg.fl=FDS_OPEN_DIALOG | FDS_CENTER;
  309.             hwndFileDlg=WinFileDlg(HWND_DESKTOP, hwnd, &FileDlg);
  310.             if ((hwndFileDlg!=NULLHANDLE) && (FileDlg.lReturn==DID_OK))
  311.                {
  312.                if ((f=fopen(FileDlg.szFullFile, "r"))!=NULL)
  313.                   {
  314.                   WinPostMsg(hwnd, UM_SETMENUSTATE, MPFROMSHORT(MI_SAVE), MPFROM2SHORT(MIA_DISABLED, ~MIA_DISABLED));
  315.                   // Get the number of bytes in MLE
  316.                   Len=LONGFROMMR(WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_QUERYTEXTLENGTH, 0L, 0L));
  317.                   // Delete the contents of the MLE
  318.                   WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_DELETE, 0L, MPFROMLONG(Len));
  319.                   strcpy(FileName, FileDlg.szFullFile);
  320.                   StartingPoint=0;
  321.                   while (!feof(f))
  322.                      {
  323.                      Read=fread(buffer, 1, MAX_BUFFER, f);
  324.                      WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_SETIMPORTEXPORT, MPFROMP(buffer), MPFROMLONG(MAX_BUFFER));
  325.                      WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_IMPORT, MPFROMP(&StartingPoint), MPFROMLONG(Read));
  326.                      }
  327.                   fclose(f);
  328.                   }
  329.                else
  330.                   WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, "Couldn't open file, file might be in use.", "Error", 0, MB_OK | MB_ERROR);
  331.                }
  332.             break;
  333.          case MI_SAVE:
  334.             if ((f=fopen(FileName, "w"))!=NULL)
  335.                {
  336.                StartingPoint=0;
  337.                // Get the number of bytes in MLE
  338.                Len=LONGFROMMR(WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_QUERYTEXTLENGTH, 0L, 0L));
  339.                while (Len>0)
  340.                   {
  341.                   WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_SETIMPORTEXPORT, MPFROMP(buffer), MPFROMLONG(MAX_BUFFER));
  342.                   Read=LONGFROMMR(WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_EXPORT, MPFROMP(&StartingPoint), MPFROMLONG(&Len)));
  343.                   Written=fwrite(buffer, 1, Read, f);
  344.                   }
  345.                fclose(f);
  346.                }
  347.             else
  348.                WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, "Couldn't open file, file might be in use.", "Error", 0, MB_OK | MB_ERROR);
  349.             break;
  350.          case MI_SAVEAS:
  351.             do
  352.                {
  353.                Reply=MBID_YES;
  354.                memset(&FileDlg, 0, sizeof(FILEDLG));
  355.                FileDlg.cbSize=sizeof(FILEDLG);
  356.                FileDlg.fl=FDS_SAVEAS_DIALOG | FDS_CENTER;
  357.                hwndFileDlg=WinFileDlg(HWND_DESKTOP, hwnd, &FileDlg);
  358.                if ((hwndFileDlg!=NULLHANDLE) && (FileDlg.lReturn==DID_OK))
  359.                   {
  360.                   if ((f=fopen(FileDlg.szFullFile, "r"))!=NULL)
  361.                      {
  362.                      Reply=WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, "File already exists, overwrite?", "Warning", 0, MB_YESNO | MB_QUERY);
  363.                      fclose(f);
  364.                      }
  365.                   else
  366.                      {
  367.                      Reply=MBID_YES;
  368.                      }
  369.                   }
  370.                else
  371.                   if (FileDlg.lReturn==0)
  372.                      WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, "Failed to open file dialog, cannot save file!", "Error", 0, MB_OK | MB_ERROR);
  373. //                  Reply=MBID_NO;
  374.                }
  375.             while (Reply==MBID_NO);
  376.             if ((hwndFileDlg!=NULLHANDLE) && (FileDlg.lReturn==DID_OK))
  377.                {
  378.                if ((f=fopen(FileDlg.szFullFile, "w"))!=NULL)
  379.                   {
  380.                   strcpy(FileName, FileDlg.szFullFile);
  381.                   StartingPoint=0;
  382.                   // Get the number of bytes in MLE
  383.                   Len=LONGFROMMR(WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_QUERYTEXTLENGTH, 0L, 0L));
  384.                   while (Len>0)
  385.                      {
  386.                      WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_SETIMPORTEXPORT, MPFROMP(buffer), MPFROMLONG(MAX_BUFFER));
  387.                      Read=LONGFROMMR(WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_EXPORT, MPFROMP(&StartingPoint), MPFROMLONG(&Len)));
  388.                      Written=fwrite(buffer, 1, Read, f);
  389.                      }
  390.                   fclose(f);
  391.                   }
  392.                else
  393.                   WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, "Couldn't open file, file might be in use.", "Error", 0, MB_OK | MB_ERROR);
  394.                }
  395.             break;
  396.          case MI_CLOSE:
  397.             WinPostMsg(hwnd, WM_CLOSE, 0L, 0L);
  398.             break;
  399.          case MI_PRODUCTINFO:
  400.             // ProductInfo menu
  401.             // Load and display the about dialog, we don't load it modally, because I hate that
  402.             WinLoadDlg(HWND_DESKTOP, HWND_DESKTOP, AboutProc, (HMODULE)0, DLG_ABOUT, NULL);
  403.             break;
  404.          default:
  405.             // The control ID is of one of the codepage menus?
  406.             if (ControlID>1000)
  407.                {
  408.                menu=WinWindowFromID(hwnd, FID_MENU); // Vraag de handle naar het menu op
  409.                WinCheckMenuItem(menu, usCodePage+1000, FALSE);
  410.                WinCheckMenuItem(menu, ControlID, TRUE);
  411.                usCodePage=ControlID-1000;
  412.                fd.fAttrs.usCodePage=usCodePage;
  413.                WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_SETFONT, MPFROMP(&fd.fAttrs), 0L);
  414.                WinSetPresParam(WinWindowFromID(hwnd, SET_ASCII), PP_FONTNAMESIZE, sizeof(CurrentFont), CurrentFont);
  415.                }
  416.             break;
  417.          }
  418.       break;
  419.    case WM_CONTROL:
  420.       // Messages from controls
  421.       // Control resource ID
  422.       ControlID=SHORT1FROMMP(mp1);
  423.       // Notify code
  424.       NotifyCode=SHORT2FROMMP(mp1);
  425.       switch (ControlID)
  426.          {
  427.          case SET_ASCII:
  428.             // Value set
  429.             if ((NotifyCode==VN_SELECT) || (NotifyCode==VN_ENTER))
  430.                {
  431.                // A Cell was selected, or enter was pressed in the selected cell
  432.                mr=WinSendMsg(WinWindowFromID(hwnd, SET_ASCII), VM_QUERYSELECTEDITEM, 0L, 0L);
  433.                row=SHORT1FROMMR(mr)-1;
  434.                col=SHORT2FROMMR(mr)-1;
  435.                ch[0]=row*MAX_COLUMN+col;
  436.                ch[1]=0;
  437.                // Insert the text in the MLE
  438.                WinSendMsg(WinWindowFromID(hwnd, MLE_TEXT), MLM_INSERT, MPFROMP(ch), 0L);
  439.                }
  440.             break;
  441.          }
  442.       break;
  443.    case WM_DRAWITEM:
  444.       ControlID=SHORT1FROMMP(mp1);
  445.       if (ControlID==SET_ASCII)
  446.          {
  447.          OwnerItem=(OWNERITEM*)PVOIDFROMMP(mp2);
  448.          switch (OwnerItem->idItem)
  449.             {
  450.             case VDA_ITEM:
  451.                DrawValueSet(OwnerItem, &fd.fAttrs);
  452.                return (MRESULT)TRUE;
  453.             }
  454.          }
  455.       break;
  456.    case WM_DRAWCLIPBOARD:
  457.       // Open the clipboard
  458.       WinOpenClipbrd(hab);
  459.       // Does the clipboard have text?
  460.       if (WinQueryClipbrdData(hab,CF_TEXT)!=0)
  461.          { // Make the Paste menu available
  462.          WinSendMsg(WinWindowFromID(hwnd, FID_MENU), MM_SETITEMATTR, MPFROM2SHORT(MI_PASTE, TRUE), MPFROM2SHORT(MIA_DISABLED, ~MIA_DISABLED));
  463.          }
  464.       else
  465.          { // Disable the Paste menu
  466.          WinSendMsg(WinWindowFromID(hwnd, FID_MENU), MM_SETITEMATTR, MPFROM2SHORT(MI_PASTE, TRUE), MPFROM2SHORT(MIA_DISABLED, MIA_DISABLED));
  467.          }
  468.       // Close the clipboard
  469.       WinCloseClipbrd(hab);
  470.       break;
  471.    case WM_PAINT:
  472.       // Window need to be repainted
  473.       // Call Default Dialog Window Procedure to repaint the window
  474.       WinDefDlgProc(hwnd, msg, mp1, mp2);
  475.       // Do a resize of the controls (should check first whether it is needed)
  476.       WinQueryWindowPos(hwnd, &newframe);
  477.       if ((newframe.cx!=oldframe.cx) || (newframe.cy!=oldframe.cy))
  478.          {
  479.          WinPostMsg(hwnd, UM_RESIZE, 0L, 0L);
  480.          }
  481.       break;
  482.    case UM_RESIZE:
  483.       // Requested to resize the controls
  484.       // Query new size of the window
  485.       WinQueryWindowPos(hwnd, &newframe);
  486.       // Check whether the new size isn't too small
  487.       if (newframe.cx<initialframe.cx)
  488.          {
  489.          // window is to small, restore it to the initial size
  490.          // Copy the initial sizes to the newframe structure
  491.          // So we use the initial size, but the new position
  492.          newframe.cx=initialframe.cx;
  493.          // Make it so
  494.          WinSetWindowPos(hwnd, NULL, newframe.x, newframe.y, newframe.cx, newframe.cy,
  495.                          SWP_ACTIVATE | SWP_SHOW | SWP_SIZE | SWP_MOVE);
  496.          // And break out, we've finished the resize (and don't want to do more unneeded work)
  497.          break;
  498.          }
  499.       if (newframe.cy<initialframe.cy)
  500.          {
  501.          // window is to small, restore it to the initial size
  502.          // Copy the initial sizes to the newframe structure
  503.          // So we use the initial size, but the new position
  504.          newframe.cy=initialframe.cy;
  505.          // Make it so
  506.          WinSetWindowPos(hwnd, NULL, newframe.x, newframe.y, newframe.cx, newframe.cy,
  507.                          SWP_ACTIVATE | SWP_SHOW | SWP_SIZE | SWP_MOVE);
  508.          // And break out, we've finished the resize (and don't want to do more unneeded work)
  509.          break;
  510.          }
  511.       // Don't move around these, in this order they will be correctly repainted
  512.       // The MLE must be moved, and be resized if the width changes
  513.       ResizeX(hwnd, oldframe, newframe, MLE_TEXT);
  514.       // These controls we don't want to size, they have their proper size already, just have to move them
  515.       // to their new positions
  516.       // Move the static text with the font name
  517.       Move(hwnd, oldframe, newframe, ST_FONT);
  518.       // The value set will be resized in both directions
  519.       ResizeXY(hwnd, oldframe, newframe, SET_ASCII);
  520.       // Query the size of the window again, to store in oldframe
  521.       // (could have copied it from newframe too)
  522.       WinQueryWindowPos(hwnd, &oldframe);
  523.       WinPostMsg(hwnd, WM_PAINT, 0L, 0L);
  524.       break;
  525.    case UM_SETMENUSTATE:
  526.       // Set state of menu
  527.       WinSendMsg(WinWindowFromID(hwnd, FID_MENU), MM_SETITEMATTR, MPFROM2SHORT(SHORT1FROMMP(mp1), TRUE), mp2);
  528.       break;
  529.    case UM_ADDCPMENU:
  530.       AddCPMenu(hwnd);
  531.       menu=WinWindowFromID(hwnd, FID_MENU); // Vraag de handle naar het menu op
  532.       WinCheckMenuItem(menu, usCodePage+1000, TRUE);
  533.       break;
  534.    case UM_DIRECTTYPE:
  535.       break;
  536.    case WM_CLOSE:
  537.       // User has selected to close the program
  538.       // (Could ask for confirmation here)
  539.       // Save the size & position
  540.       WinQueryWindowPos(hwnd, &newframe);
  541.       sprintf(temp, "%li", newframe.cx);
  542.       PrfWriteProfileString(*pProfile, "Size", "x", temp);
  543.       sprintf(temp, "%li", newframe.cy);
  544.       PrfWriteProfileString(*pProfile, "Size", "y", temp);
  545.       sprintf(temp, "%li", newframe.x);
  546.       PrfWriteProfileString(*pProfile, "Position", "x", temp);
  547.       sprintf(temp, "%li", newframe.y);
  548.       PrfWriteProfileString(*pProfile, "Position", "y", temp);
  549.       // Save the font dialog values
  550.       PrfWriteProfileData(*pProfile, "FontAttr", "PointSize", &PointSize, sizeof(INT));
  551.       PrfWriteProfileData(*pProfile, "FontAttr", "fsSelection", &fd.fAttrs.fsSelection, sizeof(USHORT));
  552.       PrfWriteProfileData(*pProfile, "FontAttr", "lMatch", &fd.fAttrs.lMatch, sizeof(LONG));
  553.       PrfWriteProfileString(*pProfile, "FontAttr", "Facename", fd.fAttrs.szFacename);
  554.       PrfWriteProfileData(*pProfile, "FontAttr", "isRegistry", &fd.fAttrs.idRegistry, sizeof(USHORT));
  555.       PrfWriteProfileData(*pProfile, "FontAttr", "usCodePage", &fd.fAttrs.usCodePage, sizeof(USHORT));
  556.       PrfWriteProfileData(*pProfile, "FontAttr", "lMaxBaselineExt", &fd.fAttrs.lMaxBaselineExt, sizeof(LONG));
  557.       PrfWriteProfileData(*pProfile, "FontAttr", "fAveCharWidth", &fd.fAttrs.lAveCharWidth, sizeof(LONG));
  558.       PrfWriteProfileData(*pProfile, "FontAttr", "fsType", &fd.fAttrs.fsType, sizeof(USHORT));
  559.       PrfWriteProfileData(*pProfile, "FontAttr", "fsFontUse", &fd.fAttrs.fsFontUse, sizeof(USHORT));
  560.       // Dismiss the window
  561.       WinDismissDlg(hwnd, TRUE);
  562.       // Let the program know we are ready to quit
  563.       WinPostMsg(hwnd, WM_QUIT, 0L, 0L);
  564.       break;
  565.    default:
  566.       // We didn't handle the message, let the default window procedure handle it
  567.       return WinDefDlgProc(hwnd, msg, mp1, mp2);
  568.    }
  569. // We have handled all messages
  570. return (MRESULT)FALSE;
  571. }
  572.  
  573.  
  574. // Function to resize and move controls
  575. // Parameters:
  576. // hwnd Window handler of the main window
  577. // oldframe & newframe SWP structures which contain the old & new sizes of the main window
  578. //   used to calculate the new size and position of the client window
  579. // ID Resource ID of the client window
  580. void ResizeXY(HWND hwnd, SWP oldframe, SWP newframe, ULONG ID)
  581. {
  582. // structure to store the current position and size
  583. SWP swp;
  584. // Flags to resize and move
  585. ULONG flags=SWP_ACTIVATE | SWP_SHOW | SWP_SIZE | SWP_MOVE;
  586. // Query current position and size
  587. WinQueryWindowPos(WinWindowFromID(hwnd, ID), &swp);
  588. // Calculate the new position and size
  589. swp.cx=swp.cx+newframe.cx-oldframe.cx;
  590. swp.cy=swp.cy+newframe.cy-oldframe.cy;
  591. // Move and resize the control
  592. WinSetWindowPos(WinWindowFromID(hwnd, ID), NULL, swp.x, swp.y, swp.cx, swp.cy, flags);
  593. }
  594.