-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpmimg_plugin_gui.h
More file actions
368 lines (304 loc) · 11.3 KB
/
Copy pathcpmimg_plugin_gui.h
File metadata and controls
368 lines (304 loc) · 11.3 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* CP/M floppy disk images plugin for the Total Commander.
* Copyright (c) 2022-2026, Oleg Farenyuk aka Indrekis ( indrekis@gmail.com )
*
*/
#pragma once
#ifndef CPMIMG_PLUGIN_GUI_INC
#define CPMIMG_PLUGIN_GUI_INC
#ifndef GUI_PLUGIN_IMPORT
#define DLLEXPORTGUI __declspec(dllexport)
#else
#define DLLEXPORTGUI __declspec(dllimport)
#endif
#ifdef _WIN32
#define STDCALL __stdcall
#else
#define STDCALL
#endif
#include "cpmtools/config.h"
#include "cpmtools/cpmfs.h"
#include "cpmtools/getopt_.h"
#include "sysio_winapi.h"
#include "minimal_fixed_string.h"
#include "plugin_config.h"
//! TODO: Provision for the Linux GUIs
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <vector>
#include <string>
#include <cstdio>
#include "cpmimg_plugin_gui_resources.h"
extern HINSTANCE g_GUI_dlg_hInstance;
class img_type_sel_GUI_t {
private:
HWND hDlg;
HWND hComboFormat;
HWND hEditSecLength;
HWND hEditTracks;
HWND hEditSectrk;
HWND hEditBlksiz;
HWND hEditMaxdir;
HWND hEditDirblks;
HWND hEditBoottrk;
HWND hEditProbability;
HWND hCheckSaveType;
HWND hCheckSaveTypeCur;
HBRUSH hDialogBgBrush = nullptr;
const std::vector<cpm_disk_descr_t>& possible_fmts;
DSK_GEOMETRY img_geom;
bool ui_retry = false;
minimal_fixed_string_t<33> image_type;
bool show_probab = false;
bool save_disk_type_res = false;
bool save_disk_type_for_cur_res = false;
static HFONT hBoldFont;
static HFONT hNormalFont;
void update_info_fields(int idx) {
if (idx < 0 || idx >= static_cast<int>(possible_fmts.size()))
return;
const auto& dsk = possible_fmts[idx];
int match_score = 0;
if (show_probab) {
if (img_geom.dg_secsize == dsk.secLength) {
SendMessage(hEditSecLength, WM_SETFONT, (WPARAM)hBoldFont, TRUE);
++match_score;
}
else {
SendMessage(hEditSecLength, WM_SETFONT, (WPARAM)hNormalFont, TRUE);
}
int geom_total_tracks = img_geom.dg_cylinders * img_geom.dg_heads;
if (geom_total_tracks == dsk.tracks) {
++match_score;
SendMessage(hEditTracks, WM_SETFONT, (WPARAM)hBoldFont, TRUE);
}
else {
SendMessage(hEditTracks, WM_SETFONT, (WPARAM)hNormalFont, TRUE);
}
if (img_geom.dg_sectors == dsk.sectrk) {
++match_score;
SendMessage(hEditSectrk, WM_SETFONT, (WPARAM)hBoldFont, TRUE);
}
else {
SendMessage(hEditSectrk, WM_SETFONT, (WPARAM)hNormalFont, TRUE);
}
}
char buf[128];
sprintf(buf, "%d", dsk.secLength); SetWindowText(hEditSecLength, buf);
sprintf(buf, "%d", dsk.tracks); SetWindowText(hEditTracks, buf);
sprintf(buf, "%d", dsk.sectrk); SetWindowText(hEditSectrk, buf);
sprintf(buf, "%d", dsk.blksiz); SetWindowText(hEditBlksiz, buf);
sprintf(buf, "%d", dsk.maxdir); SetWindowText(hEditMaxdir, buf);
sprintf(buf, "%d", dsk.dirblks); SetWindowText(hEditDirblks, buf);
sprintf(buf, "%d", dsk.boottrk); SetWindowText(hEditBoottrk, buf);
if (show_probab) {
SendMessage(hEditProbability, WM_SETFONT, (WPARAM)hBoldFont, TRUE);
switch (match_score) {
case 0:
SetWindowText(hEditProbability, "No");
break;
case 1:
SetWindowText(hEditProbability, "Low probability");
break;
case 2:
SetWindowText(hEditProbability, "Could be");
break;
case 3:
SetWindowText(hEditProbability, "Yes");
break;
default:
SetWindowText(hEditProbability, "Unknown");
}
}
}
static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {
img_type_sel_GUI_t* pThis = reinterpret_cast<img_type_sel_GUI_t*>(GetWindowLongPtr(hDlg, GWLP_USERDATA));
switch (message) {
case WM_INITDIALOG:
{
pThis = reinterpret_cast<img_type_sel_GUI_t*>(lParam);
SetWindowLongPtr(hDlg, GWLP_USERDATA, lParam);
pThis->hDlg = hDlg;
pThis->hDialogBgBrush = CreateSolidBrush(RGB(240, 240, 255));
return pThis->OnInitDialog();
}
case WM_COMMAND:
if (pThis) {
return pThis->OnCommand(LOWORD(wParam), HIWORD(wParam));
}
break;
case WM_KEYDOWN:
if (pThis && GetFocus() == pThis->hComboFormat) {
return pThis->OnComboKeyDown(wParam);
}
break;
case WM_CTLCOLORDLG:
return (INT_PTR)GetStockObject(LTGRAY_BRUSH);
// return (INT_PTR)(pThis->hDialogBgBrush);
case WM_CTLCOLORSTATIC: {
HDC hdc = (HDC)wParam;
SetBkMode(hdc, TRANSPARENT);
return (INT_PTR)GetStockObject(LTGRAY_BRUSH);
}
case WM_CLOSE:
if (pThis) {
pThis->ui_retry = false;
if (pThis->hDialogBgBrush) {
DeleteObject(pThis->hDialogBgBrush);
pThis->hDialogBgBrush = nullptr;
}
EndDialog(hDlg, IDCANCEL);
}
return TRUE;
}
return FALSE;
}
BOOL OnInitDialog() {
// Center the dialog on screen
RECT rcDlg, rcDesktop;
GetWindowRect(hDlg, &rcDlg);
GetWindowRect(GetDesktopWindow(), &rcDesktop);
int x = (rcDesktop.right - rcDesktop.left - (rcDlg.right - rcDlg.left)) / 2;
int y = (rcDesktop.bottom - rcDesktop.top - (rcDlg.bottom - rcDlg.top)) / 2;
SetWindowPos(hDlg, HWND_TOP, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
// Get control handles
hComboFormat = GetDlgItem(hDlg, IDC_COMBO_FORMAT);
hEditSecLength = GetDlgItem(hDlg, IDC_EDIT_SECLENGTH);
hEditTracks = GetDlgItem(hDlg, IDC_EDIT_TRACKS);
hEditSectrk = GetDlgItem(hDlg, IDC_EDIT_SECTRK);
hEditBlksiz = GetDlgItem(hDlg, IDC_EDIT_BLKSIZ);
hEditMaxdir = GetDlgItem(hDlg, IDC_EDIT_MAXDIR);
hEditDirblks = GetDlgItem(hDlg, IDC_EDIT_DIRBLKS);
hEditBoottrk = GetDlgItem(hDlg, IDC_EDIT_BOOTTRK);
hEditProbability = GetDlgItem(hDlg, IDC_EDIT_PROBABILITY);
hCheckSaveType = GetDlgItem(hDlg, IDC_CHECK_SAVE_TYPE);
hCheckSaveTypeCur = GetDlgItem(hDlg, IDC_CHECK_SAVE_TYPE_CUR);
auto hStaticText = GetDlgItem(hDlg, IDC_STATIC_TITLE);
// Debug: Check if controls were found
if (!hComboFormat) {
MessageBox(hDlg, "ComboBox control not found!", "Error", MB_OK);
return FALSE;
}
// Create fonts
if (!hBoldFont) {
LOGFONT lf;
GetObject(GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
lf.lfWeight = FW_BOLD;
lf.lfHeight = -16; // Approx. 12pt on 96 DPI
hBoldFont = CreateFontIndirect(&lf);
GetObject(GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
lf.lfHeight = -16; // Approx. 12pt on 96 DPI
hNormalFont = CreateFontIndirect(&lf);
}
// Populate combo box
for (const auto& dsk : possible_fmts) {
ComboBox_AddString(hComboFormat, dsk.fmt_name);
}
ComboBox_SetCurSel(hComboFormat, 0);
if (!show_probab) {
// Set checkbox state
Button_SetCheck(hCheckSaveType, BST_CHECKED);
Button_SetCheck(hCheckSaveTypeCur, BST_CHECKED);
// Hide probability field if not needed
ShowWindow(hEditProbability, SW_HIDE);
ShowWindow(GetDlgItem(hDlg, IDC_STATIC_PROBABILITY), SW_HIDE);
}
SendMessage(hStaticText, WM_SETFONT, (WPARAM)hBoldFont, TRUE);
// Update info fields
update_info_fields(0);
return TRUE;
}
BOOL OnCommand(WORD wID, WORD wNotifyCode) {
switch (wID) {
case IDC_COMBO_FORMAT:
if (wNotifyCode == CBN_SELCHANGE) {
int idx = ComboBox_GetCurSel(hComboFormat);
update_info_fields(idx);
}
break;
case IDOK:
{
int idx = ComboBox_GetCurSel(hComboFormat);
if (idx >= 0 && idx < static_cast<int>(possible_fmts.size())) {
image_type = possible_fmts[idx].fmt_name;
ui_retry = true;
save_disk_type_for_cur_res = (Button_GetCheck(hCheckSaveTypeCur) == BST_CHECKED);
save_disk_type_res = (Button_GetCheck(hCheckSaveType) == BST_CHECKED);
EndDialog(hDlg, IDOK);
}
}
break;
case IDCANCEL:
ui_retry = false;
EndDialog(hDlg, IDCANCEL);
break;
}
return FALSE;
}
BOOL OnComboKeyDown(WPARAM wParam) {
int current = ComboBox_GetCurSel(hComboFormat);
int count = ComboBox_GetCount(hComboFormat);
if (wParam == VK_UP && current > 0) {
ComboBox_SetCurSel(hComboFormat, current - 1);
update_info_fields(current - 1);
return TRUE;
}
else if (wParam == VK_DOWN && current < count - 1) {
ComboBox_SetCurSel(hComboFormat, current + 1);
update_info_fields(current + 1);
return TRUE;
}
return FALSE;
}
public:
static bool TestResourceLoading() {
HRSRC hResource = FindResource(g_GUI_dlg_hInstance, MAKEINTRESOURCE(IDD_FORMAT_SELECT), RT_DIALOG);
if (!hResource) {
DWORD error = GetLastError();
char errorMsg[256];
sprintf(errorMsg, "Dialog resource not found! Error: %lu", error);
MessageBox(nullptr, errorMsg, "Error", MB_OK | MB_ICONERROR); // TODO: Check if dialogs are allowed
return false;
}
return true;
}
img_type_sel_GUI_t(const std::vector<cpm_disk_descr_t>& possible_fmts_in,
const DSK_GEOMETRY& geom_in,
bool show_probab_in) :
possible_fmts(possible_fmts_in),
img_geom(geom_in),
show_probab(show_probab_in),
hDlg(nullptr)
{
auto can_load_res = TestResourceLoading();
HWND hParent = GetActiveWindow(); // TODO: check. For Options dialog -- use parent sent by TCmd
// Show modal dialog
INT_PTR result = DialogBoxParam(g_GUI_dlg_hInstance, // GetModuleHandle(nullptr),
MAKEINTRESOURCE(IDD_FORMAT_SELECT),
hParent, // TODO: set to parent sent by TCmd
DlgProc,
reinterpret_cast<LPARAM>(this));
// Debug: Check if dialog creation failed
if (result == -1) {
DWORD error = GetLastError();
char errorMsg[256];
sprintf(errorMsg, "DialogBoxParam failed with error: %lu", error);
MessageBox(nullptr, errorMsg, "Error", MB_OK | MB_ICONERROR); // TODO: Check if dialogs are allowed
ui_retry = false; // Should already be false
}
}
bool attempt_new_read() {
return ui_retry;
}
bool save_disk_type() const {
return save_disk_type_res;
}
bool save_disk_type_for_cur() const {
return save_disk_type_for_cur_res;
}
auto get_image_type() const {
return image_type;
}
};
#endif