Merge branch 'pr-30_fl_message_position'

Merge PR #30 with extensions to position the message box centered
over given coordinates or a widget or window.
This commit is contained in:
Albrecht Schlosser 2020-05-07 17:46:23 +02:00
commit 4ab49d30d3
3 changed files with 115 additions and 13 deletions

View File

@ -3,17 +3,17 @@
//
// Standard dialog header file for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2011 by Bill Spitzak and others.
// Copyright 1998-2020 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// http://www.fltk.org/COPYING.php
// https://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
// http://www.fltk.org/str.php
// https://www.fltk.org/str.php
//
/** \file fl_ask.H
@ -24,7 +24,6 @@
# define fl_ask_H
# include "Enumerations.H"
class Fl_Widget;
/** Different system beeps available.
@ -66,6 +65,15 @@ inline void fl_message_font(Fl_Font f, Fl_Fontsize s) {
FL_EXPORT void fl_message_hotspot(int enable);
FL_EXPORT int fl_message_hotspot(void);
FL_EXPORT void fl_message_position(const int x, const int y, const int center = 0);
FL_EXPORT void fl_message_position(Fl_Widget *widget);
FL_EXPORT int fl_message_position(int *x = 0, int *y = 0);
/** \see fl_message_position(Fl_Widget *widget). */
inline void fl_message_position(Fl_Widget &widget) {
fl_message_position(&widget);
}
FL_EXPORT void fl_message_title(const char *title);
FL_EXPORT void fl_message_title_default(const char *title);

View File

@ -3,17 +3,17 @@
//
// Standard dialog functions for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2018 by Bill Spitzak and others.
// Copyright 1998-2020 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// http://www.fltk.org/COPYING.php
// https://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
// http://www.fltk.org/str.php
// https://www.fltk.org/str.php
//
/**
@ -55,6 +55,9 @@ static const char *message_title_default;
Fl_Font fl_message_font_ = FL_HELVETICA;
Fl_Fontsize fl_message_size_ = -1;
static int enableHotspot = 1;
static int form_x = 0;
static int form_y = 0;
static int form_position = 0; // 0 = not set, 1 = absolute, 2 = centered
static char avoidRecursion = 0;
@ -236,8 +239,19 @@ static int innards(const char* fmt, va_list ap,
if (button[1]->visible() && !input->visible())
button[1]->take_focus();
if (enableHotspot)
if (form_position) {
if (form_position == 2) { // centered
form_x -= message_form->w()/2;
form_y -= message_form->h()/2;
}
message_form->position(form_x, form_y);
form_x = form_y = form_position = 0;
} else if (enableHotspot)
message_form->hotspot(button[0]);
else
message_form->free_position();
if (b0 && Fl_Widget::label_shortcut(b0))
button[0]->shortcut(0);
else
@ -362,8 +376,9 @@ int fl_ask(const char *fmt, ...) {
return r;
}
/** Shows a dialog displaying the printf style \p fmt message,
this dialog features up to 3 customizable choice buttons
/** Shows a dialog displaying the printf style \p fmt message.
This dialog features up to 3 customizable choice buttons
which are specified in order of *right-to-left* in the dialog, e.g.
\image html fl_choice_left_middle_right.png
\image latex fl_choice_left_middle_right.png "fl_choice() button ordering" width=4cm
@ -506,6 +521,82 @@ const char *fl_password(const char *fmt, const char *defstr, ...) {
return r;
}
/** Sets the preferred position for the common message box used in
many common dialogs like fl_message(), fl_alert(),
fl_ask(), fl_choice(), fl_input(), fl_password().
Resets after every call to any of the common dialogs.
The position set with this method overrides the hotspot setting,
i.e. setting a position has higher priority than the hotspot mode
set by fl_message_hotspot(int).
If the optional argument \p center is non-zero (true) the message box
will be centered at the given coordinates rather than using the X/Y
position as the window position (top left corner).
\note \#include <FL/fl_ask.H>
\param[in] x Preferred X position
\param[in] y Preferred Y position
\param[in] center 1 = centered, 0 = absolute
\see int fl_message_position(int *x, int *y)
*/
void fl_message_position(const int x, const int y, const int center) {
form_x = x;
form_y = y;
form_position = center ? 2 : 1;
}
/** Sets the preferred position for the common message box used in
many common dialogs like fl_message(), fl_alert(),
fl_ask(), fl_choice(), fl_input(), fl_password().
The common message box will be centered over the given widget
or window extensions.
Everything else is like fl_message_position(int, int, int) with
argument 'center' set to 1.
\note \#include <FL/fl_ask.H>
\param[in] w Widget or window to position the message box over.
\see int fl_message_position(int x, int y, int center)
*/
void fl_message_position(Fl_Widget *widget) {
form_x = widget->x() + widget->w()/2;
form_y = widget->y() + widget->h()/2;
form_position = 2;
}
/** Gets the preferred position for the common message box used in
many common dialogs like fl_message(), fl_alert(),
fl_ask(), fl_choice(), fl_input(), fl_password().
\note \#include <FL/fl_ask.H>
\param[out] x Preferred X position, returns -1 if not set
\param[out] y Preferred Y position, returns -1 if not set
\returns whether position is currently set or not
\retval 0 position is not set (may be hotspot or not)
\retval 1 position is set (window position)
\retval 2 position is set (message box centered)
\see fl_message_position(int, int)
\see fl_message_hotspot(int)
\see int fl_message_hotspot()
*/
int fl_message_position(int *x, int *y) {
if (x)
*x = form_position ? form_x : -1;
if (y)
*y = form_position ? form_y : -1;
return form_position;
}
/** Sets whether or not to move the common message box used in
many common dialogs like fl_message(), fl_alert(),
fl_ask(), fl_choice(), fl_input(), fl_password() to follow
@ -559,7 +650,7 @@ void fl_message_title(const char *title) {
common dialogs like fl_message(), fl_alert(), fl_ask(), fl_choice(),
fl_input(), fl_password(), unless a specific title has been set
with fl_message_title(const char *title).
The default is no title. You can override the default title for a
single dialog with fl_message_title(const char *title).

View File

@ -49,7 +49,7 @@ void rename_me_pwd(Fl_Widget*o) {
update_input_text(o, input);
}
void window_callback(Fl_Widget*, void*) {
void window_callback(Fl_Widget *win, void*) {
int hotspot = fl_message_hotspot();
fl_message_hotspot(0);
fl_message_title("note: no hotspot set for this dialog");
@ -58,8 +58,11 @@ void window_callback(Fl_Widget*, void*) {
fl_message_hotspot(hotspot);
if (rep==1)
exit(0);
else if (rep==2)
else if (rep==2) { // (Dunno)
fl_message_position(win);
fl_message_title("This dialog must be centered over the main window");
fl_message("Well, maybe you should know before we quit.");
}
}
/*