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
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>
33 #include <FL/fl_ask.H>
34 #include <FL/Fl_Help_View.H>
37 #include "gui_widgets.hxx"
38 #include "datatypes.hxx"
40 #include "romanization.hxx"
43 using aoi_config::Config
;
48 class DialogFontTest
: public Fl_Double_Window
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;
67 Fl_Font selected_font_
= -1;
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 );
123 Fl_Font n_fonts
= Fl::set_fonts("*");
125 Fl_Font n_fonts
= Fl::set_fonts("-*-*-*-*-*-*-*-*-*-*-*-*-iso10646-1");
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
);
137 browser_
->hide( FL_SYMBOL
+1 ); // protected for Help_View
138 // browser_->hide( FONT_KANJI+1 ); // reserved for program
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
);
155 selected_font_
= browser_
->value()-1;
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
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;
196 static bool abort_download_
;
197 bool downloading_
= false;
200 DialogDownload( int w
=400, int h
=125 );
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
);
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
;
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
);
221 // non-zero cancels download
222 return abort_download_
;
233 void download_url( const char *url
, const char *fname
);
234 void set_names ( const string
&url
, const string
&fname
="" );
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
)
252 // dont close when download in progress
253 if ( ((DialogDownload
*)f
)->downloading_
) {
254 fl_message("You must abort download before closing window." );
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
269 Fl_Button
*b_cancel_
= nullptr;
270 Fl_Help_View
*view_
= nullptr;
271 Label
*l_did_
= nullptr;
274 DialogEditWord ( int w
=400, int h
=500, const char *l
="Edit word" );
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(); };
290 class ManageDBDialog
: public Fl_Double_Window
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;
306 ManageDBDialog ( int w
=400, int h
=300, const char *l
="Manage Database" );
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
); };
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
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
355 // position at cursor
356 position(Fl::event_x_root(), Fl::event_y_root());
357 size( box_
->w(), box_
->h());
364 class DialogProgress
: public Fl_Double_Window
367 string message_
= "";
368 Fl_Progress
*progress_
= nullptr;
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);
382 void init () { message_
= ""; }
384 void progress ( float percent
, const string
&message
="" ){
386 progress_
->label(message_
.c_str());
387 progress_
->value( percent
/100. );
393 class DialogSettings
: public Fl_Double_Window
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_
;
403 DialogSettings( int w
=400, int h
=500 );
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 () {
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