00001 #include "bcresources.h"
00002 #include "bctitle.h"
00003 #include "bcresources.h"
00004 #include <string.h>
00005 #include <unistd.h>
00006
00007 BC_Title::BC_Title(int x,
00008 int y,
00009 char *text,
00010 int font,
00011 int color,
00012 int centered,
00013 int fixed_w)
00014 : BC_SubWindow(x, y, -1, -1, -1)
00015 {
00016 this->font = font;
00017 if(color < 0)
00018 this->color = get_resources()->default_text_color;
00019 else
00020 this->color = color;
00021 this->centered = centered;
00022 this->fixed_w = fixed_w;
00023 strcpy(this->text, text);
00024 }
00025
00026 BC_Title::~BC_Title()
00027 {
00028 }
00029
00030
00031 int BC_Title::initialize()
00032 {
00033 if(w <= 0 || h <= 0)
00034 get_size(this, font, text, fixed_w, w, h);
00035
00036 if(centered) x -= w / 2;
00037
00038 BC_SubWindow::initialize();
00039 draw();
00040 return 0;
00041 }
00042
00043 int BC_Title::set_color(int color)
00044 {
00045 this->color = color;
00046 draw();
00047 return 0;
00048 }
00049
00050 int BC_Title::resize(int w, int h)
00051 {
00052 resize_window(w, h);
00053 draw();
00054 return 0;
00055 }
00056
00057 int BC_Title::reposition(int x, int y)
00058 {
00059 reposition_window(x, y, w, h);
00060 draw();
00061 return 0;
00062 }
00063
00064
00065 int BC_Title::update(char *text)
00066 {
00067 int new_w, new_h;
00068
00069 strcpy(this->text, text);
00070 get_size(this, font, text, fixed_w, new_w, new_h);
00071 if(new_w > w || new_h > h)
00072 {
00073 resize_window(new_w, new_h);
00074 }
00075 draw();
00076 return 0;
00077 }
00078
00079 void BC_Title::update(float value)
00080 {
00081 char string[BCTEXTLEN];
00082 sprintf(string, "%.04f", value);
00083 update(string);
00084 }
00085
00086 char* BC_Title::get_text()
00087 {
00088 return text;
00089 }
00090
00091 int BC_Title::draw()
00092 {
00093 int i, j, x, y;
00094
00095
00096
00097
00098 if(font == MEDIUM_7SEGMENT)
00099 {
00100
00101 if (top_level->get_resources()->draw_clock_background)
00102 {
00103 BC_WindowBase::set_color(BLACK);
00104 draw_box(0, 0, w, h);
00105 }
00106 }
00107 else
00108 draw_top_background(parent_window, 0, 0, w, h);
00109
00110 set_font(font);
00111 BC_WindowBase::set_color(color);
00112 for(i = 0, j = 0, x = 0, y = get_text_ascent(font);
00113 i <= strlen(text);
00114 i++)
00115 {
00116 if(text[i] == '\n' || text[i] == 0)
00117 {
00118 if(centered)
00119 {
00120 draw_center_text(get_w() / 2,
00121 y,
00122 &text[j],
00123 i - j);
00124 j = i + 1;
00125 }
00126 else
00127 {
00128 draw_text(x,
00129 y,
00130 &text[j],
00131 i - j);
00132 j = i + 1;
00133 }
00134 y += get_text_height(font);
00135 }
00136 }
00137 set_font(MEDIUMFONT);
00138 flash();
00139 flush();
00140 return 0;
00141 }
00142
00143 int BC_Title::calculate_w(BC_WindowBase *gui, char *text, int font)
00144 {
00145 int temp_w, temp_h;
00146 get_size(gui, font, text, 0, temp_w, temp_h);
00147 return temp_w;
00148 }
00149
00150 int BC_Title::calculate_h(BC_WindowBase *gui, char *text, int font)
00151 {
00152 int temp_w, temp_h;
00153 get_size(gui, font, text, 0, temp_w, temp_h);
00154 return temp_h;
00155 }
00156
00157
00158
00159 void BC_Title::get_size(BC_WindowBase *gui, int font, char *text, int fixed_w, int &w, int &h)
00160 {
00161 int i, j, x, y, line_w = 0;
00162 w = 0;
00163 h = 0;
00164
00165 for(i = 0, j = 0; i <= strlen(text); i++)
00166 {
00167 line_w = 0;
00168 if(text[i] == '\n')
00169 {
00170 h++;
00171 line_w = gui->get_text_width(font, &text[j], i - j);
00172 j = i + 1;
00173 }
00174 else
00175 if(text[i] == 0)
00176 {
00177 h++;
00178 line_w = gui->get_text_width(font, &text[j]);
00179 }
00180 if(line_w > w) w = line_w;
00181 }
00182
00183 h *= gui->get_text_height(font);
00184 w += 5;
00185 if(fixed_w > 0) w = fixed_w;
00186 }