Convert jalert to Alert C++ class.

This commit is contained in:
David Capello 2011-01-27 17:21:33 -03:00
parent f558666056
commit 5a9d991219
18 changed files with 157 additions and 173 deletions

View File

@ -124,8 +124,8 @@ try_again:;
// see if the sprite has changes
while (sprite->isModified()) {
// ask what want to do the user with the changes in the sprite
int ret = jalert("Warning<<Saving changes in:<<%s||&Save||Do&n't Save||&Cancel",
get_filename(sprite->getFilename()));
int ret = Alert::show("Warning<<Saving changes in:<<%s||&Save||Do&n't Save||&Cancel",
get_filename(sprite->getFilename()));
if (ret == 1) {
// "save": save the changes

View File

@ -52,7 +52,7 @@ void ExitCommand::onExecute(Context* context)
while (sprite) {
// check if this sprite is modified
if (sprite->isModified()) {
if (jalert("Warning<<There are sprites with changes.<<Do you want quit anyway?||&Yes||&No") != 1) {
if (Alert::show("Warning<<There are sprites with changes.<<Do you want quit anyway?||&Yes||&No") != 1) {
return;
}
break;

View File

@ -118,9 +118,9 @@ void FramePropertiesCommand::onExecute(Context* context)
int num = strtol(frlen->getText(), NULL, 10);
if (m_frame == ALL_FRAMES) {
if (jalert("Warning"
"<<Do you want to change the duration of all frames?"
"||&Yes||&No") == 1) {
if (Alert::show("Warning"
"<<Do you want to change the duration of all frames?"
"||&Yes||&No") == 1) {
SpriteWriter sprite_writer(sprite);
Undoable undoable(sprite_writer, "Constant Frame-Rate");
undoable.setConstantFrameRate(num);

View File

@ -57,7 +57,7 @@ struct OpenFileData
Monitor *monitor;
FileOp *fop;
Progress *progress;
Frame* alert_window;
AlertPtr alert_window;
};
/**
@ -162,9 +162,9 @@ void OpenFileCommand::onExecute(Context* context)
data->fop = fop;
data->progress = app_get_statusbar()->addProgress();
data->alert_window = jalert_new(PACKAGE
"<<Loading file:<<%s||&Cancel",
get_filename(m_filename.c_str()));
data->alert_window = Alert::create(PACKAGE
"<<Loading file:<<%s||&Cancel",
get_filename(m_filename.c_str()));
// Add a monitor to check the loading (FileOp) progress
data->monitor = add_gui_monitor(monitor_openfile_bg,
@ -196,7 +196,6 @@ void OpenFileCommand::onExecute(Context* context)
unrecent = true;
delete data->progress;
jwidget_free(data->alert_window);
fop_free(fop);
delete data;
}

View File

@ -419,7 +419,7 @@ static void load_command(JWidget widget)
if (!filename.empty()) {
palette = Palette::load(filename.c_str());
if (!palette) {
jalert("Error<<Loading palette file||&Close");
Alert::show("Error<<Loading palette file||&Close");
}
else {
set_new_palette(palette, "Load Palette");
@ -437,8 +437,8 @@ static void save_command(JWidget widget)
filename = ase_file_selector("Save Palette", "", "png,pcx,bmp,tga,col");
if (!filename.empty()) {
if (exists(filename.c_str())) {
ret = jalert("Warning<<File exists, overwrite it?<<%s||&Yes||&No||&Cancel",
get_filename(filename.c_str()));
ret = Alert::show("Warning<<File exists, overwrite it?<<%s||&Yes||&No||&Cancel",
get_filename(filename.c_str()));
if (ret == 2)
goto again;
@ -448,7 +448,7 @@ static void save_command(JWidget widget)
Palette* palette = get_current_palette();
if (!palette->save(filename.c_str())) {
jalert("Error<<Saving palette file||&Close");
Alert::show("Error<<Saving palette file||&Close");
}
}
}
@ -503,7 +503,7 @@ static bool sort_by_criteria(Palette* palette, int from, int to, JList selected_
static void sort_command(JWidget widget)
{
if (jalert("ASE Beta<<Sort command is not available in this beta version.||&OK")) // TODO remove this
if (Alert::show("ASE Beta<<Sort command is not available in this beta version.||&OK")) // TODO remove this
return;
SortDlgData data;
@ -786,12 +786,12 @@ static void quantize_command(JWidget widget)
const CurrentSpriteReader& sprite(UIContext::instance());
if (sprite == NULL) {
jalert("Error<<There is no sprite selected to quantize.||&OK");
Alert::show("Error<<There is no sprite selected to quantize.||&OK");
return;
}
if (sprite->getImgType() != IMAGE_RGB) {
jalert("Error<<You can use this command only for RGB sprites||&OK");
Alert::show("Error<<You can use this command only for RGB sprites||&OK");
return;
}

View File

@ -33,13 +33,13 @@
#include "sprite_wrappers.h"
#include "widgets/statebar.h"
typedef struct SaveFileData
struct SaveFileData
{
Monitor *monitor;
FileOp *fop;
Progress *progress;
Frame* alert_window;
} SaveFileData;
AlertPtr alert_window;
};
/**
* Thread to do the hard work: save the file to the disk.
@ -102,9 +102,9 @@ static void save_sprite_in_background(Sprite* sprite, bool mark_as_saved)
data->fop = fop;
data->progress = app_get_statusbar()->addProgress();
data->alert_window = jalert_new(PACKAGE
"<<Saving file:<<%s||&Cancel",
get_filename(sprite->getFilename()));
data->alert_window = Alert::create(PACKAGE
"<<Saving file:<<%s||&Cancel",
get_filename(sprite->getFilename()));
/* add a monitor to check the saving (FileOp) progress */
data->monitor = add_gui_monitor(monitor_savefile_bg,
@ -137,7 +137,6 @@ static void save_sprite_in_background(Sprite* sprite, bool mark_as_saved)
}
delete data->progress;
jwidget_free(data->alert_window);
fop_free(fop);
delete data;
}
@ -164,8 +163,8 @@ static void save_as_dialog(Sprite* sprite, const char* dlg_title, bool mark_as_s
/* does the file exist? */
if (exists(filename.c_str())) {
/* ask if the user wants overwrite the file? */
ret = jalert("Warning<<File exists, overwrite it?<<%s||&Yes||&No||&Cancel",
get_filename(filename.c_str()));
ret = Alert::show("Warning<<File exists, overwrite it?<<%s||&Yes||&No||&Cancel",
get_filename(filename.c_str()));
}
else
break;

View File

@ -73,8 +73,8 @@ void SaveMaskCommand::onExecute(Context* context)
/* does the file exist? */
if (exists(filename.c_str())) {
/* ask if the user wants overwrite the file? */
ret = jalert("Warning<<File exists, overwrite it?<<%s||&Yes||&No||&Cancel",
get_filename(filename.c_str()));
ret = Alert::show("Warning<<File exists, overwrite it?<<%s||&Yes||&No||&Cancel",
get_filename(filename.c_str()));
}
else
break;
@ -90,7 +90,7 @@ void SaveMaskCommand::onExecute(Context* context)
}
if (save_msk_file(sprite->getMask(), filename.c_str()) != 0)
jalert("Error<<Error saving .msk file<<%s||&Close", filename.c_str());
Alert::show("Error<<Error saving .msk file<<%s||&Close", filename.c_str());
}
//////////////////////////////////////////////////////////////////////

View File

@ -50,7 +50,7 @@ struct ThreadData
bool cancelled : 1; /* was the effect cancelled by the user? */
Monitor* monitor; /* monitor to update the progress-bar */
Progress* progress; /* the progress-bar */
Frame* alert_window; /* alert for the user to cancel the
AlertPtr alert_window; /* alert for the user to cancel the
effect-progress if he wants */
};
@ -157,8 +157,8 @@ void effect_apply_to_target_with_progressbar(Effect* effect)
data->done = false;
data->cancelled = false;
data->progress = app_get_statusbar()->addProgress();
data->alert_window = jalert_new(PACKAGE
"<<Applying effect...||&Cancel");
data->alert_window = Alert::create(PACKAGE
"<<Applying effect...||&Cancel");
data->monitor = add_gui_monitor(monitor_effect_bg,
monitor_free, data);
@ -180,6 +180,5 @@ void effect_apply_to_target_with_progressbar(Effect* effect)
thread.join();
delete data->progress;
jwidget_free(data->alert_window);
delete data;
}

View File

@ -603,7 +603,7 @@ static bool anieditor_msg_proc(JWidget widget, JMessage msg)
anieditor_regenerate_layers(widget);
}
else {
jalert(PACKAGE "<<You can't move the `Background' layer.||&OK");
Alert::show(PACKAGE "<<You can't move the `Background' layer.||&OK");
}
}
}

