OPT added to MAkefile; KanjiView::Cell constructor: fg,bg changed to int (Fl_Color...
[aoi.git] / src / gui_widgets.cxx
blobb190aade0cb09e9bee638cae22abd35340e73a13
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;
153 void ComponentView::set_data ( const vector<Cell> &d )
155 cells_ = d;
156 redraw();
160 void ComponentView::set_highlight ( const string &components )
162 for ( auto &c: cells_ ){
163 if ( c.type == CELL_HIGHLIGHTED ) // clear previous highlight
164 c.type = CELL;
165 if ( components.find(c.str) != string::npos )
166 c.type = CELL_HIGHLIGHTED;
168 redraw();
172 int ComponentView::handle ( int event )
174 switch ( event ) {
175 case FL_PUSH:
176 int xx = Fl::event_x() - this->x();
177 int yy = Fl::event_y() - this->y();
178 int c = xx/cell_size_;
179 int r = yy/cell_size_;
180 int cell = r*cols_+c;
181 CellType t = cells_[cell].type;
182 // left click
183 if ( Fl::event_button() == FL_LEFT_MOUSE ){
184 if ( t != CELL_LABEL && t != CELL_SELECTED_2 ){
185 cells_[cell].type = (t==CELL_SELECTED_1) ? CELL:CELL_SELECTED_1;
186 do_callback();
187 redraw();
188 return 1;
191 // right click
192 else if ( Fl::event_button() == FL_RIGHT_MOUSE ){
193 if ( t != CELL_LABEL && t != CELL_SELECTED_1 ){
194 cells_[cell].type = (t==CELL_SELECTED_2) ? CELL:CELL_SELECTED_2;
195 do_callback();
196 redraw();
197 return 1;
200 break;
202 return Fl_Widget::handle(event);
206 void ComponentView::draw ()
208 rows_ = h()/cell_size_;
209 cols_ = w()/cell_size_;
211 fl_push_clip(x(), y(), w(), h());
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);
219 // available area of widget
220 int xx = x() + bx;
221 int yy = y() + by;
222 int ww = cols_ * cell_size_;
223 int hh = rows_ * cell_size_;
225 // background
226 fl_color( Fl::get_color( FL_BACKGROUND2_COLOR ) );
227 fl_rectf( x()+bx, y()+by, ww, hh );
229 int cx = xx;
230 int cy = yy;
231 for ( int i=0; i<rows_*cols_; i++ ){
232 if ( i == int(cells_.size()) )
233 break;
234 switch ( cells_[i].type ){
235 case CELL_LABEL:
236 fl_color( Fl::get_color( FL_FOREGROUND_COLOR ) );
237 fl_rectf( cx, cy, cell_size_, cell_size_ );
238 break;
239 case CELL_HIGHLIGHTED:
240 fl_color( color_highlight_ );
241 fl_rectf( cx, cy, cell_size_, cell_size_ );
242 break;
243 case CELL_SELECTED_1:
244 fl_color( color_select1_ );
245 fl_rectf( cx, cy, cell_size_, cell_size_ );
246 break;
247 case CELL_SELECTED_2:
248 fl_color( color_select2_ );
249 fl_rectf( cx, cy, cell_size_, cell_size_ );
250 break;
251 default:
252 break;
254 if ( cells_[i].type == CELL_LABEL ){
255 fl_font(font_label_,fsize_label_);
256 fl_color( Fl::get_color( FL_BACKGROUND2_COLOR ) );
258 else{
259 fl_font(font_cell_,fsize_cell_);
260 fl_color( Fl::get_color( FL_FOREGROUND_COLOR ) );
262 fl_draw( cells_[i].str.c_str(),
263 cx+cell_size_/2-2*fl_descent(), cy+cell_size_/2+2*fl_descent() );
264 cx += cell_size_;
265 if ( cx > x()+cols_*cell_size_ ){
266 cy += cell_size_;
267 cx = xx;
271 } // if !cells_.empty()
273 fl_pop_clip();
277 } // namespace aoi_ui