home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / muzsrc1.zip / SYMBOLS.CPP < prev    next >
C/C++ Source or Header  |  1992-07-22  |  7KB  |  207 lines

  1. // **********************************************
  2. // File: SYMBOLS.CPP
  3. // Symbol-treating module
  4.  
  5. #include "muzika.h"
  6.  
  7. BOOL editModeSymbolActive = TRUE; // TRUE = one of the three constant symbols
  8.  
  9. EDITMODE activeEditMode = PENCIL; // The active among the constant symbols
  10.  
  11. int activeSymbolSet = 1;          // The active symbol set on the screen left
  12. int activeSymbol = 0;             // The active symbol within the symbol set
  13. SymbolClass *currentSymbol = NULL; // The current symbol object
  14.  
  15. // **********************************************
  16. // DisplayEditModeSymbols displays the edit mode symbols
  17. // (i.e. the three constant symbols at the screen top).
  18. // If one of them is selected, it is displayed in reverse video.
  19.  
  20. void DisplayEditModeSymbols(HDC hDC)
  21. {
  22.   HDC hBitmapDC = CreateCompatibleDC(hDC);
  23.   HBITMAP hBitmap, hOldBitmap;
  24.   COLORREF hOldColor, hOldBkColor;
  25.   char name[9] = "B_";
  26.  
  27.   // Select a bitmap color
  28.   hOldColor = SetTextColor(hDC, melodyExists ? 0 : GetSysColor(COLOR_GRAYTEXT));
  29.   hOldBkColor = SetBkColor(hDC, GetSysColor(COLOR_WINDOW));
  30.  
  31.   // Draw the pencil, eraser, and hand bitmaps
  32.   for (EDITMODE index = PENCIL; index <= HAND; ++index) {
  33.     LoadString(hInst, EDITMODESTRINGS+index, name+2, sizeof(name)-2);
  34.     hBitmap = LoadBitmap(hInst, name);
  35.     hOldBitmap = SelectObject(hBitmapDC, hBitmap);
  36.  
  37.     // Put the bitmap in normal or reverse video
  38.     if (melodyExists && editModeSymbolActive && index == activeEditMode)
  39.       BitBlt(hDC, 2+36*index, 2, 32, 32, hBitmapDC, 0, 0, NOTSRCCOPY);
  40.     else
  41.       BitBlt(hDC, 2+36*index, 2, 32, 32, hBitmapDC, 0, 0, SRCCOPY);
  42.     SelectObject(hBitmapDC, hOldBitmap);
  43.     DeleteObject(hBitmap);
  44.   }
  45.  
  46.   // Delete the unneeded display context
  47.   SetTextColor(hDC, hOldColor);
  48.   SetBkColor(hDC, hOldBkColor);
  49.   DeleteDC(hBitmapDC);
  50. }
  51.  
  52. // **********************************************
  53. // IdentifyEditModeSymbol checks whether the given cursor position
  54. // (on which the mouse has been clicked) is in the region of
  55. // one of the edit mode symbols. If so, the symbol is marked as active.
  56.  
  57. BOOL IdentifyEditModeSymbol(DWORD position)
  58. {
  59.   HDC hDC;
  60.   POINT pos = MAKEPOINT(position);
  61.   char name[9] = "C_";
  62.  
  63.   // Don't identify anything if no melody
  64.   if (!melodyExists)
  65.     return FALSE;
  66.  
  67.   // See if any edit-mode symbol has been clicked
  68.   if (pos.y >= 2 && pos.y <= 33)
  69.     for (EDITMODE index = PENCIL; index <= HAND; ++index)
  70.       if (pos.x >= 2+36*index && pos.x <= 33+36*index) {
  71.         // Identification succeeded:
  72.         // mark the required symbol as active
  73.         // and redisplay the edit mode symbols
  74.         editModeSymbolActive = TRUE;
  75.         activeEditMode = index;
  76.         hDC = GetDC(hMainWnd);
  77.         DisplayEditModeSymbols(hDC);
  78.         RefreshSymbols(hDC);
  79.         ReleaseDC(hMainWnd, hDC);
  80.         LoadString(hInst, EDITMODESTRINGS+index, name+2, sizeof(name)-2);
  81.         hEditCursor = LoadCursor(hInst, name);
  82.         return TRUE;
  83.       }
  84.  
  85.   // Identification failed: return FALSE
  86.   return FALSE;
  87. }
  88.  
  89. // **********************************************
  90. // RefreshSymbols redraws the current symbol set
  91. // at the screen left, presumably after the user selected a new set.
  92.  
  93. void RefreshSymbols(HDC hDC)
  94. {
  95.   HDC hBitmapDC = CreateCompatibleDC(hDC);
  96.   COLORREF hOldColor, hOldBkColor;
  97.   HBRUSH hBrush, hOldBrush;
  98.  
  99.   // Select a bitmap color
  100.   hOldColor = SetTextColor(hDC, melodyExists ? 0 : GetSysColor(COLOR_GRAYTEXT));
  101.   hOldBkColor = SetBkColor(hDC, GetSysColor(COLOR_WINDOW));
  102.   hBrush = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  103.   hOldBrush = SelectObject(hDC, hBrush);
  104.  
  105.   // Load and display the symbol bitmaps
  106.   int lastDrawn;
  107.   for (int index = 0; symbolArray[index]->GetID() < 0x10*(activeSymbolSet+1);
  108.     index++)
  109.     if ((lastDrawn = symbolArray[index]->GetID()) >= 0x10*activeSymbolSet)
  110.       symbolArray[index]->DrawSymbol(hDC, hBitmapDC,
  111.         melodyExists && !editModeSymbolActive &&
  112.           symbolArray[index]->GetID() == activeSymbol);
  113.  
  114.   // Fill the remaining slots with the empty symbol
  115.   while (++lastDrawn % 0x10)
  116.     nullSymbol.DrawSymbol(hDC, lastDrawn);
  117.  
  118.   // Delete the unnecessary Windows objects
  119.   SelectObject(hDC, hOldBrush);
  120.   DeleteObject(hBrush);
  121.   SetTextColor(hDC, hOldColor);
  122.   SetBkColor(hDC, hOldBkColor);
  123.   DeleteDC(hBitmapDC);
  124. }
  125.  
  126. // **********************************************
  127. // IdentifySymbol checks whether the given cursor position
  128. // (on which the mouse has been clicked) is in the region of
  129. // one of the symbols on the screen left.
  130.  
  131. SymbolClass *IdentifySymbol(DWORD position)
  132. {
  133.   HDC hDC;
  134.   POINT pos = MAKEPOINT(position);
  135.  
  136.   // Don't identify anything if no melody
  137.   if (!melodyExists)
  138.     return FALSE;
  139.  
  140.   // Calculate the coordinates of the clicked symbol
  141.   if ((pos.x-2)%36 <= 31 && (pos.y-38)%24 <= 19 && pos.x <= 72 && pos.y >= 38) {
  142.     int ix = pos.x/36;
  143.     int iy = (pos.y-36)/24;
  144.     if (iy < 0x10/2) {
  145.       // The cursor was actually on the symbol range at the left side:
  146.       // search for the symbol object with the correct ID
  147.       int index = iy*2+ix+0x10*activeSymbolSet;
  148.       for (SymbolClass **p = symbolArray; (*p)->GetID() < index; ++p);
  149.       if ((*p)->GetID() == index) {
  150.         // The symbol has been found:
  151.         // set the active symbol, refresh the screen and return the pointer
  152.         editModeSymbolActive = FALSE;
  153.         activeSymbol = activeSymbolSet*0x10+iy*2+ix;
  154.         hDC = GetDC(hMainWnd);
  155.         DisplayEditModeSymbols(hDC);
  156.         RefreshSymbols(hDC);
  157.         ReleaseDC(hMainWnd, hDC);
  158.         hEditCursor = LoadCursor(NULL, IDC_ARROW);
  159.         return (currentSymbol = *p);
  160.       }
  161.     }
  162.   }
  163.  
  164.   // No symbol identified: return a null pointer
  165.   return NULL;
  166. }
  167.  
  168. // **********************************************
  169. // SelectSymbolSet changes the current symbol set
  170. // in response to the user's selection at the Symbols submenu.
  171. // The screen is updated with the new set, and the first
  172. // symbol in the set is chosen as the current one.
  173.  
  174. void SelectSymbolSet(int set)
  175. {
  176.   activeSymbolSet = set;
  177.   activeSymbol = set*0x10;
  178.   editModeSymbolActive = FALSE;
  179.   hEditCursor = LoadCursor(NULL, IDC_ARROW);
  180.  
  181.   // Search for the first symbol object belonging to this set
  182.   for (SymbolClass **p = symbolArray; (*p)->GetID() < 0x10*set; ++p);
  183.   currentSymbol = *p;
  184.  
  185.   // Refresh the screen with the new symbols
  186.   HDC hDC = GetDC(hMainWnd);
  187.   DisplayEditModeSymbols(hDC);
  188.   RefreshSymbols(hDC);
  189.   ReleaseDC(hMainWnd, hDC);
  190. }
  191.  
  192. // **********************************************
  193. // GetActiveSymbol returns the ID of the current symbol.
  194.  
  195. int GetActiveSymbol()
  196. {
  197.   return editModeSymbolActive ? activeEditMode : activeSymbol;
  198. }
  199.  
  200. // **********************************************
  201. // GetCurrentSymbol returns a pointer to the current symbol object.
  202.  
  203. SymbolClass *GetCurrentSymbol()
  204. {
  205.   return editModeSymbolActive ? NULL : currentSymbol;
  206. }
  207.