// window_sought.cpp
 
 
 
#include "../common.h"
 
 
 
 
 
// window parameters
 
#define WINDOW_CLASSNAME PROGRAM_NAME L" SoughtGames WndClass"
 
#define WINDOW_DEFAULT_WIDTH 823
 
#define WINDOW_DEFAULT_HEIGHT 600
 
#define WINDOW_MIN_WIDTH 823
 
#define WINDOW_MIN_HEIGHT 200
 
 
 
 
 
// local definitions
 
#define WINDOW_TEXT_DOUBLECLICKORNARROWDOWN 1
 
#define WINDOW_TEXT_GAMEIS 2
 
#define WINDOW_COMBOBOX_GAMEIS 3
 
#define WINDOW_COMBOBOX_RATEDUNRATED 4
 
#define WINDOW_TEXT_LEVELFROM 5
 
#define WINDOW_EDITBOX_LEVELFROM 6
 
#define WINDOW_TEXT_LEVELTO 7
 
#define WINDOW_EDITBOX_LEVELTO 8
 
#define WINDOW_TEXT_YOUCANALSOPOSTYOURSBYCLICKINGHERE 9
 
#define WINDOW_LIST_SOUGHTGAMES 10
 
#define WINDOW_TEXT_STATUSBAR 11
 
#define WINDOW_TIMER_REFRESH 1
 
 
 
 
 
// list view column definition
 
typedef struct listviewcolumn_s
 
{
 
   int width;
 
   int alignment;
 
   bool sort_descending;
 
   wchar_t *text;
 
   HWND hToolTipWnd;
 
} listviewcolumn_t;
 
 
 
 
 
// game types definition
 
typedef struct gametype_s
 
{
 
   wchar_t name[32]; // game type name
 
} gametype_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[] =
 
{
 
   { 110, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"SoughtGames_ColumnGameType")*/, NULL }, // text address needs to be set at runtime, because it's mallocated
 
   { 75, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"SoughtGames_ColumnRatedUnrated")*/, NULL },
 
   { 75, LVCFMT_LEFT, true, NULL /*LOCALIZE (L"SoughtGames_ColumnInitialTime")*/, NULL },
 
   { 75, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"SoughtGames_ColumnIncrement")*/, NULL },
 
   { 130, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"SoughtGames_ColumnOpponent")*/, NULL },
 
   { 50, LVCFMT_CENTER, false, NULL /*LOCALIZE (L"SoughtGames_ColumnRating")*/, NULL },
 
   { 55, LVCFMT_CENTER, false, NULL /*LOCALIZE (L"SoughtGames_ColumnColor")*/, NULL },
 
   { 50, LVCFMT_RIGHT, false, NULL /*LOCALIZE (L"SoughtGames_ColumnAcceptsFrom")*/, NULL },
 
   { 50, LVCFMT_LEFT, false, NULL /*LOCALIZE (L"SoughtGames_ColumnUpTo")*/, NULL },
 
   { 40, LVCFMT_CENTER, false, NULL /*LOCALIZE (L"SoughtGames_ColumnFilter")*/, NULL },
 
   { 40, LVCFMT_CENTER, false, NULL /*LOCALIZE (L"SoughtGames_ColumnAutomatic")*/, NULL },
 
};
 
static int current_sortcolumn = 2;
 
static gametype_t *gametypes; // mallocated
 
static int gametype_count;
 
static wchar_t temp_string[256];
 
static wchar_t gametype_name[32];
 
//static HWND hThisWnd = NULL;
 
#define hThisWnd hSoughtWnd // shared variable
 
 
 
 
 
// prototypes of local functions
 
