hline.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 hline.c
  25. * \brief Source: WHLine widget (horizontal line)
  26. */
  27. #include <config.h>
  28. #include <stdlib.h>
  29. #include "lib/global.h"
  30. #include "lib/tty/tty.h"
  31. #include "lib/tty/color.h"
  32. #include "lib/skin.h"
  33. #include "lib/strutil.h"
  34. #include "lib/widget.h"
  35. /*** global variables ****************************************************************************/
  36. /*** file scope macro definitions ****************************************************************/
  37. /*** file scope type declarations ****************************************************************/
  38. /*** file scope variables ************************************************************************/
  39. /*** file scope functions ************************************************************************/
  40. static cb_ret_t
  41. hline_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  42. {
  43. WHLine *l = HLINE (w);
  44. WDialog *h = w->owner;
  45. switch (msg)
  46. {
  47. case MSG_INIT:
  48. case MSG_RESIZE:
  49. if (l->auto_adjust_cols)
  50. {
  51. Widget *wo = WIDGET (h);
  52. if (((h->flags & DLG_COMPACT) != 0))
  53. {
  54. w->x = wo->x;
  55. w->cols = wo->cols;
  56. }
  57. else
  58. {
  59. w->x = wo->x + 1;
  60. w->cols = wo->cols - 2;
  61. }
  62. }
  63. return MSG_HANDLED;
  64. case MSG_FOCUS:
  65. /* We don't want to get the focus */
  66. return MSG_NOT_HANDLED;
  67. case MSG_DRAW:
  68. if (l->transparent)
  69. tty_setcolor (DEFAULT_COLOR);
  70. else
  71. tty_setcolor (h->color[DLG_COLOR_NORMAL]);
  72. tty_draw_hline (w->y, w->x + 1, ACS_HLINE, w->cols - 2);
  73. if (l->auto_adjust_cols)
  74. {
  75. widget_move (w, 0, 0);
  76. tty_print_alt_char (ACS_LTEE, FALSE);
  77. widget_move (w, 0, w->cols - 1);
  78. tty_print_alt_char (ACS_RTEE, FALSE);
  79. }
  80. if (l->text != NULL)
  81. {
  82. int text_width;
  83. text_width = str_term_width1 (l->text);
  84. widget_move (w, 0, (w->cols - text_width) / 2);
  85. tty_print_string (l->text);
  86. }
  87. return MSG_HANDLED;
  88. default:
  89. return widget_default_callback (w, sender, msg, parm, data);
  90. }
  91. }
  92. /* --------------------------------------------------------------------------------------------- */
  93. /*** public functions ****************************************************************************/
  94. /* --------------------------------------------------------------------------------------------- */
  95. WHLine *
  96. hline_new (int y, int x, int width)
  97. {
  98. WHLine *l;
  99. Widget *w;
  100. int lines = 1;
  101. l = g_new (WHLine, 1);
  102. w = WIDGET (l);
  103. widget_init (w, y, x, lines, width < 0 ? 1 : width, hline_callback, NULL);
  104. l->text = NULL;
  105. l->auto_adjust_cols = (width < 0);
  106. l->transparent = FALSE;
  107. widget_want_cursor (w, FALSE);
  108. widget_want_hotkey (w, FALSE);
  109. return l;
  110. }
  111. /* --------------------------------------------------------------------------------------------- */
  112. void
  113. hline_set_text (WHLine * l, const char *text)
  114. {
  115. g_free (l->text);
  116. if (text == NULL || *text == '\0')
  117. l->text = NULL;
  118. else
  119. l->text = g_strdup (text);
  120. widget_redraw (WIDGET (l));
  121. }
  122. /* --------------------------------------------------------------------------------------------- */