OPT added to MAkefile; KanjiView::Cell constructor: fg,bg changed to int (Fl_Color...
[aoi.git] / src / gui_kanjiview.cxx
blobf0ca68c1895d5250e186f590d442cd713ecfebff
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 <cstdio>
18 #include <cmath>
19 #include <FL/Fl_Menu_Item.H>
20 #include "gui_kanjiview.hxx"
21 #include "utils.hxx"
22 #include "logger.hxx"
23 #include <stdexcept>
25 using utils::split_string;
27 namespace aoi_ui {
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 );
36 cols(1);
37 end();
41 int KanjiView::handle ( int event )
43 switch ( event ) {
44 case FL_PUSH:
46 take_focus();
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());
49 if ( c ){
50 if ( c->selected )
51 c->selected = false;
52 else {
53 select_cell(c);
54 cb_leftclick(this);
56 redraw();
57 return 1;
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
65 { 0 }
67 const Fl_Menu_Item *m = menu->popup(Fl::event_x(), Fl::event_y(), 0, 0, 0);
68 if ( m )
69 m->do_callback(0, m->user_data());
70 return 1;
73 case FL_RELEASE:
74 if ( Fl::event_button() == FL_RIGHT_MOUSE )
75 return 1;
76 case FL_KEYUP:
78 int cell = selected_id();
79 switch ( Fl::event_key() ){
80 case FL_Left:
82 if ( cell > 0 )
83 select_cell(cell-1);
84 return 1;
86 case FL_Right:
88 size_t id = cell+1;
89 if ( id < data_.size() )
90 select_cell(id);
91 return 1;
93 case FL_Down:
95 size_t id = cell+cols();
96 if ( id < data_.size() )
97 select_cell(id);
98 return 1;
100 case FL_Up:
102 int id = cell-cols();
103 if ( id >= 0 )
104 select_cell(id);
105 return 1;
107 case FL_Enter:
108 case ' ':
110 cb_select(this);
111 return 1;
115 default:
116 return Fl_Table::handle(event);
120 void KanjiView::font_size ( int s )
122 font_size_ = s;
123 fl_font( font_, font_size_ );
124 int x = fl_height() + 2*cell_padding_;
125 cell_size(x);
128 void KanjiView::set_data ( const vector<Cell> &d )
130 // prevents division by zero
131 if ( cols() == 0 )
132 cols(1);
133 // clear view
134 data_.clear();
135 // set new data
136 data_ = d;
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 );
142 redraw();
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);
154 // background
155 fl_color( Fl::get_color( FL_BACKGROUND2_COLOR ) );
156 fl_rectf(X, Y, W, H);
157 // border
158 fl_color(FL_LIGHT2);
159 fl_rect(X, Y, W, H);
161 fl_pop_clip();
162 return;
165 Cell c = data_.at(cell);
166 fl_push_clip(X, Y, W, H);
168 // BG COLOR
169 fl_color(c.selected ? selection_color_:c.bgcolor );
170 fl_rectf(X, Y, W, H);
172 // TEXT
173 fl_font( font_, font_size_ );
174 fl_color( c.fgcolor );
175 fl_draw( c.str.c_str(), X+cell_padding_, Y+font_size_ );
177 // BORDER
178 fl_color(FL_LIGHT2);
179 fl_rect(X, Y, W, H);
181 fl_pop_clip();
185 void KanjiView::draw_cell ( TableContext context, int R, int C, int X, int Y, int W, int H )
187 switch ( context )
189 case CONTEXT_STARTPAGE:
190 fl_font( FL_HELVETICA, font_size());
191 return;
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());
198 fl_pop_clip();
199 return;
202 case CONTEXT_CELL:
204 draw_single_cell(R,C,X,Y,W,H);
205 return;
209 case CONTEXT_RC_RESIZE:
211 // append column
212 int n_cols = (w()-vscrollbar->w())/cell_size_;
213 int n_rows = ceil((float)data_.size()/n_cols);
214 if ( n_cols != cols() )
215 cols( n_cols );
216 if ( n_rows != rows() ){
217 rows(n_rows);
218 row_height_all(cell_size_);
220 return;
223 default:
224 return;
228 } // namespace