// window_opponents.cpp
 
 
 
#include "../common.h"
 
 
 
 
 
// window parameters
 
#define WINDOW_CLASSNAME PROGRAM_NAME L" Opponents WndClass"
 
#define WINDOW_DEFAULT_WIDTH 822
 
#define WINDOW_DEFAULT_HEIGHT 600
 
#define WINDOW_MIN_WIDTH 822
 
#define WINDOW_MIN_HEIGHT 200
 
 
 
 
 
// local definitions
 
#define WINDOW_TEXT_DOUBLECLICKORNARROWDOWN 1
 
#define WINDOW_TEXT_NAMECONTAINS 2
 
#define WINDOW_EDITBOX_NAMECONTAINS 3
 
#define WINDOW_TEXT_STATUSIS 4
 
#define WINDOW_COMBOBOX_STATUSIS 5
 
#define WINDOW_TEXT_LEVELFROM 6
 
#define WINDOW_EDITBOX_LEVELFROM 7
 
#define WINDOW_TEXT_LEVELTO 8
 
#define WINDOW_EDITBOX_LEVELTO 9
 
#define WINDOW_LIST_OPPONENTS 10
 
#define WINDOW_TEXT_STATUSBAR 11
 
#define WINDOW_TIMER_REFRESH 1
 
#define TICKED_COLUMN_WIDTH 41
 
#define STRING_FIDEGM ((wchar_t *) "G\x00M\x00 \x00\x42\x26\x00") // "GM " + male sign
 
#define STRING_FIDEIM ((wchar_t *) "I\x00M\x00 \x00\x42\x26\x00") // "IM " + male sign
 
#define STRING_FIDEWGM ((wchar_t *) "G\x00M\x00 \x00\x40\x26\x00") // "GM " + female sign
 
#define STRING_FIDEWIM ((wchar_t *) "I\x00M\x00 \x00\x40\x26\x00") // "IM " + female sign
 
 
 
 
 
// 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 int listviewicons[sizeof (handlestatus) / sizeof (handlestatus_t)]; // as big as the handlestatus global array
 
static listviewcolumn_t listviewcolumns[] =
 
{
 
   { 140, LVCFMT_LEFT, true, NULL /*LOCALIZE (L"Opponents_ColumnNickname")*/, NULL }, // text address needs to be set at runtime, because it's mallocated
 
   { 60, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"Opponents_ColumnFIDERank")*/, NULL },
 
   { 120, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"Opponents_ColumnStatus")*/, NULL },
 
   { 60, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"Opponents_ColumnRating")*/, NULL },
 
   { TICKED_COLUMN_WIDTH, LVCFMT_CENTER, false, NULL /*LOCALIZE (L"Opponents_ColumnComputer")*/, NULL },
 
   { TICKED_COLUMN_WIDTH, LVCFMT_CENTER, false, NULL /*LOCALIZE (L"Opponents_ColumnAdministrator")*/, NULL },
 
   { TICKED_COLUMN_WIDTH, LVCFMT_CENTER, false, NULL /*LOCALIZE (L"Opponents_ColumnBlindfold")*/, NULL },
 
   { TICKED_COLUMN_WIDTH, LVCFMT_CENTER, false, NULL /*LOCALIZE (L"Opponents_ColumnTeam")*/, NULL },
 
   { TICKED_COLUMN_WIDTH, LVCFMT_CENTER, false, NULL /*LOCALIZE (L"Opponents_ColumnChessAdvisor")*/, NULL },
 
   { TICKED_COLUMN_WIDTH, LVCFMT_CENTER, false, NULL /*LOCALIZE (L"Opponents_ColumnServiceRepresentative")*/, NULL },
 
   { TICKED_COLUMN_WIDTH, LVCFMT_CENTER, false, NULL /*LOCALIZE (L"Opponents_ColumnTournamentDirector")*/, NULL },
 
   { TICKED_COLUMN_WIDTH, LVCFMT_CENTER, false, NULL /*LOCALIZE (L"Opponents_ColumnMamerManager")*/, NULL },
 
   { TICKED_COLUMN_WIDTH, LVCFMT_CENTER, false, NULL /*LOCALIZE (L"Opponents_ColumnUnregistered")*/, NULL },
 
};
 
static int current_sortcolumn = 0;
 
static wchar_t window_title[256];
 
static wchar_t opponent_namecontains[64];
 
static wchar_t rating_string[64];
 
//static HWND hThisWnd = NULL;
 
#define hThisWnd hOpponentsWnd // shared variable
 
 
 
 
 
// prototypes of local functions
 
