1// Aseprite UI Library
2// Copyright (C) 2018-2022 Igara Studio S.A.
3// Copyright (C) 2001-2015 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "gfx/point.h"
13#include "gfx/size.h"
14#include "ui/message.h"
15#include "ui/size_hint_event.h"
16#include "ui/resize_event.h"
17#include "ui/theme.h"
18#include "ui/view.h"
19#include "ui/viewport.h"
20
21#include <algorithm>
22
23namespace ui {
24
25using namespace gfx;
26
27Viewport::Viewport()
28 : Widget(kViewViewportWidget)
29{
30 enableFlags(IGNORE_MOUSE);
31 initTheme();
32}
33
34void Viewport::onResize(ResizeEvent& ev)
35{
36 Rect rect = ev.bounds();
37 setBoundsQuietly(rect);
38
39 auto view = static_cast<View*>(parent());
40 ASSERT(view);
41 if (!view)
42 return;
43
44 Point scroll = view->viewScroll();
45
46 Rect cpos(0, 0, 0, 0);
47 cpos.x = rect.x + border().left() - scroll.x;
48 cpos.y = rect.y + border().top() - scroll.y;
49
50 for (auto child : children()) {
51 Size reqSize = child->sizeHint();
52
53 cpos.w = std::max(reqSize.w, rect.w - border().width());
54 cpos.h = std::max(reqSize.h, rect.h - border().height());
55
56 child->setBounds(cpos);
57 }
58}
59
60void Viewport::onSizeHint(SizeHintEvent& ev)
61{
62 ev.setSizeHint(gfx::Size(1 + border().width(),
63 1 + border().height()));
64}
65
66void Viewport::onPaint(PaintEvent& ev)
67{
68 theme()->paintViewViewport(ev);
69}
70
71Size Viewport::calculateNeededSize()
72{
73 Size maxSize(0, 0);
74 Size reqSize;
75
76 for (auto child : children()) {
77 reqSize = child->sizeHint();
78 maxSize.w = std::max(maxSize.w, reqSize.w);
79 maxSize.h = std::max(maxSize.h, reqSize.h);
80 }
81
82 return maxSize;
83}
84
85} // namespace ui
86