keyboard navigation in kanjiview
[aoi.git] / src / gui_kanjiview.hxx
blobee891a5ab3a5ab218412920d30ec00705f10629a
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 #ifndef _KANJIVIEW_HXX
18 #define _KANJIVIEW_HXX
20 #include <FL/Fl_Table.H>
21 #include <FL/fl_draw.H>
22 #include <map>
23 #include <string>
24 #include <vector>
26 #include "gui_settings.hxx"
29 PREDEFINED TAGS:
30 <br>
31 <sep>
32 <default>
34 WARNING:
35 FL_FLAT_BOX causes errors in redrawing
38 namespace aoi_ui {
40 using std::vector;
41 using std::string;
43 class KanjiView : public Fl_Table
45 public:
46 struct Cell {
47 public:
48 string str;
49 bool selected = false;
50 Fl_Color fgcolor;
51 Fl_Color bgcolor;
52 Cell ( const string &s, Fl_Color fg=-1, Fl_Color bg=-1 ) : str(s)
54 fgcolor = ( fg == -1 ) ? Fl::get_color( FL_FOREGROUND_COLOR ) : fg;
55 bgcolor = ( bg == -1 ) ? Fl::get_color( FL_BACKGROUND2_COLOR ) : bg;
59 private:
60 Fl_Font font_ = FONT_KANJI;
61 int font_size_ = 12;
62 Fl_Color selection_color_ = FL_LIGHT2;
63 int cell_padding_ = 5;
64 int cell_size_ = 15;
66 Fl_Callback *cb_leftclick_;
67 Fl_Callback *cb_rightclick_;
68 Fl_Callback *cb_select_;
69 void *cb_leftclick_data_ = nullptr;
70 void *cb_rightclick_data_ = nullptr;
71 void *cb_select_data_ = nullptr;
72 vector<Cell> data_;
74 inline void clear_selection () { for ( Cell &c: data_ ) c.selected=false; redraw(); }
75 void draw_single_cell ( int R, int C, int X, int Y, int W, int H );
76 void draw_cell ( TableContext context, int R=0, int C=0, int X=0, int Y=0,
77 int W=0, int H=0 );
79 public:
80 KanjiView(int x, int y, int w, int h, const char *l=0 );
81 ~KanjiView(){};
84 void set_data ( const vector<Cell> &d );
86 inline void select_cell ( int id ){
87 clear_selection();
88 data_.at(id).selected = true;
89 redraw();
92 inline Cell *cell_at_rc ( int R, int C ){
93 int ret = R*cols()+C;
94 if ( R == -1 || C == -1 || ret > int(data_.size())-1 )
95 return nullptr;
96 return &data_[ret];
99 inline Cell *cell_at_xy ( int x, int y )
101 int R = -1;
102 int C = -1;
103 int w = 0;
104 int h = 0;
105 // check rows
106 for ( int r=0; r < rows(); r++ ){
107 int nh = h + row_height(r);
108 if ( y>h && y<nh ){
109 R = r;
110 break;
112 h = nh;
114 // check columns
115 for ( int c=0; c < cols(); c++ ){
116 int nw = w + col_width(c);
117 if ( x>w && x<nw ) {
118 C = c;
119 break;
121 w = nw;
123 int ret = R*cols()+C;
124 if ( R == -1 || C == -1 || ret > int(data_.size())-1 )
125 return nullptr;
126 return &data_[ret];
130 // GETTERS
131 inline Cell *selected () {
132 for ( Cell &c: data_ )
133 if ( c.selected )
134 return &c;
135 return nullptr;
137 inline int font_size () const { return font_size_;};
138 inline Fl_Color selection_color () const { return selection_color_; };
139 inline int cell_padding () const { return cell_padding_; };
140 inline int cell_size () const { return cell_size_; };
141 inline int selected_id () const {
142 for ( int i=0; i<data_.size(); i++)
143 if ( data_.at(i).selected )
144 return i;
145 return -1;
148 // SETTERS
149 void font_size ( int s );
150 inline void selection_color ( const Fl_Color &c ) { selection_color_ = c; };
151 inline void cell_padding ( int x ) { cell_padding_ = x; };
152 inline void cell_size ( int w=-1 ) {
153 if ( w<1 ) return;
154 cell_size_ = w;
155 col_width_all(w);
156 row_height_all(w);
159 int handle ( int event );
161 // copy to clipboard
162 static void scb_copy ( Fl_Widget *w, void *userdata ) {
163 const char *d = (const char*)userdata;
164 int len = strlen(d);
165 Fl::copy( d, len, 0);
166 Fl::copy( d, len, 1);
169 // CALLBACKS - "setters"
170 inline void cb_leftclick ( Fl_Callback *cb, void *f )
171 { cb_leftclick_ = cb; cb_leftclick_data_ = f; };
172 inline void cb_rightclick ( Fl_Callback *cb, void *f )
173 { cb_rightclick_ = cb; cb_rightclick_data_ = f; };
174 inline void cb_select ( Fl_Callback *cb, void *f )
175 { cb_select_ = cb; cb_select_data_ = f; };
177 // CALLBACKS - "getters"
178 // cb_XXXclick_data_ should be pointer to App
179 inline void cb_leftclick ( Fl_Widget *w ) const
180 { cb_leftclick_(w,cb_leftclick_data_); };
181 inline void cb_rightclick ( Fl_Widget *w ) const
182 { cb_rightclick_(w, cb_rightclick_data_); };
183 inline void cb_select ( Fl_Widget *w ) const
184 { cb_select_(w,cb_select_data_); }
186 // default callback
187 inline static void scb_general ( Fl_Widget *w, void *f ) {};
190 } //namespace aoi_ui
191 #endif // _KANJIVIEW_HXX