Annotation of mstools/samples/sdktools/imagedit/viewwp.c, revision 1.1.1.1

1.1       root        1: /****************************************************************************/
                      2: /*                                                                          */
                      3: /*                         Microsoft Confidential                           */
                      4: /*                                                                          */
                      5: /*                 Copyright (c) Microsoft Corp.  1987, 1990                */
                      6: /*                           All Rights Reserved                            */
                      7: /*                                                                          */
                      8: /****************************************************************************/
                      9: /****************************** Module Header *******************************
                     10: * Module Name: viewwp.c
                     11: *
                     12: * Contains routines that handle the View window.
                     13: *
                     14: * History:
                     15: *
                     16: * 07/17/91 -  - Created.
                     17: *
                     18: ****************************************************************************/
                     19: 
                     20: #include "imagedit.h"
                     21: #include "dialogs.h"
                     22: 
                     23: 
                     24: /*
                     25:  * Style of the view window. */
                     26: #define VIEWSTYLE       (WS_POPUP | WS_CLIPSIBLINGS | WS_CAPTION | WS_SYSMENU)
                     27: 
                     28: 
                     29: STATICFN VOID NEAR ViewChar(UINT uiChar);
                     30: 
                     31: 
                     32: static INT gViewBackMargin;     // Margin of background within view window.
                     33: 
                     34: 
                     35: 
                     36: /****************************************************************************
                     37: * ViewCreate
                     38: *
                     39: * This function creates the View window.
                     40: *
                     41: * History:
                     42: *
                     43: ****************************************************************************/
                     44: 
                     45: VOID ViewCreate(VOID)
                     46: {
                     47:     INT x;
                     48:     INT y;
                     49:     INT cxDummy;
                     50:     INT cyDummy;
                     51:     RECT rc;
                     52:     BOOL fMaximized;
                     53: 
                     54:     gViewBackMargin = GetSystemMetrics(SM_CXVSCROLL) / 2;
                     55: 
                     56:     /*
                     57:      * Get the saved position of the Toolbox.  Note that we throw away
                     58:      * the size fields, because we will calculate the required size
                     59:      * later based on the image size.
                     60:      */
                     61:     if (!ReadWindowPos(szViewPos, &x, &y, &cxDummy, &cyDummy, &fMaximized)) {
                     62:         /*
                     63:          * The previous position of the View window couldn't be found.
                     64:          * Position the window to the right side of the editor, just
                     65:          * below the Toolbox.
                     66:          */
                     67:         if (ghwndToolbox) {
                     68:             GetWindowRect(ghwndToolbox, &rc);
                     69:             x = rc.left;
                     70:             y = rc.bottom + (2 * PALETTEMARGIN);
                     71:         }
                     72:         else {
                     73:             /*
                     74:              * Last resort.  Position it in the upper left corner
                     75:              * of the screen if the Toolbox cannot be found.  This
                     76:              * is unlikely because if the previous position of the
                     77:              * View window could not be found, this implies that
                     78:              * the editor has not been run before on this machine.
                     79:              * If this is true, the toolbox will come up by default
                     80:              * before this routine is called.  But just in case...
                     81:              */
                     82:             x = 2 * PALETTEMARGIN;
                     83:             y = 2 * PALETTEMARGIN;
                     84:         }
                     85:     }
                     86: 
                     87:     if (!(ghwndView = CreateWindow(szViewClass, NULL, VIEWSTYLE,
                     88:             x, y, 0, 0, ghwndMain, NULL, ghInst, NULL)))
                     89:         return;
                     90: }
                     91: 
                     92: 
                     93: 
                     94: /****************************************************************************
                     95: * ViewShow
                     96: *
                     97: * This function shows or hides the view window.
                     98: *
                     99: * History:
                    100: *
                    101: ****************************************************************************/
                    102: 
                    103: VOID ViewShow(
                    104:     BOOL fShow)
                    105: {
                    106:     if (fShow) {
                    107:         /*
                    108:          * Only show it if there is an image to display!
                    109:          */
                    110:         if (gpImageCur)
                    111:             ShowWindow(ghwndView, SW_SHOWNA);
                    112:     }
                    113:     else {
                    114:         ShowWindow(ghwndView, SW_HIDE);
                    115:     }}
                    116: 
                    117: 
                    118: 
                    119: /****************************************************************************
                    120: * ViewUpdate
                    121: *
                    122: * This function updates the view window.  It should be called any time that
                    123: * the image changes (is drawn upon).
                    124: *
                    125: * History:
                    126: *
                    127: ****************************************************************************/
                    128: 
                    129: VOID ViewUpdate(VOID)
                    130: {
                    131:     InvalidateRect(ghwndView, NULL, TRUE);
                    132: 
                    133:     /*
                    134:      * Update the workspace window also, because it must always
                    135:      * match the state of the View window.
                    136:      */
                    137:     WorkUpdate();
                    138: }
                    139: 
                    140: 
                    141: 
                    142: /****************************************************************************
                    143: * ViewReset
                    144: *
                    145: * This function resets the view window, sizing it to fit a new
                    146: * image.  It should be called any time that the current image
                    147: * is changed to another one.
                    148: *
                    149: * History:
                    150: *
                    151: ****************************************************************************/
                    152: 
                    153: VOID ViewReset(VOID)
                    154: {
                    155:     RECT rc;
                    156:     RECT rcT;
                    157: 
                    158:     GetWindowRect(ghwndView, &rc);
                    159: 
                    160:     rcT.left = 0;
                    161:     rcT.top = 0;
                    162:     rcT.right = PALETTEMARGIN + gViewBackMargin +
                    163:             gpImageCur->cx + gViewBackMargin + PALETTEMARGIN;
                    164:     rcT.bottom = PALETTEMARGIN + gViewBackMargin +
                    165:             gpImageCur->cy + gViewBackMargin + PALETTEMARGIN;
                    166:     AdjustWindowRect(&rcT, VIEWSTYLE, FALSE);
                    167: 
                    168:     rc.right = rc.left + (rcT.right - rcT.left);
                    169:     rc.bottom = rc.top + (rcT.bottom - rcT.top);
                    170:     FitRectToScreen(&rc);
                    171: 
                    172:     SetWindowPos(ghwndView, NULL, rc.left, rc.top,
                    173:             rc.right - rc.left, rc.bottom - rc.top,
                    174:             SWP_NOACTIVATE | SWP_NOZORDER);
                    175: 
                    176:     /*
                    177:      * If the user wants it, show the View window now.
                    178:      */
                    179:     if (gfShowView)
                    180:         ViewShow(TRUE);
                    181: 
                    182:     ViewUpdate();
                    183: 
                    184:     /*
                    185:      * Clear out the propbar size and position fields, because they
                    186:      * probably show the wrong information now.
                    187:      */
                    188:     PropBarClearPos();
                    189:     PropBarClearSize();
                    190: }
                    191: 
                    192: 
                    193: 
                    194: /****************************************************************************
                    195: * ViewWndProc
                    196: *
                    197: * This is the window procedure for the view window.
                    198: *
                    199: * History:
                    200: *
                    201: ****************************************************************************/
                    202: 
                    203: WINDOWPROC ViewWndProc(
                    204:     HWND hwnd,
                    205:     UINT msg,
                    206:     WPARAM wParam,
                    207:     LPARAM lParam)
                    208: {
                    209:     switch (msg) {
                    210:         case WM_CREATE:
                    211:             {
                    212:                 HMENU hmenu = GetSystemMenu(hwnd, FALSE);
                    213: 
                    214:                 RemoveMenu(hmenu, 7, MF_BYPOSITION);    // Second separator.
                    215:                 RemoveMenu(hmenu, 5, MF_BYPOSITION);    // First separator.
                    216: 
                    217:                 RemoveMenu(hmenu, SC_RESTORE, MF_BYCOMMAND);
                    218:                 RemoveMenu(hmenu, SC_SIZE, MF_BYCOMMAND);
                    219:                 RemoveMenu(hmenu, SC_MINIMIZE, MF_BYCOMMAND);
                    220:                 RemoveMenu(hmenu, SC_MAXIMIZE, MF_BYCOMMAND);
                    221:                 RemoveMenu(hmenu, SC_TASKLIST, MF_BYCOMMAND);
                    222:             }
                    223: 
                    224:             return 0;
                    225: 
                    226:         case  WM_PAINT:
                    227:             {
                    228:                 HDC hdc;
                    229:                 PAINTSTRUCT ps;
                    230:                 HBRUSH hbrOld;
                    231:                 RECT rc;
                    232: 
                    233:                 hdc = BeginPaint(hwnd, &ps);
                    234: 
                    235:                 /*
                    236:                  * The view window should not be showing if there
                    237:                  * is not an image to view!
                    238:                  */
                    239:                 if (gpImageCur) {
                    240:                     DrawMarginBorder(hwnd, hdc);
                    241: 
                    242:                     GetClientRect(hwnd, &rc);
                    243:                     hbrOld = SelectObject(hdc, ghbrScreen);
                    244:                     PatBlt(hdc, PALETTEMARGIN + 1, PALETTEMARGIN + 1,
                    245:                             rc.right - (PALETTEMARGIN * 2) - 2,
                    246:                             rc.bottom - (PALETTEMARGIN * 2) - 2,
                    247:                             PATCOPY);
                    248:                     SelectObject(hdc, hbrOld);
                    249: 
                    250:                     BitBlt(hdc, PALETTEMARGIN + gViewBackMargin,
                    251:                             PALETTEMARGIN + gViewBackMargin,
                    252:                             gcxImage, gcyImage, ghdcImage, 0, 0, SRCCOPY);
                    253:                 }
                    254: 
                    255:                 EndPaint(hwnd, &ps);
                    256:             }
                    257: 
                    258:             break;
                    259: 
                    260:         case WM_ACTIVATE:
                    261:             if (LOWORD(wParam))
                    262:                 gidCurrentDlg = DID_VIEW;
                    263: 
                    264:             break;
                    265: 
                    266:         case WM_LBUTTONDOWN:
                    267:             SetScreenColor(gargbCurrent[giColorLeft]);
                    268:             break;
                    269: 
                    270:         case WM_CHAR:
                    271:             ViewChar(wParam);
                    272:             break;
                    273: 
                    274:         case WM_CLOSE:
                    275:             /*
                    276:              * The user closed the view window from the system menu.
                    277:              * Hide the window (we don't actually destroy it so
                    278:              * that it will appear in the same spot when they show
                    279:              * it again).
                    280:              */
                    281:             ViewShow(FALSE);
                    282:             gfShowView = FALSE;
                    283:             break;
                    284: 
                    285:         case WM_DESTROY:
                    286:             {
                    287:                 RECT rc;
                    288: 
                    289:                 /*
                    290:                  * Save the position of the toolbox.
                    291:                  */
                    292:                 GetWindowRect(hwnd, &rc);
                    293:                 WriteWindowPos(&rc, FALSE, szViewPos);
                    294: 
                    295:                 /*
                    296:                  * Null out the global window handle for the view window
                    297:                  * for safety's sake.
                    298:                  */
                    299:                 ghwndView = NULL;
                    300:             }
                    301: 
                    302:             break;
                    303: 
                    304:         default:
                    305:             return DefWindowProc(hwnd, msg, wParam, lParam);
                    306:     }
                    307: 
                    308:     return 0;
                    309: }
                    310: 
                    311: 
                    312: 
                    313: /************************************************************************
                    314: * ViewChar
                    315: *
                    316: * Handles WM_CHAR messages for the view window.  Currently this just
                    317: * includes the '+' and '-' keys, which are used to cycle through all
                    318: * the possible screen colors.
                    319: *
                    320: * Arguments:
                    321: *
                    322: * History:
                    323: *
                    324: ************************************************************************/
                    325: 
                    326: STATICFN VOID NEAR ViewChar(
                    327:     UINT uiChar)
                    328: {
                    329:     INT i;
                    330:     INT iNext;
                    331: 
                    332:     switch (uiChar) {
                    333:         /*
                    334:          * Advance to the next screen color.
                    335:          */
                    336:         case '+':
                    337:             iNext = 0;
                    338:             for (i = 0; i < 16; i++) {
                    339:                 if (grgbScreen == gargbDefaultColor[i]) {
                    340:                     iNext = i + 1;
                    341:                     break;
                    342:                 }
                    343:             }
                    344: 
                    345:             if (iNext >= 16)
                    346:                 iNext = 0;
                    347: 
                    348:             SetScreenColor(gargbDefaultColor[iNext]);
                    349: 
                    350:             break;
                    351: 
                    352:         /*
                    353:          * Back up to the prior screen color.
                    354:          */
                    355:         case '-':
                    356:             iNext = 16 - 1;
                    357:             for (i = 0; i < 16; i++) {
                    358:                 if (grgbScreen == gargbDefaultColor[i]) {
                    359:                     iNext = i - 1;
                    360:                     break;
                    361:                 }
                    362:             }
                    363: 
                    364:             if (iNext < 0)
                    365:                 iNext = 16 - 1;
                    366: 
                    367:             SetScreenColor(gargbDefaultColor[iNext]);
                    368: 
                    369:             break;
                    370:     }
                    371: }
                    372: 
                    373: 
                    374: 
                    375: /****************************************************************************
                    376: * ViewSetPixel
                    377: *
                    378: * This function colors a pixel in the View window directly.  It is
                    379: * provided as an optimization when drawing a point.  The ghdcImage
                    380: * bitmap must be updated as well or the image on the screen will
                    381: * get out of synch with it.
                    382: *
                    383: * History:
                    384: *
                    385: ****************************************************************************/
                    386: 
                    387: VOID ViewSetPixel(
                    388:     INT x,
                    389:     INT y,
                    390:     INT nBrushSize)
                    391: {
                    392:     HDC hDC;
                    393:     HBRUSH hbrOld;
                    394:     INT Size;
                    395:     INT SizeX;
                    396:     INT SizeY;
                    397: 
                    398:     hDC = GetDC(ghwndView);
                    399:     hbrOld = SelectObject(hDC, ghbrDrawSolid);
                    400:     SizeX = x - nBrushSize / 2;
                    401:     SizeY = y - nBrushSize / 2;
                    402:     PatBlt(hDC, PALETTEMARGIN + gViewBackMargin + (SizeX >= 0 ? SizeX : 0),
                    403:             PALETTEMARGIN + gViewBackMargin + (SizeY >=  0 ? SizeY :  0),
                    404:             ((Size = gcxImage - SizeX) >= nBrushSize ?
                    405:             nBrushSize : Size),
                    406:             ((Size = gcyImage - SizeY) >= nBrushSize ?
                    407:             nBrushSize : Size), PATCOPY);
                    408:     SelectObject(hDC, hbrOld);
                    409:     ReleaseDC(ghwndView, hDC);
                    410: }
                    411: 
                    412: 
                    413: 
                    414: /****************************************************************************
                    415: * DrawMarginBorder
                    416: *
                    417: *
                    418: * History:
                    419: *
                    420: ****************************************************************************/
                    421: 
                    422: VOID DrawMarginBorder(
                    423:     HWND hwnd,
                    424:     HDC hdc)
                    425: {
                    426:     HBRUSH hbrOld;
                    427:     HPEN hpenOld;
                    428:     RECT rc;
                    429: 
                    430:     GetClientRect(hwnd, &rc);
                    431:     hpenOld = SelectObject(hdc, GetStockObject(BLACK_PEN));
                    432:     hbrOld = SelectObject(hdc, GetStockObject(NULL_BRUSH));
                    433:     Rectangle(hdc, PALETTEMARGIN, PALETTEMARGIN,
                    434:             rc.right - PALETTEMARGIN,
                    435:             rc.bottom - PALETTEMARGIN);
                    436:     SelectObject(hdc, hpenOld);
                    437:     SelectObject(hdc, hbrOld);
                    438: }
                    439: 
                    440: 
                    441: 
                    442: /****************************************************************************
                    443: * DrawSunkenRect
                    444: *
                    445: *
                    446: * History:
                    447: *
                    448: ****************************************************************************/
                    449: 
                    450: VOID DrawSunkenRect(
                    451:     PRECT prc,
                    452:     HDC hdc)
                    453: {
                    454:     HPEN hpenOld;
                    455: 
                    456:     hpenOld = SelectObject(hdc, hpenDarkGray);
                    457:     MoveToEx(hdc, prc->left, prc->top, NULL);
                    458:     LineTo(hdc, prc->right - 1, prc->top);
                    459:     MoveToEx(hdc, prc->left, prc->top, NULL);
                    460:     LineTo(hdc, prc->left, prc->bottom - 1);
                    461: 
                    462:     SelectObject(hdc, GetStockObject(WHITE_PEN));
                    463:     MoveToEx(hdc, prc->left + 1, prc->bottom - 1, NULL);
                    464:     LineTo(hdc, prc->right, prc->bottom - 1);
                    465:     MoveToEx(hdc, prc->right - 1, prc->top + 1, NULL);
                    466:     LineTo(hdc, prc->right - 1, prc->bottom - 1);
                    467: 
                    468:     SelectObject(hdc, hpenOld);
                    469: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.