static Ret ftk_check_button_on_event(FtkWidget* thiz, FtkEvent* event) { Ret ret = RET_OK; DECL_PRIV0(thiz, priv); switch(event->type) { case FTK_EVT_MOUSE_DOWN: { ftk_widget_set_active(thiz, 1); ftk_window_grab(ftk_widget_toplevel(thiz), thiz); break; } case FTK_EVT_MOUSE_UP: { int x = event->u.mouse.x; int y = event->u.mouse.y; if(ftk_widget_is_active(thiz)) { ftk_widget_set_active(thiz, 0); ftk_window_ungrab(ftk_widget_toplevel(thiz), thiz); if(FTK_POINT_IN_WIDGET(x, y, thiz)) { ret = FTK_CALL_LISTENER(priv->listener, priv->listener_ctx, thiz); ftk_check_button_check(thiz); } } break; } case FTK_EVT_KEY_DOWN: { if(FTK_IS_ACTIVE_KEY(event->u.key.code)) { ftk_widget_set_active(thiz, 1); } break; } case FTK_EVT_KEY_UP: { if(FTK_IS_ACTIVE_KEY(event->u.key.code) && ftk_widget_is_active(thiz)) { ftk_widget_set_active(thiz, 0); ftk_check_button_check(thiz); ret = FTK_CALL_LISTENER(priv->listener, priv->listener_ctx, thiz); } break; } default:break; } return ret; }
void ftk_widget_set_parent(FtkWidget* thiz, FtkWidget* parent) { return_if_fail(thiz != NULL && thiz->priv != NULL); thiz->parent = parent; ftk_widget_set_canvas(thiz, ftk_widget_canvas(ftk_widget_toplevel(parent))); return; }
static Ret ftk_menu_item_on_event(FtkWidget* thiz, FtkEvent* event) { Ret ret = RET_OK; DECL_PRIV0(thiz, priv); switch(event->type) { case FTK_EVT_MOUSE_DOWN: { ftk_widget_set_active(thiz, 1); ftk_window_grab(ftk_widget_toplevel(thiz), thiz); break; } case FTK_EVT_MOUSE_UP: { ftk_widget_set_active(thiz, 0); ftk_window_ungrab(ftk_widget_toplevel(thiz), thiz); ret = FTK_CALL_LISTENER(priv->listener, priv->listener_ctx, thiz); break; } case FTK_EVT_KEY_DOWN: { if(FTK_IS_ACTIVE_KEY(event->u.key.code)) { ftk_widget_set_active(thiz, 1); } break; } case FTK_EVT_KEY_UP: { if(FTK_IS_ACTIVE_KEY(event->u.key.code) && ftk_widget_is_active(thiz)) { ftk_widget_set_active(thiz, 0); ret = FTK_CALL_LISTENER(priv->listener, priv->listener_ctx, thiz); } break; } default:break; } 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; }
Ret ftk_widget_update(FtkWidget* thiz) { FtkEvent event = {0}; FtkWidget* window = NULL; FtkWidgetInfo* priv = NULL; return_val_if_fail(thiz != NULL, RET_FAIL); window = ftk_widget_toplevel(thiz); return_val_if_fail(window != NULL, RET_FAIL); priv = thiz->priv; event.type = FTK_EVT_UPDATE; event.u.rect.x = priv->left; event.u.rect.y = priv->top; event.u.rect.width = priv->width; event.u.rect.height = priv->height; return ftk_widget_event(window, &event); }
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_scroll_bar_on_event(FtkWidget* thiz, FtkEvent* event) { int x = 0; int y = 0; Ret ret = RET_OK; DECL_PRIV0(thiz, priv); if(priv->page_delta == priv->max_value) { /*one page, no scroll*/ return RET_OK; } switch(event->type) { case FTK_EVT_KEY_DOWN: { if(event->u.key.code == FTK_KEY_DOWN) { ftk_scroll_bar_inc(thiz); ret = RET_REMOVE; } else if(event->u.key.code == FTK_KEY_UP) { ftk_scroll_bar_dec(thiz); ret = RET_REMOVE; } else if(event->u.key.code == FTK_KEY_PAGEDOWN) { ftk_scroll_bar_pagedown(thiz); } else if(event->u.key.code == FTK_KEY_PAGEUP) { ftk_scroll_bar_pageup(thiz); } break; } case FTK_EVT_MOUSE_DOWN: { int pos = 0; int value = 0; x = event->u.mouse.x; y = event->u.mouse.y; priv->mouse_pressed = 1; ftk_window_grab(ftk_widget_toplevel(thiz), thiz); pos = priv->vertical ? y : x; priv->drag_enabled = pos >= priv->tracker_pos && pos < (priv->tracker_pos + priv->tracker_size); if(!priv->drag_enabled) { value = pos < priv->tracker_pos ? priv->value - priv->page_delta : priv->value + priv->page_delta; ftk_scroll_bar_set_value(thiz, value); } priv->last_mouse_pos = pos; break; } case FTK_EVT_MOUSE_MOVE: { int pos = 0; int delta =0; int value = 0; int width = 0; int height = 0; if(!priv->drag_enabled) break; x = event->u.mouse.x; y = event->u.mouse.y; pos = priv->vertical ? y : x; delta = pos - priv->last_mouse_pos; width = ftk_widget_width(thiz); height = ftk_widget_height(thiz); value = priv->vertical ? priv->max_value * delta/height : priv->max_value * delta/width; if(value != 0) { value += priv->value; priv->last_mouse_pos = pos; ftk_scroll_bar_set_value(thiz, value); } break; } case FTK_EVT_MOUSE_UP: { priv->drag_enabled = 0; priv->mouse_pressed = 0; ftk_window_ungrab(ftk_widget_toplevel(thiz), thiz); break; } default:break; } return ret; }
static Ret ftk_key_board_on_mouse_event(FtkWidget* thiz, FtkEventType event, size_t x, size_t y) { DECL_PRIV0(thiz, priv); FtkKeyBoardCell* cell = NULL; FtkKeyBoardDesc* desc = priv->desc; FtkKeyBoardView* view = desc->views + desc->current_view; x -= ftk_widget_left_abs(thiz); y -= ftk_widget_top_abs(thiz); view->last_click_pos.x = x; view->last_click_pos.y = y; cell = ftk_key_board_find_cell(thiz, x, y); if(cell == NULL) { return RET_OK; } if(cell->action == ftk_key_board_candidate_text) { size_t i = 0; size_t x_c = cell->x; for(i = desc->candidate_start; i < desc->candidate_nr; i++) { FtkKeyBoardCandidate* candidate = desc->candidates+i; if(x >= x_c && x < (x_c + candidate->extent)) { desc->candidate_focus = i; ftk_logd("%s:%d on %s\n", __func__, __LINE__, candidate->text); break; } x_c += candidate->extent + desc->candidate_space; } ftk_widget_invalidate(thiz); } switch(event) { case FTK_EVT_MOUSE_DOWN: { view->focus = cell; ftk_window_grab(ftk_widget_toplevel(thiz), thiz); break; } case FTK_EVT_MOUSE_UP: { if(cell->action != NULL) { cell->action(thiz, cell); } view->focus = NULL; desc->candidate_focus = -1; ftk_window_ungrab(ftk_widget_toplevel(thiz), thiz); } default: { if(view->focus == cell) { return RET_OK; } if(view->focus != NULL) { view->focus = cell; } } } if(cell->action != NULL) { ftk_widget_invalidate(thiz); } return RET_OK; }
static Ret ftk_progress_bar_on_event_interactive(FtkWidget* thiz, FtkEvent* event) { DECL_PRIV0(thiz, priv); int percent = priv->percent; return_val_if_fail(thiz != NULL && event != NULL, RET_FAIL); if(event->type == FTK_EVT_KEY_UP) { switch(event->u.key.code) { case FTK_KEY_HOME: { percent = 0; break; } case FTK_KEY_END: { percent = 100; break; } case FTK_KEY_PAGEUP: { percent += 10; break; } case FTK_KEY_PAGEDOWN: { percent -= 10; break; } case FTK_KEY_LEFT: case FTK_KEY_DOWN: { percent--; break; } case FTK_KEY_UP: case FTK_KEY_RIGHT: { percent++; break; } default:break; } } else if(event->type == FTK_EVT_MOUSE_DOWN) { priv->mouse_pressed = 1; ftk_window_grab(ftk_widget_toplevel(thiz), thiz); } else if(event->type == FTK_EVT_MOUSE_MOVE && priv->mouse_pressed) { int width = ftk_widget_width(thiz); int offset = event->u.mouse.x - ftk_widget_left_abs(thiz); percent = 100 * offset / width; } else if(event->type == FTK_EVT_MOUSE_UP) { int width = ftk_widget_width(thiz); int offset = event->u.mouse.x - ftk_widget_left_abs(thiz); priv->mouse_pressed = 0; percent = 100 * offset / width; ftk_window_ungrab(ftk_widget_toplevel(thiz), thiz); } else { return RET_OK; } ftk_progress_bar_set_percent(thiz, percent); return RET_OK; }