root/lib/widget/frame.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. frame_adjust
  2. frame_draw
  3. frame_new
  4. frame_callback
  5. frame_set_title

   1 /*
   2    Widgets for the Midnight Commander
   3 
   4    Copyright (C) 2020
   5    The Free Software Foundation, Inc.
   6 
   7    Authors:
   8    Andrew Borodin <aborodin@vmail.ru>, 2020
   9 
  10    This file is part of the Midnight Commander.
  11 
  12    The Midnight Commander is free software: you can redistribute it
  13    and/or modify it under the terms of the GNU General Public License as
  14    published by the Free Software Foundation, either version 3 of the License,
  15    or (at your option) any later version.
  16 
  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 
  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 
  26 /** \file frame.c
  27  *  \brief Source: WFrame widget (frame of dialogs)
  28  */
  29 
  30 #include <config.h>
  31 
  32 #include <stdlib.h>
  33 
  34 #include "lib/global.h"
  35 #include "lib/tty/tty.h"
  36 #include "lib/tty/color.h"
  37 #include "lib/skin.h"
  38 #include "lib/strutil.h"
  39 #include "lib/util.h"           /* MC_PTR_FREE */
  40 #include "lib/widget.h"
  41 
  42 /*** global variables ****************************************************************************/
  43 
  44 /*** file scope macro definitions ****************************************************************/
  45 
  46 /*** file scope type declarations ****************************************************************/
  47 
  48 /*** file scope variables ************************************************************************/
  49 
  50 /* --------------------------------------------------------------------------------------------- */
  51 /*** file scope functions ************************************************************************/
  52 /* --------------------------------------------------------------------------------------------- */
  53 
  54 static void
  55 frame_adjust (WFrame * f)
     /* [previous][next][first][last][top][bottom][index][help]  */
  56 {
  57     Widget *w = WIDGET (f);
  58     Widget *wo = WIDGET (w->owner);
  59 
  60     w->y = wo->y;
  61     w->x = wo->x;
  62     w->lines = wo->lines;
  63     w->cols = wo->cols;
  64 
  65     w->pos_flags |= WPOS_KEEP_ALL;
  66 }
  67 
  68 /* --------------------------------------------------------------------------------------------- */
  69 
  70 static void
  71 frame_draw (const WFrame * f)
     /* [previous][next][first][last][top][bottom][index][help]  */
  72 {
  73     const Widget *w = CONST_WIDGET (f);
  74     int d = f->compact ? 0 : 1;
  75     const int *colors;
  76 
  77     colors = widget_get_colors (w);
  78 
  79     if (mc_global.tty.shadows)
  80         tty_draw_box_shadow (w->y, w->x, w->lines, w->cols, SHADOW_COLOR);
  81 
  82     tty_setcolor (colors[FRAME_COLOR_NORMAL]);
  83     tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
  84     tty_draw_box (w->y + d, w->x + d, w->lines - 2 * d, w->cols - 2 * d, f->single);
  85 
  86     if (f->title != NULL)
  87     {
  88         /* TODO: truncate long title */
  89         tty_setcolor (colors[FRAME_COLOR_TITLE]);
  90         widget_gotoyx (w, d, (w->cols - str_term_width1 (f->title)) / 2);
  91         tty_print_string (f->title);
  92     }
  93 }
  94 
  95 /* --------------------------------------------------------------------------------------------- */
  96 /*** public functions ****************************************************************************/
  97 /* --------------------------------------------------------------------------------------------- */
  98 
  99 WFrame *
 100 frame_new (int y, int x, int lines, int cols, const char *title, gboolean single, gboolean compact)
     /* [previous][next][first][last][top][bottom][index][help]  */
 101 {
 102     WFrame *f;
 103     Widget *w;
 104 
 105     f = g_new (WFrame, 1);
 106     w = WIDGET (f);
 107     widget_init (w, y, x, lines, cols, frame_callback, NULL);
 108 
 109     f->single = single;
 110     f->compact = compact;
 111 
 112     f->title = NULL;
 113     frame_set_title (f, title);
 114 
 115     return f;
 116 }
 117 
 118 /* --------------------------------------------------------------------------------------------- */
 119 
 120 cb_ret_t
 121 frame_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 122 {
 123     WFrame *f = FRAME (w);
 124 
 125     switch (msg)
 126     {
 127     case MSG_INIT:
 128         frame_adjust (f);
 129         return MSG_HANDLED;
 130 
 131     case MSG_DRAW:
 132         frame_draw (f);
 133         return MSG_HANDLED;
 134 
 135     case MSG_DESTROY:
 136         g_free (f->title);
 137         return MSG_HANDLED;
 138 
 139     default:
 140         return widget_default_callback (w, sender, msg, parm, data);
 141     }
 142 }
 143 
 144 /* --------------------------------------------------------------------------------------------- */
 145 
 146 void
 147 frame_set_title (WFrame * f, const char *title)
     /* [previous][next][first][last][top][bottom][index][help]  */
 148 {
 149     MC_PTR_FREE (f->title);
 150 
 151     /* Strip existing spaces, add one space before and after the title */
 152     if (title != NULL && *title != '\0')
 153     {
 154         char *t;
 155 
 156         t = g_strstrip (g_strdup (title));
 157         if (*t != '\0')
 158             f->title = g_strdup_printf (" %s ", t);
 159         g_free (t);
 160     }
 161 
 162     widget_draw (WIDGET (f));
 163 }
 164 
 165 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */