summaryrefslogtreecommitdiff
path: root/Source/Core/Updater/UI.cpp
blob: 7f4e5cb1b25c947f8180436fc070c7523dffbda0 (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
// Copyright 2018 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "Updater/UI.h"

#include <CommCtrl.h>

#include <string>

#include "Common/Flag.h"
#include "Common/StringUtil.h"

namespace
{
HWND window_handle = nullptr;
HWND label_handle = nullptr;
HWND progressbar_handle = nullptr;

Common::Flag running;
Common::Flag request_stop;
};  // namespace

constexpr int PROGRESSBAR_FLAGS = WS_VISIBLE | WS_CHILD | PBS_SMOOTH | PBS_SMOOTHREVERSE;

namespace UI
{
bool Init()
{
  InitCommonControls();

  WNDCLASS wndcl = {};
  wndcl.lpfnWndProc = DefWindowProcW;
  wndcl.hbrBackground = GetSysColorBrush(COLOR_MENU);
  wndcl.lpszClassName = L"UPDATER";

  if (!RegisterClass(&wndcl))
    return false;

  window_handle =
      CreateWindow(L"UPDATER", L"Dolphin Updater", WS_VISIBLE | WS_CLIPCHILDREN, CW_USEDEFAULT,
                   CW_USEDEFAULT, 500, 100, nullptr, nullptr, GetModuleHandle(nullptr), 0);

  if (!window_handle)
    return false;

  label_handle = CreateWindow(L"STATIC", NULL, WS_VISIBLE | WS_CHILD, 5, 5, 500, 25, window_handle,
                              nullptr, nullptr, 0);

  if (!label_handle)
    return false;

  // Get the default system font
  NONCLIENTMETRICS metrics = {};
  metrics.cbSize = sizeof(NONCLIENTMETRICS);

  if (!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(metrics), &metrics, 0))
    return false;

  SendMessage(label_handle, WM_SETFONT,
              reinterpret_cast<WPARAM>(CreateFontIndirect(&metrics.lfMessageFont)), 0);

  progressbar_handle = CreateWindow(PROGRESS_CLASS, NULL, PROGRESSBAR_FLAGS, 5, 25, 470, 25,
                                    window_handle, nullptr, nullptr, 0);

  if (!progressbar_handle)
    return false;

  return true;
}

void Destroy()
{
  DestroyWindow(window_handle);
  DestroyWindow(label_handle);
  DestroyWindow(progressbar_handle);
}

void SetMarquee(bool marquee)
{
  SetWindowLong(progressbar_handle, GWL_STYLE, PROGRESSBAR_FLAGS | (marquee ? PBS_MARQUEE : 0));
  SendMessage(progressbar_handle, PBM_SETMARQUEE, marquee, 0);
}

void ResetProgress()
{
  SendMessage(progressbar_handle, PBM_SETPOS, 0, 0);
  SetMarquee(true);
}

void SetProgress(int current, int total)
{
  SendMessage(progressbar_handle, PBM_SETRANGE32, 0, total);
  SendMessage(progressbar_handle, PBM_SETPOS, current, 0);
}

void IncrementProgress(int amount)
{
  SendMessage(progressbar_handle, PBM_DELTAPOS, amount, 0);
}

void SetDescription(const std::string& text)
{
  SetWindowText(label_handle, UTF8ToUTF16(text).c_str());
}

void MessageLoop()
{
  request_stop.Clear();
  running.Set();

  if (!Init())
  {
    running.Clear();
    MessageBox(nullptr, L"Window init failed!", L"", MB_ICONERROR);
  }

  SetMarquee(true);

  while (!request_stop.IsSet())
  {
    MSG msg;
    while (PeekMessage(&msg, window_handle, 0, 0, PM_REMOVE))
    {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
  }

  running.Clear();

  Destroy();
}

void Stop()
{
  if (!running.IsSet())
    return;

  request_stop.Set();

  while (running.IsSet())
  {
  }
}
};  // namespace UI