| 1 |
/* |
| 2 |
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>. |
| 3 |
* It is copyright by its individual contributors, as recorded in the |
| 4 |
* project's Git history. See COPYING.txt at the top level for license |
| 5 |
* terms and a link to the Git history. |
| 6 |
*/ |
| 7 |
/* |
| 8 |
* A 'window' is simply a canvas that can receive events. |
| 9 |
* It can be anything from a simple message box to the |
| 10 |
* game screen when playing. |
| 11 |
* |
| 12 |
* See event.c for event handling code. |
| 13 |
* |
| 14 |
* -kreator 2009-05-06 |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "gr.h" |
| 18 |
#include "window.h" |
| 19 |
#include "u_mem.h" |
| 20 |
#include "console.h" |
| 21 |
#include "dxxerror.h" |
| 22 |
#include "event.h" |
| 23 |
#include "d_underlying_value.h" |
| 24 |
|
| 25 |
namespace dcx { |
| 26 |
|
| 27 |
static window *FrontWindow = nullptr; |
| 28 |
static window *FirstWindow = nullptr; |
| 29 |
|
| 30 |
window::window(grs_canvas &src, const uint16_t x, const uint16_t y, const uint16_t w, const uint16_t h) : |
| 31 |
// Default to visible and modal |
| 32 |
prev{FrontWindow} |
| 33 |
{ |
| 34 |
gr_init_sub_canvas(w_canv, src, {x}, {y}, {w}, {h}); |
| 35 |
|
| 36 |
if (FirstWindow == nullptr) |
| 37 |
FirstWindow = this; |
| 38 |
} |
| 39 |
|
| 40 |
void window::send_creation_events() |
| 41 |
{ |
| 42 |
const auto prev_front = window_get_front(); |
| 43 |
if (FrontWindow) |
| 44 |
FrontWindow->next = this; |
| 45 |
FrontWindow = this; |
| 46 |
if (prev_front) |
| 47 |
prev_front->send_event(d_event{event_type::window_deactivated}); |
| 48 |
this->send_event(d_create_event{}); |
| 49 |
this->send_event(d_event{event_type::window_activated}); |
| 50 |
} |
| 51 |
|
| 52 |
window::~window() |
| 53 |
{ |
| 54 |
if (this == FrontWindow) |
| 55 |
FrontWindow = this->prev; |
| 56 |
if (this == FirstWindow) |
| 57 |
FirstWindow = this->next; |
| 58 |
if (this->next) |
| 59 |
this->next->prev = this->prev; |
| 60 |
if (this->prev) |
| 61 |
this->prev->next = this->next; |
| 62 |
} |
| 63 |
|
| 64 |
mixin_trackable_window::~mixin_trackable_window() |
| 65 |
{ |
| 66 |
if (exists) |
| 67 |
*exists = false; |
| 68 |
} |
| 69 |
|
| 70 |
int window_close(window *wind) |
| 71 |
{ |
| 72 |
if (wind == window_get_front()) |
| 73 |
wind->send_event(d_event{event_type::window_deactivated}); // Deactivate first |
| 74 |
|
| 75 |
const auto result = wind->send_event(d_event{event_type::window_close}); |
| 76 |
if (result == window_event_result::handled) |
| 77 |
{ |
| 78 |
// User 'handled' the event, cancelling close |
| 79 |
if (wind == window_get_front()) |
| 80 |
wind->send_event(d_event{event_type::window_activated}); |
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
menu_destroy_hook(wind); |
| 85 |
|
| 86 |
if (result != window_event_result::deleted) // don't attempt to re-delete |
| 87 |
delete wind; |
| 88 |
|
| 89 |
if (const auto prev = window_get_front()) |
| 90 |
prev->send_event(d_event{event_type::window_activated}); |
| 91 |
|
| 92 |
return 1; |
| 93 |
} |
| 94 |
|
| 95 |
// Get the top window that's visible |
| 96 |
window *window_get_front(void) |
| 97 |
{ |
| 98 |
window *wind; |
| 99 |
|
| 100 |
for (wind = FrontWindow; (wind != NULL) && !wind->w_visible; wind = wind->prev) {} |
| 101 |
|
| 102 |
return wind; |
| 103 |
} |
| 104 |
|
| 105 |
window *window_get_first(void) |
| 106 |
{ |
| 107 |
return FirstWindow; |
| 108 |
} |
| 109 |
|
| 110 |
// Make wind the front window |
| 111 |
void window_select(window &wind) |
| 112 |
{ |
| 113 |
window *prev = window_get_front(); |
| 114 |
if (&wind == FrontWindow) |
| 115 |
return; |
| 116 |
if (&wind == FirstWindow && wind.next) |
| 117 |
FirstWindow = FirstWindow->next; |
| 118 |
|
| 119 |
if (wind.next) |
| 120 |
wind.next->prev = wind.prev; |
| 121 |
if (wind.prev) |
| 122 |
wind.prev->next = wind.next; |
| 123 |
wind.prev = FrontWindow; |
| 124 |
FrontWindow->next = &wind; |
| 125 |
wind.next = nullptr; |
| 126 |
FrontWindow = &wind; |
| 127 |
|
| 128 |
if (wind.is_visible()) |
| 129 |
{ |
| 130 |
if (prev) |
| 131 |
prev->send_event(d_event{event_type::window_deactivated}); |
| 132 |
wind.send_event(d_event{event_type::window_activated}); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
window *window::set_visible(uint8_t visible) |
| 137 |
{ |
| 138 |
window *prev = window_get_front(); |
| 139 |
w_visible = visible; |
| 140 |
auto wind = window_get_front(); // get the new front window |
| 141 |
if (wind == prev) |
| 142 |
return wind; |
| 143 |
|
| 144 |
if (prev) |
| 145 |
prev->send_event(d_event{event_type::window_deactivated}); |
| 146 |
|
| 147 |
if (wind) |
| 148 |
wind->send_event(d_event{event_type::window_activated}); |
| 149 |
return wind; |
| 150 |
} |
| 151 |
|
| 152 |
window_event_result window::send_event(const d_event &event |
| 153 |
#if DXX_HAVE_CXX_BUILTIN_FILE_LINE |
| 154 |
, const char *const file, const unsigned line |
| 155 |
#endif |
| 156 |
) |
| 157 |
{ |
| 158 |
#if DXX_HAVE_CXX_BUILTIN_FILE_LINE |
| 159 |
con_printf(CON_DEBUG, "%s:%u: sending event %i to window of dimensions %dx%d", file, line, underlying_value(event.type), w_canv.cv_bitmap.bm_w, w_canv.cv_bitmap.bm_h); |
| 160 |
#endif |
| 161 |
const auto r = event_handler(event); |
| 162 |
if (r == window_event_result::close) |
| 163 |
if (window_close(this)) |
| 164 |
return window_event_result::deleted; |
| 165 |
return r; |
| 166 |
} |
| 167 |
|
| 168 |
#if !DXX_USE_OGL |
| 169 |
void window_update_canvases() |
| 170 |
{ |
| 171 |
window *wind; |
| 172 |
|
| 173 |
for (wind = FirstWindow; wind != NULL; wind = wind->next) |
| 174 |
gr_init_sub_bitmap (wind->w_canv.cv_bitmap, |
| 175 |
*wind->w_canv.cv_bitmap.bm_parent, |
| 176 |
wind->w_canv.cv_bitmap.bm_x, |
| 177 |
wind->w_canv.cv_bitmap.bm_y, |
| 178 |
wind->w_canv.cv_bitmap.bm_w, |
| 179 |
wind->w_canv.cv_bitmap.bm_h); |
| 180 |
} |
| 181 |
#endif |
| 182 |
|
| 183 |
} |