// window_games.cpp
#include "../common.h"
// window parameters
#define WINDOW_CLASSNAME PROGRAM_NAME L" Games WndClass"
#define WINDOW_DEFAULT_WIDTH 800
#define WINDOW_DEFAULT_HEIGHT 600
#define WINDOW_MIN_WIDTH 480
#define WINDOW_MIN_HEIGHT 240
// local definitions
#define WINDOW_TEXT_SELECTGAME 1
#define WINDOW_LIST_GAMES 2
#define WINDOW_BUTTON_OK 3
#define WINDOW_BUTTON_CANCEL 4
#define WINDOW_TEXT_STATUSBAR 5
// list view column definition
typedef struct listviewcolumn_s
{
int width;
int alignment;
bool sort_descending;
wchar_t *text;
HWND hToolTipWnd;
} listviewcolumn_t;
// global variables used in this module only
static bool is_classregistered = false;
static listviewcolumn_t listviewcolumns[] =
{
{ 130, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"Games_Event")*/, NULL }, // text address needs to be set at runtime, because it's mallocated
{ 127, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"Games_Site")*/, NULL },
{ 70, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"Games_Date")*/, NULL },
{ 150, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"Games_White")*/, NULL },
{ 150, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"Games_Black")*/, NULL },
{ 60, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"Games_Result")*/, NULL },
{ 40, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"Games_ECO")*/, NULL },
};
static pgngame_t *chosen_game = NULL;
//static HWND hThisWnd = NULL;
#define hThisWnd hGamesWnd // shared variable
// prototypes of local functions
static LRESULT CALLBACK WindowProc_ThisWindow (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
static int CALLBACK CompareProc_ListGames (LPARAM lParam1, LPARAM lParam2, LPARAM column);
static int WINAPI ListView_WndProc (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
void Window_Games (void)
{
// helper function to fire up the child window
WNDCLASSEX wc;
// is the window we want to display already displayed ?
if (IsWindow (hThisWnd))
SetForegroundWindow (hThisWnd); // if so, just bring it to front
// else the way is clear
else
{
chosen_game = NULL; // no game is chosen until told otherwise
// is the window class NOT registered yet ?
if (!is_classregistered)
{
// if so, register the window class once and for all
memset (&wc, 0, sizeof (wc));
wc.cbSize = sizeof (wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc_ThisWindow;
wc.hInstance = hAppInstance;
wc.hIcon = LoadIcon (hAppInstance, (wchar_t *) ICON_MAIN);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = GetSysColorBrush (COLOR_3DFACE);
wc.lpszClassName = WINDOW_CLASSNAME;
RegisterClassEx (&wc);
is_classregistered = true; // remember this window class is registered
}
// create the child window
hThisWnd = CreateWindowEx (WS_EX_CLIENTEDGE, WINDOW_CLASSNAME, LOCALIZE (L"Games_Title"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_DEFAULT_WIDTH, WINDOW_DEFAULT_HEIGHT,
hMainWnd, NULL, hAppInstance, NULL);
}
return; // return as soon as the thread is fired up
}
void Window_Games_Validated (void)
{
// callback function called by the main game thread when the window is validated
// remember this callback is no longer to be called
is_window_games_validated = false;
// load the requested PGN game
if (PGNFile_LoadGame (&the_board, load_pathname, chosen_game))
{
animation_endtime = current_time + ANIMATION_DURATION; // start with an animation
sound_playtime = current_time + ANIMATION_DURATION - 0.1f; // play sound near the end of animation
the_board.reevaluate = true; // evaluate the new board
the_scene.update = true; // update scene
}
else
{
messagebox.hWndParent = hMainWnd;
wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), LOCALIZE (L"ImportantMessage"));
wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_LoadFailed"));
messagebox.flags = MB_ICONWARNING | MB_OK;
DialogBox_Message (&messagebox); // on error, display a modeless error message box
}
return; // finished, PGN game is loaded
}
static LRESULT CALLBACK WindowProc_ThisWindow (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
{
// message handler for the child window
unsigned short wParam_hiword;
unsigned short wParam_loword;
pgngame_t *game;
HWND hListWnd;
LVCOLUMN lvc;
LVITEM lvi;
HDITEM hdi;
NMLISTVIEW *lv;
MINMAXINFO *minmax;
RECT client_rect;
int column_index;
int insert_index;
int game_index;
int item_index;
// filter out the commonly used message values
wParam_hiword = HIWORD (wParam);
wParam_loword = LOWORD (wParam);
// have we just fired up this window ?
if (message == WM_CREATE)
{
// center the window
CenterWindow (hWnd, hMainWnd);
// populate the window
CreateWindowEx (0, L"static", L"",
WS_CHILD | WS_VISIBLE,
0, 0, 0, 0, hWnd, (HMENU) WINDOW_TEXT_SELECTGAME, hAppInstance, NULL);
CreateWindowEx (WS_EX_STATICEDGE, L"syslistview32", L"",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT,
0, 0, 0, 0, hWnd, (HMENU) WINDOW_LIST_GAMES, hAppInstance, NULL);
CreateWindowEx (0, L"button", L"",
WS_CHILD | WS_VISIBLE,
0, 0, 0, 0, hWnd, (HMENU) WINDOW_BUTTON_OK, hAppInstance, NULL);
CreateWindowEx (0, L"button", L"",
WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
0, 0, 0, 0, hWnd, (HMENU) WINDOW_BUTTON_CANCEL, hAppInstance, NULL);
CreateWindowEx (WS_EX_RIGHT, L"static", L"",
WS_CHILD | WS_VISIBLE,
0, 0, 0, 0, hWnd, (HMENU) WINDOW_TEXT_STATUSBAR, hAppInstance, NULL);
// associate the default GUI font with the leading caption and set its text
SendMessage (GetDlgItem (hWnd, WINDOW_TEXT_SELECTGAME), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
Static_SetText (GetDlgItem (hWnd, WINDOW_TEXT_SELECTGAME), LOCALIZE (L"Games_SelectGame"));
// associate the default GUI font with the OK button and set its text
SendMessage (GetDlgItem (hWnd, WINDOW_BUTTON_OK), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
SetWindowText (GetDlgItem (hWnd, WINDOW_BUTTON_OK), LOCALIZE (L"Button_OK"));
// associate the default GUI font with the cancel button and set its text
SendMessage (GetDlgItem (hWnd, WINDOW_BUTTON_CANCEL), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
SetWindowText (GetDlgItem (hWnd, WINDOW_BUTTON_CANCEL), LOCALIZE (L"Button_Cancel"));
// associate the default GUI font with the status bar and set its text
SendMessage (GetDlgItem (hWnd, WINDOW_TEXT_STATUSBAR), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
Static_SetText (GetDlgItem (hWnd, WINDOW_TEXT_STATUSBAR), LOCALIZE (L"Games_StatusBar"));
// get a quick access to the list control
hListWnd = GetDlgItem (hWnd, WINDOW_LIST_GAMES);
// add full row select and header columns rearranging to it
ListView_SetExtendedListViewStyle (hListWnd, ListView_GetExtendedListViewStyle (hListWnd)
| LVS_EX_LABELTIP | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
// subclass the list view procedure to handle header tooltips and save the old procedure as one of the window's property
SetProp (hListWnd, L"BaseWndProc", (HANDLE) SetWindowLongPtr (hListWnd, GWL_WNDPROC, (long) ListView_WndProc));
// create the list control columns
// tell Windows which members of the LVCOLUMN structure we're filling
memset (&lvc, 0, sizeof (lvc));
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for (column_index = 0; column_index < sizeof (listviewcolumns) / sizeof (listviewcolumn_t); column_index++)
{
lvc.iSubItem = column_index;
if (column_index == 0) lvc.pszText = LOCALIZE (L"Games_Event");
else if (column_index == 1) lvc.pszText = LOCALIZE (L"Games_Site");
else if (column_index == 2) lvc.pszText = LOCALIZE (L"Games_Date");
else if (column_index == 3) lvc.pszText = LOCALIZE (L"Games_White");
else if (column_index == 4) lvc.pszText = LOCALIZE (L"Games_Black");
else if (column_index == 5) lvc.pszText = LOCALIZE (L"Games_Result");
else if (column_index == 6) lvc.pszText = LOCALIZE (L"Games_ECO");
lvc.cx = listviewcolumns[column_index].width;
lvc.fmt = listviewcolumns[column_index].alignment;
ListView_InsertColumn (hListWnd, column_index, &lvc); // add each column to list view
}
// populate the list control
ListView_SetItemCount (hListWnd, game_count);
// tell Windows which members of the LVCOLUMN structure we're filling
memset (&lvi, 0, sizeof (lvi));
lvi.mask = LVIF_PARAM; // we want to set the item's pointer
for (game_index = 0; game_index < game_count; game_index++)
{
game = &games[game_index]; // quick access to PGN game
lvi.iItem = game_index;
lvi.lParam = (LPARAM) game;
insert_index = ListView_InsertItem (hListWnd, &lvi); // add each item to list view
// set item's substrings
ListView_SetItemText (hListWnd, insert_index, 0, game->event_str);
ListView_SetItemText (hListWnd, insert_index, 1, game->site_str);
ListView_SetItemText (hListWnd, insert_index, 2, game->date_str);
ListView_SetItemText (hListWnd, insert_index, 3, game->white_str);
ListView_SetItemText (hListWnd, insert_index, 4, game->black_str);
ListView_SetItemText (hListWnd, insert_index, 5, game->result_str);
ListView_SetItemText (hListWnd, insert_index, 6, game->eco_str);
}
// convert the status bar message to a hyperlink
ConvertStaticToHyperlink (GetDlgItem (hWnd, WINDOW_TEXT_STATUSBAR));
// now show the window
ShowWindow (hWnd, SW_SHOW);
}
// else did we click the close button on the title bar ?
else if (message == WM_CLOSE)
{
chosen_game = NULL; // we chose no game
DestroyWindow (hWnd); // close the window
}
// else are we destroying this window ?
else if (message == WM_DESTROY)
{
hThisWnd = NULL; // window is closed
SetForegroundWindow (hMainWnd); // restore focus on the main window
is_window_games_validated = true; // return a "load PGN game" value (cancel value will be implied if chosen_game is NULL)
}
// else are we resizing the window ?
else if (message == WM_SIZE)
{
// get the new window size
GetClientRect (hWnd, &client_rect);
SetWindowPos (GetDlgItem (hWnd, WINDOW_TEXT_SELECTGAME), NULL, 16, 16, client_rect.right - 32, 32, SWP_NOZORDER);
SetWindowPos (GetDlgItem (hWnd, WINDOW_LIST_GAMES), NULL, 16, 48, client_rect.right - 32, client_rect.bottom - 104, SWP_NOZORDER);
SetWindowPos (GetDlgItem (hWnd, WINDOW_BUTTON_OK), NULL, client_rect.right / 2 - 104, client_rect.bottom - 48, 96, 32, SWP_NOZORDER);
SetWindowPos (GetDlgItem (hWnd, WINDOW_BUTTON_CANCEL), NULL, client_rect.right / 2 + 8, client_rect.bottom - 48, 96, 32, SWP_NOZORDER);
SetWindowPos (GetDlgItem (hWnd, WINDOW_TEXT_STATUSBAR), NULL, 0, client_rect.bottom - 16, client_rect.right, 16, SWP_NOZORDER);
}
// else are we asking how big/small we can resize ?
else if (message == WM_GETMINMAXINFO)
{
minmax = (MINMAXINFO *) lParam; // get a pointer to the min/max info structure
minmax->ptMinTrackSize.x = WINDOW_MIN_WIDTH;
minmax->ptMinTrackSize.y = WINDOW_MIN_HEIGHT;
}
// is it a list view message ?
else if ((message == WM_NOTIFY) && (wParam == WINDOW_LIST_GAMES))
{
lv = (NMLISTVIEW *) lParam; // quick access to list view
// is it a click on one of the headers' columns ?
if (lv->hdr.code == LVN_COLUMNCLICK)
{
// cycle through all columns and reset their sort order
for (column_index = 0; column_index < sizeof (listviewcolumns) / sizeof (listviewcolumn_t); column_index++)
{
memset (&hdi, 0, sizeof (hdi));
hdi.mask = HDI_FORMAT;
Header_GetItem (ListView_GetHeader (lv->hdr.hwndFrom), column_index, &hdi); // get the column's sort arrow state
hdi.fmt &= ~(HDF_SORTDOWN | HDF_SORTUP);
if (column_index == lv->iSubItem)
{
listviewcolumns[column_index].sort_descending ^= true; // revert the sort order for the clicked column
hdi.fmt |= (listviewcolumns[column_index].sort_descending ? HDF_SORTDOWN : HDF_SORTUP);
}
else
listviewcolumns[column_index].sort_descending = false; // reset the sort order for all the other ones
Header_SetItem (ListView_GetHeader (lv->hdr.hwndFrom), column_index, &hdi); // update the column's sort arrow state
}
// now sort the list view according to the column we selected and its current sort order
ListView_SortItems (lv->hdr.hwndFrom, CompareProc_ListGames, lv->iSubItem);
}
// else is it a single click on one of the elements ?
else if (lv->hdr.code == NM_CLICK)
EnableWindow (GetDlgItem (hWnd, WINDOW_BUTTON_OK), true); // enable the OK button back
// else is it a double-click on one of the elements ?
else if (lv->hdr.code == NM_DBLCLK)
PostMessage (hWnd, WM_COMMAND, WINDOW_BUTTON_OK, NULL); // if so, act as if we clicked the OK button
}
// else did we take action on one of the controls ?
else if (message == WM_COMMAND)
{
// was it the new online game button ?
if (wParam_loword == WINDOW_BUTTON_OK)
{
chosen_game = NULL; // we chose no game until told otherwise
hListWnd = GetDlgItem (hWnd, WINDOW_LIST_GAMES); // quick access to the list control
item_index = ListView_GetNextItem (hListWnd, -1, LVNI_FOCUSED | LVNI_SELECTED); // get the first selected item
if (item_index > -1)
{
memset (&lvi, 0, sizeof (lvi));
lvi.mask = LVIF_PARAM; // we want to set the item's pointer
lvi.iItem = item_index;
if (ListView_GetItem (hListWnd, &lvi))
chosen_game = (pgngame_t *) lvi.lParam; // retrieve a pointer to the PGN game from the item
}
DestroyWindow (hWnd); // close the window
}
// else was it the cancel button ?
else if (wParam_loword == WINDOW_BUTTON_CANCEL)
{
chosen_game = NULL; // we chose no game
DestroyWindow (hWnd); // close the window
}
// else was it the status bar hyperlink ?
else if (wParam_loword == STATICTEXT_NEWGAME_STATUSBAR)
ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
}
// call the default window message processing function to keep things going
return (DefWindowProc (hWnd, message, wParam, lParam));
}
static int CALLBACK CompareProc_ListGames (LPARAM lParam1, LPARAM lParam2, LPARAM column)
{
// callback function that tells whether the lParam1 listview element comes before lParam2 in the
// sort order of the specified column
wchar_t *string_to_compare1;
wchar_t *string_to_compare2;
// retrieve strings to compare according to the column we want
if (column == 0)
{
string_to_compare1 = ((pgngame_t *) lParam1)->event_str; // compare events
string_to_compare2 = ((pgngame_t *) lParam2)->event_str;
}
else if (column == 1)
{
string_to_compare1 = ((pgngame_t *) lParam1)->site_str; // compare sites
string_to_compare2 = ((pgngame_t *) lParam2)->site_str;
}
else if (column == 2)
{
string_to_compare1 = ((pgngame_t *) lParam1)->date_str; // compare dates
string_to_compare2 = ((pgngame_t *) lParam2)->date_str;
}
else if (column == 3)
{
string_to_compare1 = ((pgngame_t *) lParam1)->white_str; // compare whites
string_to_compare2 = ((pgngame_t *) lParam2)->white_str;
}
else if (column == 4)
{
string_to_compare1 = ((pgngame_t *) lParam1)->black_str; // compare blacks
string_to_compare2 = ((pgngame_t *) lParam2)->black_str;
}
else if (column == 5)
{
string_to_compare1 = ((pgngame_t *) lParam1)->result_str; // compare results
string_to_compare2 = ((pgngame_t *) lParam2)->result_str;
}
else if (column == 6)
{
string_to_compare1 = ((pgngame_t *) lParam1)->eco_str; // compare ECO
string_to_compare2 = ((pgngame_t *) lParam2)->eco_str;
}
// which order do we want this column to be sorted ?
if (listviewcolumns[column].sort_descending)
return (wcscmp (string_to_compare1, string_to_compare2)); // normal order
else
return (-wcscmp (string_to_compare1, string_to_compare2)); // reverse order
}
static int WINAPI ListView_WndProc (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
{
// callback that subclasses the original ListView window procedure, so that we can hook
// some of its messages
static bool tooltips_initialized = false;
static bool update_tooltips = false;
WNDPROC BaseWndProc;
TOOLINFO toolinfo;
HWND hHeaderWnd;
int column_index;
// get a pointer to the base window procedure (it was stored as a window property)
BaseWndProc = (WNDPROC) GetProp (hWnd, L"BaseWndProc");
if (BaseWndProc == NULL)
return (DefWindowProc (hWnd, message, wParam, lParam)); // consistency check
// is the mouse moving around ?
if (message == WM_MOUSEMOVE)
{
// do the tooltips need to be created ?
if (!tooltips_initialized)
{
hHeaderWnd = (HWND) SendMessage (hWnd, LVM_GETHEADER, 0, 0); // get listview header
// add a tooltip for each column
for (column_index = 0; column_index < sizeof (listviewcolumns) / sizeof (listviewcolumn_t); column_index++)
{
// create the tooltip and set its window topmost
listviewcolumns[column_index].hToolTipWnd = CreateWindowEx (WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hHeaderWnd, NULL, hAppInstance, NULL);
// associate the tooltip with the tool
memset (&toolinfo, 0, sizeof (toolinfo));
toolinfo.cbSize = sizeof (toolinfo);
toolinfo.uFlags = TTF_SUBCLASS;
toolinfo.hwnd = hHeaderWnd; // this tooltip works on the list header window handle
toolinfo.uId = column_index; // tooltip ID will be column ID
toolinfo.hinst = hAppInstance;
toolinfo.lpszText = listviewcolumns[column_index].text; // tooltip text
Header_GetItemRect (hHeaderWnd, column_index, &toolinfo.rect); // get header's item rectangle
SendMessage (listviewcolumns[column_index].hToolTipWnd, TTM_ADDTOOL, 0, (LPARAM) &toolinfo);
}
tooltips_initialized = true; // do this only once
}
// else do the tooltips need to be updated ?
else if (update_tooltips)
{
hHeaderWnd = (HWND) SendMessage (hWnd, LVM_GETHEADER, 0, 0); // get listview header
// cycle through all columns
for (column_index = 0; column_index < sizeof (listviewcolumns) / sizeof (listviewcolumn_t); column_index++)
{
// update the tooltip rectangle
memset (&toolinfo, 0, sizeof (toolinfo));
toolinfo.cbSize = sizeof (toolinfo);
toolinfo.hwnd = hHeaderWnd; // this tooltip works on the list header window handle
toolinfo.uId = column_index; // tooltip ID is column ID
Header_GetItemRect (hHeaderWnd, column_index, &toolinfo.rect); // get header's item rectangle
SendMessage (listviewcolumns[column_index].hToolTipWnd, TTM_NEWTOOLRECT, 0, (LPARAM) &toolinfo);
}
update_tooltips = false; // do this only once
}
}
// else has the user finished dragging/resizing a column header ?
else if ((message == WM_NOTIFY) && ((((NMHDR *) lParam)->code == HDN_ENDTRACK) || (((NMHDR *) lParam)->code == HDN_ENDDRAG)))
update_tooltips = true; // if so, remember to update tooltips on the next mouse move
// in any case, forward all messages to the original ListView hook procedure
return (CallWindowProc (BaseWndProc, hWnd, message, wParam, lParam));
}