1
0

CloseMainDialog.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * CloseMainDialog.cpp
  3. * -------------------
  4. * Purpose: Dialog showing a list of unsaved documents, with the ability to choose which documents should be saved or not.
  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. #include "stdafx.h"
  10. #include "Mptrack.h"
  11. #include "Mainfrm.h"
  12. #include "Moddoc.h"
  13. #include "CloseMainDialog.h"
  14. OPENMPT_NAMESPACE_BEGIN
  15. BEGIN_MESSAGE_MAP(CloseMainDialog, ResizableDialog)
  16. ON_COMMAND(IDC_BUTTON1, &CloseMainDialog::OnSaveAll)
  17. ON_COMMAND(IDC_BUTTON2, &CloseMainDialog::OnSaveNone)
  18. ON_COMMAND(IDC_CHECK1, &CloseMainDialog::OnSwitchFullPaths)
  19. END_MESSAGE_MAP()
  20. void CloseMainDialog::DoDataExchange(CDataExchange* pDX)
  21. {
  22. ResizableDialog::DoDataExchange(pDX);
  23. //{{AFX_DATA_MAP(DoDataExchange)
  24. DDX_Control(pDX, IDC_LIST1, m_List);
  25. //}}AFX_DATA_MAP
  26. }
  27. CloseMainDialog::CloseMainDialog() : ResizableDialog(IDD_CLOSEDOCUMENTS)
  28. {
  29. };
  30. CString CloseMainDialog::FormatTitle(const CModDoc *modDoc, bool fullPath)
  31. {
  32. return MPT_CFORMAT("{} ({})")
  33. (mpt::ToCString(modDoc->GetSoundFile().GetCharsetInternal(), modDoc->GetSoundFile().GetTitle()),
  34. (!fullPath || modDoc->GetPathNameMpt().empty()) ? modDoc->GetTitle() : modDoc->GetPathNameMpt().ToCString());
  35. }
  36. BOOL CloseMainDialog::OnInitDialog()
  37. {
  38. ResizableDialog::OnInitDialog();
  39. // Create list of unsaved documents
  40. m_List.ResetContent();
  41. CheckDlgButton(IDC_CHECK1, BST_CHECKED);
  42. m_List.SetRedraw(FALSE);
  43. for(const auto &modDoc : theApp.GetOpenDocuments())
  44. {
  45. if(modDoc->IsModified())
  46. {
  47. int item = m_List.AddString(FormatTitle(modDoc, true));
  48. m_List.SetItemDataPtr(item, modDoc);
  49. m_List.SetSel(item, TRUE);
  50. }
  51. }
  52. m_List.SetRedraw(TRUE);
  53. if(m_List.GetCount() == 0)
  54. {
  55. // No modified documents...
  56. OnOK();
  57. }
  58. return TRUE;
  59. }
  60. void CloseMainDialog::OnOK()
  61. {
  62. const int count = m_List.GetCount();
  63. for(int i = 0; i < count; i++)
  64. {
  65. CModDoc *modDoc = static_cast<CModDoc *>(m_List.GetItemDataPtr(i));
  66. MPT_ASSERT(modDoc != nullptr);
  67. if(m_List.GetSel(i))
  68. {
  69. modDoc->ActivateWindow();
  70. if(modDoc->DoFileSave() == FALSE)
  71. {
  72. // If something went wrong, or if the user decided to cancel saving (when using "Save As"), we'll better not proceed...
  73. OnCancel();
  74. return;
  75. }
  76. } else
  77. {
  78. modDoc->SetModified(FALSE);
  79. }
  80. }
  81. ResizableDialog::OnOK();
  82. }
  83. void CloseMainDialog::OnSaveAll()
  84. {
  85. if(m_List.GetCount() == 1)
  86. m_List.SetSel(0, TRUE); // SelItemRange can't select one item: https://jeffpar.github.io/kbarchive/kb/129/Q129428/
  87. else
  88. m_List.SelItemRange(TRUE, 0, m_List.GetCount() - 1);
  89. OnOK();
  90. }
  91. void CloseMainDialog::OnSaveNone()
  92. {
  93. if(m_List.GetCount() == 1)
  94. m_List.SetSel(0, FALSE); // SelItemRange can't select one item: https://jeffpar.github.io/kbarchive/kb/129/Q129428/
  95. else
  96. m_List.SelItemRange(FALSE, 0, m_List.GetCount() - 1);
  97. OnOK();
  98. }
  99. // Switch between full path / filename only display
  100. void CloseMainDialog::OnSwitchFullPaths()
  101. {
  102. const int count = m_List.GetCount();
  103. const bool fullPath = (IsDlgButtonChecked(IDC_CHECK1) == BST_CHECKED);
  104. m_List.SetRedraw(FALSE);
  105. for(int i = 0; i < count; i++)
  106. {
  107. CModDoc *modDoc = static_cast<CModDoc *>(m_List.GetItemDataPtr(i));
  108. int item = m_List.InsertString(i + 1, FormatTitle(modDoc, fullPath));
  109. m_List.SetItemDataPtr(item, modDoc);
  110. m_List.SetSel(item, m_List.GetSel(i));
  111. m_List.DeleteString(i);
  112. }
  113. m_List.SetRedraw(TRUE);
  114. }
  115. OPENMPT_NAMESPACE_END