summaryrefslogtreecommitdiff
path: root/shared/Taskbar_Seven/taskbar.cpp
blob: 2a739e6c8df6bb81d20ab47b0af3fedcacd6ba1c (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
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************

#include "taskbar.h"

#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#include <ShObjIdl.h>

#include <map>
#include <string>
#include <comdef.h>
#include "../com_error.h"
#include "../com_ptr.h"
#include "../c_dll.h"

using namespace util;
using namespace c_dll;

namespace
{
std::wstring lastErrorMessage;


ComPtr<ITaskbarList3> getInstance()
{
    static ComPtr<ITaskbarList3> taskbarlist;
    if (!taskbarlist)
    {
        HRESULT hr = ::CoCreateInstance(CLSID_TaskbarList,
                                        NULL,
                                        CLSCTX_ALL,
                                        IID_PPV_ARGS(taskbarlist.init()));
        if (FAILED(hr))
            lastErrorMessage = generateErrorMsg(L"Error calling \"CoCreateInstance\".", hr);
    }

    return taskbarlist;
}

}
//##################################################################################################


bool tbseven::setStatus(void* hwnd, //HWND: window assciated to the taskbar icon
                        TaskBarStatus status)
{
    TBPFLAG flag = TBPF_NORMAL;
    switch (status)
    {
        case STATUS_NOPROGRESS:
            flag = TBPF_NOPROGRESS;
            break;
        case STATUS_INDETERMINATE:
            flag = TBPF_INDETERMINATE;
            break;
        case STATUS_NORMAL:
            flag = TBPF_NORMAL;
            break;
        case STATUS_ERROR:
            flag = TBPF_ERROR;
            break;
        case STATUS_PAUSED:
            flag = TBPF_PAUSED;
            break;
    }

    ComPtr<ITaskbarList3> taskbarlist = getInstance();
    if (!taskbarlist) //error msg already set
        return false;

    HRESULT hr = taskbarlist->SetProgressState(static_cast<HWND>(hwnd),  //[in]  HWND hwnd,
                                               flag); //[in]  TBPFLAG tbpFlags
    if (FAILED(hr))
    {
        lastErrorMessage = generateErrorMsg(L"Error calling \"SetProgressState\".", hr);
        return false;
    }

    return true;
}


bool tbseven::setProgress(void* hwnd, //HWND: window assciated to the taskbar icon
                          size_t current,
                          size_t total)
{
    ComPtr<ITaskbarList3> taskbarlist = getInstance();
    if (!taskbarlist) //error msg already set
        return false;

    HRESULT hr = taskbarlist->SetProgressValue(
                     static_cast<HWND>(hwnd), //[in]  HWND hwnd,
                     current, //[in]  ULONGLONG ullCompleted,
                     total);  //[in]  ULONGLONG ullTotal
    if (FAILED(hr))
    {
        lastErrorMessage = generateErrorMsg(L"Error calling \"SetProgressValue\".", hr);
        return false;
    }

    return true;
}


void tbseven::getLastError(wchar_t* errorMessage, size_t errorBufferLen)
{
    writeString(lastErrorMessage, errorMessage, errorBufferLen);
}
bgstack15