ask_mag.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "globals.h"
  2. #include "local_proto.h"
  3. #include <grass/display.h>
  4. struct box
  5. {
  6. int top, bottom, left, right;
  7. };
  8. static struct box plus, minus, value;
  9. static struct box cancel, accept;
  10. static int mag;
  11. static int inbox(struct box *, int, int);
  12. static int dotext(char *, int, int, int, int, int);
  13. static int incr(int, int);
  14. int ask_magnification(int *magnification)
  15. {
  16. static int use = 1;
  17. int x, y;
  18. int height;
  19. int stat;
  20. int width;
  21. int top, bottom, left, right;
  22. static Objects objects[] = {
  23. OTHER(incr, &use),
  24. {0}
  25. };
  26. Menu_msg("");
  27. mag = *magnification;
  28. if (mag < 1)
  29. mag = 1;
  30. height = VIEW_MENU->nrows;
  31. R_text_size(height - 4, height - 4);
  32. Get_mouse_xy(&x, &y);
  33. top = y - height / 2;
  34. if (top < SCREEN_TOP)
  35. top = SCREEN_TOP;
  36. bottom = top + 4 * height;
  37. if (bottom >= VIEW_MENU->top) {
  38. top -= bottom - (VIEW_MENU->top - 1);
  39. bottom = VIEW_MENU->top - 1;
  40. }
  41. width = Text_width("MAGNIFICATION") + 4;
  42. left = x - width / 2;
  43. if (left < SCREEN_LEFT)
  44. left = SCREEN_LEFT;
  45. right = left + width;
  46. if (right > SCREEN_RIGHT) {
  47. left -= right - SCREEN_RIGHT;
  48. right = SCREEN_RIGHT;
  49. }
  50. R_panel_save(tempfile1, top, bottom, left, right);
  51. R_standard_color(WHITE);
  52. R_box_abs(left, top, right, bottom);
  53. R_standard_color(BLACK);
  54. Outline_box(top, bottom, left, right);
  55. plus.top = top + height;
  56. plus.bottom = plus.top + height;
  57. plus.left = left;
  58. plus.right = plus.left + Text_width("++") + 4;
  59. Outline_box(plus.top, plus.bottom, plus.left, plus.right);
  60. minus.top = top + height;
  61. minus.bottom = minus.top + height;
  62. minus.right = right;
  63. minus.left = minus.right - Text_width("--") - 4;
  64. Outline_box(minus.top, minus.bottom, minus.left, minus.right);
  65. value.top = top + height;
  66. value.bottom = value.top + height;
  67. value.left = plus.right;
  68. value.right = minus.left;
  69. Outline_box(value.top, value.bottom, value.left, value.right);
  70. accept.top = value.bottom;
  71. accept.bottom = accept.top + height;
  72. accept.left = left;
  73. accept.right = right;
  74. Outline_box(accept.top, accept.bottom, accept.left, accept.right);
  75. cancel.top = accept.bottom;
  76. cancel.bottom = cancel.top + height;
  77. cancel.left = left;
  78. cancel.right = right;
  79. Outline_box(cancel.top, cancel.bottom, cancel.left, cancel.right);
  80. dotext("MAGNIFICATION", top, top + height, left, right, WHITE);
  81. dotext("+", plus.top, plus.bottom, plus.left, plus.right, GREY);
  82. dotext("-", minus.top, minus.bottom, minus.left, minus.right, GREY);
  83. dotext("ACCEPT", accept.top, accept.bottom, accept.left, accept.right,
  84. GREY);
  85. dotext("CANCEL", cancel.top, cancel.bottom, cancel.left, cancel.right,
  86. GREY);
  87. draw_mag();
  88. stat = Input_pointer(objects);
  89. /* to respond to user */
  90. R_standard_color(WHITE);
  91. R_box_abs(left, top, right, bottom);
  92. R_flush();
  93. R_panel_restore(tempfile1);
  94. R_panel_delete(tempfile1);
  95. *magnification = mag;
  96. return stat > 0;
  97. }
  98. int draw_mag(void)
  99. {
  100. char buf[10];
  101. sprintf(buf, "%d", mag);
  102. dotext(buf, value.top, value.bottom, value.left, value.right, WHITE);
  103. return 0;
  104. }
  105. static int incr(int x, int y)
  106. {
  107. if (inbox(&accept, x, y))
  108. return 1;
  109. if (inbox(&cancel, x, y))
  110. return -1;
  111. if (inbox(&plus, x, y)) {
  112. mag++;
  113. draw_mag();
  114. }
  115. else if (inbox(&minus, x, y) && mag > 1) {
  116. mag--;
  117. draw_mag();
  118. }
  119. return 0;
  120. }
  121. static int dotext(char *text, int top, int bottom, int left, int right,
  122. int background)
  123. {
  124. R_standard_color(background);
  125. R_box_abs(left + 1, top + 1, right - 1, bottom - 1);
  126. R_standard_color(BLACK);
  127. /* center the text */
  128. left = (left + right - Text_width(text)) / 2;
  129. Text(text, top, bottom, left, right, 2);
  130. R_flush();
  131. return 0;
  132. }
  133. static int inbox(struct box *box, int x, int y)
  134. {
  135. return (x > box->left && x < box->right && y > box->top &&
  136. y < box->bottom);
  137. }