2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2019-05-31 10:17:13 +08:00
|
|
|
// Copyright (C) 2018-2019 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
|
|
|
|
2016-11-22 22:54:15 +08:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <vector>
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2010-09-26 04:20:59 +08:00
|
|
|
#include "base/bind.h"
|
2011-01-24 06:19:18 +08:00
|
|
|
#include "base/memory.h"
|
2016-11-22 22:54:15 +08:00
|
|
|
#include "base/string.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "ui/ui.h"
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/app.h"
|
|
|
|
#include "app/console.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"
|
2018-07-04 23:35:15 +08:00
|
|
|
#include "ui/system.h"
|
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") {
|
2018-12-15 05:08:12 +08:00
|
|
|
m_button.Click.connect(base::Bind<void>(&ConsoleWindow::closeWindow, this, &m_button));
|
|
|
|
|
|
|
|
// When the window is closed, we clear the text
|
|
|
|
Close.connect(
|
|
|
|
base::Bind<void>(
|
|
|
|
[this] {
|
|
|
|
m_textbox.setText(std::string());
|
|
|
|
}));
|
|
|
|
|
|
|
|
m_view.attachToView(&m_textbox);
|
|
|
|
m_button.setMinSize(gfx::Size(60*ui::guiscale(), 0));
|
|
|
|
|
|
|
|
Grid* grid = new Grid(1, false);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addMessage(const std::string& msg) {
|
|
|
|
if (!m_hasText) {
|
|
|
|
m_hasText = true;
|
|
|
|
|
|
|
|
remapWindow();
|
|
|
|
setBounds(gfx::Rect(0, 0, ui::display_w()*9/10, ui::display_h()*6/10));
|
|
|
|
centerWindow();
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_textbox.setText(m_textbox.text() + msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isConsoleVisible() const {
|
|
|
|
return (m_hasText && isVisible());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-12-15 21:10:30 +08:00
|
|
|
bool onProcessMessage(ui::Message* msg) override {
|
|
|
|
if (msg->type() == ui::kKeyDownMessage) {
|
|
|
|
#if defined __APPLE__
|
|
|
|
if (msg->onlyCmdPressed())
|
|
|
|
#else
|
|
|
|
if (msg->onlyCtrlPressed())
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (static_cast<KeyMessage*>(msg)->scancode() == kKeyC)
|
|
|
|
set_clipboard_text(m_textbox.text());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Window::onProcessMessage(msg);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
|
2015-04-06 22:58:42 +08:00
|
|
|
if (ctx)
|
2015-05-19 04:04:31 +08:00
|
|
|
m_withUI = (ctx->isUIAvailable());
|
2015-04-06 22:58:42 +08:00
|
|
|
else
|
|
|
|
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() &&
|
|
|
|
Manager::getDefault()->getDisplay());
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-12-15 05:08:12 +08:00
|
|
|
m_console = 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()) {
|
2018-12-15 21:10:30 +08:00
|
|
|
m_console->manager()->attractFocus(m_console);
|
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
|
|
|
|
2018-12-15 05:08:12 +08:00
|
|
|
if (m_consoleCounter == 0) {
|
|
|
|
delete m_console; // window
|
|
|
|
m_console = nullptr;
|
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
|
|
|
ui::assert_ui_thread();
|
|
|
|
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
|
|
|
|
|
|
|
} // namespace app
|