buttonbar.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
  4. 2004, 2005, 2006, 2007, 2009, 2010, 2011
  5. The Free Software Foundation, Inc.
  6. Authors:
  7. Radek Doulik, 1994, 1995
  8. Miguel de Icaza, 1994, 1995
  9. Jakub Jelinek, 1995
  10. Andrej Borsenkow, 1996
  11. Norbert Warmuth, 1997
  12. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
  13. This file is part of the Midnight Commander.
  14. The Midnight Commander is free software: you can redistribute it
  15. and/or modify it under the terms of the GNU General Public License as
  16. published by the Free Software Foundation, either version 3 of the License,
  17. or (at your option) any later version.
  18. The Midnight Commander is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /** \file buttonbar.c
  26. * \brief Source: WButtonBar widget
  27. */
  28. #include <config.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "lib/global.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)
  115. {
  116. cb_ret_t ret = MSG_NOT_HANDLED;
  117. if ((bb != NULL) && (bb->labels[i].command != CK_IgnoreKey))
  118. ret = bb->widget.owner->callback (bb->widget.owner,
  119. (Widget *) bb, DLG_ACTION,
  120. bb->labels[i].command, bb->labels[i].receiver);
  121. return ret;
  122. }
  123. /* --------------------------------------------------------------------------------------------- */
  124. static cb_ret_t
  125. buttonbar_callback (Widget * w, widget_msg_t msg, int parm)
  126. {
  127. WButtonBar *bb = (WButtonBar *) w;
  128. int i;
  129. const char *text;
  130. switch (msg)
  131. {
  132. case WIDGET_FOCUS:
  133. return MSG_NOT_HANDLED;
  134. case WIDGET_HOTKEY:
  135. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  136. if (parm == KEY_F (i + 1) && buttonbar_call (bb, i))
  137. return MSG_HANDLED;
  138. return MSG_NOT_HANDLED;
  139. case WIDGET_DRAW:
  140. if (bb->visible)
  141. {
  142. buttonbar_init_button_positions (bb);
  143. widget_move (&bb->widget, 0, 0);
  144. tty_setcolor (DEFAULT_COLOR);
  145. tty_printf ("%-*s", bb->widget.cols, "");
  146. widget_move (&bb->widget, 0, 0);
  147. for (i = 0; i < BUTTONBAR_LABELS_NUM; i++)
  148. {
  149. int width;
  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 WIDGET_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 default_proc (msg, parm);
  167. }
  168. }
  169. /* --------------------------------------------------------------------------------------------- */
  170. static int
  171. buttonbar_event (Gpm_Event * event, void *data)
  172. {
  173. WButtonBar *bb = data;
  174. int button;
  175. if (!(event->type & GPM_UP))
  176. return MOU_NORMAL;
  177. if (event->y == 2)
  178. return MOU_NORMAL;
  179. button = buttonbar_get_button_by_x_coord (bb, event->x - 1);
  180. if (button >= 0)
  181. buttonbar_call (bb, button);
  182. return MOU_NORMAL;
  183. }
  184. /* --------------------------------------------------------------------------------------------- */
  185. /*** public functions ****************************************************************************/
  186. /* --------------------------------------------------------------------------------------------- */
  187. WButtonBar *
  188. buttonbar_new (gboolean visible)
  189. {
  190. WButtonBar *bb;
  191. bb = g_new0 (WButtonBar, 1);
  192. init_widget (&bb->widget, LINES - 1, 0, 1, COLS, buttonbar_callback, buttonbar_event);
  193. bb->widget.pos_flags = WPOS_KEEP_HORZ | WPOS_KEEP_BOTTOM;
  194. bb->visible = visible;
  195. widget_want_hotkey (bb->widget, 1);
  196. widget_want_cursor (bb->widget, 0);
  197. return bb;
  198. }
  199. /* --------------------------------------------------------------------------------------------- */
  200. void
  201. buttonbar_set_label (WButtonBar * bb, int idx, const char *text,
  202. const struct global_keymap_t *keymap, const Widget * receiver)
  203. {
  204. if ((bb != NULL) && (idx >= 1) && (idx <= BUTTONBAR_LABELS_NUM))
  205. {
  206. unsigned long command = CK_IgnoreKey;
  207. if (keymap != NULL)
  208. command = keybind_lookup_keymap_command (keymap, KEY_F (idx));
  209. if ((text == NULL) || (text[0] == '\0'))
  210. set_label_text (bb, idx, "");
  211. else
  212. set_label_text (bb, idx, text);
  213. bb->labels[idx - 1].command = command;
  214. bb->labels[idx - 1].receiver = (Widget *) receiver;
  215. }
  216. }
  217. /* --------------------------------------------------------------------------------------------- */
  218. /* Find ButtonBar widget in the dialog */
  219. WButtonBar *
  220. find_buttonbar (const Dlg_head * h)
  221. {
  222. return (WButtonBar *) find_widget_type (h, buttonbar_callback);
  223. }