buttonbar.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2015
  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/keymap.h"
  32. #include "lib/tty/tty.h"
  33. #include "lib/tty/mouse.h"
  34. #include "lib/tty/key.h" /* XCTRL and ALT macros */
  35. #include "lib/skin.h"
  36. #include "lib/strutil.h"
  37. #include "lib/util.h"
  38. #include "lib/keybind.h" /* global_keymap_t */
  39. #include "lib/widget.h"
  40. /*** global variables ****************************************************************************/
  41. /*** file scope macro definitions ****************************************************************/
  42. /*** file scope type declarations ****************************************************************/
  43. /*** file scope variables ************************************************************************/
  44. /*** file scope functions ************************************************************************/
  45. /* --------------------------------------------------------------------------------------------- */
  46. /* calculate positions of buttons; width is never less than 7 */
  47. static void
  48. buttonbar_init_button_positions (WButtonBar * bb)
  49. {
  50. int i;
  51. int pos = 0;
  52. if (COLS < BUTTONBAR_LABELS_NUM * 7)
  53. {
  54. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  55. {
  56. if (pos + 7 <= COLS)
  57. pos += 7;
  58. bb->labels[i].end_coord = pos;
  59. }
  60. }
  61. else
  62. {
  63. /* Distribute the extra width in a way that the middle vertical line
  64. (between F5 and F6) aligns with the two panels. The extra width
  65. is distributed in this order: F10, F5, F9, F4, ..., F6, F1. */
  66. int dv, md;
  67. dv = COLS / BUTTONBAR_LABELS_NUM;
  68. md = COLS % BUTTONBAR_LABELS_NUM;
  69. for (i = 0; i < BUTTONBAR_LABELS_NUM / 2; i++)
  70. {
  71. pos += dv;
  72. if (BUTTONBAR_LABELS_NUM / 2 - 1 - i < md / 2)
  73. pos++;
  74. bb->labels[i].end_coord = pos;
  75. }
  76. for (; i < BUTTONBAR_LABELS_NUM; i++)
  77. {
  78. pos += dv;
  79. if (BUTTONBAR_LABELS_NUM - 1 - i < (md + 1) / 2)
  80. pos++;
  81. bb->labels[i].end_coord = pos;
  82. }
  83. }
  84. }
  85. /* --------------------------------------------------------------------------------------------- */
  86. /* return width of one button */
  87. static int
  88. buttonbar_get_button_width (const WButtonBar * bb, int i)
  89. {
  90. if (i == 0)
  91. return bb->labels[0].end_coord;
  92. return bb->labels[i].end_coord - bb->labels[i - 1].end_coord;
  93. }
  94. /* --------------------------------------------------------------------------------------------- */
  95. static int
  96. buttonbar_get_button_by_x_coord (const WButtonBar * bb, int x)
  97. {
  98. int i;
  99. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  100. if (bb->labels[i].end_coord > x)
  101. return i;
  102. return (-1);
  103. }
  104. /* --------------------------------------------------------------------------------------------- */
  105. static void
  106. set_label_text (WButtonBar * bb, int idx, const char *text)
  107. {
  108. g_free (bb->labels[idx - 1].text);
  109. bb->labels[idx - 1].text = g_strdup (text);
  110. }
  111. /* --------------------------------------------------------------------------------------------- */
  112. /* returns TRUE if a function has been called, FALSE otherwise. */
  113. static gboolean
  114. buttonbar_call (WButtonBar * bb, int i, GError ** error)
  115. {
  116. cb_ret_t ret = MSG_NOT_HANDLED;
  117. if (bb->use_keymap)
  118. {
  119. if (bb->labels[i].keymap_section != NULL)
  120. {
  121. event_return_t event_ret;
  122. event_ret.b = TRUE;
  123. if (mc_keymap_process_group
  124. (bb->labels[i].keymap_section, bb->labels[i].command,
  125. (void *) bb->labels[i].receiver, &event_ret, error))
  126. ret = (event_ret.b) ? MSG_HANDLED : MSG_NOT_HANDLED;
  127. }
  128. }
  129. else
  130. {
  131. if ((bb != NULL) && (bb->labels[i].command != CK_IgnoreKey))
  132. {
  133. Widget *w = WIDGET (bb);
  134. ret =
  135. send_message (w->owner, w, MSG_ACTION, bb->labels[i].command,
  136. bb->labels[i].receiver);
  137. }
  138. }
  139. return ret;
  140. }
  141. /* --------------------------------------------------------------------------------------------- */
  142. static cb_ret_t
  143. buttonbar_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  144. {
  145. WButtonBar *bb = BUTTONBAR (w);
  146. int i;
  147. switch (msg)
  148. {
  149. case MSG_FOCUS:
  150. return MSG_NOT_HANDLED;
  151. case MSG_HOTKEY:
  152. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  153. if (parm == KEY_F (i + 1) && buttonbar_call (bb, i, NULL))
  154. return MSG_HANDLED;
  155. return MSG_NOT_HANDLED;
  156. case MSG_DRAW:
  157. if (bb->visible)
  158. {
  159. buttonbar_init_button_positions (bb);
  160. widget_move (w, 0, 0);
  161. tty_setcolor (DEFAULT_COLOR);
  162. tty_printf ("%-*s", w->cols, "");
  163. widget_move (w, 0, 0);
  164. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  165. {
  166. int width;
  167. const char *text;
  168. width = buttonbar_get_button_width (bb, i);
  169. if (width <= 0)
  170. break;
  171. tty_setcolor (BUTTONBAR_HOTKEY_COLOR);
  172. tty_printf ("%2d", i + 1);
  173. tty_setcolor (BUTTONBAR_BUTTON_COLOR);
  174. text = (bb->labels[i].text != NULL) ? bb->labels[i].text : "";
  175. tty_print_string (str_fit_to_term (text, width - 2, J_LEFT_FIT));
  176. }
  177. }
  178. return MSG_HANDLED;
  179. case MSG_DESTROY:
  180. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  181. g_free (bb->labels[i].text);
  182. return MSG_HANDLED;
  183. default:
  184. return widget_default_callback (w, sender, msg, parm, data);
  185. }
  186. }
  187. /* --------------------------------------------------------------------------------------------- */
  188. static int
  189. buttonbar_event (Gpm_Event * event, void *data)
  190. {
  191. Widget *w = WIDGET (data);
  192. if (!mouse_global_in_widget (event, w))
  193. return MOU_UNHANDLED;
  194. if ((event->type & GPM_UP) != 0)
  195. {
  196. WButtonBar *bb = BUTTONBAR (data);
  197. Gpm_Event local;
  198. int button;
  199. local = mouse_get_local (event, w);
  200. button = buttonbar_get_button_by_x_coord (bb, local.x - 1);
  201. if (button >= 0)
  202. buttonbar_call (bb, button, NULL);
  203. }
  204. return MOU_NORMAL;
  205. }
  206. /* --------------------------------------------------------------------------------------------- */
  207. /*** public functions ****************************************************************************/
  208. /* --------------------------------------------------------------------------------------------- */
  209. WButtonBar *
  210. buttonbar_new (gboolean visible)
  211. {
  212. WButtonBar *bb;
  213. Widget *w;
  214. bb = g_new0 (WButtonBar, 1);
  215. w = WIDGET (bb);
  216. widget_init (w, LINES - 1, 0, 1, COLS, buttonbar_callback, buttonbar_event);
  217. w->pos_flags = WPOS_KEEP_HORZ | WPOS_KEEP_BOTTOM;
  218. bb->visible = visible;
  219. bb->use_keymap = FALSE;
  220. widget_want_hotkey (w, TRUE);
  221. widget_want_cursor (w, FALSE);
  222. return bb;
  223. }
  224. /* --------------------------------------------------------------------------------------------- */
  225. /**
  226. * Init one button in buttonbar. use keymap implementation.
  227. *
  228. * @param bb the ButtonBar object
  229. * @param idx the buton index
  230. * @param text the label of button
  231. * @param keymap_section the keymap section for searching event
  232. * @param receiver the widget
  233. */
  234. void
  235. buttonbar_init_button (WButtonBar * bb, int idx, const char *text,
  236. const char *keymap_section, const Widget * w)
  237. {
  238. if ((bb != NULL) && (idx >= 1) && (idx <= BUTTONBAR_LABELS_NUM))
  239. {
  240. bb->use_keymap = TRUE;
  241. bb->labels[idx - 1].command = KEY_F (idx);
  242. bb->labels[idx - 1].receiver = WIDGET (w);
  243. bb->labels[idx - 1].keymap_section = keymap_section;
  244. if ((text == NULL) || (text[0] == '\0'))
  245. set_label_text (bb, idx, "");
  246. else
  247. set_label_text (bb, idx, text);
  248. }
  249. }
  250. /* --------------------------------------------------------------------------------------------- */
  251. void
  252. buttonbar_set_label (WButtonBar * bb, int idx, const char *text,
  253. const struct global_keymap_t *keymap, const Widget * receiver)
  254. {
  255. if ((bb != NULL) && (idx >= 1) && (idx <= BUTTONBAR_LABELS_NUM))
  256. {
  257. unsigned long command = CK_IgnoreKey;
  258. if (keymap != NULL)
  259. command = keybind_lookup_keymap_command (keymap, KEY_F (idx));
  260. if ((text == NULL) || (text[0] == '\0'))
  261. set_label_text (bb, idx, "");
  262. else
  263. set_label_text (bb, idx, text);
  264. bb->labels[idx - 1].command = command;
  265. bb->labels[idx - 1].receiver = WIDGET (receiver);
  266. }
  267. }
  268. /* --------------------------------------------------------------------------------------------- */
  269. /* Find ButtonBar widget in the dialog */
  270. WButtonBar *
  271. find_buttonbar (const WDialog * h)
  272. {
  273. return BUTTONBAR (find_widget_type (h, buttonbar_callback));
  274. }