static LRESULT CALLBACK WindowProc_ThisWindow (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
 
static int CALLBACK CompareProc_ListSoughtGames (LPARAM lParam1, LPARAM lParam2, LPARAM column);
 
static int WINAPI ListView_WndProc (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
 
 
 
 
 
void Window_Sought (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 **) &soughtgames); // free the sought games list we know
 
         soughtgame_count = -1; // and reset its count (-1 means "reply not arrived")
 
         Player_SendBuffer_Add (network_player, 1000, L"sought all\n"); // send the sought games 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"SoughtGames_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_Sought_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_sought_validated = false;
 
 
 
   return; // finished, sought answer has been issued
 
}
 
 
 
 
 
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 soughtgame_gameis;
 
   int soughtgame_ratedunrated;
 
   int soughtgame_ratingfrom;
 
   int soughtgame_ratingto;
 
   int result;
 
   HWND hComboBoxWnd;
 
   HWND hListWnd;
 
   LVCOLUMN lvc;
 
   LVITEM lvi;
 
   HDITEM hdi;
 
   NMLISTVIEW *lv;
 
   NMITEMACTIVATE *clickeditem;
 
   HIMAGELIST imagelist;
 
   MINMAXINFO *minmax;
 
   RECT client_rect;
 
   soughtgame_t *soughtgame;
 
   player_t *network_player;
 
   player_t *local_player;
 
   int soughtgame_index;
 
   int gametype_index;
 
   int column_index;
 
   int insert_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_DOUBLECLICKORNARROWDOWN, hAppInstance, NULL);
 
      CreateWindowEx (WS_EX_RIGHT, L"static", L"",
 
                      WS_CHILD | WS_VISIBLE,
 
                      0, 0, 0, 0, hWnd, (HMENU) WINDOW_TEXT_GAMEIS, hAppInstance, NULL);
 
      CreateWindowEx (WS_EX_CLIENTEDGE, L"combobox", L"", 
 
                      WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST, 
 
                      0, 0, 0, 0, hWnd, (HMENU) WINDOW_COMBOBOX_GAMEIS, hAppInstance, NULL);
 
      CreateWindowEx (WS_EX_CLIENTEDGE, L"combobox", L"", 
 
                      WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST, 
 
                      0, 0, 0, 0, hWnd, (HMENU) WINDOW_COMBOBOX_RATEDUNRATED, 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 (0, L"static", L"",
 
                      WS_CHILD | WS_VISIBLE,
 
                      0, 0, 0, 0, hWnd, (HMENU) WINDOW_TEXT_YOUCANALSOPOSTYOURSBYCLICKINGHERE, 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_SOUGHTGAMES, 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_SOUGHTGAMES);
 
 
 
      // 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"SoughtGames_ColumnGameType");
 
         else if (column_index == 1) lvc.pszText = LOCALIZE (L"SoughtGames_ColumnRatedUnrated");
 
         else if (column_index == 2) lvc.pszText = LOCALIZE (L"SoughtGames_ColumnInitialTime");
 
         else if (column_index == 3) lvc.pszText = LOCALIZE (L"SoughtGames_ColumnIncrement");
 
         else if (column_index == 4) lvc.pszText = LOCALIZE (L"SoughtGames_ColumnOpponent");
 
         else if (column_index == 5) lvc.pszText = LOCALIZE (L"SoughtGames_ColumnRating");
 
         else if (column_index == 6) lvc.pszText = LOCALIZE (L"SoughtGames_ColumnColor");
 
         else if (column_index == 7) lvc.pszText = LOCALIZE (L"SoughtGames_ColumnAcceptsFrom");
 
         else if (column_index == 8) lvc.pszText = LOCALIZE (L"SoughtGames_ColumnUpTo");
 
         else if (column_index == 9) lvc.pszText = LOCALIZE (L"SoughtGames_ColumnFilter");
 
         else if (column_index == 10) lvc.pszText = LOCALIZE (L"SoughtGames_ColumnAutomatic");
 
         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"SoughtGames_DoubleClickOrNarrowDown"));
 
 
 
      // associate the default GUI font with the "status is" message and set its text
 
      SendMessage (GetDlgItem (hWnd, WINDOW_TEXT_GAMEIS), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
 
      SetWindowText (GetDlgItem (hWnd, WINDOW_TEXT_GAMEIS), LOCALIZE (L"SoughtGames_LookingForGameOfType"));
 
 
 
      // associate the default GUI font with the "game is" combo box
 
      SendMessage (GetDlgItem (hWnd, WINDOW_COMBOBOX_GAMEIS), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
 
 
 
      // reset the game types array
 
      gametypes = NULL;
 
      gametype_count = 0;
 
 
 
      // associate the default GUI font with the "rated/unrated" combo box and populate it
 
      SendMessage (GetDlgItem (hWnd, WINDOW_COMBOBOX_RATEDUNRATED), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
 
      ComboBox_ResetContent (GetDlgItem (hWnd, WINDOW_COMBOBOX_RATEDUNRATED));
 
      ComboBox_AddString (GetDlgItem (hWnd, WINDOW_COMBOBOX_RATEDUNRATED), L"");
 
      ComboBox_AddString (GetDlgItem (hWnd, WINDOW_COMBOBOX_RATEDUNRATED), LOCALIZE (L"Unrated"));
 
      ComboBox_AddString (GetDlgItem (hWnd, WINDOW_COMBOBOX_RATEDUNRATED), LOCALIZE (L"Rated"));
 
//      if (_wcsicmp (options.network.login, L"guest") == 0)
 
//         ComboBox_SetCurSel (GetDlgItem (hWnd, WINDOW_COMBOBOX_RATEDUNRATED), 1); // if we log in as guest, select unrated games by default
 
 
 
      // 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"SoughtGames_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"SoughtGames_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 "you can also post yours" message and set its text, then convert it to a hyperlink
 
      SendMessage (GetDlgItem (hWnd, WINDOW_TEXT_YOUCANALSOPOSTYOURSBYCLICKINGHERE), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
 
      SetWindowText (GetDlgItem (hWnd, WINDOW_TEXT_YOUCANALSOPOSTYOURSBYCLICKINGHERE), LOCALIZE (L"SoughtGames_YouCanAlsoPostYoursByClickingHere"));
 
      ConvertStaticToHyperlink (GetDlgItem (hWnd, WINDOW_TEXT_YOUCANALSOPOSTYOURSBYCLICKINGHERE));
 
 
 
      // 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"SoughtGames_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);
 
   }
 
 
 
   // else did we click the close button on the title bar ?
 
   else if (message == WM_CLOSE)
 
      DestroyWindow (hWnd); // close the window
 
 
 
   // else are we destroying the 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
 
 
 
      // now that we're sure the window is closed...
 
      SAFE_free ((void **) &soughtgames); // free the sought games list we know
 
      soughtgame_count = 0; // and reset its count
 
 
 
      SAFE_free ((void **) &gametypes); // free the game types array
 
      gametype_count = 0; // and reset its count
 
 
 
      is_window_sought_validated = true; // remember we closed this window
 
   }
 
 
 
   // 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_GAMEIS),           NULL,   8, 40, 208, 16, SWP_NOZORDER);
 
      SetWindowPos (GetDlgItem (hWnd, WINDOW_COMBOBOX_GAMEIS),       NULL, 224, 38, 128, 20, SWP_NOZORDER);
 
      SetWindowPos (GetDlgItem (hWnd, WINDOW_COMBOBOX_RATEDUNRATED), NULL, 384, 38,  96, 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_TEXT_YOUCANALSOPOSTYOURSBYCLICKINGHERE), NULL, 16, 64, client_rect.right - 32, 16, SWP_NOZORDER);
 
      SetWindowPos (GetDlgItem (hWnd, WINDOW_LIST_SOUGHTGAMES), NULL, 16, 88, client_rect.right - 32, client_rect.bottom - 104, 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;
 
   }
 
 
 
   // else did we take action on one of the controls ?
 
   else if (message == WM_COMMAND)
 
   {
 
      // was the filter combo box changed ?
 
      if ((wParam_loword == WINDOW_COMBOBOX_GAMEIS) && (wParam_hiword == CBN_SELCHANGE))
 
      {
 
         soughtgames_updated = true; // remember sought games display is to be updated
 
         SendMessage (hWnd, WM_TIMER, WINDOW_TIMER_REFRESH, NULL); // refresh window now
 
      }
 
 
 
      // else was the rated/unrated combo box changed ?
 
      if ((wParam_loword == WINDOW_COMBOBOX_RATEDUNRATED) && (wParam_hiword == CBN_SELCHANGE))
 
      {
 
         soughtgames_updated = true; // remember sought games display 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))
 
      {
 
         soughtgames_updated = true; // remember sought games display 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))
 
      {
 
         soughtgames_updated = true; // remember sought games display is to be updated
 
         SendMessage (hWnd, WM_TIMER, WINDOW_TIMER_REFRESH, NULL); // refresh window now
 
      }
 
 
 
      // else was it the "seek" hyperlink ?
 
      else if (wParam_loword == WINDOW_TEXT_YOUCANALSOPOSTYOURSBYCLICKINGHERE)
 
         DialogBox_SendSeek (); // if so, open the seek dialog box
 
 
 
      // 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
 
   }
 
 
 
   // else is it a timer event AND is it our refresh timer AND do we need to update the sought games list ?
 
   else if ((message == WM_TIMER) && (wParam == WINDOW_TIMER_REFRESH) && (soughtgames_updated))
 
   {
 
      // get a quick access to the list and the combo box controls
 
      hListWnd = GetDlgItem (hWnd, WINDOW_LIST_SOUGHTGAMES);
 
      hComboBoxWnd = GetDlgItem (hWnd, WINDOW_COMBOBOX_GAMEIS);
 
 
 
      // grab the different filters
 
      soughtgame_gameis = ComboBox_GetCurSel (hComboBoxWnd);
 
      if (soughtgame_gameis > gametype_count)
 
         soughtgame_gameis = 0; // consistency check
 
      if (soughtgame_gameis > 0)
 
         wcscpy_s (gametype_name, WCHAR_SIZEOF (gametype_name), gametypes[soughtgame_gameis - 1].name); // save game type name
 
      else
 
         gametype_name[0] = 0;
 
      soughtgame_ratedunrated = ComboBox_GetCurSel (GetDlgItem (hWnd, WINDOW_COMBOBOX_RATEDUNRATED));
 
      soughtgame_ratingfrom = GetDlgItemInt (hWnd, WINDOW_EDITBOX_LEVELFROM, &result, false);
 
      soughtgame_ratingto = GetDlgItemInt (hWnd, WINDOW_EDITBOX_LEVELTO, &result, false);
 
      if (result == 0)
 
         soughtgame_ratingto = 9999; // if we couldn't read a number, set the default value
 
 
 
      // first, build the game types drop box
 
 
 
      SAFE_free ((void **) &gametypes); // free the game types array
 
      gametype_count = 0; // and reset its count
 
 
 
      // cycle through all sought games...
 
      for (soughtgame_index = 0; soughtgame_index < soughtgame_count; soughtgame_index++)
 
      {
 
         // cycle through all game types we know and see if we find it
 
         for (gametype_index = 0; gametype_index < gametype_count; gametype_index++)
 
            if (wcscmp (gametypes[gametype_index].name, soughtgames[soughtgame_index].game_type) == 0)
 
               break; // break as soon as we find it
 
 
 
         // have we NOT found it ?
 
         if (gametype_index == gametype_count)
 
         {
 
            gametypes = (gametype_t *) SAFE_realloc (gametypes, gametype_count, gametype_count + 1, sizeof (gametype_t), false);
 
            wcscpy_s (gametypes[gametype_count].name, WCHAR_SIZEOF (gametypes[gametype_count].name), soughtgames[soughtgame_index].game_type);
 
            gametype_count++; // if so, reallocate game types array and remember one game type more
 
         }
 
      }
 
 
 
      // now populate the game types combo box
 
      ComboBox_ResetContent (hComboBoxWnd);
 
      ComboBox_AddString (hComboBoxWnd, L"");
 
      for (gametype_index = 0; gametype_index < gametype_count; gametype_index++)
 
         ComboBox_AddString (hComboBoxWnd, gametypes[gametype_index].name);
 
 
 
      // cycle through all the updated game types now...
 
      for (gametype_index = 0; gametype_index < gametype_count; gametype_index++)
 
         if (wcscmp (gametypes[gametype_index].name, gametype_name) == 0)
 
         {
 
            ComboBox_SetCurSel (hComboBoxWnd, gametype_index + 1); // put the selection back
 
            break; // and stop searching as soon as we've found it again
 
         }
 
 
 
      // populate the list control
 
      ListView_DeleteAllItems (hListWnd); // start by emptying it first
 
 
 
      // 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 (soughtgame_index = 0; soughtgame_index < soughtgame_count; soughtgame_index++)
 
      {
 
         soughtgame = &soughtgames[soughtgame_index]; // quick access to sought game
 
 
 
         // should item be skipped because of its game type ?
 
         if ((soughtgame_gameis > 0) && (_wcsicmp (soughtgame->game_type, gametype_name) != 0))
 
            continue; // if so, skip it
 
 
 
         // should it be skipped because it's NOT rated and we want it to be, or the other way around ?
 
         if (((soughtgame->rating_type != GAMERATINGTYPE_SUPPORTEDUNRATED) && (soughtgame_ratedunrated == 1))
 
             || ((soughtgame->rating_type != GAMERATINGTYPE_SUPPORTEDRATED) && (soughtgame_ratedunrated == 2)))
 
            continue; // if so, skip it
 
 
 
         // should it be skipped because of its rating ?
 
         if (soughtgame->rating < soughtgame_ratingfrom)
 
            continue; // if so, skip it
 
         else if (soughtgame->rating > soughtgame_ratingto)
 
            continue; // if so, skip it
 
 
 
         // first, attach the right structure to this item
 
         lvi.lParam = (LPARAM) soughtgame;
 
 
 
         // set item's image and name
 
         lvi.iItem = soughtgame_index;
 
         if (soughtgame->rating_type != GAMERATINGTYPE_UNSUPPORTED)
 
            lvi.iImage = listviewicons[HANDLESTATUS_INGAME]; // supported variant
 
         else
 
            lvi.iImage = listviewicons[HANDLESTATUS_NOTOPENFORAMATCH]; // unsupported variant
 
 
 
         insert_index = ListView_InsertItem (hListWnd, &lvi); // add each item to list view
 
 
 
         // set item's substrings
 
 
 
         // game type
 
         ListView_SetItemText (hListWnd, insert_index, 0, soughtgame->game_type);
 
 
 
         // rated/unrated
 
         if (soughtgame->rating_type == GAMERATINGTYPE_SUPPORTEDRATED)
 
            ListView_SetItemText (hListWnd, insert_index, 1, LOCALIZE (L"Rated")) // rated game
 
         else if (soughtgame->rating_type == GAMERATINGTYPE_SUPPORTEDUNRATED)
 
            ListView_SetItemText (hListWnd, insert_index, 1, LOCALIZE (L"Unrated")) // unrated game
 
 
 
         // initial time
 
         if (soughtgame->initial_time > 0.0f)
 
         {
 
            swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), L"%.0f %s", soughtgame->initial_time, LOCALIZE (L"Minutes"));
 
            ListView_SetItemText (hListWnd, insert_index, 2, temp_string);
 
         }
 
 
 
         // Fischer increment
 
         if (soughtgame->increment > 0.0f)
 
         {
 
            swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), L"%.0f %s", soughtgame->increment, LOCALIZE (L"Seconds"));
 
            ListView_SetItemText (hListWnd, insert_index, 3, temp_string);
 
         }
 
 
 
         // opponent
 
         ListView_SetItemText (hListWnd, insert_index, 4, soughtgame->nickname);
 
 
 
         // opponent's ELO
 
         if (soughtgame->rating > 0)
 
         {
 
            swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), L"%d", soughtgame->rating);
 
            ListView_SetItemText (hListWnd, insert_index, 5, temp_string);
 
         }
 
 
 
         // requested color
 
         if (soughtgame->color != COLOR_UNSPECIFIED)
 
         {
 
            if (soughtgame->color == COLOR_BLACK)
 
               swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Games_Black"));
 
            else
 
               swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), LOCALIZE (L"Games_White"));
 
            ListView_SetItemText (hListWnd, insert_index, 6, temp_string);
 
         }
 
 
 
         // minimal accepted ELO
 
         if (soughtgame->lowest_accepted > 0)
 
         {
 
            swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), L"> %d", soughtgame->lowest_accepted);
 
            ListView_SetItemText (hListWnd, insert_index, 7, temp_string);
 
         }
 
 
 
         // maximal accepted ELO
 
         if ((soughtgame->highest_accepted > 0) && (soughtgame->highest_accepted < 9999))
 
         {
 
            swprintf_s (temp_string, WCHAR_SIZEOF (temp_string), L"< %d", soughtgame->highest_accepted);
 
            ListView_SetItemText (hListWnd, insert_index, 8, temp_string);
 
         }
 
 
 
         // whether this sought game's replies will be filtered
 
         if (soughtgame->formula_checked)
 
            ListView_SetItemText (hListWnd, insert_index, 9, L"×");
 
 
 
         // whether this sought game will start automatically
 
         if (!soughtgame->manual_start)
 
            ListView_SetItemText (hListWnd, insert_index, 10, L"×");
 
      }
 
 
 
      // now sort the list view according to the column we selected and its current sort order
 
      ListView_SortItems (hListWnd, CompareProc_ListSoughtGames, 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 (soughtgame_count >= 0)
 
         SetWindowText (hWnd, LOCALIZE (L"SoughtGames_Title"));
 
 
 
      soughtgames_updated = false; // remember we updated the list (don't do it twice)
 
   }
 
 
 
   // else is it a list view message AND is it for this list view ?
 
   else if ((message == WM_NOTIFY) && (wParam == WINDOW_LIST_SOUGHTGAMES))
 
   {
 
      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_ListSoughtGames, current_sortcolumn);
 
      }
 
 
 
      // 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_IMAGE | LVIF_PARAM;
 
            ListView_GetItem (lv->hdr.hwndFrom, &lvi);
 
 
 
            soughtgame = (soughtgame_t *) lvi.lParam; // get the sought game it is
 
 
 
            local_player = Player_FindByType (PLAYER_HUMAN); // find the local player
 
 
 
            // is it NOT ourselves ?
 
            if ((local_player != NULL) && (wcscmp (soughtgame->nickname, local_player->name) != 0))
 
            {
 
               // is this an unsupported chess variant ?
 
               if (lvi.iImage == listviewicons[HANDLESTATUS_NOTOPENFORAMATCH])
 
               {
 
                  messagebox.hWndParent = hWnd;
 
                  wcscpy_s (messagebox.title, WCHAR_SIZEOF (messagebox.title), soughtgame->game_type);
 
                  wcscpy_s (messagebox.text, WCHAR_SIZEOF (messagebox.text), LOCALIZE (L"Error_UnsupportedChessVariant"));
 
                  messagebox.flags = MB_ICONINFORMATION | MB_OK;
 
                  DialogBox_Message (&messagebox); // display a modeless error message box
 
               }
 
               else
 
               {
 
                  network_player = Player_FindByType (PLAYER_INTERNET); // quick access to network player
 
                  if (network_player == NULL)
 
                     return (0); // consistency check
 
 
 
                  // send the play notification and wait for the reply
 
                  Player_SendBuffer_Add (network_player, 1000, L"play %d\n", soughtgame->id);
 
 
 
                  // if this sought game is set to manual start, send a notification to this player's chat window
 
                  // HACK: don't display if we're likely to be refused because of guest not allowed to play rated games
 
                  if (soughtgame->manual_start && !((soughtgame->rating_type == GAMERATINGTYPE_SUPPORTEDRATED) && (wcscmp (options.network.login, L"guest") == 0)))
 
                     Interlocutor_Notify (Interlocutor_FindOrCreate (soughtgame->nickname), LOCALIZE (L"Chat_InvitationSent"), soughtgame->nickname);
 
               }
 
            }
 
            else
 
               DialogBox_SendSeek (); // else it's our own seek, so open the seek dialog box to modify it
 
         }
 
      }
 
   }
 
 
 
   // call the default window message processing function to keep things going
 
   return (DefWindowProc (hWnd, message, wParam, lParam));
 
}
 
 
 
 
 
