Annotation of mstools/samples/gridfont/app.cxx, revision 1.1.1.1

1.1       root        1: //+--------------------------------------------------------
                      2: // File:        App.cxx
                      3: //
                      4: // Classes:     CController
                      5: //
                      6: // Functions:   WinMain
                      7: //              WndProc
                      8: //              MakeWindowClass
                      9: //              AboutDlgProc
                     10: //
                     11: // History:     22-Jan-1993     asmusf  created
                     12: //----------------------------------------------------------
                     13: 
                     14: #include <windows.h>
                     15: #include <windowsx.h>
                     16: #include "app.hxx"
                     17: //+--------------------------------------------------------
                     18: // Class:       CController
                     19: //
                     20: // Purpose:     Controlling the application, UI handler
                     21: //
                     22: // History:     22-Jan-1993     asmusf  created
                     23: //----------------------------------------------------------
                     24: 
                     25: void CController::Create(HWND hwnd, LONG lParam )
                     26: {
                     27:     HANDLE handle = ((LPCREATESTRUCT) lParam)->hInstance;
                     28:     _hInst = (HINSTANCE)handle;
                     29:     _funAbout = (DLGPROC) MakeProcInstance((FARPROC)AboutDlgProc, _hInst);
                     30: 
                     31:     // Set up the Main View
                     32:     _pView = new CScrollableView(((LPCREATESTRUCT) lParam)->cx,
                     33:                                 ((LPCREATESTRUCT) lParam)->cy);
                     34:     _pView->SetScale(100);
                     35: 
                     36:     SetScrollRange(hwnd, SB_VERT, 0, PAGEHEIGHT, FALSE);
                     37:     SetScrollRange(hwnd, SB_HORZ, 0, PAGEWIDTH, FALSE);
                     38: 
                     39:     SetScrollPos(hwnd, SB_HORZ, 0, TRUE );
                     40:     SetScrollPos(hwnd, SB_VERT, 0, TRUE );
                     41: 
                     42:     // Set up the Model
                     43:     _pModel= new CModel(_hInst, HEXADECIMAL);
                     44: 
                     45:     // Set up the initial state of the Menu
                     46:     Page(hwnd, IDM_FIRSTPAGE);
                     47: 
                     48:     // initially no pop-up character box
                     49:     _pBox = 0;
                     50: }
                     51: 
                     52: void CController::Destroy()
                     53: {
                     54: 
                     55:     FreeProcInstance((FARPROC)AboutDlgProc);
                     56: 
                     57:     delete _pView;
                     58:     delete _pModel;
                     59:     PostQuitMessage(0);
                     60: }
                     61: 
                     62: void CController::Size ( LONG lParam )
                     63: {
                     64:    _pView->SetSize ( LOWORD(lParam), HIWORD(lParam) );
                     65: }
                     66: 
                     67: // Message Box with text from resource file
                     68: void CController::AlertBox( HWND hwnd, UINT idsText, UINT fuStyle)
                     69: {
                     70:     TCHAR szText[100];
                     71:     TCHAR szCaption[100];
                     72: 
                     73:     LoadString(_hInst, idsText, (LPTSTR)szText, 100);
                     74:     LoadString(_hInst, IDS_MSGCAPTION, (LPTSTR)szCaption, 100);
                     75: 
                     76:     ::MessageBox (hwnd, (LPTSTR)szText, (LPTSTR)szCaption, fuStyle) ;
                     77: };
                     78: 
                     79: // Menu commands processing
                     80: 
                     81: void CController::Command ( HWND hwnd, WPARAM wID )
                     82: {
                     83:     switch ( wID )
                     84:     {
                     85:       // File Menu
                     86:       case IDM_PRINT:
                     87:            {
                     88:                 CPrintRequest job(hwnd, 1, _pModel->GetMaxPage());
                     89: 
                     90:                 if(!job.Cancelled())
                     91:                 {
                     92:                     CPrintCanvas canvas (job);
                     93:                     job.Print (_hInst, canvas, _pModel);
                     94:                     
                     95:                     if(job.Error())
                     96:                     {
                     97:                         AlertBox (hwnd, IDS_PRINTERR, MB_OK | MB_ICONEXCLAMATION) ;
                     98:                     }
                     99:                 }
                    100:            }
                    101:            break;
                    102:       case IDM_EXIT:
                    103:            SendMessage ( hwnd, WM_CLOSE, 0L, 0L );
                    104:            break;
                    105: 
                    106:       // View Menu
                    107:       case IDM_NEXTPAGE:
                    108:            Page(hwnd, IDM_NEXTPAGE);
                    109:            break;
                    110:       case IDM_PREVPAGE:
                    111:            Page(hwnd, IDM_PREVPAGE);
                    112:            break;
                    113:       case IDM_NEXTSECTION:
                    114:            Page(hwnd, IDM_NEXTSECTION);
                    115:            break;
                    116:       case IDM_PREVSECTION:
                    117:            Page(hwnd, IDM_PREVSECTION);
                    118:            break;
                    119:       case IDM_ZOOMIN:
                    120:            {
                    121:                UINT iScale;
                    122:                _pView->GetScale(iScale);
                    123:                iScale -= (iScale > 50 ? 25 : 0);
                    124:                _pView->SetScale(iScale);
                    125:                _pView->Invalidate(hwnd);
                    126:            }
                    127:            break;
                    128:       case IDM_ZOOMOUT:
                    129:            {
                    130:                UINT iScale;
                    131:                _pView->GetScale(iScale);
                    132:                iScale += (iScale < 200 ? 25 : 0);
                    133:                _pView->SetScale(iScale);
                    134:                _pView->Invalidate(hwnd);
                    135:            }
                    136:            break;
                    137: 
                    138:       // Options Menu
                    139:       case IDM_FONT:
                    140:            _pModel->ChooseFont(hwnd);
                    141:            _pView->Invalidate(hwnd);
                    142:            break;
                    143:       case IDM_DECIMAL:
                    144:            {
                    145:                UINT fuFormat;
                    146:                _pModel->GetFormat(fuFormat);
                    147:                fuFormat ^= DECIMAL;
                    148:                CheckMenuItem (GetMenu(hwnd), IDM_DECIMAL, 
                    149:                         fuFormat & DECIMAL ? MF_CHECKED : MF_UNCHECKED ) ;
                    150:               _pModel->SetFormat(fuFormat);
                    151:            }
                    152:            _pView->Invalidate(hwnd);
                    153:            break;
                    154: 
                    155:       // Help Menu
                    156:       case IDM_HELP:
                    157:            AlertBox ( hwnd, IDS_NOTIMPLEM, MB_ICONINFORMATION | MB_OK);
                    158:            break;
                    159:       case IDM_ABOUT:
                    160:            DialogBox ( _hInst, TEXT("AboutBox"), hwnd, _funAbout );
                    161:            break;
                    162:      }
                    163: }
                    164: 
                    165: void CController::Page(HWND hwnd, WPARAM wParam)
                    166: {
                    167:     UINT iPage = _pModel->GetPage();
                    168:     switch (wParam)
                    169:     {
                    170:     case IDM_PREVPAGE: 
                    171:         _pModel->PrevPage();
                    172:         break;  
                    173:     case IDM_NEXTPAGE:
                    174:         _pModel->NextPage();
                    175:         break;  
                    176:     case IDM_NEXTSECTION:
                    177:         _pModel->NextSection();
                    178:         break;
                    179:     case IDM_PREVSECTION:
                    180:         _pModel->PrevSection();
                    181:         break;
                    182:     case IDM_FIRSTPAGE:
                    183:         _pModel->SetPage( 0 );
                    184:         break;  
                    185:     case IDM_LASTPAGE:
                    186:         _pModel->SetPage( _pModel->GetMaxPage() -1 );
                    187:         break;  
                    188:     }
                    189:     if (iPage != _pModel->GetPage())
                    190:     {   
                    191:         _pView->Invalidate(hwnd);
                    192:     }
                    193:     HMENU hmenu = GetMenu(hwnd);
                    194: 
                    195:     EnableMenuItem (hmenu, IDM_PREVPAGE, _pModel->CanPrevPage()  ?
                    196:                       MF_ENABLED : MF_DISABLED | MF_GRAYED ) ;
                    197:     EnableMenuItem (hmenu, IDM_NEXTPAGE, _pModel->CanNextPage() ?
                    198:                       MF_ENABLED : MF_DISABLED | MF_GRAYED ) ;
                    199:     EnableMenuItem (hmenu, IDM_NEXTSECTION, _pModel->CanNextSection() ?
                    200:                       MF_ENABLED : MF_DISABLED | MF_GRAYED ) ;
                    201:     EnableMenuItem (hmenu, IDM_PREVSECTION,_pModel->CanPrevSection() ?
                    202:                       MF_ENABLED : MF_DISABLED | MF_GRAYED ) ;
                    203: }
                    204: 
                    205: void CController::ButtonDown(HWND hwnd, LONG lParam )
                    206: {
                    207:     static RECT rc;
                    208:     SetCapture (hwnd);
                    209: 
                    210:     if( _pBox )
                    211:     {
                    212:         delete _pBox;
                    213:         _pBox = 0;
                    214:         _pView->Invalidate(hwnd, &rc);
                    215:         return;
                    216:     }
                    217:     
                    218:     SIZE size = { 4*(INCH1-INCH8)/5, INCH1 /*-INCH8*/ };
                    219:     CBoxFormat bxf(size);
                    220: 
                    221:     CScreenCanvas canvas(hwnd);
                    222:     
                    223:     POINT pt = {LOWORD(lParam), HIWORD(lParam)};
                    224: 
                    225:     UINT iChar = _pView->Hittest(canvas, pt, _pModel);
                    226:     
                    227:     if( iChar == 0xFFFF )
                    228:     {
                    229:         return;
                    230:     }
                    231:     HFONT hfont = _pModel->GetFont();
                    232:     _pBox = new CBox(bxf, iChar, hfont); 
                    233: 
                    234:     canvas.DPtoLP(&pt);
                    235:     pt.x -= size.cx/2;
                    236:     pt.y -= size.cy/2;
                    237: #ifdef UNICODE
                    238:     pt.x -= size.cx; // accommodate wider popup
                    239: #endif
                    240: 
                    241:     GetClientRect(hwnd, &rc);
                    242: 
                    243:     _pBox->Paint(canvas, pt, rc);
                    244:     size = _pBox->GetSize();
                    245:     
                    246:     canvas.LPtoDP(&pt);
                    247:     rc.left = pt.x;
                    248:     rc.top = pt.y;
                    249:     
                    250:     canvas.DPtoLP(&pt);
                    251:     pt.x+=size.cx;
                    252:     pt.y+=size.cy;
                    253: 
                    254:     canvas.LPtoDP(&pt);
                    255:     rc.right = pt.x;
                    256:     rc.bottom = pt.y;
                    257: }
                    258: 
                    259: void CController::ButtonUp(HWND hwnd, LONG lParam )
                    260: {
                    261:     ReleaseCapture();
                    262: }
                    263: 
                    264: void CController::KeyDown(HWND hwnd, WPARAM wParam, LPARAM lParam)
                    265: {
                    266:     switch( wParam )
                    267:     {
                    268:     case VK_PRIOR:
                    269:          Page(hwnd, IDM_PREVPAGE);
                    270:          break;
                    271:     case VK_NEXT:
                    272:          Page(hwnd, IDM_NEXTPAGE);
                    273:          break;
                    274:     case VK_HOME:
                    275:          Page(hwnd, IDM_FIRSTPAGE);
                    276:          break;
                    277:     case VK_END:
                    278:          Page(hwnd, IDM_LASTPAGE);
                    279:          break;
                    280:     case VK_UP:
                    281:          _pView->SetVScrollPos(hwnd, SB_LINEUP, lParam, _pModel);       
                    282:          break;
                    283:     case VK_DOWN:
                    284:          _pView->SetVScrollPos(hwnd, SB_LINEDOWN, lParam, _pModel);     
                    285:          break;
                    286:     case VK_LEFT:
                    287:          _pView->SetHScrollPos(hwnd, SB_LINEUP, lParam, _pModel);       
                    288:          break;
                    289:     case VK_RIGHT:
                    290:          _pView->SetHScrollPos(hwnd, SB_LINEDOWN, lParam, _pModel);     
                    291:          break;
                    292:     }
                    293: }
                    294: 
                    295: void CController::KeyUp(HWND hwnd, WPARAM wParam, LPARAM lParam)
                    296: {
                    297: }
                    298: 
                    299: void CController::HScroll(HWND hwnd, WPARAM wParam, LPARAM lParam)
                    300: {
                    301:     _pView->SetHScrollPos(hwnd, wParam, lParam, _pModel);       
                    302: }
                    303: 
                    304: void CController::VScroll(HWND hwnd, WPARAM wParam, LPARAM lParam)
                    305: {
                    306:     _pView->SetVScrollPos(hwnd, wParam, lParam, _pModel);       
                    307: }
                    308: 
                    309: // Main application
                    310: 
                    311: // NOTE: for October Beta needed to declare WinMain with HANDLE instead
                    312: //       of HINSTANCE, which requires the two casts further down
                    313: //       (compiler internally faults otherwise)
                    314: 
                    315: 
                    316: // WinMain - Main window funtion
                    317: //
                    318: int PASCAL WinMain
                    319:    ( HANDLE hInst, HANDLE hPrevInst, LPSTR cmdParam, int cmdShow )
                    320: {
                    321:     TCHAR  szAppName [] = TEXT("Grid") ;
                    322:     TCHAR  szCaption [] = TEXT("Character Grid") ;
                    323: 
                    324:     // Create Window Class
                    325:  
                    326:     if ( hPrevInst == 0 )
                    327:     {
                    328:         MakeWindowClass ( WndProc, szAppName, (HINSTANCE) hInst );
                    329:     }
                    330: 
                    331:     // Create Window
                    332:     
                    333:     CWindow win ( szCaption, szAppName, (HINSTANCE) hInst );
                    334:     
                    335:     win.Show ( cmdShow );
                    336:     
                    337:     MSG  msg;
                    338:     
                    339:     while ( GetMessage (&msg, NULL, 0, 0 ) )
                    340:     {
                    341:         TranslateMessage ( &msg );
                    342:         DispatchMessage ( &msg );
                    343:     }
                    344:     
                    345:     return msg.wParam;
                    346: }
                    347: 
                    348: // Make Window Class
                    349: //
                    350: void MakeWindowClass ( WNDPROC WndProc, LPTSTR szAppName, HINSTANCE hInst )
                    351: {
                    352:     WNDCLASS cl;
                    353:    
                    354:     cl.style = CS_HREDRAW | CS_VREDRAW;
                    355:     cl.lpfnWndProc = WndProc;
                    356:     cl.cbClsExtra = 0;
                    357:     cl.cbWndExtra = 0;
                    358:     cl.hInstance = hInst;
                    359:     cl.hIcon = LoadIcon ( hInst, szAppName );
                    360:     cl.hCursor = LoadCursor ( NULL, IDC_ARROW );
                    361:     cl.hbrBackground = GetStockBrush ( WHITE_BRUSH );
                    362:     cl.lpszMenuName = szAppName;
                    363:     cl.lpszClassName = szAppName;
                    364:    
                    365:     RegisterClass (&cl);
                    366: }
                    367: 
                    368: // Window Proc
                    369: //
                    370: 
                    371: LRESULT CALLBACK WndProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
                    372: {
                    373:     static CController ctrl;
                    374: 
                    375:     switch (message)
                    376:     {
                    377:         case WM_CREATE:
                    378:             ctrl.Create(hwnd, lParam);
                    379:             return 0;
                    380:         case WM_SIZE:
                    381:             ctrl.Size(lParam);
                    382:             return 0;
                    383:         case WM_PAINT:
                    384:             ctrl.Paint(hwnd);
                    385:             return 0;
                    386:         case WM_COMMAND:
                    387:             ctrl.Command(hwnd, wParam);
                    388:             return 0;
                    389:         case WM_LBUTTONUP:
                    390:             ctrl.ButtonUp(hwnd, lParam);
                    391:             return 0;
                    392:         case WM_LBUTTONDOWN:
                    393:             ctrl.ButtonDown(hwnd, lParam);
                    394:             return 0;
                    395:         case WM_KEYUP:
                    396:             ctrl.KeyUp(hwnd, wParam, lParam);
                    397:             return 0;
                    398:         case WM_KEYDOWN:
                    399:             ctrl.KeyDown(hwnd, wParam, lParam);
                    400:             return 0;
                    401:         case WM_VSCROLL:
                    402:             ctrl.VScroll(hwnd, wParam, lParam );
                    403:             return 0;
                    404:         case WM_HSCROLL:
                    405:             ctrl.HScroll(hwnd, wParam, lParam );
                    406:             return 0;
                    407:         case WM_DESTROY:
                    408:             ctrl.Destroy();
                    409:             return 0;
                    410: 
                    411:     }
                    412:     return DefWindowProc (hwnd, message, wParam, lParam );
                    413: }
                    414: 
                    415: // "About" dialog box procedure
                    416: // Process messages from dialog box
                    417: //
                    418: 
                    419: BOOL CALLBACK AboutDlgProc
                    420:    ( HWND hwnd, UINT message, WPARAM wParam, LONG lParam )
                    421: {
                    422: 
                    423:     switch(message)
                    424:     {
                    425:        case WM_INITDIALOG:
                    426:             return TRUE;
                    427:        case WM_COMMAND:
                    428:             switch (wParam) //Command ID
                    429:             {
                    430:                case IDOK:
                    431:                case IDCANCEL:
                    432:                     EndDialog(hwnd, 0);
                    433:                     return TRUE;
                    434:             }
                    435:             break;
                    436:     }
                    437:     return FALSE;
                    438: }
                    439: 
                    440: extern CPrintAux PrGlobal;
                    441: 
                    442: //======== PrintDlgProc ===========================================
                    443: 
                    444: BOOL CALLBACK PrintDlgProc
                    445:    ( HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam )
                    446: {
                    447:     switch (message)
                    448:     {
                    449:         case WM_INITDIALOG:
                    450:              SetWindowText (hwndDlg, (TCHAR *)lParam) ;
                    451:              EnableMenuItem (GetSystemMenu (hwndDlg, FALSE), SC_CLOSE, MF_GRAYED) ;
                    452:              return TRUE ;
                    453: 
                    454:         case WM_COMMAND:
                    455:              PrGlobal._bUserAbort = TRUE ;
                    456:              EnableWindow (GetParent (hwndDlg), TRUE) ;
                    457:              DestroyWindow (hwndDlg) ;
                    458:              PrGlobal._hDlgPrint = 0 ;
                    459:              return TRUE ;
                    460:     }
                    461:     return FALSE ;
                    462: }
                    463: 
                    464: //======== AbortProc ===========================================
                    465: 
                    466: BOOL CALLBACK AbortProc(HDC hdcPrn, short nCode)
                    467: {
                    468:     MSG   msg ;
                    469: 
                    470:     while (!PrGlobal._bUserAbort && PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
                    471:     {
                    472:         if (!PrGlobal._hDlgPrint 
                    473:                 || 
                    474:             !IsDialogMessage (PrGlobal._hDlgPrint, &msg))
                    475:         {
                    476:             TranslateMessage (&msg) ;
                    477:             DispatchMessage (&msg) ;
                    478:         }
                    479:     }
                    480:     return !PrGlobal._bUserAbort ;
                    481: }
                    482: 
                    483: 

unix.superglobalmegacorp.com

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