View File

@ -190,12 +190,12 @@ FileOp *fop_to_load_sprite(const char *filename, int flags)
App::instance()->isGui()) {
/* really want load all files? */
if ((fop->seq.filename_list.size() > 1) &&
(jalert("Notice"
"<<Possible animation with:"
"<<%s"
"<<Load the sequence of bitmaps?"
"||&Agree||&Skip",
get_filename(filename)) != 1)) {
(Alert::show("Notice"
"<<Possible animation with:"
"<<%s"
"<<Load the sequence of bitmaps?"
"||&Agree||&Skip",
get_filename(filename)) != 1)) {
/* if the user replies "Skip", we need just one file name (the
first one) */
@ -338,14 +338,14 @@ FileOp *fop_to_save_sprite(Sprite *sprite)
int ret;
if (fatal)
ret = jalert("Error<<File format \"%s\" doesn't support:%s"
"||&Close",
fop->format->name(), buf);
ret = Alert::show("Error<<File format \"%s\" doesn't support:%s"
"||&Close",
fop->format->name(), buf);
else
ret = jalert("Warning<<File format \"%s\" doesn't support:%s"
"<<Do you want continue?"
"||&Yes||&No",
fop->format->name(), buf);
ret = Alert::show("Warning<<File format \"%s\" doesn't support:%s"
"<<Do you want continue?"
"||&Yes||&No",
fop->format->name(), buf);
/* operation can't be done (by fatal error) or the user cancel
the operation */

View File

@ -37,92 +37,72 @@
#include "base/bind.h"
#include "gui/jinete.h"
static Frame* create_alert(char *buf, JList *labels, JList *buttons);
/* creates a new alert-box
the buttons will have names like: button-1, button-2, etc.
*/
Frame* jalert_new(const char *format, ...)
Alert::Alert()
: Frame(false, "")
{
JList labels, buttons;
char buf[4096];
Frame* window;
// Do nothing
}
AlertPtr Alert::create(const char* format, ...)
{
char buf[4096]; // TODO warning buffer overflow
va_list ap;
/* process arguments */
// Process arguments
va_start(ap, format);
vsprintf(buf, format, ap);
va_end(ap);
/* create the alert window */
labels = jlist_new();
buttons = jlist_new();
// Create the alert window
std::vector<Widget*> labels;
std::vector<Widget*> buttons;
window = create_alert(buf, &labels, &buttons);
AlertPtr window(new Alert());
window->processString(buf, labels, buttons);
jlist_free(labels);
jlist_free(buttons);
/* and return it */
return window;
}
int jalert(const char *format, ...)
// static
int Alert::show(const char* format, ...)
{
JList labels, buttons;
Frame* window;
Widget* killer;
char buf[4096];
int c, ret = 0;
JLink link;
char buf[4096]; // TODO warning buffer overflow
va_list ap;
/* process arguments */
// Process arguments
va_start(ap, format);
vsprintf(buf, format, ap);
va_end(ap);
/* create the alert window */
labels = jlist_new();
buttons = jlist_new();
// Create the alert window
std::vector<Widget*> labels;
std::vector<Widget*> buttons;
window = create_alert(buf, &labels, &buttons);
AlertPtr window(new Alert());
window->processString(buf, labels, buttons);
/* was created succefully? */
if (window) {
/* open it */
window->open_window_fg();
// Open it
window->open_window_fg();
/* check the killer */
killer = window->get_killer();
if (killer) {
c = 1;
JI_LIST_FOR_EACH(buttons, link) {
if (killer == (JWidget)link->data) {
ret = c;
break;
}
c++;
// Check the killer
int ret = 0;
if (Widget* killer = window->get_killer()) {
for (int i=0; i<(int)buttons.size(); ++i) {
if (killer == buttons[i]) {
ret = i+1;
break;
}
}
/* destroy the window */
jwidget_free(window);
}
jlist_free(labels);
jlist_free(buttons);
/* and return it */
// And return it
return ret;
}
static Frame* create_alert(char *buf, JList *labels, JList *buttons)
void Alert::processString(char* buf, std::vector<Widget*>& labels, std::vector<Widget*>& buttons)
{
Box* box1, *box2, *box3, *box4, *box5;
Grid* grid;
Frame* window = NULL;
bool title = true;
bool label = false;
bool separator = false;
@ -130,9 +110,8 @@ static Frame* create_alert(char *buf, JList *labels, JList *buttons)
int align = 0;
char *beg;
int c, chr;
JLink link;
/* process buffer */
// Process buffer
c = 0;
beg = buf;
for (; ; c++) {
@ -148,25 +127,25 @@ static Frame* create_alert(char *buf, JList *labels, JList *buttons)
buf[c] = 0;
if (title) {
window = new Frame(false, beg);
setText(beg);
}
else if (label) {
Label* label = new Label(beg);
label->setAlign(align);
jlist_append(*labels, label);
labels.push_back(label);
}
else if (separator) {
jlist_append(*labels, ji_separator_new(NULL, JI_HORIZONTAL));
labels.push_back(ji_separator_new(NULL, JI_HORIZONTAL));
}
else if (button) {
char button_name[256];
Button* button_widget = new Button(beg);
jwidget_set_min_size(button_widget, 60*jguiscale(), 0);
jlist_append(*buttons, button_widget);
buttons.push_back(button_widget);
usprintf(button_name, "button-%d", jlist_length(*buttons));
usprintf(button_name, "button-%d", buttons.size());
button_widget->setName(button_name);
button_widget->Click.connect(Bind<void>(&Frame::closeWindow, window, button_widget));
button_widget->Click.connect(Bind<void>(&Frame::closeWindow, this, button_widget));
}
buf[c] = chr;
@ -193,46 +172,42 @@ static Frame* create_alert(char *buf, JList *labels, JList *buttons)
}
}
if (window) {
box1 = new Box(JI_VERTICAL);
box2 = new Box(JI_VERTICAL);
grid = new Grid(1, false);
box3 = new Box(JI_HORIZONTAL | JI_HOMOGENEOUS);
box1 = new Box(JI_VERTICAL);
box2 = new Box(JI_VERTICAL);
grid = new Grid(1, false);
box3 = new Box(JI_HORIZONTAL | JI_HOMOGENEOUS);
/* to identify by the user */
box2->setName("labels");
box3->setName("buttons");
// To identify by the user
box2->setName("labels");
box3->setName("buttons");
/* pseudo separators (only to fill blank space) */
box4 = new Box(0);
box5 = new Box(0);
// Pseudo separators (only to fill blank space)
box4 = new Box(0);
box5 = new Box(0);
jwidget_expansive(box4, true);
jwidget_expansive(box5, true);
jwidget_noborders(box4);
jwidget_noborders(box5);
jwidget_expansive(box4, true);
jwidget_expansive(box5, true);
jwidget_noborders(box4);
jwidget_noborders(box5);
/* setup parent <-> children relationship */
// Setup parent <-> children relationship
jwidget_add_child(window, box1);
jwidget_add_child(this, box1);
jwidget_add_child(box1, box4); /* filler */
jwidget_add_child(box1, box2); /* labels */
jwidget_add_child(box1, box5); /* filler */
jwidget_add_child(box1, grid); /* buttons */
jwidget_add_child(box1, box4); // Filler
jwidget_add_child(box1, box2); // Labels
jwidget_add_child(box1, box5); // Filler
jwidget_add_child(box1, grid); // Buttons
grid->addChildInCell(box3, 1, 1, JI_CENTER | JI_BOTTOM);
grid->addChildInCell(box3, 1, 1, JI_CENTER | JI_BOTTOM);
JI_LIST_FOR_EACH(*labels, link)
jwidget_add_child(box2, (JWidget)link->data);
for (std::vector<Widget*>::iterator it = labels.begin(); it != labels.end(); ++it)
jwidget_add_child(box2, *it);
JI_LIST_FOR_EACH(*buttons, link)
jwidget_add_child(box3, (JWidget)link->data);
for (std::vector<Widget*>::iterator it = buttons.begin(); it != buttons.end(); ++it)
jwidget_add_child(box3, *it);
/* default button is the last one */
if (jlist_last(*buttons))
jwidget_magnetic((JWidget)jlist_last(*buttons)->data, true);
}
return window;
// Default button is the last one
if (!buttons.empty())
jwidget_magnetic(buttons[buttons.size()-1], true);
}

View File

@ -7,10 +7,23 @@
#ifndef GUI_ALERT_H_INCLUDED
#define GUI_ALERT_H_INCLUDED
class Frame;
#include "base/shared_ptr.h"
#include "gui/frame.h"
Frame* jalert_new(const char *format, ...);
int jalert(const char *format, ...);
class Alert;
typedef SharedPtr<Alert> AlertPtr;
class Alert : public Frame
{
public:
Alert();
static AlertPtr create(const char* format, ...);
static int show(const char* format, ...);
private:
void processString(char* buf, std::vector<Widget*>& labels, std::vector<Widget*>& buttons);
};
#endif

View File

@ -35,7 +35,6 @@ Job::Job(const char* job_name)
m_thread = NULL;
m_progress = NULL;
m_monitor = NULL;
m_alert_window = NULL;
m_last_progress = 0.0f;
m_done_flag = false;
m_canceled_flag = false;
@ -45,7 +44,7 @@ Job::Job(const char* job_name)
m_monitor = add_gui_monitor(&Job::monitor_proc,
&Job::monitor_free,
(void*)this);
m_alert_window = jalert_new("%s<<Working...||&Cancel", job_name);
m_alert_window = Alert::create("%s<<Working...||&Cancel", job_name);
}
Job::~Job()
@ -72,9 +71,6 @@ Job::~Job()
if (m_mutex)
delete m_mutex;
if (m_alert_window)
jwidget_free(m_alert_window);
}
void Job::startJob()

View File

@ -19,6 +19,8 @@
#ifndef CORE_JOB_H_INCLUDED
#define CORE_JOB_H_INCLUDED
#include "gui/alert.h"
namespace base { class thread; }
class Frame;
@ -71,7 +73,7 @@ private:
Monitor* m_monitor;
Progress* m_progress;
Mutex* m_mutex;
Frame* m_alert_window;
AlertPtr m_alert_window;
float m_last_progress;
bool m_done_flag;
bool m_canceled_flag;

View File

@ -22,7 +22,8 @@
#include "launcher.h"
#if defined ALLEGRO_WINDOWS
#include <windows.h>
#define BITMAP WINDOWS_BITMAP
#include <windows.h>
#endif
void Launcher::openUrl(const std::string& url)
@ -50,5 +51,5 @@ void Launcher::openFile(const std::string& file)
#endif
if (ret != 0)
jalert("Problem<<Cannot open:<<%s||&Close", file.c_str());
Alert::show("Problem<<Cannot open:<<%s||&Close", file.c_str());
}

View File

@ -255,7 +255,7 @@ void set_sprite_in_more_reliable_editor(Sprite* sprite)
void split_editor(Editor* editor, int align)
{
if (count_parents(editor) > 10) {
jalert("Error<<You cannot split this editor more||&Close");
Alert::show("Error<<You cannot split this editor more||&Close");
return;
}

View File

@ -181,7 +181,7 @@ static void get_win32_clipboard_bitmap(Image*& image, Palette*& palette)
if (bi) {
if (bi->bmiHeader.biCompression != BI_RGB &&
bi->bmiHeader.biCompression != BI_BITFIELDS) {
jalert("Error<<The current Windows clipboard format is not a bitmap.||&OK");
Alert::show("Error<<The current Windows clipboard format is not a bitmap.||&OK");
return;
}

View File

@ -1099,12 +1099,12 @@ bool Editor::onProcessMessage(JMessage msg)
(m_sprite->getCurrentLayer()->getType() == GFXOBJ_LAYER_IMAGE)) {
// TODO you can move the `Background' with tiled mode
if (m_sprite->getCurrentLayer()->is_background()) {
jalert(PACKAGE
"<<You can't move the `Background' layer."
"||&Close");
Alert::show(PACKAGE
"<<You can't move the `Background' layer."
"||&Close");
}
else if (!m_sprite->getCurrentLayer()->is_moveable()) {
jalert(PACKAGE "<<The layer movement is locked.||&Close");
Alert::show(PACKAGE "<<The layer movement is locked.||&Close");
}
else {
bool click2 = get_config_bool("Options", "MoveClick2", FALSE);
@ -1122,7 +1122,7 @@ bool Editor::onProcessMessage(JMessage msg)
Image* image = m_sprite->getCurrentImage(&x, &y, &opacity);
if (image) {
if (!m_sprite->getCurrentLayer()->is_writable()) {
jalert(PACKAGE "<<The layer is locked.||&Close");
Alert::show(PACKAGE "<<The layer is locked.||&Close");
return true;
}
@ -2102,24 +2102,24 @@ IToolLoop* Editor::createToolLoopImpl(Context* context, JMessage msg)
Layer* layer = sprite->getCurrentLayer();
if (!layer) {
jalert(PACKAGE "<<The current sprite does not have any layer.||&Close");
Alert::show(PACKAGE "<<The current sprite does not have any layer.||&Close");
return NULL;
}
// if the active layer is not visible
if (!layer->is_readable()) {
jalert(PACKAGE
"<<The current layer is hidden,"
"<<make it visible and try again"
"||&Close");
Alert::show(PACKAGE
"<<The current layer is hidden,"
"<<make it visible and try again"
"||&Close");
return NULL;
}
// if the active layer is read-only
else if (!layer->is_writable()) {
jalert(PACKAGE
"<<The current layer is locked,"
"<<unlock it and try again"
"||&Close");
Alert::show(PACKAGE
"<<The current layer is locked,"
"<<unlock it and try again"
"||&Close");
return NULL;
}
@ -2129,10 +2129,10 @@ IToolLoop* Editor::createToolLoopImpl(Context* context, JMessage msg)
Color bg = colorbar->getBgColor();
if (!fg.isValid() || !bg.isValid()) {
jalert(PACKAGE
"<<The current selected foreground and/or background color"
"<<is out of range. Select valid colors in the color-bar."
"||&Close");
Alert::show(PACKAGE
"<<The current selected foreground and/or background color"
"<<is out of range. Select valid colors in the color-bar."
"||&Close");
return NULL;
}