frame.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 2020-2021
  4. The Free Software Foundation, Inc.
  5. Authors:
  6. Andrew Borodin <aborodin@vmail.ru>, 2020
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file frame.c
  20. * \brief Source: WFrame widget (frame of dialogs)
  21. */
  22. #include <config.h>
  23. #include <stdlib.h>
  24. #include "lib/global.h"
  25. #include "lib/tty/tty.h"
  26. #include "lib/tty/color.h"
  27. #include "lib/skin.h"
  28. #include "lib/strutil.h"
  29. #include "lib/util.h" /* MC_PTR_FREE */
  30. #include "lib/widget.h"
  31. /*** global variables ****************************************************************************/
  32. /*** file scope macro definitions ****************************************************************/
  33. /*** file scope type declarations ****************************************************************/
  34. /*** file scope variables ************************************************************************/
  35. /* --------------------------------------------------------------------------------------------- */
  36. /*** file scope functions ************************************************************************/
  37. /* --------------------------------------------------------------------------------------------- */
  38. static void
  39. frame_adjust (WFrame * f)
  40. {
  41. Widget *w = WIDGET (f);
  42. Widget *wo = WIDGET (w->owner);
  43. w->y = wo->y;
  44. w->x = wo->x;
  45. w->lines = wo->lines;
  46. w->cols = wo->cols;
  47. w->pos_flags |= WPOS_KEEP_ALL;
  48. }
  49. /* --------------------------------------------------------------------------------------------- */
  50. static void
  51. frame_draw (const WFrame * f)
  52. {
  53. const Widget *w = CONST_WIDGET (f);
  54. int d = f->compact ? 0 : 1;
  55. const int *colors;
  56. colors = widget_get_colors (w);
  57. if (mc_global.tty.shadows)
  58. tty_draw_box_shadow (w->y, w->x, w->lines, w->cols, SHADOW_COLOR);
  59. tty_setcolor (colors[FRAME_COLOR_NORMAL]);
  60. tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
  61. tty_draw_box (w->y + d, w->x + d, w->lines - 2 * d, w->cols - 2 * d, f->single);
  62. if (f->title != NULL)
  63. {
  64. /* TODO: truncate long title */
  65. tty_setcolor (colors[FRAME_COLOR_TITLE]);
  66. widget_gotoyx (w, d, (w->cols - str_term_width1 (f->title)) / 2);
  67. tty_print_string (f->title);
  68. }
  69. }
  70. /* --------------------------------------------------------------------------------------------- */
  71. /*** public functions ****************************************************************************/
  72. /* --------------------------------------------------------------------------------------------- */
  73. WFrame *
  74. frame_new (int y, int x, int lines, int cols, const char *title, gboolean single, gboolean compact)
  75. {
  76. WFrame *f;
  77. Widget *w;
  78. f = g_new (WFrame, 1);
  79. w = WIDGET (f);
  80. widget_init (w, y, x, lines, cols, frame_callback, NULL);
  81. f->single = single;
  82. f->compact = compact;
  83. f->title = NULL;
  84. frame_set_title (f, title);
  85. return f;
  86. }
  87. /* --------------------------------------------------------------------------------------------- */
  88. cb_ret_t
  89. frame_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  90. {
  91. WFrame *f = FRAME (w);
  92. switch (msg)
  93. {
  94. case MSG_INIT:
  95. frame_adjust (f);
  96. return MSG_HANDLED;
  97. case MSG_DRAW:
  98. frame_draw (f);
  99. return MSG_HANDLED;
  100. case MSG_DESTROY:
  101. g_free (f->title);
  102. return MSG_HANDLED;
  103. default:
  104. return widget_default_callback (w, sender, msg, parm, data);
  105. }
  106. }
  107. /* --------------------------------------------------------------------------------------------- */
  108. void
  109. frame_set_title (WFrame * f, const char *title)
  110. {
  111. MC_PTR_FREE (f->title);
  112. /* Strip existing spaces, add one space before and after the title */
  113. if (title != NULL && *title != '\0')
  114. {
  115. char *t;
  116. t = g_strstrip (g_strdup (title));
  117. if (*t != '\0')
  118. f->title = g_strdup_printf (" %s ", t);
  119. g_free (t);
  120. }
  121. widget_draw (WIDGET (f));
  122. }
  123. /* --------------------------------------------------------------------------------------------- */