Skip to content

Commit 3cef135

Browse files
committed
Made the basis of settings
1 parent a6de2db commit 3cef135

File tree

2 files changed

+144
-4
lines changed

2 files changed

+144
-4
lines changed

src/win/WinApp.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
static const char* WINDOW_CLASS = "SplitLoafTray";
9+
static HINSTANCE g_hInstance = NULL;
910

1011
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1112
{
@@ -19,7 +20,7 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1920
case WM_COMMAND:
2021
switch (LOWORD(wParam)) {
2122
case CMD_SETTINGS:
22-
OpenSettingsWindow(hInstance, hwnd);
23+
OpenSettingsWindow(g_hInstance, hwnd);
2324
break;
2425

2526
case CMD_EXIT:
@@ -39,20 +40,20 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
3940

4041
int RunWindowsApp()
4142
{
42-
HINSTANCE hInstance = GetModuleHandle(NULL);
43+
g_hInstance = GetModuleHandle(NULL);
4344

4445
// Register class
4546
WNDCLASS wc = {};
4647
wc.lpfnWndProc = WindowProc;
47-
wc.hInstance = hInstance;
48+
wc.hInstance = g_hInstance;
4849
wc.lpszClassName = WINDOW_CLASS;
4950
RegisterClass(&wc);
5051

5152
// Create hidden window
5253
HWND hwnd = CreateWindowEx(
5354
0, WINDOW_CLASS, "Split Loaf",
5455
0, 0, 0, 0, 0,
55-
NULL, NULL, hInstance, NULL
56+
NULL, NULL, g_hInstance, NULL
5657
);
5758

5859
InitTrayIcon(hwnd);

src/win/WinSettings.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include "WinSettings.h"
2+
#include <windows.h>
3+
#include <string>
4+
5+
static HWND hWndSettings = NULL;
6+
7+
static UINT targetKey = VK_F8;
8+
static UINT lockKey = VK_F9;
9+
static UINT unlockKey = VK_F10;
10+
11+
static bool captureMode = false;
12+
static UINT* captureTarget = nullptr;
13+
14+
// Run on startup
15+
static const char* RUN_KEY = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
16+
static const char* RUN_NAME = "SplitLoaf";
17+
18+
bool GetRunOnStartup() {
19+
HKEY hKey;
20+
if (RegOpenKeyExA(HKEY_CURRENT_USER, RUN_KEY, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
21+
return false;
22+
23+
char buffer[MAX_PATH];
24+
DWORD size = sizeof(buffer);
25+
bool exists = (RegQueryValueExA(hKey, RUN_NAME, NULL, NULL, (LPBYTE)buffer, &size) == ERROR_SUCCESS);
26+
RegCloseKey(hKey);
27+
return exists;
28+
}
29+
30+
void SetRunOnStartup(bool enable) {
31+
HKEY hKey;
32+
RegOpenKeyExA(HKEY_CURRENT_USER, RUN_KEY, 0, KEY_WRITE, &hKey);
33+
34+
if (enable) {
35+
char exePath[MAX_PATH];
36+
GetModuleFileNameA(NULL, exePath, MAX_PATH);
37+
RegSetValueExA(hKey, RUN_NAME, 0, REG_SZ, (BYTE*)exePath, strlen(exePath) + 1);
38+
} else {
39+
RegDeleteValueA(hKey, RUN_NAME);
40+
}
41+
42+
RegCloseKey(hKey);
43+
}
44+
45+
LRESULT CALLBACK SettingsWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
46+
switch (msg) {
47+
case WM_CREATE: {
48+
49+
CreateWindow("STATIC", "Target Key:", WS_VISIBLE | WS_CHILD,
50+
20, 20, 150, 20, hwnd, NULL, NULL, NULL);
51+
CreateWindow("BUTTON", "Change", WS_VISIBLE | WS_CHILD,
52+
200, 15, 100, 30, hwnd, (HMENU)1, NULL, NULL);
53+
54+
CreateWindow("STATIC", "Lock Key:", WS_VISIBLE | WS_CHILD,
55+
20, 60, 150, 20, hwnd, NULL, NULL, NULL);
56+
CreateWindow("BUTTON", "Change", WS_VISIBLE | WS_CHILD,
57+
200, 55, 100, 30, hwnd, (HMENU)2, NULL, NULL);
58+
59+
CreateWindow("STATIC", "Unlock Key:", WS_VISIBLE | WS_CHILD,
60+
20, 100, 150, 20, hwnd, NULL, NULL, NULL);
61+
CreateWindow("BUTTON", "Change", WS_VISIBLE | WS_CHILD,
62+
200, 95, 100, 30, hwnd, (HMENU)3, NULL, NULL);
63+
64+
CreateWindow("BUTTON", "Run on startup",
65+
WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
66+
20, 140, 200, 20, hwnd, (HMENU)4, NULL, NULL);
67+
68+
if (GetRunOnStartup())
69+
SendDlgItemMessage(hwnd, 4, BM_SETCHECK, BST_CHECKED, 0);
70+
71+
CreateWindow("BUTTON", "Close", WS_VISIBLE | WS_CHILD,
72+
120, 180, 100, 30, hwnd, (HMENU)10, NULL, NULL);
73+
}
74+
break;
75+
76+
case WM_COMMAND:
77+
switch (LOWORD(wParam)) {
78+
case 1: captureMode = true; captureTarget = &targetKey; break;
79+
case 2: captureMode = true; captureTarget = &lockKey; break;
80+
case 3: captureMode = true; captureTarget = &unlockKey; break;
81+
82+
case 4: { // enable/disable startup
83+
bool checked = SendDlgItemMessage(hwnd, 4, BM_GETCHECK, 0, 0) == BST_CHECKED;
84+
SetRunOnStartup(checked);
85+
}
86+
break;
87+
88+
case 10:
89+
DestroyWindow(hwnd);
90+
hWndSettings = NULL;
91+
break;
92+
}
93+
break;
94+
95+
case WM_KEYDOWN:
96+
if (captureMode && captureTarget) {
97+
*captureTarget = (UINT)wParam;
98+
captureMode = false;
99+
captureTarget = nullptr;
100+
}
101+
break;
102+
103+
case WM_CLOSE:
104+
DestroyWindow(hwnd);
105+
hWndSettings = NULL;
106+
break;
107+
108+
case WM_DESTROY:
109+
hWndSettings = NULL;
110+
break;
111+
}
112+
113+
return DefWindowProc(hwnd, msg, wParam, lParam);
114+
}
115+
116+
void OpenSettingsWindow(HINSTANCE hInstance, HWND parent) {
117+
if (hWndSettings) { // already open
118+
SetForegroundWindow(hWndSettings);
119+
return;
120+
}
121+
122+
WNDCLASS wc = {};
123+
wc.lpfnWndProc = SettingsWndProc;
124+
wc.hInstance = hInstance;
125+
wc.lpszClassName = "SplitLoafSettings";
126+
127+
RegisterClass(&wc);
128+
129+
hWndSettings = CreateWindow(
130+
"SplitLoafSettings",
131+
"Split Loaf Settings",
132+
WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME,
133+
CW_USEDEFAULT, CW_USEDEFAULT,
134+
360, 260,
135+
parent, NULL, hInstance, NULL
136+
);
137+
138+
ShowWindow(hWndSettings, SW_SHOW);
139+
}

0 commit comments

Comments
 (0)