summaryrefslogtreecommitdiff
path: root/Servers/WindowServer/WSWindowFrame.cpp
blob: d82f0d83ba869095225b57eeada5b4cec3483747 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <LibGfx/CharacterBitmap.h>
#include <LibGfx/Font.h>
#include <LibGfx/Painter.h>
#include <LibGfx/StylePainter.h>
#include <WindowServer/WSButton.h>
#include <WindowServer/WSCompositor.h>
#include <WindowServer/WSEvent.h>
#include <WindowServer/WSWindow.h>
#include <WindowServer/WSWindowFrame.h>
#include <WindowServer/WSWindowManager.h>

static const int window_titlebar_height = 19;

static const char* s_close_button_bitmap_data = {
    "##    ##"
    "###  ###"
    " ###### "
    "  ####  "
    "   ##   "
    "  ####  "
    " ###### "
    "###  ###"
    "##    ##"
};

static Gfx::CharacterBitmap* s_close_button_bitmap;
static const int s_close_button_bitmap_width = 8;
static const int s_close_button_bitmap_height = 9;

static const char* s_minimize_button_bitmap_data = {
    "        "
    "        "
    "        "
    " ###### "
    "  ####  "
    "   ##   "
    "        "
    "        "
    "        "
};

static Gfx::CharacterBitmap* s_minimize_button_bitmap;
static const int s_minimize_button_bitmap_width = 8;
static const int s_minimize_button_bitmap_height = 9;

static const char* s_maximize_button_bitmap_data = {
    "        "
    "        "
    "        "
    "   ##   "
    "  ####  "
    " ###### "
    "        "
    "        "
    "        "
};

static Gfx::CharacterBitmap* s_maximize_button_bitmap;
static const int s_maximize_button_bitmap_width = 8;
static const int s_maximize_button_bitmap_height = 9;

static const char* s_unmaximize_button_bitmap_data = {
    "        "
    "   ##   "
    "  ####  "
    " ###### "
    "        "
    " ###### "
    "  ####  "
    "   ##   "
    "        "
};

static Gfx::CharacterBitmap* s_unmaximize_button_bitmap;
static const int s_unmaximize_button_bitmap_width = 8;
static const int s_unmaximize_button_bitmap_height = 9;

WSWindowFrame::WSWindowFrame(WSWindow& window)
    : m_window(window)
{
    if (!s_close_button_bitmap)
        s_close_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref();

    if (!s_minimize_button_bitmap)
        s_minimize_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_minimize_button_bitmap_data, s_minimize_button_bitmap_width, s_minimize_button_bitmap_height).leak_ref();

    if (!s_maximize_button_bitmap)
        s_maximize_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_maximize_button_bitmap_data, s_maximize_button_bitmap_width, s_maximize_button_bitmap_height).leak_ref();

    if (!s_unmaximize_button_bitmap)
        s_unmaximize_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_unmaximize_button_bitmap_data, s_unmaximize_button_bitmap_width, s_unmaximize_button_bitmap_height).leak_ref();

    m_buttons.append(make<WSButton>(*this, *s_close_button_bitmap, [this](auto&) {
        m_window.request_close();
    }));

    if (window.is_resizable()) {
        auto button = make<WSButton>(*this, *s_maximize_button_bitmap, [this](auto&) {
            m_window.set_maximized(!m_window.is_maximized());
        });
        m_maximize_button = button.ptr();
        m_buttons.append(move(button));
    }

    if (window.is_minimizable()) {
        auto button = make<WSButton>(*this, *s_minimize_button_bitmap, [this](auto&) {
            m_window.set_minimized(true);
        });
        m_minimize_button = button.ptr();
        m_buttons.append(move(button));
    }
}

WSWindowFrame::~WSWindowFrame()
{
}

void WSWindowFrame::did_set_maximized(Badge<WSWindow>, bool maximized)
{
    ASSERT(m_maximize_button);
    m_maximize_button->set_bitmap(maximized ? *s_unmaximize_button_bitmap : *s_maximize_button_bitmap);
}

Gfx::Rect WSWindowFrame::title_bar_rect() const
{
    return { 3, 3, m_window.width(), window_titlebar_height };
}

