The Battle for Wesnoth  1.19.0-dev
windows_tray_notification.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 2024
3  by Maxim Biro <nurupo.contributions@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include <SDL2/SDL.h>
19 #include <string>
20 //forces to call Unicode winapi functions instead of ASCII (default)
21 #ifndef UNICODE
22 #define UNICODE
23 #endif
24 //defines that mingw misses
25 #ifndef _WIN32_IE
26  #define _WIN32_IE _WIN32_WINNT_WIN7 //specifying target platform to be Windows 7 and higher
27 #endif
28 #ifndef NIIF_USER
29  #define NIIF_USER 0x00000004
30 #endif
31 #ifndef NIN_BALLOONTIMEOUT
32  #define NIN_BALLOONTIMEOUT (WM_USER + 4)
33 #endif
34 #ifndef NIN_BALLOONUSERCLICK
35  #define NIN_BALLOONUSERCLICK (WM_USER + 5)
36 #endif
37 // ShellAPI.h should be included after Windows.h only!
38 #include <windows.h>
39 #include <shellapi.h>
40 
42 public:
43  /**
44  * Displays a tray notification.
45  * When user clicks on the notification popup, the user switches to the wesnoth window.
46  *
47  * @param title Title of a notification. Gets truncated if longer than 64 characters, including
48  * the terminating null character.
49  * @param message Message of a notification. Gets truncated if longer than 256 characters, including
50  * the terminating null character.
51  *
52  * @return True if message was shown successfully, False otherwise.
53  */
54  static bool show(std::string title, std::string message);
55 
56  /**
57  * Frees resources when a notification disappears, switches user to the wesnoth
58  * window if the notification popup was clicked by user.
59  *
60  * @param event System event.
61  */
62  static void handle_system_event(const SDL_Event& event);
63 
64 private:
65  static NOTIFYICONDATA* nid;
66  static bool message_reset;
67  static const int ICON_ID = 1007; // just a random number
68  static const unsigned int WM_TRAYNOTIFY = 32868; // WM_APP+100
69  static const std::size_t MAX_TITLE_LENGTH = 63; // 64 including the terminating null character
70  static const std::size_t MAX_MESSAGE_LENGTH = 255; // 256 including the terminating null character
71 
72  static bool create_tray_icon();
73  static void destroy_tray_icon();
74  static bool set_tray_message(const std::string& title, const std::string& message);
75  static void adjust_length(std::string& title, std::string& message);
76  static HWND get_window_handle();
77  static void switch_to_wesnoth_window();
78  static std::wstring string_to_wstring(const std::string& string, std::size_t maxlength);
79 
83 };
static bool set_tray_message(const std::string &title, const std::string &message)
static bool show(std::string title, std::string message)
Displays a tray notification.
static const unsigned int WM_TRAYNOTIFY
static std::wstring string_to_wstring(const std::string &string, std::size_t maxlength)
static const std::size_t MAX_MESSAGE_LENGTH
static void handle_system_event(const SDL_Event &event)
Frees resources when a notification disappears, switches user to the wesnoth window if the notificati...
windows_tray_notification(const windows_tray_notification &w)
static void adjust_length(std::string &title, std::string &message)
static const std::size_t MAX_TITLE_LENGTH
windows_tray_notification & operator=(const windows_tray_notification &w)
int w