buttonbar.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2014
  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
  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/mouse.h"
  33. #include "lib/tty/key.h" /* XCTRL and ALT macros */
  34. #include "lib/skin.h"
  35. #include "lib/strutil.h"
  36. #include "lib/util.h"
  37. #include "lib/keybind.h" /* global_keymap_t */
  38. #include "lib/widget.h"
  39. /*** global variables ****************************************************************************/
  40. /*** file scope macro definitions ****************************************************************/
  41. /*** file scope type declarations ****************************************************************/
  42. /*** file scope variables ************************************************************************/
  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. if ((bb != NULL) && (bb->labels[i].command != CK_IgnoreKey))
  118. ret = send_message (w->owner, w, MSG_ACTION, bb->labels[i].command, bb->labels[i].receiver);
  119. return ret;
  120. }
  121. /* --------------------------------------------------------------------------------------------- */
  122. static cb_ret_t
  123. buttonbar_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  124. {
  125. WButtonBar *bb = BUTTONBAR (w);
  126. int i;
  127. switch (msg)
  128. {
  129. case MSG_FOCUS:
  130. return MSG_NOT_HANDLED;
  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 (bb->visible)
  138. {
  139. buttonbar_init_button_positions (bb);
  140. widget_move (w, 0, 0);
  141. tty_setcolor (DEFAULT_COLOR);
  142. tty_printf ("%-*s", w->cols, "");
  143. widget_move (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 int
  169. buttonbar_event (Gpm_Event * event, void *data)
  170. {
  171. Widget *w = WIDGET (data);
  172. if (!mouse_global_in_widget (event, w))
  173. return MOU_UNHANDLED;
  174. if ((event->type & GPM_UP) != 0)
  175. {
  176. WButtonBar *bb = BUTTONBAR (data);
  177. Gpm_Event local;
  178. int button;
  179. local = mouse_get_local (event, w);
  180. button = buttonbar_get_button_by_x_coord (bb, local.x - 1);
  181. if (button >= 0)
  182. buttonbar_call (bb, button);
  183. }
  184. return MOU_NORMAL;
  185. }
  186. /* --------------------------------------------------------------------------------------------- */
  187. /*** public functions ****************************************************************************/
  188. /* --------------------------------------------------------------------------------------------- */
  189. WButtonBar *
  190. buttonbar_new (gboolean visible)
  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_event);
  197. w->pos_flags = WPOS_KEEP_HORZ | WPOS_KEEP_BOTTOM;
  198. bb->visible = visible;
  199. widget_want_hotkey (w, TRUE);
  200. widget_want_cursor (w, FALSE);
  201. return bb;
  202. }
  203. /* --------------------------------------------------------------------------------------------- */
  204. void
  205. buttonbar_set_label (WButtonBar * bb, int idx, const char *text,
  206. const struct global_keymap_t *keymap, const Widget * receiver)
  207. {
  208. if ((bb != NULL) && (idx >= 1) && (idx <= BUTTONBAR_LABELS_NUM))
  209. {
  210. unsigned 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. }