static void rtgui_slider_onmouse(struct rtgui_slider* slider, struct rtgui_event_mouse* event) { RT_ASSERT(slider != RT_NULL); RT_ASSERT(event != RT_NULL); if (event->button & RTGUI_MOUSE_BUTTON_DOWN && event->button & RTGUI_MOUSE_BUTTON_LEFT) { int sel; int range = slider->max - slider->min; int x0, xsize; int x; x0 = 1 + slider->thumb_width/2; if (slider->orient == RTGUI_VERTICAL) { x = event->y - RTGUI_WIDGET(slider)->extent.y1; x -= x0; xsize = rtgui_rect_height(RTGUI_WIDGET(slider)->extent) - 2 * x0; } else { x = event->x - RTGUI_WIDGET(slider)->extent.x1; x -= x0; xsize = rtgui_rect_width(RTGUI_WIDGET(slider)->extent) - 2 * x0; } if (x <= 0) { sel = slider->min; } else if (x >= xsize) { sel = slider->max; } else { sel = ((range * x) + xsize/2) / xsize; sel += slider->min; } rtgui_widget_focus(RTGUI_WIDGET(slider)); rtgui_slider_set_value(slider, sel); if (slider->on_changed != RT_NULL) /* invoke callback function */ slider->on_changed(RTGUI_WIDGET(slider), RT_NULL); } }
static void function_action_setup(struct rtgui_widget* widget, void* paramter) { rtgui_view_t* view; rtgui_rect_t rect; rtgui_label_t* label; struct rtgui_button* cancel_button, *apply_button, *save_button; rtgui_workbench_t* workbench = father_workbench; /* create a demo view */ view = rtgui_view_create("Slider View"); /* 添加到父workbench中 */ rtgui_workbench_add_view(workbench, view); //设置服务函数 rtgui_widget_set_event_handler(RTGUI_WIDGET(view), view_event_handler); /* get demo view rect */ rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect); rtgui_widget_rect_to_device(RTGUI_WIDGET(view), &rect); label = rtgui_label_create("默认音量:"); rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(label)); rect.x1 += 5; rect.x2 -= 5; rect.y1 += 5; rect.y2 = rect.y1 + 18; rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect); rect.y1 += 20; rect.y2 = rect.y1 + 18; slider_volume = rtgui_slider_create(0, 100, RTGUI_HORIZONTAL); rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(slider_volume)); rtgui_widget_set_rect(RTGUI_WIDGET(slider_volume), &rect); rtgui_slider_set_value(slider_volume, radio_setup.default_volume); /* get demo view rect */ rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect); rtgui_widget_rect_to_device(RTGUI_WIDGET(view), &rect); label = rtgui_label_create("LCD背光亮度:"); rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(label)); rect.x1 += 5; rect.x2 -= 5; rect.y1 += 50; rect.y2 = rect.y1 + 18; rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect); rect.y1 += 20; rect.y2 = rect.y1 + 18; slider_brightness = rtgui_slider_create(0, 100, RTGUI_HORIZONTAL); rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(slider_brightness)); rtgui_widget_set_rect(RTGUI_WIDGET(slider_brightness), &rect); rtgui_slider_set_value(slider_brightness, radio_setup.lcd_brightness); //得到 "取消" 按键的位置 rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect); rtgui_widget_rect_to_device(RTGUI_WIDGET(view), &rect); rect.x1 += 20; rect.x2 = rect.x1 + 60; rect.y2 -= 20; rect.y1 = rect.y2 - 20; //创建 "取消" 按钮(兼返回) cancel_button = rtgui_button_create("取消"); /* 设置onbutton动作到demo_view_next函数 */ rtgui_button_set_onbutton(cancel_button, cancel_handler); /* 设置按钮的位置信息 */ rtgui_widget_set_rect(RTGUI_WIDGET(cancel_button), &rect); /* 添加按钮到视图中 */ rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(cancel_button)); rect.x1 = rect.x2 + 10; rect.x2 = rect.x1 + 60; apply_button = rtgui_button_create("应用"); /* 设置onbutton动作到demo_view_next函数 */ rtgui_button_set_onbutton(apply_button, apply_handler); /* 设置按钮的位置信息 */ rtgui_widget_set_rect(RTGUI_WIDGET(apply_button), &rect); /* 添加按钮到视图中 */ rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(apply_button)); //得到 "保存" 按键的位置 rtgui_widget_get_rect(RTGUI_WIDGET(view), &rect); rtgui_widget_rect_to_device(RTGUI_WIDGET(view), &rect); rect.x2 -= 20; rect.x1 = rect.x2 - 60; rect.y2 -= 20; rect.y1 = rect.y2 - 20; /* 创建 "保存" 按钮 */ save_button = rtgui_button_create("保存"); /* 设置onbutton动作到demo_view_prev函数 */ rtgui_button_set_onbutton(save_button, save_handler); /* 设置按钮的位置信息 */ rtgui_widget_set_rect(RTGUI_WIDGET(save_button), &rect); /* 添加按钮到视图中 */ rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(save_button)); rtgui_view_show(view, RT_FALSE); }