demo_button.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "ftk.h"
  2. #define IDC_TEST_BUTTON 1000
  3. static Ret button_quit_clicked(void* ctx, void* obj)
  4. {
  5. ftk_widget_unref(ctx);
  6. return RET_OK;
  7. }
  8. static Ret button_hide_clicked(void* ctx, void* obj)
  9. {
  10. ftk_widget_show(ftk_widget_lookup(ctx, IDC_TEST_BUTTON), 0);
  11. return RET_OK;
  12. }
  13. static Ret button_show_clicked(void* ctx, void* obj)
  14. {
  15. ftk_widget_show(ftk_widget_lookup(ctx, IDC_TEST_BUTTON), 1);
  16. return RET_OK;
  17. }
  18. static Ret button_default_clicked(void* ctx, void* obj)
  19. {
  20. printf("%s: button %s is clicked.\n", __func__, ftk_widget_get_text(obj));
  21. return RET_OK;
  22. }
  23. #ifdef FTK_AS_PLUGIN
  24. #include "ftk_app_demo.h"
  25. FTK_HIDE int FTK_MAIN(int argc, char* argv[]);
  26. FtkApp* ftk_app_demo_button_create()
  27. {
  28. return ftk_app_demo_create(_("button"), ftk_main);
  29. }
  30. #else
  31. #define FTK_HIDE extern
  32. #endif /*FTK_AS_PLUGIN*/
  33. FTK_HIDE int FTK_MAIN(int argc, char* argv[])
  34. {
  35. int width = 0;
  36. int height = 0;
  37. FtkWidget* win = NULL;
  38. FtkWidget* button = NULL;
  39. FTK_INIT(argc, argv);
  40. win = ftk_app_window_create();
  41. ftk_window_set_animation_hint(win, "app_main_window");
  42. width = ftk_widget_width(win);
  43. height = ftk_widget_height(win);
  44. width = width/3 - 10;
  45. button = ftk_button_create(win, 0, 30, width, 50);
  46. ftk_widget_set_text(button, "show");
  47. ftk_button_set_clicked_listener(button, button_show_clicked, win);
  48. button = ftk_button_create(win, width + 10, 30, width, 50);
  49. ftk_widget_set_text(button, "hide");
  50. ftk_button_set_clicked_listener(button, button_hide_clicked, win);
  51. button = ftk_button_create(win, 2*(width + 10), 30, width, 50);
  52. ftk_widget_set_text(button, "按钮测试");
  53. ftk_widget_set_id(button, IDC_TEST_BUTTON);
  54. ftk_button_set_clicked_listener(button, button_default_clicked, win);
  55. button = ftk_button_create(win, 0, 130, width, 40);
  56. ftk_widget_set_text(button, "yes");
  57. ftk_button_set_clicked_listener(button, button_default_clicked, win);
  58. button = ftk_button_create(win, 2*(width + 10), 130, width, 40);
  59. ftk_widget_set_text(button, "no");
  60. ftk_button_set_clicked_listener(button, button_default_clicked, win);
  61. button = ftk_button_create(win, width + 10, height/2, width, 60);
  62. ftk_widget_set_text(button, "quit");
  63. ftk_button_set_clicked_listener(button, button_quit_clicked, win);
  64. ftk_window_set_focus(win, button);
  65. ftk_widget_set_text(win, "button demo");
  66. ftk_widget_show_all(win, 1);
  67. FTK_QUIT_WHEN_WIDGET_CLOSE(win);
  68. FTK_RUN();
  69. return 0;
  70. }