The Battle for Wesnoth  1.19.1+dev
mp_alerts_options.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
3  by Chris Beck <render787@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 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
22 #include "gui/widgets/button.hpp"
23 #include "gui/widgets/label.hpp"
25 #include "gui/widgets/window.hpp"
26 
27 #include "mp_ui_alerts.hpp"
29 
30 #include <functional>
31 
32 #include "gettext.hpp"
33 
34 namespace gui2::dialogs
35 {
36 
37 /**
38  * Sets up the toggle buttons for a set of three MP lobby alerts
39  *
40  * @param pref_item_sound prefs_list constant for the sound preference
41  * @param pref_item_notif prefs_list constant for the notification preference
42  * @param pref_item_lobby prefs_list constant for the lobby preference
43  */
44 #define SETUP_ITEMS(pref_item_sound, pref_item_notif, pref_item_lobby) \
45 { \
46 toggle_button* sound = find_widget<toggle_button>(&window, prefs_list::pref_item_sound, false, true); \
47 sound->set_value(prefs::get().pref_item_sound()); \
48 connect_signal_mouse_left_click(*sound, std::bind([sound]() { prefs::get().set_##pref_item_sound(sound->get_value_bool()); })); \
49 \
50 toggle_button* notif = find_widget<toggle_button>(&window, prefs_list::pref_item_notif, false, true); \
51 \
52 if (!desktop::notifications::available()) { \
53  notif->set_value(false); \
54  notif->set_active(false); \
55  prefs::get().set_##pref_item_notif(false); \
56 } else { \
57  notif->set_active(true); \
58  notif->set_value(prefs::get().pref_item_notif()); \
59  connect_signal_mouse_left_click(*notif, std::bind([notif]() { prefs::get().set_##pref_item_notif(notif->get_value_bool()); }));\
60 } \
61 \
62 toggle_button* lobby = find_widget<toggle_button>(&window, prefs_list::pref_item_lobby, false, true); \
63 lobby->set_value(prefs::get().pref_item_lobby()); \
64 connect_signal_mouse_left_click(*lobby, std::bind([lobby]() { prefs::get().set_##pref_item_lobby(lobby->get_value_bool()); })); \
65 }
66 
67 /**
68  * Set the defaults on the UI after clearing the preferences.
69  *
70  * @param pref_item_sound prefs_list constant for the sound preference
71  * @param pref_item_notif prefs_list constant for the notification preference
72  * @param pref_item_lobby prefs_list constant for the lobby preference
73  */
74 #define RESET_DEFAULT(pref_item_sound, pref_item_notif, pref_item_lobby) \
75 find_widget<toggle_button>(&window, prefs_list::pref_item_sound, false, true)->set_value(prefs::get().pref_item_sound()); \
76 find_widget<toggle_button>(&window, prefs_list::pref_item_notif, false, true)->set_value(prefs::get().pref_item_notif()); \
77 find_widget<toggle_button>(&window, prefs_list::pref_item_lobby, false, true)->set_value(prefs::get().pref_item_lobby());
78 
79 
81 {
82  // clear existing preferences for MP alerts
84 
85  // each preference knows its own default, so after clearing you get the default by just using the getter
86  RESET_DEFAULT(player_joins_sound, player_joins_notif, player_joins_lobby)
87  RESET_DEFAULT(player_leaves_sound, player_leaves_notif, player_leaves_lobby)
88  RESET_DEFAULT(private_message_sound, private_message_notif, private_message_lobby)
89  RESET_DEFAULT(friend_message_sound, friend_message_notif, friend_message_lobby)
90  RESET_DEFAULT(public_message_sound, public_message_notif, public_message_lobby)
91  RESET_DEFAULT(server_message_sound, server_message_notif, server_message_lobby)
92  RESET_DEFAULT(ready_for_start_sound, ready_for_start_notif, ready_for_start_lobby)
93  RESET_DEFAULT(game_has_begun_sound, game_has_begun_notif, game_has_begun_lobby)
94  RESET_DEFAULT(turn_changed_notif, turn_changed_notif, turn_changed_lobby)
95  RESET_DEFAULT(game_created_sound, game_created_notif, game_created_lobby)
96 }
97 
99 
101  : modal_dialog(window_id())
102 {
103 }
104 
106 {
107  SETUP_ITEMS(player_joins_sound, player_joins_notif, player_joins_lobby)
108  SETUP_ITEMS(player_leaves_sound, player_leaves_notif, player_leaves_lobby)
109  SETUP_ITEMS(private_message_sound, private_message_notif, private_message_lobby)
110  SETUP_ITEMS(friend_message_sound, friend_message_notif, friend_message_lobby)
111  SETUP_ITEMS(public_message_sound, public_message_notif, public_message_lobby)
112  SETUP_ITEMS(server_message_sound, server_message_notif, server_message_lobby)
113  SETUP_ITEMS(ready_for_start_sound, ready_for_start_notif, ready_for_start_lobby)
114  SETUP_ITEMS(game_has_begun_sound, game_has_begun_notif, game_has_begun_lobby)
115  SETUP_ITEMS(turn_changed_notif, turn_changed_notif, turn_changed_lobby)
116  SETUP_ITEMS(game_created_sound, game_created_notif, game_created_lobby)
117 
119  label* nlabel = find_widget<label>(&window, "notification_label", false, true);
120  nlabel->set_tooltip(_("This build of wesnoth does not include support for desktop notifications, contact your package manager"));
121  }
122 
123  toggle_button* in_lobby;
124  in_lobby = find_widget<toggle_button>(&window, prefs_list::ready_for_start_lobby, false, true);
126 
127  in_lobby = find_widget<toggle_button>(&window, prefs_list::game_has_begun_lobby, false, true);
129 
130  in_lobby = find_widget<toggle_button>(&window, prefs_list::turn_changed_sound, false, true); // If we get a sound for this then don't remove this button
132 
133  in_lobby = find_widget<toggle_button>(&window, prefs_list::turn_changed_lobby, false, true);
135 
136  button* defaults;
137  defaults = find_widget<button>(&window,"revert_to_defaults", false, true);
138  connect_signal_mouse_left_click(*defaults, std::bind(&revert_to_default_pref_values, std::ref(window)));
139 }
140 
142 {
143 }
144 
145 } // namespace dialogs
Simple push button.
Definition: button.hpp:36
Abstract base class for all modal dialogs.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
void set_tooltip(const t_string &tooltip)
void set_visible(const visibility visible)
Definition: widget.cpp:469
@ invisible
The user set the widget invisible, that means:
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:61
void clear_mp_alert_prefs()
static prefs & get()
static std::string _(const char *str)
Definition: gettext.hpp:93
This file contains the window object, this object is a top level container which has the event manage...
#define RESET_DEFAULT(pref_item_sound, pref_item_notif, pref_item_lobby)
Set the defaults on the UI after clearing the preferences.
#define SETUP_ITEMS(pref_item_sound, pref_item_notif, pref_item_lobby)
Sets up the toggle buttons for a set of three MP lobby alerts.
bool available()
Returns whether we were compiled with support for desktop notifications.
static void revert_to_default_pref_values(window &window)
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177