static int CALLBACK CompareProc_ListSoughtGames (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;
 
 
 
   // game type
 
   if (column == 0)
 
   {
 
      string_to_compare1 = ((soughtgame_t *) lParam1)->game_type;
 
      string_to_compare2 = ((soughtgame_t *) lParam2)->game_type;
 
   }
 
 
 
   // rated/unrated
 
   else if (column == 1)
 
   {
 
      if (((soughtgame_t *) lParam1)->rating_type == GAMERATINGTYPE_SUPPORTEDRATED)
 
         string_to_compare1 = LOCALIZE (L"Rated");
 
      else if (((soughtgame_t *) lParam1)->rating_type == GAMERATINGTYPE_SUPPORTEDUNRATED)
 
         string_to_compare1 = LOCALIZE (L"Unrated");
 
      else
 
         string_to_compare1 = L"";
 
 
 
      if (((soughtgame_t *) lParam2)->rating_type == GAMERATINGTYPE_SUPPORTEDRATED)
 
         string_to_compare2 = LOCALIZE (L"Rated");
 
      else if (((soughtgame_t *) lParam2)->rating_type == GAMERATINGTYPE_SUPPORTEDUNRATED)
 
         string_to_compare2 = LOCALIZE (L"Unrated");
 
      else
 
         string_to_compare2 = L"";
 
   }
 
 
 
   // initial time
 
   else if (column == 2)
 
   {
 
      if (listviewcolumns[column].sort_descending)
 
         return (((soughtgame_t *) lParam1)->initial_time <= ((soughtgame_t *) lParam2)->initial_time);
 
      else
 
         return (((soughtgame_t *) lParam1)->initial_time >= ((soughtgame_t *) lParam2)->initial_time);
 
   }
 
 
 
   // increment
 
   else if (column == 3)
 
   {
 
      if (listviewcolumns[column].sort_descending)
 
         return (((soughtgame_t *) lParam1)->increment <= ((soughtgame_t *) lParam2)->increment);
 
      else
 
         return (((soughtgame_t *) lParam1)->increment >= ((soughtgame_t *) lParam2)->increment);
 
   }
 
 
 
   // opponent
 
   else if (column == 4)
 
   {
 
      string_to_compare1 = ((soughtgame_t *) lParam1)->nickname;
 
      string_to_compare2 = ((soughtgame_t *) lParam2)->nickname;
 
   }
 
 
 
   // rating
 
   else if (column == 5)
 
   {
 
      if (listviewcolumns[column].sort_descending)
 
         return (((soughtgame_t *) lParam1)->rating <= ((soughtgame_t *) lParam2)->rating);
 
      else
 
         return (((soughtgame_t *) lParam1)->rating >= ((soughtgame_t *) lParam2)->rating);
 
   }
 
 
 
   // color
 
   else if (column == 6)
 
   {
 
      if (((soughtgame_t *) lParam1)->color == COLOR_BLACK)
 
         string_to_compare1 = LOCALIZE (L"BlackMoves");
 
      else if (((soughtgame_t *) lParam1)->color == COLOR_WHITE)
 
         string_to_compare1 = LOCALIZE (L"WhiteMoves");
 
      else
 
         string_to_compare1 = L"";
 
 
 
      if (((soughtgame_t *) lParam2)->color == COLOR_BLACK)
 
         string_to_compare2 = LOCALIZE (L"BlackMoves");
 
      else if (((soughtgame_t *) lParam2)->color == COLOR_WHITE)
 
         string_to_compare2 = LOCALIZE (L"WhiteMoves");
 
      else
 
         string_to_compare2 = L"";
 
   }
 
 
 
   // accepts from
 
   else if (column == 7)
 
   {
 
      if (listviewcolumns[column].sort_descending)
 
         return (((soughtgame_t *) lParam1)->lowest_accepted <= ((soughtgame_t *) lParam2)->lowest_accepted);
 
      else
 
         return (((soughtgame_t *) lParam1)->lowest_accepted >= ((soughtgame_t *) lParam2)->lowest_accepted);
 
   }
 
 
 
   // up to
 
   else if (column == 8)
 
   {
 
      if (listviewcolumns[column].sort_descending)
 
         return (((soughtgame_t *) lParam1)->highest_accepted <= ((soughtgame_t *) lParam2)->highest_accepted);
 
      else
 
         return (((soughtgame_t *) lParam1)->highest_accepted >= ((soughtgame_t *) lParam2)->highest_accepted);
 
   }
 
 
 
   // filter
 
   else if (column == 9)
 
   {
 
      string_to_compare1 = (((soughtgame_t *) lParam1)->formula_checked ? L"×" : L" ");
 
      string_to_compare2 = (((soughtgame_t *) lParam2)->formula_checked ? L"×" : L" ");
 
   }
 
 
 
   // automatic
 
   else if (column == 10)
 
   {
 
      string_to_compare1 = (((soughtgame_t *) lParam1)->manual_start ? L" " : L"×");
 
      string_to_compare2 = (((soughtgame_t *) lParam2)->manual_start ? L" " : L"×");
 
   }
 
 
 
   // 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));
 
}