fix make_shader segment count, repair mask tweak keyfrm updates for xlat,rotate,scale
[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 "bcpixmap.h"
25 #include "vframe.h"
26 #include <string.h>
27 #include <unistd.h>
28
29 BC_Title::BC_Title(int x,
30                 int y,
31                 const char *text,
32                 int font,
33                 int color,
34                 int centered,
35                 int fixed_w)
36  : BC_SubWindow(x, y, -1, -1, -1)
37 {
38         this->font = font;
39         if(color < 0)
40                 this->color = get_resources()->default_text_color;
41         else
42                 this->color = color;
43         this->centered = centered;
44         this->fixed_w = fixed_w;
45         strcpy(this->text, text);
46 }
47
48 BC_Title::~BC_Title()
49 {
50 }
51
52
53 int BC_Title::initialize()
54 {
55         if(w <= 0 || h <= 0)
56                 get_size(this, font, text, fixed_w, w, h);
57
58         if(centered) x -= w / 2;
59
60         BC_SubWindow::initialize();
61         draw(0);
62         show_window(0);
63         return 0;
64 }
65
66 int BC_Title::set_color(int color)
67 {
68         this->color = color;
69         draw(0);
70         return 0;
71 }
72
73 int BC_Title::resize(int w, int h)
74 {
75         resize_window(w, h);
76         draw(0);
77         return 0;
78 }
79
80 int BC_Title::reposition(int x, int y)
81 {
82         reposition_window(x, y, w, h);
83         draw(0);
84         return 0;
85 }
86
87
88 int BC_Title::update(const char *text, int flush)
89 {
90         int new_w, new_h;
91
92         strcpy(this->text, text);
93         get_size(this, font, text, fixed_w, new_w, new_h);
94         if(new_w > w || new_h > h)
95         {
96                 resize_window(new_w, new_h);
97         }
98         draw(flush);
99         return 0;
100 }
101
102 void BC_Title::update(float value)
103 {
104         char string[BCTEXTLEN];
105         sprintf(string, "%.04f", value);
106         update(string);
107 }
108
109 char* BC_Title::get_text()
110 {
111         return text;
112 }
113
114 int BC_Title::draw(int flush)
115 {
116 // Fix background for block fonts.
117 // This should eventually be included in a BC_WindowBase::is_blocked_font()
118
119         if(font == MEDIUM_7SEGMENT)
120         {
121                 //leave it up to the theme to decide if we need a background or not.
122                 if (top_level->get_resources()->draw_clock_background) {
123                         BC_WindowBase::set_color(get_bg_color());
124                         draw_box(0, 0, w, h);
125                 }
126         }
127         else
128                 draw_top_background(parent_window, 0, 0, w, h);
129
130         set_font(font);
131         BC_WindowBase::set_color(color);
132         return draw(flush, 0, 0);
133 }
134
135 int BC_Title::draw(int flush, int x, int y)
136 {
137         y += get_text_ascent(font);
138         int len = strlen(text);
139         for( int i=0,j=0; i<=len; ++i ) {
140                 if( text[i] && text[i] != '\n' ) continue;
141                 if( centered )
142                         draw_center_text(get_w()/2, y, &text[j], i - j);
143                 else
144                         draw_text(x, y, &text[j], i - j);
145                 j = i + 1;
146                 y += get_text_height(font);
147         }
148         set_font(MEDIUMFONT);    // reset
149         flash(flush);
150         return 0;
151 }
152
153 int BC_Title::calculate_w(BC_WindowBase *gui, const char *text, int font)
154 {
155         int temp_w, temp_h;
156         get_size(gui, font, text, 0, temp_w, temp_h);
157         return temp_w;
158 }
159
160 int BC_Title::calculate_h(BC_WindowBase *gui, const char *text, int font)
161 {
162         int temp_w, temp_h;
163         get_size(gui, font, text, 0, temp_w, temp_h);
164         return temp_h;
165 }
166
167
168
169 void BC_Title::get_size(BC_WindowBase *gui, int font, const char *text, int fixed_w, int &w, int &h)
170 {
171         int i, j;
172         w = 0;
173         h = 0;
174         j = 0;
175
176         int text_len = strlen(text);
177         for(i = 0; i <= text_len; i++)
178         {
179                 int line_w = 0;
180                 if(text[i] == '\n')
181                 {
182                         h++;
183                         line_w = gui->get_text_width(font, &text[j], i - j);
184                         j = i + 1;
185                 }
186                 else
187                 if(text[i] == 0)
188                 {
189                         h++;
190                         line_w = gui->get_text_width(font, &text[j]);
191                 }
192                 if(line_w > w) w = line_w;
193         }
194
195         h *= gui->get_text_height(font);
196         w += 5;
197         if(fixed_w > 0) w = fixed_w;
198 }
199
200
201 BC_TitleBar::BC_TitleBar(int x, int y, int w, int offset, int margin,
202                 const char *text, int font, int color, VFrame *data)
203 : BC_Title(x, y, text, font, color, 0, w)
204 {
205         this->offset = offset;
206         this->margin = margin;
207         this->data = data;
208         image = 0;
209 }
210
211 BC_TitleBar::~BC_TitleBar()
212 {
213         delete image;
214 }
215
216 void BC_TitleBar::set_image(VFrame *data)
217 {
218         delete image;
219         image = new BC_Pixmap(get_parent_window(), data, PIXMAP_ALPHA);
220 }
221
222 int BC_TitleBar::initialize()
223 {
224         if(data)
225                 set_image(data);
226         else
227                 set_image(get_resources()->bar_data);
228         BC_Title::initialize();
229         draw(0);
230         return 0;
231 }
232
233 int BC_TitleBar::draw(int flush)
234 {
235         int w = get_w(), h = get_h(), h2 = h/2;
236         draw_top_background(get_parent_window(), 0, 0,w, h);
237         draw_3segmenth(0,h2, offset, 0, offset, image);
238         int tx = offset + margin, tw, th;
239         set_font(font);
240         BC_WindowBase::set_color(color);
241         BC_Title::draw(flush, tx, 0);
242         get_size(get_parent_window(), font, text, 0, tw, th);
243         tx += tw + margin;
244         draw_3segmenth(tx,h2, w-tx, tx,w-tx, image);
245         flash(flush);
246         return 0;
247 }
248