Dillo v3.1.1-98-g318d1f14
Loading...
Searching...
No Matches
dialog.cc
Go to the documentation of this file.
1/*
2 * File: dialog.cc
3 *
4 * Copyright (C) 2005-2007 Jorge Arellano Cid <jcid@dillo.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 */
11
16#include <math.h> // for rint()
17#include <string.h>
18
19#include <FL/fl_ask.H>
20#include <FL/Fl_Window.H>
21#include <FL/Fl_File_Chooser.H>
22#include <FL/Fl_Return_Button.H>
23#include <FL/Fl_Text_Display.H>
24#include <FL/Fl_Button.H>
25#include <FL/Fl_Return_Button.H>
26#include <FL/Fl_Output.H>
27#include <FL/Fl_Input.H>
28#include <FL/Fl_Secret_Input.H>
29#include <FL/Fl_Choice.H>
30#include <FL/Fl_Menu_Item.H>
31
32#include "msg.h"
33#include "dialog.hh"
34#include "misc.h"
35#include "prefs.h"
36#include "dlib/dlib.h"
37
38/*
39 * Local Data
40 */
41static int input_answer;
42static char *input_str = NULL;
43static int choice_answer;
44
45
46/*
47 * Local sub classes
48 */
49
50//----------------------------------------------------------------------------
55class CustInput3 : public Fl_Input {
56public:
57 CustInput3 (int x, int y, int w, int h, const char* l=0) :
58 Fl_Input(x,y,w,h,l) {};
59 int handle(int e);
60};
61
62int CustInput3::handle(int e)
63{
64 int k = Fl::event_key();
65
66 _MSG("CustInput3::handle event=%d\n", e);
67
68 // We're only interested in some flags
69 unsigned modifier = Fl::event_state() & (FL_SHIFT | FL_CTRL | FL_ALT);
70
71 if (e == FL_KEYBOARD && modifier == FL_CTRL) {
72 if (k == 'a' || k == 'e') {
73 position(k == 'a' ? 0 : size());
74 return 1;
75 } else if (k == 'k') {
76 cut(position(), size());
77 return 1;
78 } else if (k == 'd') {
79 cut(position(), position()+1);
80 return 1;
81 }
82 }
83 return Fl_Input::handle(e);
84}
85
89class CustChoice2 : public Fl_Choice {
90public:
91 CustChoice2 (int x, int y, int w, int h, const char* l=0) :
92 Fl_Choice(x,y,w,h,l) {};
93 int handle(int e) {
94 if (e == FL_KEYBOARD &&
95 (Fl::event_key() == FL_Enter || Fl::event_key() == FL_Down) &&
96 (Fl::event_state() & (FL_SHIFT|FL_CTRL|FL_ALT|FL_META)) == 0) {
97 return Fl_Choice::handle(FL_PUSH);
98 }
99 return Fl_Choice::handle(e);
100 };
101};
102
103class EnterButton : public Fl_Button {
104public:
105 EnterButton (int x,int y,int w,int h, const char* label = 0) :
106 Fl_Button (x,y,w,h,label) {};
107 int handle(int e);
108};
109
110int EnterButton::handle(int e)
111{
112 if (e == FL_KEYBOARD && Fl::focus() == this && Fl::event_key() == FL_Enter){
113 set_changed();
114 simulate_key_action();
115 do_callback();
116 return 1;
117 }
118 return Fl_Button::handle(e);
119}
120
121//----------------------------------------------------------------------------
122
123
127void a_Dialog_msg(const char *title, const char *msg)
128{
129 if (!(title && *title))
130 title = "Dillo: Message";
131 fl_message_title(title);
132 fl_message("%s", msg);
133}
134
135
139static void input_cb(Fl_Widget *button, void *number)
140{
141 input_answer = VOIDP2INT(number);
142 button->window()->hide();
143}
144
151const char *a_Dialog_input(const char *title, const char *msg)
152{
153 static Fl_Menu_Item *pm = 0;
154 int ww = 450, wh = 130, gap = 10, ih = 60, bw = 80, bh = 30;
155
156 input_answer = 0;
157
158 if (!(title && *title))
159 title = "Dillo: Input";
160
161 Fl_Window *window = new Fl_Window(ww,wh,title);
162 window->set_modal();
163 window->begin();
164 Fl_Group* ib = new Fl_Group(0,0,window->w(),window->h());
165 ib->begin();
166 window->resizable(ib);
167
168 /* '?' Icon */
169 Fl_Box* o = new Fl_Box(gap, gap, ih, ih);
170 o->box(FL_THIN_UP_BOX);
171 o->labelfont(FL_TIMES_BOLD);
172 o->labelsize(34);
173 o->label("?");
174 o->show();
175
176 Fl_Box *box = new Fl_Box(ih+2*gap,gap,ww-(ih+3*gap),ih/2, msg);
177 box->labelfont(FL_HELVETICA);
178 box->labelsize(14);
179 box->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE|FL_ALIGN_CLIP|FL_ALIGN_WRAP);
180
181 CustInput3 *c_inp = new CustInput3(ih+2*gap,gap+ih/2+gap,ww-(ih+3*gap),24);
182 c_inp->labelsize(14);
183 c_inp->textsize(14);
184
185 CustChoice2 *ch = new CustChoice2(1*gap,ih+3*gap,180,24);
186 if (!pm) {
187 int n_it = dList_length(prefs.search_urls);
188 pm = new Fl_Menu_Item[n_it+1];
189 memset(pm, '\0', (n_it + 1) * sizeof(Fl_Menu_Item));
190 for (int i = 0, j = 0; i < n_it; i++) {
191 char *label, *url, *source;
192 source = (char *)dList_nth_data(prefs.search_urls, i);
193 if (!source || a_Misc_parse_search_url(source, &label, &url) < 0)
194 continue;
195 pm[j++].label(FL_NORMAL_LABEL, dStrdup(label));
196 }
197 }
198 ch->tooltip("Select search engine");
199 ch->menu(pm);
200 ch->value(prefs.search_url_idx);
201
202 int xpos = ww-2*(gap+bw), ypos = ih+3*gap;
203 Fl_Return_Button *rb = new Fl_Return_Button(xpos, ypos, bw, bh, "OK");
204 rb->align(FL_ALIGN_INSIDE|FL_ALIGN_CLIP);
205 rb->box(FL_UP_BOX);
206 rb->callback(input_cb, INT2VOIDP(1));
207
208 xpos = ww-(gap+bw);
209 Fl_Button *b = new Fl_Button(xpos, ypos, bw, bh, "Cancel");
210 b->align(FL_ALIGN_INSIDE|FL_ALIGN_CLIP);
211 b->box(FL_UP_BOX);
212 b->callback(input_cb, INT2VOIDP(2));
213
214 window->end();
215
216 window->show();
217 while (window->shown())
218 Fl::wait();
219 if (input_answer == 1) {
220 /* we have a string, save it */
222 input_str = dStrdup(c_inp->value());
223 prefs.search_url_idx = ch->value();
224 }
225 delete window;
226
227 return (input_answer == 1) ? input_str : NULL;
228}
229
233const char *a_Dialog_passwd(const char *title, const char *msg)
234{
235 if (!(title && *title))
236 title = "Dillo: Password";
237 fl_message_title(title);
238 return fl_password("%s", "", msg);
239}
240
246const char *a_Dialog_save_file(const char *title,
247 const char *pattern, const char *fname)
248{
249 return fl_file_chooser(title, pattern, fname);
250}
251
257const char *a_Dialog_select_file(const char *title,
258 const char *pattern, const char *fname)
259{
260 /*
261 * FileChooser::type(MULTI) appears to allow multiple files to be selected,
262 * but just follow save_file's path for now.
263 */
264 return a_Dialog_save_file(title, pattern, fname);
265}
266
272char *a_Dialog_open_file(const char *title,
273 const char *pattern, const char *fname)
274{
275 const char *fc_name;
276
277 fc_name = fl_file_chooser(title, pattern, fname);
278 return (fc_name) ? a_Misc_escape_chars(fc_name, "% #") : NULL;
279}
280
284static void text_window_close_cb(Fl_Widget *, void *vtd)
285{
286 Fl_Text_Display *td = (Fl_Text_Display *)vtd;
287 Fl_Text_Buffer *buf = td->buffer();
288
289 delete (Fl_Window*)td->window();
290 delete buf;
291}
292
296void a_Dialog_text_window(const char *title, const char *txt)
297{
298 int wh = prefs.height, ww = prefs.width, bh = 30;
299
300 if (!(title && *title))
301 title = "Dillo: Text";
302
303 Fl_Window *window = new Fl_Window(ww, wh, title);
304 Fl_Group::current(0);
305
306
307 Fl_Text_Buffer *buf = new Fl_Text_Buffer();
308 buf->text(txt);
309 Fl_Text_Display *td = new Fl_Text_Display(0,0,ww, wh-bh);
310 td->buffer(buf);
311 td->textsize((int) rint(14.0 * prefs.font_factor));
312
313 /* enable wrapping lines; text uses entire width of window */
314 td->wrap_mode(Fl_Text_Display::WRAP_AT_BOUNDS, 0);
315 window->add(td);
316
317 Fl_Return_Button *b = new Fl_Return_Button (0, wh-bh, ww, bh, "Close");
318 b->callback(text_window_close_cb, td);
319 window->add(b);
320
321 window->callback(text_window_close_cb, td);
322 window->resizable(td);
323 window->show();
324}
325
326/*--------------------------------------------------------------------------*/
327
328static void choice_cb(Fl_Widget *button, void *number)
329{
330 choice_answer = VOIDP2INT(number);
331 _MSG("choice_cb: %d\n", choice_answer);
332
333 button->window()->hide();
334}
335
342int a_Dialog_choice(const char *title, const char *msg, ...)
343{
344 va_list ap;
345 int i, n;
346
347 if (title == NULL || *title == '\0')
348 title = "Dillo: Choice";
349
350 va_start(ap, msg);
351 for (n = 0; va_arg(ap, char *) != NULL; n++);
352 va_end(ap);
353
354 if (n == 0) {
355 MSG_ERR("Dialog_choice: no alternatives.\n");
356 return 0;
357 }
358
359 int gap = 8;
360 int ww = 140 + n * 60, wh = 120;
361 int bw = (ww - gap) / n - gap, bh = 45;
362
363 Fl_Window *window = new Fl_Window(ww, wh, title);
364 window->set_modal();
365 window->begin();
366
367 Fl_Text_Buffer *buf = new Fl_Text_Buffer();
368 buf->text(msg);
369 Fl_Text_Display *td = new Fl_Text_Display(0, 0, ww, wh - bh);
370 td->buffer(buf);
371 td->textsize((int) rint(14.0 * prefs.font_factor));
372 td->wrap_mode(Fl_Text_Display::WRAP_AT_BOUNDS, 0);
373
374 window->resizable(td);
375
376 int xpos = gap;
377 va_start(ap, msg);
378 for (i = 1; i <= n; i++) {
379 Fl_Button *b = new EnterButton(xpos, wh-bh, bw, bh, va_arg(ap, char *));
380 b->align(FL_ALIGN_WRAP | FL_ALIGN_CLIP);
381 b->box(FL_UP_BOX);
382 b->callback(choice_cb, INT2VOIDP(i));
383 xpos += bw + gap;
384 /* TODO: set focus to the *-prefixed alternative */
385 }
386 va_end(ap);
387 window->end();
388
389 choice_answer = 0;
390
391 window->show();
392 while (window->shown())
393 Fl::wait();
394 _MSG("Dialog_choice answer = %d\n", answer);
395 td->buffer(NULL);
396 delete buf;
397 delete window;
398
399 return choice_answer;
400}
401
402/*--------------------------------------------------------------------------*/
403static void Dialog_user_password_cb(Fl_Widget *button, void *)
404{
405 button->window()->user_data(button);
406 button->window()->hide();
407}
408
414int a_Dialog_user_password(const char *title, const char *msg,
415 UserPasswordCB cb, void *vp)
416{
417 int ok = 0, window_h = 280, y, msg_w, msg_h;
418 const int window_w = 300, input_x = 80, input_w = 200, input_h = 30,
419 button_h = 30;
420
421 /* window is resized below */
422 if (!(title && *title))
423 title = "Dillo: User/Password";
424 Fl_Window *window = new Fl_Window(window_w,window_h,title);
425 Fl_Group::current(0);
426 window->user_data(NULL);
427
428 /* message */
429 y = 20;
430 msg_w = window_w - 40;
431 Fl_Box *msg_box = new Fl_Box(20, y, msg_w, 100); /* resized below */
432 msg_box->label(msg);
433 msg_box->labelfont(FL_HELVETICA);
434 msg_box->labelsize(14);
435 msg_box->align(FL_ALIGN_INSIDE | FL_ALIGN_TOP_LEFT | FL_ALIGN_WRAP);
436
437 fl_font(msg_box->labelfont(), msg_box->labelsize());
438 msg_w -= 6; /* The label doesn't fill the entire box. */
439 fl_measure(msg_box->label(), msg_w, msg_h, 0); // fl_measure wraps at msg_w
440 msg_box->size(msg_box->w(), msg_h);
441 window->add(msg_box);
442
443 /* inputs */
444 y += msg_h + 20;
445 Fl_Input *user_input = new Fl_Input(input_x, y, input_w, input_h, "User");
446 user_input->labelsize(14);
447 user_input->textsize(14);
448 window->add(user_input);
449 y += input_h + 10;
450 Fl_Secret_Input *password_input =
451 new Fl_Secret_Input(input_x, y, input_w, input_h, "Password");
452 password_input->labelsize(14);
453 password_input->textsize(14);
454 window->add(password_input);
455
456 /* "OK" button */
457 y += input_h + 20;
458 Fl_Button *ok_button = new EnterButton(200, y, 50, button_h, "OK");
459 ok_button->labelsize(14);
460 ok_button->callback(Dialog_user_password_cb);
461 window->add(ok_button);
462
463 /* "Cancel" button */
464 Fl_Button *cancel_button =
465 new EnterButton(50, y, 100, button_h, "Cancel");
466 cancel_button->labelsize(14);
467 cancel_button->callback(Dialog_user_password_cb);
468 window->add(cancel_button);
469
470 y += button_h + 20;
471 window_h = y;
472 window->size(window_w, window_h);
473 window->size_range(window_w, window_h, window_w, window_h);
474 window->resizable(window);
475
476 window->show();
477 while (window->shown())
478 Fl::wait();
479
480 ok = ((Fl_Widget *)window->user_data()) == ok_button ? 1 : 0;
481
482 if (ok) {
483 /* call the callback */
484 const char *user, *password;
485 user = user_input->value();
486 password = password_input->value();
487 _MSG("a_Dialog_user_passwd: ok = %d\n", ok);
488 (*cb)(user, password, vp);
489 }
490 delete window;
491
492 return ok;
493}
494
#define _MSG(...)
Definition bookmarks.c:45
char * a_Dialog_open_file(const char *title, const char *pattern, const char *fname)
Show the open file dialog.
Definition dialog.cc:272
static char * input_str
Definition dialog.cc:42
int a_Dialog_choice(const char *title, const char *msg,...)
Make a question-dialog with a question and alternatives.
Definition dialog.cc:342
static int input_answer
Definition dialog.cc:41
void a_Dialog_msg(const char *title, const char *msg)
Display a message in a popup window.
Definition dialog.cc:127
static void Dialog_user_password_cb(Fl_Widget *button, void *)
Definition dialog.cc:403
const char * a_Dialog_save_file(const char *title, const char *pattern, const char *fname)
Show the save file dialog.
Definition dialog.cc:246
const char * a_Dialog_passwd(const char *title, const char *msg)
Dialog for password.
Definition dialog.cc:233
static int choice_answer
Definition dialog.cc:43
static void choice_cb(Fl_Widget *button, void *number)
Definition dialog.cc:328
void a_Dialog_text_window(const char *title, const char *txt)
Show a new window with the provided text.
Definition dialog.cc:296
static void text_window_close_cb(Fl_Widget *, void *vtd)
Close text window.
Definition dialog.cc:284
static void input_cb(Fl_Widget *button, void *number)
Callback for a_Dialog_input()
Definition dialog.cc:139
const char * a_Dialog_select_file(const char *title, const char *pattern, const char *fname)
Show the select file dialog.
Definition dialog.cc:257
int a_Dialog_user_password(const char *title, const char *msg, UserPasswordCB cb, void *vp)
Make a user/password dialog.
Definition dialog.cc:414
const char * a_Dialog_input(const char *title, const char *msg)
Dialog for one line of Input with a message.
Definition dialog.cc:151
void(* UserPasswordCB)(const char *user, const char *password, void *vp)
Definition dialog.hh:8
void dFree(void *mem)
Definition dlib.c:68
char * dStrdup(const char *s)
Definition dlib.c:77
int dList_length(Dlist *lp)
For completing the ADT.
Definition dlib.c:613
void * dList_nth_data(Dlist *lp, int n0)
Return the nth data item, NULL when not found or 'n0' is out of range.
Definition dlib.c:662
#define VOIDP2INT(p)
Definition dlib.h:43
#define INT2VOIDP(i)
Definition dlib.h:44
#define MSG_ERR(...)
Definition dpid_common.h:23
static Fl_Window * window
int a_Misc_parse_search_url(char *source, char **label, char **urlstr)
Parse dillorc's search_url string ([<label> ]<url>) Return value: -1 on error, 0 on success (and labe...
Definition misc.c:391
char * a_Misc_escape_chars(const char *str, const char *esc_set)
Escape characters as XX sequences.
Definition misc.c:26
DilloPrefs prefs
Global Data.
Definition prefs.c:33
int width
Definition prefs.h:38
Dlist * search_urls
Definition prefs.h:118
double font_factor
Definition prefs.h:75
int height
Definition prefs.h:39
bool_t search_url_idx
Definition prefs.h:117