Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PlayerNavigationDialog.cpp « mpc-hc « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 86e9c35d324520dd8d0b4675498461841a8d6694 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * (C) 2010-2015 see Authors.txt
 *
 * This file is part of MPC-HC.
 *
 * MPC-HC is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * MPC-HC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "stdafx.h"
#include "PlayerNavigationDialog.h"
#include "DSUtil.h"
#include "mplayerc.h"
#include "MainFrm.h"

// CPlayerNavigationDialog dialog

// IMPLEMENT_DYNAMIC(CPlayerNavigationDialog, CResizableDialog)
CPlayerNavigationDialog::CPlayerNavigationDialog(CMainFrame* pMainFrame)
    : CResizableDialog(CPlayerNavigationDialog::IDD, nullptr)
    , m_pMainFrame(pMainFrame)
    , m_bChannelInfoAvailable(false)
    , m_bTVStations(true)
{
}

BOOL CPlayerNavigationDialog::Create(CWnd* pParent)
{
    if (!__super::Create(IDD, pParent)) {
        return FALSE;
    }

    AddAnchor(IDC_LISTCHANNELS, TOP_LEFT, BOTTOM_RIGHT);
    AddAnchor(IDC_NAVIGATION_INFO, BOTTOM_LEFT);
    AddAnchor(IDC_NAVIGATION_SCAN, BOTTOM_RIGHT);
    AddAnchor(IDC_NAVIGATION_FILTERSTATIONS, BOTTOM_LEFT, BOTTOM_RIGHT);

    return TRUE;
}

void CPlayerNavigationDialog::DoDataExchange(CDataExchange* pDX)
{
    __super::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LISTCHANNELS, m_channelList);
    DDX_Control(pDX, IDC_NAVIGATION_INFO, m_buttonInfo);
    DDX_Control(pDX, IDC_NAVIGATION_FILTERSTATIONS, m_buttonFilterStations);
}

BOOL CPlayerNavigationDialog::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN) {
        if (pMsg->wParam == VK_RETURN) {
            CWnd* pFocused = GetFocus();
            if (pFocused && pFocused->m_hWnd == m_channelList.m_hWnd) {
                return TRUE;
            }
        }
    }
    return __super::PreTranslateMessage(pMsg);
}

BEGIN_MESSAGE_MAP(CPlayerNavigationDialog, CResizableDialog)
    ON_WM_DESTROY()
    ON_WM_CONTEXTMENU()
    ON_LBN_SELCHANGE(IDC_LISTCHANNELS, OnChangeChannel)
    ON_BN_CLICKED(IDC_NAVIGATION_INFO, OnShowChannelInfo)
    ON_UPDATE_COMMAND_UI(IDC_NAVIGATION_INFO, OnUpdateShowChannelInfoButton)
    ON_BN_CLICKED(IDC_NAVIGATION_SCAN, OnTunerScan)
    ON_BN_CLICKED(IDC_NAVIGATION_FILTERSTATIONS, OnTvRadioStations)
END_MESSAGE_MAP()


// CPlayerNavigationDialog message handlers

BOOL CPlayerNavigationDialog::OnInitDialog()
{
    __super::OnInitDialog();

    if (m_bTVStations) {
        m_buttonFilterStations.SetWindowText(ResStr(IDS_DVB_TVNAV_SEERADIO));
    } else {
        m_buttonFilterStations.SetWindowText(ResStr(IDS_DVB_TVNAV_SEETV));
    }

    ATOM atom = ::GlobalAddAtom(MICROSOFT_TABLETPENSERVICE_PROPERTY);
    ::SetProp(m_channelList.GetSafeHwnd(), MICROSOFT_TABLETPENSERVICE_PROPERTY, nullptr);
    ::GlobalDeleteAtom(atom);

    return FALSE;  // return FALSE so that the dialog does not steal focus
    // EXCEPTION: OCX Property Pages should return FALSE
}

void CPlayerNavigationDialog::OnDestroy()
{
    m_channelList.ResetContent();
    __super::OnDestroy();
}

void CPlayerNavigationDialog::OnChangeChannel()
{
    int nItem = m_channelList.GetCurSel();
    if (nItem != LB_ERR) {
        UINT nChannelID = (UINT)m_channelList.GetItemData(nItem) + ID_NAVIGATE_JUMPTO_SUBITEM_START;
        m_pMainFrame->OnNavigateJumpTo(nChannelID);
    }
}

