awtk/tools/theme_gen/theme_gen.cc

173 lines
4.4 KiB
C++
Raw Normal View History

2018-02-21 19:36:38 +08:00
/**
* File: theme_gen.cc
2018-05-15 09:31:58 +08:00
* Author: AWTK Develop Team
2018-02-21 19:36:38 +08:00
* Brief: theme_gen interface
*
2019-01-07 10:58:36 +08:00
* Copyright (c) 2018 - 2019 Guangzhou ZHIYUAN Electronics Co.,Ltd.
2018-02-21 19:36:38 +08:00
*
* 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
* License file for more details.
*
*/
/**
* History:
* ================================================================
* 2018-01-14 Li XianJing <xianjimli@hotmail.com> created
*
*/
#include "theme_gen.h"
#include "tkc/utils.h"
2018-03-30 22:15:45 +08:00
#include "base/enums.h"
2018-02-21 19:36:38 +08:00
#include "base/theme.h"
#include "tkc/buffer.h"
#include "tkc/types_def.h"
2018-02-21 19:36:38 +08:00
2018-06-03 10:19:34 +08:00
Style::Style() {
}
2018-02-21 19:36:38 +08:00
2018-12-25 15:33:44 +08:00
Style::Style(const string& widget_type, const string& name, const string& state) {
2018-03-26 11:17:38 +08:00
this->widget_type = widget_type;
2018-07-17 11:27:14 +08:00
this->name = name;
2018-02-21 19:36:38 +08:00
this->state = state;
}
2018-06-03 10:19:34 +08:00
Style::~Style() {
}
2018-02-21 19:36:38 +08:00
2018-12-25 18:00:46 +08:00
bool Style::AddInt(const string& name, int32_t value) {
for (vector<NameIntValue>::iterator i = this->int_values.begin(); i != this->int_values.end();
i++) {
if (i->name == name) {
i->value = value;
return true;
}
}
2018-02-21 19:36:38 +08:00
this->int_values.push_back(NameIntValue(name, value));
return true;
}
2018-12-25 18:00:46 +08:00
bool Style::AddString(const string& name, const string& value) {
for (vector<NameStringValue>::iterator i = this->str_values.begin(); i != this->str_values.end();
i++) {
if (i->name == name) {
i->value = value;
return true;
}
}
2018-02-21 19:36:38 +08:00
this->str_values.push_back(NameStringValue(name, value));
return true;
}
bool Style::Reset() {
this->int_values.clear();
this->str_values.clear();
return true;
}
bool Style::Merge(Style& other) {
for (vector<NameIntValue>::iterator i = other.int_values.begin(); i != other.int_values.end();
i++) {
this->AddInt(i->name, i->value);
}
for (vector<NameStringValue>::iterator i = other.str_values.begin(); i != other.str_values.end();
i++) {
this->AddString(i->name, i->value);
}
return true;
}
2018-02-21 19:36:38 +08:00
uint8_t* Style::Output(uint8_t* buff, uint32_t max_size) {
uint32_t size = 0;
uint8_t* p = buff;
uint8_t* end = buff + max_size;
return_value_if_fail(buff != NULL && max_size > 32, NULL);
size = this->int_values.size();
save_uint32(p, size);
2018-12-25 15:33:44 +08:00
printf(" size=%d widget_type=%s name=%s state=%s\n", size, this->widget_type.c_str(),
this->name.c_str(), this->state.c_str());
2018-02-21 19:36:38 +08:00
for (vector<NameIntValue>::iterator i = this->int_values.begin(); i != this->int_values.end();
i++) {
2018-12-25 18:00:46 +08:00
const string& name = i->name;
2018-02-21 19:36:38 +08:00
uint32_t value = i->value;
2018-12-25 18:00:46 +08:00
style_int_data_t data;
2018-03-30 22:15:45 +08:00
2018-12-25 18:00:46 +08:00
data.value = value;
2018-12-26 09:14:02 +08:00
tk_strncpy(data.name, name.c_str(), TK_NAME_LEN);
2018-02-21 19:36:38 +08:00
2018-12-25 18:00:46 +08:00
return_value_if_fail((end - p) > sizeof(data), NULL);
memcpy(p, &data, sizeof(data));
p += sizeof(data);
printf(" %s=0x%08x\n", data.name, data.value);
2018-02-21 19:36:38 +08:00
}
return_value_if_fail((end - p) > 32, NULL);
size = this->str_values.size();
save_uint32(p, size);
for (vector<NameStringValue>::iterator i = this->str_values.begin(); i != this->str_values.end();
i++) {
2018-12-25 18:00:46 +08:00
style_str_data_t data;
const string& name = i->name;
const string& value = i->value;
2018-03-30 22:15:45 +08:00
2018-12-26 09:14:02 +08:00
tk_strncpy(data.name, name.c_str(), TK_NAME_LEN);
tk_strncpy(data.value, value.c_str(), TK_NAME_LEN);
2018-02-21 19:36:38 +08:00
2018-12-25 18:00:46 +08:00
return_value_if_fail((end - p) > sizeof(data), NULL);
memcpy(p, &data, sizeof(data));
p += sizeof(data);
2018-03-30 22:15:45 +08:00
2018-12-25 18:00:46 +08:00
printf(" %s=%s\n", data.name, data.value);
2018-02-21 19:36:38 +08:00
}
return p;
}
bool ThemeGen::AddStyle(const Style& style) {
this->styles.push_back(style);
return true;
}
uint8_t* ThemeGen::Output(uint8_t* buff, uint32_t max_size) {
2018-06-25 17:27:17 +08:00
uint32_t nr = this->styles.size();
2018-02-21 19:36:38 +08:00
uint8_t* end = buff + max_size;
2018-06-25 17:27:17 +08:00
theme_header_t* header = (theme_header_t*)buff;
uint32_t data_start = sizeof(theme_header_t) + nr * sizeof(theme_item_t);
theme_item_t* item = (theme_item_t*)(buff + sizeof(theme_header_t));
uint8_t* p = buff + data_start;
2018-02-21 19:36:38 +08:00
2018-06-25 17:27:17 +08:00
return_value_if_fail(p != NULL && max_size > data_start + 128, NULL);
2018-02-21 19:36:38 +08:00
2018-06-25 17:27:17 +08:00
memset(buff, 0x00, max_size);
header->magic = THEME_MAGIC;
header->version = 0;
header->nr = nr;
2018-02-21 19:36:38 +08:00
for (vector<Style>::iterator iter = this->styles.begin(); iter != this->styles.end(); iter++) {
2018-06-25 17:27:17 +08:00
item->offset = p - buff;
2018-12-26 09:14:02 +08:00
tk_strncpy(item->state, iter->state.c_str(), TK_NAME_LEN);
tk_strncpy(item->name, iter->name.c_str(), TK_NAME_LEN);
tk_strncpy(item->widget_type, iter->widget_type.c_str(), TK_NAME_LEN);
2018-02-21 19:36:38 +08:00
p = iter->Output(p, end - p);
2018-06-25 17:27:17 +08:00
item++;
2018-02-21 19:36:38 +08:00
}
return p;
}