home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / imagedit / toolbox.c < prev    next >
Text File  |  1996-06-12  |  17KB  |  522 lines

  1.     /****************************************************************************/
  2.     /*                                                                          */
  3.     /*                 Copyright (C) 1987-1996 Microsoft Corp.                */
  4.     /*                           All Rights Reserved                            */
  5.     /*                                                                          */
  6.     /****************************************************************************/
  7.     /****************************** Module Header *******************************
  8.     * Module Name: toolbox.c
  9.     *
  10.     * Contains routines that handle the toolbox.
  11.     *
  12.     * History:
  13.     *
  14.     * 04/30/91 - Copied from DlgEdit.
  15.     *
  16.     ****************************************************************************/
  17.     
  18.     #include "imagedit.h"
  19.     #include "dialogs.h"
  20.  
  21.      #include <windowsx.h>
  22.     
  23.     #define TOOLBOXCOLUMNS  2       // Columns in the Toolbox.
  24.     
  25.     /*
  26.      * Style of the toolbox window.
  27.      */
  28.     #define TOOLBOXSTYLE    (WS_POPUP | WS_CLIPSIBLINGS | WS_CAPTION | WS_SYSMENU)
  29.     
  30.     
  31.     STATICFN VOID NEAR ToolboxDrawBitmap(HDC hDC, INT tool);
  32.     
  33.     
  34.     /*
  35.      * Dimensions of a tool button bitmap.
  36.      */
  37.     static INT cxToolBtn;
  38.     static INT cyToolBtn;
  39.     
  40.     /*
  41.      * Index to the last available tool.  If tools at the end of the
  42.      * toolbox are disabled, this number gets lowered.
  43.      */
  44.     static INT iToolLast = CTOOLS - 1;
  45.     
  46.     
  47.     
  48.     /****************************************************************************
  49.     * ToolboxCreate
  50.     *
  51.     * This function creates the toolbox window.
  52.     *
  53.     * History:
  54.     *
  55.     ****************************************************************************/
  56.     
  57.     VOID ToolboxCreate(VOID)
  58.     {
  59.         BITMAP bmp;
  60.         INT i;
  61.         INT x;
  62.         INT y;
  63.         INT cx;
  64.         INT cy;
  65.         INT cxDummy;
  66.         INT cyDummy;
  67.         RECT rc;
  68.         RECT rcClient;
  69.         POINT pt;
  70.         BOOL fMaximized;
  71.     
  72.         /*
  73.          * Load the bitmaps.
  74.          */
  75.         for (i = 0; i < CTOOLS; i++) {
  76.             if (!(gaTools[i].hbmToolBtnUp = LoadBitmap(ghInst,
  77.                     MAKEINTRESOURCE(gaTools[i].idbmToolBtnUp))))
  78.                 return;
  79.     
  80.             if (!(gaTools[i].hbmToolBtnDown = LoadBitmap(ghInst,
  81.                     MAKEINTRESOURCE(gaTools[i].idbmToolBtnDown))))
  82.                 return;
  83.         }
  84.     
  85.         /*
  86.          * Get the dimensions of the tool button bitmaps.
  87.          */
  88.         GetObject(gaTools[0].hbmToolBtnUp, sizeof(BITMAP), (PSTR)&bmp);
  89.         cxToolBtn = bmp.bmWidth;
  90.         cyToolBtn = bmp.bmHeight;
  91.     
  92.         /*
  93.          * Calculate the required window size for the client area
  94.          * size we want.  The size leaves room for a margin, and
  95.          * assumes that adjacent buttons overlap their borders by
  96.          * one pixel.
  97.          */
  98.         rc.left = 0;
  99.         rc.top = 0;
  100.         rc.right = PALETTEMARGIN + ((cxToolBtn - 1) * 2) + 1 + PALETTEMARGIN;
  101.         rc.bottom = PALETTEMARGIN + ((cyToolBtn - 1) *
  102.                 (CTOOLS / 2)) + 1 + PALETTEMARGIN;
  103.         AdjustWindowRect(&rc, TOOLBOXSTYLE, FALSE);
  104.         cx = rc.right - rc.left;
  105.         cy = rc.bottom - rc.top;
  106.     
  107.         /*
  108.          * Get the saved position of the Toolbox.  Note that we throw away
  109.          * the size fields, because we just calculated the required size.
  110.          */
  111.         if (!ReadWindowPos(szTBPos, &x, &y, &cxDummy, &cyDummy, &fMaximized)) {
  112.             /*
  113.              * The previous position of the Toolbox couldn't be found.
  114.              * Position the toolbox to the upper right corner of the
  115.              * client area of the editor, but make sure it is completely
  116.              * visible.
  117.              */
  118.             GetClientRect(ghwndMain, &rcClient);
  119.             pt.x = rcClient.right - cx - (2 * PALETTEMARGIN);
  120.             pt.y = rcClient.top + gcyPropBar + (2 * PALETTEMARGIN);
  121.             ClientToScreen(ghwndMain, &pt);
  122.             SetRect(&rc, pt.x, pt.y, pt.x + cx, pt.y + cy);
  123.             FitRectToScreen(&rc);
  124.             x = rc.left;
  125.             y = rc.top;
  126.         }
  127.     
  128.         if (!(ghwndToolbox = CreateWindow(szToolboxClass, NULL, TOOLBOXSTYLE,
  129.                 x, y, cx, cy, ghwndMain, NULL, ghInst, NULL)))
  130.             return;
  131.     
  132.         /*
  133.          * Create the buttons.
  134.          */
  135.         x = PALETTEMARGIN;
  136.         y = PALETTEMARGIN;
  137.         for (i = 0; i < CTOOLS; i++) {
  138.             CreateWindow(szToolBtnClass, NULL,
  139.                     WS_CHILD | WS_VISIBLE,
  140.                     x, y, cxToolBtn, cyToolBtn,
  141.                     ghwndToolbox, (HMENU)i, ghInst, NULL);
  142.     
  143.             if (x == PALETTEMARGIN) {
  144.                 x += cxToolBtn - 1;
  145.             }
  146.             else {
  147.                 x = PALETTEMARGIN;
  148.                 y += cyToolBtn - 1;
  149.             }
  150.         }
  151.     
  152.         ToolboxUpdate();
  153.     }
  154.     
  155.     
  156.     
  157.     /****************************************************************************
  158.     * ToolboxShow
  159.     *
  160.     * This function shows or hides the toolbox window.
  161.     *
  162.     * History:
  163.     *
  164.     ****************************************************************************/
  165.     
  166.     VOID ToolboxShow(
  167.         BOOL fShow)
  168.     {
  169.         if (fShow)
  170.             ShowWindow(ghwndToolbox, SW_SHOWNA);
  171.         else
  172.             ShowWindow(ghwndToolbox, SW_HIDE);
  173.     }
  174.     
  175.     
  176.     
  177.     /****************************************************************************
  178.     * ToolboxUpdate
  179.     *
  180.     * This function updates the toolbox.  It should be called any time that
  181.     * a new file is opened/created for editing.
  182.     *
  183.     * History:
  184.     *
  185.     ****************************************************************************/
  186.     
  187.     VOID ToolboxUpdate(VOID)
  188.     {
  189.         if (!ghwndToolbox)
  190.             return;
  191.     
  192.         if (giType == FT_CURSOR) {
  193.             ShowWindow(GetDlgItem(ghwndToolbox, TOOL_HOTSPOT), SW_SHOWNA);
  194.             iToolLast = CTOOLS - 1;
  195.         }
  196.         else {
  197.             ShowWindow(GetDlgItem(ghwndToolbox, TOOL_HOTSPOT), SW_HIDE);
  198.             iToolLast = TOOL_HOTSPOT - 1;
  199.             if (gCurTool == TOOL_HOTSPOT)
  200.                 ToolboxSelectTool(TOOL_FIRST);
  201.         }
  202.     }
  203.     
  204.     
  205.     
  206.     /****************************************************************************
  207.     * ToolboxWndProc
  208.     *
  209.     * This is the window procedure for the toolbox window.
  210.     *
  211.     * History:
  212.     *
  213.     ****************************************************************************/
  214.     
  215.     WINDOWPROC ToolboxWndProc(
  216.         HWND hwnd,
  217.         UINT msg,
  218.         WPARAM wParam,
  219.         LPARAM lParam)
  220.     {
  221.         switch (msg) {
  222.             case WM_CREATE:
  223.                 {
  224.                     HMENU hmenu = GetSystemMenu(hwnd, FALSE);
  225.     
  226.                     RemoveMenu(hmenu, 7, MF_BYPOSITION);    // Second separator.
  227.                     RemoveMenu(hmenu, 5, MF_BYPOSITION);    // First separator.
  228.     
  229.                     RemoveMenu(hmenu, SC_RESTORE, MF_BYCOMMAND);
  230.                     RemoveMenu(hmenu, SC_SIZE, MF_BYCOMMAND);
  231.                     RemoveMenu(hmenu, SC_MINIMIZE, MF_BYCOMMAND);
  232.                     RemoveMenu(hmenu, SC_MAXIMIZE, MF_BYCOMMAND);
  233.                     RemoveMenu(hmenu, SC_TASKLIST, MF_BYCOMMAND);
  234.                 }
  235.     
  236.                 return 0;
  237.     
  238.             case WM_KEYDOWN:
  239.                 {
  240.                     INT iToolNext;
  241.     
  242.                     switch (wParam) {
  243.                         case VK_UP:
  244.                             if ((GetKeyState(VK_SHIFT) & 0x8000) ||
  245.                                     (GetKeyState(VK_CONTROL) & 0x8000))
  246.                                 break;
  247.     
  248.                             /*
  249.                              * Go up a row, but don't go beyond the top.
  250.                              */
  251.                             iToolNext = gCurTool - TOOLBOXCOLUMNS;
  252.                             if (iToolNext < 0)
  253.                                 break;
  254.     
  255.                             ToolboxSelectTool(iToolNext);
  256.     
  257.                             break;
  258.     
  259.                         case VK_DOWN:
  260.                             if ((GetKeyState(VK_SHIFT) & 0x8000) ||
  261.                                     (GetKeyState(VK_CONTROL) & 0x8000))
  262.                                 break;
  263.     
  264.                             /*
  265.                              * Go down a row, but don't go beyond the bottom.
  266.                              */
  267.                             iToolNext = gCurTool + TOOLBOXCOLUMNS;
  268.                             if (iToolNext > iToolLast)
  269.                                 break;
  270.     
  271.                             ToolboxSelectTool(iToolNext);
  272.     
  273.                             break;
  274.     
  275.                         case VK_LEFT:
  276.                             if ((GetKeyState(VK_SHIFT) & 0x8000) ||
  277.                                     (GetKeyState(VK_CONTROL) & 0x8000))
  278.                                 break;
  279.     
  280.                             /*
  281.                              * Already at left edge.
  282.                              */
  283.                             if (!(gCurTool % TOOLBOXCOLUMNS))
  284.                                 break;
  285.     
  286.                             /*
  287.                              * Go left a column.
  288.                              */
  289.                             ToolboxSelectTool(gCurTool - 1);
  290.     
  291.                             break;
  292.     
  293.                         case VK_RIGHT:
  294.                             if ((GetKeyState(VK_SHIFT) & 0x8000) ||
  295.                                     (GetKeyState(VK_CONTROL) & 0x8000))
  296.                                 break;
  297.     
  298.                             /*
  299.                              * Already at right edge.
  300.                              */
  301.                             if ((gCurTool % TOOLBOXCOLUMNS) == TOOLBOXCOLUMNS - 1)
  302.                                 break;
  303.     
  304.                             /*
  305.                              * Don't go off the end of the available tools.
  306.                              */
  307.                             if (gCurTool + 1 > iToolLast)
  308.                                 break;
  309.     
  310.                             /*
  311.                              * Go right a column.
  312.                              */
  313.                             ToolboxSelectTool(gCurTool + 1);
  314.     
  315.                             break;
  316.     
  317.                         case VK_TAB:
  318.                             if (GetKeyState(VK_CONTROL) & 0x8000)
  319.                                 break;
  320.     
  321.                             /*
  322.                              * Is the shift key pressed also?
  323.                              */
  324.                             if (GetKeyState(VK_SHIFT) & 0x8000) {
  325.                                 if (gCurTool == TOOL_FIRST)
  326.                                     iToolNext = iToolLast;
  327.                                 else
  328.                                     iToolNext = gCurTool - 1;
  329.                             }
  330.                             else {
  331.                                 if (gCurTool == iToolLast)
  332.                                     iToolNext = TOOL_FIRST;
  333.                                 else
  334.                                     iToolNext = gCurTool + 1;
  335.                             }
  336.     
  337.                             ToolboxSelectTool(iToolNext);
  338.     
  339.                             break;
  340.     
  341.                         case VK_END:
  342.                             if ((GetKeyState(VK_SHIFT) & 0x8000) ||
  343.                                     (GetKeyState(VK_CONTROL) & 0x8000))
  344.                                 break;
  345.     
  346.                             ToolboxSelectTool(iToolLast);
  347.     
  348.                             break;
  349.     
  350.                         case VK_HOME:
  351.                         case VK_ESCAPE:
  352.                             if ((GetKeyState(VK_SHIFT) & 0x8000) ||
  353.                                     (GetKeyState(VK_CONTROL) & 0x8000))
  354.                                 break;
  355.     
  356.                             ToolboxSelectTool(TOOL_FIRST);
  357.     
  358.                             break;
  359.                     }
  360.                 }
  361.     
  362.                 break;
  363.     
  364.             case WM_ACTIVATE:
  365.                 if (GET_WM_ACTIVATE_STATE(wParam, lParam))
  366.                     gidCurrentDlg = DID_TOOLBOX;
  367.     
  368.                 break;
  369.     
  370.             case  WM_PAINT:
  371.                 {
  372.                     HDC hdc;
  373.                     PAINTSTRUCT ps;
  374.     
  375.                     hdc = BeginPaint(hwnd, &ps);
  376.                     DrawMarginBorder(hwnd, hdc);
  377.                     EndPaint(hwnd, &ps);
  378.                 }
  379.     
  380.                 break;
  381.     
  382.             case WM_CLOSE:
  383.                 /*
  384.                  * The user closed the toolbox from the system menu.
  385.                  * Hide the toolbox (we don't actually destroy it so
  386.                  * that it will appear in the same spot when they show
  387.                  * it again).
  388.                  */
  389.                 ToolboxShow(FALSE);
  390.                 gfShowToolbox = FALSE;
  391.                 break;
  392.     
  393.             case WM_DESTROY:
  394.                 {
  395.                     INT i;
  396.                     RECT rc;
  397.     
  398.                     for (i = 0; i < CTOOLS; i++) {
  399.                         DeleteObject(gaTools[i].hbmToolBtnUp);
  400.                         gaTools[i].hbmToolBtnUp = NULL;
  401.                         DeleteObject(gaTools[i].hbmToolBtnDown);
  402.                         gaTools[i].hbmToolBtnDown = NULL;
  403.                     }
  404.     
  405.                     /*
  406.                      * Save the position of the toolbox.
  407.                      */
  408.                     GetWindowRect(hwnd, &rc);
  409.                     WriteWindowPos(&rc, FALSE, szTBPos);
  410.     
  411.                     /*
  412.                      * Null out the global window handle for the toolbox
  413.                      * for safety's sake.
  414.                      */
  415.                     ghwndToolbox = NULL;
  416.                 }
  417.     
  418.                 break;
  419.     
  420.             default:
  421.                 return DefWindowProc(hwnd, msg, wParam, lParam);
  422.         }
  423.     
  424.         return 0;
  425.     }
  426.     
  427.     
  428.     
  429.     /****************************************************************************
  430.     * ToolBtnWndProc
  431.     *
  432.     * This is the window procedure for the buttons in the toolbox window.
  433.     *
  434.     * History:
  435.     *
  436.     ****************************************************************************/
  437.     
  438.     WINDOWPROC ToolBtnWndProc(
  439.         HWND hwnd,
  440.         UINT msg,
  441.         WPARAM wParam,
  442.         LPARAM lParam)
  443.     {
  444.         switch (msg) {
  445.             case WM_LBUTTONDOWN:
  446.                 /*
  447.                  * Select the tool that was clicked on.
  448.                  */
  449.                 ToolboxSelectTool((UINT)GetWindowLong((hwnd), GWL_ID));
  450.     
  451.                 break;
  452.     
  453.             case WM_PAINT:
  454.                 {
  455.                     HDC hDC;
  456.                     PAINTSTRUCT ps;
  457.     
  458.                     hDC = BeginPaint(hwnd, &ps);
  459.                     ToolboxDrawBitmap(hDC, (UINT)GetWindowLong((hwnd), GWL_ID));
  460.                     EndPaint(hwnd, &ps);
  461.                 }
  462.     
  463.                 break;
  464.     
  465.             default:
  466.                 return DefWindowProc(hwnd, msg, wParam, lParam);
  467.         }
  468.     
  469.         return 0;
  470.     }
  471.     
  472.     
  473.     
  474.     /****************************************************************************
  475.     * ToolboxDrawBitmap
  476.     *
  477.     *
  478.     * History:
  479.     *
  480.     ****************************************************************************/
  481.     
  482.     STATICFN VOID NEAR ToolboxDrawBitmap(
  483.         HDC hDC,
  484.         INT tool)
  485.     {
  486.         HDC hMemDC;
  487.         HBITMAP hbmOld;
  488.     
  489.         /*
  490.          * Draw the image.
  491.          */
  492.         hMemDC = CreateCompatibleDC(hDC);
  493.         hbmOld = SelectObject(hMemDC, (tool == gCurTool) ?
  494.                 gaTools[tool].hbmToolBtnDown : gaTools[tool].hbmToolBtnUp);
  495.         BitBlt(hDC, 0, 0, cxToolBtn, cyToolBtn, hMemDC, 0, 0, SRCCOPY);
  496.         SelectObject(hMemDC, hbmOld);
  497.         DeleteDC(hMemDC);
  498.     }
  499.     
  500.     
  501.     
  502.     /****************************************************************************
  503.     * ToolboxSelectTool
  504.     *
  505.     *
  506.     * History:
  507.     *
  508.     ****************************************************************************/
  509.     
  510.     VOID ToolboxSelectTool(
  511.         INT tool)
  512.     {
  513.         if (gCurTool != tool) {
  514.             if (ghwndToolbox) {
  515.                 InvalidateRect(GetDlgItem(ghwndToolbox, gCurTool), NULL, FALSE);            InvalidateRect(GetDlgItem(ghwndToolbox, tool), NULL, FALSE);
  516.             }
  517.     
  518.             gCurTool = tool;
  519.             gpfnDrawProc = gaTools[gCurTool].pfnDrawProc;
  520.         }
  521.     }
  522.