Gfx::Rect WSWindowFrame::title_bar_icon_rect() const
{
    auto titlebar_rect = title_bar_rect();
    return {
        titlebar_rect.x() + 1,
        titlebar_rect.y() + 2,
        16,
        titlebar_rect.height(),
    };
}

Gfx::Rect WSWindowFrame::title_bar_text_rect() const
{
    auto titlebar_rect = title_bar_rect();
    auto titlebar_icon_rect = title_bar_icon_rect();
    return {
        titlebar_rect.x() + 2 + titlebar_icon_rect.width() + 2,
        titlebar_rect.y(),
        titlebar_rect.width() - 4 - titlebar_icon_rect.width() - 2,
        titlebar_rect.height()
    };
}

void WSWindowFrame::paint(Gfx::Painter& painter)
{
    Gfx::PainterStateSaver saver(painter);
    painter.translate(rect().location());

    if (m_window.type() != WSWindowType::Normal)
        return;

    auto palette = WSWindowManager::the().palette();
    auto& window = m_window;

    auto titlebar_rect = title_bar_rect();
    auto titlebar_icon_rect = title_bar_icon_rect();
    auto titlebar_inner_rect = title_bar_text_rect();
    Gfx::Rect outer_rect = { {}, rect().size() };

    auto titlebar_title_rect = titlebar_inner_rect;
    titlebar_title_rect.set_width(Gfx::Font::default_bold_font().width(window.title()));

    Color title_color;
    Color border_color;
    Color border_color2;

    auto& wm = WSWindowManager::the();

    if (&window == wm.m_highlight_window) {
        border_color = palette.highlight_window_border1();
        border_color2 = palette.highlight_window_border2();
        title_color = palette.highlight_window_title();
    } else if (&window == wm.m_move_window) {
        border_color = palette.moving_window_border1();
        border_color2 = palette.moving_window_border2();
        title_color = palette.moving_window_title();
    } else if (&window == wm.m_active_window) {
        border_color = palette.active_window_border1();
        border_color2 = palette.active_window_border2();
        title_color = palette.active_window_title();
    } else {
        border_color = palette.inactive_window_border1();
        border_color2 = palette.inactive_window_border2();
        title_color = palette.inactive_window_title();
    }

    Gfx::StylePainter::paint_window_frame(painter, outer_rect, palette);

    if (!window.show_titlebar())
        return;

    painter.draw_line(titlebar_rect.bottom_left().translated(0, 1), titlebar_rect.bottom_right().translated(0, 1), palette.button());

    auto leftmost_button_rect = m_buttons.is_empty() ? Gfx::Rect() : m_buttons.last().relative_rect();

    painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2);

    int stripe_left = titlebar_title_rect.right() + 4;
    int stripe_right = leftmost_button_rect.left() - 3;
    if (stripe_left && stripe_right && stripe_left < stripe_right) {
        for (int i = 2; i <= titlebar_inner_rect.height() - 2; i += 2) {
            painter.draw_line({ stripe_left, titlebar_inner_rect.y() + i }, { stripe_right, titlebar_inner_rect.y() + i }, border_color);
        }
    }

    auto clipped_title_rect = titlebar_title_rect;
    clipped_title_rect.set_width(stripe_right - clipped_title_rect.x());
    if (!clipped_title_rect.is_empty()) {
        painter.draw_text(clipped_title_rect.translated(1, 2), window.title(), wm.window_title_font(), Gfx::TextAlignment::CenterLeft, border_color.darkened(0.4), Gfx::TextElision::Right);
        // FIXME: The translated(0, 1) wouldn't be necessary if we could center text based on its baseline.
        painter.draw_text(clipped_title_rect.translated(0, 1), window.title(), wm.window_title_font(), Gfx::TextAlignment::CenterLeft, title_color, Gfx::TextElision::Right);
    }

    painter.blit(titlebar_icon_rect.location(), window.icon(), window.icon().rect());

    for (auto& button : m_buttons) {
        button.paint(painter);
    }
}

