removed alert2(), DialogAlert
[aoi.git] / src / gui_dialogs.hxx
blobf9a353c5baad6fe65f06339841c014afae7b1881
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>
35 #include "logger.hxx"
36 #include "gui_widgets.hxx"
37 #include "datatypes.hxx"
38 #include "config.hxx"
39 #include "romanization.hxx"
42 using aoi_config::Config;
43 using aoi::Kanji;
45 namespace aoi_ui {
47 class DialogFontTest : public Fl_Double_Window
49 private:
50 Fl_Hold_Browser *browser_ = nullptr;
51 Fl_Box *l_fonts_ = nullptr;
52 Fl_Box *l_ascii_ = nullptr;
53 Fl_Box *l_jis208_ = nullptr;
54 Fl_Box *l_jis212_ = nullptr;
55 Fl_Box *l_jis213_ = nullptr;
56 Fl_Box *l_kana_ = nullptr;
57 Fl_Box *b_ascii_ = nullptr;
58 Fl_Box *b_jis208_ = nullptr;
59 Fl_Box *b_jis212_ = nullptr;
60 Fl_Box *b_jis213_ = nullptr;
61 Fl_Box *b_kana_ = nullptr;
62 Fl_Button *bt_ok_ = nullptr;
63 Fl_Button *bt_cancel_ = nullptr;
64 Fl_Input *input_ = nullptr;
65 string s_fonts_;
66 Fl_Font selected_font_ = -1;
68 public:
69 DialogFontTest( int w=600, int h=400 ) : Fl_Double_Window(w,h)
71 browser_ = new Fl_Hold_Browser( 5, 5, 230, 365 );
72 browser_->callback(static_callback, (void*)this);
73 l_fonts_ = new Fl_Box( 5, 370, 250, 30 );
74 l_fonts_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
76 l_ascii_ = new Fl_Box( 240, 5, 110, 40, "ASCII" );
77 l_ascii_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
78 l_ascii_->labelfont(FL_BOLD);
79 b_ascii_ = new Fl_Box( 355, 5, 240, 40, "Hello world" );
80 b_ascii_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
81 b_ascii_->labelsize(36);
83 l_kana_ = new Fl_Box( 240, 50, 110, 40, "Kana" );
84 l_kana_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
85 l_kana_->labelfont(FL_BOLD);
86 b_kana_ = new Fl_Box( 355, 50, 240, 40, "ひらーカタ." );
87 b_kana_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
88 b_kana_->labelsize(36);
90 l_jis208_ = new Fl_Box( 240, 95, 110, 40, "JIS X 208" );
91 l_jis208_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
92 l_jis208_->labelfont(FL_BOLD);
93 b_jis208_ = new Fl_Box( 355, 95, 240, 40, "龠堯槇遙凜熙");
94 b_jis208_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
95 b_jis208_->labelsize(36);
97 l_jis212_ = new Fl_Box( 240, 140, 110, 40, "JIS X 212" );
98 l_jis212_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
99 l_jis212_->labelfont(FL_BOLD);
100 b_jis212_ = new Fl_Box( 355, 140, 240, 40, "龑龒龔龖龗龞");
101 b_jis212_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
102 b_jis212_->labelsize(36);
105 l_jis213_ = new Fl_Box( 240, 185, 110, 40, "JIS X 213" );
106 l_jis213_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
107 l_jis213_->labelfont(FL_BOLD);
108 b_jis213_ = new Fl_Box( 355, 185, 240, 40, "臭艹艹著贈辶");
109 b_jis213_->align( FL_ALIGN_INSIDE|FL_ALIGN_LEFT);
110 b_jis213_->labelsize(36);
112 input_ = new Fl_Input( 240, 270, 350, 40, "Custom text" );
113 input_->align( FL_ALIGN_TOP_LEFT );
114 input_->labelfont( FL_HELVETICA|FL_BOLD );
116 bt_ok_ = new Fl_Button( w-70, h-35, 65, 30, "OK");
117 bt_ok_->callback( static_ok, (void*)this );
118 bt_cancel_ = new Fl_Button( w-140, h-35, 65, 30, "Cancel");
119 bt_cancel_->callback( static_cancel, (void*)this );
121 #ifdef WINDOWS
122 Fl_Font n_fonts = Fl::set_fonts("*");
123 #else
124 Fl_Font n_fonts = Fl::set_fonts("-*-*-*-*-*-*-*-*-*-*-*-*-iso10646-1");
125 #endif
127 s_fonts_ = std::to_string(n_fonts) + string(" fonts found");
128 l_fonts_->label(s_fonts_.c_str());
130 for ( int i=0; i<n_fonts; ++i ){
131 const char *name = Fl::get_font_name(i);
132 browser_->add( name, (void*)i );
135 // hide some fonts
136 browser_->hide( FL_SYMBOL+1 ); // protected for Help_View
137 // browser_->hide( FONT_KANJI+1 ); // reserved for program
139 label("Fonts");
140 hide();
143 void cb ( Fl_Font f ){
144 b_ascii_->labelfont(f);
145 b_kana_->labelfont(f);
146 b_jis208_->labelfont(f);
147 b_jis212_->labelfont(f);
148 b_jis213_->labelfont(f);
149 input_->textfont(f);
150 redraw();
153 void cb_ok () {
154 selected_font_ = browser_->value()-1;
155 hide();
158 void cb_cancel () {
159 selected_font_ = -1;
160 hide();
163 int run (){
164 show();
165 while ( visible() ){
166 usleep(1000);
167 Fl::check();
169 return selected_font();
172 inline Fl_Font selected_font() const { return selected_font_; };
173 inline static void static_callback ( Fl_Widget *w, void *p )
174 { ((DialogFontTest*)p)->cb( ((Fl_Hold_Browser*)w)->value()-1 ); };
175 inline static void static_ok ( Fl_Widget *w, void *p )
176 { ((DialogFontTest*)p)->cb_ok(); }
177 inline static void static_cancel ( Fl_Widget *w, void *p )
178 { ((DialogFontTest*)p)->cb_cancel(); };
183 class DialogDownload : public Fl_Double_Window
185 private:
186 Fl_Progress *progress_ = nullptr;
187 Fl_Button *b_abort_ = nullptr;
188 Fl_Button *b_url_ = nullptr;
189 Fl_Button *b_fname_ = nullptr;
190 Fl_Button *b_dload_ = nullptr;
191 Fl_Box *l_url_ = nullptr;
192 Fl_Box *l_fname_ = nullptr;
193 string url_ = "";
194 string fname_ = "";
195 static bool abort_download_;
196 bool downloading_ = false;
198 public:
199 DialogDownload( int w=400, int h=125 );
200 ~DialogDownload();
202 static size_t mycurl_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
204 size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
205 return written;
207 static size_t mycurl_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
208 { return fread(ptr, size, nmemb, stream); }
210 static int mycurl_progress_func(void *progress_widget,
211 double dltotal, double dlnow,
212 double ultotal, double ulnow )
214 float perc = 100.*dlnow/dltotal;
215 char buff[128];
216 sprintf(buff, "%.1f%% of %.1f MB", perc, dltotal/1024/1024);
217 ((Fl_Progress*)progress_widget)->label( buff );
218 ((Fl_Progress*)progress_widget)->value(perc);
219 Fl::check();
220 // non-zero cancels download
221 return abort_download_;
224 void run (){
225 show();
226 while ( visible() ){
227 usleep(1000);
228 Fl::check();
232 void download_url( const char *url, const char *fname );
233 void set_names ( const string &url, const string &fname="" );
234 void abort ();
235 void cb_download ();
236 void cb_url_callback ();
237 void cb_fname_callback ();
239 inline static void scb_url_callback ( Fl_Widget *w, void *p ){
240 ((DialogDownload*)p)->cb_url_callback(); }
241 inline static void scb_fname_callback ( Fl_Widget *w, void *p ){
242 ((DialogDownload*)p)->cb_fname_callback(); }
243 inline static void scb_abort ( Fl_Widget *w, void *p ){
244 ((DialogDownload*)p)->abort(); }
245 inline static void scb_download ( Fl_Widget *w, void *p ){
246 ((DialogDownload*)p)->cb_download(); }
247 inline static void scb_close_window ( Fl_Widget *w, void *f ){
248 // dont close main window on ESC
249 if ( Fl::event() == FL_SHORTCUT && Fl::event_key() == FL_Escape )
250 return;
251 // dont close when download in progress
252 if ( ((DialogDownload*)f)->downloading_ ) {
253 fl_message("You must abort download before closing window." );
254 return;
256 ((DialogDownload*)w)->hide();
261 class EditWordDialog : public Fl_Double_Window
263 private:
265 public:
266 EditWordDialog ( int x, int y, int w, int h, const char *l="Edit word" );
267 ~EditWordDialog(){};
271 class ManageDBDialog : public Fl_Double_Window
273 private:
274 Fl_Button *b_close_ = nullptr;
275 Fl_Button *b_dload_ = nullptr;
276 Fl_Button *b_check_main_ = nullptr;
277 Fl_Button *b_check_user_ = nullptr;
278 Label *lver_main_ = nullptr;
279 Label *l_main_created_ = nullptr;
280 Label *lver_jmdict_ = nullptr;
281 Label *lver_kanjidic_ = nullptr;
282 Label *lver_kradfile_ = nullptr;
283 Label *lver_tatoeba_ = nullptr;
284 Label *l_checked_against_= nullptr;
286 public:
287 ManageDBDialog ( int w=400, int h=300, const char *l="Manage Database" );
288 ~ManageDBDialog(){};
290 // setters
291 inline void main_version ( const string &s )
292 { lver_main_->set(s); };
293 inline void main_created ( const string &s )
294 { l_main_created_->set(s); };
295 inline void main_ver_jmdict ( const string &s )
296 { lver_jmdict_->set(s); };
297 inline void main_ver_kanjidic ( const string &s )
298 { lver_kanjidic_->set(s); };
299 inline void main_ver_kradfile ( const string &s )
300 { lver_kradfile_->set(s); };
301 inline void main_ver_tatoeba ( const string &s )
302 { lver_tatoeba_->set(s); };
303 inline void user_checked_against ( const string &s )
304 { l_checked_against_->set(s); };
306 // static callbacks
307 inline static void scb_dload ( Fl_Widget *w, void *p )
308 { fl_alert("Nothing yet."); };
309 inline static void scb_check_user ( Fl_Widget *w, void *p )
310 { fl_alert("Nothing yet."); };
311 inline static void scb_check_main ( Fl_Widget *w, void *p )
312 { fl_alert("Nothing yet."); };
313 inline static void scb_close ( Fl_Widget *w, void *p )
314 { ((ManageDBDialog*)p)->hide(); };
318 class KanjiPopupWindow : public Fl_Menu_Window
320 private:
321 KanjiInfo *box_;
323 public:
324 KanjiPopupWindow ( int w, int h );
325 ~KanjiPopupWindow (){};
327 inline void font_a ( Fl_Font f, int s ) { box_->font_a( f, s ); }
328 inline void font_b ( Fl_Font f, int s ) { box_->font_b( f, s ); }
329 inline void set ( const Kanji &k ) { box_->set_data(k); }
331 int handle ( int event );
333 inline void popup() {
334 // XXX: after clear_border() take_focus() does not work
335 // clear_border();
336 // position at cursor
337 position(Fl::event_x_root(), Fl::event_y_root());
338 size( box_->w(), box_->h());
339 show();
340 take_focus();
345 class DialogProgress : public Fl_Double_Window
347 private:
348 string message_ = "";
349 Fl_Progress *progress_ = nullptr;
351 public:
352 DialogProgress( int w, int h ):Fl_Double_Window(w,h){
353 box( FL_BORDER_BOX );
354 progress_ = new Fl_Progress( 10, 10, w-20, 30 );
355 progress_->minimum(0);
356 progress_->maximum(1);
357 set_modal();
358 clear_border();
359 end();
361 ~DialogProgress(){}
363 void init () { message_ = ""; }
365 void progress ( float percent, const string &message="" ){
366 message_ = message;
367 progress_->label(message_.c_str());
368 progress_->value( percent/100. );
369 Fl::check();
374 class DialogSettings : public Fl_Double_Window
376 private:
377 Fl_Hold_Browser *browser_ = nullptr;
378 Fl_Button *b_ok_ = nullptr;
379 Fl_Button *b_defaults_ = nullptr;
380 std::map<string,Config::Value> data_;
381 std::map<string,Config::Value> defaults_;
383 public:
384 DialogSettings( int w=400, int h=500 );
385 ~DialogSettings(){};
386 int handle ( int event );
387 std::map<string,Config::Value> run ();
388 void set ( const std::map<string,Config::Value> &m, const std::map<string,Config::Value> &defaults={} );
389 inline void restore_defaults () {
390 data_ = defaults_;
391 hide();
393 void cb ( int line );
394 inline static void static_callback ( Fl_Widget *w, void *p )
395 { ((DialogSettings*)p)->cb( ((Fl_Hold_Browser*)w)->value()); };
396 inline static void static_ok ( Fl_Widget *w, void *p )
397 { ((DialogSettings*)p)->hide(); }
398 inline static void scb_restore_defaults ( Fl_Widget *w, void *p )
399 { ((DialogSettings*)p)->restore_defaults(); };
402 } // namespace aoi_ui
403 #endif // _GUI_DIALOGS_HXX