fixed issue: only 12156 kanji searchable
[aoi.git] / src / gui_widgets.cxx
blob41d364f8fc90c28628183061098285ee6b16b7a8
1 /*
2 Copyright 2013 Karel Matas
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "gui_widgets.hxx"
19 namespace aoi_ui{
21 void KanjiInfo::set_data ( const Kanji &k )
23 kanji_ = k;
25 box(FL_BORDER_BOX);
26 fl_font( font_a_, size_a_ );
27 int row_height = fl_height(font_a_, size_a_) + fl_descent();
29 string knj = kanji_.kanji();
30 strings_.push_back(knj);
31 kanjibox_ = new Fl_Box( this->x()+padding_, this->y()+padding_,
32 row_height*5, row_height*5, knj.c_str());
33 kanjibox_->labelfont( FONT_KANJI );
34 kanjibox_->labelsize( size_a_*6 );
35 kanjibox_->box(FL_BORDER_BOX);
37 fl_font( font_a_, size_a_ );
38 int cw = fl_width("Frequency:"); // caption width: longest word in the first column
39 int tw = fl_width("U+123456"); // text width: longest possible word in 2nd column
40 int total_width = kanjibox_->w() + cw +tw+padding_;
42 int xx = this->x() + padding_ + kanjibox_->h() + 5;
43 int yy = this->y() + padding_;
45 const int rh = row_height;
47 yy += draw_caption_label( "UCS", "U+"+kanji_.ucs(), xx ,yy, cw, tw, rh );
48 yy += draw_caption_label( "Strokes", std::to_string(kanji_.strokes()), xx ,yy, cw,tw,rh);
49 if ( kanji_.rad_classic() )
50 yy += draw_caption_label( "Radical", kanji_.str_radicals(), xx ,yy, cw,tw,rh);
51 if ( kanji_.freq() )
52 yy += draw_caption_label( "Frequency", std::to_string(kanji_.freq()), xx ,yy, cw,tw,rh);
53 if ( kanji_.jlpt() )
54 yy += draw_caption_label( "JLPT", std::to_string(kanji_.jlpt()), xx ,yy, cw,tw,rh);
55 if ( kanji_.grade() )
56 yy += draw_caption_label( "Grade", std::to_string(kanji_.grade()), xx ,yy, cw,tw,rh);
57 bool skip_label_drawn = false;
58 for ( auto skip : kanji_.skip() ){
59 char buff[64];
60 sprintf( buff, "%d-%d-%d %s",
61 skip.s1, skip.s2, skip.s3, skip.misclass.empty() ? "":"!" );
62 yy += draw_caption_label( skip_label_drawn ? "":"SKIP", buff, xx ,yy, cw,tw,rh);
63 skip_label_drawn = true;
67 // draw box ignores hh and sets the height as necesary
68 // string ss = kanji_.components();
69 // yy += draw_box(font_kanji_, size_a_+2, ss,xx,yy,cw+tw,rh);
72 if ( yy < kanjibox_->x() + kanjibox_->h() + 5 )
73 yy = kanjibox_->x() + kanjibox_->h() + 5;
75 xx = this->x() + padding_;
76 int hh = row_height*3;
77 int ww = total_width - 2*padding_;
79 string ss = kanji_.str_onyomi();
80 int bh = draw_box(font_kanji_, size_a_+2, ss,xx,yy,ww,hh);
81 yy += bh +size_b_;
83 ss = kanji_.str_kunyomi();
84 bh = draw_box(font_kanji_, size_a_+2, ss,xx,yy,ww,hh);
85 yy += bh +size_b_;
87 ss = kanji_.str_nanori();
88 bh = draw_box(font_kanji_, size_a_, ss,xx,yy,ww,hh);
89 yy += bh +size_b_;
91 ss = kanji_.str_meaning();
92 bh = draw_box(font_b_, size_b_, ss,xx,yy,ww,hh);
93 yy += bh +10;
95 ss = kanji_.str_flags();
96 bh = draw_box(font_b_,size_b_, ss.empty() ? "FLAGS":ss,xx,yy,ww,hh);
99 if ( autoresize() ){
100 resizable(0);
101 size(total_width,yy+row_height+padding_);
104 end();
107 //////////////////////////////////////////////////////////////////////////
108 // COMPONENTVIEW
111 ComponentView::ComponentView ( int x, int y, int w, int h, const char *l ) : Fl_Widget(x,y,w,h,l)
113 box(FL_BORDER_BOX);
114 font_cell();
115 font_label();
119 void ComponentView::font_cell ( Fl_Font f, int s)
121 font_cell_ = f;
122 fsize_cell_ = s;
123 set_cellsize();
126 void ComponentView::set_cellsize ()
128 fl_font(font_cell_,fsize_cell_);
129 int cellsize1 = fl_height() + fl_descent()/2;
130 fl_font(font_label_,fsize_label_);
131 int cellsize2 = fl_height() + fl_descent()/2;
132 cell_size_ = std::max(cellsize1,cellsize2);
136 void ComponentView::font_label ( Fl_Font f, int s)
138 font_label_ = f;
139 fsize_label_ = s;
140 set_cellsize();
143 vector<string> ComponentView::cells_by_type ( CellType t )
145 vector<string> v;
146 for ( Cell &c: cells_ )
147 if ( c.type == t )
148 v.push_back(c.str);
149 return v;
152 void ComponentView::set_data ( const vector<Cell> &d )
154 cells_ = d;
155 redraw();
159 void ComponentView::set_highlight ( const string &components )
161 for ( auto &c: cells_ ){
162 if ( c.type == CELL_HIGHLIGHTED ) // clear previous highlight
163 c.type = CELL;
164 if ( components.find(c.str) != string::npos )
165 c.type = CELL_HIGHLIGHTED;
167 redraw();
170 int ComponentView::handle ( int event )
172 switch ( event ) {
173 case FL_PUSH:
174 int xx = Fl::event_x() - this->x();
175 int yy = Fl::event_y() - this->y();
176 int c = xx/cell_size_;
177 int r = yy/cell_size_;
178 int cell = r*cols_+c;
179 CellType t = cells_[cell].type;
180 if ( Fl::event_button() == FL_LEFT_MOUSE ){
181 if ( t != CELL_LABEL && t != CELL_SELECTED_2 ){
182 cells_[cell].type = (t==CELL_SELECTED_1) ? CELL:CELL_SELECTED_1;
183 do_callback();
184 redraw();
185 return 1;
188 else if ( Fl::event_button() == FL_RIGHT_MOUSE ){
189 if ( t != CELL_LABEL && t != CELL_SELECTED_1 ){
190 cells_[cell].type = (t==CELL_SELECTED_2) ? CELL:CELL_SELECTED_2;
191 do_callback();
192 redraw();
193 return 1;
196 // printf("x,y: %d,%d cell: %d (%d,%d)\n",xx,yy,cell,c,r);
197 break;
199 return Fl_Widget::handle(event);
203 void ComponentView::draw ()
205 rows_ = h()/cell_size_;
206 cols_ = w()/cell_size_;
208 // printf("rows: %d cols: %d cells: %d\n",rows_,cols_,rows_*cols_);
210 fl_push_clip(x(), y(), w(), h());
212 // TODO: upravit podle skutecne velikosti
213 draw_box();
214 if ( !cells_.empty() ) {
215 Fl_Boxtype b = box();
216 int bx = Fl::box_dx(b);
217 int by = Fl::box_dy(b);
218 // int bw = Fl::box_dw(b);
219 // int bh = Fl::box_dh(b);
221 // available area of widget
222 int xx = x() + bx;
223 int yy = y() + by;
224 int ww = cols_ * cell_size_;
225 int hh = rows_ * cell_size_;
227 // background
228 fl_color( Fl::get_color( FL_BACKGROUND2_COLOR ) );
229 fl_rectf( x()+bx, y()+by, ww, hh );
231 int cx = xx;
232 int cy = yy;
233 for ( int i=0; i<rows_*cols_; i++ ){
234 if ( i == int(cells_.size()) )
235 break;
236 switch ( cells_[i].type ){
237 case CELL_LABEL:
238 fl_color( Fl::get_color( FL_FOREGROUND_COLOR ) );
239 fl_rectf( cx, cy, cell_size_, cell_size_ );
240 break;
241 case CELL_HIGHLIGHTED:
242 fl_color( color_highlight_ );
243 fl_rectf( cx, cy, cell_size_, cell_size_ );
244 break;
245 case CELL_SELECTED_1:
246 fl_color( color_select1_ );
247 fl_rectf( cx, cy, cell_size_, cell_size_ );
248 break;
249 case CELL_SELECTED_2:
250 fl_color( color_select2_ );
251 fl_rectf( cx, cy, cell_size_, cell_size_ );
252 break;
253 default:
254 break;
256 if ( cells_[i].type == CELL_LABEL ){
257 fl_font(font_label_,fsize_label_);
258 fl_color( Fl::get_color( FL_BACKGROUND2_COLOR ) );
260 else{
261 fl_font(font_cell_,fsize_cell_);
262 fl_color( Fl::get_color( FL_FOREGROUND_COLOR ) );
264 fl_draw( cells_[i].str.c_str(),
265 cx+cell_size_/2-2*fl_descent(), cy+cell_size_/2+2*fl_descent() );
266 cx += cell_size_;
267 if ( cx > x()+cols_*cell_size_ ){
268 cy += cell_size_;
269 cx = xx;
273 } // if !cells_.empty()
275 fl_pop_clip();
279 } // namespace aoi_ui