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