home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / beaversweeper_v101.zip / src / AEWindow.cpp next >
C/C++ Source or Header  |  2003-01-06  |  8KB  |  391 lines

  1. // AEWindow.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. //#include "tracker.h"
  6. #include "AEWindow.h"
  7. #include "isInstrument.h"
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CAEWindow
  17.  
  18. CAEWindow::CAEWindow()
  19. {
  20.     m_posx = 1;
  21.     m_posy = 1;
  22.     m_digitno = 0;
  23.     m_cols = 7;
  24.     m_rows = 33;
  25.     m_isedit = FALSE;    
  26. }
  27.  
  28. CAEWindow::~CAEWindow()
  29. {
  30.     enterLeave();
  31.  
  32. }
  33.  
  34.  
  35. BEGIN_MESSAGE_MAP(CAEWindow, CWnd)
  36.     //{{AFX_MSG_MAP(CAEWindow)
  37.     ON_WM_PAINT()
  38.     ON_WM_SETFOCUS()
  39.     ON_WM_KEYDOWN()
  40.     ON_WM_SYSKEYDOWN()
  41.     ON_WM_GETDLGCODE()
  42.     ON_WM_CHAR()
  43.     ON_WM_LBUTTONDOWN()
  44.     //}}AFX_MSG_MAP
  45. END_MESSAGE_MAP()
  46.  
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CAEWindow message handlers
  50.  
  51. void CAEWindow::OnPaint() 
  52. {
  53.  
  54.     CPaintDC dc(this); // device context for painting
  55.     RECT dcr;
  56.     CWnd::GetClientRect( &dcr );
  57.     CWnd::GetClientRect( &m_field );
  58.     
  59.     // TODO: Add your message handler code here    
  60.     CDC dcc;
  61.     dcc.CreateCompatibleDC(0);
  62.     CBitmap bmp;
  63.     bmp.CreateCompatibleBitmap( &dc, dcr.right, dcr.bottom );
  64.     dcc.SelectObject( &bmp );
  65.     CDC* pDC = &dcc;
  66.  
  67.     pDC->PatBlt( 0, 0, dcr.right, dcr.bottom, BLACKNESS );
  68.  
  69.     CGdiObject *old_pen, *old_brush, *old_font;
  70.  
  71.     DWORD col1 = RGB(255,255,255);
  72.     DWORD col2 = RGB(64,78,92);
  73.     DWORD col3 = RGB(255,255,0);
  74.     DWORD col4 = RGB(50,255,50);
  75.     DWORD bkcolor = RGB(0,0,0);
  76.  
  77.     CPen pen1( PS_SOLID, 1, col1 );
  78.     CPen pen2( PS_SOLID, 1, col2 );
  79.     CBrush brush( bkcolor );
  80.  
  81.     old_pen = pDC->SelectObject( &pen2 );
  82.     old_brush = pDC->SelectObject( &brush );            
  83.  
  84.     pDC->SetTextColor( col1 );
  85.     pDC->SetBkColor( bkcolor );
  86.  
  87.     CFont font;
  88.     font.CreateFont(11,0,0,0,0,0,0,0,0,0,0,0,0,"Lucida Console");
  89.  
  90.  
  91.     old_font = pDC->SelectObject( &font );
  92.  
  93.     for (int y=0;y<m_rows;y++) {
  94.         for (int x=0;x<8;x++) {
  95.             RECT r;
  96.             getItemRect( x, y, &r );
  97.             // RΣkna ut rect f÷r denna
  98.             pDC->Rectangle( &r );
  99.         }
  100.     }
  101.     
  102.     RECT r;
  103.     getItemRect( m_posx, m_posy, &r );
  104.     pDC->SelectObject( &pen1 );
  105.     pDC->Rectangle( &r );
  106.     
  107.     for (y=0;y<m_rows;y++) {
  108.  
  109.         for (int x=0;x<8;x++) {    
  110.  
  111.             if (!y)
  112.                 pDC->SetTextColor( col1 );
  113.             else {
  114.  
  115.                 if (x == 0)
  116.                     pDC->SetTextColor( col2 );
  117.                 if (x >= 1 && x <= 3)
  118.                     pDC->SetTextColor( col3 );
  119.                 if (x > 3)
  120.                     pDC->SetTextColor( col4 );
  121.  
  122.             }
  123.  
  124.             _TCHAR tmp[32];
  125.             _tcscpy( tmp, _T("") );
  126.             getItemRect( x, y, &r );
  127.             getItemText( x, y, tmp );
  128.  
  129.             pDC->DrawText( tmp, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
  130.  
  131.         }        
  132.     }
  133.  
  134.     pDC->SelectObject( old_pen );
  135.     pDC->SelectObject( old_brush );
  136.     pDC->SelectObject( old_font );
  137.  
  138.  
  139.     dc.BitBlt( 0, 0, dcr.right, dcr.bottom, pDC, 0, 0, SRCCOPY );
  140.  
  141.     bmp.DeleteObject();
  142.     dcc.DeleteDC();
  143.         
  144.     // Do not call CWnd::OnPaint() for painting messages
  145. }
  146.  
  147. BOOL CAEWindow::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
  148. {
  149.     // TODO: Add your specialized code here and/or call the base class
  150.     BOOL m = CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);        
  151.  
  152.     enterEdit();
  153.  
  154.  
  155.     return m;
  156. }
  157.  
  158.  
  159. void CAEWindow::getItemRect( int x, int y, RECT *rect )
  160. {
  161.     int s_x = (m_field.right - m_field.left) / m_cols;
  162.     int s_y = (m_field.bottom - m_field.top) / m_rows;
  163.     int ofs_y = 0; //(m_field.bottom - m_field.top - s_y) / 2;
  164.     int real_y = y;
  165.     rect->left = m_field.left + x * s_x;
  166.     rect->right = rect->left + s_x + 1;
  167.     rect->top = m_field.top + real_y * s_y + ofs_y;
  168.     rect->bottom = rect->top + s_y + 1;            
  169. }
  170.  
  171. void CAEWindow::getItemText( int x, int y, _TCHAR *str )
  172. {
  173.     if (x == m_posx && y == m_posy && TRUE == m_isedit) {
  174.         _tcscpy( str, m_editstr );
  175.         return;
  176.     }
  177.  
  178.     if (!y) {
  179.  
  180.         switch (x) {                    
  181.             case 0:
  182.                 _tcscpy( str, _T("pos") );
  183.                 break;
  184.             case 1:
  185.                 _tcscpy( str, _T("note1") );
  186.                 break;
  187.             case 2:
  188.                 _tcscpy( str, _T("note2") );
  189.                 break;
  190.             case 3:
  191.                 _tcscpy( str, _T("note3") );
  192.                 break;
  193.             case 4:
  194.                 _tcscpy( str, _T("velocity") );
  195.                 break;
  196.             case 5:
  197.                 _tcscpy( str, _T("length") );
  198.                 break;
  199.             case 6:
  200.                 _tcscpy( str, _T("octave") );
  201.                 break;
  202.         }
  203.     } else {
  204.  
  205.         if (!x) {
  206.             _stprintf(str,_T("%02d"),y-1);            
  207.         } else {        
  208.             if (x >= 1 && x <= 3 ) {
  209.                 // not
  210.                 unsigned char _naat = m_instrument->arpeggiator.step[y-1].index[x-1];
  211.                 if (_naat == 255) {
  212.                     _tcscpy( str, _T("") );
  213.                 } else
  214.                     _stprintf(str,_T("%02d"),_naat + 1);                
  215.             }
  216.             if (x == 4) 
  217.                 _stprintf(str,_T("%02d"),m_instrument->arpeggiator.step[y-1].velocity );        
  218.             if (x == 5)
  219.                 _stprintf(str,_T("%02d"),m_instrument->arpeggiator.step[y-1].length );
  220.             if (x == 6)
  221.                 _stprintf(str,_T("%02d"),m_instrument->arpeggiator.step[y-1].octave );
  222.             
  223.         }
  224.     }
  225. }
  226.  
  227. void CAEWindow::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  228. {
  229.     if (_tcslen( m_editstr ) > 0 && nChar == VK_BACK)
  230.         m_editstr[ _tcslen( m_editstr ) - 1 ] = 0;
  231.         
  232.  
  233.  
  234.     switch (nChar) {    
  235.             break;
  236.  
  237.         case VK_DELETE:        
  238.             _tcscpy( m_editstr, _T("") );
  239.             break;
  240.  
  241.         case VK_DOWN:
  242.             enterLeave();
  243.             m_posy++;
  244.             if (m_posy >= m_rows)
  245.                 m_posy = m_rows - 1;
  246.             enterEdit();
  247.             break;
  248.         case VK_UP:
  249.             enterLeave();
  250.             m_posy--;
  251.             if (m_posy < 1)
  252.                 m_posy = 1;
  253.             enterEdit();
  254.             break;
  255.         case VK_LEFT:
  256.             enterLeave();
  257.             m_posx--;
  258.             if (m_posx < 1)
  259.                 m_posx = 1;
  260.             enterEdit();
  261.             break;
  262.         case VK_RIGHT:
  263.             enterLeave();
  264.             m_posx++;            
  265.             if (m_posx >= m_cols)
  266.                 m_posx = m_cols - 1;
  267.             enterEdit();
  268.             break;
  269.  
  270.         default:
  271.             CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
  272.             return;
  273.  
  274.     }
  275.     InvalidateRect(0);
  276.     UpdateWindow();
  277.     
  278. }
  279.  
  280. void CAEWindow::OnSetFocus(CWnd* pOldWnd) 
  281. {
  282.     CWnd::OnSetFocus(pOldWnd);
  283.     
  284.     // TODO: Add your message handler code here        
  285. }
  286.  
  287. void CAEWindow::OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  288. {
  289.     // TODO: Add your message handler code here and/or call default
  290.     
  291.     
  292.     CWnd::OnSysKeyDown(nChar, nRepCnt, nFlags);
  293. }
  294.  
  295. UINT CAEWindow::OnGetDlgCode() 
  296. {
  297.     // TODO: Add your message handler code here and/or call default    
  298.     return CWnd::OnGetDlgCode() | DLGC_WANTARROWS | DLGC_WANTCHARS | DLGC_WANTALLKEYS;
  299. }
  300.  
  301.  
  302.  
  303. void CAEWindow::enterEdit(void)
  304. {
  305.     m_isedit = FALSE;
  306.     getItemText( m_posx, m_posy, m_editstr );
  307.     m_isedit = TRUE;
  308.     InvalidateRect(0);
  309.     UpdateWindow();
  310. }
  311.  
  312. void CAEWindow::enterLeave(void)
  313. {
  314.     
  315.     if (m_posy < 1 || m_posy > m_rows) {
  316.         m_isedit = FALSE;
  317.         return;
  318.     }
  319.  
  320.     if (TRUE == m_isedit) {
  321.         int _val = atoi( m_editstr );
  322.  
  323.         if (m_posx >= 1 && m_posx <= 3) {
  324.             if (_tcslen( m_editstr ) == 0) 
  325.                 _val = 256;
  326.             else {
  327.                 if (_val < 1) _val = 1;
  328.                 if (_val > 16) _val = 16;
  329.             }
  330.             m_instrument->arpeggiator.step[m_posy-1].index[m_posx-1] = _val - 1;
  331.         }
  332.         if (m_posx == 4) {
  333.             if (_val < 0) _val = 0;
  334.             if (_val > 255) _val = 255;
  335.             m_instrument->arpeggiator.step[m_posy-1].velocity = _val;
  336.         }
  337.         if (m_posx == 5) {
  338.             if (_val < 0) _val = 0;
  339.             if (_val > 255) _val = 255;
  340.             m_instrument->arpeggiator.step[m_posy-1].length = _val;
  341.         }
  342.         if (m_posx == 6) {
  343.             if (_val < -128) _val = -128;
  344.             if (_val > 127) _val = 127;
  345.             m_instrument->arpeggiator.step[m_posy-1].octave = _val;
  346.         }
  347.     }
  348.     m_isedit = FALSE;
  349. }
  350.  
  351. void CAEWindow::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
  352. {
  353.     if (FALSE == m_isedit)
  354.         return;
  355.  
  356.     // TODO: Add your message handler code here and/or call default
  357.     if ((nChar >= '0' && nChar <= '9') || nChar == '+' || nChar == '-') {
  358.         _TCHAR add[2] = { (_TCHAR)nChar, 0 };
  359.         if (_tcslen( m_editstr ) < 4)
  360.             _tcscat( m_editstr, add );
  361.     }
  362.     
  363.     InvalidateRect(0);
  364.     UpdateWindow();
  365.     CWnd::OnChar(nChar, nRepCnt, nFlags);
  366. }
  367.  
  368. void CAEWindow::OnLButtonDown(UINT nFlags, CPoint point) 
  369. {
  370.     // TODO: Add your message handler code here and/or call default
  371.     SetForegroundWindow();
  372.     CDialog *prnt = (CDialog *) GetParent();
  373.     prnt->GotoDlgCtrl( this );
  374.  
  375.  
  376.     RECT r;
  377.     for (int y=0;y<m_rows;y++)
  378.         for (int x=0;x<m_cols;x++) {
  379.             getItemRect( x, y, &r );
  380.             if (point.x > r.left && point.x < r.right && point.y > r.top && point.y < r.bottom) {
  381.                 enterLeave();
  382.                 m_posx = x;
  383.                 m_posy = y;
  384.                 enterEdit();
  385.                 break;
  386.             }
  387.         }    
  388.     
  389.     CWnd::OnLButtonDown(nFlags, point);
  390. }
  391.