1
0

PatternClipboard.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * PatternClipboard.h
  3. * ------------------
  4. * Purpose: Implementation of the pattern clipboard mechanism
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include "ResizableDialog.h"
  12. #include "../soundlib/Snd_defs.h"
  13. OPENMPT_NAMESPACE_BEGIN
  14. struct PatternEditPos;
  15. class PatternRect;
  16. class CSoundFile;
  17. struct PatternClipboardElement
  18. {
  19. std::string content;
  20. CString description;
  21. };
  22. class PatternClipboard
  23. {
  24. friend class PatternClipboardDialog;
  25. public:
  26. enum PasteModes
  27. {
  28. pmOverwrite,
  29. pmMixPaste,
  30. pmMixPasteIT,
  31. pmPasteFlood,
  32. pmPushForward,
  33. };
  34. // Clipboard index type
  35. using clipindex_t = size_t;
  36. protected:
  37. // The one and only pattern clipboard object
  38. static PatternClipboard instance;
  39. // Active internal clipboard index
  40. clipindex_t m_activeClipboard = 0;
  41. // Internal clipboard collection
  42. std::vector<PatternClipboardElement> m_clipboards;
  43. PatternClipboardElement m_channelClipboard;
  44. PatternClipboardElement m_patternClipboard;
  45. public:
  46. // Copy a range of patterns to both the system clipboard and the internal clipboard.
  47. static bool Copy(const CSoundFile &sndFile, ORDERINDEX first, ORDERINDEX last, bool onlyOrders);
  48. // Copy a pattern selection to both the system clipboard and the internal clipboard.
  49. static bool Copy(const CSoundFile &sndFile, PATTERNINDEX pattern, PatternRect selection);
  50. // Copy a pattern or pattern channel to the internal pattern or channel clipboard.
  51. static bool Copy(const CSoundFile &sndFile, PATTERNINDEX pattern, CHANNELINDEX channel = CHANNELINDEX_INVALID);
  52. // Try pasting a pattern selection from the system clipboard.
  53. static bool Paste(CSoundFile &sndFile, PatternEditPos &pastePos, PasteModes mode, PatternRect &pasteRect, bool &orderChanged);
  54. // Try pasting a pattern selection from an internal clipboard.
  55. static bool Paste(CSoundFile &sndFile, PatternEditPos &pastePos, PasteModes mode, PatternRect &pasteRect, clipindex_t internalClipboard, bool &orderChanged);
  56. // Paste from pattern or channel clipboard.
  57. static bool Paste(CSoundFile &sndFile, PATTERNINDEX pattern, CHANNELINDEX channel = CHANNELINDEX_INVALID);
  58. // Copy one of the internal clipboards to the system clipboard.
  59. static bool SelectClipboard(clipindex_t which);
  60. // Switch to the next internal clipboard.
  61. static bool CycleForward();
  62. // Switch to the previous internal clipboard.
  63. static bool CycleBackward();
  64. // Set the maximum number of internal clipboards.
  65. static void SetClipboardSize(clipindex_t maxEntries);
  66. // Return the current number of clipboards.
  67. static clipindex_t GetClipboardSize() { return instance.m_clipboards.size(); };
  68. // Check whether patterns can be pasted from clipboard
  69. static bool CanPaste();
  70. protected:
  71. PatternClipboard() { SetClipboardSize(1); };
  72. static std::string GetFileExtension(const char *ext, bool addPadding);
  73. static std::string FormatClipboardHeader(const CSoundFile &sndFile);
  74. // Create the clipboard text for a pattern selection
  75. static std::string CreateClipboardString(const CSoundFile &sndFile, PATTERNINDEX pattern, PatternRect selection);
  76. // Parse clipboard string and perform the pasting operation.
  77. static bool HandlePaste(CSoundFile &sndFile, PatternEditPos &pastePos, PasteModes mode, const std::string &data, PatternRect &pasteRect, bool &orderChanged);
  78. // System-specific clipboard functions
  79. static bool ToSystemClipboard(const PatternClipboardElement &clipboard) { return ToSystemClipboard(clipboard.content); };
  80. static bool ToSystemClipboard(const std::string_view &data);
  81. static bool FromSystemClipboard(std::string &data);
  82. };
  83. class PatternClipboardDialog : public ResizableDialog
  84. {
  85. protected:
  86. // The one and only pattern clipboard dialog object
  87. static PatternClipboardDialog instance;
  88. // Edit class for clipboard name editing
  89. class CInlineEdit : public CEdit
  90. {
  91. protected:
  92. PatternClipboardDialog &parent;
  93. public:
  94. CInlineEdit(PatternClipboardDialog &dlg);
  95. DECLARE_MESSAGE_MAP();
  96. afx_msg void OnKillFocus(CWnd *newWnd);
  97. };
  98. CSpinButtonCtrl m_numClipboardsSpin;
  99. CListBox m_clipList;
  100. CInlineEdit m_editNameBox;
  101. int m_posX = -1, m_posY = -1;
  102. bool m_isLocked = true, m_isCreated = false;
  103. public:
  104. static void UpdateList();
  105. static void Show();
  106. static void Toggle()
  107. {
  108. if(instance.m_isCreated)
  109. instance.OnCancel();
  110. else
  111. instance.Show();
  112. }
  113. protected:
  114. PatternClipboardDialog();
  115. void DoDataExchange(CDataExchange *pDX) override;
  116. void OnOK() override;
  117. void OnCancel() override;
  118. afx_msg void OnNumClipboardsChanged();
  119. afx_msg void OnSelectClipboard();
  120. // Edit clipboard name
  121. afx_msg void OnEditName();
  122. void OnEndEdit(bool apply = true);
  123. DECLARE_MESSAGE_MAP();
  124. };
  125. OPENMPT_NAMESPACE_END