r419: various fixes, memory allocations, etc.
[cinelerra_cv/mob.git] / guicast / bctitle.C
blobd67fc8f77f2c0e5433b28305f369f38c8763040e
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(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(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 char* BC_Title::get_text()
81         return text;
84 int BC_Title::draw()
86         int i, j, x, y;
88 // Fix background for block fonts.
89 // This should eventually be included in a BC_WindowBase::is_blocked_font()
91         if(font == MEDIUM_7SEGMENT)
92         {
93           //leave it up to the theme to decide if we need a background or not.
94           if (top_level->get_resources()->draw_clock_background)
95           {
96               BC_WindowBase::set_color(BLACK);
97               draw_box(0, 0, w, h);
98           }
99         }
100         else
101                 draw_top_background(parent_window, 0, 0, w, h);
103         set_font(font);
104         BC_WindowBase::set_color(color);
105         for(i = 0, j = 0, x = 0, y = get_text_ascent(font); 
106                 i <= strlen(text); 
107                 i++)
108         {
109                 if(text[i] == '\n' || text[i] == 0)
110                 {
111                         if(centered)
112                         {
113                                 draw_center_text(get_w() / 2, 
114                                         y,
115                                         &text[j],
116                                         i - j);
117                                 j = i + 1;
118                         }
119                         else
120                         {
121                                 draw_text(x, 
122                                         y,
123                                         &text[j],
124                                         i - j);
125                                 j = i + 1;
126                         }
127                         y += get_text_height(font);
128                 }
129         }
130         set_font(MEDIUMFONT);    // reset
131         flash();
132         flush();
133         return 0;
136 int BC_Title::get_size(int &w, int &h)
138         int i, j, x, y, line_w = 0;
139         w = 0;
140         h = 0;
142         for(i = 0, j = 0; i <= strlen(text); i++)
143         {
144                 line_w = 0;
145                 if(text[i] == '\n')
146                 {
147                         h++;
148                         line_w = get_text_width(font, &text[j], i - j);
149                         j = i + 1;
150                 }
151                 else
152                 if(text[i] == 0)
153                 {
154                         h++;
155                         line_w = get_text_width(font, &text[j]);
156                 }
157                 if(line_w > w) w = line_w;
158         }
160         h *= get_text_height(font);
161         w += 5;
162         if(fixed_w > 0) w = fixed_w;
164         return 0;