aseprite/src/dialogs/repo.cpp

252 lines
6.5 KiB
C++
Raw Normal View History

2007-11-17 02:25:45 +08:00
/* ASE - Allegro Sprite Editor
2011-01-19 07:49:53 +08:00
* Copyright (C) 2001-2011 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
*/
#include "config.h"
#include <allegro/gfx.h>
#include <allegro/unicode.h>
#include "base/bind.h"
#include "gui/gui.h"
2007-09-19 07:57:02 +08:00
#include "dialogs/repo.h"
#include "ini_file.h"
2007-09-19 07:57:02 +08:00
#include "modules/gui.h"
static void fill_listbox(RepoDlg *repo_dlg);
static void kill_listbox(RepoDlg *repo_dlg);
2007-09-19 07:57:02 +08:00
static int repo_listbox_type();
static bool repo_listbox_msg_proc(JWidget widget, Message* msg);
2007-09-19 07:57:02 +08:00
static void use_command(Button* widget, RepoDlg* repo_dlg);
static void add_command(Button* widget, RepoDlg* repo_dlg);
static void delete_command(Button* widget, RepoDlg* repo_dlg);
2007-09-19 07:57:02 +08:00
void ji_show_repo_dlg(RepoDlg *repo_dlg)
2007-09-19 07:57:02 +08:00
{
Frame* window = new Frame(false, repo_dlg->title);
2011-01-25 06:48:09 +08:00
Box* box1 = new Box(JI_HORIZONTAL);
Box* box2 = new Box(JI_VERTICAL);
View* view = new View();
repo_dlg->listbox = jlistbox_new();
repo_dlg->button_use = new Button(repo_dlg->use_text);
repo_dlg->button_add = new Button("&Add");
repo_dlg->button_delete = new Button("&Delete");
Button* button_close = new Button("&Close");
2007-09-19 07:57:02 +08:00
jwidget_add_hook(repo_dlg->listbox, repo_listbox_type(),
repo_listbox_msg_proc, repo_dlg);
2007-09-19 07:57:02 +08:00
repo_dlg->button_use->Click.connect(Bind<void>(&use_command, repo_dlg->button_use, repo_dlg));
repo_dlg->button_add->Click.connect(Bind<void>(&add_command, repo_dlg->button_add, repo_dlg));
repo_dlg->button_delete->Click.connect(Bind<void>(&delete_command, repo_dlg->button_delete, repo_dlg));
button_close->Click.connect(Bind<void>(&Frame::closeWindow, window, button_close));
2007-09-19 07:57:02 +08:00
jwidget_magnetic(repo_dlg->button_use, true);
2007-09-19 07:57:02 +08:00
jwidget_expansive(view, true);
view->attachToView(repo_dlg->listbox);
jwidget_set_min_size(view, JI_SCREEN_W*25/100, JI_SCREEN_H*25/100);
2007-09-19 07:57:02 +08:00
/* fill the list */
fill_listbox(repo_dlg);
jlistbox_select_index(repo_dlg->listbox, 0);
2007-09-19 07:57:02 +08:00
/* no items? */
if (jlistbox_get_selected_index(repo_dlg->listbox) != 0) {
repo_dlg->button_use->setEnabled(false);
repo_dlg->button_delete->setEnabled(false);
2007-09-19 07:57:02 +08:00
}
/* hierarchy */
box2->addChild(repo_dlg->button_use);
box2->addChild(repo_dlg->button_add);
box2->addChild(repo_dlg->button_delete);
box2->addChild(button_close);
box1->addChild(box2);
box1->addChild(view);
window->addChild(box1);
2007-09-19 07:57:02 +08:00
/* default position */
window->remap_window();
window->center_window();
2007-09-19 07:57:02 +08:00
/* load window configuration */
load_window_pos(window, repo_dlg->config_section);
2007-09-19 07:57:02 +08:00
/* open window */
window->open_window_fg();
2007-09-19 07:57:02 +08:00
/* kill the list */
kill_listbox(repo_dlg);
2007-09-19 07:57:02 +08:00
/* save window configuration */
save_window_pos(window, repo_dlg->config_section);
2007-09-19 07:57:02 +08:00
jwidget_free(window);
2007-09-19 07:57:02 +08:00
}
static void fill_listbox(RepoDlg *repo_dlg)
2007-09-19 07:57:02 +08:00
{
/* no item selected (ID=0) */
repo_dlg->listitem = 0;
/* load the entries */
if (repo_dlg->load_listbox)
(*repo_dlg->load_listbox)(repo_dlg);
2007-09-19 07:57:02 +08:00
View::getView(repo_dlg->listbox)->updateView();
2007-09-19 07:57:02 +08:00
}
static void kill_listbox(RepoDlg *repo_dlg)
2007-09-19 07:57:02 +08:00
{
JWidget listbox = repo_dlg->listbox;
JWidget listitem;
JLink link, next;
/* save the entries */
if (repo_dlg->save_listbox)
(*repo_dlg->save_listbox) (repo_dlg);
/* remove all items */
JI_LIST_FOR_EACH_SAFE(listbox->children, link, next) {
2008-10-01 05:01:54 +08:00
listitem = reinterpret_cast<JWidget>(link->data);
2007-09-19 07:57:02 +08:00
repo_dlg->listbox->removeChild(listitem);
2007-09-19 07:57:02 +08:00
if (repo_dlg->free_listitem) {
repo_dlg->listitem = listitem;
(*repo_dlg->free_listitem) (repo_dlg);
}
jwidget_free(listitem);
2007-09-19 07:57:02 +08:00
}
}
static int repo_listbox_type()
2007-09-19 07:57:02 +08:00
{
static int type = 0;
if (!type)
type = ji_register_widget_type();
return type;
}
static bool repo_listbox_msg_proc(JWidget widget, Message* msg)
2007-09-19 07:57:02 +08:00
{
2008-10-01 05:01:54 +08:00
RepoDlg* repo_dlg = reinterpret_cast<RepoDlg*>(jwidget_get_data(widget, repo_listbox_type()));
2007-09-19 07:57:02 +08:00
switch (msg->type) {
case JM_DESTROY:
/* do nothing (don't free repo_dlg) */
break;
case JM_SIGNAL:
switch (msg->signal.num) {
case JI_SIGNAL_LISTBOX_CHANGE:
repo_dlg->listitem = jlistbox_get_selected_child(widget);
repo_dlg->button_use->setEnabled(true);
repo_dlg->button_delete->setEnabled(true);
2007-09-19 07:57:02 +08:00
break;
case JI_SIGNAL_LISTBOX_SELECT:
if (repo_dlg->use_listitem)
if (!(*repo_dlg->use_listitem)(repo_dlg))
2011-01-22 10:49:07 +08:00
widget->closeWindow();
2007-09-19 07:57:02 +08:00
break;
}
break;
}
return false;
2007-09-19 07:57:02 +08:00
}
static void use_command(Button* widget, RepoDlg* repo_dlg)
2007-09-19 07:57:02 +08:00
{
if (repo_dlg->use_listitem) {
if (!(*repo_dlg->use_listitem)(repo_dlg))
2011-01-22 10:49:07 +08:00
widget->closeWindow();
2007-09-19 07:57:02 +08:00
}
}
static void add_command(Button* widget, RepoDlg* repo_dlg)
2007-09-19 07:57:02 +08:00
{
int ret, added;
if (repo_dlg->add_listitem) {
added = false;
2007-09-19 07:57:02 +08:00
ret = (*repo_dlg->add_listitem)(repo_dlg, &added);
if (added) {
/* update the list-box */
View::getView(repo_dlg->listbox)->updateView();
2007-09-19 07:57:02 +08:00
/* select the last item */
jlistbox_select_index(repo_dlg->listbox,
jlist_length(repo_dlg->listbox->children)-1);
2007-09-19 07:57:02 +08:00
}
if (!ret)
2011-01-22 10:49:07 +08:00
widget->closeWindow();
2007-09-19 07:57:02 +08:00
}
}
static void delete_command(Button* widget, RepoDlg* repo_dlg)
2007-09-19 07:57:02 +08:00
{
int ret, kill, index, last;
if (repo_dlg->delete_listitem) {
kill = false;
2007-09-19 07:57:02 +08:00
ret = (*repo_dlg->delete_listitem) (repo_dlg, &kill);
if (kill) {
index = jlistbox_get_selected_index(repo_dlg->listbox);
2007-09-19 07:57:02 +08:00
/* delete "repo_dlg->listitem" */
repo_dlg->listbox->removeChild(repo_dlg->listitem);
2007-09-19 07:57:02 +08:00
if (repo_dlg->free_listitem)
(*repo_dlg->free_listitem) (repo_dlg);
jwidget_free(repo_dlg->listitem);
2007-09-19 07:57:02 +08:00
/* disable buttons */
if (!repo_dlg->listbox->children) {
repo_dlg->button_use->setEnabled(false);
repo_dlg->button_delete->setEnabled(false);
2007-09-19 07:57:02 +08:00
}
/* select other item */
else {
last = jlist_length(repo_dlg->listbox->children)-1;
2007-09-19 07:57:02 +08:00
jlistbox_select_index(repo_dlg->listbox,
index > last ? last: index);
2007-09-19 07:57:02 +08:00
}
/* update the list-box */
View::getView(repo_dlg->listbox)->updateView();
2007-09-19 07:57:02 +08:00
}
if (!ret)
2011-01-22 10:49:07 +08:00
widget->closeWindow();
2007-09-19 07:57:02 +08:00
}
}