demo_button.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. width = ftk_widget_width(win);
  42. height = ftk_widget_height(win);
  43. width = width/3 - 10;
  44. button = ftk_button_create(win, 0, 30, width, 50);
  45. ftk_widget_set_text(button, "show");
  46. ftk_button_set_clicked_listener(button, button_show_clicked, win);
  47. button = ftk_button_create(win, width + 10, 30, width, 50);
  48. ftk_widget_set_text(button, "hide");
  49. ftk_button_set_clicked_listener(button, button_hide_clicked, win);
  50. button = ftk_button_create(win, 2*(width + 10), 30, width, 50);
  51. ftk_widget_set_text(button, "按钮测试");
  52. ftk_widget_set_id(button, IDC_TEST_BUTTON);
  53. ftk_button_set_clicked_listener(button, button_default_clicked, win);
  54. button = ftk_button_create(win, 0, 130, width, 40);
  55. ftk_widget_set_text(button, "yes");
  56. ftk_button_set_clicked_listener(button, button_default_clicked, win);
  57. button = ftk_button_create(win, 2*(width + 10), 130, width, 40);
  58. ftk_widget_set_text(button, "no");
  59. ftk_button_set_clicked_listener(button, button_default_clicked, win);
  60. button = ftk_button_create(win, width + 10, height/2, width, 60);
  61. ftk_widget_set_text(button, "quit");
  62. ftk_button_set_clicked_listener(button, button_quit_clicked, win);
  63. ftk_window_set_focus(win, button);
  64. ftk_widget_set_text(win, "button demo");
  65. ftk_widget_show_all(win, 1);
  66. FTK_QUIT_WHEN_WIDGET_CLOSE(win);
  67. FTK_RUN();
  68. return 0;
  69. }