static Gfx::Rect frame_rect_for_window(WSWindow& window, const Gfx::Rect& rect)
{
    auto type = window.type();
    auto offset = !window.show_titlebar() ? (window_titlebar_height + 1) : 0;

    switch (type) {
    case WSWindowType::Normal:
        return { rect.x() - 3,
            rect.y() - window_titlebar_height - 4 + offset,
            rect.width() + 6,
            rect.height() + 7 + window_titlebar_height - offset };
    default:
        return rect;
    }
}

static Gfx::Rect frame_rect_for_window(WSWindow& window)
{
    return frame_rect_for_window(window, window.rect());
}

Gfx::Rect WSWindowFrame::rect() const
{
    return frame_rect_for_window(m_window);
}

void WSWindowFrame::invalidate_title_bar()
{
    WSWindowManager::the().invalidate(title_bar_rect().translated(rect().location()));
}

void WSWindowFrame::notify_window_rect_changed(const Gfx::Rect& old_rect, const Gfx::Rect& new_rect)
{
    int window_button_width = 15;
    int window_button_height = 15;
    int x = title_bar_text_rect().right() + 1;

    for (auto& button : m_buttons) {
        x -= window_button_width;
        Gfx::Rect rect { x, 0, window_button_width, window_button_height };
        rect.center_vertically_within(title_bar_text_rect());
        button.set_relative_rect(rect);
    }

    auto& wm = WSWindowManager::the();
    wm.invalidate(frame_rect_for_window(m_window, old_rect));
    wm.invalidate(frame_rect_for_window(m_window, new_rect));
    wm.notify_rect_changed(m_window, old_rect, new_rect);
}

void WSWindowFrame::on_mouse_event(const WSMouseEvent& event)
{
    ASSERT(!m_window.is_fullscreen());

    if (m_window.is_blocked_by_modal_window())
        return;

    auto& wm = WSWindowManager::the();
    if (m_window.type() != WSWindowType::Normal)
        return;

    if (event.type() == WSEvent::MouseDown && (event.button() == MouseButton::Left || event.button() == MouseButton::Right) &&
        title_bar_icon_rect().contains(event.position())) {
        wm.move_to_front_and_make_active(m_window);
        m_window.popup_window_menu(event.position().translated(rect().location()));
        return;
    }

    // This is slightly hackish, but expand the title bar rect by one pixel downwards,
    // so that mouse events between the title bar and window contents don't act like
    // mouse events on the border.
    auto adjusted_title_bar_rect = title_bar_rect();
    adjusted_title_bar_rect.set_height(adjusted_title_bar_rect.height() + 1);

    if (adjusted_title_bar_rect.contains(event.position())) {
        wm.clear_resize_candidate();

        if (event.type() == WSEvent::MouseDown)
            wm.move_to_front_and_make_active(m_window);

        for (auto& button : m_buttons) {
            if (button.relative_rect().contains(event.position()))
                return button.on_mouse_event(event.translated(-button.relative_rect().location()));
        }
        if (event.type() == WSEvent::MouseDown) {
            if (event.button() == MouseButton::Right) {
                m_window.popup_window_menu(event.position().translated(rect().location()));
                return;
            }
            if (event.button() == MouseButton::Left)
                wm.start_window_move(m_window, event.translated(rect().location()));
        }
        return;
    }

    if (m_window.is_resizable() && event.type() == WSEvent::MouseMove && event.buttons() == 0) {
        constexpr ResizeDirection direction_for_hot_area[3][3] = {
            { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
            { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
            { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
        };
        Gfx::Rect outer_rect = { {}, rect().size() };
        ASSERT(outer_rect.contains(event.position()));
        int window_relative_x = event.x() - outer_rect.x();
        int window_relative_y = event.y() - outer_rect.y();
        int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
        int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
        wm.set_resize_candidate(m_window, direction_for_hot_area[hot_area_row][hot_area_column]);
        WSCompositor::the().invalidate_cursor();
        return;
    }

    if (m_window.is_resizable() && event.type() == WSEvent::MouseDown && event.button() == MouseButton::Left)
        wm.start_window_resize(m_window, event.translated(rect().location()));
}