Minor changes in ui::Alert to simplify its code

This commit is contained in:
David Capello 2016-11-15 18:06:27 -03:00
parent 64af97131d
commit 7f17400178
2 changed files with 29 additions and 26 deletions

View File

@ -82,12 +82,8 @@ AlertPtr Alert::create(const char* format, ...)
va_end(ap);
// Create the alert window
std::vector<Widget*> labels;
std::vector<Widget*> buttons;
AlertPtr window(new Alert());
window->processString(buf, labels, buttons);
window->processString(buf);
return window;
}
@ -103,20 +99,21 @@ int Alert::show(const char* format, ...)
va_end(ap);
// Create the alert window
std::vector<Widget*> labels;
std::vector<Widget*> buttons;
AlertPtr window(new Alert());
window->processString(buf, labels, buttons);
window->processString(buf);
return window->show();
}
int Alert::show()
{
// Open it
window->openWindowInForeground();
openWindowInForeground();
// Check the closer
int ret = 0;
if (Widget* closer = window->closer()) {
for (int i=0; i<(int)buttons.size(); ++i) {
if (closer == buttons[i]) {
if (Widget* closer = this->closer()) {
for (int i=0; i<(int)m_buttons.size(); ++i) {
if (closer == m_buttons[i]) {
ret = i+1;
break;
}
@ -127,7 +124,7 @@ int Alert::show(const char* format, ...)
return ret;
}
void Alert::processString(char* buf, std::vector<Widget*>& labels, std::vector<Widget*>& buttons)
void Alert::processString(char* buf)
{
bool title = true;
bool label = false;
@ -158,18 +155,18 @@ void Alert::processString(char* buf, std::vector<Widget*>& labels, std::vector<W
else if (label) {
Label* label = new Label(beg);
label->setAlign(align);
labels.push_back(label);
m_labels.push_back(label);
}
else if (separator) {
labels.push_back(new Separator("", HORIZONTAL));
m_labels.push_back(new Separator("", HORIZONTAL));
}
else if (button) {
char buttonId[256];
Button* button_widget = new Button(beg);
button_widget->setMinSize(gfx::Size(60*guiscale(), 0));
buttons.push_back(button_widget);
m_buttons.push_back(button_widget);
sprintf(buttonId, "button-%lu", buttons.size());
sprintf(buttonId, "button-%lu", m_buttons.size());
button_widget->setId(buttonId);
button_widget->Click.connect(base::Bind<void>(&Window::closeWindow, this, button_widget));
}
@ -177,10 +174,10 @@ void Alert::processString(char* buf, std::vector<Widget*>& labels, std::vector<W
buf[c] = chr;
}
/* done */
// Done
if (!buf[c])
break;
/* next widget */
// Next widget
else {
title = label = separator = button = false;
beg = buf+c+2;
@ -230,15 +227,15 @@ void Alert::processString(char* buf, std::vector<Widget*>& labels, std::vector<W
grid->addChildInCell(box3, 1, 1, CENTER | BOTTOM | HORIZONTAL);
for (std::vector<Widget*>::iterator it = labels.begin(); it != labels.end(); ++it)
for (auto it=m_labels.begin(); it!=m_labels.end(); ++it)
box2->addChild(*it);
for (std::vector<Widget*>::iterator it = buttons.begin(); it != buttons.end(); ++it)
for (auto it=m_buttons.begin(); it!=m_buttons.end(); ++it)
box3->addChild(*it);
// Default button is the last one
if (!buttons.empty())
buttons[buttons.size()-1]->setFocusMagnet(true);
if (!m_buttons.empty())
m_buttons[m_buttons.size()-1]->setFocusMagnet(true);
}
} // namespace ui

View File

@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2001-2013, 2015 David Capello
// Copyright (C) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
@ -11,6 +11,8 @@
#include "base/shared_ptr.h"
#include "ui/window.h"
#include <vector>
namespace ui {
class Box;
@ -26,14 +28,18 @@ namespace ui {
void addProgress();
void setProgress(double progress);
int show();
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);
void processString(char* buf);
Slider* m_progress;
Box* m_progressPlaceholder;
std::vector<Widget*> m_labels;
std::vector<Widget*> m_buttons;
};
} // namespace ui