Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / guicast / bctitle.C
blobe807bc507a6fce156d59435e0d326b94abab3712
1 #include "bcresources.h"
2 #include "bctitle.h"
3 #include "bcresources.h"
4 #include <string.h>
5 #include <unistd.h>
7 BC_Title::BC_Title(int x, 
8                 int y, 
9                 char *text, 
10                 int font, 
11                 int color, 
12                 int centered,
13                 int fixed_w)
14  : BC_SubWindow(x, y, -1, -1, -1)
16         this->font = font;
17         if(color < 0) 
18                 this->color = get_resources()->default_text_color;
19         else
20                 this->color = color;
21         this->centered = centered;
22         this->fixed_w = fixed_w;
23         strcpy(this->text, text);
26 BC_Title::~BC_Title()
31 int BC_Title::initialize()
33         if(w <= 0 || h <= 0)
34                 get_size(this, font, text, fixed_w, w, h);
36         if(centered) x -= w / 2;
38         BC_SubWindow::initialize();
39         draw();
40         return 0;
43 int BC_Title::set_color(int color)
45         this->color = color;
46         draw();
47         return 0;
50 int BC_Title::resize(int w, int h)
52         resize_window(w, h);
53         draw();
54         return 0;
57 int BC_Title::reposition(int x, int y)
59         reposition_window(x, y, w, h);
60         draw();
61         return 0;
65 int BC_Title::update(char *text)
67         int new_w, new_h;
69         strcpy(this->text, text);
70         get_size(this, font, text, fixed_w, new_w, new_h);
71         if(new_w > w || new_h > h)
72         {
73                 resize_window(new_w, new_h);
74         }
75         draw();
76         return 0;
79 void BC_Title::update(float value)
81         char string[BCTEXTLEN];
82         sprintf(string, "%.04f", value);
83         update(string);
86 char* BC_Title::get_text()
88         return text;
91 int BC_Title::draw()
93         int i, j, x, y;
95 // Fix background for block fonts.
96 // This should eventually be included in a BC_WindowBase::is_blocked_font()
98         if(font == MEDIUM_7SEGMENT)
99         {
100           //leave it up to the theme to decide if we need a background or not.
101           if (top_level->get_resources()->draw_clock_background)
102           {
103               BC_WindowBase::set_color(BLACK);
104               draw_box(0, 0, w, h);
105           }
106         }
107         else
108                 draw_top_background(parent_window, 0, 0, w, h);
110         set_font(font);
111         BC_WindowBase::set_color(color);
112         for(i = 0, j = 0, x = 0, y = get_text_ascent(font); 
113                 i <= strlen(text); 
114                 i++)
115         {
116                 if(text[i] == '\n' || text[i] == 0)
117                 {
118                         if(centered)
119                         {
120                                 draw_center_text(get_w() / 2, 
121                                         y,
122                                         &text[j],
123                                         i - j);
124                                 j = i + 1;
125                         }
126                         else
127                         {
128                                 draw_text(x, 
129                                         y,
130                                         &text[j],
131                                         i - j);
132                                 j = i + 1;
133                         }
134                         y += get_text_height(font);
135                 }
136         }
137         set_font(MEDIUMFONT);    // reset
138         flash();
139         flush();
140         return 0;
143 int BC_Title::calculate_w(BC_WindowBase *gui, char *text, int font)
145         int temp_w, temp_h;
146         get_size(gui, font, text, 0, temp_w, temp_h);
147         return temp_w;
150 int BC_Title::calculate_h(BC_WindowBase *gui, char *text, int font)
152         int temp_w, temp_h;
153         get_size(gui, font, text, 0, temp_w, temp_h);
154         return temp_h;
159 void BC_Title::get_size(BC_WindowBase *gui, int font, char *text, int fixed_w, int &w, int &h)
161         int i, j, x, y, line_w = 0;
162         w = 0;
163         h = 0;
165         for(i = 0, j = 0; i <= strlen(text); i++)
166         {
167                 line_w = 0;
168                 if(text[i] == '\n')
169                 {
170                         h++;
171                         line_w = gui->get_text_width(font, &text[j], i - j);
172                         j = i + 1;
173                 }
174                 else
175                 if(text[i] == 0)
176                 {
177                         h++;
178                         line_w = gui->get_text_width(font, &text[j]);
179                 }
180                 if(line_w > w) w = line_w;
181         }
183         h *= gui->get_text_height(font);
184         w += 5;
185         if(fixed_w > 0) w = fixed_w;
188 //      Local Variables:
189 //      mode: C++
190 //      c-file-style: "linux"
191 //      End: