home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / accel / accel.c next >
Encoding:
C/C++ Source or Header  |  1990-07-06  |  7.9 KB  |  259 lines

  1. /*
  2.  *  ACCEL.C -- Sample demonstrating calls included with INCL_WINACCELERATORS
  3.  *
  4.  *  Overview:
  5.  *    Accelerators are used to reduce the number of keystrokes needed to
  6.  *  execute a command (hence "accelerating" a user's processing time)
  7.  *
  8.  *  Strategy:
  9.  *    This application allows the user to experiment with various settings,
  10.  *  by popping up a dialog box in which the user can specify an accelerator.
  11.  *  One possible modification to this program is to have the user hit the
  12.  *  desired key sequence, and to use KbdCharIn() to figure out what the key
  13.  *  sequence is, and then set the accelerator.  Another is to implement the
  14.  *  "Delete" operation, by perhaps listing the accelerators in a list box.
  15.  *  This wasn't done primarily because that would require reorganization
  16.  *  (compression) of the accelerator table:  it could not be easily done with
  17.  *  a WinDeleteAccel call (because such a call does not exist).
  18.  */
  19. #define INCL_WINACCELERATORS
  20. #define    INCL_WINBUTTONS            // Needed for checkboxes in dialogs
  21. #define    INCL_WINDIALOGS
  22. #define    INCL_WINMESSAGEMGR
  23. #define    INCL_WINFRAMEMGR        // for SC_MINIMIZE constant
  24. #define INCL_WINWINDOWMGR
  25. #include <os2.h>
  26.  
  27. #include <malloc.h>            // Needed for dynamic memory allocation
  28. #include <stdio.h>            // Needed for sscanf() call
  29. #include "accel.h"            // Needed for resource IDs
  30. /*
  31.  * Globals
  32.  */
  33. char    ach[8];             // Temporary:  used to store Key: field
  34. char    szAppName[]    = "ACCEL.EXE";
  35. char    szClassName[]    = "Accelerator";
  36. char    szMessage[]    = " - Accelerator Table Example";
  37. int    cbSize;             // Size of Accel. Table in bytes
  38. int    iTemp;                // Used to store Key: value, if number
  39. void    *pTemp;             // Used so free() won't give warnings
  40. HAB    hab;
  41. HACCEL    haccSystem;            // Handle to system accelerator table
  42. HACCEL    haccTable;            // Handle to app-local acceltable
  43. HMQ     hmqAccel;
  44. HWND    hwndAccel;            // Client window
  45. HWND    hwndAccelFrame;         // Frame window
  46. PACCELTABLE    pacctTable;        // Points to table with ACCEL entries
  47. /*
  48.     Macros
  49. */
  50. #define Message(s) WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, s, \
  51.             szAppName, 0, MB_OK | MB_ICONEXCLAMATION)
  52. #define    Check(b)   WinSendDlgItemMsg(hwndDlg, b, \
  53.             BM_SETCHECK, MPFROMSHORT(1), 0L)
  54. #define Checked(b) WinSendDlgItemMsg(hwndDlg, b, BM_QUERYCHECK, 0L, 0L)
  55. /*
  56.     Internals
  57. */
  58. BOOL InitializeAccelTable(void);
  59. /*
  60.  * Main routine...initializes window and message queue
  61.  */
  62. void cdecl main(void) {
  63.     QMSG qmsg;
  64.     ULONG ctldata;
  65.  
  66.     /* Initialize a PM application */
  67.     hab = WinInitialize(0);
  68.     hmqAccel = WinCreateMsgQueue(hab, 0);
  69.  
  70.     /* Register the main window's class */
  71.     if (!WinRegisterClass(hab, szClassName, AccelWndProc, CS_SIZEREDRAW, 0))
  72.         return;
  73.     /*
  74.     Create the window
  75.     We create it without an accelerator table, but we'll load one later
  76.     */
  77.     ctldata = FCF_STANDARD & ~FCF_ACCELTABLE;
  78.     hwndAccelFrame = WinCreateStdWindow(HWND_DESKTOP, WS_VISIBLE, &ctldata,
  79.     szClassName, szMessage, WS_VISIBLE, (HMODULE) NULL, IDR_ACCEL, &hwndAccel);
  80.     WinShowWindow(hwndAccelFrame, TRUE);
  81.     /*
  82.     Load the accelerator tables
  83.     */
  84.     if (!InitializeAccelTable()) {
  85.     Message("Accelerator table not initialized!");
  86.     return;
  87.     }
  88.  
  89.     /* Poll messages from event queue */
  90.     while(WinGetMsg(hab, (PQMSG)&qmsg, (HWND)NULL, 0, 0))
  91.         WinDispatchMsg(hab, (PQMSG)&qmsg);
  92.  
  93.     /* Clean up */
  94.     if (!WinDestroyAccelTable(haccTable))
  95.     Message("Could not destroy ACCELTABLE");
  96.     WinDestroyWindow(hwndAccelFrame);
  97.     WinDestroyMsgQueue(hmqAccel);
  98.     WinTerminate(hab);
  99. }
  100.  
  101. MRESULT CALLBACK AccelWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2) {
  102. /*
  103.  * This routine processes WM_PAINT.  It passes
  104.  * everything else to the Default Window Procedure.
  105.  */
  106.     HPS        hPS;
  107.     RECTL    rcl;
  108.  
  109.     switch (msg) {
  110.  
  111.     case WM_HELP:
  112.         /* If WM_HELP, pop up Help dialog box */
  113.         WinDlgBox(HWND_DESKTOP, hwnd, AboutDlgProc, (HMODULE) NULL, IDD_HELP, NULL);
  114.         break;
  115.  
  116.     case WM_COMMAND:
  117.         switch (COMMANDMSG(&msg)->cmd) {
  118.  
  119.         /* On most WM_COMMAND messages, give the About... box */
  120.         case IDM_ABOUT:
  121.             WinDlgBox(HWND_DESKTOP, hwnd, AboutDlgProc,
  122.                   (HMODULE) NULL, IDD_ABOUT, NULL);
  123.             break;
  124.  
  125.         /* Create your own accelerator dialog */
  126.         case IDM_CREATE:
  127.             WinDlgBox(HWND_DESKTOP, hwnd, CreateDlgProc,
  128.                   (HMODULE) NULL, IDD_CREATE, NULL);
  129.  
  130.         default: break;
  131.         }
  132.         break;
  133.  
  134.     case WM_PAINT:
  135.         /* Open the presentation space */
  136.         hPS = WinBeginPaint(hwnd, NULL, &rcl);
  137.  
  138.         /* Fill the background with Dark Blue */
  139.         WinFillRect(hPS, &rcl, CLR_DARKBLUE);
  140.  
  141.         /* Finish painting */
  142.         WinEndPaint(hPS);
  143.         break;
  144.  
  145.     default: return WinDefWindowProc(hwnd, msg, mp1, mp2); break;
  146.     }
  147.     return 0L;
  148. }
  149.  
  150. MRESULT CALLBACK AboutDlgProc(hwndDlg, msg, mp1, mp2)
  151. /*
  152.     About... dialog procedure
  153. */
  154. HWND hwndDlg;
  155. USHORT msg;
  156. MPARAM mp1;
  157. MPARAM mp2;
  158. {
  159.     switch(msg) {
  160.     case WM_COMMAND:
  161.         switch(COMMANDMSG(&msg)->cmd) {
  162.         case DID_OK: WinDismissDlg(hwndDlg, TRUE);
  163.         default: break;
  164.         }
  165.     default: return WinDefDlgProc(hwndDlg, msg, mp1, mp2);
  166.     }
  167.     return FALSE;
  168. }
  169.  
  170. MRESULT CALLBACK CreateDlgProc(hwndDlg, msg, mp1, mp2)
  171. /*
  172.     Create Accelerator dialog procedure
  173. */
  174. HWND hwndDlg;
  175. USHORT msg;
  176. MPARAM mp1;
  177. MPARAM mp2;
  178. {
  179.     switch(msg) {
  180.     case WM_INITDLG:
  181.         /* Set the defaults */
  182.         Check(IDD_CHAR); Check(IDD_CMD);
  183.         break;
  184.  
  185.     case WM_COMMAND:
  186.         switch(COMMANDMSG(&msg)->cmd) {
  187.         case DID_OK:
  188.             /* Get the accelerator table (allocate an extra space) */
  189.             cbSize = WinCopyAccelTable(haccTable, NULL, 0);
  190.             pTemp = (void *) malloc(cbSize + sizeof(ACCEL));
  191.             pacctTable = (PACCELTABLE) pTemp;
  192.             cbSize = WinCopyAccelTable(haccTable, pacctTable, cbSize);
  193.             
  194. #define accNew    pacctTable->aaccel[pacctTable->cAccel]
  195.  
  196.             /*
  197.             Command:
  198.                 if SYSCOMMAND, make the window minimize.
  199.                 if HELP, we'll pop up a dialog box.
  200.                 otherwise, pop up the About... dialog box.
  201.             */
  202.             if (Checked(IDD_SYSCMD)) accNew.cmd = SC_MINIMIZE;
  203.             else accNew.cmd = IDM_ABOUT;
  204.  
  205.             /* Get the states from the dialog box */
  206.             accNew.fs = 0;
  207.             if (Checked(IDD_ALT))    accNew.fs |= AF_ALT;
  208.             if (Checked(IDD_CHAR))    accNew.fs |= AF_CHAR;
  209.             if (Checked(IDD_CONTROL))    accNew.fs |= AF_CONTROL;
  210.             if (Checked(IDD_FHELP))    accNew.fs |= AF_HELP;
  211.             if (Checked(IDD_LONEKEY))    accNew.fs |= AF_LONEKEY;
  212.             if (Checked(IDD_SCANCODE))    accNew.fs |= AF_SCANCODE;
  213.             if (Checked(IDD_SHIFT))    accNew.fs |= AF_SHIFT;
  214.             if (Checked(IDD_SYSCMD))    accNew.fs |= AF_SYSCOMMAND;
  215.             if (Checked(IDD_VKEY))    accNew.fs |= AF_VIRTUALKEY;
  216.  
  217.             /* Get the key to be defined */
  218.             WinQueryDlgItemText(hwndDlg, IDD_ENTRY, 8, ach);
  219.             if (('0' <= ach[0]) && (ach[0] <= '9')) {
  220.             sscanf(ach, "%i", &iTemp);
  221.             accNew.key = (USHORT) iTemp;
  222.             }
  223.             else accNew.key = (USHORT) ach[0];
  224.  
  225.             /* Increment the count of accelerator records */
  226.             pacctTable->cAccel++;
  227.  
  228.             /* Cleanup, then create a new accelerator table */
  229.             WinDestroyAccelTable(haccTable);
  230.             haccTable = WinCreateAccelTable(hab, pacctTable);
  231.  
  232.             /* Set the new accelerator table, and clean up */
  233.             WinSetAccelTable(hab, haccTable, hwndAccelFrame);
  234.             free(pTemp);
  235.  
  236.         case DID_CANCEL:
  237.             WinDismissDlg(hwndDlg, TRUE);
  238.  
  239.         default: break;
  240.         }
  241.     default: return WinDefDlgProc(hwndDlg, msg, mp1, mp2);
  242.     }
  243.     return FALSE;
  244. }
  245.  
  246. BOOL InitializeAccelTable(void) {
  247.     /*
  248.     Initialize the accelerator table by loading it from the
  249.     resource file.    Note that you can load an accelerator
  250.     table from a DLL, if you change the NULL parameter.
  251.     The system accelerator table is accessible after this
  252.     call:  one possible use for this would be a List...
  253.     dialog box, which would list all system & app. accelerators.
  254.     */
  255.     haccSystem = WinQueryAccelTable(hab, NULL);
  256.     haccTable = WinLoadAccelTable(hab, 0, IDR_ACCEL);
  257.     return WinSetAccelTable(hab, haccTable, hwndAccelFrame);
  258. }
  259.