The Battle for Wesnoth  1.19.7+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 // ShellAPI.h should be included after Windows.h only!
25 #include <windows.h>
26 #include <shellapi.h>
27 
29 public:
30  /**
31  * Displays a tray notification.
32  * When user clicks on the notification popup, the user switches to the wesnoth window.
33  *
34  * @param title Title of a notification. Gets truncated if longer than 64 characters, including
35  * the terminating null character.
36  * @param message Message of a notification. Gets truncated if longer than 256 characters, including
37  * the terminating null character.
38  *
39  * @return True if message was shown successfully, False otherwise.
40  */
41  static bool show(std::string title, std::string message);
42 
43  /**
44  * Frees resources when a notification disappears, switches user to the wesnoth
45  * window if the notification popup was clicked by user.
46  *
47  * @param event System event.
48  */
49  static void handle_system_event(const SDL_Event& event);
50 
51 private:
52  static NOTIFYICONDATA* nid;
53  static bool message_reset;
54  static const int ICON_ID = 1007; // just a random number
55  static const unsigned int WM_TRAYNOTIFY = 32868; // WM_APP+100
56  static const std::size_t MAX_TITLE_LENGTH = 63; // 64 including the terminating null character
57  static const std::size_t MAX_MESSAGE_LENGTH = 255; // 256 including the terminating null character
58 
59  static bool create_tray_icon();
60  static void destroy_tray_icon();
61  static bool set_tray_message(const std::string& title, const std::string& message);
62  static void adjust_length(std::string& title, std::string& message);
63  static HWND get_window_handle();
64  static void switch_to_wesnoth_window();
65  static std::wstring string_to_wstring(const std::string& string, std::size_t maxlength);
66 
70 };
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