buttonbar.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2024
  4. Free Software Foundation, Inc.
  5. Authors:
  6. Radek Doulik, 1994, 1995
  7. Miguel de Icaza, 1994, 1995
  8. Jakub Jelinek, 1995
  9. Andrej Borsenkow, 1996
  10. Norbert Warmuth, 1997
  11. Andrew Borodin <aborodin@vmail.ru>, 2009-2022
  12. This file is part of the Midnight Commander.
  13. The Midnight Commander is free software: you can redistribute it
  14. and/or modify it under the terms of the GNU General Public License as
  15. published by the Free Software Foundation, either version 3 of the License,
  16. or (at your option) any later version.
  17. The Midnight Commander is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /** \file buttonbar.c
  25. * \brief Source: WButtonBar widget
  26. */
  27. #include <config.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "lib/global.h"
  31. #include "lib/tty/tty.h"
  32. #include "lib/tty/key.h" /* XCTRL and ALT macros */
  33. #include "lib/skin.h"
  34. #include "lib/strutil.h"
  35. #include "lib/util.h"
  36. #include "lib/widget.h"
  37. /*** global variables ****************************************************************************/
  38. /*** file scope macro definitions ****************************************************************/
  39. /*** file scope type declarations ****************************************************************/
  40. /*** forward declarations (file scope functions) *************************************************/
  41. /*** file scope variables ************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. /*** file scope functions ************************************************************************/
  44. /* --------------------------------------------------------------------------------------------- */
  45. /* calculate positions of buttons; width is never less than 7 */
  46. static void
  47. buttonbar_init_button_positions (WButtonBar *bb)
  48. {
  49. int i;
  50. int pos = 0;
  51. if (COLS < BUTTONBAR_LABELS_NUM * 7)
  52. {
  53. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  54. {
  55. if (pos + 7 <= COLS)
  56. pos += 7;
  57. bb->labels[i].end_coord = pos;
  58. }
  59. }
  60. else
  61. {
  62. /* Distribute the extra width in a way that the middle vertical line
  63. (between F5 and F6) aligns with the two panels. The extra width
  64. is distributed in this order: F10, F5, F9, F4, ..., F6, F1. */
  65. int dv, md;
  66. dv = COLS / BUTTONBAR_LABELS_NUM;
  67. md = COLS % BUTTONBAR_LABELS_NUM;
  68. for (i = 0; i < BUTTONBAR_LABELS_NUM / 2; i++)
  69. {
  70. pos += dv;
  71. if (BUTTONBAR_LABELS_NUM / 2 - 1 - i < md / 2)
  72. pos++;
  73. bb->labels[i].end_coord = pos;
  74. }
  75. for (; i < BUTTONBAR_LABELS_NUM; i++)
  76. {
  77. pos += dv;
  78. if (BUTTONBAR_LABELS_NUM - 1 - i < (md + 1) / 2)
  79. pos++;
  80. bb->labels[i].end_coord = pos;
  81. }
  82. }
  83. }
  84. /* --------------------------------------------------------------------------------------------- */
  85. /* return width of one button */
  86. static int
  87. buttonbar_get_button_width (const WButtonBar *bb, int i)
  88. {
  89. if (i == 0)
  90. return bb->labels[0].end_coord;
  91. return bb->labels[i].end_coord - bb->labels[i - 1].end_coord;
  92. }
  93. /* --------------------------------------------------------------------------------------------- */
  94. static int
  95. buttonbar_get_button_by_x_coord (const WButtonBar *bb, int x)
  96. {
  97. int i;
  98. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  99. if (bb->labels[i].end_coord > x)
  100. return i;
  101. return (-1);
  102. }
  103. /* --------------------------------------------------------------------------------------------- */
  104. static void
  105. set_label_text (WButtonBar *bb, int idx, const char *text)
  106. {
  107. g_free (bb->labels[idx - 1].text);
  108. bb->labels[idx - 1].text = g_strdup (text);
  109. }
  110. /* --------------------------------------------------------------------------------------------- */
  111. /* returns TRUE if a function has been called, FALSE otherwise. */
  112. static gboolean
  113. buttonbar_call (WButtonBar *bb, int i)
  114. {
  115. cb_ret_t ret = MSG_NOT_HANDLED;
  116. Widget *w = WIDGET (bb);
  117. Widget *target;
  118. if ((bb != NULL) && (bb->labels[i].command != CK_IgnoreKey))
  119. {
  120. target = (bb->labels[i].receiver != NULL) ? bb->labels[i].receiver : WIDGET (w->owner);
  121. ret = send_message (target, w, MSG_ACTION, bb->labels[i].command, NULL);
  122. }
  123. return ret;
  124. }
  125. /* --------------------------------------------------------------------------------------------- */
  126. static cb_ret_t
  127. buttonbar_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
  128. {
  129. WButtonBar *bb = BUTTONBAR (w);
  130. int i;
  131. switch (msg)
  132. {
  133. case MSG_HOTKEY:
  134. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  135. if (parm == KEY_F (i + 1) && buttonbar_call (bb, i))
  136. return MSG_HANDLED;
  137. return MSG_NOT_HANDLED;
  138. case MSG_DRAW:
  139. if (widget_get_state (w, WST_VISIBLE))
  140. {
  141. buttonbar_init_button_positions (bb);
  142. widget_gotoyx (w, 0, 0);
  143. tty_setcolor (DEFAULT_COLOR);
  144. tty_printf ("%-*s", w->rect.cols, "");
  145. widget_gotoyx (w, 0, 0);
  146. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  147. {
  148. int width;
  149. const char *text;
  150. width = buttonbar_get_button_width (bb, i);
  151. if (width <= 0)
  152. break;
  153. tty_setcolor (BUTTONBAR_HOTKEY_COLOR);
  154. tty_printf ("%2d", i + 1);
  155. tty_setcolor (BUTTONBAR_BUTTON_COLOR);
  156. text = (bb->labels[i].text != NULL) ? bb->labels[i].text : "";
  157. tty_print_string (str_fit_to_term (text, width - 2, J_LEFT_FIT));
  158. }
  159. }
  160. return MSG_HANDLED;
  161. case MSG_DESTROY:
  162. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  163. g_free (bb->labels[i].text);
  164. return MSG_HANDLED;
  165. default:
  166. return widget_default_callback (w, sender, msg, parm, data);
  167. }
  168. }
  169. /* --------------------------------------------------------------------------------------------- */
  170. static void
  171. buttonbar_mouse_callback (Widget *w, mouse_msg_t msg, mouse_event_t *event)
  172. {
  173. switch (msg)
  174. {
  175. case MSG_MOUSE_CLICK:
  176. {
  177. WButtonBar *bb = BUTTONBAR (w);
  178. int button;
  179. button = buttonbar_get_button_by_x_coord (bb, event->x);
  180. if (button >= 0)
  181. buttonbar_call (bb, button);
  182. break;
  183. }
  184. default:
  185. break;
  186. }
  187. }
  188. /* --------------------------------------------------------------------------------------------- */
  189. /*** public functions ****************************************************************************/
  190. /* --------------------------------------------------------------------------------------------- */
  191. WButtonBar *
  192. buttonbar_new (void)
  193. {
  194. WRect r = { LINES - 1, 0, 1, COLS };
  195. WButtonBar *bb;
  196. Widget *w;
  197. bb = g_new0 (WButtonBar, 1);
  198. w = WIDGET (bb);
  199. widget_init (w, &r, buttonbar_callback, buttonbar_mouse_callback);
  200. w->pos_flags = WPOS_KEEP_HORZ | WPOS_KEEP_BOTTOM;
  201. w->options |= WOP_WANT_HOTKEY;
  202. return bb;
  203. }
  204. /* --------------------------------------------------------------------------------------------- */
  205. void
  206. buttonbar_set_label (WButtonBar *bb, int idx, const char *text, const global_keymap_t *keymap,
  207. Widget *receiver)
  208. {
  209. if ((bb != NULL) && (idx >= 1) && (idx <= BUTTONBAR_LABELS_NUM))
  210. {
  211. long command = CK_IgnoreKey;
  212. if (keymap != NULL)
  213. command = keybind_lookup_keymap_command (keymap, KEY_F (idx));
  214. if ((text == NULL) || (text[0] == '\0'))
  215. set_label_text (bb, idx, "");
  216. else
  217. set_label_text (bb, idx, text);
  218. bb->labels[idx - 1].command = command;
  219. bb->labels[idx - 1].receiver = WIDGET (receiver);
  220. }
  221. }
  222. /* --------------------------------------------------------------------------------------------- */
  223. /* Find ButtonBar widget in the dialog */
  224. WButtonBar *
  225. buttonbar_find (const WDialog *h)
  226. {
  227. return BUTTONBAR (widget_find_by_type (CONST_WIDGET (h), buttonbar_callback));
  228. }