INT_PTR CALLBACK GeneralDialogProc()

in Lilypad/Config.cpp [1756:2034]


INT_PTR CALLBACK GeneralDialogProc(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam) {
	int i;
	HWND hWndList = GetDlgItem(hWnd, IDC_PAD_LIST);
	switch (msg) {
	case WM_INITDIALOG:
		{
			HWND hWndCombo = GetDlgItem(hWnd, IDC_PAD_TYPE);
			if (SendMessage(hWndCombo, CB_GETCOUNT, 0, 0) == 0) {
				LVCOLUMN c;
				c.mask = LVCF_TEXT | LVCF_WIDTH;
				c.cx = 50;
				c.pszText = L"Pad";
				ListView_InsertColumn(hWndList, 0, &c);
				c.cx = 120;
				c.pszText = L"Type";
				ListView_InsertColumn(hWndList, 1, &c);
				c.cx = 70;
				c.pszText = L"Bindings";
				ListView_InsertColumn(hWndList, 2, &c);
				selected = 0;
				ListView_SetExtendedListViewStyleEx(hWndList, LVS_EX_FULLROWSELECT|LVS_EX_DOUBLEBUFFER, LVS_EX_FULLROWSELECT|LVS_EX_DOUBLEBUFFER);
				SendMessage(hWndList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
				SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM) L"Unplugged");
				SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM) L"Dualshock 2");
				SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM) L"Guitar");
			}
		}
		UpdatePadPages();
		hWndGeneral = hWnd;
		RefreshEnabledDevicesAndDisplay(0, hWnd, 0);
		UpdatePadList(hWnd);

		if (!DualShock3Possible()) {
			config.gameApis.dualShock3 = 0;
			EnableWindow(GetDlgItem(hWnd, IDC_G_DS3), 0);
		}

		for (int j=0; j<sizeof(BoolOptionsInfo)/sizeof(BoolOptionsInfo[0]); j++) {
			CheckDlgButton(hWnd, BoolOptionsInfo[j].ControlId, BST_CHECKED * config.bools[j]);
		}

		CheckDlgButton(hWnd, IDC_CLOSE_HACK1, BST_CHECKED * (config.closeHacks&1));
		CheckDlgButton(hWnd, IDC_CLOSE_HACK2, BST_CHECKED * ((config.closeHacks&2)>>1));

		if (config.osVersion < 6) EnableWindow(GetDlgItem(hWnd, IDC_VISTA_VOLUME), 0);


		if (config.keyboardApi < 0 || config.keyboardApi > 3) config.keyboardApi = NO_API;
		CheckRadioButton(hWnd, IDC_KB_DISABLE, IDC_KB_RAW, IDC_KB_DISABLE + config.keyboardApi);
		if (config.mouseApi < 0 || config.mouseApi > 3) config.mouseApi = NO_API;
		CheckRadioButton(hWnd, IDC_M_DISABLE, IDC_M_RAW, IDC_M_DISABLE + config.mouseApi);

		if (!InitializeRawInput()) {
			EnableWindow(GetDlgItem(hWnd, IDC_KB_RAW), 0);
			EnableWindow(GetDlgItem(hWnd, IDC_M_RAW), 0);
		}
		break;
	case WM_DEVICECHANGE:
		if (wParam == DBT_DEVNODES_CHANGED) {
			RefreshEnabledDevicesAndDisplay(1, hWndGeneral, 1);
			UpdatePadList(hWnd);
		}
		break;
	case WM_COMMAND:
		if (LOWORD(wParam) == IDC_PAD_TYPE) {
			if (HIWORD(wParam) == CBN_SELCHANGE) {
				HWND hWndCombo = GetDlgItem(hWnd, IDC_PAD_TYPE);
				int index = ListView_GetNextItem(hWndList, -1, LVNI_SELECTED);
				int sel = SendMessage(hWndCombo, CB_GETCURSEL, 0, 0);
				int port, slot;
				if (sel < 0 || !ListIndexToPortAndSlot(index, &port, &slot)) break;
				if (sel != config.padConfigs[port][slot].type) {
					config.padConfigs[port][slot].type = (PadType)sel;
					UpdatePadList(hWnd);
					UpdatePadPages();
					RefreshEnabledDevicesAndDisplay(0, hWnd, 1);
					PropSheet_Changed(hWndProp, hWnd);
				}
			}
		}
		else if (HIWORD(wParam)==BN_CLICKED && (LOWORD(wParam) == ID_LOAD || LOWORD(wParam) == ID_SAVE)) {
			OPENFILENAMEW ofn;
			memset (&ofn, 0, sizeof(ofn));
			ofn.lStructSize = sizeof(ofn);
			ofn.hwndOwner = hWnd;
			ofn.lpstrFilter = L"LilyPad Config Files\0*.lily\0All Files\0*.*\0\0";
			wchar_t file[MAX_PATH+1];
			ofn.lpstrFile = file;
			ofn.nMaxFile = MAX_PATH;
			wcscpy(file, config.lastSaveConfigFileName);
			ofn.lpstrInitialDir = config.lastSaveConfigPath;
			ofn.Flags = OFN_DONTADDTORECENT | OFN_LONGNAMES | OFN_NOCHANGEDIR;
			if (LOWORD(wParam) == ID_LOAD) {
				ofn.lpstrTitle = L"Load LilyPad Configuration";
				ofn.Flags |= OFN_FILEMUSTEXIST;
				if (GetOpenFileNameW(&ofn)) {
					LoadSettings(1, ofn.lpstrFile);
					GeneralDialogProc(hWnd, WM_INITDIALOG, 0, 0);
					PropSheet_Changed(hWndProp, hWnd);
				}
			}
			else {
				ofn.lpstrTitle = L"Save LilyPad Configuration";
				ofn.Flags |= OFN_OVERWRITEPROMPT;
				if (GetSaveFileNameW(&ofn)) {
					if (SaveSettings(ofn.lpstrFile) == -1) {
						MessageBox(hWnd, L"Save fail", L"Couldn't save file.", MB_OK);
					}
				}
			}
			break;
		}

		else if (HIWORD(wParam)==BN_CLICKED && LOWORD(wParam) == ID_TEST) {
			Diagnostics(hWnd);
			RefreshEnabledDevices();
		}
		else if (HIWORD(wParam)==BN_CLICKED && LOWORD(wParam) == ID_REFRESH) {
			RefreshEnabledDevicesAndDisplay(1, hWnd, 1);
			UpdatePadList(hWnd);
		}
		else if (HIWORD(wParam)==BN_CLICKED && LOWORD(wParam) == IDC_ANALOG_START1) {
			int index = ListView_GetNextItem(hWndList, -1, LVNI_SELECTED);
			int port, slot;
			if (!ListIndexToPortAndSlot(index, &port, &slot)) break;
			config.padConfigs[port][slot].autoAnalog = (IsDlgButtonChecked(hWnd, IDC_ANALOG_START1) == BST_CHECKED);
			PropSheet_Changed(hWndProp, hWnd);
		}
		else {
			int t = IDC_CLOSE_HACK1;
			int test = LOWORD(wParam);
			if (test == IDC_CLOSE_HACK1) {
				CheckDlgButton(hWnd, IDC_CLOSE_HACK2, BST_UNCHECKED);
			}
			else if (test == IDC_CLOSE_HACK2) {
				CheckDlgButton(hWnd, IDC_CLOSE_HACK1, BST_UNCHECKED);
			}

			int mtap = config.multitap[0] + 2*config.multitap[1];
			int vistaVol = config.vistaVolume;

			for (int j=0; j<sizeof(BoolOptionsInfo)/sizeof(BoolOptionsInfo[0]); j++) {
				config.bools[j] = (IsDlgButtonChecked(hWnd, BoolOptionsInfo[j].ControlId) == BST_CHECKED);
			}

			config.closeHacks = (IsDlgButtonChecked(hWnd, IDC_CLOSE_HACK1) == BST_CHECKED) |
				((IsDlgButtonChecked(hWnd, IDC_CLOSE_HACK2) == BST_CHECKED)<<1);

			if (!config.vistaVolume) {
				if (vistaVol) {
					// Restore volume if just disabled.  Don't touch, otherwise, just in case
					// sound plugin plays with it.
					SetVolume(100);
				}
				config.vistaVolume = 100;
			}

			for (i=0; i<4; i++) {
				if (i && IsDlgButtonChecked(hWnd, IDC_KB_DISABLE+i) == BST_CHECKED) {
					config.keyboardApi = (DeviceAPI)i;
				}
				if (IsDlgButtonChecked(hWnd, IDC_M_DISABLE+i) == BST_CHECKED) {
					config.mouseApi = (DeviceAPI)i;
				}
			}

			if (mtap != config.multitap[0] + 2*config.multitap[1]) {
				UpdatePadPages();
			}
			RefreshEnabledDevicesAndDisplay(0, hWnd, 1);
			UpdatePadList(hWnd);

			PropSheet_Changed(hWndProp, hWnd);
		}
		break;
	case WM_NOTIFY:
		{
			PSHNOTIFY* n = (PSHNOTIFY*) lParam;
			if (n->hdr.hwndFrom == hWndProp) {
				switch(n->hdr.code) {
				case PSN_QUERYCANCEL:
				case PSN_KILLACTIVE:
					EndBinding(hWnd);
					return 0;
				case PSN_SETACTIVE:
					//selected = 0;
					UpdatePadList(hWnd);
					return 0;
				case PSN_APPLY:
					selected = 0;
					if (SaveSettings()) {
						SetWindowLong(hWnd, DWL_MSGRESULT, PSNRET_INVALID_NOCHANGEPAGE);
						return 0;
					}
					SetWindowLong(hWnd, DWL_MSGRESULT, PSNRET_NOERROR);
					return 1;
				}
			}
			else if (n->hdr.idFrom == IDC_LIST && n->hdr.code == NM_DBLCLK) {
				Diagnostics(hWnd);
			}
			else if (n->hdr.idFrom == IDC_PAD_LIST) {
				if (n->hdr.code == LVN_ITEMCHANGED) {
					UpdatePadList(hWnd);
				}
				if (n->hdr.code == NM_RCLICK) {
					UpdatePadList(hWnd);
					int index = ListView_GetNextItem(hWndList, -1, LVNI_SELECTED);
					int port1, slot1, port2, slot2;
					if (!ListIndexToPortAndSlot(index, &port1, &slot1)) break;
					HMENU hMenu = CreatePopupMenu();
					if (!hMenu) break;
					MENUITEMINFOW info;
					for (port2=1; port2>=0; port2--) {
						for (slot2 = 3; slot2>=0; slot2--) {
							wchar_t text[40];
							wchar_t pad[20];
							if (!GetPadString(pad, port2, slot2)) continue;
							info.cbSize = sizeof(info);
							info.fMask = MIIM_STRING | MIIM_ID;
							info.dwTypeData = text;
							if (port2 == port1 && slot2 == slot1) {
								int index = GetMenuItemCount(hMenu);
								wsprintfW(text, L"Clear %s Bindings", pad);
								info.wID = -1;
								InsertMenuItemW(hMenu, index, 1, &info);
								info.fMask = MIIM_TYPE;
								info.fType = MFT_SEPARATOR;
								InsertMenuItemW(hMenu, index, 1, &info);
							}
							else {
								info.wID = port2+2*slot2+1;
								wsprintfW(text, L"Swap with %s", pad);
								InsertMenuItemW(hMenu, 0, 1, &info);
							}
						}
					}
					POINT pos;
					GetCursorPos(&pos);
					short res = TrackPopupMenuEx(hMenu, TPM_NONOTIFY|TPM_RETURNCMD, pos.x, pos.y, hWndProp, 0);
					DestroyMenu(hMenu);
					if (!res) break;
					if (res > 0) {
						res--;
						slot2 = res / 2;
						port2 = res&1;
						PadConfig padCfgTemp = config.padConfigs[port1][slot1];
						config.padConfigs[port1][slot1] = config.padConfigs[port2][slot2];
						config.padConfigs[port2][slot2] = padCfgTemp;
						for (int i=0; i<dm->numDevices; i++) {
							if (dm->devices[i]->type == IGNORE) continue;
							PadBindings bindings = dm->devices[i]->pads[port1][slot1];
							dm->devices[i]->pads[port1][slot1] = dm->devices[i]->pads[port2][slot2];
							dm->devices[i]->pads[port2][slot2] = bindings;
						}
					}
					else {
						for (int i=0; i<dm->numDevices; i++) {
							if (dm->devices[i]->type == IGNORE) continue;
							free(dm->devices[i]->pads[port1][slot1].bindings);
							for (int j=0; j<dm->devices[i]->pads[port1][slot1].numFFBindings; j++) {
								free(dm->devices[i]->pads[port1][slot1].ffBindings[j].axes);
							}
							free(dm->devices[i]->pads[port1][slot1].ffBindings);
							memset(&dm->devices[i]->pads[port1][slot1], 0, sizeof(dm->devices[i]->pads[port1][slot1]));
						}
					}
					UpdatePadPages();
					UpdatePadList(hWnd);
					PropSheet_Changed(hWndProp, hWnd);
				}
			}
		}
		break;
	default:
		break;
	}
	return 0;
}