TOOLBOX.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:17k
源码类别:

Windows编程

开发平台:

Visual C++

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