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

View File

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