Debian Codegraph @

Examples/GUIStyle/ZZ_Theme/inputbox_zz.cpp

raw
1 #include <ClanLib/core.h>
2 #include <ClanLib/display.h>
3
4 #include "stylemanager_zz.h"
5 #include "inputbox_zz.h"
6
7 #pragma warning ( push, 1 )
8 #pragma warning ( disable: 4244 ) // conversion from 'int' to 'float', possible loss of data
9
10
11 CL_InputBox_ZZ::CL_InputBox_ZZ(
12 CL_InputBox *inputbox,
13 CL_StyleManager_ZZ *style)
14 :
15 CL_ComponentStyle(inputbox),
16 CL_InputBox_StyleBase(inputbox, style->get_resources(), "InputBox/font", 3),
17 first_paint(false)
18 {
19 //this->style = style;
20 //resources = style->get_resources();
21
22 slots.connect(inputbox->sig_paint(), this, &CL_InputBox_ZZ::on_paint);
23 }
24
25 CL_InputBox_ZZ::~CL_InputBox_ZZ()
26 {
27 }
28
29 void CL_InputBox_ZZ::on_paint()
30 {
31 on_paint_default();
32 }
33
34 void CL_InputBox_ZZ::on_changed()
35 {
36 // TODO:
37 }
38
39 void CL_InputBox_ZZ::on_paint_default()
40 {
41 // Fill
42 //-------
43 CL_Color const clr_background(0, 40, 0, 60);
44 CL_Rect const rc_main(inputbox->get_screen_rect());
45 CL_StyleManager_ZZ::fill_rounded_rect(rc_main, clr_background);
46
47 // Outline
48 CL_Color const clr_outline(175, 205, 148);
49 CL_StyleManager_ZZ::draw_rounded_rect(rc_main, clr_outline);
50
51 on_paint_input();
52 }
53
54 void CL_InputBox_ZZ::on_paint_input()
55 {
56 // That code is from NetObject, with small
57 // correction to width_offset for font
58
59 // For easy reference, put inputfield-text into local variable
60 std::string const & text = inputbox->get_text();
61 int width = inputbox->get_width();
62
63 int pixel_y_offset = 4;
64
65 int const font_width_offset = font->get_width_offset();
66 font->set_width_offset(0);
67
68 // Calculate scroll offset
69 int drawing_text_width = 0;
70 int draw_to = draw_from;
71
72 const int BORDER_SIZE = 6;
73
74 while (draw_to < text.length())
75 {
76 int w = get_chars_width( draw_to, draw_to );
77 if (drawing_text_width + w > width - BORDER_SIZE * 2)
78 break;
79
80 drawing_text_width += w;
81 draw_to ++;
82 }
83
84 // Display marked text, if any
85 if (inputbox->has_marked_text())
86 {
87 int start = inputbox->get_selection_start();
88 int end = start + inputbox->get_selection_length();
89
90 if (start < draw_from) start = draw_from;
91 if (end > draw_to ) end = draw_to;
92
93 int mark_x1 = get_chars_width( draw_from, start-1 );
94 int mark_x2 = get_chars_width( draw_from, end-1 );
95
96 CL_Display::fill_rect(
97 CL_Rect(inputbox->get_screen_x() + BORDER_SIZE + mark_x1 - 1,
98 inputbox->get_screen_y() + pixel_y_offset,
99 inputbox->get_screen_x() + BORDER_SIZE + mark_x2,
100 inputbox->get_screen_y() + pixel_y_offset + font->get_height() + 1),
101 CL_Color(150, 180, 150, static_cast<unsigned int>(0.5f * 255)));
102 }
103
104 // Display text
105 int draw_count = draw_to - draw_from;
106
107 if (draw_from >= 0 && draw_count > 0)
108 {
109 font->draw(inputbox->get_screen_x() + BORDER_SIZE,
110 inputbox->get_screen_y() + pixel_y_offset, text.substr( draw_from, draw_count ));
111 }
112
113 // Show blinking cursor
114 if(inputbox->has_focus())
115 {
116 if (show_cursor)
117 {
118 int cursor_pos = inputbox->get_cursor_position();
119 int cursor_x = border_size + get_chars_width( draw_from, cursor_pos - 1 );
120
121 //style->
122 CL_Display::draw_line(
123 inputbox->get_screen_x() + cursor_x,
124 inputbox->get_screen_y() + pixel_y_offset,
125 inputbox->get_screen_x() + cursor_x,
126 inputbox->get_screen_y() + pixel_y_offset + font->get_height() + 1,
127 CL_Color::whitesmoke);
128 }
129 unsigned int cur_time = CL_System::get_time();
130 if (cur_time >= cursor_blink_time)
131 {
132 cursor_blink_time = cur_time + 400;
133 show_cursor = !show_cursor;
134 }
135 }
136 font->set_width_offset(font_width_offset);
137 }
138
139
140 #pragma warning ( pop )