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/>.
19 #include <FL/Fl_Menu_Item.H>
20 #include "gui_kanjiview.hxx"
25 using utils::split_string
;
29 KanjiView::KanjiView ( int x
, int y
, int w
, int h
, const char *l
): Fl_Table(x
,y
,w
,h
,l
)
31 when( FL_WHEN_RELEASE
|FL_WHEN_CHANGED
);
32 table_box( FL_NO_BOX
);
33 cb_leftclick( scb_general
, nullptr );
34 cb_rightclick( scb_general
, nullptr );
35 cb_select( scb_general
, nullptr );
41 int KanjiView::handle ( int event
)
47 if ( Fl::event_button() == FL_LEFT_MOUSE
&& Fl::belowmouse() == this ){
48 Cell
*c
= cell_at_xy(Fl::event_x()-x(), Fl::event_y()-y());
60 else if ( Fl::event_button() == FL_RIGHT_MOUSE
) {
61 Cell
*c
= cell_at_xy(Fl::event_x()-x(), Fl::event_y()-y());
62 Fl_Menu_Item menu
[] = {
63 { "Copy", 0, scb_copy
, (void*)c
->str
.c_str() },
64 // TODO: copy all kanjidata
67 const Fl_Menu_Item
*m
= menu
->popup(Fl::event_x(), Fl::event_y(), 0, 0, 0);
69 m
->do_callback(0, m
->user_data());
74 if ( Fl::event_button() == FL_RIGHT_MOUSE
)
78 int cell
= selected_id();
79 switch ( Fl::event_key() ){
89 if ( id
< data_
.size() )
95 size_t id
= cell
+cols();
96 if ( id
< data_
.size() )
102 int id
= cell
-cols();
116 return Fl_Table::handle(event
);
120 void KanjiView::font_size ( int s
)
123 fl_font( font_
, font_size_
);
124 int x
= fl_height() + 2*cell_padding_
;
128 void KanjiView::set_data ( const vector
<Cell
> &d
)
130 // prevents division by zero
137 int ncols
= d
.size()/cols();
138 rows( (ncols
>1) ? ncols
:1 );
139 row_height_all( cell_size_
);
140 col_width_all( cell_size_
);
141 draw_cell( CONTEXT_RC_RESIZE
, 0, 0, 0, 0 );
146 void KanjiView::draw_single_cell ( int R
, int C
, int X
, int Y
, int W
, int H
)
148 int cell
= R
*cols()+C
;
150 // empty cells at the end of the last row
151 if ( cell
>= int(data_
.size()) ){
152 fl_push_clip(X
, Y
, W
, H
);
155 fl_color( Fl::get_color( FL_BACKGROUND2_COLOR
) );
156 fl_rectf(X
, Y
, W
, H
);
165 Cell c
= data_
.at(cell
);
166 fl_push_clip(X
, Y
, W
, H
);
169 fl_color(c
.selected
? selection_color_
:c
.bgcolor
);
170 fl_rectf(X
, Y
, W
, H
);
173 fl_font( font_
, font_size_
);
174 fl_color( c
.fgcolor
);
175 fl_draw( c
.str
.c_str(), X
+cell_padding_
, Y
+font_size_
);
185 void KanjiView::draw_cell ( TableContext context
, int R
, int C
, int X
, int Y
, int W
, int H
)
189 case CONTEXT_STARTPAGE
:
190 fl_font( FL_HELVETICA
, font_size());
193 case CONTEXT_COL_HEADER
:
194 fl_push_clip(X
, Y
, W
, H
);
196 fl_draw_box(FL_THIN_UP_BOX
, X
, Y
, W
, H
, col_header_color());
204 draw_single_cell(R
,C
,X
,Y
,W
,H
);
209 case CONTEXT_RC_RESIZE
:
212 int n_cols
= (w()-vscrollbar
->w())/cell_size_
;
213 int n_rows
= ceil((float)data_
.size()/n_cols
);
214 if ( n_cols
!= cols() )
216 if ( n_rows
!= rows() ){
218 row_height_all(cell_size_
);