fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

fl_ask.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // Standard dialog functions for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2011 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
24 // Implementation of fl_message, fl_ask, fl_choice, fl_input
25 // The three-message fl_show_x functions are for forms compatibility
26 // mostly. In most cases it is easier to get a multi-line message
27 // by putting newlines in the message.
28 
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include "flstring.h"
32 
33 #include <FL/Fl.H>
34 
35 #include <FL/fl_ask.H>
36 
37 #include <FL/Fl_Box.H>
38 #include <FL/Fl_Button.H>
39 #include <FL/Fl_Return_Button.H>
40 #include <FL/Fl_Window.H>
41 #include <FL/Fl_Input.H>
42 #include <FL/Fl_Secret_Input.H>
43 #include <FL/x.H>
44 #include <FL/fl_draw.H>
45 
47 static Fl_Box *message;
48 static Fl_Box *icon;
49 static Fl_Button *button[3];
50 static Fl_Input *input;
51 static int ret_val;
52 static const char *iconlabel = "?";
53 static const char *message_title_default;
56 static int enableHotspot = 1;
57 #ifdef __APPLE__
58 extern "C" void NSBeep(void);
59 #endif
60 
61 static char avoidRecursion = 0;
62 
63 // Sets the global return value (ret_val) and closes the window.
64 // Note: this is used for the button callbacks and the window
65 // callback (closing the window with the close button or menu).
66 // The first argument (Fl_Widget *) can either be an Fl_Button*
67 // pointer to one of the buttons or an Fl_Window* pointer to the
68 // message window (message_form).
69 static void button_cb(Fl_Widget *, long val) {
70  ret_val = (int) val;
71  message_form->hide();
72 }
73 
74 static Fl_Window *makeform() {
75  if (message_form) {
76  return message_form;
77  }
78  // make sure that the dialog does not become the child of some
79  // current group
80  Fl_Group *previously_current_group = Fl_Group::current();
82  // create a new top level window
83  Fl_Window *w = message_form = new Fl_Window(410,103);
85  // w->clear_border();
86  // w->box(FL_UP_BOX);
87  (message = new Fl_Box(60, 25, 340, 20))
89  (input = new Fl_Input(60, 37, 340, 23))->hide();
90  {Fl_Box* o = icon = new Fl_Box(10, 10, 50, 50);
91  o->box(FL_THIN_UP_BOX);
93  o->labelsize(34);
94  o->color(FL_WHITE);
95  o->labelcolor(FL_BLUE);
96  }
97  w->end(); // don't add the buttons automatically
98  // create the buttons (right to left)
99  {
100  for (int b=0, x=310; b<3; b++, x -= 100) {
101  if (b==1)
102  button[b] = new Fl_Return_Button(x, 70, 90, 23);
103  else
104  button[b] = new Fl_Button(x, 70, 90, 23);
107  }
108  }
110  // add the buttons (left to right)
111  {
112  for (int b=2; b>=0; b--)
113  w->add(button[b]);
114  }
115  w->begin();
116  w->resizable(new Fl_Box(60,10,110-60,27));
117  w->end();
118  w->set_modal();
119  Fl_Group::current(previously_current_group);
120  return w;
121 }
122 
123 /*
124  * 'resizeform()' - Resize the form and widgets so that they hold everything
125  * that is asked of them...
126  */
127 
128 static void resizeform() {
129  int i;
130  int message_w, message_h;
131  int text_height;
132  int button_w[3], button_h[3];
133  int x, w, h, max_w, max_h;
134  const int icon_size = 50;
135 
136  message_form->size(410,103);
137 
139  message_w = message_h = 0;
140  fl_measure(message->label(), message_w, message_h);
141 
142  message_w += 10;
143  message_h += 10;
144  if (message_w < 340)
145  message_w = 340;
146  if (message_h < 30)
147  message_h = 30;
148 
149  fl_font(button[0]->labelfont(), button[0]->labelsize());
150 
151  memset(button_w, 0, sizeof(button_w));
152  memset(button_h, 0, sizeof(button_h));
153 
154  for (max_h = 25, i = 0; i < 3; i ++)
155  if (button[i]->visible())
156  {
157  fl_measure(button[i]->label(), button_w[i], button_h[i]);
158 
159  if (i == 1)
160  button_w[1] += 20;
161 
162  button_w[i] += 30;
163  button_h[i] += 10;
164 
165  if (button_h[i] > max_h)
166  max_h = button_h[i];
167  }
168 
169  if (input->visible()) text_height = message_h + 25;
170  else text_height = message_h;
171 
172  max_w = message_w + 10 + icon_size;
173  w = button_w[0] + button_w[1] + button_w[2] - 10;
174 
175  if (w > max_w)
176  max_w = w;
177 
178  message_w = max_w - 10 - icon_size;
179 
180  w = max_w + 20;
181  h = max_h + 30 + text_height;
182 
183  message_form->size(w, h);
184  message_form->size_range(w, h, w, h);
185 
186  message->resize(20 + icon_size, 10, message_w, message_h);
187  icon->resize(10, 10, icon_size, icon_size);
188  icon->labelsize(icon_size - 10);
189  input->resize(20 + icon_size, 10 + message_h, message_w, 25);
190 
191  for (x = w, i = 0; i < 3; i ++)
192  if (button_w[i])
193  {
194  x -= button_w[i];
195  button[i]->resize(x, h - 10 - max_h, button_w[i] - 10, max_h);
196 
197 // printf("button %d (%s) is %dx%d+%d,%d\n", i, button[i]->label(),
198 // button[i]->w(), button[i]->h(),
199 // button[i]->x(), button[i]->y());
200  }
201 }
202 
203 static int innards(const char* fmt, va_list ap,
204  const char *b0,
205  const char *b1,
206  const char *b2)
207 {
208  Fl::pushed(0); // stop dragging (STR #2159)
209 
210  avoidRecursion = 1;
211 
212  makeform();
213  message_form->size(410,103);
214  char buffer[1024];
215  if (!strcmp(fmt,"%s")) {
216  message->label(va_arg(ap, const char*));
217  } else {
218  ::vsnprintf(buffer, 1024, fmt, ap);
219  message->label(buffer);
220  }
221 
223  if (fl_message_size_ == -1)
225  else
227  if (b0) {button[0]->show(); button[0]->label(b0); button[1]->position(210,70);}
228  else {button[0]->hide(); button[1]->position(310,70);}
229  if (b1) {button[1]->show(); button[1]->label(b1);}
230  else button[1]->hide();
231  if (b2) {button[2]->show(); button[2]->label(b2);}
232  else button[2]->hide();
233  const char* prev_icon_label = icon->label();
234  if (!prev_icon_label) icon->label(iconlabel);
235 
236  resizeform();
237 
238  if (button[1]->visible() && !input->visible())
239  button[1]->take_focus();
240  if (enableHotspot)
242  if (b0 && Fl_Widget::label_shortcut(b0))
243  button[0]->shortcut(0);
244  else
246 
247  // set default window title, if defined and a specific title is not set
250 
251  // deactivate Fl::grab(), because it is incompatible with modal windows
252  Fl_Window* g = Fl::grab();
253  if (g) Fl::grab(0);
254  Fl_Group *current_group = Fl_Group::current(); // make sure the dialog does not interfere with any active group
255  message_form->show();
256  Fl_Group::current(current_group);
257  while (message_form->shown()) Fl::wait();
258  if (g) // regrab the previous popup menu, if there was one
259  Fl::grab(g);
260  icon->label(prev_icon_label);
261  message_form->label(0); // reset window title
262 
263  avoidRecursion = 0;
264  return ret_val;
265 }
266 
270 // pointers you can use to change FLTK to another language:
271 const char* fl_no = "No";
272 const char* fl_yes= "Yes";
273 const char* fl_ok = "OK";
274 const char* fl_cancel= "Cancel";
275 const char* fl_close= "Close";
276 
277 // fltk functions:
283 void fl_beep(int type) {
284 #ifdef WIN32
285  switch (type) {
286  case FL_BEEP_QUESTION :
287  case FL_BEEP_PASSWORD :
288  MessageBeep(MB_ICONQUESTION);
289  break;
290  case FL_BEEP_MESSAGE :
291  MessageBeep(MB_ICONASTERISK);
292  break;
293  case FL_BEEP_NOTIFICATION :
294  MessageBeep(MB_ICONASTERISK);
295  break;
296  case FL_BEEP_ERROR :
297  MessageBeep(MB_ICONERROR);
298  break;
299  default :
300  MessageBeep(0xFFFFFFFF);
301  break;
302  }
303 #elif defined(__APPLE__)
304  switch (type) {
305  case FL_BEEP_DEFAULT :
306  case FL_BEEP_ERROR :
307  NSBeep();
308  break;
309  default :
310  break;
311  }
312 #else
313  switch (type) {
314  case FL_BEEP_DEFAULT :
315  case FL_BEEP_ERROR :
316  if (!fl_display) fl_open_display();
317 
318  XBell(fl_display, 100);
319  break;
320  default :
321  if (!fl_display) fl_open_display();
322 
323  XBell(fl_display, 50);
324  break;
325  }
326 #endif // WIN32
327 }
328 
338 void fl_message(const char *fmt, ...) {
339 
340  if (avoidRecursion) return;
341 
342  va_list ap;
343 
344  // fl_beep(FL_BEEP_MESSAGE);
345 
346  va_start(ap, fmt);
347  iconlabel = "i";
348  innards(fmt, ap, 0, fl_close, 0);
349  va_end(ap);
350  iconlabel = "?";
351 }
352 
361 void fl_alert(const char *fmt, ...) {
362 
363  if (avoidRecursion) return;
364 
365  va_list ap;
366 
367  // fl_beep(FL_BEEP_ERROR);
368 
369  va_start(ap, fmt);
370  iconlabel = "!";
371  innards(fmt, ap, 0, fl_close, 0);
372  va_end(ap);
373  iconlabel = "?";
374 }
390 int fl_ask(const char *fmt, ...) {
391 
392  if (avoidRecursion) return 0;
393 
394  va_list ap;
395 
396  // fl_beep(FL_BEEP_QUESTION);
397 
398  va_start(ap, fmt);
399  int r = innards(fmt, ap, fl_no, fl_yes, 0);
400  va_end(ap);
401 
402  return r;
403 }
404 
459 int fl_choice(const char*fmt,const char *b0,const char *b1,const char *b2,...){
460 
461  if (avoidRecursion) return 0;
462 
463  va_list ap;
464 
465  // fl_beep(FL_BEEP_QUESTION);
466 
467  va_start(ap, b2);
468  int r = innards(fmt, ap, b0, b1, b2);
469  va_end(ap);
470  return r;
471 }
478 
479 static const char* input_innards(const char* fmt, va_list ap,
480  const char* defstr, uchar type) {
481  makeform();
482  message_form->size(410,103);
483  message->position(60,10);
484  input->type(type);
485  input->show();
486  input->value(defstr);
487  input->take_focus();
488 
489  int r = innards(fmt, ap, fl_cancel, fl_ok, 0);
490  input->hide();
491  message->position(60,25);
492  return r ? input->value() : 0;
493 }
494 
505 const char* fl_input(const char *fmt, const char *defstr, ...) {
506 
507  if (avoidRecursion) return 0;
508 
509  // fl_beep(FL_BEEP_QUESTION);
510 
511  va_list ap;
512  va_start(ap, defstr);
513  const char* r = input_innards(fmt, ap, defstr, FL_NORMAL_INPUT);
514  va_end(ap);
515  return r;
516 }
517 
531 const char *fl_password(const char *fmt, const char *defstr, ...) {
532 
533  if (avoidRecursion) return 0;
534 
535  // fl_beep(FL_BEEP_PASSWORD);
536 
537  va_list ap;
538  va_start(ap, defstr);
539  const char* r = input_innards(fmt, ap, defstr, FL_SECRET_INPUT);
540  va_end(ap);
541  return r;
542 }
543 
555 void fl_message_hotspot(int enable) {
556  enableHotspot = enable ? 1 : 0;
557 }
558 
568  return enableHotspot;
569 }
570 
586 void fl_message_title(const char *title) {
587  makeform();
589 }
590 
607 void fl_message_title_default(const char *title) {
608  if (message_title_default) {
609  free ((void *)message_title_default);
611  }
612  if (title)
613  message_title_default = strdup(title);
614 }
615 
618 //
619 // End of "$Id$".
620 //
fl_message_hotspot
void fl_message_hotspot(int enable)
Definition: fl_ask.cxx:555
fl_font
void fl_font(Fl_Font face, Fl_Fontsize fsize)
Definition: fl_draw.H:509
FL_ALIGN_LEFT
const Fl_Align FL_ALIGN_LEFT
Definition: Enumerations.H:839
fl_message_size_
Fl_Fontsize fl_message_size_
Definition: fl_ask.cxx:55
Fl.H
fl_ask
int fl_ask(const char *fmt,...)
Definition: fl_ask.cxx:390
iconlabel
static const char * iconlabel
Definition: fl_ask.cxx:52
Fl_Widget::align
Fl_Align align() const
Definition: Fl_Widget.H:348
Fl_Window::show
virtual void show()
Definition: Fl_win32.cxx:2542
message_title_default
static const char * message_title_default
Definition: fl_ask.cxx:53
Fl_Return_Button.H
input
static Fl_Input * input
Definition: fl_ask.cxx:50
Fl_Group::end
void end()
Definition: Fl_Group.cxx:75
fl_message
void fl_message(const char *fmt,...)
Definition: fl_ask.cxx:338
vsnprintf
#define vsnprintf
Definition: flstring.h:69
Fl_Input.H
Fl_Widget::show
virtual void show()
Definition: Fl_Widget.cxx:271
Fl_Widget::labelcolor
Fl_Color labelcolor() const
Definition: Fl_Widget.H:472
FL_BEEP_NOTIFICATION
Notification beep.
Definition: fl_ask.H:38
input_innards
static const char * input_innards(const char *fmt, va_list ap, const char *defstr, uchar type)
Definition: fl_ask.cxx:479
FL_WHITE
const Fl_Color FL_WHITE
Definition: Enumerations.H:971
Fl_Window::hotspot
void hotspot(int x, int y, int offscreen=0)
Definition: Fl_Window_hotspot.cxx:24
fl_ask.H
message
static Fl_Box * message
Definition: fl_ask.cxx:47
Fl_Button::shortcut
int shortcut() const
Definition: Fl_Button.H:125
Fl_Input_::value
int value(const char *)
Definition: Fl_Input_.cxx:1284
Fl_Button
Buttons generate callbacks when they are clicked by the user.
Definition: Fl_Button.H:79
fl_password
const char * fl_password(const char *fmt, const char *defstr,...)
Definition: fl_ask.cxx:531
Fl_Widget::labelfont
Fl_Font labelfont() const
Definition: Fl_Widget.H:487
x.H
FL_SECRET_INPUT
#define FL_SECRET_INPUT
Definition: Fl_Input_.H:34
free
void free()
FL_BLUE
const Fl_Color FL_BLUE
Definition: Enumerations.H:960
FL_BEEP_PASSWORD
Password beep.
Definition: fl_ask.H:37
message_form
static Fl_Window * message_form
Definition: fl_ask.cxx:46
b
long b
Definition: jpegint.h:397
icon
static Fl_Box * icon
Definition: fl_ask.cxx:48
innards
static int innards(const char *fmt, va_list ap, const char *b0, const char *b1, const char *b2)
Definition: fl_ask.cxx:203
enableHotspot
static int enableHotspot
Definition: fl_ask.cxx:56
fl_choice
int fl_choice(const char *fmt, const char *b0, const char *b1, const char *b2,...)
Definition: fl_ask.cxx:459
fl_yes
const char * fl_yes
string pointer used in common dialogs, you can change it to another language
Definition: fl_ask.cxx:272
buffer
static char * buffer
Definition: file.cxx:215
FL_ALIGN_INSIDE
const Fl_Align FL_ALIGN_INSIDE
Definition: Enumerations.H:843
Fl_Window.H
Fl_Widget::color
Fl_Color color() const
Definition: Fl_Widget.H:378
button
static Fl_Button * button[3]
Definition: fl_ask.cxx:49
Fl_Box.H
Fl_Widget::position
void position(int X, int Y)
Definition: Fl_Widget.H:332
Fl_Input_::resize
void resize(int, int, int, int)
Definition: Fl_Input_.cxx:1294
fl_input
const char * fl_input(const char *fmt, const char *defstr,...)
Definition: fl_ask.cxx:505
FL_THIN_UP_BOX
see figure 1
Definition: Enumerations.H:611
fl_message_title
void fl_message_title(const char *title)
Definition: fl_ask.cxx:586
fl_draw.H
utility header to pull drawing functions together
FL_HELVETICA
const Fl_Font FL_HELVETICA
Helvetica (or Arial) normal (0)
Definition: Enumerations.H:879
fl_no
const char * fl_no
string pointer used in common dialogs, you can change it to another language
Definition: fl_ask.cxx:271
Fl_Window::set_modal
void set_modal()
Definition: Fl_Window.H:289
Fl_Window::copy_label
void copy_label(const char *a)
Definition: Fl_Window.cxx:170
Fl_Window::size_range
void size_range(int minw, int minh, int maxw=0, int maxh=0, int dw=0, int dh=0, int aspect=0)
Definition: Fl_Window.H:438
fl_beep
void fl_beep(int type)
Definition: fl_ask.cxx:283
Fl_Widget::visible
unsigned int visible() const
Definition: Fl_Widget.H:660
fl_measure
FL_EXPORT void fl_measure(const char *str, int &x, int &y, int draw_symbols=1)
Definition: fl_draw.cxx:380
Fl_Window
Definition: Fl_Window.H:57
fl_ok
const char * fl_ok
string pointer used in common dialogs, you can change it to another language
Definition: fl_ask.cxx:273
Fl_Return_Button
Definition: Fl_Return_Button.H:33
Fl_Widget::hide
virtual void hide()
Definition: Fl_Widget.cxx:283
Fl_Input
Definition: Fl_Input.H:222
Fl_Fontsize
int Fl_Fontsize
Definition: Enumerations.H:906
fl_display
FL_EXPORT HINSTANCE fl_display
END TIMERS.
Definition: Fl_win32.cxx:2147
FL_NORMAL_SIZE
Fl_Fontsize FL_NORMAL_SIZE
normal font size
Definition: Fl_Widget.cxx:117
Fl_Widget
Definition: Fl_Widget.H:101
Fl_Group
Definition: Fl_Group.H:41
Fl_Widget::label_shortcut
static unsigned int label_shortcut(const char *t)
Definition: fl_shortcut.cxx:423
Fl_Group::current
static Fl_Group * current()
Definition: Fl_Group.cxx:84
Fl_Group::add
void add(Fl_Widget &)
Definition: Fl_Group.cxx:491
Fl_Widget::box
Fl_Boxtype box() const
Definition: Fl_Widget.H:363
FL_NORMAL_INPUT
#define FL_NORMAL_INPUT
Definition: Fl_Input_.H:29
Fl_Window::shown
int shown()
Definition: Fl_Window.H:485
Fl::pushed
static Fl_Widget * pushed()
Definition: Fl.H:837
Fl_Window::hide
virtual void hide()
Definition: Fl.cxx:1546
fl_message_title_default
void fl_message_title_default(const char *title)
Definition: fl_ask.cxx:607
Fl_Widget::label
const char * label() const
Definition: Fl_Widget.H:421
Fl_Window::label
const char * label() const
Definition: Fl_Window.H:450
makeform
static Fl_Window * makeform()
Definition: fl_ask.cxx:74
Fl_Box
Definition: Fl_Box.H:34
FL_BEEP_QUESTION
Question beep.
Definition: fl_ask.H:36
ret_val
static int ret_val
Definition: fl_ask.cxx:51
Fl_Widget::size
void size(int W, int H)
Definition: Fl_Widget.H:341
x
int x
Definition: test.c:73
Fl_Widget::callback
Fl_Callback_p callback() const
Definition: Fl_Widget.H:561
Fl_Font
int Fl_Font
Definition: Enumerations.H:877
Fl::wait
static int wait()
Definition: Fl.cxx:666
FL_ALIGN_WRAP
const Fl_Align FL_ALIGN_WRAP
Definition: Enumerations.H:851
avoidRecursion
static char avoidRecursion
Definition: fl_ask.cxx:61
FL_TIMES_BOLD
const Fl_Font FL_TIMES_BOLD
Times roman bold.
Definition: Enumerations.H:888
Fl_Widget::type
uchar type() const
Definition: Fl_Widget.H:274
resizeform
static void resizeform()
Definition: fl_ask.cxx:128
FL_BEEP_DEFAULT
Default beep.
Definition: fl_ask.H:33
Fl_Button.H
Fl_Secret_Input.H
fl_cancel
const char * fl_cancel
string pointer used in common dialogs, you can change it to another language
Definition: fl_ask.cxx:274
flstring.h
title
static const char * title
Definition: Fl_arg.cxx:55
Fl_Widget::resize
virtual void resize(int x, int y, int w, int h)
Definition: Fl_Widget.cxx:150
FL_BEEP_ERROR
Error beep.
Definition: fl_ask.H:35
Fl_Widget::take_focus
int take_focus()
Definition: Fl_Widget.cxx:162
fl_open_display
void fl_open_display()
Definition: Fl_win32.cxx:496
Fl_Widget::labelsize
Fl_Fontsize labelsize() const
Definition: Fl_Widget.H:502
fl_message_font_
Fl_Font fl_message_font_
Definition: fl_ask.cxx:54
FL_BEEP_MESSAGE
Message beep.
Definition: fl_ask.H:34
button_cb
static void button_cb(Fl_Widget *, long val)
Definition: fl_ask.cxx:69
uchar
unsigned char uchar
Definition: fl_types.h:30
fl_alert
void fl_alert(const char *fmt,...)
Definition: fl_ask.cxx:361
Fl_Group::begin
void begin()
Definition: Fl_Group.cxx:69
Fl::grab
static Fl_Window * grab()
Definition: Fl.H:555
FL_Escape
#define FL_Escape
The escape key.
Definition: Enumerations.H:474
fl_message_icon
Fl_Widget * fl_message_icon()
Definition: fl_ask.cxx:477
Fl_Group::resizable
void resizable(Fl_Widget &o)
Definition: Fl_Group.H:117
fl_close
const char * fl_close
string pointer used in common dialogs, you can change it to another language
Definition: fl_ask.cxx:275