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"
21 void KanjiInfo::set_data ( const Kanji
&k
)
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
);
52 yy
+= draw_caption_label( "Frequency", std::to_string(kanji_
.freq()), xx
,yy
, cw
,tw
,rh
);
54 yy
+= draw_caption_label( "JLPT", std::to_string(kanji_
.jlpt()), xx
,yy
, cw
,tw
,rh
);
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() ){
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
);
83 ss
= kanji_
.str_kunyomi();
84 bh
= draw_box(font_kanji_
, size_a_
+2, ss
,xx
,yy
,ww
,hh
);
87 ss
= kanji_
.str_nanori();
88 bh
= draw_box(font_kanji_
, size_a_
, ss
,xx
,yy
,ww
,hh
);
91 ss
= kanji_
.str_meaning();
92 bh
= draw_box(font_b_
, size_b_
, ss
,xx
,yy
,ww
,hh
);
95 ss
= kanji_
.str_flags();
96 bh
= draw_box(font_b_
,size_b_
, ss
.empty() ? "FLAGS":ss
,xx
,yy
,ww
,hh
);
101 size(total_width
,yy
+row_height
+padding_
);
107 //////////////////////////////////////////////////////////////////////////
111 ComponentView::ComponentView ( int x
, int y
, int w
, int h
, const char *l
) : Fl_Widget(x
,y
,w
,h
,l
)
119 void ComponentView::font_cell ( Fl_Font f
, int s
)
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
)
143 vector
<string
> ComponentView::cells_by_type ( CellType t
)
146 for ( Cell
&c
: cells_
)
153 void ComponentView::set_data ( const vector
<Cell
> &d
)
160 void ComponentView::set_highlight ( const string
&components
)
162 for ( auto &c
: cells_
){
163 if ( c
.type
== CELL_HIGHLIGHTED
) // clear previous highlight
165 if ( components
.find(c
.str
) != string::npos
)
166 c
.type
= CELL_HIGHLIGHTED
;
172 int ComponentView::handle ( int event
)
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
;
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
;
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
;
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());
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
222 int ww
= cols_
* cell_size_
;
223 int hh
= rows_
* cell_size_
;
226 fl_color( Fl::get_color( FL_BACKGROUND2_COLOR
) );
227 fl_rectf( x()+bx
, y()+by
, ww
, hh
);
231 for ( int i
=0; i
<rows_
*cols_
; i
++ ){
232 if ( i
== int(cells_
.size()) )
234 switch ( cells_
[i
].type
){
236 fl_color( Fl::get_color( FL_FOREGROUND_COLOR
) );
237 fl_rectf( cx
, cy
, cell_size_
, cell_size_
);
239 case CELL_HIGHLIGHTED
:
240 fl_color( color_highlight_
);
241 fl_rectf( cx
, cy
, cell_size_
, cell_size_
);
243 case CELL_SELECTED_1
:
244 fl_color( color_select1_
);
245 fl_rectf( cx
, cy
, cell_size_
, cell_size_
);
247 case CELL_SELECTED_2
:
248 fl_color( color_select2_
);
249 fl_rectf( cx
, cy
, cell_size_
, cell_size_
);
254 if ( cells_
[i
].type
== CELL_LABEL
){
255 fl_font(font_label_
,fsize_label_
);
256 fl_color( Fl::get_color( FL_BACKGROUND2_COLOR
) );
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() );
265 if ( cx
> x()+cols_
*cell_size_
){
271 } // if !cells_.empty()
277 } // namespace aoi_ui