CMainWindow.cpp
author Volker Birk <vb@pep.foundation>
Fri, 03 Jul 2020 10:55:44 +0200
branchCOM-115
changeset 434 bf1832802558
parent 429 2e8e6705baa0
child 436 6d239d38f5ce
permissions -rw-r--r--
use string table
     1 #include "stdafx.h"
     2 #include "CMainWindow.h"
     3 #include "GateKeeper.h"
     4 
     5 static const GUID nidGUID =
     6 { 0xa4dbdbe1, 0x4051, 0x4d89, { 0xb1, 0x17, 0x62, 0x82, 0x18, 0x5a, 0x61, 0x5c } };
     7 
     8 LRESULT CMainWindow::OnCreate(UINT, WPARAM, LPARAM, BOOL&)
     9 {
    10     // remove leftoff before creating a new one
    11     BOOL _b;
    12     OnDestroy(0, 0, 0, _b);
    13 
    14     NOTIFYICONDATA nid = {};
    15     nid.cbSize = sizeof(nid);
    16     nid.uFlags = NIF_ICON | NIF_TIP | NIF_GUID | NIF_MESSAGE;
    17     nid.hWnd = m_hWnd;
    18     nid.guidItem = nidGUID;
    19     StringCchCopy(nid.szTip, ARRAYSIZE(nid.szTip), r(IDS_PROJNAME).c_str());
    20     nid.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_LOGO));
    21     nid.uCallbackMessage = WM_PEP_NOTIFICATION;
    22     Shell_NotifyIcon(NIM_ADD, &nid);
    23 
    24     nid = {};
    25     nid.cbSize = sizeof(nid);
    26     nid.uVersion = NOTIFYICON_VERSION_4;
    27     Shell_NotifyIcon(NIM_SETVERSION, &nid);
    28 
    29     return S_OK;
    30 }
    31 
    32 LRESULT CMainWindow::OnDestroy(UINT, WPARAM, LPARAM, BOOL&)
    33 {
    34     NOTIFYICONDATA nid = {};
    35     nid.cbSize = sizeof(nid);
    36     nid.uFlags = NIF_ICON | NIF_GUID;
    37     nid.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_LOGO));
    38     nid.guidItem = nidGUID;
    39     Shell_NotifyIcon(NIM_DELETE, &nid);
    40 
    41     return S_OK;
    42 }
    43 
    44 LRESULT CMainWindow::OnNotification(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    45 {
    46     auto event = LOWORD(lParam);
    47     auto icon_id = HIWORD(lParam);
    48     auto x = GET_X_LPARAM(wParam);
    49     auto y = GET_Y_LPARAM(wParam);
    50 
    51     HMENU menuContext;
    52     HMENU _menuContext = NULL;
    53 
    54     MENUINFO info;
    55     BOOL enabled;
    56 
    57     switch (event) {
    58     case WM_CONTEXTMENU:
    59     case WM_RBUTTONDOWN:
    60         menuContext = LoadMenu(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_POPUPMENU));
    61         info = {};
    62         info.cbSize = sizeof(info);
    63         info.fMask = MIM_APPLYTOSUBMENUS | MIM_STYLE;
    64         info.dwStyle = MNS_AUTODISMISS | MNS_NOTIFYBYPOS;
    65         SetMenuInfo(menuContext, &info);
    66         _menuContext = GetSubMenu(menuContext, 0);
    67 
    68         enabled = pEp::GateKeeper::gatekeeper()->update_enabled();
    69         CheckMenuItem(_menuContext, ID_POPUP_SCHEDULEUPDATES, enabled ? MF_CHECKED : MF_UNCHECKED);
    70 
    71         SetForegroundWindow(m_hWnd); // this is utter nonsense, but required by TrackPopupMenuEx
    72         POINT point;
    73         GetCursorPos(&point);
    74         TrackPopupMenuEx(_menuContext, TPM_LEFTALIGN | TPM_BOTTOMALIGN, point.x, point.y, m_hWnd, NULL);
    75         PostMessage(WM_NULL, 0, 0); // this is utter nonsense, but required by TrackPopupMenuEx
    76 
    77         DestroyMenu(menuContext);
    78         bHandled = true;
    79         break;
    80 
    81     default:
    82         bHandled = false;
    83     }
    84 
    85     return S_OK;
    86 }
    87 
    88 static const auto UPDATE_NOW = 0;
    89 static const auto SCHEDULE_UPDATES = 2;
    90 
    91 LRESULT CMainWindow::OnMenuCommand(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    92 {
    93     auto index = wParam;
    94     HMENU hMenu = (HMENU)lParam;
    95     BOOL enabled;
    96 
    97     switch (index) {
    98     case UPDATE_NOW:
    99         ShowNotificationInfo(r(IDS_UPDATESTITLE), r(IDS_UPDATESTEXT));
   100         pEp::GateKeeper::gatekeeper()->update_now();
   101         bHandled = true;
   102         break;
   103 
   104     case SCHEDULE_UPDATES:
   105         enabled = !pEp::GateKeeper::gatekeeper()->update_enabled();
   106         if (enabled)
   107             pEp::GateKeeper::gatekeeper()->enable_update();
   108         else
   109             pEp::GateKeeper::gatekeeper()->disable_update();
   110         CheckMenuItem(hMenu, ID_POPUP_SCHEDULEUPDATES, enabled ? MF_CHECKED : MF_UNCHECKED);
   111         bHandled = true;
   112         break;
   113 
   114     default:
   115         bHandled = false;
   116     }
   117     return S_OK;
   118 }
   119 
   120 CMainWindow::tstring CMainWindow::r(UINT resid)
   121 {
   122     LPCTSTR rstr;
   123     size_t rsize = LoadString(GetModuleHandle(NULL), resid, (LPWSTR) &rstr, 0);
   124     tstring str = tstring(rstr, rsize);
   125     return str;
   126 }
   127 
   128 void CMainWindow::ShowNotificationInfo(tstring title, tstring text)
   129 {
   130     NOTIFYICONDATA nid = {};
   131     nid.cbSize = sizeof(nid);
   132     nid.uFlags = NIF_GUID | NIF_MESSAGE | NIF_INFO;
   133     nid.dwInfoFlags = NIIF_LARGE_ICON;
   134     nid.hWnd = m_hWnd;
   135     nid.guidItem = nidGUID;
   136     nid.hBalloonIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_LOGO));
   137     StringCchCopy(nid.szInfoTitle, ARRAYSIZE(nid.szInfoTitle), title.substr(0, ARRAYSIZE(nid.szInfoTitle)-1).c_str());
   138     StringCchCopy(nid.szInfo, ARRAYSIZE(nid.szInfo), text.substr(0, ARRAYSIZE(nid.szInfo) - 1).c_str());
   139     nid.uCallbackMessage = WM_PEP_NOTIFICATION;
   140     Shell_NotifyIcon(NIM_MODIFY, &nid);
   141 }