1// Aseprite
2// Copyright (C) 2021 Igara Studio S.A.
3// Copyright (C) 2001-2017 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/ui/zoom_entry.h"
13
14#include "app/modules/gui.h"
15#include "base/scoped_value.h"
16#include "gfx/rect.h"
17#include "gfx/region.h"
18#include "ui/manager.h"
19#include "ui/message.h"
20#include "ui/popup_window.h"
21#include "ui/slider.h"
22#include "ui/system.h"
23#include "ui/theme.h"
24
25#include <cmath>
26#include <cstdio>
27#include <cstdlib>
28
29namespace app {
30
31using namespace gfx;
32using namespace ui;
33
34ZoomEntry::ZoomEntry()
35 : IntEntry(0, render::Zoom::linearValues()-1, this)
36 , m_locked(false)
37{
38 setSuffix("%");
39 setMaxTextLength(6); // strlen("6400.0") == 6
40 setup_mini_look(this);
41
42 setZoom(render::Zoom(1, 1));
43 initTheme();
44}
45
46void ZoomEntry::setZoom(const render::Zoom& zoom)
47{
48 if (m_locked)
49 return;
50
51 std::string newText = onGetTextFromValue(zoom.linearScale());
52 if (newText != text())
53 setText(newText);
54}
55
56void ZoomEntry::onValueChange()
57{
58 base::ScopedValue<bool> lock(m_locked, true, m_locked);
59 IntEntry::onValueChange();
60
61 render::Zoom zoom = render::Zoom::fromLinearScale(getValue());
62 ZoomChange(zoom);
63}
64
65std::string ZoomEntry::onGetTextFromValue(int value)
66{
67 render::Zoom zoom = render::Zoom::fromLinearScale(value);
68
69 char buf[256];
70 std::sprintf(buf, "%.1f", zoom.scale() * 100.0);
71 return buf;
72}
73
74int ZoomEntry::onGetValueFromText(const std::string& text)
75{
76 double value = std::strtod(text.c_str(), nullptr);
77 return render::Zoom::fromScale(value / 100.0).linearScale();
78}
79
80} // namespace app
81