Merge branch 'devel'
[aoi.git] / src / gui_dicview.hxx
blob56d4d86074774f590c4e474ca29502c6133bfbee
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 _DICVIEW_HXX
18 #define _DICVIEW_HXX
20 #include <FL/Fl_Table_Row.H>
21 #include <FL/fl_draw.H>
22 #include <map>
23 #include <string>
24 #include <vector>
25 #include <stdexcept>
28 PREDEFINED TAGS:
29 <br>
30 <sep>
31 <default>
33 WARNING:
34 FL_FLAT_BOX causes errors in redrawing
37 namespace aoi_ui {
39 using std::vector;
40 using std::map;
41 using std::string;
43 struct TextStyle
45 enum Separator {
46 SEPARATOR_NONE,
47 SEPARATOR_SQUARE
50 enum NewlineStyle {
51 NEWLINE_NONE,
52 NEWLINE_AFTER
55 Fl_Font font;
56 float relsize;
57 Fl_Color color;
58 int offset_y;
59 Separator separator;
60 NewlineStyle newline;
62 TextStyle( Fl_Font fnt=FL_TIMES , float sz=1.0, Fl_Color clr=FL_BLACK, int offy=0,
63 Separator sep=SEPARATOR_NONE, NewlineStyle nl=NEWLINE_NONE)
64 :font(fnt),relsize(sz),color(clr), offset_y(offy), separator(sep),newline(nl){};
66 TextStyle( Separator sep ): TextStyle() { separator=sep; };
67 TextStyle( NewlineStyle nl ): TextStyle() { newline=nl; };
71 class DicView : public Fl_Table_Row
73 private:
74 int font_size_ = 12;
75 int cell_padding_x_ = 5;
76 int cell_padding_y_ = 5;
77 map<string,TextStyle> tag_table_; // tag_name:style
78 vector<string> headers_;
80 void redraw_row ( int R, int H );
82 protected:
83 Fl_Callback *cb_leftclick_;
84 Fl_Callback *cb_rightclick_;
85 Fl_Callback *cb_doubleclick_;
86 Fl_Callback *cb_select_;
87 void *cb_leftclick_data_ = nullptr;
88 void *cb_rightclick_data_ = nullptr;
89 void *cb_doubleclick_data_ = nullptr;
90 void *cb_select_data_ = nullptr;
91 vector<int> cell_heights_;
92 vector<string> data_;
93 vector<int> ids_;
95 // returns cell height
96 int draw_single_cell ( int R, int C, int X, int Y, int W, int H );
97 void draw_cell ( TableContext context, int R=0, int C=0, int X=0, int Y=0,
98 int W=0, int H=0 );
100 public:
101 DicView(int x, int y, int w, int h, const char *l=0 );
102 ~DicView(){};
104 void set_data ( const vector<string> &d, const vector<int> &cell_ids={} );
106 inline void register_tag ( const string &name, const TextStyle &style )
107 { tag_table_[name] = style; };
109 inline TextStyle *get_style ( const string &tag = "default" ) const
111 auto mi = tag_table_.find(tag);
112 if ( mi != tag_table_.end() )
113 return const_cast<TextStyle*>(&mi->second);
114 return const_cast<TextStyle*>(&tag_table_.at("default"));
117 // copy to clipboard
118 static void scb_copy ( Fl_Widget *w, void *userdata ) {
119 const char *d = (const char*)userdata;
120 int len = strlen(d);
121 Fl::copy( d, len, 0);
122 Fl::copy( d, len, 1);
126 inline int cell_id ( int R, int C )
128 int n = R*cols()+C;
129 int ret;
130 try {
131 ret = ids_.at(n);
133 catch ( std::out_of_range ) {
134 ret = -1;
136 return ret;
139 inline int selected_row_id () { return cell_id( selected_row(), 0 );};
140 inline int row_id ( int R ) { return cell_id(R,0); };
142 inline int cell_at_xy ( int x, int y )
144 int R = -1;
145 int C = -1;
146 int w = 0;
147 int h = 0;
148 // check rows
149 for ( int r=0; r < rows(); r++ ){
150 int nh = h + row_height(r);
151 if ( y>h && y<nh ){
152 R = r;
153 break;
155 h = nh;
157 // check columns
158 for ( int c=0; c < cols(); c++ ){
159 int nw = w + col_width(c);
160 if ( x>w && x<nw ) {
161 C = c;
162 break;
164 w = nw;
166 int ret = R*cols()+C;
167 if ( R == -1 || C == -1 || ret > int(data_.size())-1 )
168 return -1;
169 return ret;
173 inline int row_at_y ( int y ){
174 int R = 0;
175 int C = 0;
176 ResizeFlag flag;
177 cursor2rowcol( R, C, flag );
178 return R;
182 int handle ( int event );
184 // GETTERS
185 inline int font_size () const { return font_size_;};
186 inline int cell_padding_x () const { return cell_padding_x_; };
187 inline int cell_padding_y () const { return cell_padding_y_; };
188 inline int selected_row () {
189 for ( int r=0; r<rows(); r++ )
190 if ( row_selected(r) )
191 return r;
192 return -1;
195 // SETTERS
196 inline void font_size ( int s ) { font_size_ = s; }
197 inline void cell_padding ( int x ) { cell_padding_x_ = x; cell_padding_y_=x;}
198 inline void cell_padding_x ( int x ) { cell_padding_x_ = x; };
199 inline void cell_padding_y ( int y ) { cell_padding_y_ = y; };
200 inline void headers ( const vector<string> &v ) { headers_=v; };
202 // CALLBACKS - "setters"
203 inline void cb_leftclick ( Fl_Callback *cb, void *f )
204 { cb_leftclick_ = cb; cb_leftclick_data_ = f; };
205 inline void cb_rightclick ( Fl_Callback *cb, void *f )
206 { cb_rightclick_ = cb; cb_rightclick_data_ = f; };
207 inline void cb_doubleclick( Fl_Callback *cb, void *f )
208 { cb_doubleclick_= cb; cb_doubleclick_data_ = f; };
209 inline void cb_select ( Fl_Callback *cb, void *f )
210 { cb_select_ = cb; cb_select_data_ = f; };
212 // CALLBACKS - "getters"
213 inline void cb_leftclick ( Fl_Widget *w ) const
214 { cb_leftclick_(w,cb_leftclick_data_); };
215 inline void cb_rightclick ( Fl_Widget *w ) const
216 { cb_rightclick_(w, cb_rightclick_data_); };
217 inline void cb_doubleclick( Fl_Widget *w ) const
218 { cb_doubleclick_(w, cb_doubleclick_data_ ); };
219 inline void cb_select ( Fl_Widget *w ) const
220 { cb_select_(w,cb_select_data_); }
222 // default callback
223 inline static void scb_general ( Fl_Widget *w, void *f ) {};
226 } //namespace aoi_ui
227 #endif // _DICVIEW_HXX