/apps/designer/select_file.c

http://ftk.googlecode.com/ · C · 175 lines · 109 code · 37 blank · 29 comment · 7 complexity · 53384550be9f1b2a00af2edb9657473b MD5 · raw file

  1. /*
  2. * File: save.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: ui to save xul file.
  5. *
  6. * Copyright (c) 2009 - 2011 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2011-09-27 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "save.h"
  31. #include "code_gen.h"
  32. #include "ftk_util.h"
  33. #include "ftk_file_system.h"
  34. #include "ftk_file_browser.h"
  35. static char s_last_file[FTK_MAX_PATH+1] = {0};
  36. typedef struct _Info
  37. {
  38. FtkWidget* widget_file_name;
  39. FtkListener on_select;
  40. void* on_select_ctx;
  41. char* init_path;
  42. }Info;
  43. static Info* info_create(void)
  44. {
  45. Info* info = FTK_NEW(Info);
  46. return info;
  47. }
  48. static void info_destroy(void* data)
  49. {
  50. Info* info = (Info*)data;
  51. if(info->init_path != NULL)
  52. {
  53. FTK_FREE(info->init_path);
  54. }
  55. FTK_FREE(data);
  56. return;
  57. }
  58. static Ret on_file_selected(void* ctx, int index, const char* path)
  59. {
  60. FtkWidget* win = (FtkWidget*)ctx;
  61. char filename[FTK_MAX_PATH+1] = {0};
  62. Info* info = (Info*)ftk_widget_user_data(win);
  63. ftk_strncpy(filename, path, FTK_MAX_PATH);
  64. ftk_normalize_path(filename);
  65. ftk_widget_set_text(info->widget_file_name, filename);
  66. return RET_OK;
  67. }
  68. static Ret button_browse_clicked(void* ctx, void* obj)
  69. {
  70. FtkWidget* win = ftk_file_browser_create(FTK_FILE_BROWER_SINGLE_CHOOSER);
  71. ftk_file_browser_set_choosed_handler(win, on_file_selected, ctx);
  72. ftk_file_browser_set_path(win, s_last_file);
  73. ftk_file_browser_load(win);
  74. return RET_OK;
  75. }
  76. static Ret button_ok_clicked(void* ctx, void* obj)
  77. {
  78. char filename[FTK_MAX_PATH+1] = {0};
  79. FtkWidget* win = (FtkWidget*)ctx;
  80. Info* info = (Info*)ftk_widget_user_data(win);
  81. ftk_strncpy(filename, ftk_widget_get_text(info->widget_file_name), FTK_MAX_PATH);
  82. return_val_if_fail(strlen(filename) > 0, RET_FAIL);
  83. if(info->on_select != NULL)
  84. {
  85. info->on_select(info->on_select_ctx, filename);
  86. ftk_strncpy(s_last_file, filename, FTK_MAX_PATH);
  87. }
  88. ftk_widget_unref(win);
  89. return RET_OK;
  90. }
  91. static Ret button_cancel_clicked(void* ctx, void* obj)
  92. {
  93. FtkWidget* win = (FtkWidget*)ctx;
  94. ftk_widget_unref(win);
  95. return RET_OK;
  96. }
  97. Ret designer_select_file(const char* title, const char* init_path, FtkListener on_select, void* ctx)
  98. {
  99. int width = 0;
  100. int height = 0;
  101. int y_offset = 0;
  102. Info* info = NULL;
  103. FtkWidget* win = NULL;
  104. FtkWidget* label = NULL;
  105. FtkWidget* entry = NULL;
  106. FtkWidget* button = NULL;
  107. info = info_create();
  108. return_val_if_fail(info != NULL, RET_FAIL);
  109. win = ftk_app_window_create();
  110. ftk_widget_set_user_data(win, info_destroy, info);
  111. ftk_widget_set_text(win, title);
  112. width = ftk_widget_width(win);
  113. height = ftk_widget_height(win);
  114. info->on_select = on_select;
  115. info->on_select_ctx = ctx;
  116. info->init_path = ftk_strdup(init_path);
  117. y_offset = height/3 - 30;
  118. label = ftk_label_create(win, 0, y_offset, width/5, 30);
  119. ftk_widget_set_text(label, _("FileName:"));
  120. y_offset = height/3;
  121. info->widget_file_name = entry = ftk_entry_create(win, 5, y_offset, width*4/5-2, 30);
  122. if(s_last_file[0] == '\0')
  123. {
  124. ftk_fs_get_cwd(s_last_file);
  125. }
  126. ftk_widget_set_text(entry, s_last_file);
  127. button = ftk_button_create(win, width*4/5, y_offset-5, width/5, 40);
  128. ftk_widget_set_text(button, _("..."));
  129. ftk_button_set_clicked_listener(button, button_browse_clicked, win);
  130. button = ftk_button_create(win, width/5, height-60, width/5, 50);
  131. ftk_widget_set_text(button, _("OK"));
  132. ftk_button_set_clicked_listener(button, button_ok_clicked, win);
  133. button = ftk_button_create(win, width*3/5, height-60, width/5, 50);
  134. ftk_widget_set_text(button, _("Cancel"));
  135. ftk_button_set_clicked_listener(button, button_cancel_clicked, win);
  136. ftk_widget_show_all(win, 1);
  137. return RET_OK;
  138. }