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/app.h" |
12 | #include "app/commands/command.h" |
13 | #include "app/commands/commands.h" |
14 | #include "app/context_access.h" |
15 | #include "app/modules/editors.h" |
16 | #include "app/ui/doc_view.h" |
17 | #include "app/ui/status_bar.h" |
18 | #include "app/ui/workspace.h" |
19 | #include "app/ui_context.h" |
20 | #include "doc/sprite.h" |
21 | #include "ui/ui.h" |
22 | |
23 | #include <memory> |
24 | |
25 | namespace app { |
26 | |
27 | using namespace ui; |
28 | |
29 | class CloseFileCommand : public Command { |
30 | public: |
31 | CloseFileCommand() |
32 | : Command(CommandId::CloseFile(), CmdUIOnlyFlag) { |
33 | } |
34 | |
35 | protected: |
36 | |
37 | bool onEnabled(Context* context) override { |
38 | Workspace* workspace = App::instance()->workspace(); |
39 | WorkspaceView* view = workspace->activeView(); |
40 | return (view != nullptr); |
41 | } |
42 | |
43 | void onExecute(Context* context) override { |
44 | Workspace* workspace = App::instance()->workspace(); |
45 | WorkspaceView* view = workspace->activeView(); |
46 | if (view) |
47 | workspace->closeView(view, false); |
48 | } |
49 | }; |
50 | |
51 | class CloseAllFilesCommand : public Command { |
52 | public: |
53 | CloseAllFilesCommand() |
54 | : Command(CommandId::CloseAllFiles(), CmdRecordableFlag) { |
55 | m_quitting = false; |
56 | } |
57 | |
58 | protected: |
59 | |
60 | void onLoadParams(const Params& params) override { |
61 | m_quitting = params.get_as<bool>("quitting"); |
62 | } |
63 | |
64 | void onExecute(Context* context) override { |
65 | Workspace* workspace = App::instance()->workspace(); |
66 | |
67 | // Collect all document views |
68 | DocViews docViews; |
69 | for (auto view : *workspace) { |
70 | DocView* docView = dynamic_cast<DocView*>(view); |
71 | if (docView) |
72 | docViews.push_back(docView); |
73 | } |
74 | |
75 | for (auto docView : docViews) { |
76 | if (!workspace->closeView(docView, m_quitting)) |
77 | break; |
78 | } |
79 | } |
80 | |
81 | private: |
82 | bool m_quitting; |
83 | }; |
84 | |
85 | Command* CommandFactory::createCloseFileCommand() |
86 | { |
87 | return new CloseFileCommand; |
88 | } |
89 | |
90 | Command* CommandFactory::createCloseAllFilesCommand() |
91 | { |
92 | return new CloseAllFilesCommand; |
93 | } |
94 | |
95 | } // namespace app |
96 |