buttonbar.c 8.3 KB

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