diff src/gui.c @ 81:c06dcab17923 v2.1

*: change license to BSD, update README for Unicode src/gui: use tchar.h for unicode instead of our own macros
author Paper <paper@paper.us.eu.org>
date Fri, 22 Mar 2024 22:04:16 -0400
parents 8f90d5addda9
children
line wrap: on
line diff
--- a/src/gui.c	Wed Mar 20 17:09:43 2024 -0400
+++ b/src/gui.c	Fri Mar 22 22:04:16 2024 -0400
@@ -1,31 +1,4 @@
-/**
- * msvpvf GUI for Windows 
- * Copyright (c) Paper 2022-2024
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- * 
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
-**/
-
-/* change these macros to configure the window size */
-#define WINDOW_WIDTH  225
-#define WINDOW_HEIGHT 200
-
-/* mingw */
+/* mingw hack */
 #ifdef UNICODE
 #define NTDDI_VERSION 0x06000000
 #define _WIN32_WINNT  0x0600
@@ -36,40 +9,46 @@
 #include <windef.h>
 #include <winbase.h>
 #include <shlwapi.h>
-#include <shobjidl.h>
 
 #include <stdint.h>
 #include <stdio.h>
 
+#include <tchar.h> /* tchar versions of string.h functions */
+
 #include "common.h"
 
-/* we use COM when `UNICODE=1` to avoid file paths being cut off */
-#define COM_INITFLAGS (COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)
-
-#define OPEN_FILE_BUTTON	0
-#define COMBOBOX		1
-#define LISTBOX			2
-#define SAVE_FILE_BUTTON	3
-#define VERSION			4
-
-/* adjust for these functions for Unicode */
-#if UNICODE
-#define _tstrncpy wcsncpy
-#define _tstrdup _wcsdup
-#define _tfopen _wfopen
-#define _sntprintf _snwprintf
-#else
-#define _tstrncpy strncpy
-#define _tstrdup _strdup
-#define _tfopen fopen
-#define _sntprintf _snprintf
+/* make sure this is defined... */
+#ifndef _MAX_ULTOSTR_BASE10_COUNT
+#define _MAX_ULTOSTR_BASE10_COUNT (10 + 1)
 #endif
 
-/* enumerate over this */
-static const enum types types[] = {TYPES_VF, TYPES_VEG};
+/* put all of our types in an array */
+static const enum types types[] = { TYPES_VF, TYPES_VEG };
+
+/* HWND ids */
+enum {
+	OPEN_FILE_BUTTON = 0,
+	COMBOBOX,
+	LISTBOX,
+	SAVE_FILE_BUTTON,
+	VERSION
+};
+
+#if UNICODE
+/* use COM when `UNICODE=1` to avoid file paths being cut off */
+#include <shobjidl.h>
+
+static const DWORD COM_INITFLAGS = (COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
+#endif
+
+/* user-adjustable. set these values to whatever you want, the GUI will scale */
+#define WINDOW_WIDTH  225U
+#define WINDOW_HEIGHT 200U
 
 static LPTSTR file_path = NULL;
-static uint8_t version = 13;
+
+/* these values are loaded by AddControls() if they're valid */
+static uint8_t version = 11;
 static enum types type = TYPES_VEG;
 
 /* we edit this from display_file() */
@@ -243,7 +222,7 @@
 #endif
 			/* fallback to OPENFILENAME if COM fucks up for whatever reason (or we're on ANSI)... */
 			output = calloc(MAX_PATH + 1, sizeof(TCHAR));
-			_tstrncpy(output, output_template, MAX_PATH);
+			_tcsncpy(output, output_template, MAX_PATH);
 			free(output_template);
 
 			OPENFILENAME ofn = {0};
@@ -298,37 +277,38 @@
 								 WINDOW_WIDTH * 7 / 18, WINDOW_HEIGHT * 3 / 20, WINDOW_WIDTH * 2 / 9, WINDOW_HEIGHT, 
 								 hWnd, (HMENU)COMBOBOX, NULL, NULL);
 
