void mainmenu_register_internal_app(char *name, char *text, rtgui_image_t *image, void (*app_starter)(void *), void *p) { int i; for (i = 0; i < ITEM_MAX; i++) { if (!items[i].text) break; if (!items[i].name) continue; if (strcmp(items[i].name, name) == 0) return; } if (i == ITEM_MAX) { rt_kprintf("app list is full\n"); return; } items[i].name = rt_strdup(name); if (!items[i].name) return; items[i].text = rt_strdup(name); if (!items[i].text) goto _err; items[i].icon = image; items[i].app_starter = app_starter; items[i].parameter = p; items[i].is_external = 1; return; _err: items[i].app_starter = RT_NULL; if (items[i].name) { rt_free(items[i].name); items[i].name = RT_NULL; } if (items[i].text) { rt_free(items[i].text); items[i].text = RT_NULL; } if (items[i].icon) { rtgui_image_destroy(items[i].icon); items[i].icon = RT_NULL; } }
void play_list_append(char* fn) { int media; char *ptr; play_list_size ++; if (play_list == RT_NULL) play_list = (struct play_item*) rt_malloc (play_list_size * sizeof(struct play_item)); else play_list = (struct play_item*) rt_realloc(play_list, play_list_size * sizeof(struct play_item)); media = media_type(fn); if (media == MEDIA_MP3) { struct tag_info info; memset(&info, 0, sizeof(info)); mp3_get_info(fn, &info); ptr = strrchr(fn, '/'); //WP MCU工作室 rt_snprintf(play_list[play_list_size - 1].title, sizeof(play_list[play_list_size - 1].title), ptr+1); /* if (info.title[0] == '\0') rt_snprintf(play_list[play_list_size - 1].title, sizeof(play_list[play_list_size - 1].title), "<未知名音乐>"); else strcpy(play_list[play_list_size - 1].title, info.title); */ play_list[play_list_size - 1].fn = rt_strdup(fn); play_list[play_list_size - 1].duration = info.duration; } else if (media == MEDIA_RADIO) { rt_snprintf(play_list[play_list_size - 1].title, sizeof(play_list[play_list_size - 1].title), "<未知名电台>"); play_list[play_list_size - 1].fn = rt_strdup(fn); play_list[play_list_size - 1].duration = 0; } else if (media == MEDIA_WAV) { struct tag_info info; memset(&info, 0, sizeof(info)); get_wav_info(fn, &info); ptr = strrchr(fn, '/'); //UP MCU工作室 rt_snprintf(play_list[play_list_size - 1].title, sizeof(play_list[play_list_size - 1].title), ptr+1); play_list[play_list_size - 1].fn = rt_strdup(fn); play_list[play_list_size - 1].duration = info.duration; } }
rtgui_workbench_t *rtgui_workbench_create(const char* panel_name, const unsigned char* title) { struct rtgui_workbench* workbench; /* the server thread id */ rt_thread_t server = rtgui_thread_get_server(); if (server == RT_NULL) { rt_kprintf("can't find rtgui server\n"); return RT_NULL; } /* create workbench */ workbench = (rtgui_workbench_t*) rtgui_widget_create (RTGUI_WORKBENCH_TYPE); if (workbench != RT_NULL) { /* the buffer uses to receive event */ union { struct rtgui_event_panel_attach ecreate; struct rtgui_event_panel_info epanel; char buffer[256]; /* use to recv other information */ } event; /* set workbench title */ workbench->title = (unsigned char*)rt_strdup((char*)title); /* create application in server */ RTGUI_EVENT_PANEL_ATTACH_INIT(&(event.ecreate)); /* set the panel name and workbench */ rt_strncpy(event.ecreate.panel_name, panel_name, RTGUI_NAME_MAX); event.ecreate.workbench = workbench; /* send PANEL ATTACH to server */ if (rtgui_thread_send_sync(server, &(event.ecreate.parent), sizeof(struct rtgui_event_panel_attach)) != RTGUI_STATUS_OK) { return RT_NULL; } /* get PANEL INFO */ rtgui_thread_recv_filter(RTGUI_EVENT_PANEL_INFO, &(event.epanel.parent), sizeof(event)); /* set panel */ workbench->panel = (struct rtgui_panel*)event.epanel.panel; /* connected */ RTGUI_TOPLEVEL(workbench)->server = server; /* set extent of workbench */ rtgui_widget_set_rect(RTGUI_WIDGET(workbench), &(event.epanel.extent)); /* set workbench in thread */ rtgui_thread_set_widget(RTGUI_WIDGET(workbench)); } return workbench; }
void demo_bitmap_open(struct rtgui_object* object, struct rtgui_event* event) { char *str; rtgui_button_t *button = RTGUI_BUTTON(object); /* 从textbox控件中取得文件名 */ str = (char*)rtgui_textbox_get_value(bmpdt.box); if(str == RT_NULL) return; if(*str == '/' && (rt_strstr(str, ".bmp")!=RT_NULL || rt_strstr(str, ".BMP")!=RT_NULL)) { /* 如果是bmp文件, 且文件名有效, 则读入图像数据 */ if(bmpdt.filename != RT_NULL) rt_free(bmpdt.filename); bmpdt.filename = rt_strdup(str); if(bmpdt.image != RT_NULL) rtgui_image_destroy(bmpdt.image); bmpdt.image = rtgui_image_create_from_file("bmp", bmpdt.filename, RT_TRUE); if(bmpdt.image != RT_NULL) { bmpdt.showimg = bmpdt.image; bmpdt.scale = 1.0; bmpdt.angle = 0.0; rtgui_widget_update(RTGUI_WIDGET(bmpdt.showbox)); } } else rt_kprintf("Bad filename!"); }
rtgui_win_t* rtgui_win_create(rtgui_toplevel_t* parent_toplevel, const char* title, rtgui_rect_t *rect, rt_uint8_t style) { struct rtgui_win* win; /* allocate win memory */ win = (struct rtgui_win*) rtgui_widget_create (RTGUI_WIN_TYPE); if (win != RT_NULL) { /* set parent toplevel */ win->parent_toplevel = parent_toplevel; /* set title, rect and style */ if (title != RT_NULL) win->title = rt_strdup(title); else win->title = RT_NULL; rtgui_widget_set_rect(RTGUI_WIDGET(win), rect); win->style = style; if (_rtgui_win_create_in_server(win) == RT_FALSE) { rtgui_widget_destroy(RTGUI_WIDGET(win)); return RT_NULL; } } return win; }
rtgui_win_t* rtgui_win_create(struct rtgui_win* parent_window, const char* title, rtgui_rect_t *rect, rt_uint16_t style) { struct rtgui_win* win; /* allocate win memory */ win = RTGUI_WIN(rtgui_widget_create(RTGUI_WIN_TYPE)); if (win == RT_NULL) return RT_NULL; /* set parent toplevel */ win->parent_window = parent_window; /* set title, rect and style */ if (title != RT_NULL) win->title = rt_strdup(title); else win->title = RT_NULL; rtgui_widget_set_rect(RTGUI_WIDGET(win), rect); win->style = style; if (_rtgui_win_create_in_server(win) == RT_FALSE) { goto __on_err; } return win; __on_err: rtgui_widget_destroy(RTGUI_WIDGET(win)); return RT_NULL; }
void player_update_list() { int index; struct play_item* item; if (music_listitems != RT_NULL) { for (index = 0; index < music_listitems_size; index ++) { rt_free(music_listitems[index].name); } rt_free(music_listitems); music_listitems = RT_NULL; music_listitems_size = 0; } music_listitems_size = play_list_items(); if (music_listitems_size > 0) { music_listitems = (struct rtgui_listbox_item*) rt_malloc ( music_listitems_size * sizeof(struct rtgui_listbox_item)); for (index = 0; index < music_listitems_size; index ++) { music_listitems[index].image = RT_NULL; item = play_list_item(index); music_listitems[index].name = rt_strdup(item->title); } } /* re-set listbox items */ rtgui_listbox_set_items(music_listbox, music_listitems, music_listitems_size); }
struct rtgui_app *rtgui_app_create(const char *title) { rt_thread_t tid = rt_thread_self(); struct rtgui_app *app; struct rtgui_app *srv_app; struct rtgui_event_application event; char mq_name[RT_NAME_MAX]; RT_ASSERT(tid != RT_NULL); RT_ASSERT(title != RT_NULL); /* create application */ app = RTGUI_APP(rtgui_object_create(RTGUI_APP_TYPE)); if (app == RT_NULL) return RT_NULL; /* one thread only can create one rtgui application */ RT_ASSERT(tid->user_data == 0); app->tid = tid; rt_snprintf(mq_name, RT_NAME_MAX, "g%s", title); app->mq = rt_mq_create(mq_name, sizeof(union rtgui_event_generic), 32, RT_IPC_FLAG_FIFO); if (app->mq == RT_NULL) { rt_kprintf("create msgq failed.\n"); goto __mq_err; } /* set application title */ app->name = (unsigned char *)rt_strdup((char *)title); if (app->name == RT_NULL) goto __err; /* the first app should be the server */ srv_app = rtgui_get_server(); if (srv_app == RT_NULL) { /* set user thread */ tid->user_data = (rt_uint32_t)app; return app; } RTGUI_EVENT_APP_CREATE_INIT(&event); event.app = app; /* notify rtgui server to one application has been created */ if (rtgui_send_sync(srv_app, RTGUI_EVENT(&event), sizeof(event)) == RT_EOK) { /* set user thread */ tid->user_data = (rt_uint32_t)app; return app; } __err: __mq_err: rtgui_object_destroy(RTGUI_OBJECT(app)); return RT_NULL; }
void rtgui_wintitle_set_title(rtgui_wintitle_t *wintitle, const char *title) { RT_ASSERT(wintitle != RT_NULL); if (wintitle->title != RT_NULL) { rt_free(wintitle->title); } if (title != RT_NULL) wintitle->title = (char *)rt_strdup((const char *)title); else wintitle->title = RT_NULL; }
/* append radio */ void play_list_append_radio(const char* url, const char* station) { play_list_size ++; if (play_list == RT_NULL) play_list = (struct play_item*) rt_malloc (play_list_size * sizeof(struct play_item)); else play_list = (struct play_item*) rt_realloc(play_list, play_list_size * sizeof(struct play_item)); strncpy(play_list[play_list_size - 1].title, station, sizeof(play_list[play_list_size - 1].title)); play_list[play_list_size - 1].fn = rt_strdup(url); play_list[play_list_size - 1].duration = 0; }
int system(const char *command) { int ret = -RT_ENOMEM; char *cmd = rt_strdup(command); if (cmd) { ret = msh_exec(cmd, rt_strlen(cmd)); rt_free(cmd); } return ret; }
rtgui_fileview_t* rtgui_fileview_create(pvoid parent, const char* directory, const char* pattern, int left, int top, int w, int h) { rtgui_container_t *container; rtgui_fileview_t* fview = RT_NULL; RT_ASSERT(parent != RT_NULL); container = RTGUI_CONTAINER(parent); /* create a new view */ fview = rtgui_widget_create(RTGUI_FILEVIEW_TYPE); if(fview != RT_NULL) { rtgui_rect_t rect; rtgui_widget_get_rect(container, &rect); rtgui_widget_rect_to_device(container,&rect); rect.x1 += left; rect.y1 += top; rect.x2 = rect.x1+w; rect.y2 = rect.y1+h; fview->items = RT_NULL; fview->pattern = rt_strdup(pattern); fview->item_per_page = RC_H(rect) / (1 + RTGUI_SEL_H); rtgui_widget_set_rect(fview,&rect); rtgui_container_add_child(container, fview); { /* create scrollbar */ rt_uint32_t sLeft,sTop,sw=RTGUI_DEFAULT_SB_WIDTH,sLen; sLeft = RC_W(rect)-RTGUI_WIDGET_BORDER_SIZE(fview)-sw; sTop = RTGUI_WIDGET_BORDER_SIZE(fview); sLen = rect.y2-rect.y1-RTGUI_WIDGET_BORDER_SIZE(fview)*2; fview->sbar = rtgui_scrollbar_create(fview,sLeft,sTop,sw,sLen,RTGUI_VERTICAL); if(fview->sbar != RT_NULL) { fview->sbar->widget_link = fview; fview->sbar->on_scroll = rtgui_fileview_sbar_handle; RTGUI_WIDGET_HIDE(fview->sbar);/* default hid scrollbar */ } } rtgui_fileview_set_directory(fview, directory); } return fview; }
rtgui_win_t* rtgui_win_create(struct rtgui_win* parent_window, const char* title, rtgui_rect_t *rect, rt_uint16_t style) { struct rtgui_win* win; /* allocate win memory */ win = RTGUI_WIN(rtgui_widget_create(RTGUI_WIN_TYPE)); if (win == RT_NULL) return RT_NULL; /* set parent toplevel */ #ifdef RTGUI_USING_DESKTOP_WINDOW if (style & RTGUI_WIN_STYLE_DESKTOP) { RT_ASSERT(the_desktop_window == RT_NULL); win->parent_window = RT_NULL; the_desktop_window = win; } else if (parent_window == RT_NULL) { RT_ASSERT(the_desktop_window != RT_NULL); win->parent_window = the_desktop_window; } else win->parent_window = parent_window; #else win->parent_window = parent_window; #endif /* set title, rect and style */ if (title != RT_NULL) win->title = rt_strdup(title); else win->title = RT_NULL; rtgui_widget_set_rect(RTGUI_WIDGET(win), rect); win->style = style; if (_rtgui_win_create_in_server(win) == RT_FALSE) { goto __on_err; } return win; __on_err: rtgui_widget_destroy(RTGUI_WIDGET(win)); return RT_NULL; }
struct rtgui_radiobox *rtgui_radiobox_create(const char *label, int orient, char **radio_items, int number) { struct rtgui_radiobox *radiobox; radiobox = (struct rtgui_radiobox *) rtgui_widget_create(RTGUI_RADIOBOX_TYPE); if (radiobox != RT_NULL) { rt_uint8_t board_size; struct rtgui_rect rect; radiobox->items = radio_items; radiobox->item_count = number; radiobox->item_selection = -1; radiobox->text = rt_strdup(label); /* set proper of control */ rtgui_radiobox_set_orientation(radiobox, orient); rtgui_font_get_metrics(RTGUI_WIDGET_FONT(radiobox), "H", &rect); board_size = rtgui_rect_height(rect); if (orient == RTGUI_VERTICAL) { radiobox->item_size = board_size; } else { int index; struct rtgui_font *font; struct rtgui_rect rect; /* set init item size */ radiobox->item_size = 0; font = RTGUI_WIDGET_FONT(radiobox); for (index = 0; index < number; index ++) { rtgui_font_get_metrics(font, radio_items[index], &rect); if ((board_size + 3 + rtgui_rect_width(rect)) > radiobox->item_size) radiobox->item_size = board_size + 3 + rtgui_rect_width(rect); } } if (radiobox->item_size < RADIO_BOX_H + 2) radiobox->item_size = RADIO_BOX_H + 2; } return radiobox; }
void rtgui_label_set_text(rtgui_label_t* label, const char* text) { RT_ASSERT(label != RT_NULL); if (label->text != RT_NULL) { /* release old text memory */ rt_free(label->text); } if (text != RT_NULL) label->text = (char*)rt_strdup((const char*)text); else label->text = RT_NULL; /* update widget */ rtgui_theme_draw_label(label); }
struct rtgui_iconbox* rtgui_iconbox_create(struct rtgui_image* image, const char* text, int position) { struct rtgui_iconbox* iconbox; iconbox = (struct rtgui_iconbox*)rtgui_widget_create(RTGUI_ICONBOX_TYPE); if (iconbox != RT_NULL) { rtgui_rect_t rect = {0, 0, 0, 0}, text_rect; rect.x2 = image->w; rect.y2 = image->h; /* get text rect */ rtgui_font_get_metrics(rtgui_font_default(), text, &text_rect); if (position == RTGUI_ICONBOX_TEXT_BELOW) { rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN; if (text_rect.x2 > rect.x2) { rect.x2 = text_rect.x2; } rect.y2 += text_rect.y2; } else if (position == RTGUI_ICONBOX_TEXT_RIGHT) { rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN; if (text_rect.y2 > rect.y2) { rect.y2 = text_rect.y2; } rect.x2 += text_rect.x2; } /* set widget rect */ rtgui_widget_set_rect(RTGUI_WIDGET(iconbox), &rect); /* set image and text position */ iconbox->image = image; iconbox->text = (char*)rt_strdup((const char*)text); iconbox->text_position = position; } return iconbox; }
rtgui_filelist_view_t* rtgui_filelist_view_create(const char* directory, const char* pattern, const rtgui_rect_t* rect) { struct rtgui_filelist_view* view = RT_NULL; /* create a new view */ view = (struct rtgui_filelist_view*) rtgui_widget_create(RTGUI_FILELIST_VIEW_TYPE); if (view != RT_NULL) { view->items = RT_NULL; view->pattern = rt_strdup(pattern); view->page_items = rtgui_rect_height(*rect) / (1 + rtgui_theme_get_selected_height()); rtgui_filelist_view_set_directory(view, directory); } return view; }
void rtgui_win_set_title(rtgui_win_t* win, const char *title) { /* send title to server */ if (win->flag & RTGUI_WIN_FLAG_CONNECTED) { } /* modify in local side */ if (win->title != RT_NULL) { rtgui_free(win->title); win->title = RT_NULL; } if (title != RT_NULL) { win->title = rt_strdup(title); } }
void rtgui_win_set_title(rtgui_win_t* win, const char *title) { /* send title to server */ if (RTGUI_TOPLEVEL(win)->server != RT_NULL) { } /* modify in local side */ if (win->title != RT_NULL) { rtgui_free(win->title); win->title = RT_NULL; } if (title != RT_NULL) { win->title = rt_strdup(title); } }
void rtgui_label_set_text(rtgui_label_t* label, const char* text) { RT_ASSERT(label != RT_NULL); if (label->text != RT_NULL) { /* it's a same text string */ if (rt_strncmp(text, label->text, rt_strlen(text)) == 0) return; /* release old text memory */ rtgui_free(label->text); } if (text != RT_NULL) label->text = (char*)rt_strdup((const char*)text); else label->text = RT_NULL; /* update widget */ rtgui_theme_draw_label(label); }
/* 打开列表视图用的按钮触发函数 */ static void open_btn_onbutton(rtgui_widget_t* widget, struct rtgui_event* event) { rtgui_rect_t rect; rt_uint32_t index; /* 获得顶层的workbench */ workbench = RTGUI_WORKBENCH(rtgui_widget_get_toplevel(widget)); rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect); /* 初始化图标列表 */ if (items == RT_NULL) { char item_name[32]; items = (struct rtgui_list_item *) rtgui_malloc((ITEM_MAX + 1) * sizeof(struct rtgui_list_item)); for (index = 0; index < ITEM_MAX; index ++) { rt_snprintf(item_name, sizeof(item_name), "图标%d", index); items[index].action = listitem_action; items[index].image = item_icon; items[index].name = rt_strdup(item_name); items[index].parameter = (void*) index; } items[ITEM_MAX].action = return_action; items[ITEM_MAX].image = exit_icon; items[ITEM_MAX].name = "退出"; items[ITEM_MAX].parameter = RT_NULL; } /* 创建一个列表视图, 项指定为items */ _view = rtgui_list_view_create(items, ITEM_MAX + 1, &rect, RTGUI_LIST_VIEW_ICON); /* 在workbench中添加相应的视图 */ rtgui_workbench_add_view(workbench, RTGUI_VIEW(_view)); /* 模式显示视图 */ rtgui_view_show(RTGUI_VIEW(_view), RT_TRUE); rtgui_view_destroy(RTGUI_VIEW(_view)); _view = RT_NULL; }
rtgui_label_t* rtgui_label_create(const char* text) { struct rtgui_label* label; label = (struct rtgui_label*) rtgui_widget_create(RTGUI_LABEL_TYPE); if (label != RT_NULL) { rtgui_rect_t rect; /* set default rect */ rtgui_font_get_metrics(rtgui_font_default(), text, &rect); rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1); rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1); rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect); /* set text */ label->text = (char*)rt_strdup((const char*)text); } return label; }
void log_trace_file_init(const char *filename) { rt_device_t device; device = rt_device_find("logfile"); if (device == RT_NULL) { rt_memset(&_file_device, 0x00, sizeof(_file_device)); _file_device.parent.type = RT_Device_Class_Char; _file_device.parent.init = RT_NULL; _file_device.parent.open = fdevice_open; _file_device.parent.close = fdevice_close; _file_device.parent.write = fdevice_write; rt_device_register(&_file_device.parent, "logfile", O_RDWR); } _file_device.filename = rt_strdup(filename); _file_device.fd = -1; }
rtgui_filelist_view_t *rtgui_filelist_view_create(const char *directory, const char *pattern, const rtgui_rect_t *rect) { struct rtgui_filelist_view *view = RT_NULL; /* create a new view */ view = (struct rtgui_filelist_view *) rtgui_widget_create(RTGUI_FILELIST_VIEW_TYPE); if (view != RT_NULL) { rtgui_widget_set_rect(RTGUI_WIDGET(view), rect); view->items = RT_NULL; view->pattern = rt_strdup(pattern); view->page_items = rtgui_rect_height(*rect) / (1 + rtgui_theme_get_selected_height()); /* Avoid divide by 0 error, display at least one item. */ if (view->page_items == 0) view->page_items = 1; rtgui_filelist_view_set_directory(view, directory); } return view; }
void rtgui_notebook_add_image(struct rtgui_notebook* notebook, const char* label, struct rtgui_widget* child, struct rtgui_image *pressed_image, struct rtgui_image *unpressed_image) { rtgui_rect_t rect; RT_ASSERT(notebook != RT_NULL); notebook->count += 1; notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs, sizeof(struct rtgui_notebook_tab) * notebook->count); notebook->childs[notebook->count - 1].title = rt_strdup(label); notebook->childs[notebook->count - 1].widget = child; notebook->childs[notebook->count - 1].pressed_image = pressed_image; notebook->childs[notebook->count - 1].unpressed_image = unpressed_image; /* set parent */ rtgui_widget_set_parent(child, RTGUI_WIDGET(notebook)); _rtgui_notebook_get_page_rect(notebook, &rect); rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect); rtgui_widget_set_rect(child, &rect); if (notebook->count - 1 != notebook->current) rtgui_widget_hide(child); if (RTGUI_WIDGET(notebook)->toplevel != RT_NULL && RTGUI_IS_WIN(RTGUI_WIDGET(notebook)->toplevel)) { struct rtgui_event_update_toplvl eup; RTGUI_EVENT_UPDATE_TOPLVL_INIT(&eup); eup.toplvl = RTGUI_WIDGET(notebook)->toplevel; if (RTGUI_OBJECT(child)->event_handler) RTGUI_OBJECT(child)->event_handler(RTGUI_OBJECT(child), (struct rtgui_event*)&eup); } return; }
static int xml_event_handler(rt_uint8_t event, const char* text, rt_size_t len, void* user) { static XML_STATUS status = IDLE; char fn[64]; if(event == EVENT_START) { if(strcmp(text, "name") == 0) status = READ_NAME; else if(strcmp(text, "image") == 0) status = READ_ICON; else if(strcmp(text, "author") == 0) status = READ_AUTHOR; else if(strcmp(text, "license") == 0) status = READ_LICENSE; } else if(event == EVENT_TEXT) { switch(status) { case READ_NAME: items[++pos].name = rt_strdup(text); items[pos].parameter = items[pos].name; break; case READ_ICON: rt_snprintf(fn, sizeof(fn), "%s/%s", APP_PATH, text); items[pos].image = rtgui_image_create(fn, RT_FALSE); break; case READ_AUTHOR: break; case READ_LICENSE: break; } status = IDLE; } return 1; }
void finsh_sysvar_append(const char* name, u_char type, void* var_addr) { /* create a sysvar */ struct finsh_sysvar_item* item; item = (struct finsh_sysvar_item*) rt_malloc (sizeof(struct finsh_sysvar_item)); if (item != NULL) { item->next = NULL; item->sysvar.name = rt_strdup(name); item->sysvar.type = type; item->sysvar.var = var_addr; if (global_sysvar_list == NULL) { global_sysvar_list = item; } else { item->next = global_sysvar_list; global_sysvar_list = item; } } }
/** * this function will normalize a path according to specified parent directory and file name. * * @param directory the parent path * @param filename the file name * * @return the built full file path (absolute path) */ char *dfs_normalize_path(const char *directory, const char *filename) { char *fullpath; char *dst0, *dst, *src; /* check parameters */ RT_ASSERT(filename != RT_NULL); #ifdef DFS_USING_WORKDIR if (directory == RT_NULL) /* shall use working directory */ directory = &working_directory[0]; #else if ((directory == RT_NULL) && (filename[0] != '/')) { rt_kprintf(NO_WORKING_DIR); return RT_NULL; } #endif if (filename[0] != '/') /* it's a absolute path, use it directly */ { fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2); /* join path and file name */ rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2, "%s/%s", directory, filename); } else { fullpath = rt_strdup(filename); /* copy string */ } src = fullpath; dst = fullpath; dst0 = dst; while (1) { char c = *src; if (c == '.') { if (!src[1]) src ++; /* '.' and ends */ else if (src[1] == '/') { /* './' case */ src += 2; while ((*src == '/') && (*src != '\0')) src ++; continue; } else if (src[1] == '.') { if (!src[2]) { /* '..' and ends case */ src += 2; goto up_one; } else if (src[2] == '/') { /* '../' case */ src += 3; while ((*src == '/') && (*src != '\0')) src ++; goto up_one; } } } /* copy up the next '/' and erase all '/' */ while ((c = *src++) != '\0' && c != '/') *dst ++ = c; if (c == '/') { *dst ++ = '/'; while (c == '/') c = *src++; src --; } else if (!c) break; continue; up_one: dst --; if (dst < dst0) { rt_free(fullpath); return RT_NULL; } while (dst0 < dst && dst[-1] != '/') dst --; } *dst = '\0'; /* remove '/' in the end of path if exist */ dst --; if ((dst != fullpath) && (*dst == '/')) *dst = '\0'; return fullpath; }
void rtgui_fileview_set_directory(rtgui_fileview_t* fview, const char* directory) { char fullpath[256]; rtgui_fileview_item_t *item; fview->first_item = 0; fview->item_count = 0; /* clear file information */ rtgui_fileview_clear(fview); if(directory != RT_NULL) { DIR* dir; struct stat s; rt_uint32_t i; struct dirent* dirent; fview->item_count = 0; /* open directory */ dir = opendir(directory); if(dir == RT_NULL) return; /* set current directory */ if(fview->current_dir != RT_NULL) { rt_free(fview->current_dir); fview->current_dir = RT_NULL; } fview->current_dir = rt_strdup(directory); if(fview->dlg != RT_NULL) { if(fview->dlg->path != RT_NULL) rt_free(fview->dlg->path); fview->dlg->path = rt_strdup(fview->current_dir); if(fview->dlg->tbox_path != RT_NULL) { rtgui_textbox_set_value(fview->dlg->tbox_path,fview->dlg->path); rtgui_textbox_ondraw(fview->dlg->tbox_path); } } do { dirent = readdir(dir); if(dirent == RT_NULL) break; fview->item_count ++; } while(dirent != RT_NULL); closedir(dir); if((fview->item_count > fview->item_per_page) && fview->sbar!=RT_NULL) { RTGUI_WIDGET_SHOW(fview->sbar); rtgui_scrollbar_set_line_step(fview->sbar,1); rtgui_scrollbar_set_page_step(fview->sbar, fview->item_per_page); rtgui_scrollbar_set_range(fview->sbar, fview->item_count); } else { RTGUI_WIDGET_HIDE(fview->sbar); } rtgui_widget_update_clip(fview); /* apply to memory for store all items. */ fview->items = (rtgui_fileview_item_t*) rt_malloc(sizeof(rtgui_fileview_item_t) * fview->item_count); if(fview->items == RT_NULL) goto __return; /* under the folder has not sub files. */ /* reopen directory */ dir = opendir(directory); if(dir == RT_NULL) goto __return; for(i=0; i < fview->item_count; i ++) { dirent = readdir(dir); if(dirent == RT_NULL) break; item = &(fview->items[i]); item->name = rt_strdup(dirent->d_name); rt_memset(&s, 0, sizeof(struct stat)); /* get fullpath of file */ dfs_get_fullpath(fullpath, directory, dirent->d_name); stat(fullpath, &s); if(s.st_mode & S_IFDIR) { item->type = RTGUI_FITEM_DIR; item->size = 0; } else { item->type = RTGUI_FITEM_FILE; item->size = s.st_size; } } closedir(dir); } fview->now_item = 0; __return: /* update view */ rtgui_widget_update(fview); }
/* update fileview */ void rtgui_fileview_update_current(rtgui_fileview_t* fview) { rtgui_fileview_item_t *item; rtgui_rect_t rect, item_rect, image_rect; rtgui_dc_t *dc; RT_ASSERT(fview != RT_NULL); /* begin drawing */ dc = rtgui_dc_begin_drawing(fview); if(dc == RT_NULL)return; /* if directory is null, no dispost */ if(fview->items==RT_NULL)return; rtgui_widget_get_rect(fview, &rect); if(fview->sbar && !RTGUI_WIDGET_IS_HIDE(fview->sbar)) rect.x2 -= RC_W(fview->sbar->parent.extent); if((fview->old_item >= fview->first_item) && (fview->old_item < fview->first_item+fview->item_per_page) && (fview->old_item != fview->now_item)) { /* these condition dispell blinked when drawed */ /* get old item rect */ item_rect = rect; item_rect.x1 += RTGUI_WIDGET_BORDER_SIZE(fview); item_rect.x2 -= RTGUI_WIDGET_BORDER_SIZE(fview); item_rect.y1 += RTGUI_WIDGET_BORDER_SIZE(fview); item_rect.y1 += ((fview->old_item-fview->first_item) % fview->item_per_page) * (1 + RTGUI_SEL_H); item_rect.y2 = item_rect.y1 + (1 + RTGUI_SEL_H); /* get image rect */ image_rect.x1 = RTGUI_MARGIN; image_rect.y1 = 0; image_rect.x2 = RTGUI_MARGIN + file_image->w; image_rect.y2 = file_image->h; rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL); /* draw old item */ item = &(fview->items[fview->old_item]); if(item->type == RTGUI_FITEM_FILE) /* draw item image */ rtgui_image_paste(file_image, dc, &image_rect, Black); else rtgui_image_paste(folder_image, dc, &image_rect,Black); item_rect.x1 += RTGUI_MARGIN + file_image->w + 2; item_rect.x2 = item_rect.x1 + rtgui_font_get_string_width(RTGUI_DC_FONT(dc), item->name); RTGUI_DC_BC(dc) = theme.blankspace; RTGUI_DC_FC(dc) = theme.foreground; rtgui_dc_fill_rect(dc,&item_rect); rtgui_dc_draw_text(dc, item->name, &item_rect); } /* draw current item */ item_rect = rect; item_rect.x1 += RTGUI_WIDGET_BORDER_SIZE(fview); item_rect.x2 -= RTGUI_WIDGET_BORDER_SIZE(fview); item_rect.y1 += RTGUI_WIDGET_BORDER_SIZE(fview); item_rect.y1 += ((fview->now_item-fview->first_item) % fview->item_per_page) * (1 + RTGUI_SEL_H); item_rect.y2 = item_rect.y1 + (1 + RTGUI_SEL_H); /* get image base rect */ image_rect.x1 = RTGUI_MARGIN; image_rect.y1 = 0; image_rect.x2 = RTGUI_MARGIN + file_image->w; image_rect.y2 = file_image->h; rtgui_rect_moveto_align(&item_rect, &image_rect, RTGUI_ALIGN_CENTER_VERTICAL); item = &(fview->items[fview->now_item]); if(item->type == RTGUI_FITEM_FILE) /* draw item image */ rtgui_image_paste(file_image, dc, &image_rect, Black); else rtgui_image_paste(folder_image, dc, &image_rect, Black); if(fview->dlg != RT_NULL) { if(fview->dlg->filename != RT_NULL) { rt_free(fview->dlg->filename); fview->dlg->filename = RT_NULL; } fview->dlg->filename = rt_strdup(item->name); } item_rect.x1 += RTGUI_MARGIN + file_image->w + 2; item_rect.x2 = item_rect.x1 + rtgui_font_get_string_width(RTGUI_DC_FONT(dc), item->name); { if(RTGUI_WIDGET_IS_FOCUSED(fview)) { RTGUI_DC_BC(dc) = DarkBlue; RTGUI_DC_FC(dc) = theme.blankspace; } else { RTGUI_DC_BC(dc) = Gray; RTGUI_DC_FC(dc) = theme.foreground; } rtgui_dc_fill_rect(dc, &item_rect); rtgui_dc_draw_text(dc, item->name, &item_rect); } if(fview->dlg != RT_NULL) { if(item->type == RTGUI_FITEM_FILE) { if(fview->dlg->tbox_filename != RT_NULL) { rtgui_textbox_set_value(fview->dlg->tbox_filename,fview->dlg->filename); RTGUI_DC_FC(dc) = theme.foreground; rtgui_textbox_ondraw(fview->dlg->tbox_filename); } } } rtgui_dc_end_drawing(dc); }