frame.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 2020-2024
  4. The Free Software Foundation, Inc.
  5. Authors:
  6. Andrew Borodin <aborodin@vmail.ru>, 2020-2022
  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. /*** forward declarations (file scope functions) *************************************************/
  35. /*** file scope variables ************************************************************************/
  36. /* --------------------------------------------------------------------------------------------- */
  37. /*** file scope functions ************************************************************************/
  38. /* --------------------------------------------------------------------------------------------- */
  39. static void
  40. frame_adjust (WFrame *f)
  41. {
  42. Widget *w = WIDGET (f);
  43. w->rect = WIDGET (w->owner)->rect;
  44. w->pos_flags |= WPOS_KEEP_ALL;
  45. }
  46. /* --------------------------------------------------------------------------------------------- */
  47. static void
  48. frame_draw (const WFrame *f)
  49. {
  50. const Widget *wf = CONST_WIDGET (f);
  51. const WRect *w = &wf->rect;
  52. int d = f->compact ? 0 : 1;
  53. const int *colors;
  54. colors = widget_get_colors (wf);
  55. if (mc_global.tty.shadows)
  56. tty_draw_box_shadow (w->y, w->x, w->lines, w->cols, SHADOW_COLOR);
  57. tty_setcolor (colors[FRAME_COLOR_NORMAL]);
  58. tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
  59. tty_draw_box (w->y + d, w->x + d, w->lines - 2 * d, w->cols - 2 * d, f->single);
  60. if (f->title != NULL)
  61. {
  62. /* TODO: truncate long title */
  63. tty_setcolor (colors[FRAME_COLOR_TITLE]);
  64. widget_gotoyx (f, d, (w->cols - str_term_width1 (f->title)) / 2);
  65. tty_print_string (f->title);
  66. }
  67. }
  68. /* --------------------------------------------------------------------------------------------- */
  69. /*** public functions ****************************************************************************/
  70. /* --------------------------------------------------------------------------------------------- */
  71. WFrame *
  72. frame_new (int y, int x, int lines, int cols, const char *title, gboolean single, gboolean compact)
  73. {
  74. WRect r = { y, x, lines, cols };
  75. WFrame *f;
  76. Widget *w;
  77. f = g_new (WFrame, 1);
  78. w = WIDGET (f);
  79. widget_init (w, &r, frame_callback, NULL);
  80. f->single = single;
  81. f->compact = compact;
  82. f->title = NULL;
  83. frame_set_title (f, title);
  84. return f;
  85. }
  86. /* --------------------------------------------------------------------------------------------- */
  87. cb_ret_t
  88. frame_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
  89. {
  90. WFrame *f = FRAME (w);
  91. switch (msg)
  92. {
  93. case MSG_INIT:
  94. frame_adjust (f);
  95. return MSG_HANDLED;
  96. case MSG_DRAW:
  97. frame_draw (f);
  98. return MSG_HANDLED;
  99. case MSG_DESTROY:
  100. g_free (f->title);
  101. return MSG_HANDLED;
  102. default:
  103. return widget_default_callback (w, sender, msg, parm, data);
  104. }
  105. }
  106. /* --------------------------------------------------------------------------------------------- */
  107. void
  108. frame_set_title (WFrame *f, const char *title)
  109. {
  110. MC_PTR_FREE (f->title);
  111. /* Strip existing spaces, add one space before and after the title */
  112. if (title != NULL && *title != '\0')
  113. {
  114. char *t;
  115. t = g_strstrip (g_strdup (title));
  116. if (*t != '\0')
  117. f->title = g_strdup_printf (" %s ", t);
  118. g_free (t);
  119. }
  120. widget_draw (WIDGET (f));
  121. }
  122. /* --------------------------------------------------------------------------------------------- */