1
0

demo_dialog.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. int modal = (int)ctx;
  41. ftk_logd("%s:%d begin\n", __func__, __LINE__);
  42. dialog = ftk_dialog_create(0, 40, 320, 240);
  43. icon = ftk_theme_load_image(ftk_default_theme(), "info-icon"FTK_STOCK_IMG_SUFFIX);
  44. ftk_dialog_set_icon(dialog, icon);
  45. width = ftk_widget_width(dialog);
  46. height = ftk_widget_height(dialog);
  47. label = ftk_label_create(dialog, width/6, height/4, 5*width/6, 20);
  48. ftk_widget_set_text(label, "Are you sure to quit?");
  49. button = ftk_button_create(dialog, width/6, height/2, width/3, 50);
  50. ftk_widget_set_text(button, "yes");
  51. ftk_button_set_clicked_listener(button, button_quit_clicked, modal ? &id : NULL);
  52. button = ftk_button_create(dialog, width/2, height/2, width/3, 50);
  53. ftk_widget_set_text(button, "no");
  54. ftk_button_set_clicked_listener(button, button_quit_clicked, modal ? &id : NULL);
  55. ftk_window_set_focus(dialog, button);
  56. ftk_widget_set_text(dialog, modal ? "model dialog" : "normal dialog");
  57. ftk_widget_show_all(dialog, 1);
  58. if(modal)
  59. {
  60. assert(ftk_dialog_run(dialog) == id);
  61. ftk_widget_unref(dialog);
  62. }
  63. else
  64. {
  65. ftk_widget_show_all(dialog, 1);
  66. }
  67. ftk_logd("%s:%d end\n", __func__, __LINE__);
  68. return RET_OK;
  69. }
  70. FTK_HIDE int FTK_MAIN(int argc, char* argv[])
  71. {
  72. int width = 0;
  73. int height = 0;
  74. FtkWidget* win = NULL;
  75. FtkWidget* button = NULL;
  76. FTK_INIT(argc, argv);
  77. win = ftk_app_window_create();
  78. ftk_window_set_animation_hint(win, "app_main_window");
  79. width = ftk_widget_width(win);
  80. height = ftk_widget_height(win);
  81. button = ftk_button_create(win, 0, height/6, width/3, 50);
  82. ftk_widget_set_text(button, "Normal");
  83. ftk_button_set_clicked_listener(button, button_dialog_clicked, NULL);
  84. button = ftk_button_create(win, 2*width/3, height/6, width/3, 50);
  85. ftk_widget_set_text(button, "Modal");
  86. ftk_button_set_clicked_listener(button, button_dialog_clicked, (void*)1);
  87. button = ftk_button_create(win, width/4, height/2, width/2, 60);
  88. ftk_widget_set_text(button, "quit");
  89. ftk_button_set_clicked_listener(button, button_close_clicked, win);
  90. ftk_widget_set_text(win, "demo dialog");
  91. ftk_widget_show_all(win, 1);
  92. FTK_QUIT_WHEN_WIDGET_CLOSE(win);
  93. FTK_RUN();
  94. return 0;
  95. }