2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2018-11-21 06:42:53 +08:00
|
|
|
// Copyright (C) 2018 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;
|
|
|
|
|
2012-07-09 10:24:42 +08:00
|
|
|
static Window* wid_console = NULL;
|
2010-01-26 08:38:05 +08:00
|
|
|
static Widget* wid_view = NULL;
|
|
|
|
static Widget* wid_textbox = NULL;
|
|
|
|
static Widget* wid_cancel = NULL;
|
2007-09-19 07:57:02 +08:00
|
|
|
static int console_counter = 0;
|
|
|
|
static bool console_locked;
|
2010-01-31 00:43:13 +08:00
|
|
|
static bool want_close_flag = false;
|
2017-05-25 01:54:59 +08:00
|
|
|
static bool has_text = false;
|
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
|
|
|
|
2017-05-25 01:54:59 +08:00
|
|
|
if (console_counter == 0)
|
|
|
|
has_text = false;
|
|
|
|
|
2015-04-06 22:58:42 +08:00
|
|
|
console_counter++;
|
|
|
|
if (wid_console || console_counter > 1)
|
2007-09-19 07:57:02 +08:00
|
|
|
return;
|
|
|
|
|
2015-04-06 22:58:42 +08:00
|
|
|
Window* window = new Window(Window::WithTitleBar, "Errors Console");
|
|
|
|
Grid* grid = new Grid(1, false);
|
|
|
|
View* view = new View();
|
2015-06-24 01:00:00 +08:00
|
|
|
TextBox* textbox = new TextBox("", WORDWRAP);
|
2015-04-06 22:58:42 +08:00
|
|
|
Button* button = new Button("&Cancel");
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2015-04-06 22:58:42 +08:00
|
|
|
// The "button" closes the console
|
2017-03-09 06:15:34 +08:00
|
|
|
button->processMnemonicFromText();
|
2018-11-21 06:42:53 +08:00
|
|
|
button->Click.connect(
|
|
|
|
base::Bind<void>(
|
|
|
|
[window, button, textbox] {
|
|
|
|
textbox->setText(std::string());
|
|
|
|
window->closeWindow(button);
|
|
|
|
}));
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2015-04-06 22:58:42 +08:00
|
|
|
view->attachToView(textbox);
|
2008-03-27 22:29:33 +08:00
|
|
|
|
2015-04-06 22:58:42 +08:00
|
|
|
button->setMinSize(gfx::Size(60, 0));
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2015-06-24 01:00:00 +08:00
|
|
|
grid->addChildInCell(view, 1, 1, HORIZONTAL | VERTICAL);
|
|
|
|
grid->addChildInCell(button, 1, 1, CENTER);
|
2015-04-06 22:58:42 +08:00
|
|
|
window->addChild(grid);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2015-04-06 22:58:42 +08:00
|
|
|
view->setVisible(false);
|
|
|
|
button->setFocusMagnet(true);
|
|
|
|
view->setExpansive(true);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2015-04-06 22:58:42 +08:00
|
|
|
wid_console = window;
|
|
|
|
wid_view = view;
|
|
|
|
wid_textbox = textbox;
|
|
|
|
wid_cancel = button;
|
|
|
|
console_locked = false;
|
|
|
|
want_close_flag = false;
|
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;
|
|
|
|
|
2007-09-19 07:57:02 +08:00
|
|
|
console_counter--;
|
|
|
|
|
|
|
|
if ((wid_console) && (console_counter == 0)) {
|
2008-01-04 07:22:04 +08:00
|
|
|
if (console_locked
|
2012-01-06 06:45:03 +08:00
|
|
|
&& !want_close_flag
|
|
|
|
&& wid_console->isVisible()) {
|
2015-06-24 01:14:06 +08:00
|
|
|
// Open in foreground
|
2012-07-09 10:24:42 +08:00
|
|
|
wid_console->openWindowInForeground();
|
2008-01-04 07:22:04 +08:00
|
|
|
}
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-01-06 06:45:03 +08:00
|
|
|
delete wid_console; // window
|
2007-09-19 07:57:02 +08:00
|
|
|
wid_console = NULL;
|
2010-01-31 00:43:13 +08:00
|
|
|
want_close_flag = false;
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 01:54:59 +08:00
|
|
|
bool Console::hasText() const
|
|
|
|
{
|
|
|
|
return has_text;
|
|
|
|
}
|
|
|
|
|
2013-10-15 06:58:11 +08:00
|
|
|
void Console::printf(const char* format, ...)
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2017-05-25 01:54:59 +08:00
|
|
|
has_text = true;
|
|
|
|
|
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
|
|
|
|
2015-04-06 22:58:42 +08:00
|
|
|
if (!m_withUI || !wid_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
|
|
|
|
if (!wid_console->isVisible()) {
|
|
|
|
wid_console->openWindow();
|
|
|
|
ui::Manager::getDefault()->invalidate();
|
|
|
|
}
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2016-11-22 22:54:15 +08:00
|
|
|
// Update the textbox
|
2015-04-06 22:58:42 +08:00
|
|
|
if (!console_locked) {
|
|
|
|
console_locked = true;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2015-04-06 22:58:42 +08:00
|
|
|
wid_view->setVisible(true);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2015-04-06 22:58:42 +08:00
|
|
|
wid_console->remapWindow();
|
|
|
|
wid_console->setBounds(gfx::Rect(0, 0, ui::display_w()*9/10, ui::display_h()*6/10));
|
|
|
|
wid_console->centerWindow();
|
|
|
|
wid_console->invalidate();
|
|
|
|
}
|
2007-09-19 07:57:02 +08:00
|
|
|
|
Refactor several "getNoun()" getters to "noun()"
This is a work-in-progress to create a consistent API and finally
separate the whole Aseprite base/gfx/ui libs into a reusable C++ library.
Classes:
app::IFileItem, app::AppMenuItem, app::skin::SkinPart,
gfx::Rect, gfx::Border, she::FileDialog,
ui::IButtonIcon, ui::Graphics, ui::Overlay, ui::Widget,
ui::ScrollableViewDelegate, and UI events
2015-12-05 01:39:04 +08:00
|
|
|
const std::string& text = wid_textbox->text();
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2015-04-06 22:58:42 +08:00
|
|
|
std::string final;
|
|
|
|
if (!text.empty())
|
|
|
|
final += text;
|
2016-11-22 22:54:15 +08:00
|
|
|
final += msg;
|
2015-04-06 22:58:42 +08:00
|
|
|
|
|
|
|
wid_textbox->setText(final.c_str());
|
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
|
|
|
{
|
|
|
|
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
|