-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDlg.cpp
550 lines (470 loc) · 14.9 KB
/
AppDlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/******************************************************************************
** (C) Chris Oldwood
**
** MODULE: APPDLG.CPP
** COMPONENT: The Application.
** DESCRIPTION: CAppDlg class definition.
**
*******************************************************************************
*/
#include "Common.hpp"
#include "AppDlg.hpp"
#include "UTSvrBrowser.hpp"
#include <limits.h>
#include <WCL/BusyCursor.hpp>
/******************************************************************************
**
** Constants.
**
*******************************************************************************
*/
const int CAppDlg::DEF_COLUMN_WIDTH = 50;
/******************************************************************************
** Method: Default constructor.
**
** Description: .
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
CAppDlg::CAppDlg()
: CMainDlg(IDD_MAIN)
{
DEFINE_CTRL_TABLE
CTRL(IDC_GRID, &m_lvGrid)
END_CTRL_TABLE
DEFINE_GRAVITY_TABLE
CTRLGRAV(IDC_GRID, LEFT_EDGE, TOP_EDGE, RIGHT_EDGE, BOTTOM_EDGE)
END_GRAVITY_TABLE
DEFINE_CTRLMSG_TABLE
NFY_CTRLMSG(IDC_GRID, LVN_ITEMCHANGED, &CAppDlg::OnGridSelchange)
NFY_CTRLMSG(IDC_GRID, NM_RCLICK, &CAppDlg::OnGridRightClick)
NFY_CTRLMSG(IDC_GRID, NM_DBLCLK, &CAppDlg::OnGridDoubleClick)
NFY_CTRLMSG(IDC_GRID, LVN_COLUMNCLICK, &CAppDlg::OnGridClickColumn)
END_CTRLMSG_TABLE
}
/******************************************************************************
** Method: Destructor.
**
** Description: .
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
/*
CAppDlg::~CAppDlg()
{
delete m_pRS;
}
*/
/******************************************************************************
** Method: OnInitDialog()
**
** Description: Initialise the dialog.
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CAppDlg::OnInitDialog()
{
// Set grid style.
// m_lvGrid.Font(CFont(ANSI_FIXED_FONT));
m_lvGrid.FullRowSelect(true);
// m_lvGrid.GridLines(true);
// Create grid columns.
m_lvGrid.InsertColumn(HOST_NAME, TXT("Server"), App.m_anColWidths[HOST_NAME], LVCFMT_LEFT);
m_lvGrid.InsertColumn(MOD_NAME, TXT("Mod"), App.m_anColWidths[MOD_NAME], LVCFMT_LEFT);
m_lvGrid.InsertColumn(MAP_TITLE, TXT("Map"), App.m_anColWidths[MAP_TITLE], LVCFMT_LEFT);
m_lvGrid.InsertColumn(GAME_TYPE, TXT("Type"), App.m_anColWidths[GAME_TYPE], LVCFMT_LEFT);
m_lvGrid.InsertColumn(PLAYERS, TXT("Players"), App.m_anColWidths[PLAYERS], LVCFMT_LEFT);
m_lvGrid.InsertColumn(PING_TIME, TXT("Ping"), App.m_anColWidths[PING_TIME], LVCFMT_LEFT);
m_lvGrid.InsertColumn(IP_ADDRESS, TXT("IP Address"), App.m_anColWidths[IP_ADDRESS], LVCFMT_LEFT);
m_lvGrid.InsertColumn(IP_PORT, TXT("IP Port"), App.m_anColWidths[IP_PORT], LVCFMT_LEFT);
// m_lvGrid.InsertColumn(LAST_ERROR, TXT("Error"), App.m_anColWidths[LAST_ERROR], LVCFMT_LEFT);
// Set the listview icons.
m_lvGrid.ImageList(LVSIL_SMALL, IDB_LIST_ICONS, 16, RGB(255, 0, 255));
// Fetch inital sort options.
m_nSortColumn = App.m_nSortCol;
m_eSortOrder = (CSortColumns::Dir) App.m_nSortDir;
}
/******************************************************************************
** Method: OnDestroy()
**
** Description: The dialog is closing, save the column widths.
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CAppDlg::OnDestroy()
{
// Save final sort order.
App.m_nSortCol = m_nSortColumn;
App.m_nSortDir = m_eSortOrder;
// Save final column widths.
for (int i = 0; i < NUM_COLUMNS; ++i)
App.m_anColWidths[i] = m_lvGrid.ColumnWidth(i);
}
/******************************************************************************
** Method: RefreshView()
**
** Description: Refresh the master server list view.
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CAppDlg::RefreshView()
{
CBusyCursor oCursor;
// Remove previous data set.
m_lvGrid.DeleteAllItems();
App.m_oRS.OrderBy(CSortColumns(TableColumn(m_nSortColumn), m_eSortOrder));
// Initialise listview with new data set.
m_lvGrid.Reserve(App.m_oServers.RowCount());
// For all rows.
for (size_t i = 0; i < App.m_oRS.Count(); ++i)
{
CRow& oRow = App.m_oRS[i];
int nIcon = (oRow[CServers::FAV_ID] == null) ? 1 : 0;
// Add to the grid.
m_lvGrid.InsertItem(i, oRow[CServers::HOST_NAME], nIcon);
m_lvGrid.ItemText (i, MOD_NAME, oRow[CServers::MOD_NAME]);
m_lvGrid.ItemText (i, MAP_TITLE, oRow[CServers::MAP_TITLE]);
m_lvGrid.ItemText (i, GAME_TYPE, oRow[CServers::GAME_TYPE]);
m_lvGrid.ItemText (i, PLAYERS, FmtPlayers(oRow));
m_lvGrid.ItemText (i, PING_TIME, FmtPingTime(oRow));
m_lvGrid.ItemText (i, IP_ADDRESS, oRow[CServers::IP_ADDRESS]);
m_lvGrid.ItemText (i, IP_PORT, oRow[CServers::IP_PORT].Format());
// m_lvGrid.ItemText (i, LAST_ERROR, FmtLastErr(oRow));
m_lvGrid.ItemPtr (i, &oRow);
}
}
/******************************************************************************
** Method: RefreshRow()
**
** Description: Refresh a single servers details.
**
** Parameters: oRow The servers details.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CAppDlg::RefreshRow(CRow& oRow)
{
// Find grid row number.
int nRow = m_lvGrid.FindItem(&oRow);
if (nRow == LB_ERR)
return;
// Display server details.
m_lvGrid.ItemImage(nRow, (oRow[CServers::FAV_ID] == null) ? 1 : 0);
m_lvGrid.ItemText (nRow, HOST_NAME, oRow[CServers::HOST_NAME]);
m_lvGrid.ItemText (nRow, MOD_NAME, oRow[CServers::MOD_NAME]);
m_lvGrid.ItemText (nRow, MAP_TITLE, oRow[CServers::MAP_TITLE]);
m_lvGrid.ItemText (nRow, GAME_TYPE, oRow[CServers::GAME_TYPE]);
m_lvGrid.ItemText (nRow, PLAYERS, FmtPlayers(oRow));
m_lvGrid.ItemText (nRow, PING_TIME, FmtPingTime(oRow));
m_lvGrid.ItemText (nRow, IP_ADDRESS, oRow[CServers::IP_ADDRESS]);
m_lvGrid.ItemText (nRow, IP_PORT, oRow[CServers::IP_PORT].Format());
// m_lvGrid.ItemText (nRow, LAST_ERROR, FmtLastErr(oRow));
}
/******************************************************************************
** Method: RefreshAllRows()
**
** Description: Refreshes all servers details without changing the listview
** structure.
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CAppDlg::RefreshAllRows()
{
CBusyCursor oCursor;
// For all rows...
for (size_t i = 0; i < App.m_oRS.Count(); ++i)
{
CRow& oRow = App.m_oRS[i];
// Display server details.
m_lvGrid.ItemImage(i, (oRow[CServers::FAV_ID] == null) ? 1 : 0);
m_lvGrid.ItemText (i, HOST_NAME, oRow[CServers::HOST_NAME]);
m_lvGrid.ItemText (i, MOD_NAME, oRow[CServers::MOD_NAME]);
m_lvGrid.ItemText (i, MAP_TITLE, oRow[CServers::MAP_TITLE]);
m_lvGrid.ItemText (i, GAME_TYPE, oRow[CServers::GAME_TYPE]);
m_lvGrid.ItemText (i, PLAYERS, FmtPlayers(oRow));
m_lvGrid.ItemText (i, PING_TIME, FmtPingTime(oRow));
m_lvGrid.ItemText (i, IP_ADDRESS, oRow[CServers::IP_ADDRESS]);
m_lvGrid.ItemText (i, IP_PORT, oRow[CServers::IP_PORT].Format());
// m_lvGrid.ItemText (i, LAST_ERROR, FmtLastErr(oRow));
}
}
/******************************************************************************
** Method: Sort()
**
** Description: Sort the view by the view column specified.
**
** Parameters: nColumn The view column to sort on.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CAppDlg::Sort(size_t nColumn)
{
// Reverse sort order by default.
CSortColumns::Dir eDir = (CSortColumns::Dir) -m_eSortOrder;
// Sort by different column?
if (nColumn != m_nSortColumn)
{
// Decode column.
switch (nColumn)
{
case HOST_NAME: eDir = CSortColumns::ASC; break;
case MOD_NAME: eDir = CSortColumns::ASC; break;
case MAP_TITLE: eDir = CSortColumns::ASC; break;
case GAME_TYPE: eDir = CSortColumns::ASC; break;
case PLAYERS: eDir = CSortColumns::DESC; break;
case PING_TIME: eDir = CSortColumns::ASC; break;
case IP_ADDRESS: eDir = CSortColumns::ASC; break;
case IP_PORT: eDir = CSortColumns::ASC; break;
// case LAST_ERROR: eDir = CSortColumns::ASC; break;
default: ASSERT_FALSE(); break;
}
}
m_nSortColumn = nColumn;
m_eSortOrder = eDir;
RefreshView();
}
/******************************************************************************
** Method: ToggleColumn()
**
** Description: Show/Hide the given column.
**
** Parameters: nColumn The view column to show/hide.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CAppDlg::ToggleColumn(size_t nColumn)
{
uint nWidth = m_lvGrid.ColumnWidth(nColumn);
// Show?
if (nWidth == 0)
{
nWidth = App.m_anColWidths[nColumn];
// Column saved as hidden?
if (nWidth == 0)
m_lvGrid.ColumnWidthAuto(nColumn);
else
m_lvGrid.ColumnWidth(nColumn, nWidth);
}
// Hide
else
{
App.m_anColWidths[nColumn] = nWidth;
m_lvGrid.ColumnWidth(nColumn, 0);
}
}
/******************************************************************************
** Method: AutoSizeColumns()
**
** Description: Auto-size all the columns.
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CAppDlg::AutoSizeColumns()
{
int nColumns = m_lvGrid.NumColumns();
for (int i = 0; i < nColumns; ++i)
m_lvGrid.ColumnWidthAuto(i);
}
/******************************************************************************
** Method: Fmt*()
**
** Description: Column formatting methods.
**
** Parameters: oRow The row to format.
**
** Returns: The formatted string.
**
*******************************************************************************
*/
CString CAppDlg::FmtPlayers(const CRow& oRow)
{
CString str;
int nCurPlayers = oRow[CServers::NUM_PLAYERS];
int nMaxPlayers = oRow[CServers::MAX_PLAYERS];
if (nMaxPlayers != 0)
str.Format(TXT("%d/%d"), nCurPlayers, nMaxPlayers);
else
str = TXT("N/A");
return str;
}
CString CAppDlg::FmtPingTime(const CRow& oRow)
{
CString str;
int nTime = oRow[CServers::PING_TIME];
if (nTime != INT_MAX)
str.Format(TXT("%d ms"), nTime);
else
str = TXT("N/A");
return str;
}
CString CAppDlg::FmtLastErr(const CRow& oRow)
{
int nError = oRow[CServers::LAST_ERROR];
switch (nError)
{
case CServers::ERROR_NONE: return TXT("");
case CServers::ERROR_FAILED: return TXT("Socket Error");
case CServers::ERROR_TIMED_OUT: return TXT("Timed Out");
default: break;
}
return TXT("");
}
/******************************************************************************
** Method: TableColumn()
**
** Description: Gets the table column that corresponds to the view column.
**
** Parameters: nColumn The view column.
**
** Returns: The table column.
**
*******************************************************************************
*/
size_t CAppDlg::TableColumn(size_t nColumn)
{
int nTabCol = CServers::HOST_NAME;
// Decode column.
switch (nColumn)
{
case HOST_NAME: nTabCol = CServers::HOST_NAME; break;
case MOD_NAME: nTabCol = CServers::MOD_NAME; break;
case MAP_TITLE: nTabCol = CServers::MAP_TITLE; break;
case GAME_TYPE: nTabCol = CServers::GAME_TYPE; break;
case PLAYERS: nTabCol = CServers::NUM_PLAYERS; break;
case PING_TIME: nTabCol = CServers::PING_TIME; break;
case IP_ADDRESS: nTabCol = CServers::IP_ADDRESS; break;
case IP_PORT: nTabCol = CServers::IP_PORT; break;
// case LAST_ERROR: nTabCol = CServers::LAST_ERROR; break;
default: ASSERT_FALSE(); break;
}
return nTabCol;
}
/******************************************************************************
** Method: OnGridSelchange()
**
** Description: Grid item (de)selected, update the main UI.
**
** Parameters: See LVN_ITEMCHANGED.
**
** Returns: See LVN_ITEMCHANGED.
**
*******************************************************************************
*/
LRESULT CAppDlg::OnGridSelchange(NMHDR& oNMHdr)
{
NMLISTVIEW& oMsgHdr = reinterpret_cast<NMLISTVIEW&>(oNMHdr);
if (oMsgHdr.uChanged & LVIF_STATE)
App.m_AppCmds.UpdateUI();
return 0;
}
/******************************************************************************
** Method: OnGridRightClick()
**
** Description: Right click on the grid, show context menu.
**
** Parameters: See NM_RCLICK.
**
** Returns: See NM_RCLICK.
**
*******************************************************************************
*/
LRESULT CAppDlg::OnGridRightClick(NMHDR& oNMHdr)
{
NMITEMACTIVATE& oMsgHdr = reinterpret_cast<NMITEMACTIVATE&>(oNMHdr);
CPopupMenu oMenu(IDR_GRIDMENU);
CRow* pSelRow = SelectedRow();
bool bAnyRows = (m_lvGrid.ItemCount() > 0);
bool bSelection = (pSelRow != nullptr);
bool bFiltered = (App.m_pFilter != nullptr);
bool bModValid = ((pSelRow != nullptr) && (tstrlen(pSelRow->Field(CServers::MOD_NAME)) > 0));
bool bCanAddFav = ((bModValid) && (pSelRow->Field(CServers::FAV_ID) == null));
bool bCanDelFav = ((bModValid) && (pSelRow->Field(CServers::FAV_ID) != null));
// Enable/Disable relevant menu commands.
oMenu.EnableCmd(ID_SERVERS_PING, bSelection);
oMenu.EnableCmd(ID_SERVERS_PING_FILTERED, bFiltered);
oMenu.EnableCmd(ID_SERVERS_PING_ALL, bAnyRows);
oMenu.EnableCmd(ID_SERVERS_REFRESH, true);
oMenu.EnableCmd(ID_SERVERS_ADD_FAV, bCanAddFav);
oMenu.EnableCmd(ID_SERVERS_DEL_FAV, bCanDelFav);
oMenu.EnableCmd(ID_VIEW_DETAILS, bSelection);
// Calculate popup menu position.
CPoint ptMenu = oMsgHdr.ptAction;
::ClientToScreen(m_lvGrid.Handle(), &ptMenu);
// Show menu.
uint nCmdID = oMenu.TrackMenu(m_lvGrid, ptMenu);
// Dispatch command.
if (nCmdID != 0)
App.m_AppWnd.PostCommand(nCmdID);
return 0;
}
/******************************************************************************
** Method: OnGridDoubleClick()
**
** Description: Grid row double-clicked, show the details.
**
** Parameters: See LVN_COLUMNCLICK.
**
** Returns: See LVN_COLUMNCLICK.
**
*******************************************************************************
*/
LRESULT CAppDlg::OnGridDoubleClick(NMHDR& /*oNMHeader*/)
{
// Repost as a "View | Details" command.
if (m_lvGrid.IsSelection())
App.m_AppWnd.PostCommand(ID_VIEW_DETAILS);
return 0;
}
/******************************************************************************
** Method: OnGridClickColumn()
**
** Description: Grid column clicked, resort the grid.
**
** Parameters: See LVN_COLUMNCLICK.
**
** Returns: See LVN_COLUMNCLICK.
**
*******************************************************************************
*/
LRESULT CAppDlg::OnGridClickColumn(NMHDR& oHdr)
{
NMLISTVIEW& oParam = (NMLISTVIEW&) oHdr;
// Sort view by column selected.
Sort(oParam.iSubItem);
App.m_AppCmds.UpdateUI();
return 0;
}