buttonbar.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2021
  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, 2010, 2013, 2016
  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. /*** file scope variables ************************************************************************/
  41. /*** file scope functions ************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. /* calculate positions of buttons; width is never less than 7 */
  44. static void
  45. buttonbar_init_button_positions (WButtonBar * bb)
  46. {
  47. int i;
  48. int pos = 0;
  49. if (COLS < BUTTONBAR_LABELS_NUM * 7)
  50. {
  51. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  52. {
  53. if (pos + 7 <= COLS)
  54. pos += 7;
  55. bb->labels[i].end_coord = pos;
  56. }
  57. }
  58. else
  59. {
  60. /* Distribute the extra width in a way that the middle vertical line
  61. (between F5 and F6) aligns with the two panels. The extra width
  62. is distributed in this order: F10, F5, F9, F4, ..., F6, F1. */
  63. int dv, md;
  64. dv = COLS / BUTTONBAR_LABELS_NUM;
  65. md = COLS % BUTTONBAR_LABELS_NUM;
  66. for (i = 0; i < BUTTONBAR_LABELS_NUM / 2; i++)
  67. {
  68. pos += dv;
  69. if (BUTTONBAR_LABELS_NUM / 2 - 1 - i < md / 2)
  70. pos++;
  71. bb->labels[i].end_coord = pos;
  72. }
  73. for (; i < BUTTONBAR_LABELS_NUM; i++)
  74. {
  75. pos += dv;
  76. if (BUTTONBAR_LABELS_NUM - 1 - i < (md + 1) / 2)
  77. pos++;
  78. bb->labels[i].end_coord = pos;
  79. }
  80. }
  81. }
  82. /* --------------------------------------------------------------------------------------------- */
  83. /* return width of one button */
  84. static int
  85. buttonbar_get_button_width (const WButtonBar * bb, int i)
  86. {
  87. if (i == 0)
  88. return bb->labels[0].end_coord;
  89. return bb->labels[i].end_coord - bb->labels[i - 1].end_coord;
  90. }
  91. /* --------------------------------------------------------------------------------------------- */
  92. static int
  93. buttonbar_get_button_by_x_coord (const WButtonBar * bb, int x)
  94. {
  95. int i;
  96. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  97. if (bb->labels[i].end_coord > x)
  98. return i;
  99. return (-1);
  100. }
  101. /* --------------------------------------------------------------------------------------------- */
  102. static void
  103. set_label_text (WButtonBar * bb, int idx, const char *text)
  104. {
  105. g_free (bb->labels[idx - 1].text);
  106. bb->labels[idx - 1].text = g_strdup (text);
  107. }
  108. /* --------------------------------------------------------------------------------------------- */
  109. /* returns TRUE if a function has been called, FALSE otherwise. */
  110. static gboolean
  111. buttonbar_call (WButtonBar * bb, int i)
  112. {
  113. cb_ret_t ret = MSG_NOT_HANDLED;
  114. Widget *w = WIDGET (bb);
  115. Widget *target;
  116. if ((bb != NULL) && (bb->labels[i].command != CK_IgnoreKey))
  117. {
  118. target = (bb->labels[i].receiver != NULL) ? bb->labels[i].receiver : WIDGET (w->owner);
  119. ret = send_message (target, w, MSG_ACTION, bb->labels[i].command, NULL);
  120. }
  121. return ret;
  122. }
  123. /* --------------------------------------------------------------------------------------------- */
  124. static cb_ret_t
  125. buttonbar_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  126. {
  127. WButtonBar *bb = BUTTONBAR (w);
  128. int i;
  129. switch (msg)
  130. {
  131. case MSG_HOTKEY:
  132. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  133. if (parm == KEY_F (i + 1) && buttonbar_call (bb, i))
  134. return MSG_HANDLED;
  135. return MSG_NOT_HANDLED;
  136. case MSG_DRAW:
  137. if (widget_get_state (w, WST_VISIBLE))
  138. {
  139. buttonbar_init_button_positions (bb);
  140. widget_gotoyx (w, 0, 0);
  141. tty_setcolor (DEFAULT_COLOR);
  142. tty_printf ("%-*s", w->cols, "");
  143. widget_gotoyx (w, 0, 0);
  144. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  145. {
  146. int width;
  147. const char *text;
  148. width = buttonbar_get_button_width (bb, i);
  149. if (width <= 0)
  150. break;
  151. tty_setcolor (BUTTONBAR_HOTKEY_COLOR);
  152. tty_printf ("%2d", i + 1);
  153. tty_setcolor (BUTTONBAR_BUTTON_COLOR);
  154. text = (bb->labels[i].text != NULL) ? bb->labels[i].text : "";
  155. tty_print_string (str_fit_to_term (text, width - 2, J_LEFT_FIT));
  156. }
  157. }
  158. return MSG_HANDLED;
  159. case MSG_DESTROY:
  160. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  161. g_free (bb->labels[i].text);
  162. return MSG_HANDLED;
  163. default:
  164. return widget_default_callback (w, sender, msg, parm, data);
  165. }
  166. }
  167. /* --------------------------------------------------------------------------------------------- */
  168. static void
  169. buttonbar_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
  170. {
  171. switch (msg)
  172. {
  173. case MSG_MOUSE_CLICK:
  174. {
  175. WButtonBar *bb = BUTTONBAR (w);
  176. int button;
  177. button = buttonbar_get_button_by_x_coord (bb, event->x);
  178. if (button >= 0)
  179. buttonbar_call (bb, button);
  180. break;
  181. }
  182. default:
  183. break;
  184. }
  185. }
  186. /* --------------------------------------------------------------------------------------------- */
  187. /*** public functions ****************************************************************************/
  188. /* --------------------------------------------------------------------------------------------- */
  189. WButtonBar *
  190. buttonbar_new (void)
  191. {
  192. WButtonBar *bb;
  193. Widget *w;
  194. bb = g_new0 (WButtonBar, 1);
  195. w = WIDGET (bb);
  196. widget_init (w, LINES - 1, 0, 1, COLS, buttonbar_callback, buttonbar_mouse_callback);
  197. w->pos_flags = WPOS_KEEP_HORZ | WPOS_KEEP_BOTTOM;
  198. widget_want_hotkey (w, TRUE);
  199. return bb;
  200. }
  201. /* --------------------------------------------------------------------------------------------- */
  202. void
  203. buttonbar_set_label (WButtonBar * bb, int idx, const char *text, const global_keymap_t * keymap,
  204. Widget * receiver)
  205. {
  206. if ((bb != NULL) && (idx >= 1) && (idx <= BUTTONBAR_LABELS_NUM))
  207. {
  208. long command = CK_IgnoreKey;
  209. if (keymap != NULL)
  210. command = keybind_lookup_keymap_command (keymap, KEY_F (idx));
  211. if ((text == NULL) || (text[0] == '\0'))
  212. set_label_text (bb, idx, "");
  213. else
  214. set_label_text (bb, idx, text);
  215. bb->labels[idx - 1].command = command;
  216. bb->labels[idx - 1].receiver = WIDGET (receiver);
  217. }
  218. }
  219. /* --------------------------------------------------------------------------------------------- */
  220. /* Find ButtonBar widget in the dialog */
  221. WButtonBar *
  222. find_buttonbar (const WDialog * h)
  223. {
  224. return BUTTONBAR (widget_find_by_type (CONST_WIDGET (h), buttonbar_callback));
  225. }