00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "precomp.h"
00010 #include "API/Display/Font/font.h"
00011 #include "API/Core/System/system.h"
00012 #include "API/Core/Resources/resource_manager.h"
00013 #include "API/GUI/gui_manager.h"
00014 #include "inputbox_default.h"
00015
00016 #define BORDER_SIZE 3
00017
00018 CL_InputBox_Default::CL_InputBox_Default(
00019 CL_InputBox *inputbox,
00020 const CL_ComponentOptions &options,
00021 CL_StyleManager_Default *style)
00022 : CL_ComponentStyle(inputbox), inputbox(inputbox)
00023 {
00024 this->style = style;
00025
00026 resources = style->get_resources();
00027 font = CL_Font::load("InputBox/font", resources);
00028 font_disabled = CL_Font::load("InputBox/font_disabled", resources);
00029
00030 CL_Rect position = inputbox->get_position();
00031 position.y2 = position.y1 + font->get_height() + 5;
00032 inputbox->set_position(position);
00033
00034 show_cursor = false;
00035 character_offset = 0;
00036 cursor_blink_time = CL_System::get_time();
00037
00038 slot_paint = inputbox->sig_paint().connect(CL_CreateSlot(this, &CL_InputBox_Default::on_paint));
00039 }
00040
00041 CL_InputBox_Default::~CL_InputBox_Default()
00042 {
00043
00044 }
00045
00046 void CL_InputBox_Default::on_paint()
00047 {
00048 int width = inputbox->get_width();
00049 int height = inputbox->get_height();
00050
00051 if(inputbox->is_enabled() == false || inputbox->has_focus() == false)
00052 {
00053
00054 style->fill_rect(1, 1, width - 1, height - 1, GUICOLOR_WHITE);
00055
00056
00057 style->draw_rect(0, 0, width, height, GUICOLOR_DARK_OUTLINE);
00058 }
00059 else
00060 {
00061
00062 style->fill_rect(2, 2, width - 2, height - 2, GUICOLOR_WHITE);
00063
00064
00065 style->draw_rect(0, 0, width, height, GUICOLOR_DARK_OUTLINE);
00066
00067
00068 style->draw_box(1, 1, width - 1, height - 1, GUICOLOR_DARKER_SHADE, GUICOLOR_MEDIUM_SHADE);
00069 }
00070
00071
00072 const char *text = inputbox->get_text().c_str();
00073
00074
00075 int character_offset = 0;
00076 int pixel_offset = 0;
00077 int text_width = (int)font->get_text_width(text);
00078 if (text_width > width - BORDER_SIZE * 2)
00079 {
00080 while (
00081 text_width >= width - BORDER_SIZE * 2 &&
00082 character_offset + 1 < inputbox->get_cursor_position())
00083 {
00084 int w = font->get_char_width(text[character_offset]);
00085 character_offset++;
00086 pixel_offset += w;
00087 text_width -= w;
00088 }
00089 }
00090
00091
00092 if(inputbox->has_focus())
00093 {
00094 if (inputbox->has_marked_text())
00095 {
00096 int start = inputbox->get_selection_start();
00097 int end = start + inputbox->get_selection_length();
00098
00099 int mark_x1 = 0, mark_x2 = 0;
00100
00101 int i;
00102 for(i = 0; i < start; i++)
00103 mark_x1 += font->get_char_width(text[i]);
00104
00105 for(i = start; i < end; i++)
00106 mark_x2 += font->get_char_width(text[i]);
00107
00108 style->fill_rect(
00109 BORDER_SIZE + mark_x1 - 1,
00110 BORDER_SIZE - 1,
00111 BORDER_SIZE + mark_x1 + mark_x2,
00112 BORDER_SIZE + font->get_height(),
00113 GUICOLOR_SELECTION);
00114 }
00115 }
00116
00117
00118 if(inputbox->is_enabled() == false)
00119 font_disabled->print_left(BORDER_SIZE, 2, &text[character_offset]);
00120 else
00121 font->print_left(BORDER_SIZE, 2, &text[character_offset]);
00122
00123
00124 if(inputbox->has_focus())
00125 {
00126 if (show_cursor)
00127 {
00128 int cursor_x = BORDER_SIZE - pixel_offset;
00129
00130 for(int i = 0; i < inputbox->get_cursor_position(); i++)
00131 cursor_x += font->get_char_width(text[i]);
00132
00133 style->draw_line(cursor_x, BORDER_SIZE - 1, cursor_x, BORDER_SIZE + font->get_height() - 1, GUICOLOR_CARET);
00134 }
00135 unsigned int cur_time = CL_System::get_time();
00136 if (cur_time >= cursor_blink_time)
00137 {
00138 cursor_blink_time = cur_time + 400;
00139 show_cursor = !show_cursor;
00140 }
00141 }
00142 }
00143
00144 int CL_InputBox_Default::get_mouse_position(int x, int y)
00145 {
00146 int delta_x = x - BORDER_SIZE;
00147 unsigned int i;
00148 for (i = 0; i < inputbox->get_text().size(); i++)
00149 {
00150 char buf[2];
00151 buf[0] = inputbox->get_text()[i];
00152 buf[1] = 0;
00153 int w = font->get_text_width(buf);
00154 delta_x -= w;
00155 if (delta_x <= -w/2)
00156 {
00157 return i + character_offset;
00158 }
00159 }
00160 return i;
00161 }