Skip to content

Commit 23fe35f

Browse files
committed
Fixed auto-selecting tabs and matched titles. Fixed selecting mods.
1 parent 7a324df commit 23fe35f

File tree

3 files changed

+66
-37
lines changed

3 files changed

+66
-37
lines changed

gui-helper-win.c

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lPara
128128
if (res != LB_ERR)
129129
{
130130
selected_index = res;
131+
int tabIndex = TabCtrl_GetCurSel(hWndTab);
132+
int tabSize = sizeof(tabCounts) / sizeof(tabCounts[0]);
133+
for (int i = 0; i < tabIndex; i++)
134+
{
135+
selected_index += tabCounts[i];
136+
}
131137
PostQuitMessage(WM_QUIT);
132138
}
133139
break;
@@ -157,7 +163,7 @@ int ShowMessageBox(const char* title, const char* message)
157163
return MessageBox(NULL, message, title, MB_OK | MB_ICONEXCLAMATION);
158164
}
159165

160-
int SelectionDialog(const wchar_t* title, int count, const char** list, int modsCount, const char** modsList, int selection)
166+
int SelectionDialog(const char* title, int count, const char** list, int modsCount, const char** modsList, int selection)
161167
{
162168
INITCOMMONCONTROLSEX icex;
163169
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
@@ -201,7 +207,7 @@ int SelectionDialog(const wchar_t* title, int count, const char** list, int mods
201207

202208
RegisterClassExW(&wc);
203209

204-
const wchar_t* unicodeTitle = title;
210+
const wchar_t* unicodeTitle = ConvertStringToUnicode(title);
205211

206212
hWnd = CreateWindowExW(
207213
WS_EX_CLIENTEDGE,
@@ -214,34 +220,40 @@ int SelectionDialog(const wchar_t* title, int count, const char** list, int mods
214220
hInstance, NULL
215221
);
216222

217-
//free(unicodeTitle);
223+
free(unicodeTitle);
218224

219-
for (int i = 0; i < count; ++i)
225+
// Find current tab and selection, and populate the ListBox
226+
int tabIndex;
227+
int tabSize = sizeof(tabCounts) / sizeof(tabCounts[0]);
228+
for (tabIndex = 0; tabIndex < tabSize; tabIndex++)
220229
{
221-
wchar_t * unicodeGameName = ConvertStringToUnicode(list[i]);
222-
SendMessageW(hWndList, LB_ADDSTRING, 0, (LPARAM)(LPCWSTR)unicodeGameName);
223-
free(unicodeGameName);
230+
if (tabCounts[tabIndex] < selection) {
231+
selection -= tabCounts[tabIndex];
232+
} else {
233+
break;
234+
}
224235
}
236+
PopulateListBoxWithSelection(tabIndex, selection);
225237

226-
SendMessageW(hWndList, LB_SETCURSEL, selection, 0);
227-
238+
// Show window and get all events
228239
ShowWindow(hWnd, SW_SHOWNORMAL);
229240

230-
// TODO: Start at where game is matched
231-
PopulateListBox(0);
232-
233241
while (GetMessage(&Msg, NULL, 0, 0))
234242
{
235243
TranslateMessage(&Msg);
236244
DispatchMessage(&Msg);
237245
}
238246

247+
// Return the current selection
239248
return selected_index;
240249
}
241250

242-
void PopulateListBox(int tabIndex)
251+
// Load values into current ListBox with a highlighted option
252+
void PopulateListBoxWithSelection(int tabIndex, int selection)
243253
{
244-
SendMessage(hWndList, LB_RESETCONTENT, 0, 0);
254+
SendMessageW(hWndList, LB_RESETCONTENT, 0, 0);
255+
/*SendMessageW(hWndList, TCM_SETCURSEL, 0, (LPARAM)tabIndex);*/
256+
TabCtrl_SetCurSel(hWndTab, tabIndex);
245257

246258
for (int i = 0; i < tabCounts[tabIndex]; ++i)
247259
{
@@ -250,11 +262,17 @@ void PopulateListBox(int tabIndex)
250262
free(unicode);
251263
}
252264

253-
if (tabCounts[tabIndex] > 0) {
254-
SendMessage(hWndList, LB_SETCURSEL, 0, 0);
265+
if (selection > -1) {
266+
SendMessageW(hWndList, LB_SETCURSEL, selection, 0);
255267
}
256268
}
257269

270+
// Load valuies into current ListBox without a highlighted option
271+
void PopulateListBox(int tabIndex)
272+
{
273+
PopulateListBoxWithSelection(tabIndex, -1);
274+
}
275+
258276

259277
// Convert ANSI string to unicode (char to widechar)
260278
wchar_t* ConvertStringToUnicode(const char* string)

gui-helper.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
#define GUI_HELPER_H
33

44
int ShowMessageBox(const char* title, const char* message);
5+
void PopulateListBoxWithSelection(int tabIndex, int selection);
56
void PopulateListBox(int tabIndex);
67

7-
#if defined(_WIN32) || defined(WIN32)
8-
int SelectionDialog(const wchar_t* title, int count, const char** list, int modsCount, const char** modsList, int selection);
98

9+
int SelectionDialog(const char * title, int count, const char** list, int modsCount, const char** modsList, int selection);
10+
11+
#if defined(_WIN32) || defined(WIN32)
1012
// This function is only used in Windows. It shouldn't exist, but we have to deal with Microsoft.
1113
wchar_t * ConvertStringToUnicode(const char* string);
12-
#else
13-
int SelectionDialog(const char* title, int count, const char** list, int modsCount, const char** modsList, int selection);
1414
#endif
1515

1616
#endif

sgdboop.c

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -946,20 +946,10 @@ struct nonSteamApp* selectNonSteamApp(char* sgdbName, struct nonSteamApp* apps,
946946
struct nonSteamApp* appData = malloc(sizeof(nonSteamApp));
947947

948948
// Create title string
949-
#if OS_Windows
950-
wchar_t* sgdbNameW = ConvertStringToUnicode(sgdbName);
951-
wchar_t* title = malloc((40 + wcslen(sgdbNameW)) * sizeof(wchar_t));
952-
953-
wcscpy(title, L"SGDBoop: Pick a game for '");
954-
wcscat(title, sgdbNameW);
955-
wcscat(title, L"'");
956-
free(sgdbNameW);
957-
#else
958949
char* title = malloc(40 + strlen(sgdbName));
959950
strcpy(title, "SGDBoop: Pick a game for '");
960951
strcat(title, sgdbName);
961952
strcat(title, "'");
962-
#endif
963953

964954
// Sort values
965955
qsort(values, _nonSteamAppsCount, sizeof(const char*), compareStrings);
@@ -974,6 +964,15 @@ struct nonSteamApp* selectNonSteamApp(char* sgdbName, struct nonSteamApp* apps,
974964
}
975965
}
976966

967+
if (selection < 0) {
968+
for (int i = 0; i < _modsCount; i++) {
969+
if (strcmp_i(modsValues[i], sgdbName) == 0) {
970+
selection = i + _nonSteamAppsCount;
971+
break;
972+
}
973+
}
974+
}
975+
977976
// Create selection dialog
978977
int retval = SelectionDialog(title, _nonSteamAppsCount, (const char**)values, _modsCount, (const char**)modsValues, selection);
979978

@@ -987,14 +986,26 @@ struct nonSteamApp* selectNonSteamApp(char* sgdbName, struct nonSteamApp* apps,
987986
exit(0);
988987
}
989988

989+
990990
// Find match
991-
for (int i = 0; i < _nonSteamAppsCount; i++) {
992-
if (strcmp(apps[i].name, values[retval]) == 0) {
993-
strcpy(appData->appid, apps[i].appid);
994-
strcpy(appData->name, apps[i].name);
995-
appData->index = apps[i].index;
996-
strcpy(appData->appid_old, apps[i].appid_old);
997-
strcpy(appData->type, apps[i].type);
991+
struct nonSteamApp * matchedStruct;
992+
char ** matchedValues;
993+
if (retval >= _nonSteamAppsCount) {
994+
retval -= _nonSteamAppsCount;
995+
matchedStruct = appsMods;
996+
matchedValues = modsValues;
997+
} else {
998+
matchedStruct = apps;
999+
matchedValues = values;
1000+
}
1001+
1002+
for (int i = 0; i < _nonSteamAppsCount + _modsCount; i++) {
1003+
if (strcmp(matchedStruct[i].name, matchedValues[retval]) == 0) {
1004+
strcpy(appData->appid, matchedStruct[i].appid);
1005+
strcpy(appData->name, matchedStruct[i].name);
1006+
appData->index = matchedStruct[i].index;
1007+
strcpy(appData->appid_old, matchedStruct[i].appid_old);
1008+
strcpy(appData->type, matchedStruct[i].type);
9981009
break;
9991010
}
10001011
}

0 commit comments

Comments
 (0)