/src/demos/demo_dialog.c

http://ftk.googlecode.com/ · C · 126 lines · 96 code · 25 blank · 5 comment · 4 complexity · f2ced2c1afe01c416b72b0be4c3b57da MD5 · raw file

  1. #include "ftk.h"
  2. #ifdef FTK_AS_PLUGIN
  3. #include "ftk_app_demo.h"
  4. FTK_HIDE int FTK_MAIN(int argc, char* argv[]);
  5. FtkApp* ftk_app_demo_dialog_create()
  6. {
  7. return ftk_app_demo_create(_("dialog"), ftk_main);
  8. }
  9. #else
  10. #define FTK_HIDE extern
  11. #endif /*FTK_AS_PLUGIN*/
  12. static Ret button_quit_clicked(void* ctx, void* obj)
  13. {
  14. if(ctx != NULL)
  15. {
  16. /*modal*/
  17. *(int*)ctx = ftk_widget_id(obj);
  18. }
  19. else
  20. {
  21. /*modal-less*/
  22. ftk_widget_unref(ftk_widget_toplevel(obj));
  23. }
  24. return RET_QUIT;
  25. }
  26. static Ret button_close_clicked(void* ctx, void* obj)
  27. {
  28. ftk_widget_unref(ctx);
  29. return RET_OK;
  30. }
  31. static Ret button_dialog_clicked(void* ctx, void* obj)
  32. {
  33. int id = 0;
  34. int width = 0;
  35. int height = 0;
  36. // FtkWidget* label = NULL;
  37. FtkWidget* button = NULL;
  38. FtkWidget* dialog = NULL;
  39. FtkBitmap* icon = NULL;
  40. FtkWidget* combo_box = NULL;
  41. int modal = (int)ctx;
  42. ftk_logd("%s:%d begin\n", __func__, __LINE__);
  43. dialog = ftk_dialog_create(0, 40, 320, 240);
  44. icon = ftk_theme_load_image(ftk_default_theme(), "info"FTK_STOCK_IMG_SUFFIX);
  45. ftk_dialog_set_icon(dialog, icon);
  46. width = ftk_widget_width(dialog);
  47. height = ftk_widget_height(dialog);
  48. // label = ftk_label_create(dialog, width/6, height/4, 5*width/6, 20);
  49. // ftk_widget_set_text(label, "Are you sure to quit?");
  50. combo_box = ftk_combo_box_create(dialog, width/6, height/4, 2*width/3, 30);
  51. ftk_combo_box_set_text(combo_box, "1 second");
  52. ftk_combo_box_append(combo_box, NULL, "1 second");
  53. ftk_combo_box_append(combo_box, NULL, "2 seconds");
  54. ftk_combo_box_append(combo_box, NULL, "3 seconds");
  55. ftk_entry_set_readonly(ftk_combo_box_get_entry(combo_box), modal);
  56. button = ftk_button_create(dialog, width/6, height/2, width/3, 50);
  57. ftk_widget_set_text(button, "yes");
  58. ftk_button_set_clicked_listener(button, button_quit_clicked, modal ? &id : NULL);
  59. button = ftk_button_create(dialog, width/2, height/2, width/3, 50);
  60. ftk_widget_set_text(button, "no");
  61. ftk_button_set_clicked_listener(button, button_quit_clicked, modal ? &id : NULL);
  62. ftk_window_set_focus(dialog, button);
  63. ftk_widget_set_text(dialog, modal ? "model dialog" : "normal dialog");
  64. if(modal)
  65. {
  66. assert(ftk_dialog_run(dialog) == id);
  67. ftk_widget_unref(dialog);
  68. }
  69. else
  70. {
  71. ftk_widget_show_all(dialog, 1);
  72. }
  73. ftk_logd("%s:%d end\n", __func__, __LINE__);
  74. return RET_OK;
  75. }
  76. FTK_HIDE int FTK_MAIN(int argc, char* argv[])
  77. {
  78. int width = 0;
  79. int height = 0;
  80. FtkWidget* win = NULL;
  81. FtkWidget* button = NULL;
  82. FTK_INIT(argc, argv);
  83. win = ftk_app_window_create();
  84. ftk_window_set_animation_hint(win, "app_main_window");
  85. width = ftk_widget_width(win);
  86. height = ftk_widget_height(win);
  87. button = ftk_button_create(win, 0, height/6, width/3, 50);
  88. ftk_widget_set_text(button, "Normal");
  89. ftk_button_set_clicked_listener(button, button_dialog_clicked, NULL);
  90. button = ftk_button_create(win, 2*width/3, height/6, width/3, 50);
  91. ftk_widget_set_text(button, "Modal");
  92. ftk_button_set_clicked_listener(button, button_dialog_clicked, (void*)1);
  93. button = ftk_button_create(win, width/4, height/2, width/2, 60);
  94. ftk_widget_set_text(button, "quit");
  95. ftk_button_set_clicked_listener(button, button_close_clicked, win);
  96. ftk_widget_set_text(win, "demo dialog");
  97. ftk_widget_show_all(win, 1);
  98. FTK_QUIT_WHEN_WIDGET_CLOSE(win);
  99. FTK_RUN();
  100. return 0;
  101. }