static Ret ftk_app_window_on_event(FtkWidget* thiz, FtkEvent* event) { DECL_PRIV1(thiz, priv); if(event->type == FTK_EVT_KEY_UP && event->u.key.code == FTK_KEY_MENU && priv->on_prepare_options_menu != NULL) { FtkWidget* menu_panel = ftk_menu_panel_create(); if(priv->on_prepare_options_menu(priv->on_prepare_options_menu_ctx, menu_panel) == RET_OK) { ftk_menu_panel_relayout(menu_panel); ftk_widget_show_all(menu_panel, 1); } else { ftk_widget_unref(menu_panel); } ftk_logd("%s\n", __func__); return RET_REMOVE; } else if(event->type == FTK_EVT_KEY_UP && event->u.key.code == FTK_KEY_RETURN) { if(!ftk_widget_has_attr(thiz, FTK_ATTR_IGNORE_CLOSE)) { ftk_widget_unref(thiz); } return RET_REMOVE; } return priv->parent_on_event(thiz, event); }
static Ret button_close_top_clicked(void* ctx, void* obj) { FtkWidget* panel = ftk_default_status_panel(); FtkWidget* title_widget = ftk_widget_lookup(panel, IDC_TITLE_ITEM); FtkWidget* top_window = (FtkWidget*)ftk_widget_user_data(title_widget); if(top_window != NULL && ftk_widget_type(top_window) == FTK_WINDOW) { if(!ftk_widget_has_attr(top_window, FTK_ATTR_IGNORE_CLOSE)) { ftk_widget_set_user_data(title_widget, NULL, NULL); ftk_logd("%s: close window %s\n", __func__, ftk_widget_get_text(top_window)); ftk_widget_unref(top_window); } else { ftk_logd("%s: the top window has attribute FTK_ATTR_IGNORE_CLOSE\n", __func__); } } else { ftk_logd("%s: not normal window.\n", __func__); } return RET_OK; }
static Ret ftk_file_browser_on_create_folder(void* ctx, void* obj) { int ret = 0; const char* name = NULL; FtkWidget* win = (FtkWidget*)ctx; FtkWidget* dialog = NULL; PrivInfo* priv = (PrivInfo*)ftk_widget_user_data(win); dialog = ftk_file_browser_input_dialog(win, _("Please input folder name:")); ret = ftk_dialog_run(dialog); if(ret == ID_OK) { name = ftk_entry_get_text(ftk_widget_lookup(dialog, ID_ENTRY)); if(name != NULL) { char folder_name[FTK_MAX_PATH+1] = {0}; ftk_strs_cat(folder_name, FTK_MAX_PATH, priv->path, "/", name, NULL); if(ftk_fs_create_dir(folder_name) == RET_OK) { ftk_file_browser_load(win); } else { ftk_tips(_("Create folder failed.")); } ftk_logd("%s: create %s\n", __func__, folder_name); } } ftk_widget_unref(dialog); ftk_logd("%s: ret=%d\n", __func__, ret); return RET_OK; }
static Ret ftk_music_on_shutdown(void* ctx, void* obj) { system("killall mplayer"); ftk_widget_unref(ctx); return RET_OK; }
static Ret button_close_clicked(void* ctx, void* obj) { FtkWidget* win = ctx; ftk_logd("%s: close window %s\n", __func__, ftk_widget_get_text(win)); ftk_widget_unref(win); return RET_OK; }
static Ret button_cancel_clicked(void* ctx, void* obj) { FtkWidget* win = (FtkWidget*)ctx; ftk_widget_unref(win); return RET_OK; }
static Ret designer_on_quit(void* ctx, void* item) { FtkWidget* win = (FtkWidget*)ctx; ftk_widget_unref(win); return RET_OK; }
static Ret ftk_file_browser_on_item_clicked(void* ctx, void* list) { FtkWidget* win = (FtkWidget*)ctx; FtkListItemInfo* info = NULL; int i = ftk_list_view_get_selected((FtkWidget*)list); PrivInfo* priv = (PrivInfo*)ftk_widget_user_data(win); FtkListModel* model = ftk_list_view_get_model((FtkWidget*)list); return_val_if_fail(priv != NULL, RET_FAIL); ftk_list_model_get_data(model, i, (void**)&info); if(info != NULL && info->text != NULL) { char* p = NULL; char path[FTK_MAX_PATH+1] = {0}; char* file_name = info->text; if(strcmp(file_name, _("..")) == 0) { ftk_strcpy(path, priv->path); if((p = strrchr(path, FTK_PATH_DELIM)) != NULL) { if(p == path) { p[1] = '\0'; } else { *p = '\0'; } } ftk_file_browser_set_path(win, path); ftk_file_browser_load(win); } else if(info->value) /*enter selected folder*/ { ftk_strs_cat(path, FTK_MAX_PATH, priv->path, "/", file_name, NULL); ftk_file_browser_set_path(win, path); ftk_file_browser_load(win); } else if(priv->type == FTK_FILE_BROWER_SINGLE_CHOOSER) { if(priv->on_choosed != NULL) { ftk_strs_cat(path, FTK_MAX_PATH, priv->path, "/", file_name, NULL); priv->on_choosed(priv->on_choosed_ctx, 0, path); ftk_widget_unref(win); } } else if(priv->type == FTK_FILE_BROWER_MULTI_CHOOSER) { info->state = !info->state; } } return RET_OK; }
void ftk_widget_unref(FtkWidget* thiz) { if(thiz != NULL) { if(thiz->children != NULL) { ftk_widget_unref(thiz->children); } if(thiz->next != NULL) { ftk_widget_unref(thiz->next); } ftk_widget_unref_self(thiz); } return; }
static Ret ftk_bluetooth_on_shutdown(void* ctx, void* obj) { if (_timer) { ftk_main_loop_remove_source(ftk_default_main_loop(), _timer); _timer = NULL; } ftk_widget_unref(ctx); return RET_OK; }
static Ret button_dialog_clicked(void* ctx, void* obj) { int id = 0; int width = 0; int height = 0; // FtkWidget* label = NULL; FtkWidget* button = NULL; FtkWidget* dialog = NULL; FtkBitmap* icon = NULL; FtkWidget* combo_box = NULL; int modal = (int)ctx; ftk_logi("%s:%d begin\n", __func__, __LINE__); dialog = ftk_dialog_create(0, 40, 320, 240); icon = ftk_theme_load_image(ftk_default_theme(), "info"FTK_STOCK_IMG_SUFFIX); ftk_dialog_set_icon(dialog, icon); width = ftk_widget_width(dialog); height = ftk_widget_height(dialog); // label = ftk_label_create(dialog, width/6, height/4, 5*width/6, 20); // ftk_widget_set_text(label, "Are you sure to quit?"); combo_box = ftk_combo_box_create(dialog, width/6, height/4, 2*width/3, 30); ftk_combo_box_set_text(combo_box, "1 second"); ftk_combo_box_append(combo_box, NULL, "1 second"); ftk_combo_box_append(combo_box, NULL, "2 seconds"); ftk_combo_box_append(combo_box, NULL, "3 seconds"); ftk_entry_set_readonly(ftk_combo_box_get_entry(combo_box), modal); button = ftk_button_create(dialog, width/6, height/2, width/3, 50); ftk_widget_set_text(button, "yes"); ftk_button_set_clicked_listener(button, button_quit_clicked, modal ? &id : NULL); button = ftk_button_create(dialog, width/2, height/2, width/3, 50); ftk_widget_set_text(button, "no"); ftk_button_set_clicked_listener(button, button_quit_clicked, modal ? &id : NULL); ftk_window_set_focus(dialog, button); ftk_widget_set_text(dialog, modal ? "model dialog" : "normal dialog"); if(modal) { assert(ftk_dialog_run(dialog) == id); ftk_widget_unref(dialog); } else { ftk_widget_show_all(dialog, 1); } ftk_logi("%s:%d end\n", __func__, __LINE__); return RET_OK; }
int main(int argc, char* argv[]) { FtkWidget* win = NULL; ftk_init(argc, argv); win = ftk_xul_load(t1, strlen(t1)); ftk_widget_unref(win); return 0; }
static Ret designer_on_open_ok(void* ctx, FtkWidget* win) { FtkWidget* old_win = (FtkWidget*)ctx; if(win != NULL) { designer_init(win); } ftk_widget_unref(old_win); return RET_OK; }
static Ret ftk_menu_panel_on_event(FtkWidget* thiz, FtkEvent* event) { Ret ret = RET_OK; DECL_PRIV1(thiz, priv); if(event->type == FTK_EVT_KEY_UP && event->u.key.code == FTK_KEY_MENU) { ftk_widget_unref(thiz); return ret; } if(event->type == FTK_EVT_SHOW) { ftk_wnd_manager_grab(ftk_default_wnd_manager(), thiz); } if(event->type == FTK_EVT_HIDE) { ftk_wnd_manager_ungrab(ftk_default_wnd_manager(), thiz); } if((event->type == FTK_EVT_MOUSE_UP && ret != RET_IGNORED) || (event->type == FTK_EVT_KEY_UP && FTK_IS_ACTIVE_KEY(event->u.key.code))) { ftk_widget_show(thiz, 0); } ret = priv->parent_on_event(thiz, event); if((event->type == FTK_EVT_MOUSE_UP && ret != RET_IGNORED) || (event->type == FTK_EVT_KEY_UP && FTK_IS_ACTIVE_KEY(event->u.key.code))) { ftk_widget_unref(thiz); } return ret; }
static Ret button_quit_clicked(void* ctx, void* obj) { if(ctx != NULL) { /*modal*/ *(int*)ctx = ftk_widget_id(obj); } else { /*modal-less*/ ftk_widget_unref(ftk_widget_toplevel(obj)); } return RET_QUIT; }
static Ret ftk_file_browser_on_rename(void* ctx, void* obj) { int i = 0; int ret = 0; const char* name = NULL; FtkWidget* win = (FtkWidget*)ctx; FtkWidget* dialog = NULL; FtkListItemInfo* info = NULL; PrivInfo* priv = (PrivInfo*)ftk_widget_user_data(win); i = ftk_list_view_get_selected(priv->list_view); ftk_list_model_get_data(priv->model, i, (void**)&info); return_val_if_fail(info != NULL, RET_FAIL); if(strcmp(info->text, _("..")) == 0) { return RET_FAIL; } dialog = ftk_file_browser_input_dialog(win, _("Please input new name:")); ret = ftk_dialog_run(dialog); if(ret == ID_OK) { name = ftk_entry_get_text(ftk_widget_lookup(dialog, ID_ENTRY)); if(name != NULL) { char new_name[FTK_MAX_PATH+1] = {0}; char old_name[FTK_MAX_PATH+1] = {0}; ftk_strs_cat(old_name, FTK_MAX_PATH, priv->path, "/", info->text, NULL); ftk_strs_cat(new_name, FTK_MAX_PATH, priv->path, "/", name, NULL); if(ftk_fs_move(old_name, new_name) == RET_OK) { ftk_file_browser_load(win); } else { ftk_tips(_("Rename failed.")); } ftk_logd("%s: %s --> %s\n", __func__, old_name, new_name); } } ftk_widget_unref(dialog); ftk_logd("%s: ret=%d\n", __func__, ret); return RET_OK; }
static Ret designer_on_new(void* ctx, void* item) { FtkWidget* win = NULL; FtkWidget* old_win = (FtkWidget*)ctx; win = ftk_app_window_create(); ftk_widget_set_text(win, _("FTK UI Designer")); ftk_window_set_animation_hint(win, "app_main_window"); designer_init(win); if(old_win != NULL) { ftk_widget_unref(old_win); } return RET_OK; }
static Ret button_ok_clicked(void* ctx, void* obj) { char filename[FTK_MAX_PATH+1] = {0}; FtkWidget* win = (FtkWidget*)ctx; Info* info = (Info*)ftk_widget_user_data(win); ftk_strncpy(filename, ftk_widget_get_text(info->widget_file_name), FTK_MAX_PATH); return_val_if_fail(strlen(filename) > 0, RET_FAIL); if(info->on_select != NULL) { info->on_select(info->on_select_ctx, filename); ftk_strncpy(s_last_file, filename, FTK_MAX_PATH); } ftk_widget_unref(win); return RET_OK; }
static void ftk_wnd_manager_default_destroy(FtkWndManager* thiz) { int i = 0; DECL_PRIV(thiz, priv); if(thiz != NULL) { int nr = priv->top; priv->dieing = 1; for(i = 0; i < nr; i++) { FtkWidget* win = priv->windows[0]; ftk_wnd_manager_remove(thiz, win); ftk_widget_unref(win); } ftk_source_unref(priv->long_press_timer); FTK_ZFREE(thiz, sizeof(FtkWndManager) + sizeof(PrivInfo)); } return; }
static Ret timeout(void* ctx) { TimerInfo* info = ctx; char buffer[32] = {0}; if(info->times > 0) { ftk_snprintf(buffer, sizeof(buffer), "Quit after %d seconds", info->times); ftk_widget_set_text(info->label, buffer); info->times--; return RET_OK; } else { ftk_widget_unref(ftk_widget_toplevel(info->label)); ftk_logd("%s: timeout and quit.\n", __func__); FTK_FREE(info); FTK_QUIT(); return RET_REMOVE; } }
static Ret ftk_file_browser_on_ok(void* ctx, void* obj) { int i = 0; FtkWidget* win = (FtkWidget*)ctx; FtkListItemInfo* info = NULL; char path[FTK_MAX_PATH+1] = {0}; PrivInfo* priv = (PrivInfo*)ftk_widget_user_data(win); return_val_if_fail(priv != NULL && priv->on_choosed != NULL, RET_FAIL); if(priv->type == FTK_FILE_BROWER_SINGLE_CHOOSER) { i = ftk_list_view_get_selected(priv->list_view); ftk_list_model_get_data(priv->model, i, (void**)&info); if(info != NULL) { ftk_strs_cat(path, FTK_MAX_PATH, priv->path, "/", info->text, NULL); priv->on_choosed(priv->on_choosed_ctx, 0, path); } } else { int index = 0; int n = ftk_list_model_get_total(priv->model); for(i = 0; i < n; i++) { ftk_list_model_get_data(priv->model, i, (void**)&info); if(info != NULL && info->state) { ftk_strs_cat(path, FTK_MAX_PATH, priv->path, "/", info->text, NULL); priv->on_choosed(priv->on_choosed_ctx, index++, path); } } priv->on_choosed(priv->on_choosed_ctx, -1, NULL); } ftk_widget_unref((FtkWidget*)ctx); return RET_OK; }
static Ret ftk_file_browser_on_quit(void* ctx, void* obj) { ftk_widget_unref((FtkWidget*)ctx); return RET_OK; }
int ftk_message_box(FtkBitmap* icon, const char* title, const char* text, const char* buttons[FTK_MSGBOX_MAX_BUTTONS]) { int i = 0; int w = 0; int h = 0; int id = 0; int ret = 0; int width = 0; int height = 0; int xoffset = 0; int yoffset = 0; int h_margin = 0; int has_title = icon != NULL || title != NULL; int buttons_nr = ftk_count_strings(buttons); FtkWidget* label = NULL; FtkWidget* button = NULL; FtkWidget* dialog = NULL; return_val_if_fail(text != NULL, -1); ftk_message_box_size(has_title, buttons_nr, text, &width, &height); dialog = ftk_dialog_create(0, 0, width, height); ftk_window_set_animation_hint(dialog, "msgbox"); if(has_title) { ftk_dialog_set_icon(dialog, icon); ftk_widget_set_text(dialog, title); } else { ftk_dialog_hide_title(dialog); ftk_widget_set_attr(dialog, FTK_ATTR_POPUP); } width = ftk_widget_width(dialog); height = ftk_widget_height(dialog); /*create label.*/ xoffset = FTK_H_MARGIN; yoffset = FTK_V_MARGIN; w = width - 2 * (FTK_DIALOG_BORDER + FTK_H_MARGIN); h = height - FTK_DIALOG_BORDER - 4 * FTK_V_MARGIN; h -= has_title ? ftk_dialog_get_title_height() : 0; h -= buttons_nr > 0 ? FTK_BUTTON_DEFAULT_HEIGHT : 0; label = ftk_label_create(dialog, xoffset, yoffset, w, h); ftk_widget_set_text(label, text); /*create buttons*/ if(buttons_nr > 0) { xoffset = 0; w = FTK_BUTTON_DEFAULT_WIDTH; h = FTK_BUTTON_DEFAULT_HEIGHT; w = ((buttons_nr + 1) * w) < width ? w : (width / (buttons_nr + 1)); yoffset = height - h - FTK_V_MARGIN - (has_title ? ftk_dialog_get_title_height() : 0); h_margin = (width - buttons_nr * w) / (buttons_nr + 1); for(i = 0; i < buttons_nr; i++) { xoffset += h_margin; button = ftk_button_create(dialog, xoffset, yoffset, w, h); ftk_widget_set_id(button, i + 1); ftk_widget_set_text(button, buttons[i]); ftk_button_set_clicked_listener(button, message_box_on_button_clicked, &id); xoffset += w; } } else { /*if no buttons, quit when timeout.*/ ftk_dialog_quit_after(dialog, 3000); } ftk_widget_show_all(dialog, 1); ret = ftk_dialog_run(dialog); ftk_widget_unref(dialog); if(icon != NULL) { ftk_bitmap_unref(icon); } return ret; }
static Ret ftk_wnd_manager_default_dispatch_event(FtkWndManager* thiz, FtkEvent* event) { DECL_PRIV(thiz, priv); FtkWidget* target = NULL; return_val_if_fail(thiz != NULL && event != NULL, RET_FAIL); if(priv->dieing) { return RET_OK; } if(event->type == FTK_EVT_KEY_DOWN || event->type == FTK_EVT_KEY_UP) { ftk_wnd_manager_default_key_translate(thiz, event); } if(ftk_wnd_manager_dispatch_globals(thiz, event) != RET_OK) { return RET_REMOVE; } switch(event->type) { case FTK_EVT_WND_DESTROY: { if(priv->top_window == event->widget) { priv->top_window = NULL; } priv->disable_anim++; ftk_wnd_manager_default_remove(thiz, (FtkWidget*)event->widget); priv->disable_anim--; return RET_OK; } case FTK_EVT_HIDE: { priv->disable_anim++; ftk_wnd_manager_default_emit_top_wnd_changed(thiz); priv->disable_anim--; ftk_wnd_manager_default_do_animation(thiz, event); ftk_wnd_manager_update(thiz); return RET_OK; } case FTK_EVT_SHOW: { priv->disable_anim++; ftk_wnd_manager_default_emit_top_wnd_changed(thiz); priv->disable_anim--; ftk_wnd_manager_default_do_animation(thiz, event); if(ftk_widget_type((FtkWidget*)event->widget) == FTK_WINDOW && ftk_wnd_manager_default_map_panels(thiz, !ftk_wnd_manager_default_has_fullscreen_win(thiz)) == RET_OK) { ftk_wnd_manager_update(thiz); } return RET_OK; } case FTK_EVT_RELAYOUT_WND: { ftk_wnd_manager_default_relayout(thiz); break; } default:break; } if(event->type == FTK_EVT_MOUSE_DOWN || event->type == FTK_EVT_KEY_DOWN) { priv->pressed_event = *event; ftk_source_ref(priv->long_press_timer); ftk_source_timer_reset(priv->long_press_timer); ftk_main_loop_add_source(ftk_default_main_loop(), priv->long_press_timer); } if(event->type == FTK_EVT_MOUSE_UP|| event->type == FTK_EVT_KEY_UP) { ftk_main_loop_remove_source(ftk_default_main_loop(), priv->long_press_timer); } if((event->type == FTK_EVT_MOUSE_DOWN || event->type == FTK_EVT_MOUSE_UP || event->type == FTK_EVT_MOUSE_MOVE) && priv->grab_widget == NULL) { int x = event->u.mouse.x; int y = event->u.mouse.y; target = ftk_wnd_manager_find_target(thiz, x, y); if(event->type == FTK_EVT_MOUSE_DOWN && !ftk_widget_has_attr(target, FTK_ATTR_NO_FOCUS)) { priv->focus_widget = target; } } else if(event->widget != NULL) { target = (FtkWidget*)event->widget; } else if(priv->grab_widget != NULL) { target = priv->grab_widget; } else if(priv->focus_widget != NULL) { target = priv->focus_widget; } else if(priv->top > 0) { target = priv->windows[priv->top - 1]; } if(target != NULL && !ftk_widget_is_insensitive(target)) { ftk_widget_ref(target); ftk_widget_event(target, event); ftk_widget_unref(target); } return RET_OK; }
static Ret button_quit_clicked(void* ctx, void* obj) { ftk_widget_unref(ctx); return RET_OK; }