void CPlayerNavigationDialog::UpdateElementList()
{
    if (m_pMainFrame->GetPlaybackMode() == PM_DIGITAL_CAPTURE) {
        const auto& s = AfxGetAppSettings();
        m_channelList.ResetContent();

        for (const auto& channel : s.m_DVBChannels) {
            if (channel.GetAudioCount() && (m_bTVStations && channel.GetVideoPID() || !m_bTVStations && !channel.GetVideoPID())) {
                int nItem = m_channelList.AddString(channel.GetName());
                if (nItem != LB_ERR) {
                    m_channelList.SetItemData(nItem, (DWORD_PTR)channel.GetPrefNumber());
                    if (s.nDVBLastChannel == channel.GetPrefNumber()) {
                        m_channelList.SetCurSel(nItem);
                    }
                }
            }
        }
    }
}

void CPlayerNavigationDialog::UpdatePos(int nID)
{
    for (int i = 0, count = m_channelList.GetCount(); i < count; i++) {
        if ((int)m_channelList.GetItemData(i) == nID) {
            m_channelList.SetCurSel(i);
            break;
        }
    }
}

void CPlayerNavigationDialog::SetChannelInfoAvailable(bool bAvailable)
{
    m_bChannelInfoAvailable = bAvailable;
}

void CPlayerNavigationDialog::OnTunerScan()
{
    m_pMainFrame->OnTunerScan();
    UpdateElementList();
}

void CPlayerNavigationDialog::OnShowChannelInfo()
{
    m_pMainFrame->UpdateCurrentChannelInfo(true, true);
}

void CPlayerNavigationDialog::OnUpdateShowChannelInfoButton(CCmdUI* pCmdUI)
{
    pCmdUI->Enable(m_bChannelInfoAvailable);
}

void CPlayerNavigationDialog::OnTvRadioStations()
{
    m_bTVStations = !m_bTVStations;
    UpdateElementList();

    if (m_bTVStations) {
        m_buttonFilterStations.SetWindowText(ResStr(IDS_DVB_TVNAV_SEERADIO));
    } else {
        m_buttonFilterStations.SetWindowText(ResStr(IDS_DVB_TVNAV_SEETV));
    }
}

