@@ -26,8 +26,17 @@ using namespace NWindows;
2626
2727extern HINSTANCE g_hInstance;
2828extern bool g_DisableUserQuestions;
29- // Set true by CProgressDialog when error messages occur.
30- bool g_bProcessError = false ;
29+
30+ namespace {
31+ bool & ProcessErrorRef ()
32+ {
33+ static bool s_error = false ;
34+ return s_error;
35+ }
36+ }
37+
38+ void ProgressDialog_SetError (bool error) { ProcessErrorRef () = error; }
39+ bool ProgressDialog_HadError () { return ProcessErrorRef (); }
3140
3241static const UINT WM_TRAY_ICON_NOTIFY = WM_APP + 10 ;
3342static const UINT ID_SYSTRAY_ICON = 100 ;
@@ -52,7 +61,7 @@ static BOOL SetSysTray(HWND dlg,
5261
5362 if (tip != nullptr && *tip != 0 )
5463 {
55- ::lstrcpynW (data.szTip, tip, sizeof (data.szTip) / sizeof( WCHAR ));
64+ ::lstrcpynW (data.szTip, tip, Z7_ARRAY_SIZE (data.szTip));
5665 }
5766
5867 return Shell_NotifyIconW (message, &data);
@@ -303,11 +312,6 @@ CProgressDialog::CProgressDialog():
303312 IconID(-1 ),
304313 MainWindow(NULL )
305314{
306- for (int i = 0 ; i < kNumTrayIcons ; i++)
307- _iconSysTrayArray[i] = nullptr ;
308- _sysTrayIconArrayId = -1 ;
309- _sysTrayMenu = nullptr ;
310-
311315 if (_dialogCreatedEvent.Create () != S_OK )
312316 throw 1334987 ;
313317 if (_createDialogEvent.Create () != S_OK )
@@ -888,14 +892,14 @@ void CProgressDialog::UpdateStatInfo(bool showAll)
888892 _prevPercentValue = percent;
889893 needSetTitle = true ;
890894
891- wchar_t szPercent[ 32 ] ;
892- wchar_t *p = ConvertUInt64ToString (percent, szPercent);
895+ std::array< wchar_t , 32 > szPercent ;
896+ wchar_t *p = ConvertUInt64ToString (percent, szPercent. data () );
893897 *p++ = L' %' ;
894898 *p = 0 ;
895- SetItemText (IDC_PROGRESS_PERCENT , szPercent);
899+ SetItemText (IDC_PROGRESS_PERCENT , szPercent. data () );
896900
897- if (_background && _iconSysTrayArray [0 ] != nullptr )
898- UpdateSysTrayIcon ( false , true );
901+ if (_background && _sysTray. Icons [0 ] != nullptr )
902+ _sysTray. UpdateIcon (_window, _prevPercentValue, false , true );
899903 }
900904 }
901905
@@ -1131,18 +1135,18 @@ bool CProgressDialog::OnMessage(UINT message, WPARAM wParam, LPARAM lParam)
11311135 if (MainWindow != nullptr )
11321136 ShowWindow (MainWindow, SW_SHOW );
11331137 }
1134- if (_iconSysTrayArray [0 ] != nullptr )
1138+ if (_sysTray. Icons [0 ] != nullptr )
11351139 {
1136- for (int i = 0 ; i < kNumTrayIcons ; i++ )
1140+ for (auto &icon : _sysTray. Icons )
11371141 {
1138- DestroyIcon (_iconSysTrayArray[i] );
1139- _iconSysTrayArray[i] = nullptr ;
1142+ DestroyIcon (icon );
1143+ icon = nullptr ;
11401144 }
11411145 }
1142- if (_sysTrayMenu != nullptr )
1146+ if (_sysTray. Menu != nullptr )
11431147 {
1144- ::DestroyMenu (_sysTrayMenu );
1145- _sysTrayMenu = nullptr ;
1148+ ::DestroyMenu (_sysTray.Menu );
1149+ _sysTray. Menu = nullptr ;
11461150 }
11471151 return OnExternalCloseMessage ();
11481152 }
@@ -1204,8 +1208,8 @@ void CProgressDialog::SetPauseText()
12041208 SetItemText (IDB_PAUSE , pszText);
12051209 SetTitleText ();
12061210
1207- if (_sysTrayMenu != nullptr )
1208- ModifyMenuW (_sysTrayMenu , IDB_PAUSE , MF_BYCOMMAND , IDB_PAUSE , pszText);
1211+ if (_sysTray. Menu != nullptr )
1212+ ModifyMenuW (_sysTray. Menu , IDB_PAUSE , MF_BYCOMMAND , IDB_PAUSE , pszText);
12091213}
12101214
12111215void CProgressDialog::OnPauseButton ()
@@ -1235,7 +1239,7 @@ void CProgressDialog::OnPriorityButton()
12351239 SetPriorityClass (GetCurrentProcess (), _background ? IDLE_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS );
12361240 if (!_background) // foreground
12371241 {
1238- if (_iconSysTrayArray [0 ] != nullptr )
1242+ if (_sysTray. Icons [0 ] != nullptr )
12391243 {
12401244 if (MainWindow != nullptr )
12411245 ShowWindow (MainWindow, SW_SHOW );
@@ -1245,12 +1249,12 @@ void CProgressDialog::OnPriorityButton()
12451249 }
12461250 else
12471251 {
1248- if (LoadSysTrayIcons ( ))
1252+ if (_sysTray. LoadIcons (g_hInstance, IDI_SYSTRAY_0 ))
12491253 {
12501254 if (MainWindow != nullptr )
12511255 ShowWindow (MainWindow, SW_HIDE );
12521256 Show (SW_HIDE );
1253- UpdateSysTrayIcon ( true , true );
1257+ _sysTray. UpdateIcon (_window, _prevPercentValue, true , true );
12541258 }
12551259 }
12561260 #endif
@@ -1316,7 +1320,7 @@ void CProgressDialog::UpdateMessagesDialog()
13161320 }
13171321 if (!messages.IsEmpty ())
13181322 {
1319- g_bProcessError = true ;
1323+ ProgressDialog_SetError ( true ) ;
13201324
13211325 FOR_VECTOR (i, messages)
13221326 AddMessage (messages[i]);
@@ -1499,67 +1503,76 @@ void CProgressDialog::CopyToClipboard()
14991503}
15001504
15011505
1502- bool CProgressDialog::LoadSysTrayIcons ( )
1506+ CSysTray::CSysTray () : IconArrayId(- 1 ), Menu( nullptr )
15031507{
1504- if (_iconSysTrayArray[0 ] != nullptr ) return true ;
1508+ for (auto &icon : Icons)
1509+ icon = nullptr ;
1510+ }
15051511
1506- int id = IDI_SYSTRAY_0 ;
1507- for (int n = 0 ; n < kNumTrayIcons ; n++, id++)
1512+ bool CSysTray::LoadIcons (HINSTANCE hInst, int firstResId)
1513+ {
1514+ if (Icons[0 ] != nullptr ) return true ;
1515+
1516+ int id = firstResId;
1517+ for (auto &icon : Icons)
15081518 {
1509- _iconSysTrayArray[n] = (HICON )LoadImage (g_hInstance, MAKEINTRESOURCE (id), IMAGE_ICON , 16 , 16 , LR_DEFAULTCOLOR );
1519+ icon = (HICON )LoadImage (hInst, MAKEINTRESOURCE (id), IMAGE_ICON , 16 , 16 , LR_DEFAULTCOLOR );
1520+ id++;
15101521 }
15111522
1512- return _iconSysTrayArray [0 ] != nullptr ;
1523+ return Icons [0 ] != nullptr ;
15131524}
15141525
1515- void CProgressDialog::UpdateSysTrayIcon ( bool addIcon, bool updateTip)
1526+ void CSysTray::UpdateIcon ( HWND dlg, UInt64 percentValue, bool addIcon, bool updateTip)
15161527{
1517- int newIconId = (_prevPercentValue / kTrayPercentPerIcon ) % kNumTrayIcons ;
1518- bool updateIcon = newIconId > _sysTrayIconArrayId ;
1528+ int newIconId = (int )((percentValue / kPercentPerIcon ) % kNumIcons ) ;
1529+ bool updateIcon = newIconId > IconArrayId ;
15191530
15201531 if (updateIcon || updateTip || addIcon)
15211532 {
1522- wchar_t tip[ 64 ] ;
1523- wchar_t *p = ConvertUInt64ToString (_prevPercentValue , tip);
1533+ std::array< wchar_t , 64 > tip ;
1534+ wchar_t *p = ConvertUInt64ToString (percentValue , tip. data () );
15241535 *p++ = L' %' ;
15251536 *p = 0 ;
15261537
15271538 UINT flags = NIF_TIP ;
15281539 if (updateIcon)
15291540 flags |= NIF_ICON ;
15301541
1531- if (!SetSysTray (_window , NIM_MODIFY , ID_SYSTRAY_ICON , flags, 0 , _iconSysTrayArray [newIconId], tip))
1542+ if (!SetSysTray (dlg , NIM_MODIFY , ID_SYSTRAY_ICON , flags, 0 , Icons [newIconId], tip. data () ))
15321543 {
1533- SetSysTray (_window , NIM_DELETE , ID_SYSTRAY_ICON , 0 , 0 , nullptr , nullptr );
1534- SetSysTray (_window , NIM_ADD , ID_SYSTRAY_ICON , NIF_ICON | NIF_MESSAGE | NIF_TIP , WM_TRAY_ICON_NOTIFY ,
1535- _iconSysTrayArray [newIconId], tip);
1544+ SetSysTray (dlg , NIM_DELETE , ID_SYSTRAY_ICON , 0 , 0 , nullptr , nullptr );
1545+ SetSysTray (dlg , NIM_ADD , ID_SYSTRAY_ICON , NIF_ICON | NIF_MESSAGE | NIF_TIP , WM_TRAY_ICON_NOTIFY ,
1546+ Icons [newIconId], tip. data () );
15361547 }
15371548
1538- _sysTrayIconArrayId = newIconId;
1549+ IconArrayId = newIconId;
15391550 }
15401551}
15411552
1542- bool CProgressDialog::CreateSysTrayMenu ( )
1553+ bool CSysTray::BuildPopupMenu ( LPCWSTR foregroundText, LPCWSTR pauseOrContinueText, LPCWSTR cancelText )
15431554{
1544- if (_sysTrayMenu != nullptr ) return true ;
1555+ if (Menu != nullptr ) return true ;
15451556
1546- _sysTrayMenu = CreatePopupMenu ();
1547- AppendMenuW (_sysTrayMenu , MF_STRING , IDB_PROGRESS_BACKGROUND , _foreground_String );
1548- AppendMenuW (_sysTrayMenu , MF_STRING , IDB_PAUSE , Sync. Get_Paused () ? _continue_String : _pause_String );
1549- AppendMenuW (_sysTrayMenu , MF_STRING , IDCANCEL , cancelString );
1550- return _sysTrayMenu != nullptr ;
1557+ Menu = CreatePopupMenu ();
1558+ AppendMenuW (Menu , MF_STRING , IDB_PROGRESS_BACKGROUND , foregroundText );
1559+ AppendMenuW (Menu , MF_STRING , IDB_PAUSE , pauseOrContinueText );
1560+ AppendMenuW (Menu , MF_STRING , IDCANCEL , cancelText );
1561+ return Menu != nullptr ;
15511562}
15521563
15531564bool CProgressDialog::OnTrayNotification (LPARAM lParam)
15541565{
15551566 switch (lParam)
15561567 {
15571568 case WM_RBUTTONUP :
1558- if (CreateSysTrayMenu ())
1569+ if (_sysTray.BuildPopupMenu (_foreground_String,
1570+ Sync.Get_Paused () ? _continue_String : _pause_String,
1571+ cancelString))
15591572 {
15601573 POINT point;
15611574 ::GetCursorPos (&point);
1562- ::TrackPopupMenu (_sysTrayMenu , TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON , point.x, point.y, 0 , _window, nullptr );
1575+ ::TrackPopupMenu (_sysTray.Menu , TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON , point.x, point.y, 0 , _window, nullptr );
15631576 // BUGFIX: see "PRB: Menus for Notification Icons Don't Work Correctly"
15641577 ::PostMessage (_window, WM_NULL , 0 , 0 );
15651578 }
0 commit comments