remove Features5, rework gradient plugin, fix paste_edl track title bug, gl_probe...
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / bctitle.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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  02111-1307  USA
19  *
20  */
21
22 #include "bcresources.h"
23 #include "bctitle.h"
24 #include "bcresources.h"
25 #include <string.h>
26 #include <unistd.h>
27
28 BC_Title::BC_Title(int x,
29                 int y,
30                 const char *text,
31                 int font,
32                 int color,
33                 int centered,
34                 int fixed_w)
35  : BC_SubWindow(x, y, -1, -1, -1)
36 {
37         this->font = font;
38         if(color < 0)
39                 this->color = get_resources()->default_text_color;
40         else
41                 this->color = color;
42         this->centered = centered;
43         this->fixed_w = fixed_w;
44         strcpy(this->text, text);
45 }
46
47 BC_Title::~BC_Title()
48 {
49 }
50
51
52 int BC_Title::initialize()
53 {
54         if(w <= 0 || h <= 0)
55                 get_size(this, font, text, fixed_w, w, h);
56
57         if(centered) x -= w / 2;
58
59         BC_SubWindow::initialize();
60         draw(0);
61         show_window(0);
62         return 0;
63 }
64
65 int BC_Title::set_color(int color)
66 {
67         this->color = color;
68         draw(0);
69         return 0;
70 }
71
72 int BC_Title::resize(int w, int h)
73 {
74         resize_window(w, h);
75         draw(0);
76         return 0;
77 }
78
79 int BC_Title::reposition(int x, int y)
80 {
81         reposition_window(x, y, w, h);
82         draw(0);
83         return 0;
84 }
85
86
87 int BC_Title::update(const char *text, int flush)
88 {
89         int new_w, new_h;
90
91         strcpy(this->text, text);
92         get_size(this, font, text, fixed_w, new_w, new_h);
93         if(new_w > w || new_h > h)
94         {
95                 resize_window(new_w, new_h);
96         }
97         draw(flush);
98         return 0;
99 }
100
101 void BC_Title::update(float value)
102 {
103         char string[BCTEXTLEN];
104         sprintf(string, "%.04f", value);
105         update(string);
106 }
107
108 char* BC_Title::get_text()
109 {
110         return text;
111 }
112
113 int BC_Title::draw(int flush)
114 {
115         int i, j, x, y;
116
117 // Fix background for block fonts.
118 // This should eventually be included in a BC_WindowBase::is_blocked_font()
119
120         if(font == MEDIUM_7SEGMENT)
121         {
122                 //leave it up to the theme to decide if we need a background or not.
123                 if (top_level->get_resources()->draw_clock_background) {
124                         BC_WindowBase::set_color(get_bg_color());
125                         draw_box(0, 0, w, h);
126                 }
127         }
128         else
129                 draw_top_background(parent_window, 0, 0, w, h);
130
131         set_font(font);
132         BC_WindowBase::set_color(color);
133         int text_len = strlen(text);
134         j = 0;  x = 0;  y = get_text_ascent(font);
135         for(i = 0; i <= text_len; i++)
136         {
137                 if(text[i] == '\n' || text[i] == 0)
138                 {
139                         if(centered)
140                         {
141                                 draw_center_text(get_w() / 2,
142                                         y,
143                                         &text[j],
144                                         i - j);
145                                 j = i + 1;
146                         }
147                         else
148                         {
149                                 draw_text(x,
150                                         y,
151                                         &text[j],
152                                         i - j);
153                                 j = i + 1;
154                         }
155                         y += get_text_height(font);
156                 }
157         }
158         set_font(MEDIUMFONT);    // reset
159         flash(flush);
160         return 0;
161 }
162
163 int BC_Title::calculate_w(BC_WindowBase *gui, const char *text, int font)
164 {
165         int temp_w, temp_h;
166         get_size(gui, font, text, 0, temp_w, temp_h);
167         return temp_w;
168 }
169
170 int BC_Title::calculate_h(BC_WindowBase *gui, const char *text, int font)
171 {
172         int temp_w, temp_h;
173         get_size(gui, font, text, 0, temp_w, temp_h);
174         return temp_h;
175 }
176
177
178
179 void BC_Title::get_size(BC_WindowBase *gui, int font, const char *text, int fixed_w, int &w, int &h)
180 {
181         int i, j;
182         w = 0;
183         h = 0;
184         j = 0;
185
186         int text_len = strlen(text);
187         for(i = 0; i <= text_len; i++)
188         {
189                 int line_w = 0;
190                 if(text[i] == '\n')
191                 {
192                         h++;
193                         line_w = gui->get_text_width(font, &text[j], i - j);
194                         j = i + 1;
195                 }
196                 else
197                 if(text[i] == 0)
198                 {
199                         h++;
200                         line_w = gui->get_text_width(font, &text[j]);
201                 }
202                 if(line_w > w) w = line_w;
203         }
204
205         h *= gui->get_text_height(font);
206         w += 5;
207         if(fixed_w > 0) w = fixed_w;
208 }