added Show word as a temp. proxy for EditWord
[aoi.git] / src / gui_dialogs.hxx
blob0a695365f1fb2f530914ed06fa1f5c60f905f05a
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 _GUI_DIALOGS_HXX
18 #define _GUI_DIALOGS_HXX
20 #include <cmath>
21 #include <curl/curl.h>
22 #include <FL/Fl_Double_Window.H>
23 #include <FL/Fl_Text_Display.H>
24 #include <FL/Fl_Text_Buffer.H>
25 #include <FL/Fl_Menu_Window.H>
26 #include <FL/Fl_Menu_Button.H>
27 #include <FL/Fl_Progress.H>
28 #include <FL/Fl_Choice.H>
29 #include <FL/Fl_Box.H>
30 #include <FL/Fl_Button.H>
31 #include <FL/Fl_Hold_Browser.H>
32 #include <FL/Fl.H>
33 #include <FL/fl_ask.H>
34 #include <FL/Fl_Help_View.H>
36 #include "logger.hxx"
37 #include "gui_widgets.hxx"
38 #include "datatypes.hxx"
39 #include "config.hxx"
40 #include "romanization.hxx"
43 using aoi_config::Config;
44 using aoi::Kanji;
46 namespace aoi_ui {
48 class DialogFontTest : public Fl_Double_Window
50 private:
51 Fl_Hold_Browser *browser_ = nullptr;
52 Fl_Box *l_fonts_ = nullptr;
53 Fl_Box *l_ascii_ = nullptr;
54 Fl_Box *l_jis208_ = nullptr;
55 Fl_Box *l_jis212_ = nullptr;
56 Fl_Box *l_jis213_ = nullptr;
57 Fl_Box *l_kana_ = nullptr;
58 Fl_Box *b_ascii_ = nullptr;
59 Fl_Box *b_jis208_ = nullptr;
60 Fl_Box *b_jis212_ = nullptr;
61 Fl_Box *b_jis213_ = nullptr;
62 Fl_Box *b_kana_ = nullptr;
63 Fl_Button *bt_ok_ = nullptr;
64 Fl_Button *bt_cancel_ = nullptr;
65 Fl_Input *input_ = nullptr;
66 string s_fonts_;
67 Fl_Font selected_font_ = -1;
69 public:
70 DialogFontTest( int w=600, int h=400 ) : Fl_Double_Window(w,h)
72 browser_ = new Fl_Hold_Browser( 5, 5, 230, 365 );
73 browser_->callback(static_callback, (void*)this);
74 l_fonts_ = new Fl_Box( 5, 370, 250, 30 );
75 l_fonts_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
77 l_ascii_ = new Fl_Box( 240, 5, 110, 40, "ASCII" );
78 l_ascii_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
79 l_ascii_->labelfont(FL_BOLD);
80 b_ascii_ = new Fl_Box( 355, 5, 240, 40, "Hello world" );
81 b_ascii_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
82 b_ascii_->labelsize(36);
84 l_kana_ = new Fl_Box( 240, 50, 110, 40, "Kana" );
85 l_kana_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
86 l_kana_->labelfont(FL_BOLD);
87 b_kana_ = new Fl_Box( 355, 50, 240, 40, "ひらーカタ." );
88 b_kana_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
89 b_kana_->labelsize(36);
91 l_jis208_ = new Fl_Box( 240, 95, 110, 40, "JIS X 208" );
92 l_jis208_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
93 l_jis208_->labelfont(FL_BOLD);
94 b_jis208_ = new Fl_Box( 355, 95, 240, 40, "龠堯槇遙凜熙");
95 b_jis208_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
96 b_jis208_->labelsize(36);
98 l_jis212_ = new Fl_Box( 240, 140, 110, 40, "JIS X 212" );
99 l_jis212_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
100 l_jis212_->labelfont(FL_BOLD);
101 b_jis212_ = new Fl_Box( 355, 140, 240, 40, "龑龒龔龖龗龞");
102 b_jis212_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
103 b_jis212_->labelsize(36);
106 l_jis213_ = new Fl_Box( 240, 185, 110, 40, "JIS X 213" );
107 l_jis213_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
108 l_jis213_->labelfont(FL_BOLD);
109 b_jis213_ = new Fl_Box( 355, 185, 240, 40, "臭艹艹著贈辶");
110 b_jis213_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
111 b_jis213_->labelsize(36);
113 input_ = new Fl_Input( 240, 270, 350, 40, "Custom text" );
114 input_->align( FL_ALIGN_TOP_LEFT );
115 input_->labelfont( FL_HELVETICA|FL_BOLD );
117 bt_ok_ = new Fl_Button( w-70, h-35, 65, 30, "OK");
118 bt_ok_->callback( static_ok, (void*)this );
119 bt_cancel_ = new Fl_Button( w-140, h-35, 65, 30, "Cancel");
120 bt_cancel_->callback( static_cancel, (void*)this );
122 #ifdef WINDOWS
123 Fl_Font n_fonts = Fl::set_fonts("*");
124 #else
125 Fl_Font n_fonts = Fl::set_fonts("-*-*-*-*-*-*-*-*-*-*-*-*-iso10646-1");
126 #endif
128 s_fonts_ = std::to_string(n_fonts) + string(" fonts found");
129 l_fonts_->label(s_fonts_.c_str());
131 for ( int i=0; i<n_fonts; ++i ){
132 const char *name = Fl::get_font_name(i);
133 browser_->add( name, (void*)i );
136 // hide some fonts
137 browser_->hide( FL_SYMBOL+1 ); // protected for Help_View
138 // browser_->hide( FONT_KANJI+1 ); // reserved for program
140 label("Fonts");
141 hide();
144 void cb ( Fl_Font f ){
145 b_ascii_->labelfont(f);
146 b_kana_->labelfont(f);
147 b_jis208_->labelfont(f);
148 b_jis212_->labelfont(f);
149 b_jis213_->labelfont(f);
150 input_->textfont(f);
151 redraw();
154 void cb_ok () {
155 selected_font_ = browser_->value()-1;
156 hide();
159 void cb_cancel () {
160 selected_font_ = -1;
161 hide();
164 int run (){
165 show();
166 while ( visible() ){
167 usleep(1000);
168 Fl::check();
170 return selected_font();
173 inline Fl_Font selected_font() const { return selected_font_; };
174 inline static void static_callback ( Fl_Widget *w, void *p )
175 { ((DialogFontTest*)p)->cb( ((Fl_Hold_Browser*)w)->value()-1 ); };
176 inline static void static_ok ( Fl_Widget *w, void *p )
177 { ((DialogFontTest*)p)->cb_ok(); }
178 inline static void static_cancel ( Fl_Widget *w, void *p )
179 { ((DialogFontTest*)p)->cb_cancel(); };
184 class DialogDownload : public Fl_Double_Window
186 private:
187 Fl_Progress *progress_ = nullptr;
188 Fl_Button *b_abort_ = nullptr;
189 Fl_Button *b_url_ = nullptr;
190 Fl_Button *b_fname_ = nullptr;
191 Fl_Button *b_dload_ = nullptr;
192 Fl_Box *l_url_ = nullptr;
193 Fl_Box *l_fname_ = nullptr;
194 string url_ = "";
195 string fname_ = "";
196 static bool abort_download_;
197 bool downloading_ = false;
199 public:
200 DialogDownload( int w=400, int h=125 );
201 ~DialogDownload();
203 static size_t mycurl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
205 size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
206 return written;
208 static size_t mycurl_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
209 { return fread(ptr, size, nmemb, stream); }
211 static int mycurl_progress_func(void *progress_widget,
212 double dltotal, double dlnow,
213 double ultotal, double ulnow )
215 float perc = 100.*dlnow/dltotal;
216 char buff[128];
217 sprintf(buff, "%.1f%% of %.1f MB", perc, dltotal/1024/1024);
218 ((Fl_Progress*)progress_widget)->label( buff );
219 ((Fl_Progress*)progress_widget)->value(perc);
220 Fl::check();
221 // non-zero cancels download
222 return abort_download_;
225 void run (){
226 show();
227 while ( visible() ){
228 usleep(1000);
229 Fl::check();
233 void download_url( const char *url, const char *fname );
234 void set_names ( const string &url, const string &fname="" );
235 void abort ();
236 void cb_download ();
237 void cb_url_callback ();
238 void cb_fname_callback ();
240 inline static void scb_url_callback ( Fl_Widget *w, void *p ){
241 ((DialogDownload*)p)->cb_url_callback(); }
242 inline static void scb_fname_callback ( Fl_Widget *w, void *p ){
243 ((DialogDownload*)p)->cb_fname_callback(); }
244 inline static void scb_abort ( Fl_Widget *w, void *p ){
245 ((DialogDownload*)p)->abort(); }
246 inline static void scb_download ( Fl_Widget *w, void *p ){
247 ((DialogDownload*)p)->cb_download(); }
248 inline static void scb_close_window ( Fl_Widget *w, void *f ){
249 // dont close main window on ESC
250 if ( Fl::event() == FL_SHORTCUT && Fl::event_key() == FL_Escape )
251 return;
252 // dont close when download in progress
253 if ( ((DialogDownload*)f)->downloading_ ) {
254 fl_message("You must abort download before closing window." );
255 return;
257 ((DialogDownload*)w)->hide();
263 * Shows all known informations about word.
264 * \todo change to true edit word dialog
266 class DialogEditWord : public Fl_Double_Window
268 private:
269 Fl_Button *b_cancel_ = nullptr;
270 Fl_Help_View *view_ = nullptr;
271 Label *l_did_ = nullptr;
273 public:
274 DialogEditWord ( int w=400, int h=500, const char *l="Edit word" );
275 ~DialogEditWord(){};
277 inline void set ( const aoi::DicWord &w ){
278 l_did_->set( std::to_string(w.did()) );
279 view_->value( w.html().c_str() );
282 inline static void scb_cancel ( Fl_Widget *w, void *p )
283 { ((DialogEditWord*)p)->hide(); };
285 // run();
290 class ManageDBDialog : public Fl_Double_Window
292 private:
293 Fl_Button *b_close_ = nullptr;
294 Fl_Button *b_dload_ = nullptr;
295 Fl_Button *b_check_main_ = nullptr;
296 Fl_Button *b_check_user_ = nullptr;
297 Label *lver_main_ = nullptr;
298 Label *l_main_created_ = nullptr;
299 Label *lver_jmdict_ = nullptr;
300 Label *lver_kanjidic_ = nullptr;
301 Label *lver_kradfile_ = nullptr;
302 Label *lver_tatoeba_ = nullptr;
303 Label *l_checked_against_= nullptr;
305 public:
306 ManageDBDialog ( int w=400, int h=300, const char *l="Manage Database" );
307 ~ManageDBDialog(){};
309 // setters
310 inline void main_version ( const string &s )
311 { lver_main_->set(s); };
312 inline void main_created ( const string &s )
313 { l_main_created_->set(s); };
314 inline void main_ver_jmdict ( const string &s )
315 { lver_jmdict_->set(s); };
316 inline void main_ver_kanjidic ( const string &s )
317 { lver_kanjidic_->set(s); };
318 inline void main_ver_kradfile ( const string &s )
319 { lver_kradfile_->set(s); };
320 inline void main_ver_tatoeba ( const string &s )
321 { lver_tatoeba_->set(s); };
322 inline void user_checked_against ( const string &s )
323 { l_checked_against_->set(s); };
325 // static callbacks
326 inline static void scb_dload ( Fl_Widget *w, void *p )
327 { fl_alert("Nothing yet."); };
328 inline static void scb_check_user ( Fl_Widget *w, void *p )
329 { fl_alert("Nothing yet."); };
330 inline static void scb_check_main ( Fl_Widget *w, void *p )
331 { fl_alert("Nothing yet."); };
332 inline static void scb_close ( Fl_Widget *w, void *p )
333 { ((ManageDBDialog*)p)->hide(); };
337 class KanjiPopupWindow : public Fl_Menu_Window
339 private:
340 KanjiInfo *box_;
342 public:
343 KanjiPopupWindow ( int w, int h );
344 ~KanjiPopupWindow (){};
346 inline void font_a ( Fl_Font f, int s ) { box_->font_a( f, s ); }
347 inline void font_b ( Fl_Font f, int s ) { box_->font_b( f, s ); }
348 inline void set ( const Kanji &k ) { box_->set_data(k); }
350 int handle ( int event );
352 inline void popup() {
353 // XXX: after clear_border() take_focus() does not work
354 // clear_border();
355 // position at cursor
356 position(Fl::event_x_root(), Fl::event_y_root());
357 size( box_->w(), box_->h());
358 show();
359 take_focus();
364 class DialogProgress : public Fl_Double_Window
366 private:
367 string message_ = "";
368 Fl_Progress *progress_ = nullptr;
370 public:
371 DialogProgress( int w, int h ):Fl_Double_Window(w,h){
372 box( FL_BORDER_BOX );
373 progress_ = new Fl_Progress( 10, 10, w-20, 30 );
374 progress_->minimum(0);
375 progress_->maximum(1);
376 set_modal();
377 clear_border();
378 end();
380 ~DialogProgress(){}
382 void init () { message_ = ""; }
384 void progress ( float percent, const string &message="" ){
385 message_ = message;
386 progress_->label(message_.c_str());
387 progress_->value( percent/100. );
388 Fl::check();
393 class DialogSettings : public Fl_Double_Window
395 private:
396 Fl_Hold_Browser *browser_ = nullptr;
397 Fl_Button *b_ok_ = nullptr;
398 Fl_Button *b_defaults_ = nullptr;
399 std::map<string,Config::Value> data_;
400 std::map<string,Config::Value> defaults_;
402 public:
403 DialogSettings( int w=400, int h=500 );
404 ~DialogSettings(){};
405 int handle ( int event );
406 std::map<string,Config::Value> run ();
407 void set ( const std::map<string,Config::Value> &m, const std::map<string,Config::Value> &defaults={} );
408 inline void restore_defaults () {
409 data_ = defaults_;
410 hide();
412 void cb ( int line );
413 inline static void static_callback ( Fl_Widget *w, void *p )
414 { ((DialogSettings*)p)->cb( ((Fl_Hold_Browser*)w)->value()); };
415 inline static void static_ok ( Fl_Widget *w, void *p )
416 { ((DialogSettings*)p)->hide(); }
417 inline static void scb_restore_defaults ( Fl_Widget *w, void *p )
418 { ((DialogSettings*)p)->restore_defaults(); };
421 } // namespace aoi_ui
422 #endif // _GUI_DIALOGS_HXX