aseprite/src/app/console.cpp

162 lines
3.5 KiB
C++
Raw Normal View History

2015-02-12 23:16:25 +08:00
// Aseprite
// Copyright (C) 2001-2015 David Capello
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
2007-09-19 07:57:02 +08:00
#ifdef HAVE_CONFIG_H
2007-09-19 07:57:02 +08:00
#include "config.h"
#endif
2007-09-19 07:57:02 +08:00
#include <stdarg.h>
#include <stdio.h>
2007-09-19 07:57:02 +08:00
#include "base/bind.h"
#include "base/memory.h"
#include "ui/ui.h"
2007-09-19 07:57:02 +08:00
#include "app/app.h"
#include "app/console.h"
#include "app/context.h"
#include "app/modules/gui.h"
#include "app/ui/status_bar.h"
namespace app {
2007-09-19 07:57:02 +08:00
using namespace ui;
2012-07-09 10:24:42 +08:00
static Window* wid_console = NULL;
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;
static bool want_close_flag = false;
2007-09-19 07:57:02 +08:00
Console::Console(Context* ctx)
: m_withUI(false)
2007-09-19 07:57:02 +08:00
{
if (ctx)
m_withUI = (ctx->isUIAvailable());
else
m_withUI =
(App::instance()->isGui() &&
Manager::getDefault() &&
Manager::getDefault()->getDisplay());
if (!m_withUI)
return;
2007-09-19 07:57:02 +08:00
console_counter++;
if (wid_console || console_counter > 1)
2007-09-19 07:57:02 +08:00
return;
Window* window = new Window(Window::WithTitleBar, "Errors Console");
Grid* grid = new Grid(1, false);
View* view = new View();
TextBox* textbox = new TextBox("", JI_WORDWRAP);
Button* button = new Button("&Cancel");
2007-09-19 07:57:02 +08:00
if (!grid || !textbox || !button)
return;
// The "button" closes the console
button->Click.connect(Bind<void>(&Window::closeWindow, window, button));
2007-09-19 07:57:02 +08:00
view->attachToView(textbox);
button->setMinSize(gfx::Size(60, 0));
2007-09-19 07:57:02 +08:00
grid->addChildInCell(view, 1, 1, JI_HORIZONTAL | JI_VERTICAL);
grid->addChildInCell(button, 1, 1, JI_CENTER);
window->addChild(grid);
2007-09-19 07:57:02 +08:00
view->setVisible(false);
button->setFocusMagnet(true);
view->setExpansive(true);
2007-09-19 07:57:02 +08:00
/* force foreground mode */
/* ji_find_widget(window)->in_foreground = true; */
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
}
Console::~Console()
2007-09-19 07:57:02 +08:00
{
if (!m_withUI)
return;
2007-09-19 07:57:02 +08:00
console_counter--;
if ((wid_console) && (console_counter == 0)) {
if (console_locked
&& !want_close_flag
&& wid_console->isVisible()) {
/* open in foreground */
2012-07-09 10:24:42 +08:00
wid_console->openWindowInForeground();
}
2007-09-19 07:57:02 +08:00
delete wid_console; // window
2007-09-19 07:57:02 +08:00
wid_console = NULL;
want_close_flag = false;
2007-09-19 07:57:02 +08:00
}
}
void Console::printf(const char* format, ...)
2007-09-19 07:57:02 +08:00
{
char buf[4096]; // TODO warning buffer overflow
2007-09-19 07:57:02 +08:00
va_list ap;
va_start(ap, format);
vsprintf(buf, format, ap);
va_end(ap);
2007-09-19 07:57:02 +08:00
if (!m_withUI || !wid_console) {
fputs(buf, stdout);
fflush(stdout);
return;
}
2007-09-19 07:57:02 +08:00
// Open the window
if (!wid_console->isVisible()) {
wid_console->openWindow();
ui::Manager::getDefault()->invalidate();
}
/* update the textbox */
if (!console_locked) {
console_locked = true;
2007-09-19 07:57:02 +08:00
wid_view->setVisible(true);
2007-09-19 07:57:02 +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
const std::string& text = wid_textbox->getText();
2007-09-19 07:57:02 +08:00
std::string final;
if (!text.empty())
final += text;
final += buf;
wid_textbox->setText(final.c_str());
2007-09-19 07:57:02 +08:00
}
// static
void Console::showException(const std::exception& e)
{
Console console;
console.printf("A problem has occurred.\n\nDetails:\n%s\n", e.what());
}
} // namespace app