home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / SHOWGDI / DC.C_ / DC.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  9.9 KB  |  280 lines

  1. /****************************************************************************
  2.  DC.c
  3.  
  4.  The DC module handles setting and reading of the DC attributes.
  5.  
  6. ****************************************************************************/
  7.  
  8. #include "windows.h"
  9.  
  10. #include "MENU.h"
  11.  
  12. #include "util.h"
  13. #include "main.h"
  14. #include "view.h"
  15. #include "dib.h"
  16. #include "dialogs.h"
  17.  
  18. #include "dc.h"
  19.  
  20.  
  21. /****************************************************************************
  22.    Globals
  23. ****************************************************************************/
  24.  
  25. /* The global DC settings */
  26. DCValues dcv;
  27.  
  28.  
  29. /****************************************************************************
  30.    Private Data
  31. ****************************************************************************/
  32.  
  33. /* Saved objects used by SetupDC() and CleanupDC() */
  34. static HFONT    prevFont;
  35. static HBRUSH   prevBrush;
  36. static HPEN     prevPen;
  37.  
  38.  
  39. /****************************************************************************
  40.    Functions
  41. ****************************************************************************/
  42.                          
  43. void DCCmd( int item )
  44. /* Run the command for menu item 'item' from the DC menu group. */
  45. {
  46.    HDC   hdcMain;
  47.  
  48.    switch( item )
  49.    {
  50.       case IDM_DEFAULTS:
  51.          hdcMain = GetDC( hwndMain );     /* will get default values */
  52.          ReadDC( hdcMain );
  53.          ReleaseDC( hwndMain, hdcMain );
  54.          RefreshView( TRUE );             /* to redraw grid lines */
  55.          break;
  56.          
  57.       case IDM_COORDS:        
  58.          if (Dlg( CoordsDlg ))
  59.             RefreshView( TRUE );          /* to redraw grid lines */
  60.          break;
  61.          
  62.       case IDM_CLIP:          Dlg( ClipDlg );                     break;
  63.       case IDM_BKCOLOR:       Dlg( BkColorDlg );                  break;
  64.       case IDM_BKMODE:        Dlg( BkModeDlg );                   break;
  65.       case IDM_BRUSHORG:      Dlg( BrushOrgDlg );                 break;
  66.       case IDM_PENPOS:        Dlg( PenPosDlg );                   break;
  67.       case IDM_FONT:          Dlg( FontDlg );                     break;
  68.       case IDM_TEXTALIGN:     Dlg( TextAlignDlg );                break;
  69.       case IDM_TEXTJUST:      Dlg( TextJustDlg );                 break;
  70.       case IDM_TEXTEXTRA:     Dlg( TextExtraDlg );                break;
  71.       case IDM_TEXTCOLOR:     Dlg( TextColorDlg );                break;
  72.       case IDM_ROP2:          Dlg( ROP2Dlg );                     break;
  73.       case IDM_POLYMODE:      Dlg( PolyModeDlg );                 break;
  74.       case IDM_BLTMODE:       Dlg( BltModeDlg );                  break;
  75.  
  76.       case IDM_NULLBRUSH:     dcv.brush.lbStyle = BS_NULL;        break;
  77.       case IDM_SOLIDBRUSH:    Dlg( SolidBrushDlg );               break;
  78.       case IDM_HATCHBRUSH:    Dlg( HatchBrushDlg );               break;
  79.       case IDM_PATBRUSH:      Dlg( PatBrushDlg );                 break;
  80.       case IDM_DIBPATBRUSH:   Dlg( DIBPatBrushDlg );              break;
  81.                               
  82.       case IDM_NULLPEN:       dcv.pen.lopnStyle = PS_NULL;        break;
  83.       case IDM_SOLIDPEN:      dcv.pen.lopnStyle = PS_SOLID;       break;
  84.       case IDM_INSIDEFRAMEPEN:dcv.pen.lopnStyle = PS_INSIDEFRAME; break;
  85.       case IDM_DASHPEN:       dcv.pen.lopnStyle = PS_DASH;        break;
  86.       case IDM_DOTPEN:        dcv.pen.lopnStyle = PS_DOT;         break;
  87.       case IDM_DASHDOTPEN:    dcv.pen.lopnStyle = PS_DASHDOT;     break;
  88.       case IDM_DASHDOTDOTPEN: dcv.pen.lopnStyle = PS_DASHDOTDOT;  break;
  89.       case IDM_PENWIDTH:      Dlg( PenWidthDlg );                 break;
  90.       case IDM_PENCOLOR:      Dlg( PenColorDlg );                 break;
  91.    } /* switch */
  92. }
  93.  
  94.  
  95. void ReadDC( HDC hdc )
  96. /* Read the settings from 'hdc' into the global DC settings 'dcv'. */
  97. {
  98.    HFONT    stockFont;
  99.    HPEN     stockPen;
  100.    HBRUSH   stockBrush;
  101.  
  102.    dcv.mapMode = GetMapMode( hdc );
  103.    *((DWORD *) &dcv.windowOrg) = GetWindowOrg( hdc );
  104.    *((DWORD *) &dcv.windowExt) = GetWindowExt( hdc );
  105.    *((DWORD *) &dcv.viewportOrg) = GetViewportOrg( hdc );
  106.    *((DWORD *) &dcv.viewportExt) = GetViewportExt( hdc );
  107.  
  108.    *((DWORD *) &dcv.penPos) = GetCurrentPosition( hdc );
  109.    *((DWORD *) &dcv.brushOrg) = GetBrushOrg( hdc );
  110.  
  111.    dcv.exclude = dcv.intersect = FALSE;
  112.  
  113.    dcv.bkColor = GetBkColor( hdc );
  114.    dcv.bkMode = GetBkMode( hdc );
  115.    
  116.    stockFont = GetStockObject( SYSTEM_FONT );
  117.    dcv.hFont = SelectObject( hdc, stockFont );
  118.    GetObject( dcv.hFont, sizeof( LOGFONT ), (LPSTR) &dcv.font );
  119.    SelectObject( hdc, dcv.hFont );
  120.  
  121.    dcv.textAlign = GetTextAlign( hdc );
  122.    dcv.breakExtra = dcv.breakCount = 0;
  123.    dcv.textExtra = GetTextCharacterExtra( hdc );
  124.    dcv.textColor = GetTextColor( hdc );
  125.  
  126.    dcv.rop2 = GetROP2( hdc );
  127.    dcv.polyFillMode = GetPolyFillMode( hdc );
  128.    dcv.bltMode = GetStretchBltMode( hdc );
  129.  
  130.    stockBrush = GetStockObject( NULL_BRUSH );
  131.    dcv.hBrush = SelectObject( hdc, stockBrush );
  132.    GetObject( dcv.hBrush, sizeof( LOGBRUSH ), (LPSTR) &dcv.brush );
  133.    SelectObject( hdc, dcv.hBrush );
  134.  
  135.    stockPen = GetStockObject( NULL_PEN );
  136.    dcv.hPen = SelectObject( hdc, stockPen );
  137.    GetObject( dcv.hPen, sizeof( LOGPEN ), (LPSTR) &dcv.pen );
  138.    SelectObject( hdc, dcv.hPen );
  139. }
  140.  
  141.  
  142. HBITMAP MakePatternBitmap( char checks[64] )
  143. /* Create a pattern bitmap by treating 'checks' as an array of booleans
  144.    where a check indicates a 0 bit. */
  145. {
  146.    unsigned    i, mask;
  147.    short       shorts[8];
  148.  
  149.    for (i = 0; i < 64; i++) {
  150.       mask = 1 << (7 - (i % 8));
  151.       if (checks[i]) {
  152.          shorts[i / 8] &= ~mask;
  153.       }else{
  154.          shorts[i / 8] |= mask;
  155.       }
  156.    }
  157.    return CreateBitmap( 8, 8, 1, 1, (LPSTR) shorts );
  158. }
  159.  
  160.  
  161. void SetupDC( HDC hdc )
  162. /* Setup 'hdc' based on the global DC settings 'dcv'. */
  163. {
  164.    COLORREF savedBrushColor;
  165.  
  166.    SetMapMode( hdc, dcv.mapMode );
  167.    SetWindowOrg( hdc, dcv.windowOrg.x, dcv.windowOrg.y );
  168.    SetWindowExt( hdc, dcv.windowExt.x, dcv.windowExt.y );
  169.    SetViewportOrg( hdc, dcv.viewportOrg.x, dcv.viewportOrg.y );
  170.    SetViewportExt( hdc, dcv.viewportExt.x, dcv.viewportExt.y );
  171.  
  172.    MoveTo( hdc, dcv.penPos.x, dcv.penPos.y );
  173.    SetBrushOrg( hdc, dcv.brushOrg.x, dcv.brushOrg.y );
  174.  
  175.    if( dcv.intersect ) {
  176.       IntersectClipRect( hdc, dcv.rIntersect.left, dcv.rIntersect.top, 
  177.                          dcv.rIntersect.right, dcv.rIntersect.bottom );
  178.    }
  179.    if( dcv.exclude ) {
  180.       ExcludeClipRect( hdc, dcv.rExclude.left, dcv.rExclude.top, 
  181.                        dcv.rExclude.right, dcv.rExclude.bottom );
  182.    }
  183.  
  184.    SetBkColor( hdc, dcv.bkColor );
  185.    SetBkMode( hdc, dcv.bkMode );
  186.    
  187.    dcv.hFont = CreateFontIndirect( &dcv.font );
  188.    if( dcv.hFont == NULL ) {
  189.       SetupError("CreateFontIndirect() failed (invalid LOGFONT)");
  190.       prevFont = NULL;
  191.    }else{
  192.       prevFont = SelectObject( hdc, dcv.hFont );
  193.    }
  194.  
  195.    SetTextAlign( hdc, dcv.textAlign );
  196.    SetTextJustification( hdc, dcv.breakExtra, dcv.breakCount );
  197.    SetTextCharacterExtra( hdc, dcv.textExtra );
  198.    SetTextColor( hdc, dcv.textColor );
  199.  
  200.    SetROP2( hdc, dcv.rop2 );
  201.    SetPolyFillMode( hdc, dcv.polyFillMode );
  202.    SetStretchBltMode( hdc, dcv.bltMode );
  203.  
  204.    savedBrushColor = dcv.brush.lbColor;
  205.    if (dcv.brush.lbStyle == BS_PATTERN) {
  206.       dcv.brush.lbHatch = (short) MakePatternBitmap( dcv.patChecks );
  207.    }else if (dcv.brush.lbStyle == BS_DIBPATTERN) {
  208.       dcv.brush.lbHatch = (short) curDIB;
  209.       dcv.brush.lbColor = (COLORREF) DIB_RGB_COLORS;
  210.    }
  211.    dcv.hBrush = CreateBrushIndirect( &dcv.brush );
  212.    if( dcv.hBrush == NULL ) {
  213.       SetupError("CreateBrushIndirect() failed (invalid LOGBRUSH)");
  214.       prevBrush = NULL;
  215.    }else{
  216.       prevBrush = SelectObject( hdc, dcv.hBrush );
  217.    }
  218.    dcv.brush.lbColor = savedBrushColor;
  219.  
  220.    dcv.hPen = CreatePenIndirect( &dcv.pen );
  221.    if( dcv.hPen == NULL ) {
  222.       SetupError("CreatePenIndirect() failed (invalid LOGPEN)");
  223.       prevPen = NULL;
  224.    }else{
  225.       prevPen = SelectObject( hdc, dcv.hPen );
  226.    }
  227. }
  228.  
  229.  
  230. void SetupError( char *string )
  231. /* Alert and take action for an error during DC setup. */
  232. {
  233.    MessageBox( hwndMain, (LPSTR) string, (LPSTR) "DC Setup Error", MB_OK );
  234. }
  235.  
  236.  
  237. void CleanupDC( HDC hdc )
  238. /* Remove and delete any objects selected into 'hdc' by SetupDC(), and
  239.    perform any other post-drawing work for the global DC setting 'dcv'. */
  240. {
  241.    /* Update DC setting fields which drawing can change */
  242.    *((DWORD *) &dcv.penPos) = GetCurrentPosition( hdc );
  243.  
  244.    /* Clear the text justification since it's not a retained attribute */
  245.    dcv.breakExtra = dcv.breakCount = 0;
  246.  
  247.    /* Deselect and delete any objects created */
  248.    if (dcv.brush.lbStyle == BS_PATTERN) {
  249.       DeleteObject( (HBITMAP) dcv.brush.lbHatch );
  250.    }
  251.    if (prevFont != NULL) {
  252.       DeleteObject( SelectObject( hdc, prevFont ) );
  253.    }
  254.    if (prevBrush != NULL) {
  255.       DeleteObject( SelectObject( hdc, prevBrush ) );
  256.    }
  257.    if (prevPen != NULL) {
  258.       DeleteObject( SelectObject( hdc, prevPen ) );
  259.    }
  260. }
  261.  
  262.  
  263. void CheckDCMenuItems( HMENU hMenu )
  264. /* Update enabling and checks on the menus in the DC menu group. */
  265. {
  266.    CheckMenuCmd( hMenu, IDM_NULLPEN, (dcv.pen.lopnStyle == PS_NULL) );
  267.    CheckMenuCmd( hMenu, IDM_SOLIDPEN, (dcv.pen.lopnStyle == PS_SOLID) );
  268.    CheckMenuCmd( hMenu, IDM_INSIDEFRAMEPEN, (dcv.pen.lopnStyle == PS_INSIDEFRAME) );
  269.    CheckMenuCmd( hMenu, IDM_DASHPEN, (dcv.pen.lopnStyle == PS_DASH) );
  270.    CheckMenuCmd( hMenu, IDM_DOTPEN, (dcv.pen.lopnStyle == PS_DOT) );
  271.    CheckMenuCmd( hMenu, IDM_DASHDOTPEN, (dcv.pen.lopnStyle == PS_DASHDOT) );
  272.    CheckMenuCmd( hMenu, IDM_DASHDOTDOTPEN, (dcv.pen.lopnStyle == PS_DASHDOTDOT) );
  273.  
  274.    CheckMenuCmd( hMenu, IDM_NULLBRUSH, (dcv.brush.lbStyle == BS_NULL) );
  275.    CheckMenuCmd( hMenu, IDM_SOLIDBRUSH, (dcv.brush.lbStyle == BS_SOLID) );
  276.    CheckMenuCmd( hMenu, IDM_HATCHBRUSH, (dcv.brush.lbStyle == BS_HATCHED) );
  277.    CheckMenuCmd( hMenu, IDM_PATBRUSH, (dcv.brush.lbStyle == BS_PATTERN) );
  278.    CheckMenuCmd( hMenu, IDM_DIBPATBRUSH, (dcv.brush.lbStyle == BS_DIBPATTERN) );
  279. }
  280.