home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gbtn02.zip / ButtonTest.CPP < prev    next >
C/C++ Source or Header  |  1997-04-22  |  9KB  |  208 lines

  1. // Test program for the shaded button class
  2.  
  3. #define INCL_WIN
  4. #define INCL_GPI
  5. #include <os2.h>
  6. #include "Buttons.H"
  7. #include "ButtonTest.H"
  8.  
  9. typedef union {
  10.     ULONG Index;
  11.     struct {
  12.         UCHAR blue,green,red;
  13.         UCHAR dummy;
  14.     } rgb;
  15. } CLR;
  16.  
  17. MRESULT EXPENTRY ClientWndProc(HWND, ULONG, MPARAM, MPARAM);
  18. MRESULT EXPENTRY AboutDlgProc(HWND, ULONG, MPARAM, MPARAM);
  19.  
  20. int main() {
  21.         static UCHAR  szClientClass[] = "ButtonTest";
  22.         static ULONG flFrameFlags= FCF_TITLEBAR | FCF_SYSMENU | FCF_SIZEBORDER | FCF_MINMAX | FCF_SHELLPOSITION | FCF_TASKLIST;
  23.         HAB          hab;
  24.         HMQ          hmq;
  25.         HWND         hWndFrame, hWndClient;
  26.         QMSG         qmsg;
  27.  
  28.         hab = WinInitialize(0);
  29.         hmq = WinCreateMsgQueue(hab, 0);
  30.  
  31.         RegisterShadedButtonClass();
  32.         WinRegisterClass(hab, szClientClass, ClientWndProc, CS_SIZEREDRAW, 0);
  33.         hWndFrame = WinCreateStdWindow(HWND_DESKTOP, WS_VISIBLE, &flFrameFlags, szClientClass, NULL, 0L, 0, 0, &hWndClient);
  34.  
  35.         while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
  36.                 WinDispatchMsg(hab, &qmsg);
  37.  
  38.         WinDestroyWindow(hWndFrame);
  39.         WinDestroyMsgQueue(hmq);
  40.         WinTerminate(hab);
  41.         return 0;
  42. }
  43.  
  44. MRESULT EXPENTRY ClientWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2) {
  45.     static HWND hwndFrame, hwndButton[10];
  46.     static INT  cxClient, cyClient, cyChar;
  47.     static LONG ButtonWidth, ButtonHeight;
  48.     static CLR  buttonColour;
  49.     CHAR        szBuffer[32];
  50.     FONTMETRICS fm;
  51.     HAB         hab;
  52.     HPS         hps;
  53.     INT         id;
  54.     RECTL       rcl;
  55.  
  56.     switch(msg) {
  57.         case WM_CREATE:
  58.             hab = WinQueryAnchorBlock(hwnd);
  59.             hwndFrame = WinQueryWindow(hwnd, QW_PARENT);
  60.             hps = WinGetPS(hwnd);
  61.             GpiQueryFontMetrics(hps, sizeof(fm),&fm);
  62.             cyChar = fm.lMaxBaselineExt;
  63.             WinReleasePS(hps);
  64.             buttonColour.Index = 0x00CCCCCC;
  65.  
  66. // Buttons should be big enough to hold the pointer, with a 4 pixel border
  67.             ButtonWidth = WinQuerySysValue(HWND_DESKTOP, SV_CXPOINTER) + 4;
  68.             ButtonHeight = WinQuerySysValue(HWND_DESKTOP, SV_CYPOINTER) + 4;
  69.             hwndButton[0] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "100", WS_VISIBLE | GBT_NORIGHTLINE | GBT_GRAPHIC, 0, 0, ButtonWidth, ButtonHeight, hwnd, HWND_BOTTOM, 0, NULL, NULL);
  70.             hwndButton[1] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "101", WS_VISIBLE | GBT_NOLEFTLINE | GBT_GRAPHIC, 0, 0, ButtonWidth, ButtonHeight, hwnd, HWND_BOTTOM, 1, NULL, NULL);
  71.  
  72. // This is the status bar
  73.             hwndButton[2] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "Status Bar", WS_VISIBLE | GBT_NOPUSH, 0, 0, 0, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 2, NULL, NULL);
  74.             sprintf(szBuffer, "Red: %d Green: %d Blue: %d", buttonColour.rgb.red, buttonColour.rgb.green, buttonColour.rgb.blue);
  75.             WinSetWindowText(hwndButton[2], szBuffer);
  76.  
  77. // Buttons to change the colour of the about button
  78.             hwndButton[3] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "<", WS_VISIBLE | GBT_NORIGHTLINE, 0, 0, ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 3, NULL, NULL);
  79.             hwndButton[4] = WinCreateWindow(hwnd, SHADEDBTNCLASS, ">", WS_VISIBLE | GBT_NOLEFTLINE, 0, 0, ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 4, NULL, NULL);
  80.             hwndButton[5] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "<", WS_VISIBLE | GBT_NORIGHTLINE, 0, 0, ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 5, NULL, NULL);
  81.             hwndButton[6] = WinCreateWindow(hwnd, SHADEDBTNCLASS, ">", WS_VISIBLE | GBT_NOLEFTLINE, 0, 0, ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 6, NULL, NULL);
  82.             hwndButton[7] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "<", WS_VISIBLE | GBT_NORIGHTLINE, 0, 0, ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 7, NULL, NULL);
  83.             hwndButton[8] = WinCreateWindow(hwnd, SHADEDBTNCLASS, ">", WS_VISIBLE | GBT_NOLEFTLINE, 0, 0, ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 8, NULL, NULL);
  84. // info about this program
  85.             hwndButton[9] = WinCreateWindow(hwnd, SHADEDBTNCLASS, "About", WS_VISIBLE | GBT_RIGHTROUND | GBT_LEFTROUND, 0, 0, 2 * ButtonWidth, cyChar * 5 / 4, hwnd, HWND_BOTTOM, 9, NULL, NULL);
  86.  
  87. // change the colour of the buttons
  88.             WinSendMsg(hwndButton[3], GBM_SETCOLOR, MPFROMLONG(0x00FFCCCC), NULL);
  89.             WinSendMsg(hwndButton[4], GBM_SETCOLOR, MPFROMLONG(0x00FFCCCC), NULL);
  90.             WinSendMsg(hwndButton[5], GBM_SETCOLOR, MPFROMLONG(0x00CCFFCC), NULL);
  91.             WinSendMsg(hwndButton[6], GBM_SETCOLOR, MPFROMLONG(0x00CCFFCC), NULL);
  92.             WinSendMsg(hwndButton[7], GBM_SETCOLOR, MPFROMLONG(0x00CCCCFF), NULL);
  93.             WinSendMsg(hwndButton[8], GBM_SETCOLOR, MPFROMLONG(0x00CCCCFF), NULL);
  94.             WinSendMsg(hwndButton[9], GBM_SETCOLOR, MPFROMLONG(buttonColour.Index), NULL);
  95.             return 0;
  96.         case WM_SIZE:
  97.             hps = WinGetPS(hwnd);
  98.             GpiQueryFontMetrics(hps, sizeof(fm),&fm);
  99.             cyChar = fm.lMaxBaselineExt;
  100.             WinReleasePS(hps);
  101.             cxClient = SHORT1FROMMP(mp2);
  102.             cyClient = SHORT2FROMMP(mp2);
  103. // Center the buttons
  104.             for(id = 0; id < 2; id++)
  105.                 WinSetWindowPos(hwndButton[id], NULLHANDLE, (cxClient >> 1) + ButtonWidth * (id - 1), (cyClient - ButtonHeight) >> 1, 0, 0, SWP_MOVE);
  106.             for(id = 3; id < 9; id++)
  107.                 WinSetWindowPos(hwndButton[id], NULLHANDLE, (cxClient >> 1) + ButtonWidth * (-(id % 2)), ((cyClient + ButtonHeight) >> 1) + (3 - (id-1) / 2) * cyChar * 5 / 4, 0, 0, SWP_MOVE);
  108.             WinSetWindowPos(hwndButton[9], NULLHANDLE, (cxClient >> 1) - ButtonWidth, ((cyClient - ButtonHeight) >> 1) - cyChar * 15 / 8, 0, 0, SWP_MOVE);
  109. // make the status bar span the width of the window
  110.             WinSetWindowPos(hwndButton[2], NULLHANDLE, 0, 0, cxClient, cyChar * 5 / 4, SWP_SIZE);
  111.  
  112. // Enable "Smaller" button if window is large enough for the buttons
  113.             if (!WinIsWindowEnabled(hwndButton[0]) && ((9 * cxClient / 10) >= (2 * ButtonWidth)) && ((9  * cyClient / 10) >= (ButtonHeight + cyChar * 15 / 2)))
  114.                 WinEnableWindow(hwndButton[0], TRUE);
  115. // Disable "Smaller" button if window is too small for the buttons
  116.             if (WinIsWindowEnabled(hwndButton[0]) && (((9 * cxClient / 10) < (2 * ButtonWidth)) || ((9  * cyClient / 10) < (ButtonHeight + cyChar * 15 / 2))))
  117.                 WinEnableWindow(hwndButton[0], FALSE);
  118.             return 0;
  119.         case WM_COMMAND:
  120.             WinQueryWindowRect(hwnd, &rcl);
  121.             WinMapWindowPoints(hwnd, HWND_DESKTOP, (PPOINTL) &rcl, 2);
  122.  
  123.             switch(COMMANDMSG(&msg)->cmd) {
  124.                 case 0: // "Smaller"
  125.                     rcl.xLeft   += cxClient / 20;
  126.                     rcl.xRight  -= cxClient / 20;
  127.                     rcl.yBottom += cyClient / 20;
  128.                     rcl.yTop    -= cyClient / 20;
  129.                     break;
  130.                 case 1: // "Larger"
  131.                     rcl.xLeft   -= cxClient / 20;
  132.                     rcl.xRight  += cxClient / 20;
  133.                     rcl.yBottom -= cyClient / 20;
  134.                     rcl.yTop    += cyClient / 20;
  135.                     break;
  136.                 case 3: // change red
  137.                     buttonColour.rgb.red--;
  138.                     if (buttonColour.rgb.red == 0)
  139.                         WinEnableWindow(hwndButton[3], FALSE);
  140.                     if (!WinIsWindowEnabled(hwndButton[4]))
  141.                         WinEnableWindow(hwndButton[4], TRUE);
  142.                     break;
  143.                 case 4:
  144.                     buttonColour.rgb.red++;
  145.                     if (buttonColour.rgb.red == 255)
  146.                         WinEnableWindow(hwndButton[4], FALSE);
  147.                     if (!WinIsWindowEnabled(hwndButton[3]))
  148.                         WinEnableWindow(hwndButton[3], TRUE);
  149.                     break;
  150.                 case 5: // change green
  151.                     buttonColour.rgb.green--;
  152.                     if (buttonColour.rgb.green == 0)
  153.                         WinEnableWindow(hwndButton[5], FALSE);
  154.                     if (!WinIsWindowEnabled(hwndButton[6]))
  155.                         WinEnableWindow(hwndButton[6], TRUE);
  156.                     break;
  157.                 case 6:
  158.                     buttonColour.rgb.green++;
  159.                     if (buttonColour.rgb.green == 255)
  160.                         WinEnableWindow(hwndButton[6], FALSE);
  161.                     if (!WinIsWindowEnabled(hwndButton[5]))
  162.                         WinEnableWindow(hwndButton[5], TRUE);
  163.                     break;
  164.                 case 7:// change blue
  165.                     buttonColour.rgb.blue--;
  166.                     if (buttonColour.rgb.blue == 0)
  167.                         WinEnableWindow(hwndButton[7], FALSE);
  168.                     if (!WinIsWindowEnabled(hwndButton[8]))
  169.                         WinEnableWindow(hwndButton[8], TRUE);
  170.                     break;
  171.                 case 8:
  172.                     buttonColour.rgb.blue++;
  173.                     if (buttonColour.rgb.blue == 255)
  174.                         WinEnableWindow(hwndButton[8], FALSE);
  175.                     if (!WinIsWindowEnabled(hwndButton[7]))
  176.                         WinEnableWindow(hwndButton[7], TRUE);
  177.                     break;
  178.                 case 9:
  179.                     WinDlgBox(HWND_DESKTOP, hwnd, AboutDlgProc, NULLHANDLE, IDD_ABOUT, NULL);
  180.                     return 0;
  181.             }
  182.             sprintf(szBuffer, "Red: %d Green: %d Blue: %d", buttonColour.rgb.red, buttonColour.rgb.green, buttonColour.rgb.blue);
  183.             WinSetWindowText(hwndButton[2], szBuffer);
  184.             WinCalcFrameRect(hwndFrame, &rcl, FALSE);
  185.             WinSetWindowPos(hwndFrame, NULLHANDLE, rcl.xLeft, rcl.yBottom, rcl.xRight - rcl.xLeft, rcl.yTop - rcl.yBottom, SWP_MOVE | SWP_SIZE);
  186.             WinPostMsg(hwndButton[9], GBM_SETCOLOR, MPFROMLONG(buttonColour.Index), NULL);
  187.             return 0;
  188.         case WM_ERASEBACKGROUND:
  189.             WinFillRect((HPS)LONGFROMMP(mp1), PVOIDFROMMP(mp2), CLR_PALEGRAY);
  190.             return 0;
  191.     }
  192.     return WinDefWindowProc(hwnd, msg, mp1, mp2);
  193. }
  194.  
  195.  
  196. MRESULT EXPENTRY AboutDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2) {
  197.     switch(msg) {
  198.         case WM_COMMAND:
  199.             switch(COMMANDMSG(&msg)->cmd) {
  200.                 case DID_OK:
  201.                     WinDismissDlg(hwnd, TRUE);
  202.                     return 0;
  203.             }
  204.             break;
  205.     }
  206.     return WinDefDlgProc(hwnd, msg, mp1, mp2);
  207. }
  208.