view win/wintoast.cpp @ 2264:5c981407b0f3

GTK4: Add experimental support for GTK4... This is in progress and doesn't compile yet. GTK4 support will likely be less full than GTK3 due to architectural decisions by the GTK team. Certain features have been removed with no replacements, such as status icon support. Additionally all of the thread safety has been removed and everything MUST run on the main thread. I will likely implement a similar solution to what I did on MacOS where threads will call into the main thread to do API calls, passing the arguments between threads. Copied from GTK3 removing all existing now dead thread safety, all deprecated code and everything supporting earlier versions of GTK3. GTK4 is not available in any mainstream distributions yet... hoping to have it at least functional by the time there are any.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 29 Jan 2021 10:57:12 +0000
parents 663467f6eee4
children 2e804b4db81e
line wrap: on
line source

/* Simple WinToast forwarder from Dynamic Windows APIs */

#include "dw.h"
#include "wintoastlib.h"

using namespace WinToastLib;

extern "C" {
   LRESULT CALLBACK _wndproc(HWND hWnd, UINT msg, WPARAM mp1, LPARAM mp2);
}

class DWHandler : public IWinToastHandler {
public:
    WinToastTemplate *templ;

    void toastActivated() const {
        // The user clicked in this toast
        _wndproc((HWND)templ, WM_USER+102, 0, 0);
        dw_signal_disconnect_by_window((HWND)templ);
        delete templ;
    }

    void toastActivated(int actionIndex) const {
        // The user clicked on action
        _wndproc((HWND)templ, WM_USER+102, 0, 0);
        dw_signal_disconnect_by_window((HWND)templ);
        delete templ;
    }

    void toastDismissed(WinToastDismissalReason state) const {
        switch (state) {
        case UserCanceled:
            // The user dismissed this toast
            dw_signal_disconnect_by_window((HWND)templ);
            delete templ;
            break;
        case TimedOut:
            // The toast has timed out
            break;
        case ApplicationHidden:
            // The application hid the toast using ToastNotifier.hide()
            break;
        default:
            // Toast not activated
            break;
        }
    }

    void toastFailed() const {
        // Error showing current toast
        delete templ;
    }
};


enum Results {
	ToastClicked,					// user clicked on the toast
	ToastDismissed,					// user dismissed the toast
	ToastTimeOut,					// toast timed out
	ToastHided,						// application hid the toast
	ToastNotActivated,				// toast was not activated
	ToastFailed,					// toast failed
	SystemNotSupported,				// system does not support toasts
	UnhandledOption,				// unhandled option
	MultipleTextNotSupported,		// multiple texts were provided
	InitializationFailure,			// toast notification manager initialization failure
	ToastNotLaunched				// toast could not be launched
};

extern "C" {
   
   void _dw_toast_init(LPWSTR AppName, LPWSTR AppID)
   {
      if(WinToast::isCompatible()) 
      {
         // Generate a Microsoft compatible Application User Model ID
         LPWSTR company = wcschr(AppID, '.');
         *company = 0;
         LPWSTR product = wcschr(++company, '.');
         *product = 0;
         LPWSTR subproduct = wcschr(++product, '.');
         if(subproduct)
                *subproduct = 0;
         LPWSTR version = subproduct ? wcschr(++subproduct, '.') : NULL;

         WinToast::instance()->setAppName(AppName);
         WinToast::instance()->setAppUserModelId(WinToast::instance()->configureAUMI(company, product, 
             subproduct ? subproduct : L"", (version && version++ && _wtoi(version) > 0) ? version : L""));
         WinToast::instance()->initialize();
      }
   }

   void *_dw_notification_new(LPWSTR title, LPWSTR image, LPWSTR description)
   {
      if(WinToast::isCompatible()) 
      {
         WinToastTemplate *templ = new WinToastTemplate(image ? WinToastTemplate::ImageAndText02 : WinToastTemplate::Text02);
         templ->setTextField(title, WinToastTemplate::FirstLine);
         templ->setAttributionText(description);
         if(image)
         {
            WCHAR fullpath[MAX_PATH+1] = {0};
            
            GetFullPathNameW(image, MAX_PATH, fullpath, NULL);
            templ->setImagePath(fullpath);
         }
         return (void *)templ;
      }
      return NULL;
   }

   int _dw_notification_send(void *notification)
   {
      if(WinToast::isCompatible()) 
      {
         WinToastTemplate *templ = (WinToastTemplate *)notification;
         DWHandler *handler = new DWHandler();
         handler->templ = templ;

         if(templ && WinToast::instance()->showToast(*templ, handler) >= 0)
            return DW_ERROR_NONE;
      }
      return DW_ERROR_UNKNOWN;
   }

   BOOL _dw_toast_is_compatible(void)
   {
       return WinToast::isCompatible();
   }
}