Subversion Repositories Games.Chess Giants

Rev

Rev 21 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. // window_motd.cpp
  2.  
  3. #include "../common.h"
  4.  
  5.  
  6. // window parameters
  7. #define WINDOW_CLASSNAME PROGRAM_NAME L" MOTD WndClass"
  8. #define WINDOW_DEFAULT_WIDTH 720
  9. #define WINDOW_DEFAULT_HEIGHT 600
  10. #define WINDOW_MIN_WIDTH 320
  11. #define WINDOW_MIN_HEIGHT 240
  12.  
  13.  
  14. // local definitions
  15. #define WINDOW_EDITBOX_MOTD 1
  16. #define WINDOW_CHECKBOX_SHOWMOTD 2
  17. #define WINDOW_TEXT_STATUSBAR 3
  18.  
  19.  
  20. // global variables
  21. static bool is_classregistered = false;
  22. //static HWND hThisWnd = NULL;
  23. #define hThisWnd hMOTDWnd // shared variable
  24.  
  25.  
  26. // prototypes of local functions
  27. static LRESULT CALLBACK WindowProc_ThisWindow (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam);
  28.  
  29.  
  30. void Window_MOTD (void)
  31. {
  32.    // helper function to fire up the child window
  33.  
  34.    WNDCLASSEX wc;
  35.  
  36.    // is the window we want to display already displayed ?
  37.    if (IsWindow (hThisWnd))
  38.       SetForegroundWindow (hThisWnd); // if so, just bring it to front
  39.  
  40.    // else the way is clear
  41.    else
  42.    {
  43.       // is the window class NOT registered yet ?
  44.       if (!is_classregistered)
  45.       {
  46.          // if so, register the window class once and for all
  47.          memset (&wc, 0, sizeof (wc));
  48.          wc.cbSize = sizeof (wc);
  49.          wc.style = CS_HREDRAW | CS_VREDRAW;
  50.          wc.lpfnWndProc = WindowProc_ThisWindow;
  51.          wc.hInstance = hAppInstance;
  52.          wc.hIcon = LoadIcon (hAppInstance, (wchar_t *) ICON_MAIN);
  53.          wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  54.          wc.hbrBackground = GetSysColorBrush (COLOR_3DFACE);
  55.          wc.lpszClassName = WINDOW_CLASSNAME;
  56.          RegisterClassEx (&wc);
  57.  
  58.          is_classregistered = true; // remember this window class is registered
  59.       }
  60.  
  61.       // create the child window
  62.       hThisWnd = CreateWindowEx (WS_EX_CLIENTEDGE, WINDOW_CLASSNAME, LOCALIZE (L"MOTD_Title"), WS_OVERLAPPEDWINDOW,
  63.                                  CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_DEFAULT_WIDTH, WINDOW_DEFAULT_HEIGHT,
  64.                                  NULL, NULL, hAppInstance, NULL);
  65.    }
  66.  
  67.    return; // return as soon as the thread is fired up
  68. }
  69.  
  70.  
  71. void Window_MOTD_Validated (void)
  72. {
  73.    // callback function called by the main game thread when the window is validated
  74.  
  75.    // remember this callback is no longer to be called
  76.    is_window_motd_validated = false;
  77.  
  78.    return; // finished
  79. }
  80.  
  81.  
  82. static LRESULT CALLBACK WindowProc_ThisWindow (HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
  83. {
  84.    // message handler for the child window
  85.  
  86.    static const wchar_t rtf_template_begin[] = L"{\\rtf1\\ansi{\\fonttbl\\f0\\f{Lucida Console};}\\f0\\fs20\\pard\n";
  87.    static const wchar_t rtf_template_end[] = L"\\par\n}\n";
  88.    static wchar_t line_buffer[256];
  89.  
  90.    unsigned short wParam_hiword;
  91.    unsigned short wParam_loword;
  92.    MINMAXINFO *minmax;
  93.    RECT client_rect;
  94.    wchar_t *rtf_string; // mallocated
  95.    wchar_t *string_pointer;
  96.    int rtf_stringsize;
  97.  
  98.    // filter out the commonly used message values
  99.    wParam_hiword = HIWORD (wParam);
  100.    wParam_loword = LOWORD (wParam);
  101.  
  102.    // have we just fired up this window ?
  103.    if (message == WM_CREATE)
  104.    {
  105.       // center the window
  106.       CenterWindow (hWnd, hMainWnd);
  107.  
  108.       // populate the window
  109.       LoadLibrary (L"riched32.dll"); // ensure the RichEdit controls DLL is loaded
  110.       CreateWindowEx (0, L"RichEdit20A", L"", // for some reason, only the ANSI version of this control accepts RTF data :(
  111.                       WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY | ES_SELECTIONBAR | ES_SUNKEN,
  112.                       0, 0, 0, 0, hWnd, (HMENU) WINDOW_EDITBOX_MOTD, hAppInstance, NULL);
  113.       CreateWindowEx (0, L"button", L"",
  114.                       BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE,
  115.                       0, 0, 0, 0, hWnd, (HMENU) WINDOW_CHECKBOX_SHOWMOTD, hAppInstance, NULL);
  116.       CreateWindowEx (WS_EX_RIGHT, L"static", L"",
  117.                       WS_CHILD | WS_VISIBLE,
  118.                       0, 0, 0, 0, hWnd, (HMENU) WINDOW_TEXT_STATUSBAR, hAppInstance, NULL);
  119.  
  120.       // set the MOTD text
  121.       rtf_stringsize = wcslen (rtf_template_begin) + 2 * wcslen (server_motd) + wcslen (rtf_template_end) + 1;
  122.       rtf_string = (wchar_t *) SAFE_malloc (rtf_stringsize, sizeof (wchar_t), false);
  123.       wcscpy_s (rtf_string, rtf_stringsize, rtf_template_begin);
  124.       string_pointer = server_motd; // start reading MOTD line per line
  125.       while ((string_pointer = wcsgets (line_buffer, WCHAR_SIZEOF (line_buffer), string_pointer)) != NULL)
  126.       {
  127.          wcscat_s (line_buffer, WCHAR_SIZEOF (line_buffer), L"\\line\n"); // append a RTF linebreak to each line
  128.          wcscat_s (rtf_string, rtf_stringsize, line_buffer); // and append each line to the translated MOTD buffer
  129.       }
  130.       wcscat_s (rtf_string, rtf_stringsize, rtf_template_end);
  131.       SendMessage (GetDlgItem (hWnd, WINDOW_EDITBOX_MOTD), EM_SETBKGNDCOLOR, 0, RGB (224, 224, 224));
  132.       SetWindowText (GetDlgItem (hWnd, WINDOW_EDITBOX_MOTD), rtf_string);
  133.       SAFE_free ((void **) &rtf_string);
  134.  
  135.       // associate the default GUI font with the check box, set its text and check/uncheck it if needed
  136.       SendMessage (GetDlgItem (hWnd, WINDOW_CHECKBOX_SHOWMOTD), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
  137.       SetWindowText (GetDlgItem (hWnd, WINDOW_CHECKBOX_SHOWMOTD), LOCALIZE (L"MOTD_ShowMOTD"));
  138.       Button_SetCheck (GetDlgItem (hWnd, WINDOW_CHECKBOX_SHOWMOTD), (options.network.want_motdonconnect ? BST_CHECKED : BST_UNCHECKED));
  139.  
  140.       // associate the default GUI font with the status bar and set its text
  141.       SendMessage (GetDlgItem (hWnd, WINDOW_TEXT_STATUSBAR), WM_SETFONT, (WPARAM) GetStockObject (DEFAULT_GUI_FONT), false);
  142.       SetWindowText (GetDlgItem (hWnd, WINDOW_TEXT_STATUSBAR), LOCALIZE (L"MOTD_StatusBar"));
  143.  
  144.       // convert the status bar message to a hyperlink
  145.       ConvertStaticToHyperlink (GetDlgItem (hWnd, WINDOW_TEXT_STATUSBAR));
  146.  
  147.       // now show the window
  148.       ShowWindow (hWnd, SW_SHOW);
  149.  
  150.       return (0); // as MSDN says
  151.    }
  152.  
  153.    // else did we click the close button on the title bar ?
  154.    else if (message == WM_CLOSE)
  155.    {
  156.       DestroyWindow (hWnd); // close the window
  157.       return (0); // as MSDN says
  158.    }
  159.  
  160.    // else are we destroying this window ?
  161.    else if (message == WM_DESTROY)
  162.    {
  163.       hThisWnd = NULL; // window is closed
  164.       SetForegroundWindow (hMainWnd); // restore focus on the main window
  165.       is_window_motd_validated = true; // remember we closed this window
  166.       the_board.reevaluate = true; // refresh the GUI buttons if needed
  167.       return (0); // as MSDN says
  168.    }
  169.  
  170.    // else are we resizing the window ?
  171.    else if (message == WM_SIZE)
  172.    {
  173.       // get the new window size
  174.       GetClientRect (hWnd, &client_rect);
  175.  
  176.       // position the window elements
  177.       SetWindowPos (GetDlgItem (hWnd, WINDOW_EDITBOX_MOTD), NULL, 16, 16, client_rect.right - 32, client_rect.bottom - 64, SWP_NOZORDER);
  178.       SetWindowPos (GetDlgItem (hWnd, WINDOW_CHECKBOX_SHOWMOTD), NULL, 16, client_rect.bottom - 32, client_rect.right - 32, 16, SWP_NOZORDER);
  179.       SetWindowPos (GetDlgItem (hWnd, WINDOW_TEXT_STATUSBAR), NULL, 0, client_rect.bottom - 16, client_rect.right, 16, SWP_NOZORDER);
  180.  
  181.       return (0); // as MSDN says
  182.    }
  183.  
  184.    // else are we asking how big/small we can resize ?
  185.    else if (message == WM_GETMINMAXINFO)
  186.    {
  187.       minmax = (MINMAXINFO *) lParam; // get a pointer to the min/max info structure
  188.  
  189.       minmax->ptMinTrackSize.x = WINDOW_MIN_WIDTH;
  190.       minmax->ptMinTrackSize.y = WINDOW_MIN_HEIGHT;
  191.  
  192.       return (0); // as MSDN says
  193.    }
  194.  
  195.    // else did we take action on one of the controls ?
  196.    else if (message == WM_COMMAND)
  197.    {
  198.       // was it the show MOTD checkbox ?
  199.       if (wParam_loword == WINDOW_CHECKBOX_SHOWMOTD)
  200.          options.network.want_motdonconnect = (Button_GetCheck (GetDlgItem (hWnd, WINDOW_CHECKBOX_SHOWMOTD)) != 0); // if so, update settings
  201.  
  202.       // else was it the status bar hyperlink ?
  203.       else if (wParam_loword == WINDOW_TEXT_STATUSBAR)
  204.          ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized
  205.  
  206.       return (0); // as MSDN says
  207.    }
  208.  
  209.    // call the default window message processing function to keep things going
  210.    return (DefWindowProc (hWnd, message, wParam, lParam));
  211. }
  212.