aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/mptrack/CloseMainDialog.cpp
blob: 4fadaddb2ac1aaae3ac6cd2d8aaf569d55370a43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * CloseMainDialog.cpp
 * -------------------
 * Purpose: Dialog showing a list of unsaved documents, with the ability to choose which documents should be saved or not.
 * Notes  : (currently none)
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */


#include "stdafx.h"
#include "Mptrack.h"
#include "Mainfrm.h"
#include "Moddoc.h"
#include "CloseMainDialog.h"


OPENMPT_NAMESPACE_BEGIN


BEGIN_MESSAGE_MAP(CloseMainDialog, ResizableDialog)
	ON_COMMAND(IDC_BUTTON1,			&CloseMainDialog::OnSaveAll)
	ON_COMMAND(IDC_BUTTON2,			&CloseMainDialog::OnSaveNone)
	ON_COMMAND(IDC_CHECK1,			&CloseMainDialog::OnSwitchFullPaths)
END_MESSAGE_MAP()


void CloseMainDialog::DoDataExchange(CDataExchange* pDX)
{
	ResizableDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DoDataExchange)
	DDX_Control(pDX, IDC_LIST1,		m_List);
	//}}AFX_DATA_MAP
}


CloseMainDialog::CloseMainDialog() : ResizableDialog(IDD_CLOSEDOCUMENTS)
{
};


CString CloseMainDialog::FormatTitle(const CModDoc *modDoc, bool fullPath)
{
	return MPT_CFORMAT("{} ({})")
		(mpt::ToCString(modDoc->GetSoundFile().GetCharsetInternal(), modDoc->GetSoundFile().GetTitle()),
		(!fullPath || modDoc->GetPathNameMpt().empty()) ? modDoc->GetTitle() : modDoc->GetPathNameMpt().ToCString());
}


BOOL CloseMainDialog::OnInitDialog()
{
	ResizableDialog::OnInitDialog();

	// Create list of unsaved documents
	m_List.ResetContent();

	CheckDlgButton(IDC_CHECK1, BST_CHECKED);

	m_List.SetRedraw(FALSE);
	for(const auto &modDoc : theApp.GetOpenDocuments())
	{
		if(modDoc->IsModified())
		{
			int item = m_List.AddString(FormatTitle(modDoc, true));
			m_List.SetItemDataPtr(item, modDoc);
			m_List.SetSel(item, TRUE);
		}
	}
	m_List.SetRedraw(TRUE);

	if(m_List.GetCount() == 0)
	{
		// No modified documents...
		OnOK();
	}

	return TRUE;
}


void CloseMainDialog::OnOK()
{
	const int count = m_List.GetCount();
	for(int i = 0; i < count; i++)
	{
		CModDoc *modDoc = static_cast<CModDoc *>(m_List.GetItemDataPtr(i));
		MPT_ASSERT(modDoc != nullptr);
		if(m_List.GetSel(i))
		{
			modDoc->ActivateWindow();
			if(modDoc->DoFileSave() == FALSE)
			{
				// If something went wrong, or if the user decided to cancel saving (when using "Save As"), we'll better not proceed...
				OnCancel();
				return;
			}
		} else
		{
			modDoc->SetModified(FALSE);
		}
	}

	ResizableDialog::OnOK();
}


void CloseMainDialog::OnSaveAll()
{
	if(m_List.GetCount() == 1)
		m_List.SetSel(0, TRUE);	// SelItemRange can't select one item: https://jeffpar.github.io/kbarchive/kb/129/Q129428/
	else
		m_List.SelItemRange(TRUE, 0, m_List.GetCount() - 1);
	OnOK();
}


void CloseMainDialog::OnSaveNone()
{
	if(m_List.GetCount() == 1)
		m_List.SetSel(0, FALSE);	// SelItemRange can't select one item: https://jeffpar.github.io/kbarchive/kb/129/Q129428/
	else
		m_List.SelItemRange(FALSE, 0, m_List.GetCount() - 1);
	OnOK();
}


// Switch between full path / filename only display
void CloseMainDialog::OnSwitchFullPaths()
{
	const int count = m_List.GetCount();
	const bool fullPath = (IsDlgButtonChecked(IDC_CHECK1) == BST_CHECKED);
	m_List.SetRedraw(FALSE);
	for(int i = 0; i < count; i++)
	{
		CModDoc *modDoc = static_cast<CModDoc *>(m_List.GetItemDataPtr(i));
		int item = m_List.InsertString(i + 1, FormatTitle(modDoc, fullPath));
		m_List.SetItemDataPtr(item, modDoc);
		m_List.SetSel(item, m_List.GetSel(i));
		m_List.DeleteString(i);
	}
	m_List.SetRedraw(TRUE);
}

OPENMPT_NAMESPACE_END