Rev 21 | Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | pmbaty | 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 | |||
| 151 | // else did we click the close button on the title bar ? |
||
| 152 | else if (message == WM_CLOSE) |
||
| 153 | DestroyWindow (hWnd); // destroy this window |
||
| 154 | |||
| 155 | // else are we destroying this window ? |
||
| 156 | else if (message == WM_DESTROY) |
||
| 157 | { |
||
| 158 | hThisWnd = NULL; // window is closed |
||
| 159 | SetForegroundWindow (hMainWnd); // restore focus on the main window |
||
| 160 | is_window_motd_validated = true; // remember we closed this window |
||
| 161 | } |
||
| 162 | |||
| 163 | // else are we resizing the window ? |
||
| 164 | else if (message == WM_SIZE) |
||
| 165 | { |
||
| 166 | // get the new window size |
||
| 167 | GetClientRect (hWnd, &client_rect); |
||
| 168 | |||
| 169 | // position the window elements |
||
| 170 | SetWindowPos (GetDlgItem (hWnd, WINDOW_EDITBOX_MOTD), NULL, 16, 16, client_rect.right - 32, client_rect.bottom - 64, SWP_NOZORDER); |
||
| 171 | SetWindowPos (GetDlgItem (hWnd, WINDOW_CHECKBOX_SHOWMOTD), NULL, 16, client_rect.bottom - 32, client_rect.right - 32, 16, SWP_NOZORDER); |
||
| 172 | SetWindowPos (GetDlgItem (hWnd, WINDOW_TEXT_STATUSBAR), NULL, 0, client_rect.bottom - 16, client_rect.right, 16, SWP_NOZORDER); |
||
| 173 | } |
||
| 174 | |||
| 175 | // else are we asking how big/small we can resize ? |
||
| 176 | else if (message == WM_GETMINMAXINFO) |
||
| 177 | { |
||
| 178 | minmax = (MINMAXINFO *) lParam; // get a pointer to the min/max info structure |
||
| 179 | |||
| 180 | minmax->ptMinTrackSize.x = WINDOW_MIN_WIDTH; |
||
| 181 | minmax->ptMinTrackSize.y = WINDOW_MIN_HEIGHT; |
||
| 182 | } |
||
| 183 | |||
| 184 | // else did we take action on one of the controls ? |
||
| 185 | else if (message == WM_COMMAND) |
||
| 186 | { |
||
| 187 | // was it the show MOTD checkbox ? |
||
| 188 | if (wParam_loword == WINDOW_CHECKBOX_SHOWMOTD) |
||
| 189 | options.network.want_motdonconnect = (Button_GetCheck (GetDlgItem (hWnd, WINDOW_CHECKBOX_SHOWMOTD)) != 0); // if so, update settings |
||
| 190 | |||
| 191 | // else was it the status bar hyperlink ? |
||
| 192 | else if (wParam_loword == WINDOW_TEXT_STATUSBAR) |
||
| 193 | ShellExecute (NULL, L"open", PROGRAM_URL, NULL, NULL, SW_MAXIMIZE); // open the donation page in the default browser, maximized |
||
| 194 | } |
||
| 195 | |||
| 196 | // call the default window message processing function to keep things going |
||
| 197 | return (DefWindowProc (hWnd, message, wParam, lParam)); |
||
| 198 | } |