#include <ucx/map.h>
#include <inttypes.h>
#include "toolbar.h"
#include "menu.h"
#include "stock.h"
static UcxMap *toolbar_items = ucx_map_new(
16);
static UcxList *defaults;
UiToolItem::UiToolItem(
char *label, ui_callback f,
void *userdata) {
this->label = label;
this->image =
NULL;
this->callback = f;
this->userdata = userdata;
this->isimportant = false;
this->groups =
NULL;
}
void UiToolItem::addGroup(
int group) {
groups = ucx_list_append(groups, (
void*)(
intptr_t)group);
}
void UiToolItem::addTo(UiObject *obj, QToolBar *toolbar) {
QString str = QString::fromUtf8(label);
UiAction *action = new UiAction(obj, str, callback, userdata);
action->setIcon(QIcon::fromTheme(image));
toolbar->addAction(action);
QObject::connect(action,
SIGNAL(triggered()), action,
SLOT(trigger()));
}
UiStockToolItem::UiStockToolItem(
char *stockid, ui_callback f,
void *userdata) {
this->stockid = stockid;
this->callback = f;
this->userdata = userdata;
this->isimportant = false;
this->groups =
NULL;
}
void UiStockToolItem::addGroup(
int group) {
groups = ucx_list_append(groups, (
void*)(
intptr_t)group);
}
void UiStockToolItem::addTo(UiObject *obj, QToolBar *toolbar) {
UiStockItem *stockItem = ui_get_stock_item(stockid);
QString str = QString::fromUtf8(stockItem->label);
UiAction *action = new UiAction(obj, str, callback, userdata);
action->setIcon(QIcon::fromTheme(stockItem->icon_name));
toolbar->addAction(action);
QObject::connect(action,
SIGNAL(triggered()), action,
SLOT(trigger()));
}
void ui_toolitem_vstgr(
char *name,
char *stockid,
int isimportant,
ui_callback f,
void *userdata,
va_list ap)
{
UiStockToolItem *item = new UiStockToolItem(stockid, f, userdata);
item->isimportant = isimportant;
int group;
while((group = va_arg(ap,
int)) != -
1) {
item->addGroup(group);
}
ucx_map_cstr_put(toolbar_items, name, item);
}
void ui_toolitem_img(
char *name,
char *label,
char *img, ui_callback f,
void *udata) {
UiToolItem *item = new UiToolItem(label, f, udata);
item->image = img;
item->isimportant = false;
ucx_map_cstr_put(toolbar_items, name, item);
}
void ui_toolitem(
char *name,
char *label, ui_callback f,
void *udata) {
ui_toolitem_img(name, label,
NULL, f, udata);
}
void ui_toolitem_st(
char *name,
char *stockid, ui_callback f,
void *userdata) {
ui_toolitem_stgr(name, stockid, f, userdata, -
1);
}
void ui_toolitem_sti(
char *name,
char *stockid, ui_callback f,
void *userdata) {
ui_toolitem_stgri(name, stockid, f, userdata, -
1);
}
void ui_toolitem_stgr(
char *name,
char *stockid, ui_callback f,
void *userdata, ...) {
va_list ap;
va_start(ap, userdata);
ui_toolitem_vstgr(name, stockid,
0, f, userdata, ap);
va_end(ap);
}
void ui_toolitem_stgri(
char *name,
char *stockid, ui_callback f,
void *userdata, ...) {
va_list ap;
va_start(ap, userdata);
ui_toolitem_vstgr(name, stockid,
1, f, userdata, ap);
va_end(ap);
}
void ui_toolbar_add_default(
char *name) {
char *s = strdup(name);
defaults = ucx_list_append(defaults, s);
}
QToolBar* ui_create_toolbar(UiObject *obj) {
QToolBar *toolbar = new QToolBar();
UCX_FOREACH(elm, defaults) {
UiToolItemI *item = (UiToolItemI*)ucx_map_cstr_get(toolbar_items, (
char*)elm->data);
if(item) {
item->addTo(obj, toolbar);
}
else if(!strcmp((
char*)elm->data,
"@separator")) {
}
else {
fprintf(stderr,
"UI Error: Unknown toolbar item: %s\n", elm->data);
}
}
return toolbar;
}