demo_transparent.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "ftk.h"
  2. void create_dialog(FtkBitmap* bitmap, FtkColor bg);
  3. static void create_app_window(void);
  4. static Ret button_open_image_dialog(void* ctx, void* obj)
  5. {
  6. char filename[FTK_MAX_PATH+1] = {0};
  7. FtkBitmap* bitmap = NULL;
  8. FtkColor bg = {0};
  9. bg.r = 0xff;
  10. bg.g = 0xff;
  11. bg.b = 0xff;
  12. ftk_snprintf(filename, FTK_MAX_PATH, "%s/earth.png",
  13. ftk_config_get_test_data_dir(ftk_default_config()));
  14. bitmap = ftk_bitmap_factory_load(ftk_default_bitmap_factory(), filename);
  15. bg.a = 0x0;
  16. create_dialog(bitmap, bg);
  17. return RET_OK;
  18. }
  19. static Ret button_open_transparent_dialog(void* ctx, void* obj)
  20. {
  21. FtkColor bg = {0};
  22. bg.a = 0x80;
  23. bg.r = 0xff;
  24. bg.g = 0xff;
  25. bg.b = 0xff;
  26. create_dialog(NULL, bg);
  27. return RET_OK;
  28. }
  29. static Ret button_quit_clicked(void* ctx, void* obj)
  30. {
  31. *(int*)ctx = ftk_widget_id(obj);
  32. return RET_QUIT;
  33. }
  34. static Ret button_close_clicked(void* ctx, void* obj)
  35. {
  36. ftk_widget_unref(ctx);
  37. return RET_OK;
  38. }
  39. void create_dialog(FtkBitmap* bitmap, FtkColor bg)
  40. {
  41. int id = 0;
  42. int width = ftk_display_width(ftk_default_display());
  43. int height = ftk_display_height(ftk_default_display());
  44. FtkWidget* button = NULL;
  45. FtkWidget* label = NULL;
  46. FtkWidget* dialog = ftk_dialog_create(0, 5, 320, height-60);
  47. ftk_widget_set_attr(dialog, FTK_ATTR_BG_CENTER);
  48. ftk_window_set_background_with_alpha(dialog, bitmap, bg);
  49. width = ftk_widget_width(dialog);
  50. height = ftk_widget_height(dialog);
  51. //label = ftk_label_create(dialog, width/8, height/4, 3*width/4, 20);
  52. //ftk_widget_set_text(label, "Are you sure to quit?");
  53. label = ftk_label_create(dialog, width/8, height/2, 3*width/4, 20);
  54. ftk_widget_set_text(label, "Are you sure to quit?");
  55. button = ftk_button_create(dialog, width/6, 3*height/4, width/3, 50);
  56. ftk_widget_set_text(button, "yes");
  57. ftk_button_set_clicked_listener(button, button_quit_clicked, &id);
  58. button = ftk_button_create(dialog, width/2, 3*height/4, width/3, 50);
  59. ftk_widget_set_text(button, "no");
  60. ftk_button_set_clicked_listener(button, button_quit_clicked, &id);
  61. ftk_window_set_focus(dialog, button);
  62. ftk_widget_set_text(dialog, "transparent demo");
  63. ftk_widget_show_all(dialog, 1);
  64. assert(ftk_dialog_run(dialog) == id);
  65. ftk_widget_unref(dialog);
  66. return;
  67. }
  68. static void create_app_window(void)
  69. {
  70. int width = 0;
  71. int height = 0;
  72. FtkWidget* win = ftk_app_window_create();
  73. FtkWidget* button = NULL;
  74. width = ftk_widget_width(win);
  75. height = ftk_widget_height(win);
  76. button = ftk_button_create(win, 0, height/6, width/3, 50);
  77. ftk_widget_set_text(button, "图片背景");
  78. ftk_button_set_clicked_listener(button, button_open_image_dialog, win);
  79. button = ftk_button_create(win, 2*width/3, height/6, width/3, 50);
  80. ftk_widget_set_text(button, "半透明效果");
  81. ftk_button_set_clicked_listener(button, button_open_transparent_dialog, win);
  82. button = ftk_button_create(win, width/4, height/2, width/2, 60);
  83. ftk_widget_set_text(button, "quit");
  84. ftk_button_set_clicked_listener(button, button_close_clicked, win);
  85. ftk_widget_set_text(win, "transparent");
  86. ftk_widget_show_all(win, 1);
  87. FTK_QUIT_WHEN_WIDGET_CLOSE(win);
  88. return;
  89. }
  90. #ifdef FTK_AS_PLUGIN
  91. #include "ftk_app_demo.h"
  92. FTK_HIDE int FTK_MAIN(int argc, char* argv[]);
  93. FtkApp* ftk_app_demo_transparent_create()
  94. {
  95. return ftk_app_demo_create(_("transparent"), ftk_main);
  96. }
  97. #else
  98. #define FTK_HIDE extern
  99. #endif /*FTK_AS_PLUGIN*/
  100. FTK_HIDE int FTK_MAIN(int argc, char* argv[])
  101. {
  102. FTK_INIT(argc, argv);
  103. create_app_window();
  104. FTK_RUN();
  105. return 0;
  106. }