1
0

Ctrl_com.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Ctrl_com.cpp
  3. * ------------
  4. * Purpose: Song comments tab, upper panel.
  5. * Notes : (currently none)
  6. * Authors: Olivier Lapicque
  7. * OpenMPT Devs
  8. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  9. */
  10. #include "stdafx.h"
  11. #include "Mptrack.h"
  12. #include "Mainfrm.h"
  13. #include "Moddoc.h"
  14. #include "Globals.h"
  15. #include "Ctrl_com.h"
  16. #include "view_com.h"
  17. #include "InputHandler.h"
  18. #include "../soundlib/mod_specifications.h"
  19. //#define MPT_COMMENTS_LONG_LINES_WRAP
  20. //#define MPT_COMMENTS_LONG_LINES_TRUNCATE
  21. #define MPT_COMMENTS_MARGIN 4
  22. OPENMPT_NAMESPACE_BEGIN
  23. BEGIN_MESSAGE_MAP(CCtrlComments, CModControlDlg)
  24. //{{AFX_MSG_MAP(CCtrlComments)
  25. ON_EN_UPDATE(IDC_EDIT_COMMENTS, &CCtrlComments::OnCommentsUpdated)
  26. ON_EN_CHANGE(IDC_EDIT_COMMENTS, &CCtrlComments::OnCommentsChanged)
  27. //}}AFX_MSG_MAP
  28. END_MESSAGE_MAP()
  29. void CCtrlComments::DoDataExchange(CDataExchange* pDX)
  30. {
  31. CModControlDlg::DoDataExchange(pDX);
  32. //{{AFX_DATA_MAP(CCtrlComments)
  33. DDX_Control(pDX, IDC_EDIT_COMMENTS, m_EditComments);
  34. //}}AFX_DATA_MAP
  35. }
  36. CCtrlComments::CCtrlComments(CModControlView &parent, CModDoc &document) : CModControlDlg(parent, document)
  37. {
  38. }
  39. CRuntimeClass *CCtrlComments::GetAssociatedViewClass()
  40. {
  41. return RUNTIME_CLASS(CViewComments);
  42. }
  43. void CCtrlComments::OnActivatePage(LPARAM)
  44. {
  45. // Don't stop generating VU meter messages
  46. m_modDoc.SetNotifications(Notification::Default);
  47. m_modDoc.SetFollowWnd(m_hWnd);
  48. m_EditComments.SetFocus();
  49. }
  50. void CCtrlComments::OnDeactivatePage()
  51. {
  52. CModControlDlg::OnDeactivatePage();
  53. }
  54. BOOL CCtrlComments::OnInitDialog()
  55. {
  56. CModControlDlg::OnInitDialog();
  57. // Initialize comments
  58. UINT margin = Util::ScalePixels(MPT_COMMENTS_MARGIN, m_EditComments.m_hWnd);
  59. m_EditComments.SetMargins(margin, margin);
  60. UpdateView(CommentHint().ModType());
  61. m_EditComments.SetFocus();
  62. m_EditComments.FmtLines(FALSE);
  63. m_bInitialized = TRUE;
  64. return FALSE;
  65. }
  66. void CCtrlComments::RecalcLayout()
  67. {
  68. CRect rcClient, rect;
  69. int cx0, cy0;
  70. if ((!m_hWnd) || (!m_EditComments.m_hWnd)) return;
  71. GetClientRect(&rcClient);
  72. m_EditComments.GetWindowRect(&rect);
  73. ScreenToClient(&rect);
  74. cx0 = rect.Width();
  75. cy0 = rect.Height();
  76. rect.bottom = rcClient.bottom - 3;
  77. rect.right = rcClient.right - rect.left;
  78. if ((rect.right > rect.left) && (rect.bottom > rect.top))
  79. {
  80. int cx = rect.Width(), cy = rect.Height();
  81. if(m_sndFile.GetModSpecifications().commentLineLengthMax != 0)
  82. {
  83. int cxmax = Util::ScalePixels(GetSystemMetrics(SM_CXBORDER) + MPT_COMMENTS_MARGIN + m_sndFile.GetModSpecifications().commentLineLengthMax * charWidth + MPT_COMMENTS_MARGIN + GetSystemMetrics(SM_CXVSCROLL) + GetSystemMetrics(SM_CXBORDER) - 1, m_EditComments.m_hWnd);
  84. if (cx > cxmax && cxmax != 0) cx = cxmax;
  85. //SetWindowLong(m_EditComments.m_hWnd, GWL_STYLE, GetWindowLong(m_EditComments.m_hWnd, GWL_STYLE) & ~WS_HSCROLL);
  86. } else
  87. {
  88. //SetWindowLong(m_EditComments.m_hWnd, GWL_STYLE, GetWindowLong(m_EditComments.m_hWnd, GWL_STYLE) | WS_HSCROLL);
  89. }
  90. if ((cx != cx0) || (cy != cy0)) m_EditComments.SetWindowPos(NULL, 0,0, cx, cy, SWP_NOMOVE|SWP_NOZORDER|SWP_DRAWFRAME);
  91. }
  92. }
  93. void CCtrlComments::UpdateView(UpdateHint hint, CObject *pHint)
  94. {
  95. CommentHint commentHint = hint.ToType<CommentHint>();
  96. if (pHint == this || !commentHint.GetType()[HINT_MODCOMMENTS | HINT_MPTOPTIONS | HINT_MODTYPE]) return;
  97. if (m_nLockCount) return;
  98. m_nLockCount++;
  99. static FontSetting previousFont;
  100. FontSetting font = TrackerSettings::Instance().commentsFont;
  101. // Point size to pixels
  102. int32 fontSize = -MulDiv(font.size, m_nDPIy, 720);
  103. if(previousFont != font)
  104. {
  105. previousFont = font;
  106. CMainFrame::GetCommentsFont() = ::CreateFont(fontSize, 0, 0, 0, font.flags[FontSetting::Bold] ? FW_BOLD : FW_NORMAL,
  107. font.flags[FontSetting::Italic] ? TRUE :FALSE, FALSE, FALSE,
  108. DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
  109. CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  110. FIXED_PITCH | FF_MODERN, mpt::ToCString(font.name));
  111. }
  112. m_EditComments.SendMessage(WM_SETFONT, (WPARAM)CMainFrame::GetCommentsFont());
  113. CDC * pDC = m_EditComments.GetDC();
  114. pDC->SelectObject(CMainFrame::GetCommentsFont());
  115. TEXTMETRIC tm;
  116. pDC->GetTextMetrics(&tm);
  117. charWidth = tm.tmAveCharWidth;
  118. m_EditComments.ReleaseDC(pDC);
  119. RecalcLayout();
  120. m_EditComments.SetRedraw(FALSE);
  121. std::string text = m_sndFile.m_songMessage.GetFormatted(SongMessage::leCRLF);
  122. for(std::size_t i = 0; i < text.length(); ++i)
  123. {
  124. // replace control characters
  125. char c = text[i];
  126. if(c > '\0' && c < ' ' && c != '\r' && c != '\n')
  127. {
  128. c = ' ';
  129. }
  130. text[i] = c;
  131. }
  132. CString new_text = mpt::ToCString(m_sndFile.GetCharsetInternal(), text);
  133. CString old_text;
  134. m_EditComments.GetWindowText(old_text);
  135. if(new_text != old_text)
  136. {
  137. m_EditComments.SetWindowText(new_text);
  138. }
  139. if(commentHint.GetType() & HINT_MODTYPE)
  140. {
  141. m_EditComments.SetReadOnly(!m_sndFile.GetModSpecifications().hasComments);
  142. }
  143. m_EditComments.SetRedraw(TRUE);
  144. m_nLockCount--;
  145. }
  146. void CCtrlComments::OnCommentsUpdated()
  147. {
  148. #if defined(MPT_COMMENTS_LONG_LINES_TRUNCATE) || defined(MPT_COMMENTS_LONG_LINES_WRAP)
  149. if(m_Reformatting)
  150. {
  151. return;
  152. }
  153. if(!m_sndFile.GetModSpecifications().hasComments)
  154. {
  155. return;
  156. }
  157. if(m_sndFile.GetModSpecifications().commentLineLengthMax == 0)
  158. {
  159. return;
  160. }
  161. m_Reformatting = true;
  162. const std::size_t maxline = m_sndFile.GetModSpecifications().commentLineLengthMax;
  163. int beg = 0;
  164. int end = 0;
  165. m_EditComments.GetSel(beg, end);
  166. CString text;
  167. m_EditComments.GetWindowText(text);
  168. std::string lines_new;
  169. lines_new.reserve(text.GetLength());
  170. bool modified = false;
  171. std::size_t pos = 0;
  172. #if defined(MPT_COMMENTS_LONG_LINES_WRAP)
  173. std::string lines = text.GetString();
  174. std::size_t line_length = 0;
  175. for(std::size_t i = 0; i < lines.length(); ++i)
  176. {
  177. if(lines[i] == '\r')
  178. {
  179. // nothing
  180. } else if (lines[i] == '\n')
  181. {
  182. line_length = 0;
  183. } else
  184. {
  185. line_length += 1;
  186. }
  187. if(line_length > maxline)
  188. {
  189. modified = true;
  190. lines_new.push_back('\r');
  191. lines_new.push_back('\n');
  192. if(beg >= 0)
  193. {
  194. if(beg >= pos)
  195. {
  196. beg += 2;
  197. }
  198. }
  199. if(end >= 0)
  200. {
  201. if(end >= pos)
  202. {
  203. end += 2;
  204. }
  205. }
  206. pos += 2;
  207. line_length = 1;
  208. }
  209. lines_new.push_back(lines[i]);
  210. pos++;
  211. }
  212. #elif defined(MPT_COMMENTS_LONG_LINES_TRUNCATE)
  213. std::vector<std::string> lines = mpt::String::Split<std::string>(std::string(text.GetString()), std::string("\r\n"));
  214. for(std::size_t i = 0; i < lines.size(); ++i)
  215. {
  216. if(i > 0)
  217. {
  218. pos += 2;
  219. }
  220. if(lines[i].length() > maxline)
  221. {
  222. modified = true;
  223. pos += maxline;
  224. for(std::size_t n = 0; n < lines[i].length() - maxline; ++n)
  225. {
  226. if(beg >= 0)
  227. {
  228. if(beg > pos)
  229. {
  230. beg--;
  231. }
  232. }
  233. if(end >= 0)
  234. {
  235. if(end > pos)
  236. {
  237. end--;
  238. }
  239. }
  240. }
  241. lines[i] = lines[i].substr(0, maxline);
  242. } else
  243. {
  244. pos += lines[i].length();
  245. }
  246. }
  247. lines_new = mpt::String::Combine(lines, std::string("\r\n"));
  248. #endif
  249. if(modified)
  250. {
  251. text = lines_new.c_str();
  252. m_EditComments.SetWindowText(text);
  253. m_EditComments.SetSel(beg, end);
  254. }
  255. m_Reformatting = false;
  256. #endif
  257. }
  258. void CCtrlComments::OnCommentsChanged()
  259. {
  260. if(m_nLockCount)
  261. return;
  262. if ((!m_bInitialized) || (!m_EditComments.m_hWnd) || (!m_EditComments.GetModify())) return;
  263. CString text;
  264. m_EditComments.GetWindowText(text);
  265. m_EditComments.SetModify(FALSE);
  266. if(m_sndFile.m_songMessage.SetFormatted(mpt::ToCharset(m_sndFile.GetCharsetInternal(), text), SongMessage::leCRLF))
  267. {
  268. m_modDoc.SetModified();
  269. m_modDoc.UpdateAllViews(nullptr, CommentHint(), this);
  270. }
  271. }
  272. BOOL CCtrlComments::PreTranslateMessage(MSG *pMsg)
  273. {
  274. if(pMsg->message == WM_KEYDOWN && pMsg->wParam == 'A' && GetKeyState(VK_CONTROL) < 0)
  275. {
  276. // Ctrl-A is not handled by multiline edit boxes
  277. if(::GetFocus() == m_EditComments.m_hWnd)
  278. m_EditComments.SetSel(0, -1);
  279. } else if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_TAB && CMainFrame::GetMainFrame()->GetInputHandler()->GetModifierMask() == ModNone)
  280. {
  281. int selStart, selEnd;
  282. m_EditComments.GetSel(selStart, selEnd);
  283. int posInLine = (selStart - m_EditComments.LineIndex(m_EditComments.LineFromChar(selStart)));
  284. CString tabs(_T(' '), 4 - (posInLine % 4));
  285. m_EditComments.ReplaceSel(tabs, TRUE);
  286. m_EditComments.SetSel(-1, -1, TRUE);
  287. return TRUE;
  288. }
  289. return CModControlDlg::PreTranslateMessage(pMsg);
  290. }
  291. OPENMPT_NAMESPACE_END