1 // Emacs style mode select   -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 2006 Simon Howard
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 // 02111-1307, USA.
20 //
21 
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "txt_separator.h"
26 #include "txt_gui.h"
27 #include "txt_io.h"
28 #include "txt_main.h"
29 #include "txt_window.h"
30 
TXT_SeparatorSizeCalc(TXT_UNCAST_ARG (separator))31 static void TXT_SeparatorSizeCalc(TXT_UNCAST_ARG(separator))
32 {
33     TXT_CAST_ARG(txt_separator_t, separator);
34 
35     if (separator->label != NULL)
36     {
37         // Minimum width is the string length + two spaces for padding
38 
39         separator->widget.w = strlen(separator->label) + 2;
40     }
41     else
42     {
43         separator->widget.w = 0;
44     }
45 
46     separator->widget.h = 1;
47 }
48 
TXT_SeparatorDrawer(TXT_UNCAST_ARG (separator))49 static void TXT_SeparatorDrawer(TXT_UNCAST_ARG(separator))
50 {
51     TXT_CAST_ARG(txt_separator_t, separator);
52     int x, y;
53     int w;
54 
55     w = separator->widget.w;
56 
57     TXT_GetXY(&x, &y);
58 
59     // Draw separator.  Go back one character and draw two extra
60     // to overlap the window borders.
61 
62     TXT_DrawSeparator(x-2, y, w + 4);
63 
64     if (separator->label != NULL)
65     {
66         TXT_GotoXY(x, y);
67 
68         TXT_FGColor(TXT_COLOR_BRIGHT_GREEN);
69         TXT_DrawString(" ");
70         TXT_DrawString(separator->label);
71         TXT_DrawString(" ");
72     }
73 }
74 
TXT_SeparatorDestructor(TXT_UNCAST_ARG (separator))75 static void TXT_SeparatorDestructor(TXT_UNCAST_ARG(separator))
76 {
77     TXT_CAST_ARG(txt_separator_t, separator);
78 
79     free(separator->label);
80 }
81 
TXT_SetSeparatorLabel(txt_separator_t * separator,char * label)82 void TXT_SetSeparatorLabel(txt_separator_t *separator, char *label)
83 {
84     free(separator->label);
85 
86     if (label != NULL)
87     {
88         separator->label = strdup(label);
89     }
90     else
91     {
92         separator->label = NULL;
93     }
94 }
95 
96 txt_widget_class_t txt_separator_class =
97 {
98     TXT_NeverSelectable,
99     TXT_SeparatorSizeCalc,
100     TXT_SeparatorDrawer,
101     NULL,
102     TXT_SeparatorDestructor,
103     NULL,
104     NULL,
105 };
106 
TXT_NewSeparator(char * label)107 txt_separator_t *TXT_NewSeparator(char *label)
108 {
109     txt_separator_t *separator;
110 
111     separator = malloc(sizeof(txt_separator_t));
112 
113     TXT_InitWidget(separator, &txt_separator_class);
114 
115     separator->label = NULL;
116     TXT_SetSeparatorLabel(separator, label);
117 
118     return separator;
119 }
120 
121