-	static LPCTSTR versions[] = {TEXT("8"),  TEXT("9"),  TEXT("10"), 
-	                             TEXT("11"), TEXT("12"), TEXT("13"), 
-	                             TEXT("14"), TEXT("15"), TEXT("16"), 
-	                             TEXT("17"), TEXT("18"), TEXT("19"),
-	                             TEXT("20"), TEXT("21")}; /* wuss 9+10 */
+	for (size_t v = 8; v <= 21; v++) {
+		TCHAR v_str[_MAX_ULTOSTR_BASE10_COUNT] = {'\0'};
+
+		_ultot(v, v_str, 10);
 
-	for (size_t i = 0; i < ARRAYSIZE(versions); i++)
-		SendMessage(combobox, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)versions[i]);
-
-	SendMessage(combobox, CB_SETCURSEL, (WPARAM)3, (LPARAM)0);
+		LRESULT pos = SendMessage(combobox, CB_ADDSTRING, 0, (LPARAM)v_str);
+		SendMessage(combobox, CB_SETITEMDATA, pos, v);
+		if (v == version)
+			SendMessage(combobox, CB_SETCURSEL, pos, 0);
+	}
 
 	/* Type */
 	HWND listbox = CreateWindow(TEXT("Listbox"), NULL, WS_VISIBLE | WS_CHILD | LBS_STANDARD | LBS_NOTIFY, WINDOW_WIDTH * 5 / 18, WINDOW_HEIGHT * 11 / 40, WINDOW_WIDTH * 4 / 9, WINDOW_HEIGHT / 5, hWnd, (HMENU)LISTBOX, NULL, NULL);
 
 	for (size_t i = 0; i < ARRAYSIZE(types); i++) {
 		LRESULT pos = SendMessage(listbox, LB_ADDSTRING, i, (LPARAM)type_to_string(types[i]));
-		SendMessage(listbox, LB_SETITEMDATA, pos, (LPARAM)types[i]);
+		SendMessage(listbox, LB_SETITEMDATA, pos, types[i]);
 		if (types[i] == type)
-			SendMessage(listbox, LB_SETCURSEL, (WPARAM)pos, (LPARAM)0);
+			SendMessage(listbox, LB_SETCURSEL, pos, 0);
 	}
 
 	/* Save File */
 	HWND save_button = CreateWindow(TEXT("Button"), TEXT("Save"), WS_VISIBLE | WS_CHILD, WINDOW_WIDTH * 7 / 18, WINDOW_HEIGHT * 9 / 20, WINDOW_WIDTH * 2 / 9, WINDOW_HEIGHT / 10, hWnd, (HMENU)SAVE_FILE_BUTTON, NULL, NULL);
 
 	/* Version and Type display */
-	hwnd_version = CreateWindow(TEXT("Edit"), TEXT(""), WS_VISIBLE | WS_CHILD | WS_BORDER | ES_READONLY | ES_CENTER | ES_MULTILINE | SS_CENTER, WINDOW_WIDTH / 6, WINDOW_HEIGHT * 3 / 5, WINDOW_WIDTH * 2 / 3, WINDOW_HEIGHT / 5, hWnd, (HMENU)VERSION, NULL, NULL);
+	hwnd_version = CreateWindow(TEXT("Edit"), TEXT("No file opened!"), WS_VISIBLE | WS_CHILD | WS_BORDER | ES_READONLY | ES_CENTER | ES_MULTILINE | SS_CENTER, WINDOW_WIDTH / 6, WINDOW_HEIGHT * 3 / 5, WINDOW_WIDTH * 2 / 3, WINDOW_HEIGHT / 5, hWnd, (HMENU)VERSION, NULL, NULL);
 	if (!open_button || !save_button || !listbox || !combobox || !hwnd_version)
 		MessageBox(hWnd, TEXT("how did you even trigger this"), TEXT("GUI could not be initialized!"), MB_ICONEXCLAMATION); 
 }
 
-int CALLBACK SetFont(HWND child, LPARAM font) {
+/* Make the fonts not as ugly */
+BOOL CALLBACK SetFont(HWND child, LPARAM font) {
 	SendMessage(child, WM_SETFONT, font, 1);
 	return 1;
 }
@@ -338,12 +318,14 @@
 		case WM_COMMAND:
 			if (HIWORD(wParam) == CBN_SELCHANGE) {
 				switch (LOWORD(wParam)) {
-					case COMBOBOX:
-						version = (uint8_t)(8+SendMessage((HWND)lParam, (UINT)CB_GETCURSEL, (WPARAM)0, (LPARAM)0));
+					case COMBOBOX: {
+						LRESULT i = SendMessage((HWND)lParam, CB_GETCURSEL, 0, 0);
+						version = (uint8_t)SendMessage((HWND)lParam, CB_GETITEMDATA, i, 0);
 						break;
+					}
 					case LISTBOX: {
-						LRESULT i = SendMessage((HWND)lParam, (UINT)LB_GETCURSEL, (WPARAM)0, (LPARAM)0);
-						type = (enum types)SendMessage((HWND)lParam, (UINT)LB_GETITEMDATA, (WPARAM)i, (LPARAM)0);
+						LRESULT i = SendMessage((HWND)lParam, LB_GETCURSEL, 0, 0);
+						type = (enum types)SendMessage((HWND)lParam, LB_GETITEMDATA, i, 0);
 						break;
 					}
 					default:
@@ -366,7 +348,7 @@
 			break;
 		case WM_CREATE:
 			AddControls(hWnd);
-			EnumChildWindows(hWnd, (WNDENUMPROC)SetFont, (LPARAM)GetStockObject(DEFAULT_GUI_FONT));
+			EnumChildWindows(hWnd, SetFont, (LPARAM)GetStockObject(DEFAULT_GUI_FONT));
 			break;
 		case WM_DESTROY:
 			PostQuitMessage(0);