2013-08-09 08:01:20 +08:00
|
|
|
/* Aseprite
|
2013-01-27 23:13:13 +08:00
|
|
|
* Copyright (C) 2001-2013 David Capello
|
2007-09-19 07:57:02 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
|
|
#include <stdarg.h>
|
2010-09-26 04:20:59 +08:00
|
|
|
#include <stdio.h>
|
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"
|
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"
|
|
|
|
#include "app/modules/gui.h"
|
|
|
|
#include "app/ui/status_bar.h"
|
|
|
|
|
|
|
|
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;
|
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
|
|
|
{
|
|
|
|
console_counter++;
|
|
|
|
|
2014-09-01 01:17:49 +08:00
|
|
|
if (!App::instance()->isGui() ||
|
|
|
|
!Manager::getDefault() ||
|
|
|
|
!Manager::getDefault()->getDisplay() ||
|
2007-09-19 07:57:02 +08:00
|
|
|
wid_console ||
|
|
|
|
console_counter > 1)
|
|
|
|
return;
|
|
|
|
else {
|
2013-11-24 04:47:57 +08:00
|
|
|
Window* window = new Window(Window::WithTitleBar, "Errors Console");
|
2011-01-27 06:51:52 +08:00
|
|
|
Grid* grid = new Grid(1, false);
|
2011-02-21 05:35:21 +08:00
|
|
|
View* view = new View();
|
2013-10-15 06:58:11 +08:00
|
|
|
TextBox* textbox = new TextBox("", JI_WORDWRAP);
|
2010-09-19 10:54:56 +08:00
|
|
|
Button* button = new Button("&Cancel");
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2008-03-27 22:29:33 +08:00
|
|
|
if (!grid || !textbox || !button)
|
2007-09-19 07:57:02 +08:00
|
|
|
return;
|
|
|
|
|
2010-08-24 04:41:19 +08:00
|
|
|
// The "button" closes the console
|
2012-07-09 10:24:42 +08:00
|
|
|
button->Click.connect(Bind<void>(&Window::closeWindow, window, button));
|
2010-08-24 04:41:19 +08:00
|
|
|
|
2011-02-21 05:35:21 +08:00
|
|
|
view->attachToView(textbox);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2014-04-22 09:15:29 +08:00
|
|
|
button->setMinSize(gfx::Size(60, 0));
|
2008-03-27 22:29:33 +08:00
|
|
|
|
2011-01-27 06:51:52 +08:00
|
|
|
grid->addChildInCell(view, 1, 1, JI_HORIZONTAL | JI_VERTICAL);
|
|
|
|
grid->addChildInCell(button, 1, 1, JI_CENTER);
|
2011-03-30 08:35:17 +08:00
|
|
|
window->addChild(grid);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2010-07-04 23:03:14 +08:00
|
|
|
view->setVisible(false);
|
2012-04-06 06:00:19 +08:00
|
|
|
button->setFocusMagnet(true);
|
|
|
|
view->setExpansive(true);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
|
|
|
/* force foreground mode */
|
2010-01-31 00:43:13 +08:00
|
|
|
/* ji_find_widget(window)->in_foreground = true; */
|
2007-09-19 07:57:02 +08:00
|
|
|
|
|
|
|
wid_console = window;
|
|
|
|
wid_view = view;
|
|
|
|
wid_textbox = textbox;
|
|
|
|
wid_cancel = button;
|
2010-01-31 00:43:13 +08:00
|
|
|
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
|
|
|
{
|
|
|
|
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()) {
|
2008-01-04 07:22:04 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-15 06:58:11 +08:00
|
|
|
void Console::printf(const char* format, ...)
|
2007-09-19 07:57:02 +08:00
|
|
|
{
|
2013-10-15 06:58:11 +08:00
|
|
|
char buf[4096]; // TODO warning buffer overflow
|
2007-09-19 07:57:02 +08:00
|
|
|
va_list ap;
|
|
|
|
|
2008-01-04 07:22:04 +08:00
|
|
|
va_start(ap, format);
|
2013-10-15 06:58:11 +08:00
|
|
|
vsprintf(buf, format, ap);
|
2008-01-04 07:22:04 +08:00
|
|
|
va_end(ap);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
|
|
|
if (wid_console) {
|
2010-07-04 23:03:14 +08:00
|
|
|
// Open the window
|
|
|
|
if (!wid_console->isVisible()) {
|
2012-07-09 10:24:42 +08:00
|
|
|
wid_console->openWindow();
|
2012-06-18 09:02:54 +08:00
|
|
|
ui::Manager::getDefault()->invalidate();
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* update the textbox */
|
|
|
|
if (!console_locked) {
|
2010-01-31 00:43:13 +08:00
|
|
|
console_locked = true;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2010-07-04 23:03:14 +08:00
|
|
|
wid_view->setVisible(true);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2013-01-11 23:43:25 +08:00
|
|
|
wid_console->remapWindow();
|
2014-09-01 01:17:49 +08:00
|
|
|
wid_console->setBounds(gfx::Rect(0, 0, ui::display_w()*9/10, ui::display_h()*6/10));
|
2013-01-11 23:43:25 +08:00
|
|
|
wid_console->centerWindow();
|
2011-01-22 06:45:04 +08:00
|
|
|
wid_console->invalidate();
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
|
|
|
|
2013-10-15 06:58:11 +08:00
|
|
|
const std::string& text = wid_textbox->getText();
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2014-04-21 06:53:27 +08:00
|
|
|
std::string final;
|
2013-10-15 06:58:11 +08:00
|
|
|
if (!text.empty())
|
|
|
|
final += text;
|
|
|
|
final += buf;
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2013-10-15 06:58:11 +08:00
|
|
|
wid_textbox->setText(final.c_str());
|
2007-09-19 07:57:02 +08:00
|
|
|
}
|
2008-03-23 02:43:56 +08:00
|
|
|
else {
|
2010-01-29 11:15:33 +08:00
|
|
|
fputs(buf, stdout);
|
|
|
|
fflush(stdout);
|
2008-03-23 02:43:56 +08:00
|
|
|
}
|
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;
|
|
|
|
console.printf("A problem has occurred.\n\nDetails:\n%s", e.what());
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|