static LRESULT CALLBACK WindowProc_ThisWindow (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
 
static int CALLBACK CompareProc_ListOpponents (LPARAM lParam1, LPARAM lParam2, LPARAM column);
 
static int WINAPI ListView_WndProc (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
 
 
 
 
 
void Window_Opponents (void)
 
{
 
   // helper function to fire up the child window
 
 
 
   WNDCLASSEX wc;
 
   player_t *network_player;
 
 
 
   // 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
 
   {
 
      // find the network player and make him ask an update from the server
 
      if ((network_player = Player_FindByType (PLAYER_INTERNET)) != NULL)
 
      {
 
         SAFE_free ((void **) &onlineplayers); // free the online players list we know
 
         onlineplayer_count = -1; // and reset its count (-1 means "reply not arrived")
 
         Player_SendBuffer_Add (network_player, 1000, L"who\n"); // send the players update request
 
      }
 
 
 
      // 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"Opponents_TitleBuildingList"), 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_Opponents_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_opponents_validated = false;
 
 
 
   return; // finished
 
}
 
 
 
 
 
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;
 
   int opponent_statusis;
 
   int opponent_ratingfrom;
 
   int opponent_ratingto;
 
   int result;
 
   HWND hListWnd;
 
   LVCOLUMN lvc;
 
   LVITEM lvi;
 
   HDITEM hdi;
 
   NMLISTVIEW *lv;
 
   NMITEMACTIVATE *clickeditem;
 
   HIMAGELIST imagelist;
 
   MINMAXINFO *minmax;
 
   RECT client_rect;
 
   onlineplayer_t *onlineplayer;
 
   player_t *local_player;
 
   int onlineplayer_index;
 
   int column_index;
 
   int insert_index;
 
   int displayed_count;
 
 
 
   // 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_DOUBLECLICKORNARROWDOWN, hAppInstance, NULL);
 
      CreateWindowEx (WS_EX_RIGHT, L"static", L"",
 
                      WS_CHILD | WS_VISIBLE,
 
                      0, 0, 0, 0, hWnd, (HMENU) WINDOW_TEXT_NAMECONTAINS, hAppInstance, NULL);
 
      CreateWindowEx (WS_EX_CLIENTEDGE, L"edit", L"", 
 
                      WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL, 
 
                      0, 0, 0, 0, hWnd, (HMENU) WINDOW_EDITBOX_NAMECONTAINS, hAppInstance, NULL);
 
      CreateWindowEx (WS_EX_RIGHT, L"static", L"",
 
                      WS_CHILD | WS_VISIBLE,
 
                      0, 0, 0, 0, hWnd, (HMENU) WINDOW_TEXT_STATUSIS, hAppInstance, NULL);
 
      CreateWindowEx (WS_EX_CLIENTEDGE, L"combobox", L"", 
 
                      WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST, 
 
                      0, 0, 0, 0, hWnd, (HMENU) WINDOW_COMBOBOX_STATUSIS, hAppInstance, NULL);
 
      CreateWindowEx (WS_EX_RIGHT, L"static", L"",
 
                      WS_CHILD | WS_VISIBLE,
 
                      0, 0, 0, 0, hWnd, (HMENU) WINDOW_TEXT_LEVELFROM, hAppInstance, NULL);
 
      CreateWindowEx (WS_EX_CLIENTEDGE, L"edit", L"", 
 
                      WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | SS_CENTERIMAGE, 
 
                      0, 0, 0, 0, hWnd, (HMENU) WINDOW_EDITBOX_LEVELFROM, hAppInstance, NULL);
 
      CreateWindowEx (WS_EX_RIGHT, L"static", L"",
 
                      WS_CHILD | WS_VISIBLE,
 
                      0, 0, 0, 0, hWnd, (HMENU) WINDOW_TEXT_LEVELTO, hAppInstance, NULL);
 
      CreateWindowEx (WS_EX_CLIENTEDGE, L"edit", L"", 
 
                      WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL | SS_CENTERIMAGE, 
 
                      0, 0, 0, 0, hWnd, (HMENU) WINDOW_EDITBOX_LEVELTO, 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_OPPONENTS, hAppInstance, NULL);
 
      CreateWindowEx (WS_EX_RIGHT, L"static", L"",
 
                      WS_CHILD | WS_VISIBLE,
 
                      0, 0, 0, 0, hWnd, (HMENU) WINDOW_TEXT_STATUSBAR, hAppInstance, NULL);
 
 
 
      // prepare the list view : do it before anything that could trigger a fill
 
 
 
      // get a quick access to the list control
 
      hListWnd = GetDlgItem (hWnd, WINDOW_LIST_OPPONENTS);
 
 
 
      // add full row select and header columns rearranging to it
 
      ListView_SetExtendedListViewStyle (hListWnd, ListView_GetExtendedListViewStyle (hListWnd)
 
                                                   | LVS_EX_GRIDLINES | 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));
 
 
 
      // 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"Opponents_ColumnNickname");
 
         else if (column_index == 1) lvc.pszText = LOCALIZE (L"Opponents_ColumnFIDERank");
 
         else if (column_index == 2) lvc.pszText = LOCALIZE (L"Opponents_ColumnStatus");
 
         else if (column_index == 3) lvc.pszText = LOCALIZE (L"Opponents_ColumnRating");
 
         else if (column_index == 4) lvc.pszText = LOCALIZE (L"Opponents_ColumnComputer");
 
         else if (column_index == 5) lvc.pszText = LOCALIZE (L"Opponents_ColumnAdministrator");
 
         else if (column_index == 6) lvc.pszText = LOCALIZE (L"Opponents_ColumnBlindfold");
 
         else if (column_index == 7) lvc.pszText = LOCALIZE (L"Opponents_ColumnTeam");
 
         else if (column_index == 8) lvc.pszText = LOCALIZE (L"Opponents_ColumnChessAdvisor");
 
         else if (column_index == 9) lvc.pszText = LOCALIZE (L"Opponents_ColumnServiceRepresentative");
 
         else if (column_index == 10) lvc.pszText = LOCALIZE (L"Opponents_ColumnTournamentDirector");
 
         else if (column_index == 11) lvc.pszText = LOCALIZE (L"Opponents_ColumnMamerManager");
 
         else if (column_index == 12) lvc.pszText = LOCALIZE (L"Opponents_ColumnUnregistered");
 
         lvc.cx = listviewcolumns[column_index].width;
 
         lvc.fmt = listviewcolumns[column_index].alignment;
 
         ListView_InsertColumn (hListWnd, column_index, &lvc); // add each column to list view
 
      }
 
 
 
      // create the listview image list
 
      imagelist = ImageList_Create (16, 16, ILC_COLOR32, sizeof (handlestatus) / sizeof (handlestatus_t), 1); // create an imagelist holding N 32-bit images
 
      for (insert_index = 1; insert_index < sizeof (handlestatus) / sizeof (handlestatus_t); insert_index++)
 
         listviewicons[insert_index] = ImageList_AddIcon (imagelist, handlestatus[insert_index].icon); // add our icons in the image list
 
      ListView_SetImageList (hListWnd, imagelist, LVSIL_SMALL); // associate it with the listview
 
 
 
      // associate the default GUI font with the "double-click or narrow down" message and set its text
 
      SendMessage (GetDlgItem (hWnd, WINDOW_TEXT_DOUBLECLICKORNARROWDOWN), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
 
      SetWindowText (GetDlgItem (hWnd, WINDOW_TEXT_DOUBLECLICKORNARROWDOWN), LOCALIZE (L"Opponents_DoubleClickOrNarrowDown"));
 
 
 
      // associate the default GUI font with the "name contains" message and set its text
 
      SendMessage (GetDlgItem (hWnd, WINDOW_TEXT_NAMECONTAINS), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
 
      SetWindowText (GetDlgItem (hWnd, WINDOW_TEXT_NAMECONTAINS), LOCALIZE (L"Opponents_NameContains"));
 
 
 
      // associate the default GUI font with the "name contains" edit box and reset it
 
      SendMessage (GetDlgItem (hWnd, WINDOW_EDITBOX_NAMECONTAINS), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
 
      SetDlgItemText (hWnd, WINDOW_EDITBOX_NAMECONTAINS, L"");
 
 
 
      // associate the default GUI font with the "status is" message and set its text
 
      SendMessage (GetDlgItem (hWnd, WINDOW_TEXT_STATUSIS), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
 
      SetWindowText (GetDlgItem (hWnd, WINDOW_TEXT_STATUSIS), LOCALIZE (L"Opponents_StatusIs"));
 
 
 
      // associate the default GUI font with the "status is" combo box and populate the combo box
 
      SendMessage (GetDlgItem (hWnd, WINDOW_COMBOBOX_STATUSIS), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
 
      ComboBox_AddString (GetDlgItem (hWnd, WINDOW_COMBOBOX_STATUSIS), L"");
 
      ComboBox_AddString (GetDlgItem (hWnd, WINDOW_COMBOBOX_STATUSIS), LOCALIZE (L"Opponents_StatusInGame"));
 
      ComboBox_AddString (GetDlgItem (hWnd, WINDOW_COMBOBOX_STATUSIS), LOCALIZE (L"Opponents_StatusInSimulation"));
 
      ComboBox_AddString (GetDlgItem (hWnd, WINDOW_COMBOBOX_STATUSIS), LOCALIZE (L"Opponents_StatusInTournament"));
 
      ComboBox_AddString (GetDlgItem (hWnd, WINDOW_COMBOBOX_STATUSIS), LOCALIZE (L"Opponents_StatusExaminingAGame"));
 
      ComboBox_AddString (GetDlgItem (hWnd, WINDOW_COMBOBOX_STATUSIS), LOCALIZE (L"Opponents_StatusNotOpenForAMatch"));
 
      ComboBox_AddString (GetDlgItem (hWnd, WINDOW_COMBOBOX_STATUSIS), LOCALIZE (L"Opponents_StatusInactiveOrBusy"));
 
      ComboBox_AddString (GetDlgItem (hWnd, WINDOW_COMBOBOX_STATUSIS), LOCALIZE (L"Opponents_StatusAvailable"));
 
 
 
      // associate the default GUI font with the "level from" message and set its text
 
      SendMessage (GetDlgItem (hWnd, WINDOW_TEXT_LEVELFROM), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
 
      SetWindowText (GetDlgItem (hWnd, WINDOW_TEXT_LEVELFROM), LOCALIZE (L"Opponents_LevelFrom"));
 
 
 
      // associate the default GUI font with the "level from" edit box and reset it
 
      SendMessage (GetDlgItem (hWnd, WINDOW_EDITBOX_LEVELFROM), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
 
      SetDlgItemText (hWnd, WINDOW_EDITBOX_LEVELFROM, L"");
 
 
 
      // associate the default GUI font with the "level to" message and set its text
 
      SendMessage (GetDlgItem (hWnd, WINDOW_TEXT_LEVELTO), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
 
      SetWindowText (GetDlgItem (hWnd, WINDOW_TEXT_LEVELTO), LOCALIZE (L"Opponents_LevelTo"));
 
 
 
      // associate the default GUI font with the "level to" edit box and reset it
 
      SendMessage (GetDlgItem (hWnd, WINDOW_EDITBOX_LEVELTO), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
 
      SetDlgItemText (hWnd, WINDOW_EDITBOX_LEVELTO, L"");
 
 
 
      // 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);
 
      SetWindowText (GetDlgItem (hWnd, WINDOW_TEXT_STATUSBAR), LOCALIZE (L"Opponents_StatusBar"));
 
 
 
      // refresh the window every second
 
      SetTimer (hWnd, WINDOW_TIMER_REFRESH, 1000, NULL);
 
 
 
      // convert the status bar message to a hyperlink
 
      ConvertStaticToHyperlink (GetDlgItem (hWnd, WINDOW_TEXT_STATUSBAR));
 
 
 
      // now show the window
 
      ShowWindow (hWnd, SW_SHOW);
 
 
 
      // and send focus to the player name filter field (note: GetDlgItem works ALSO with windows! MSDN certified :))
 
      if (GetForegroundWindow () == hWnd)
 
         SetFocus (GetDlgItem (hWnd, WINDOW_EDITBOX_NAMECONTAINS));
 
 
 
      return (0); // as MSDN says
 
   }
 
 
 
   // else did we click the close button on the title bar ?
 
   else if (message == WM_CLOSE)
 
   {
 
      DestroyWindow (hWnd); // close the window
 
      return (0); // as MSDN says
 
   }
 
 
 
   // else are we destroying this window ?
 
   else if (message == WM_DESTROY)
 
   {
 
      KillTimer (hWnd, WINDOW_TIMER_REFRESH); // destroy the timer we used to refresh the window
 
      hThisWnd = NULL; // window is closed
 
      SetForegroundWindow (hMainWnd); // restore focus on the main window
 
      is_window_opponents_validated = true; // remember we closed this window
 
      the_board.reevaluate = true; // refresh the GUI buttons if needed
 
      return (0); // as MSDN says
 
   }
 
 
 
   // 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_DOUBLECLICKORNARROWDOWN), NULL, 16, 16, client_rect.right - 32, 16, SWP_NOZORDER);
 
      SetWindowPos (GetDlgItem (hWnd, WINDOW_TEXT_NAMECONTAINS), NULL, 16, 40, 136, 16, SWP_NOZORDER);
 
      SetWindowPos (GetDlgItem (hWnd, WINDOW_EDITBOX_NAMECONTAINS), NULL, 160, 38, 128, 20, SWP_NOZORDER);
 
      SetWindowPos (GetDlgItem (hWnd, WINDOW_TEXT_STATUSIS), NULL, 288, 40, 88, 16, SWP_NOZORDER);
 
      SetWindowPos (GetDlgItem (hWnd, WINDOW_COMBOBOX_STATUSIS), NULL, 384, 38, 128, 20, SWP_NOZORDER);
 
      SetWindowPos (GetDlgItem (hWnd, WINDOW_TEXT_LEVELFROM), NULL, 512, 40, 120, 16, SWP_NOZORDER);
 
      SetWindowPos (GetDlgItem (hWnd, WINDOW_EDITBOX_LEVELFROM), NULL, 640, 38, 48, 20, SWP_NOZORDER);
 
      SetWindowPos (GetDlgItem (hWnd, WINDOW_TEXT_LEVELTO), NULL, 688, 40, 40, 16, SWP_NOZORDER);
 
      SetWindowPos (GetDlgItem (hWnd, WINDOW_EDITBOX_LEVELTO), NULL, 736, 38, 48, 20, SWP_NOZORDER);
 
      SetWindowPos (GetDlgItem (hWnd, WINDOW_LIST_OPPONENTS), NULL, 16, 64, client_rect.right - 32, client_rect.bottom - 80, SWP_NOZORDER);
 
      SetWindowPos (GetDlgItem (hWnd, WINDOW_TEXT_STATUSBAR), NULL, 0, client_rect.bottom - 16, client_rect.right, 16, SWP_NOZORDER);
 
 
 
      return (0); // as MSDN says
 
   }
 
 
 
   // 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;
 
 
 
      return (0); // as MSDN says
 
   }
 
 
 
   // else is it a timer event AND is it our refresh timer ?
 
   else if ((message == WM_TIMER) && (wParam == WINDOW_TIMER_REFRESH))
 
   {
 
      // do we need to update the opponents list ?
 
      if (onlineplayers_updated)
 
      {
 
         hListWnd = GetDlgItem (hWnd, WINDOW_LIST_OPPONENTS); // get a quick access to the list control
 
 
 
         // grab the different filters
 
         GetDlgItemText (hWnd, WINDOW_EDITBOX_NAMECONTAINS, opponent_namecontains, WCHAR_SIZEOF (opponent_namecontains));
 
         opponent_statusis = ComboBox_GetCurSel (GetDlgItem (hWnd, WINDOW_COMBOBOX_STATUSIS));
 
         opponent_ratingfrom = GetDlgItemInt (hWnd, WINDOW_EDITBOX_LEVELFROM, &result, false);
 
         opponent_ratingto = GetDlgItemInt (hWnd, WINDOW_EDITBOX_LEVELTO, &result, false);
 
         if (result == 0)
 
            opponent_ratingto = 9999; // if we couldn't read a number, set the default value
 
 
 
         // populate the list control
 
         ListView_DeleteAllItems (hListWnd); // start by emptying it first
 
         displayed_count = 0;
 
 
 
         // tell Windows which members of the LVCOLUMN structure we're filling
 
         memset (&lvi, 0, sizeof (lvi));
 
         lvi.mask = LVIF_IMAGE | LVIF_PARAM; // we want to set the image and the item's pointer
 
         for (onlineplayer_index = 0; onlineplayer_index < onlineplayer_count; onlineplayer_index++)
 
         {
 
            onlineplayer = &onlineplayers[onlineplayer_index]; // quick access to online player
 
 
 
            // should item be skipped because of its name ?
 
            if ((opponent_namecontains[0] != 0) && (wcsistr (onlineplayer->nickname, opponent_namecontains) == NULL))
 
               continue; // if so, skip it
 
 
 
            // should item be skipped because of its status ?
 
            if ((opponent_statusis == 1) && (onlineplayer->handlestatus != HANDLESTATUS_INGAME))
 
               continue; // if so, skip it
 
            else if ((opponent_statusis == 2) && (onlineplayer->handlestatus != HANDLESTATUS_INSIMULATION))
 
               continue; // if so, skip it
 
            else if ((opponent_statusis == 3) && (onlineplayer->handlestatus != HANDLESTATUS_INTOURNAMENT))
 
               continue; // if so, skip it
 
            else if ((opponent_statusis == 4) && (onlineplayer->handlestatus != HANDLESTATUS_EXAMININGAGAME))
 
               continue; // if so, skip it
 
            else if ((opponent_statusis == 5) && (onlineplayer->handlestatus != HANDLESTATUS_NOTOPENFORAMATCH))
 
               continue; // if so, skip it
 
            else if ((opponent_statusis == 6) && (onlineplayer->handlestatus != HANDLESTATUS_INACTIVEORBUSY))
 
               continue; // if so, skip it
 
            else if ((opponent_statusis == 7) && (onlineplayer->handlestatus != HANDLESTATUS_AVAILABLE))
 
               continue; // if so, skip it
 
 
 
            // should it be skipped because of its rating (low value) ?
 
            if (onlineplayer->rating < opponent_ratingfrom)
 
               continue; // if so, skip it
 
            else if (onlineplayer->rating > opponent_ratingto)
 
               continue; // if so, skip it
 
 
 
            // first, attach the right structure to this item
 
            lvi.lParam = (LPARAM) onlineplayer;
 
 
 
            // set item's image and name
 
            lvi.iItem = onlineplayer_index;
 
            lvi.iImage = listviewicons[onlineplayer->handlestatus];
 
            insert_index = ListView_InsertItem (hListWnd, &lvi); // add each item to list view
 
 
 
            // set item's substrings
 
 
 
            // nickname
 
            ListView_SetItemText (hListWnd, insert_index, 0, onlineplayer->nickname);
 
 
 
            // FIDE rank
 
            if (onlineplayer->handlecodes & HANDLECODE_FIDEGREATMASTER)
 
               ListView_SetItemText (hListWnd, insert_index, 1, STRING_FIDEGM)
 
            else if (onlineplayer->handlecodes & HANDLECODE_FIDEINTERNATIONALMASTER)
 
               ListView_SetItemText (hListWnd, insert_index, 1, STRING_FIDEIM)
 
            else if (onlineplayer->handlecodes & HANDLECODE_FIDEMASTER)
 
               ListView_SetItemText (hListWnd, insert_index, 1, L"M")
 
            else if (onlineplayer->handlecodes & HANDLECODE_FIDEWOMENSGREATMASTER)
 
               ListView_SetItemText (hListWnd, insert_index, 1, STRING_FIDEWGM)
 
            else if (onlineplayer->handlecodes & HANDLECODE_FIDEWOMENSINTERNATIONALMASTER)
 
               ListView_SetItemText (hListWnd, insert_index, 1, STRING_FIDEWIM)
 
 
 
            // status
 
            ListView_SetItemText (hListWnd, insert_index, 2, handlestatus[onlineplayer->handlestatus].text)
 
 
 
            // rating
 
            if (onlineplayer->ratingtype == OPPONENTRATINGTYPE_ESTIMATED)
 
               swprintf_s (rating_string, WCHAR_SIZEOF (rating_string), L"%d (est.)", onlineplayer->rating);
 
            else if (onlineplayer->ratingtype == OPPONENTRATINGTYPE_PROVISIONAL)
 
               swprintf_s (rating_string, WCHAR_SIZEOF (rating_string), L"%d (prov.)", onlineplayer->rating);
 
            else
 
               swprintf_s (rating_string, WCHAR_SIZEOF (rating_string), L"%d", onlineplayer->rating);
 
            ListView_SetItemText (hListWnd, insert_index, 3, rating_string);
 
 
 
            // characteristics coulumns
 
            if (onlineplayer->handlecodes & HANDLECODE_COMPUTER)
 
               ListView_SetItemText (hListWnd, insert_index, 4, L"×");
 
            if (onlineplayer->handlecodes & HANDLECODE_ADMINISTRATOR)
 
               ListView_SetItemText (hListWnd, insert_index, 5, L"×");
 
            if (onlineplayer->handlecodes & HANDLECODE_BLINDFOLD)
 
               ListView_SetItemText (hListWnd, insert_index, 6, L"×");
 
            if (onlineplayer->handlecodes & HANDLECODE_TEAM)
 
               ListView_SetItemText (hListWnd, insert_index, 7, L"×");
 
            if (onlineplayer->handlecodes & HANDLECODE_CHESSADVISOR)
 
               ListView_SetItemText (hListWnd, insert_index, 8, L"×");
 
            if (onlineplayer->handlecodes & HANDLECODE_SERVICEREPRESENTATIVE)
 
               ListView_SetItemText (hListWnd, insert_index, 9, L"×");
 
            if (onlineplayer->handlecodes & HANDLECODE_TOURNAMENTDIRECTOR)
 
               ListView_SetItemText (hListWnd, insert_index, 10, L"×");
 
            if (onlineplayer->handlecodes & HANDLECODE_MAMERMANAGER)
 
               ListView_SetItemText (hListWnd, insert_index, 11, L"×");
 
            if (onlineplayer->handlecodes & HANDLECODE_UNREGISTERED)
 
               ListView_SetItemText (hListWnd, insert_index, 12, L"×");
 
 
 
            displayed_count++; // we displayed now one player more
 
         }
 
 
 
         // now sort the list view according to the column we selected and its current sort order
 
         ListView_SortItems (hListWnd, CompareProc_ListOpponents, current_sortcolumn);
 
 
 
         // 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 (hListWnd), column_index, &hdi); // get the column's sort arrow state
 
            hdi.fmt &= ~(HDF_SORTDOWN | HDF_SORTUP);
 
            if (column_index == current_sortcolumn)
 
               hdi.fmt |= (listviewcolumns[column_index].sort_descending ? HDF_SORTDOWN : HDF_SORTUP);
 
            Header_SetItem (ListView_GetHeader (hListWnd), column_index, &hdi); // update the column's sort arrow state
 
         }
 
 
 
         // now that the display is finished, IF the reply is arrived, set the totals in the window title
 
         if (onlineplayer_count >= 0)
 
         {
 
            swprintf_s (window_title, WCHAR_SIZEOF (window_title), LOCALIZE (L"Opponents_Title"), onlineplayer_count, displayed_count);
 
            SetWindowText (hWnd, window_title);
 
         }
 
 
 
         onlineplayers_updated = false; // remember we updated the list (don't do it twice)
 
      }
 
 
 
      return (0); // as MSDN says
 
   }
 
 
 
   // is it a list view message ?
 
   else if ((message == WM_NOTIFY) && (wParam == WINDOW_LIST_OPPONENTS))
 
   {
 
      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);
 
               current_sortcolumn = column_index; // save the current sort column
 
            }
 
            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_ListOpponents, lv->iSubItem);
 
      }
 
 
 
      // else is it a double-click on one of the elements ?
 
      else if (lv->hdr.code == NM_DBLCLK)
 
      {
 
         clickeditem = (NMITEMACTIVATE *) lParam; // get the item it is
 
 
 
         // is it valid ?
 
         if (clickeditem->iItem != -1)
 
         {
 
            // get which item it is in the listview data
 
            memset (&lvi, 0, sizeof (lvi));
 
            lvi.iItem = clickeditem->iItem;
 
            lvi.mask = LVIF_PARAM;
 
            ListView_GetItem (lv->hdr.hwndFrom, &lvi);
 
 
 
            onlineplayer = (onlineplayer_t *) lvi.lParam; // get the online player it is
 
 
 
            local_player = Player_FindByType (PLAYER_HUMAN); // find the local player
 
 
 
            // is it NOT ourselves ? if so, find or create an interlocutor structure for this cc member
 
            if ((local_player != NULL) && (wcscmp (onlineplayer->nickname, local_player->name) != 0))
 
               Interlocutor_FindOrCreate (onlineplayer->nickname);
 
            else
 
               PlayerCard_FindOrCreate (onlineplayer->nickname); // else just display his player card
 
         }
 
      }
 
 
 
      return (0); // as MSDN says
 
   }
 
 
 
   // else did we take action on one of the controls ?
 
   else if (message == WM_COMMAND)
 
   {
 
      // was the name edit box changed ?
 
      if ((wParam_loword == WINDOW_EDITBOX_NAMECONTAINS) && (wParam_hiword == EN_CHANGE))
 
      {
 
         onlineplayers_updated = true; // remember online player list is to be updated
 
         SendMessage (hWnd, WM_TIMER, WINDOW_TIMER_REFRESH, NULL); // refresh window now
 
      }
 
 
 
      // else was the filter combo box changed ?
 
      else if ((wParam_loword == WINDOW_COMBOBOX_STATUSIS) && (wParam_hiword == CBN_SELCHANGE))
 
      {
 
         onlineplayers_updated = true; // remember online player list is to be updated
 
         SendMessage (hWnd, WM_TIMER, WINDOW_TIMER_REFRESH, NULL); // refresh window now
 
      }
 
 
 
      // else was either of the rating edit box changed ?
 
      else if ((wParam_loword == WINDOW_EDITBOX_LEVELFROM) && (wParam_hiword == EN_CHANGE))
 
      {
 
         onlineplayers_updated = true; // remember online player list is to be updated
 
         SendMessage (hWnd, WM_TIMER, WINDOW_TIMER_REFRESH, NULL); // refresh window now
 
      }
 
      else if ((wParam_loword == WINDOW_EDITBOX_LEVELTO) && (wParam_hiword == EN_CHANGE))
 
      {
 
         onlineplayers_updated = true; // remember online player list is to be updated
 
         SendMessage (hWnd, WM_TIMER, WINDOW_TIMER_REFRESH, NULL); // refresh window now
 
      }
 
 
 
      // else was it the status bar hyperlink ?
 
      else if (wParam_loword == WINDOW_TEXT_STATUSBAR)
 
         ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
 
 
 
      return (0); // as MSDN says
 
   }
 
 
 
   // call the default window message processing function to keep things going
 
   return (DefWindowProc (hWnd, message, wParam, lParam));
 
}
 
 
 
 
 
static int CALLBACK CompareProc_ListOpponents (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;
 
 
 
   // nickname
 
   if (column == 0)
 
   {
 
      string_to_compare1 = ((onlineplayer_t *) lParam1)->nickname;
 
      string_to_compare2 = ((onlineplayer_t *) lParam2)->nickname;
 
   }
 
 
 
   // FIDE rank
 
   else if (column == 1)
 
   {
 
      if (((onlineplayer_t *) lParam1)->handlecodes & HANDLECODE_FIDEGREATMASTER)
 
         string_to_compare1 = STRING_FIDEGM;
 
      else if (((onlineplayer_t *) lParam1)->handlecodes & HANDLECODE_FIDEINTERNATIONALMASTER)
 
         string_to_compare1 = STRING_FIDEIM;
 
      else if (((onlineplayer_t *) lParam1)->handlecodes & HANDLECODE_FIDEMASTER)
 
         string_to_compare1 = L"M";
 
      else if (((onlineplayer_t *) lParam1)->handlecodes & HANDLECODE_FIDEWOMENSGREATMASTER)
 
         string_to_compare1 = STRING_FIDEWGM;
 
      else if (((onlineplayer_t *) lParam1)->handlecodes & HANDLECODE_FIDEWOMENSINTERNATIONALMASTER)
 
         string_to_compare1 = STRING_FIDEWIM;
 
      else
 
         string_to_compare1 = L"";
 
 
 
      if (((onlineplayer_t *) lParam2)->handlecodes & HANDLECODE_FIDEGREATMASTER)
 
         string_to_compare2 = STRING_FIDEGM;
 
      else if (((onlineplayer_t *) lParam2)->handlecodes & HANDLECODE_FIDEINTERNATIONALMASTER)
 
         string_to_compare2 = STRING_FIDEIM;
 
      else if (((onlineplayer_t *) lParam2)->handlecodes & HANDLECODE_FIDEMASTER)
 
         string_to_compare2 = L"M";
 
      else if (((onlineplayer_t *) lParam2)->handlecodes & HANDLECODE_FIDEWOMENSGREATMASTER)
 
         string_to_compare2 = STRING_FIDEWGM;
 
      else if (((onlineplayer_t *) lParam2)->handlecodes & HANDLECODE_FIDEWOMENSINTERNATIONALMASTER)
 
         string_to_compare2 = STRING_FIDEWIM;
 
      else
 
         string_to_compare2 = L"";
 
   }
 
 
 
   // status
 
   else if (column == 2)
 
   {
 
      string_to_compare1 = handlestatus[((onlineplayer_t *) lParam1)->handlestatus].text;
 
      string_to_compare2 = handlestatus[((onlineplayer_t *) lParam2)->handlestatus].text;
 
   }
 
 
 
   // FIDE rating
 
   else if (column == 3)
 
   {
 
      if (listviewcolumns[column].sort_descending)
 
         return (((onlineplayer_t *) lParam1)->rating <= ((onlineplayer_t *) lParam2)->rating);
 
      else
 
         return (((onlineplayer_t *) lParam1)->rating >= ((onlineplayer_t *) lParam2)->rating);
 
   }
 
 
 
#define ELSE_IF_COLUMN_SORT_BY_FLAG(col,flag) \
 
   else if (column == (col)) \
 
   { \
 
      string_to_compare1 = (((onlineplayer_t *) lParam1)->handlecodes & (flag) ? L"×" : L" "); \
 
      string_to_compare2 = (((onlineplayer_t *) lParam2)->handlecodes & (flag) ? L"×" : L" "); \
 
   }
 
 
 
   ELSE_IF_COLUMN_SORT_BY_FLAG (4, HANDLECODE_COMPUTER)
 
   ELSE_IF_COLUMN_SORT_BY_FLAG (5, HANDLECODE_ADMINISTRATOR)
 
   ELSE_IF_COLUMN_SORT_BY_FLAG (6, HANDLECODE_BLINDFOLD)
 
   ELSE_IF_COLUMN_SORT_BY_FLAG (7, HANDLECODE_TEAM)
 
   ELSE_IF_COLUMN_SORT_BY_FLAG (8, HANDLECODE_CHESSADVISOR)
 
   ELSE_IF_COLUMN_SORT_BY_FLAG (9, HANDLECODE_SERVICEREPRESENTATIVE)
 
   ELSE_IF_COLUMN_SORT_BY_FLAG (10, HANDLECODE_TOURNAMENTDIRECTOR)
 
   ELSE_IF_COLUMN_SORT_BY_FLAG (11, HANDLECODE_MAMERMANAGER)
 
   ELSE_IF_COLUMN_SORT_BY_FLAG (12, HANDLECODE_UNREGISTERED)
 
 
 
#undef ELSE_IF_COLUMN_SORT_BY_FLAG
 
 
 
   // which order do we want this column to be sorted ?
 
   if (listviewcolumns[column].sort_descending)
 
      return (_wcsicmp (string_to_compare1, string_to_compare2)); // normal order
 
   else
 
      return (-_wcsicmp (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));
 
}