home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / beaversweeper_v101.zip / src / trackerView.cpp < prev    next >
C/C++ Source or Header  |  2005-10-23  |  12KB  |  481 lines

  1. // trackerView.cpp : implementation of the CPatternView class
  2. //
  3. /*
  4.  
  5. TODO:
  6.     Copy/paste buggen
  7.     Fixa distortion volume
  8.     Ta bort fula dialog boxen
  9.     BΣttre CPU mΣtare
  10.     Se till att man kan stΣnga Scopes f÷nstret 
  11.     Effektivisera scopes f÷nstret...
  12.     Effekt kedjor (ned prioriterat, frσn min sida, jag har inte grepp om hur jag ska lΣgga upp det )
  13.  
  14. */
  15.  
  16. #include "stdafx.h"
  17. #include "tracker.h"
  18.  
  19. #include "mainfrm.h"
  20. #include "gtkformat.h"
  21. #include "trackerDoc.h"
  22. #include "trackerView.h"
  23. #include "Globals.h"
  24. #include "MIDIInput.h"
  25.  
  26. #ifdef _DEBUG
  27. #define new DEBUG_NEW
  28. #undef THIS_FILE
  29. static char THIS_FILE[] = __FILE__;
  30. #endif
  31.  
  32. #define SETHIBYTE(a,b) ((a)=((a)&0x0f)|(((b)<<4)&0xf0));
  33. #define SETLOWBYTE(a,b) ((a)=((a)&0xf0)|((b)&0x0f));
  34. //            posPtr->fx = (posPtr->fx & 0x0f) | ((val << 4) & 0xf0);
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CPatternView
  38.  
  39. IMPLEMENT_DYNCREATE(CPatternView, CDblBuffView)
  40.  
  41. BEGIN_MESSAGE_MAP(CPatternView, CDblBuffView)
  42.     //{{AFX_MSG_MAP(CPatternView)
  43.     ON_WM_ERASEBKGND()
  44.     ON_WM_KEYDOWN()
  45.     ON_WM_SIZE()
  46.     ON_COMMAND(ID_ACTION_PLAY, OnActionPlay)
  47.     ON_COMMAND(ID_ACTION_STOP, OnActionStop)
  48.     ON_WM_HSCROLL()
  49.     ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  50.     ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  51.     ON_WM_SHOWWINDOW()
  52.     //}}AFX_MSG_MAP
  53.     // Standard printing commands
  54.     ON_COMMAND(ID_FILE_PRINT, CDblBuffView::OnFilePrint)
  55.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CDblBuffView::OnFilePrint)
  56.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CDblBuffView::OnFilePrintPreview)
  57. END_MESSAGE_MAP()
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CPatternView construction/destruction
  61.  
  62. CPatternView::CPatternView()
  63. {
  64.     // TODO: add construction code here
  65.     seqpatscr = 0;
  66.  
  67.  
  68. }
  69.  
  70. CPatternView::~CPatternView()
  71. {
  72.     g_midi_input->endmsgdest( m_hWnd );
  73. }
  74.  
  75. BOOL CPatternView::PreCreateWindow(CREATESTRUCT& cs)
  76. {
  77.     // TODO: Modify the Window class or styles here by modifying
  78.     //  the CREATESTRUCT cs
  79. /*
  80.     isTrkEdit = 0;
  81.     trackPtr = NULL;
  82. */
  83.     seqCursor.num = 0;
  84.     seqCursor.pos = 0;
  85.     seqCursor.col = 0;
  86.     seqCursor.rowcol = 0;
  87.     patCursor.num = 0;
  88.     patCursor.pos = 0;
  89.     patCursor.col = 0;
  90.     patCursor.rowcol = 0;
  91.  
  92.     isPatternEdit = 0;
  93.     seqPos = 0;
  94.     patternPos = 0;
  95.  
  96.  
  97.     return CDblBuffView::PreCreateWindow(cs);
  98. }
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. // CPatternView drawing
  102. void CPatternView::OnDraw(CDC* pDC)
  103. {
  104.   CMainFrame *mf;
  105.  
  106.   mf = (CMainFrame *)AfxGetMainWnd();
  107.   if (mf->m_trackerControls.m_scrollData)
  108.   {
  109.     DrawPattern (pDC);
  110.     DrawSequencer (pDC);
  111.   }
  112.     
  113. /*
  114.     CTrackerDoc *pDoc = GetDocument ();
  115.     char buffer[256];
  116.     sprintf(buffer, "Tick=%02d Seqpos=%02d Pattern=%02d Patternrow=%02d",0,pDoc->play_seqpos,pDoc->play_pattern,pDoc->play_patternrow);
  117.     pDC->TextOut (0,0,buffer);
  118. */
  119. }
  120.  
  121. /////////////////////////////////////////////////////////////////////////////
  122. // CPatternView printing
  123.  
  124. BOOL CPatternView::OnPreparePrinting(CPrintInfo* pInfo)
  125. {
  126.     // default preparation
  127.     return DoPreparePrinting(pInfo);
  128. }
  129.  
  130. void CPatternView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  131. {
  132.     // TODO: add extra initialization before printing
  133. }
  134.  
  135. void CPatternView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  136. {
  137.     // TODO: add cleanup after printing
  138. }
  139.  
  140. /////////////////////////////////////////////////////////////////////////////
  141. // CPatternView diagnostics
  142.  
  143. #ifdef _DEBUG
  144. void CPatternView::AssertValid() const
  145. {
  146.     CDblBuffView::AssertValid();
  147. }
  148.  
  149. void CPatternView::Dump(CDumpContext& dc) const
  150. {
  151.     CDblBuffView::Dump(dc);
  152. }
  153.  
  154. CTrackerDoc* CPatternView::GetDocument() // non-debug version is inline
  155. {
  156.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTrackerDoc)));
  157.     return (CTrackerDoc*)m_pDocument;
  158. }
  159. #endif //_DEBUG
  160.  
  161.  
  162. /////////////////////////////////////////////////////////////////////////////
  163. // CPatternView message handlers
  164.  
  165. BOOL CPatternView::OnEraseBkgnd(CDC* pDC) 
  166. {
  167.     // TODO: Add your message handler code here and/or call default
  168.     
  169.     // Set brush to desired background color.
  170.     return TRUE;
  171. }
  172. static int legal_note_num[]=
  173. {
  174.     1,2,3,4,5,6,7,8,9,10,11,12,13,14,
  175.     12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
  176. };
  177. static char *legal_note_char="ZSXDCVGBHNJM,L.Q2W3ER5T6Y7UI9O0P";
  178. static char *legal_trkctrl_char="KCV";
  179. static char *legal_num_char="0123456789ABCDEF";
  180.  
  181.  
  182. void CPatternView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
  183. {
  184.     // TODO: Add your message handler code here and/or call default
  185.     CTrackerDoc *pDoc = GetDocument ();
  186.  
  187.     switch (nChar)
  188.     {
  189.         case VK_F7 :
  190.             OnActionPlay ();
  191.             break;
  192.         case VK_F8 :
  193.             OnActionStop ();
  194.             break;
  195. /*
  196.         case VK_CONTROL :
  197.             if (pDoc->playing)
  198.                 OnActionStop ();
  199.             else
  200.                 OnActionPlay ();
  201.             break;    
  202. */
  203.         default :
  204.             if (isPatternEdit) ProcessPatternKey (nChar,nRepCnt,nFlags);
  205.             else ProcessSequencerKey (nChar, nRepCnt,nFlags);
  206.  
  207.     }
  208.     
  209.  
  210.     CDblBuffView::OnKeyDown(nChar, nRepCnt, nFlags);
  211. }
  212.  
  213.  
  214. /*
  215. void CPatternView::OnFileOpen() 
  216. {
  217.     // TODO: Add your command handler code here
  218.     
  219. }
  220. */
  221.  
  222.  
  223. static void WINAPI UpdateCallback(LPVOID passed)
  224. {
  225.     float cpu;
  226.     DWORD buflen;
  227.     float *bufpek;
  228.     char buffer[128];
  229.     CPatternView *pv = (CPatternView *)passed;
  230.     CMainFrame *mf;
  231.  
  232.     CTrackerDoc* pDoc = pv->GetDocument();
  233.     int chn=pv->seqCursor.col;
  234.  
  235.     pDoc->pl->GetCurrentTick();
  236.     pDoc->pl->GetCurrentPosition( chn, pDoc->play_seqpos, pDoc->play_pattern, pDoc->play_patternrow );  
  237.     pv->InvalidateRect (NULL,FALSE);
  238.  
  239.     cpu=pDoc->synth->GetCPUUsage();
  240.     sprintf (buffer,"%f%",cpu);
  241.     mf=(CMainFrame *)AfxGetMainWnd();
  242.     mf=(CMainFrame *)AfxGetApp()->GetMainWnd();
  243.  
  244.     if (mf && mf->IsKindOf(RUNTIME_CLASS(CMainFrame)))
  245.     {
  246.         mf->SendMessage(WM_USER+10,0,(LPARAM)buffer);
  247.         bufpek=pDoc->pl->GetLeftPlayingBuffer(&buflen);
  248.         mf->m_trackerScopes.PrepareUpdate(0,bufpek,buflen);
  249.         mf->m_trackerScopes.Update();
  250.         bufpek=pDoc->pl->GetRightPlayingBuffer(&buflen);
  251.         mf->m_trackerScopes.PrepareUpdate(1,bufpek,buflen);
  252.         mf->m_trackerScopes.Update();
  253.         //  mf->m_trackerScopes.Invalidate(FALSE);
  254.     }
  255. }
  256.  
  257. void CPatternView::OnActionPlay() 
  258. {
  259.     // TODO: Add your command handler code here
  260.     CTrackerDoc* pDoc = GetDocument();
  261.  
  262.     if (!pDoc->playing)
  263.     {
  264.         CMainFrame *mfrm;
  265.         mfrm = (CMainFrame *)AfxGetMainWnd();
  266.         mfrm->m_trackerScopes.CreateScopes(2);
  267. //          pDoc->pl->Start (pDoc->gtk);
  268.         if (pDoc->gtk != NULL)
  269.         {
  270.             pDoc->pl->Start (pDoc->gtk, seqCursor.pos );
  271.             pDoc->pl->SetUpdateCallback (UpdateCallback,(LPVOID)this);
  272.             pDoc->playing = 1;
  273.         }
  274.     }
  275. }
  276.  
  277. void CPatternView::OnActionStop() 
  278. {
  279.     // TODO: Add your command handler code here
  280.     CTrackerDoc* pDoc = GetDocument();
  281.  
  282.   CMainFrame *mfrm;
  283.   mfrm = (CMainFrame *)AfxGetMainWnd();
  284.   mfrm->m_trackerScopes.CreateScopes(2);
  285.   
  286.     if (pDoc->playing)
  287.     {
  288.         pDoc->pl->Stop ();
  289.     }
  290.  
  291.     pDoc->playing = 0;
  292. }
  293. void CPatternView::OnSize(UINT nType, int cx, int cy) 
  294. {
  295.     CDblBuffView::OnSize(nType, cx, cy);
  296.     
  297.     // TODO: Add your message handler code here
  298.     if (seqpatscr)
  299.     {
  300.         CRect rect;
  301.         seqScroller.GetWindowRect (&rect);
  302.         ScreenToClient (&rect);
  303.         rect.right=cx;    
  304.         seqScroller.MoveWindow (&rect);
  305.  
  306.         patScroller.GetWindowRect (&rect);
  307.         ScreenToClient (&rect);
  308.         rect.right=cx;    
  309.          rect.top = cy-12;
  310.         rect.bottom = cy;
  311.         patScroller.MoveWindow (&rect);
  312.     }
  313.  
  314. }
  315.  
  316. void CPatternView::OnInitialUpdate() 
  317. {
  318.     CDblBuffView::OnInitialUpdate();
  319.  
  320.  
  321.     CRect crect;
  322.     GetClientRect (&crect);
  323.     CRect seqRect (0,128-16,crect.Width (),128);;
  324.     CRect patRect (0,crect.Height ()-10,crect.Width (),crect.Height ());
  325.  
  326.     
  327.     seqScroller.Create (WS_CHILD|WS_VISIBLE|SBS_HORZ|SBS_BOTTOMALIGN,seqRect,this,0x666);
  328.     seqScroller.SetScrollRange (0,16);
  329.     seqScroller.SetScrollPos (0);
  330.  
  331.     patScroller.Create (WS_CHILD|WS_VISIBLE|SBS_HORZ|SBS_BOTTOMALIGN,patRect,this,0x666);    
  332.     patScroller.SetScrollRange (0,16);
  333.     patScroller.SetScrollPos (0);
  334.     seqpatscr=1;
  335.  
  336.   CMainFrame *mfrm;
  337.   mfrm = (CMainFrame *)AfxGetMainWnd();
  338.   mfrm->m_trackerScopes.CreateScopes(2);
  339.   
  340.     // TODO: Add your specialized code here and/or call the base class
  341. }
  342.  
  343. void CPatternView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
  344. {
  345.     // TODO: Add your message handler code here and/or call default
  346.  
  347.     int ps;
  348.     int minpos;
  349.     int maxpos;
  350.     pScrollBar->GetScrollRange(&minpos, &maxpos); 
  351.     maxpos = pScrollBar->GetScrollLimit();
  352.  
  353.    // Get the current position of scroll box.
  354.     int curpos = pScrollBar->GetScrollPos();
  355.  
  356.     if (pScrollBar != &seqScroller) ps = 1;
  357.     else ps = 0;
  358.  
  359.     switch (nSBCode)
  360.     {
  361.         case SB_LEFT:      // Scroll to far left.
  362.             curpos = minpos;
  363.             break;
  364.  
  365.         case SB_RIGHT:      // Scroll to far right.
  366.             curpos =maxpos;
  367.             break;
  368.  
  369.         case SB_ENDSCROLL:   // End scroll.
  370.             break;
  371.  
  372.         case SB_LINELEFT:      // Scroll left.
  373.             if (curpos > minpos)
  374.                  curpos--;
  375.  
  376.             break;
  377.  
  378.         case SB_LINERIGHT:   // Scroll right.
  379.             if (curpos < maxpos)
  380.                  curpos++;
  381.             break;
  382.  
  383.         case SB_PAGELEFT:    // Scroll one page left.
  384.         {
  385.             // Get the page size. 
  386.             SCROLLINFO   info;
  387.             pScrollBar->GetScrollInfo(&info, SIF_ALL);
  388.  
  389.             if (curpos > minpos)
  390.             curpos = max(minpos, curpos - (int) info.nPage);
  391.         }
  392.             break;
  393.  
  394.         case SB_PAGERIGHT:      // Scroll one page right.
  395.         {
  396.             // Get the page size. 
  397.             SCROLLINFO   info;
  398.             pScrollBar->GetScrollInfo(&info, SIF_ALL);
  399.  
  400.             if (curpos < maxpos)
  401.                  curpos = min(maxpos, curpos + (int) info.nPage);
  402.         }
  403.             break;
  404.  
  405.         case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
  406.             curpos = nPos;      // of the scroll box at the end of the drag operation.
  407.             break;
  408.  
  409.         case SB_THUMBTRACK:   // Drag scroll box to specified position. nPos is the
  410.             curpos = nPos;     // position that the scroll box has been dragged to.
  411.             break;
  412.     }
  413.  
  414.    // Set the new position of the thumb (scroll box).
  415.     pScrollBar->SetScrollPos(curpos);
  416.  
  417. //    if (!ps) SetSequencerCursor (curpos,2);
  418.  
  419.  
  420.     CTrackerDoc* pDoc = GetDocument();
  421.   pDoc->UpdateAllViews (NULL);
  422.   
  423.  
  424.     CDblBuffView::OnHScroll(nSBCode, nPos, pScrollBar);
  425.   
  426. }
  427.  
  428. void CPatternView::OnEditCopy() 
  429. {
  430.     // TODO: Add your command handler code here
  431.   CTrackerDoc* pDoc = GetDocument();
  432.   if (pDoc->editData.state == GTK_EDITSTATE_PATTERNSELECT)
  433.   {
  434.     CopyPatternBlock();
  435.     pDoc->editData.state = GTK_EDITSTATE_DEFAULT;
  436.     pDoc->UpdateAllViews(NULL);
  437.   }
  438.     
  439.     
  440. }
  441.  
  442. void CPatternView::OnEditPaste() 
  443. {
  444.     // TODO: Add your command handler code here
  445.     
  446.   CTrackerDoc* pDoc = GetDocument();
  447.   if (pDoc->editData.copybuffer.hasdata)
  448.   {
  449.     PastePatternBlock();
  450.     pDoc->editData.state = GTK_EDITSTATE_DEFAULT;
  451.     pDoc->UpdateAllViews(NULL);
  452.   }
  453. }
  454.  
  455. LRESULT CPatternView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  456. {
  457.     // TODO: Add your specialized code here and/or call the base class
  458.     MIDIReceptor::FilterMidiMsg( message, wParam, lParam );
  459.     return CDblBuffView::WindowProc(message, wParam, lParam);
  460. }
  461.  
  462. void CPatternView::OnShowWindow(BOOL bShow, UINT nStatus) 
  463. {
  464.     CDblBuffView::OnShowWindow(bShow, nStatus);
  465.     // TODO: Add your message handler code here
  466.  
  467.     g_midi_input->setmsgdest( m_hWnd );
  468.     
  469. }
  470.  
  471. void CPatternView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
  472. {
  473.     // TODO: Add your specialized code here and/or call the base class
  474.     if (TRUE == bActivate) {
  475.         g_midi_input->setmsgdest( m_hWnd );
  476.     } else {
  477.         g_midi_input->endmsgdest( m_hWnd );
  478.     }    
  479.     CDblBuffView::OnActivateView(bActivate, pActivateView, pDeactiveView);
  480. }
  481.