1// Aseprite
2// Copyright (C) 2001-2018 David Capello
3//
4// This program is distributed under the terms of
5// the End-User License Agreement for Aseprite.
6
7#ifdef HAVE_CONFIG_H
8#include "config.h"
9#endif
10
11#include "app/ui/input_chain.h"
12
13#include "app/ui/input_chain_element.h"
14
15#include <algorithm>
16
17namespace app {
18
19void InputChain::prioritize(InputChainElement* element,
20 const ui::Message* msg)
21{
22 const bool alreadyInFront =
23 (!m_elements.empty() && m_elements.front() == element);
24
25 if (!alreadyInFront) {
26 auto it = std::find(m_elements.begin(), m_elements.end(), element);
27 if (it != m_elements.end())
28 m_elements.erase(it);
29 }
30
31 for (auto e : m_elements)
32 e->onNewInputPriority(element, msg);
33
34 if (!alreadyInFront)
35 m_elements.insert(m_elements.begin(), element);
36}
37
38bool InputChain::canCut(Context* ctx)
39{
40 for (auto e : m_elements) {
41 if (e->onCanCut(ctx))
42 return true;
43 }
44 return false;
45}
46
47bool InputChain::canCopy(Context* ctx)
48{
49 for (auto e : m_elements) {
50 if (e->onCanCopy(ctx))
51 return true;
52 }
53 return false;
54}
55
56bool InputChain::canPaste(Context* ctx)
57{
58 for (auto e : m_elements) {
59 if (e->onCanPaste(ctx))
60 return true;
61 }
62 return false;
63}
64
65bool InputChain::canClear(Context* ctx)
66{
67 for (auto e : m_elements) {
68 if (e->onCanClear(ctx))
69 return true;
70 }
71 return false;
72}
73
74void InputChain::cut(Context* ctx)
75{
76 for (auto e : m_elements) {
77 if (e->onCanCut(ctx) && e->onCut(ctx))
78 break;
79 }
80}
81
82void InputChain::copy(Context* ctx)
83{
84 for (auto e : m_elements) {
85 if (e->onCanCopy(ctx) && e->onCopy(ctx))
86 break;
87 }
88}
89
90void InputChain::paste(Context* ctx)
91{
92 for (auto e : m_elements) {
93 if (e->onCanPaste(ctx) && e->onPaste(ctx))
94 break;
95 }
96}
97
98void InputChain::clear(Context* ctx)
99{
100 for (auto e : m_elements) {
101 if (e->onCanClear(ctx) && e->onClear(ctx))
102 break;
103 }
104}
105
106void InputChain::cancel(Context* ctx)
107{
108 for (auto e : m_elements)
109 e->onCancel(ctx);
110}
111
112} // namespace app
113