editable system colors
[aoi.git] / src / gui_kanjiview.hxx
blobc216204c1adba5f34e8c79657ad1d22ba1d08903
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 Cell *cell_at_rc ( int R, int C ){
87 int ret = R*cols()+C;
88 if ( R == -1 || C == -1 || ret > int(data_.size())-1 )
89 return nullptr;
90 return &data_[ret];
93 inline Cell *cell_at_xy ( int x, int y )
95 int R = -1;
96 int C = -1;
97 int w = 0;
98 int h = 0;
99 // check rows
100 for ( int r=0; r < rows(); r++ ){
101 int nh = h + row_height(r);
102 if ( y>h && y<nh ){
103 R = r;
104 break;
106 h = nh;
108 // check columns
109 for ( int c=0; c < cols(); c++ ){
110 int nw = w + col_width(c);
111 if ( x>w && x<nw ) {
112 C = c;
113 break;
115 w = nw;
117 int ret = R*cols()+C;
118 if ( R == -1 || C == -1 || ret > int(data_.size())-1 )
119 return nullptr;
120 return &data_[ret];
124 // GETTERS
125 inline Cell *selected () {
126 for ( Cell &c: data_ )
127 if ( c.selected )
128 return &c;
129 return nullptr;
131 inline int font_size () const { return font_size_;};
132 inline Fl_Color selection_color () const { return selection_color_; };
133 inline int cell_padding () const { return cell_padding_; };
134 inline int cell_size () const { return cell_size_; };
136 // SETTERS
137 void font_size ( int s );
138 inline void selection_color ( const Fl_Color &c ) { selection_color_ = c; };
139 inline void cell_padding ( int x ) { cell_padding_ = x; };
140 inline void cell_size ( int w=-1 ) {
141 if ( w<1 ) return;
142 cell_size_ = w;
143 col_width_all(w);
144 row_height_all(w);
147 int handle ( int event );
149 // copy to clipboard
150 static void scb_copy ( Fl_Widget *w, void *userdata ) {
151 const char *d = (const char*)userdata;
152 int len = strlen(d);
153 Fl::copy( d, len, 0);
154 Fl::copy( d, len, 1);
157 // CALLBACKS - "setters"
158 inline void cb_leftclick ( Fl_Callback *cb, void *f )
159 { cb_leftclick_ = cb; cb_leftclick_data_ = f; };
160 inline void cb_rightclick ( Fl_Callback *cb, void *f )
161 { cb_rightclick_ = cb; cb_rightclick_data_ = f; };
162 inline void cb_select ( Fl_Callback *cb, void *f )
163 { cb_select_ = cb; cb_select_data_ = f; };
165 // CALLBACKS - "getters"
166 // cb_XXXclick_data_ should be pointer to App
167 inline void cb_leftclick ( Fl_Widget *w ) const
168 { cb_leftclick_(w,cb_leftclick_data_); };
169 inline void cb_rightclick ( Fl_Widget *w ) const
170 { cb_rightclick_(w, cb_rightclick_data_); };
171 inline void cb_select ( Fl_Widget *w ) const
172 { cb_select_(w,cb_select_data_); }
174 // default callback
175 inline static void scb_general ( Fl_Widget *w, void *f ) {};
178 } //namespace aoi_ui
179 #endif // _KANJIVIEW_HXX