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
|
#include "toneview.h"
#include <stdbool.h>
#include <windowsx.h>
enum {
TIMER_UPDATE = 1
};
enum {
ID_COPY0 = 0x10,
ID_COPY1,
ID_COPY2,
ID_COPY3,
ID_COPY4,
ID_COPY5,
ID_NORMALIZE,
ID_LIST,
};
struct toneview_g toneview_g = {
.flag = ATOMIC_FLAG_INIT
};
static struct {
HINSTANCE hinst;
HWND toneviewer;
HWND tonelabel[6];
ATOM toneviewer_class;
struct fmplayer_tonedata tonedata;
struct fmplayer_tonedata tonedata_n;
char strbuf[FMPLAYER_TONEDATA_STR_SIZE];
wchar_t strbuf_w[FMPLAYER_TONEDATA_STR_SIZE];
enum fmplayer_tonedata_format format;
bool normalize;
HFONT font;
HFONT font_mono;
HWND checkbox;
HWND formatlist;
} g = {
.normalize = true
};
extern HWND g_currentdlg;
static void on_destroy(HWND hwnd) {
for (int i = 0; i < 6; i++) {
DestroyWindow(g.tonelabel[i]);
}
DestroyWindow(g.checkbox);
DestroyWindow(g.formatlist);
g.toneviewer = 0;
}
enum {
LIST_W = 200,
NORMALIZE_X = 10 + LIST_W + 5,
NORMALIZE_W = 200,
TOP_H = 25,
TONELABEL_X = 10,
TONELABEL_H = 100,
TONELABEL_W = 300,
COPY_X = TONELABEL_X + TONELABEL_W + 5,
COPY_W = 100,
WIN_H = 10 + TOP_H + 5 + TONELABEL_H*6 + 5*5 + 10,
WIN_W = 10 + TONELABEL_W + 5 + COPY_W + 10,
};
static void on_command(HWND hwnd, int id, HWND hwnd_c, UINT code) {
if (code == BN_CLICKED && ((ID_COPY0 <= id) && (id <= ID_COPY5))) {
int i = id - ID_COPY0;
HGLOBAL gmem = GlobalAlloc(GMEM_MOVEABLE, FMPLAYER_TONEDATA_STR_SIZE*sizeof(wchar_t));
if (!gmem) return;
wchar_t *buf = GlobalLock(gmem);
GetWindowText(g.tonelabel[i], buf, FMPLAYER_TONEDATA_STR_SIZE*sizeof(wchar_t));
GlobalUnlock(gmem);
if (!OpenClipboard(hwnd)) return;
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, gmem);
CloseClipboard();
} else if (id == ID_NORMALIZE && code == BN_CLICKED) {
g.normalize ^= 1;
Button_SetCheck(hwnd_c, g.normalize);
} else if (id == ID_LIST && code == CBN_SELCHANGE) {
g.format = ComboBox_GetCurSel(g.formatlist);
}
}
static bool on_create(HWND hwnd, const CREATESTRUCT *cs) {
RECT wr;
wr.left = 0;
wr.right = WIN_W;
wr.top = 0;
wr.bottom = WIN_H;
DWORD style = GetWindowLongPtr(hwnd, GWL_STYLE);
DWORD exstyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
AdjustWindowRectEx(&wr, style, 0, exstyle);
SetWindowPos(hwnd, HWND_TOP, 0, 0, wr.right-wr.left, wr.bottom-wr.top,
SWP_NOZORDER | SWP_NOMOVE);
if (!g.font_mono) {
g.font_mono = CreateFont(
16, 0, 0, 0,
FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
CLEARTYPE_QUALITY, FIXED_PITCH, 0);
}
if (!g.font) {
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(ncm);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0);
g.font = CreateFontIndirect(&ncm.lfMessageFont);
}
g.formatlist = CreateWindowEx(0, L"combobox", 0,
WS_TABSTOP | WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
10, 10, LIST_W, 100,
hwnd, (HMENU)ID_LIST, g.hinst, 0);
SetWindowFont(g.formatlist, g.font, TRUE);
ComboBox_AddString(g.formatlist, L"PMD");
ComboBox_AddString(g.formatlist, L"FMP");
ComboBox_SetCurSel(g.formatlist, 0);
g.checkbox = CreateWindowEx(0, L"button",
L"&Normalize",
WS_CHILD | WS_VISIBLE | BS_CHECKBOX | WS_TABSTOP,
NORMALIZE_X, 10, NORMALIZE_W, 25,
hwnd, (HMENU)ID_NORMALIZE, g.hinst, 0
);
Button_SetCheck(g.checkbox, g.normalize);
SetWindowFont(g.checkbox, g.font, TRUE);
for (int i = 0; i < 6; i++) {
g.tonelabel[i] = CreateWindowEx(WS_EX_CLIENTEDGE, L"static",
L"@ 0\n123 123",
WS_VISIBLE | WS_CHILD,
10, 40 + (TONELABEL_H+5)*i, TONELABEL_W, TONELABEL_H,
hwnd, 0, g.hinst, 0);
SetWindowFont(g.tonelabel[i], g.font_mono, TRUE);
wchar_t text[] = L"Copy (& )";
text[7] = L'1' + i;
HWND copybutton = CreateWindowEx(0, L"button",
text,
WS_VISIBLE | WS_CHILD | WS_TABSTOP,
COPY_X, 40 + (TONELABEL_H+5)*i, 100, TONELABEL_H,
hwnd, (HMENU)(ID_COPY0+i), g.hinst, 0);
SetWindowFont(copybutton, g.font, TRUE);
}
ShowWindow(hwnd, SW_SHOW);
SetTimer(hwnd, TIMER_UPDATE, 16, 0);
return true;
}
static void on_timer(HWND hwnd, UINT id) {
if (id == TIMER_UPDATE) {
if (!atomic_flag_test_and_set_explicit(
&toneview_g.flag, memory_order_acquire)) {
g.tonedata = toneview_g.tonedata;
atomic_flag_clear_explicit(&toneview_g.flag, memory_order_release);
}
g.tonedata_n = g.tonedata;
for (int c = 0; c < 6; c++) {
if (g.normalize) {
tonedata_ch_normalize_tl(&g.tonedata_n.ch[c]);
}
tonedata_ch_string(g.format, g.strbuf, &g.tonedata_n.ch[c], 0);
for (int i = 0; i < FMPLAYER_TONEDATA_STR_SIZE; i++) {
g.strbuf_w[i] = g.strbuf[i];
}
// RedrawWindow(g.tonelabel[c], 0, 0, RDW_ERASE);
SetWindowText(g.tonelabel[c], g.strbuf_w);
}
}
}
static void on_activate(HWND hwnd, bool activate, HWND targetwnd, WINBOOL state) {
if (activate) g_currentdlg = hwnd;
else g_currentdlg = 0;
}
static LRESULT CALLBACK wndproc(
HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam
) {
switch (msg) {
HANDLE_MSG(hwnd, WM_DESTROY, on_destroy);
HANDLE_MSG(hwnd, WM_CREATE, on_create);
HANDLE_MSG(hwnd, WM_TIMER, on_timer);
HANDLE_MSG(hwnd, WM_COMMAND, on_command);
HANDLE_MSG(hwnd, WM_ACTIVATE, on_activate);
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
void show_toneview(HINSTANCE hinst, HWND parent) {
g.hinst = hinst;
if (!g.toneviewer) {
if (!g.toneviewer_class) {
WNDCLASS wc = {0};
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = wndproc;
wc.hInstance = hinst;
wc.hIcon = LoadIcon(g.hinst, MAKEINTRESOURCE(1));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
wc.lpszClassName = L"myon_fmplayer_ym2608_toneviewer";
g.toneviewer_class = RegisterClass(&wc);
}
if (!g.toneviewer_class) {
MessageBox(parent, L"Cannot register class", L"Error", MB_ICONSTOP);
return;
}
g.toneviewer = CreateWindowEx(0,
MAKEINTATOM(g.toneviewer_class),
L"FMPlayer Tone Viewer",
WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
parent, 0, g.hinst, 0);
} else {
SetForegroundWindow(g.toneviewer);
}
}
|