void CPlayerNavigationDialog::OnContextMenu(CWnd* pWnd, CPoint point)
{
    auto& s = AfxGetAppSettings();
    BOOL bOutside;
    int nItem;
    const int curSel = m_channelList.GetCurSel();
    const int channelCount = m_channelList.GetCount();

    if (point.x == -1 && point.y == -1) {
        CRect r;
        if (m_channelList.GetItemRect(curSel, r) != LB_ERR) {
            point.SetPoint(r.left, r.bottom);
        } else {
            point.SetPoint(0, 0);
        }
        m_channelList.ClientToScreen(&point);
        nItem = curSel;
        bOutside = nItem == LB_ERR;
    } else {
        CPoint clientPoint = point;
        m_channelList.ScreenToClient(&clientPoint);
        nItem = (int)m_channelList.ItemFromPoint(clientPoint, bOutside);
    }

    CMenu m;
    m.CreatePopupMenu();

    enum {
        M_WATCH = 1,
        M_MOVE_UP,
        M_MOVE_DOWN,
        M_SORT,
        M_REMOVE,
        M_REMOVE_ALL
    };

    auto findChannelByItemNumber = [this](std::vector<CDVBChannel>& c, int nItem) {
        int nPrefNumber = (int)m_channelList.GetItemData(nItem);
        return find_if(c.begin(), c.end(), [&](CDVBChannel const & channel) {
            return channel.GetPrefNumber() == nPrefNumber;
        });
    };

    auto swapChannels = [&](int n1, int n2) {
        auto it1 = findChannelByItemNumber(s.m_DVBChannels, n1);
        auto it2 = findChannelByItemNumber(s.m_DVBChannels, n2);
        int nPrefNumber1 = it1->GetPrefNumber(), nPrefNumber2 = it2->GetPrefNumber();
        // Make sure the current channel number is updated if swapping the channel is going to change it
        if (nPrefNumber1 == s.nDVBLastChannel) {
            s.nDVBLastChannel = nPrefNumber2;
        } else if (nPrefNumber2 == s.nDVBLastChannel) {
            s.nDVBLastChannel = nPrefNumber1;
        }
        // The preferred number shouldn't be swapped so we swap it twice for no-op
        it1->SetPrefNumber(nPrefNumber2);
        it2->SetPrefNumber(nPrefNumber1);
        // Actually swap the channels
        iter_swap(it1, it2);
    };

    if (!bOutside) {
        m_channelList.SetCurSel(nItem);

        m.AppendMenu(MF_STRING | (curSel != nItem ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)), M_WATCH, ResStr(IDS_NAVIGATION_WATCH));
        m.AppendMenu(MF_SEPARATOR);
        m.AppendMenu(MF_STRING | (nItem == 0 ? (MF_DISABLED | MF_GRAYED) : MF_ENABLED), M_MOVE_UP, ResStr(IDS_NAVIGATION_MOVE_UP));
        m.AppendMenu(MF_STRING | (nItem == channelCount - 1 ? (MF_DISABLED | MF_GRAYED) : MF_ENABLED), M_MOVE_DOWN, ResStr(IDS_NAVIGATION_MOVE_DOWN));
    }
    m.AppendMenu(MF_STRING | (channelCount > 1 ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)), M_SORT, ResStr(IDS_NAVIGATION_SORT));
    m.AppendMenu(MF_SEPARATOR);
    if (!bOutside) {
        m.AppendMenu(MF_STRING | MF_ENABLED, M_REMOVE, ResStr(IDS_PLAYLIST_REMOVE));
    }
    m.AppendMenu(MF_STRING | (channelCount > 0 ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)), M_REMOVE_ALL, ResStr(IDS_NAVIGATION_REMOVE_ALL));

    int nID = (int)m.TrackPopupMenu(TPM_LEFTBUTTON | TPM_RETURNCMD, point.x, point.y, this);

    try {
        switch (nID) {
            case M_WATCH:
                OnChangeChannel();
                break;
            case M_MOVE_UP:
                swapChannels(nItem, nItem - 1);
                UpdateElementList();
                break;
            case M_MOVE_DOWN:
                swapChannels(nItem, nItem + 1);
                UpdateElementList();
                break;
            case M_SORT: {
                sort(s.m_DVBChannels.begin(), s.m_DVBChannels.end());
                // Update the preferred numbers
                int nPrefNumber = 0;
                int nDVBLastChannel = s.nDVBLastChannel;
                for (auto& channel : s.m_DVBChannels) {
                    // Make sure the current channel number will be updated
                    if (channel.GetPrefNumber() == s.nDVBLastChannel) {
                        nDVBLastChannel = nPrefNumber;
                    }
                    channel.SetPrefNumber(nPrefNumber++);
                }
                s.nDVBLastChannel = nDVBLastChannel;
                UpdateElementList();
                break;
            }
            case M_REMOVE: {
                const auto it = findChannelByItemNumber(s.m_DVBChannels, nItem);
                const int nRemovedPrefNumber = it->GetPrefNumber();
                s.m_DVBChannels.erase(it);
                // Update channels pref number
                for (CDVBChannel& channel : s.m_DVBChannels) {
                    const int nPrefNumber = channel.GetPrefNumber();
                    ASSERT(nPrefNumber != nRemovedPrefNumber);
                    if (nPrefNumber > nRemovedPrefNumber) {
                        channel.SetPrefNumber(nPrefNumber - 1);
                    }
                }
                UpdateElementList();

                int newCurSel = curSel;
                if ((newCurSel >= nItem) && (channelCount - 1 == nItem || newCurSel > nItem)) {
                    --newCurSel;
                }

                const auto newChannelIt = findChannelByItemNumber(s.m_DVBChannels, newCurSel);
                if (newChannelIt != s.m_DVBChannels.end()) {
                    // Update pref number of the current channel
                    s.nDVBLastChannel = newChannelIt->GetPrefNumber();
                    m_channelList.SetCurSel(newCurSel);
                    if (curSel == nItem) {
                        // Set closest channel on list after removing current channel
                        m_pMainFrame->SetChannel(s.nDVBLastChannel);
                    }
                } else { // The last channel was removed
                    s.nDVBLastChannel = INT_ERROR;
                }
            }
            break;
            case M_REMOVE_ALL:
                if (IDYES == AfxMessageBox(IDS_REMOVE_CHANNELS_QUESTION, MB_ICONQUESTION | MB_YESNO, 0)) {
                    s.m_DVBChannels.clear();
                    s.nDVBLastChannel = INT_ERROR;
                    UpdateElementList();
                }
                break;
            default:
                m_channelList.SetCurSel(curSel);
                break;
        }
    } catch (std::exception& e) {
        UNREFERENCED_PARAMETER(e);
        TRACE(_T("Navigation Dialog requested operation failed: \"%s\""), e.what());
        UpdateElementList();
        ASSERT(FALSE);
    }
}