| 
									
										
										
										
											2015-02-12 23:16:25 +08:00
										 |  |  | // Aseprite
 | 
					
						
							| 
									
										
										
										
											2021-01-13 08:39:12 +08:00
										 |  |  | // Copyright (C) 2018-2021  Igara Studio S.A.
 | 
					
						
							| 
									
										
										
										
											2018-07-04 23:35:15 +08:00
										 |  |  | // Copyright (C) 2001-2018  David Capello
 | 
					
						
							| 
									
										
										
										
											2015-02-12 23:16:25 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2016-08-27 04:02:58 +08:00
										 |  |  | // This program is distributed under the terms of
 | 
					
						
							|  |  |  | // the End-User License Agreement for Aseprite.
 | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #ifdef HAVE_CONFIG_H
 | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | #include "config.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  | #include "app/console.h"
 | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #include "app/app.h"
 | 
					
						
							| 
									
										
										
										
											2015-04-06 22:58:42 +08:00
										 |  |  | #include "app/context.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | #include "app/modules/gui.h"
 | 
					
						
							|  |  |  | #include "app/ui/status_bar.h"
 | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  | #include "base/memory.h"
 | 
					
						
							|  |  |  | #include "base/string.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-04 23:35:15 +08:00
										 |  |  | #include "ui/system.h"
 | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  | #include "ui/ui.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <cstdarg>
 | 
					
						
							|  |  |  | #include <cstdio>
 | 
					
						
							|  |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace app { | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-18 09:02:54 +08:00
										 |  |  | using namespace ui; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  | class Console::ConsoleWindow : public Window { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |   ConsoleWindow() : Window(Window::WithTitleBar, "Console"), | 
					
						
							|  |  |  |                     m_textbox("", WORDWRAP), | 
					
						
							| 
									
										
										
										
											2018-12-15 21:10:30 +08:00
										 |  |  |                     m_button("Cancel") { | 
					
						
							| 
									
										
										
										
											2020-07-04 08:51:46 +08:00
										 |  |  |     m_button.Click.connect([this]{ closeWindow(&m_button); }); | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // When the window is closed, we clear the text
 | 
					
						
							|  |  |  |     Close.connect( | 
					
						
							| 
									
										
										
										
											2020-07-04 08:51:46 +08:00
										 |  |  |       [this]{ | 
					
						
							|  |  |  |         m_textbox.setText(std::string()); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     m_view.attachToView(&m_textbox); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 01:04:01 +08:00
										 |  |  |     ui::Grid* grid = new ui::Grid(1, false); | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  |     grid->addChildInCell(&m_view, 1, 1, HORIZONTAL | VERTICAL); | 
					
						
							|  |  |  |     grid->addChildInCell(&m_button, 1, 1, CENTER); | 
					
						
							|  |  |  |     addChild(grid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-15 21:10:30 +08:00
										 |  |  |     m_textbox.setFocusMagnet(true); | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  |     m_button.setFocusMagnet(true); | 
					
						
							|  |  |  |     m_view.setExpansive(true); | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     initTheme(); | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void addMessage(const std::string& msg) { | 
					
						
							|  |  |  |     if (!m_hasText) { | 
					
						
							|  |  |  |       m_hasText = true; | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  |       centerConsole(); | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_textbox.setText(m_textbox.text() + msg); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bool isConsoleVisible() const { | 
					
						
							|  |  |  |     return (m_hasText && isVisible()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  |   void centerConsole() { | 
					
						
							|  |  |  |     initTheme(); | 
					
						
							| 
									
										
										
										
											2021-02-18 23:30:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-20 05:57:56 +08:00
										 |  |  |     Display* display = ui::Manager::getDefault()->display(); | 
					
						
							|  |  |  |     const gfx::Rect displayRc = display->bounds(); | 
					
						
							|  |  |  |     gfx::Rect rc; | 
					
						
							|  |  |  |     rc.w = displayRc.w*9/10; | 
					
						
							|  |  |  |     rc.h = displayRc.h*6/10; | 
					
						
							|  |  |  |     rc.x = displayRc.x + displayRc.w/2 - rc.w/2; | 
					
						
							|  |  |  |     rc.y = displayRc.y + displayRc.h/2 - rc.h/2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ui::fit_bounds(display, this, rc); | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2018-12-15 21:10:30 +08:00
										 |  |  |   bool onProcessMessage(ui::Message* msg) override { | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  |     switch (msg->type()) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       case ui::kKeyDownMessage: | 
					
						
							| 
									
										
										
										
											2018-12-15 21:10:30 +08:00
										 |  |  | #if defined __APPLE__
 | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  |         if (msg->onlyCmdPressed()) | 
					
						
							| 
									
										
										
										
											2018-12-15 21:10:30 +08:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  |         if (msg->onlyCtrlPressed()) | 
					
						
							| 
									
										
										
										
											2018-12-15 21:10:30 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  |         { | 
					
						
							|  |  |  |           if (static_cast<KeyMessage*>(msg)->scancode() == kKeyC) | 
					
						
							|  |  |  |             set_clipboard_text(m_textbox.text()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2018-12-15 21:10:30 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return Window::onProcessMessage(msg); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  |   void onInitTheme(InitThemeEvent& ev) override { | 
					
						
							|  |  |  |     Window::onInitTheme(ev); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_button.setMinSize(gfx::Size(60*ui::guiscale(), 0)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  |   View m_view; | 
					
						
							|  |  |  |   TextBox m_textbox; | 
					
						
							|  |  |  |   Button m_button; | 
					
						
							|  |  |  |   bool m_hasText = false; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int Console::m_consoleCounter = 0; | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  | std::unique_ptr<Console::ConsoleWindow> Console::m_console = nullptr; | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-06 22:58:42 +08:00
										 |  |  | Console::Console(Context* ctx) | 
					
						
							|  |  |  |   : m_withUI(false) | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-04 23:35:15 +08:00
										 |  |  |   if (!ui::is_ui_thread()) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-18 23:30:14 +08:00
										 |  |  |   if (ctx) { | 
					
						
							| 
									
										
										
										
											2015-05-19 04:04:31 +08:00
										 |  |  |     m_withUI = (ctx->isUIAvailable()); | 
					
						
							| 
									
										
										
										
											2021-02-18 23:30:14 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							| 
									
										
										
										
											2015-04-06 22:58:42 +08:00
										 |  |  |     m_withUI = | 
					
						
							| 
									
										
										
										
											2017-05-25 01:54:59 +08:00
										 |  |  |       (App::instance() && | 
					
						
							|  |  |  |        App::instance()->isGui() && | 
					
						
							| 
									
										
										
										
											2015-04-06 22:58:42 +08:00
										 |  |  |        Manager::getDefault() && | 
					
						
							| 
									
										
										
										
											2021-02-18 23:30:14 +08:00
										 |  |  |        Manager::getDefault()->display()->nativeWindow()); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-04-06 22:58:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (!m_withUI) | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  |   ++m_consoleCounter; | 
					
						
							|  |  |  |   if (m_console || m_consoleCounter > 1) | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  |   m_console.reset(new ConsoleWindow); | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-06-11 23:11:11 +08:00
										 |  |  | Console::~Console() | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-04-06 22:58:42 +08:00
										 |  |  |   if (!m_withUI) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  |   --m_consoleCounter; | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  |   if (m_console && m_console->isConsoleVisible()) { | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  |     m_console->manager()->attractFocus(m_console.get()); | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  |     m_console->openWindowInForeground(); | 
					
						
							| 
									
										
										
										
											2018-11-22 00:10:47 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  |   if (m_consoleCounter == 0) | 
					
						
							|  |  |  |     m_console.reset(); | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-15 06:58:11 +08:00
										 |  |  | void Console::printf(const char* format, ...) | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-11-22 22:54:15 +08:00
										 |  |  |   std::va_list ap; | 
					
						
							| 
									
										
										
										
											2008-01-04 07:22:04 +08:00
										 |  |  |   va_start(ap, format); | 
					
						
							| 
									
										
										
										
											2016-11-22 22:54:15 +08:00
										 |  |  |   std::string msg = base::string_vprintf(format, ap); | 
					
						
							| 
									
										
										
										
											2008-01-04 07:22:04 +08:00
										 |  |  |   va_end(ap); | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  |   if (!m_withUI || !m_console) { | 
					
						
							| 
									
										
										
										
											2016-11-22 22:54:15 +08:00
										 |  |  |     fputs(msg.c_str(), stdout); | 
					
						
							| 
									
										
										
										
											2015-04-06 22:58:42 +08:00
										 |  |  |     fflush(stdout); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-06 22:58:42 +08:00
										 |  |  |   // Open the window
 | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  |   if (!m_console->isVisible()) { | 
					
						
							|  |  |  |     m_console->openWindow(); | 
					
						
							| 
									
										
										
										
											2015-04-06 22:58:42 +08:00
										 |  |  |     ui::Manager::getDefault()->invalidate(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-01-06 06:45:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-22 22:54:15 +08:00
										 |  |  |   // Update the textbox
 | 
					
						
							| 
									
										
										
										
											2018-12-15 05:08:12 +08:00
										 |  |  |   m_console->addMessage(msg); | 
					
						
							| 
									
										
										
										
											2007-09-19 07:57:02 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-01-21 10:33:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // static
 | 
					
						
							| 
									
										
										
										
											2011-03-09 11:18:43 +08:00
										 |  |  | void Console::showException(const std::exception& e) | 
					
						
							| 
									
										
										
										
											2011-01-21 10:33:57 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-31 10:17:13 +08:00
										 |  |  |   if (!ui::is_ui_thread()) { | 
					
						
							|  |  |  |     LOG(ERROR, "A problem has occurred.\n\nDetails:\n%s\n", e.what()); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-21 10:33:57 +08:00
										 |  |  |   Console console; | 
					
						
							| 
									
										
										
										
											2016-07-23 07:46:29 +08:00
										 |  |  |   if (typeid(e) == typeid(std::bad_alloc)) | 
					
						
							|  |  |  |     console.printf("There is not enough memory to complete the action."); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     console.printf("A problem has occurred.\n\nDetails:\n%s\n", e.what()); | 
					
						
							| 
									
										
										
										
											2011-01-21 10:33:57 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-12 02:52:14 +08:00
										 |  |  | // static
 | 
					
						
							|  |  |  | void Console::notifyNewDisplayConfiguration() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (m_console) | 
					
						
							|  |  |  |     m_console->centerConsole(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-06 08:20:19 +08:00
										 |  |  | } // namespace app
 |