decor-fancy.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <stdint.h>
  2. #include <toaru/yutani.h>
  3. #include <toaru/graphics.h>
  4. #include <toaru/decorations.h>
  5. #include <toaru/drawstring.h>
  6. #define INACTIVE 9
  7. #define TTK_FANCY_PATH "/usr/share/ttk/"
  8. static int u_height = 33;
  9. static int ul_width = 10;
  10. static int ur_width = 10;
  11. static int ml_width = 6;
  12. static int mr_width = 6;
  13. static int l_height = 9;
  14. static int ll_width = 9;
  15. static int lr_width = 9;
  16. static int llx_offset = 3;
  17. static int lly_offset = 3;
  18. static int lrx_offset = 3;
  19. static int lry_offset = 3;
  20. static sprite_t * sprites[20];
  21. #define TEXT_OFFSET 10
  22. static void init_sprite(int id, char * path) {
  23. sprites[id] = malloc(sizeof(sprite_t));
  24. load_sprite(sprites[id], path);
  25. sprites[id]->alpha = ALPHA_EMBEDDED;
  26. }
  27. static void render_decorations_fancy(yutani_window_t * window, gfx_context_t * ctx, char * title, int decors_active) {
  28. int width = window->width;
  29. int height = window->height;
  30. for (int j = 0; j < decor_top_height; ++j) {
  31. for (int i = 0; i < width; ++i) {
  32. GFX(ctx,i,j) = 0;
  33. }
  34. }
  35. for (int j = decor_top_height; j < height - decor_bottom_height; ++j) {
  36. for (int i = 0; i < decor_left_width; ++i) {
  37. GFX(ctx,i,j) = 0;
  38. }
  39. for (int i = width - decor_right_width; i < width; ++i) {
  40. GFX(ctx,i,j) = 0;
  41. }
  42. }
  43. for (int j = height - decor_bottom_height; j < height; ++j) {
  44. for (int i = 0; i < width; ++i) {
  45. GFX(ctx,i,j) = 0;
  46. }
  47. }
  48. if (decors_active == DECOR_INACTIVE) decors_active = INACTIVE;
  49. draw_sprite(ctx, sprites[decors_active + 0], 0, 0);
  50. for (int i = 0; i < width - (ul_width + ur_width); ++i) {
  51. draw_sprite(ctx, sprites[decors_active + 1], i + ul_width, 0);
  52. }
  53. draw_sprite(ctx, sprites[decors_active + 2], width - ur_width, 0);
  54. for (int i = 0; i < height - (u_height + l_height); ++i) {
  55. draw_sprite(ctx, sprites[decors_active + 3], 0, i + u_height);
  56. draw_sprite(ctx, sprites[decors_active + 4], width - mr_width, i + u_height);
  57. }
  58. draw_sprite(ctx, sprites[decors_active + 5], 0, height - l_height);
  59. for (int i = 0; i < width - (ll_width + lr_width); ++i) {
  60. draw_sprite(ctx, sprites[decors_active + 6], i + ll_width, height - l_height);
  61. }
  62. draw_sprite(ctx, sprites[decors_active + 7], width - lr_width, height - l_height);
  63. char * tmp_title = strdup(title);
  64. int t_l = strlen(tmp_title);
  65. #define EXTRA_SPACE 40
  66. if (draw_string_width(tmp_title) + EXTRA_SPACE > width) {
  67. while (t_l >= 0 && (draw_string_width(tmp_title) + EXTRA_SPACE > width)) {
  68. tmp_title[t_l] = '\0';
  69. t_l--;
  70. }
  71. }
  72. if (strlen(tmp_title)) {
  73. int title_offset = (width / 2) - (draw_string_width(tmp_title) / 2);
  74. if (decors_active == 0) {
  75. draw_string(ctx, title_offset, TEXT_OFFSET, rgb(226,226,226), tmp_title);
  76. } else {
  77. draw_string(ctx, title_offset, TEXT_OFFSET, rgb(147,147,147), tmp_title);
  78. }
  79. }
  80. free(tmp_title);
  81. /* Buttons */
  82. draw_sprite(ctx, sprites[decors_active + 8], width - 28, 16);
  83. }
  84. static int check_button_press_fancy(yutani_window_t * window, int x, int y) {
  85. if (x >= window->width - 28 && x <= window->width - 18 &&
  86. y >= 16 && y <= 26) {
  87. return DECOR_CLOSE;
  88. }
  89. return 0;
  90. }
  91. void decor_init() {
  92. init_sprite(0, TTK_FANCY_PATH "active/ul.bmp");
  93. init_sprite(1, TTK_FANCY_PATH "active/um.bmp");
  94. init_sprite(2, TTK_FANCY_PATH "active/ur.bmp");
  95. init_sprite(3, TTK_FANCY_PATH "active/ml.bmp");
  96. init_sprite(4, TTK_FANCY_PATH "active/mr.bmp");
  97. init_sprite(5, TTK_FANCY_PATH "active/ll.bmp");
  98. init_sprite(6, TTK_FANCY_PATH "active/lm.bmp");
  99. init_sprite(7, TTK_FANCY_PATH "active/lr.bmp");
  100. init_sprite(8, TTK_FANCY_PATH "active/button-close.bmp");
  101. init_sprite(INACTIVE + 0, TTK_FANCY_PATH "inactive/ul.bmp");
  102. init_sprite(INACTIVE + 1, TTK_FANCY_PATH "inactive/um.bmp");
  103. init_sprite(INACTIVE + 2, TTK_FANCY_PATH "inactive/ur.bmp");
  104. init_sprite(INACTIVE + 3, TTK_FANCY_PATH "inactive/ml.bmp");
  105. init_sprite(INACTIVE + 4, TTK_FANCY_PATH "inactive/mr.bmp");
  106. init_sprite(INACTIVE + 5, TTK_FANCY_PATH "inactive/ll.bmp");
  107. init_sprite(INACTIVE + 6, TTK_FANCY_PATH "inactive/lm.bmp");
  108. init_sprite(INACTIVE + 7, TTK_FANCY_PATH "inactive/lr.bmp");
  109. init_sprite(INACTIVE + 8, TTK_FANCY_PATH "inactive/button-close.bmp");
  110. decor_top_height = 33;
  111. decor_bottom_height = 6;
  112. decor_left_width = 6;
  113. decor_right_width = 6;
  114. decor_render_decorations = render_decorations_fancy;
  115. decor_check_button_press = check_button_press_fancy;
  116. }