OSDN Git Service

Merge
[winmerge-jp/winmerge-jp.git] / Src / PluginsListDlg.cpp
1 /**
2  * @file  PluginsListDlg.cpp
3  *
4  * @brief Implementation file for PluginsList dialog
5  */
6
7 #include "stdafx.h"
8 #include "PluginsListDlg.h"
9 #include "UnicodeString.h"
10 #include "Plugins.h"
11 #include "OptionsDef.h"
12 #include "OptionsMgr.h"
13
14 IMPLEMENT_DYNAMIC(PluginsListDlg, CTrDialog)
15
16 BEGIN_MESSAGE_MAP(PluginsListDlg, CTrDialog)
17         ON_BN_CLICKED(IDOK, OnBnClickedOk)
18         ON_BN_CLICKED(IDC_PLUGIN_SETTINGS, OnBnClickedPluginSettings)
19         ON_NOTIFY(NM_DBLCLK, IDC_PLUGINSLIST_LIST, OnNMDblclkList)
20 END_MESSAGE_MAP()
21
22 /**
23  * @brief Constructor.
24  */
25 PluginsListDlg::PluginsListDlg(CWnd* pParent /*=NULL*/)
26         : CTrDialog(PluginsListDlg::IDD, pParent)
27 {
28 }
29
30 /**
31  * @brief Destructor.
32  */
33 PluginsListDlg::~PluginsListDlg()
34 {
35 }
36
37 void PluginsListDlg::DoDataExchange(CDataExchange* pDX)
38 {
39         CTrDialog::DoDataExchange(pDX);
40         DDX_Control(pDX, IDC_PLUGINSLIST_LIST, m_list);
41 }
42
43 /**
44  * @brief Initialize the dialog.
45  */
46 BOOL PluginsListDlg::OnInitDialog()
47 {
48         CTrDialog::OnInitDialog();
49         
50         InitList();
51         AddPlugins();
52         m_list.SetItemState(0, LVIS_SELECTED, LVIS_SELECTED);
53
54         CheckDlgButton(IDC_PLUGINS_ENABLE, 
55                 GetOptionsMgr()->GetBool(OPT_PLUGINS_ENABLED) ? BST_CHECKED : BST_UNCHECKED);
56
57         return FALSE;  // return TRUE unless you set the focus to a control
58                       // EXCEPTION: OCX Property Pages should return FALSE
59 }
60
61 /**
62  * @brief Set-up the list control.
63  */
64 void PluginsListDlg::InitList()
65 {
66         // Show selection across entire row.
67         // Also enable infotips.
68         m_list.SetExtendedStyle(LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);
69
70         const int lpx = CClientDC(this).GetDeviceCaps(LOGPIXELSX);
71         auto pointToPixel = [lpx](int point) { return MulDiv(point, lpx, 72); };
72
73         String title = _("Name");
74         m_list.InsertColumn(0, title.c_str(), LVCFMT_LEFT, pointToPixel(150));
75         title = _("Type");
76         m_list.InsertColumn(1, title.c_str(), LVCFMT_LEFT, pointToPixel(75));
77         title = _("Description");
78         m_list.InsertColumn(2, title.c_str(), LVCFMT_LEFT, pointToPixel(225));
79 }
80
81 /**
82  * @brief Add found plugins to the list.
83  */
84 void PluginsListDlg::AddPlugins()
85 {
86         String type = _("Unpacker");
87         AddPluginsToList(L"FILE_PACK_UNPACK", type);
88         AddPluginsToList(L"BUFFER_PACK_UNPACK", type);
89         AddPluginsToList(L"FILE_FOLDER_PACK_UNPACK", type);
90         type = _("Prediffer");
91         AddPluginsToList(L"FILE_PREDIFF", type);
92         AddPluginsToList(L"BUFFER_PREDIFF", type);
93         type = _("Editor script");
94         AddPluginsToList(L"EDITOR_SCRIPT", type);
95 }
96
97 /**
98  * @brief Add plugins of given event type to the list.
99  * @param [in] pluginEvent Event type for plugins to add.
100  * @param [in] pluginType String to use as type in the list.
101  */
102 void PluginsListDlg::AddPluginsToList(const wchar_t *pluginEvent, const String& pluginType)
103 {
104         PluginArray * piPluginArray = 
105                 CAllThreadsScripts::GetActiveSet()->GetAvailableScripts(pluginEvent);
106
107         for (int iPlugin = 0 ; iPlugin < piPluginArray->size() ; iPlugin++)
108         {
109                 const PluginInfoPtr& plugin = piPluginArray->at(iPlugin);
110                 int ind = m_list.InsertItem(m_list.GetItemCount(), plugin->m_name.c_str());
111                 m_list.SetItemText(ind, 1, pluginType.c_str());
112                 m_list.SetItemText(ind, 2, plugin->m_description.c_str());
113                 m_list.SetCheck(ind, !plugin->m_disabled);
114         }
115 }
116
117 /**
118  * @brief Save plugins enabled setting when closing the dialog.
119  */
120 void PluginsListDlg::OnBnClickedOk()
121 {
122         GetOptionsMgr()->SaveOption(OPT_PLUGINS_ENABLED, 
123                 (IsDlgButtonChecked(IDC_PLUGINS_ENABLE) == 1));
124
125         for (int i = 0; i < m_list.GetItemCount(); ++i)
126         {
127                 PluginInfo * plugin = CAllThreadsScripts::GetActiveSet()->GetPluginByName(NULL, String(m_list.GetItemText(i, 0)));
128                 plugin->m_disabled = !m_list.GetCheck(i);
129         }
130         CAllThreadsScripts::GetActiveSet()->SaveSettings();
131         OnOK();
132 }
133
134 void PluginsListDlg::OnBnClickedPluginSettings()
135 {
136         POSITION pos = m_list.GetFirstSelectedItemPosition();
137         if (pos)
138         {
139                 String pluginName = static_cast<const TCHAR *>(m_list.GetItemText(m_list.GetNextSelectedItem(pos), 0));
140                 for (int i = 0; TransformationCategories[i]; ++i)
141                 {
142                         PluginInfo * plugin = CAllThreadsScripts::GetActiveSet()->GetPluginByName(TransformationCategories[i], pluginName);
143                         if (plugin)
144                         {
145                                 EnableWindow(false);
146                                 InvokeShowSettingsDialog(plugin->m_lpDispatch);
147                                 EnableWindow(true);
148                                 SetForegroundWindow();
149                                 break;
150                         }
151                 }
152         }
153 }
154
155 void PluginsListDlg::OnNMDblclkList(NMHDR *pNMHDR, LRESULT *pResult)
156 {
157         OnBnClickedPluginSettings();
158 }