editable system colors
[aoi.git] / src / gui_kanjiview.cxx
blobceb87ff1c4c31c2991e9624026cdac92c5587255
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 #ifdef DEBUG
24 // std::runtime_error is used only with DEBUG
25 #include <stdexcept>
26 #endif
28 using utils::split_string;
30 namespace aoi_ui {
32 KanjiView::KanjiView ( int x, int y, int w, int h, const char *l ): Fl_Table(x,y,w,h,l)
34 when( FL_WHEN_RELEASE|FL_WHEN_CHANGED );
35 table_box( FL_NO_BOX );
36 cb_leftclick( scb_general, nullptr );
37 cb_rightclick( scb_general, nullptr );
38 cb_select( scb_general, nullptr );
39 cols(1);
40 end();
44 int KanjiView::handle ( int event )
46 switch ( event ) {
47 case FL_PUSH:
49 take_focus();
50 if ( Fl::event_button() == FL_LEFT_MOUSE && Fl::belowmouse() == this ){
51 Cell *c = cell_at_xy(Fl::event_x()-x(), Fl::event_y()-y());
52 if ( c ){
53 if ( c->selected )
54 c->selected = false;
55 else {
56 clear_selection();
57 c->selected = true;
58 cb_leftclick(this);
60 redraw();
61 return 1;
64 else if ( Fl::event_button() == FL_RIGHT_MOUSE ) {
65 Cell *c = cell_at_xy(Fl::event_x()-x(), Fl::event_y()-y());
66 Fl_Menu_Item menu[] = {
67 { "Copy", 0, scb_copy, (void*)c->str.c_str() },
68 // TODO: copy all kanjidata
69 { 0 }
71 const Fl_Menu_Item *m = menu->popup(Fl::event_x(), Fl::event_y(), 0, 0, 0);
72 if ( m )
73 m->do_callback(0, m->user_data());
74 return(1); // (tells caller we handled this event)
77 case FL_RELEASE:
78 if ( Fl::event_button() == FL_RIGHT_MOUSE )
79 return(1); // (tells caller we handled this event)
80 default:
81 return Fl_Table::handle(event);
85 void KanjiView::font_size ( int s )
87 font_size_ = s;
88 fl_font( font_, font_size_ );
89 int x = fl_height() + 2*cell_padding_;
90 cell_size(x);
93 void KanjiView::set_data ( const vector<Cell> &d )
95 // prevents division by zero
96 if ( cols() == 0 )
97 cols(1);
98 // clear view
99 data_.clear();
100 // set new data
101 data_ = d;
102 int ncols = d.size()/cols();
103 rows( (ncols>1) ? ncols:1 );
104 row_height_all( cell_size_ );
105 col_width_all( cell_size_ );
106 draw_cell( CONTEXT_RC_RESIZE, 0, 0, 0, 0 );
107 redraw();
111 void KanjiView::draw_single_cell ( int R, int C, int X, int Y, int W, int H )
113 int cell = R*cols()+C;
115 // empty cells at the end of the last row
116 if ( cell >= int(data_.size()) ){
117 fl_push_clip(X, Y, W, H);
119 // background
120 fl_color( Fl::get_color( FL_BACKGROUND2_COLOR ) );
121 fl_rectf(X, Y, W, H);
122 // border
123 fl_color(FL_LIGHT2);
124 fl_rect(X, Y, W, H);
126 fl_pop_clip();
127 return;
130 Cell c = data_.at(cell);
131 fl_push_clip(X, Y, W, H);
133 // BG COLOR
134 fl_color(c.selected ? selection_color_:c.bgcolor );
135 fl_rectf(X, Y, W, H);
137 // TEXT
138 fl_font( font_, font_size_ );
139 fl_color( c.fgcolor );
140 fl_draw( c.str.c_str(), X+cell_padding_, Y+font_size_ );
142 // BORDER
143 fl_color(FL_LIGHT2);
144 fl_rect(X, Y, W, H);
146 fl_pop_clip();
150 void KanjiView::draw_cell ( TableContext context, int R, int C, int X, int Y, int W, int H )
152 switch ( context )
154 case CONTEXT_STARTPAGE:
155 fl_font( FL_HELVETICA, font_size());
156 return;
158 case CONTEXT_COL_HEADER:
159 fl_push_clip(X, Y, W, H);
161 fl_draw_box(FL_THIN_UP_BOX, X, Y, W, H, col_header_color());
163 fl_pop_clip();
164 return;
167 case CONTEXT_CELL:
169 draw_single_cell(R,C,X,Y,W,H);
170 return;
174 case CONTEXT_RC_RESIZE:
176 // append column
177 int n_cols = (w()-vscrollbar->w())/cell_size_;
178 int n_rows = ceil((float)data_.size()/n_cols);
179 if ( n_cols != cols() )
180 cols( n_cols );
181 if ( n_rows != rows() ){
182 rows(n_rows);
183 row_height_all(cell_size_);
185 return;
188 default:
189 return;
193 } // namespace