awtk/demos/preview_ui.c

234 lines
5.2 KiB
C
Raw Normal View History

2018-03-11 11:26:12 +08:00
/**
2019-08-08 10:53:54 +08:00
* File: preview_ui.c
2018-05-15 09:31:58 +08:00
* Author: AWTK Develop Team
2019-08-08 10:53:54 +08:00
* Brief: preview ui
2018-03-11 11:26:12 +08:00
*
2020-01-01 11:27:36 +08:00
* Copyright (c) 2018 - 2020 Guangzhou ZHIYUAN Electronics Co.,Ltd.
2018-03-11 11:26:12 +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-02-16 Li XianJing <xianjimli@hotmail.com> created
*
*/
2018-08-05 18:56:46 +08:00
#include "awtk.h"
#include "tkc/fs.h"
#include "tkc/mem.h"
#include "tkc/utils.h"
2018-10-10 17:30:59 +08:00
#include "base/timer.h"
2018-08-24 07:45:37 +08:00
#include "assets.h"
2019-10-24 18:12:21 +08:00
#include "tkc/utf8.h"
2018-08-07 11:29:55 +08:00
2018-03-11 11:26:12 +08:00
#include "ui_loader/ui_loader_xml.h"
2018-06-08 17:03:06 +08:00
#include "ui_loader/ui_loader_default.h"
2018-03-11 11:26:12 +08:00
#include "ui_loader/ui_builder_default.h"
2020-05-21 20:35:02 +08:00
#if defined(WITH_SDL) && !defined(MINGW)
2019-10-24 18:12:21 +08:00
#include <SDL.h>
#endif
2019-05-31 11:05:36 +08:00
static ret_t refresh_in_timer(const timer_info_t* info) {
2018-10-10 17:30:59 +08:00
widget_t* widget = WIDGET(info->ctx);
widget_invalidate(widget, NULL);
return RET_REPEAT;
}
2018-06-08 17:03:06 +08:00
widget_t* preview_ui(const char* filename) {
str_t s;
2018-03-11 11:26:12 +08:00
uint32_t size = 0;
2021-06-09 13:58:02 +08:00
widget_t* root = NULL;
2018-12-26 09:14:02 +08:00
char name[TK_NAME_LEN + 1];
2018-06-15 19:12:34 +08:00
ui_builder_t* builder = NULL;
uint8_t* content = NULL;
bool_t is_bin = strstr(filename, ".bin") != NULL;
ui_loader_t* loader = is_bin ? default_ui_loader() : xml_ui_loader();
str_init(&s, 0);
if (is_bin) {
content = (uint8_t*)file_read(filename, &size);
} else {
xml_file_expand_read(filename, &s);
content = (uint8_t*)s.str;
size = s.size;
}
2018-06-08 17:03:06 +08:00
2018-12-26 09:14:02 +08:00
filename_to_name(filename, name, TK_NAME_LEN);
2021-06-09 13:58:02 +08:00
builder = ui_builder_default_create(name);
2018-03-11 11:26:12 +08:00
printf("preview %s\n", filename);
2018-03-11 14:21:19 +08:00
return_value_if_fail(content != NULL, NULL);
2018-03-11 11:26:12 +08:00
ui_loader_load(loader, content, size, builder);
2019-05-31 11:05:36 +08:00
if (builder->root == NULL) {
builder->root = label_create(NULL, 10, 10, 100, 30);
widget_set_text_utf8(builder->root, "not found ui file!");
}
2019-04-12 17:21:28 +08:00
if (builder->root != NULL && !(builder->root->vt->is_window)) {
widget_t* win = window_create(NULL, 0, 0, 0, 0);
widget_add_child(win, builder->root);
widget_layout(win);
2019-05-31 11:05:36 +08:00
timer_add(refresh_in_timer, builder->root, 1000);
2019-04-12 17:21:28 +08:00
}
if (is_bin) {
TKMEM_FREE(content);
} else {
str_reset(&s);
}
2018-03-11 11:26:12 +08:00
2021-06-09 13:58:02 +08:00
root = builder->root;
ui_builder_destroy(builder);
return root;
2018-03-11 11:26:12 +08:00
}
#include "tkc/path.h"
2018-08-24 07:45:37 +08:00
#define DEFAULT_UI "./demos/assets/raw/ui/main.xml"
2020-05-21 20:35:02 +08:00
#if defined(WIN32) && !defined(MINGW)
2018-03-11 11:26:12 +08:00
#include <windows.h>
2019-10-24 18:12:21 +08:00
#define MAX_ARGV 7
void command_line_to_argv(char* lpcmdline, const char* argv[MAX_ARGV], int32_t* argc) {
int32_t i = 1;
char* p = lpcmdline;
argv[0] = "preview.exe";
for (i = 1; i < MAX_ARGV; i++) {
argv[i] = p;
2019-10-24 18:12:21 +08:00
if (*p == '\"') {
argv[i] = p + 1;
p = strchr(p + 1, '\"');
if (p == NULL) break;
*p++ = '\0';
if (*p == 0) break;
} else {
p = strchr(p, ' ');
}
if (p == NULL) {
break;
}
while (*p == ' ') {
*p++ = '\0';
}
}
*argc = i + 1;
return;
}
2019-10-24 18:12:21 +08:00
int WINAPI wWinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPWSTR lpcmdline, int ncmdshow) {
str_t str;
2018-03-11 11:26:12 +08:00
int argc = 1;
2019-10-24 18:12:21 +08:00
int32_t len = wcslen(lpcmdline) * 6;
char* utf8_line = (char*)TKMEM_ALLOC(len);
tk_utf8_from_utf16(lpcmdline, utf8_line, len);
char* argv[MAX_ARGV];
str_init(&str, 1024);
2019-10-24 18:12:21 +08:00
str_set(&str, utf8_line);
command_line_to_argv(str.str, argv, &argc);
2019-05-21 16:23:10 +08:00
#if defined(WIN32)
2019-11-20 15:47:39 +08:00
TK_ENABLE_CONSOLE();
2019-05-21 16:23:10 +08:00
#endif /*WIN32*/
2018-03-11 11:26:12 +08:00
#else
#include "tkc/mem.h"
2018-03-11 11:26:12 +08:00
int main(int argc, char* argv[]) {
#endif
int32_t w = 320;
int32_t h = 480;
2019-10-24 18:12:21 +08:00
bool_t have_lang = FALSE;
const char* filename = DEFAULT_UI;
2020-03-16 21:19:20 +08:00
const char* theme = "default";
2019-07-24 11:05:34 +08:00
char res_root[MAX_PATH + 1];
2019-10-24 18:12:21 +08:00
char country[3];
char language[3];
2019-07-24 11:05:34 +08:00
memset(res_root, 0x00, sizeof(res_root));
2018-03-11 11:26:12 +08:00
if (argc > 1) {
filename = argv[1];
} else {
2020-03-16 21:19:20 +08:00
log_debug("%s ui_file [w] [h] [res root] [language] [theme]\n", argv[0]);
#ifdef WIN32
assert(!"no ui file provided");
#endif /*WIN32*/
exit(0);
}
if (argc > 2) {
int32_t ww = atoi(argv[2]);
if (ww > 0) {
w = ww;
}
}
if (argc > 3) {
int32_t hh = atoi(argv[3]);
if (h > 0) {
h = hh;
}
}
2019-10-24 18:12:21 +08:00
if (argc > 4) {
2019-07-24 11:05:34 +08:00
memcpy(res_root, argv[4], strlen(argv[4]));
} else {
char* p = NULL;
memcpy(res_root, filename, strlen(filename));
p = strstr(res_root, "assets");
if (p != NULL) {
*p = '\0';
}
}
log_debug("%s %s %d %d %s\n", argv[0], argv[1], w, h, res_root);
2019-10-24 18:12:21 +08:00
if (argc > 5) {
char str[MAX_PATH + 1];
memset(str, 0x00, sizeof(str));
memcpy(str, argv[5], strlen(argv[5]));
tk_strncpy(language, str, 2);
tk_strncpy(country, str + 3, 2);
have_lang = TRUE;
}
2020-03-16 21:19:20 +08:00
if (argc > 6) {
theme = argv[6];
}
2020-03-14 12:11:32 +08:00
tk_init(w, h, APP_SIMULATOR, NULL, res_root);
2019-10-25 11:19:14 +08:00
assets_manager_set_res_root(assets_manager(), system_info()->app_root);
2019-07-01 17:40:15 +08:00
#ifdef WITH_FS_RES
2019-10-25 18:20:34 +08:00
system_info_set_default_font(system_info(), "default_full");
2019-07-01 17:40:15 +08:00
#endif /*WITH_FS_RES*/
2020-03-18 21:36:24 +08:00
assets_init();
2018-08-07 11:29:55 +08:00
tk_ext_widgets_init();
2018-07-28 19:45:28 +08:00
2020-03-18 21:36:24 +08:00
if (!tk_str_eq(theme, "default")) {
widget_set_theme(window_manager(), theme);
}
2018-08-17 18:11:08 +08:00
preview_ui(filename);
2019-10-24 18:12:21 +08:00
if (have_lang) {
locale_info_change(locale_info(), language, country);
}
widget_set_style_str(window_manager(), "bg_color", "white");
2018-04-27 11:23:09 +08:00
tk_run();
2018-03-11 11:26:12 +08:00
2020-05-21 20:35:02 +08:00
#if defined(WIN32) && !defined(MINGW)
2019-10-24 18:12:21 +08:00
TKMEM_FREE(utf8_line);
#endif
2018-03-11 11:26:12 +08:00
return 0;
}