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
|
|
|
|
*
|
2018-05-08 10:22:32 +08:00
|
|
|
* Copyright (c) 2018 - 2018 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"
|
2018-12-15 17:22:05 +08:00
|
|
|
#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"
|
2018-12-15 17:22:05 +08:00
|
|
|
#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
|
|
|
|
|
|
|
bool Style::AddInt(uint32_t name, int32_t value) {
|
2018-03-31 19:48:49 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Style::AddString(uint32_t name, const string& value) {
|
2018-03-31 19:48:49 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-31 19:48:49 +08:00
|
|
|
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++) {
|
|
|
|
uint32_t name = i->name;
|
|
|
|
uint32_t value = i->value;
|
2018-03-30 22:15:45 +08:00
|
|
|
const key_type_value_t* item = style_id_find_by_value(name);
|
|
|
|
|
2018-02-21 19:36:38 +08:00
|
|
|
return_value_if_fail((end - p) > 8, NULL);
|
|
|
|
|
|
|
|
save_uint32(p, name);
|
|
|
|
save_uint32(p, value);
|
2018-03-30 22:15:45 +08:00
|
|
|
if (item != NULL) {
|
|
|
|
printf(" %s=0x%08x\n", item->name, 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++) {
|
|
|
|
uint32_t name = i->name;
|
|
|
|
string value = i->value;
|
|
|
|
uint32_t s_size = value.size();
|
2018-03-30 22:15:45 +08:00
|
|
|
const key_type_value_t* item = style_id_find_by_value(name);
|
|
|
|
|
2018-02-21 19:36:38 +08:00
|
|
|
return_value_if_fail((end - p) > s_size + 5, NULL);
|
|
|
|
|
|
|
|
save_uint32(p, name);
|
|
|
|
memcpy(p, value.c_str(), s_size + 1);
|
|
|
|
p += s_size + 1;
|
2018-03-30 22:15:45 +08:00
|
|
|
|
|
|
|
if (item != NULL) {
|
|
|
|
printf(" %s=%s\n", item->name, value.c_str());
|
|
|
|
}
|
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-25 15:33:44 +08:00
|
|
|
tk_strncpy(item->state, iter->state.c_str(), NAME_LEN);
|
2018-07-17 11:27:14 +08:00
|
|
|
tk_strncpy(item->name, iter->name.c_str(), NAME_LEN);
|
2018-06-25 17:27:17 +08:00
|
|
|
tk_strncpy(item->widget_type, iter->widget_type.c_str(), 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;
|
|
|
|
}
|