1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
/*!********************************************************************
Audacity: A Digital Audio Editor
@file BasicUI.h
@brief Toolkit-neutral facade for basic user interface services
Paul Licameli
**********************************************************************/
#ifndef __AUDACITY_BASIC_UI__
#define __AUDACITY_BASIC_UI__
#include <functional>
#include <memory>
#include "Identifier.h"
#include "Internat.h"
namespace BasicUI {
//! @name Types used in the Services interface
//! @{
using Action = std::function<void()>;
using ProgressReporter = std::function<void(double)>;
//! Subclasses may hold information such as a parent window pointer for a dialog.
/*! The default-constructed empty value of this base class must be accepted by overrides of methods of
Services */
class BASIC_UI_API WindowPlacement {
public:
WindowPlacement() = default;
//! Don't slice
WindowPlacement( const WindowPlacement& ) = delete;
//! Don't slice
WindowPlacement &operator=( const WindowPlacement& ) = delete;
//! Whether null; default in the base class returns false
virtual explicit operator bool() const;
virtual ~WindowPlacement();
};
enum class ErrorDialogType {
ModelessError,
ModalError,
ModalErrorReport, /*!< If error reporting is enabled, may give option to
send; if not, then like ModalError
*/
};
//! Options for variations of error dialogs; the default is for modal dialogs
struct ErrorDialogOptions {
ErrorDialogOptions() = default;
//! Non-explicit
ErrorDialogOptions( ErrorDialogType type ) : type{ type } {}
//! @name Chain-call style initializers
//! @{
ErrorDialogOptions &&ModalHelp( bool modalHelp_ ) &&
{ modalHelp = modalHelp_; return std::move(*this); }
ErrorDialogOptions &&Log( std::wstring log_ ) &&
{ log = std::move(log_); return std::move(*this); }
//! @}
//! Type of help dialog
ErrorDialogType type{ ErrorDialogType::ModalError };
//! Whether the secondary help dialog with more information should be modal
bool modalHelp{ true };
//! Optional extra logging information to be shown
std::wstring log;
};
//! "Message", suitably translated
BASIC_UI_API TranslatableString DefaultCaption();
enum class Icon {
None,
Warning,
Error,
Question,
Information,
};
enum class Button {
Default, //!< Like Ok, except maybe minor difference of dialog position
Ok, //!< One button
YesNo //!< Two buttons
};
struct MessageBoxOptions {
//! @name Chain-call style initializers
//! @{
MessageBoxOptions &&Parent(WindowPlacement *parent_) &&
{ parent = parent_; return std::move(*this); }
MessageBoxOptions &&Caption(TranslatableString caption_) &&
{ caption = std::move(caption_); return std::move(*this); }
MessageBoxOptions &&IconStyle(Icon style) &&
{ iconStyle = style; return std::move(*this); }
MessageBoxOptions &&ButtonStyle(Button style) &&
{ buttonStyle = style; return std::move(*this); }
//! Override the usual defaulting to Yes; affects only the YesNo case
MessageBoxOptions &&DefaultIsNo() &&
{ yesOrOkDefaultButton = false; return std::move(*this); }
MessageBoxOptions &&CancelButton() &&
{ cancelButton = true; return std::move(*this); }
//! Center the dialog on its parent window, if any
MessageBoxOptions &&Centered() &&
{ centered = true; return std::move(*this); }
//! @}
WindowPlacement *parent{ nullptr };
TranslatableString caption{ DefaultCaption() };
Icon iconStyle{ Icon::None };
Button buttonStyle{ Button::Default };
bool yesOrOkDefaultButton{ true };
bool cancelButton{ false };
bool centered{ false };
};
enum class MessageBoxResult : int {
None, //!< May be returned if no Services are installed
Yes,
No,
Ok,
Cancel,
};
enum ProgressDialogOptions : unsigned {
ProgressShowStop = (1 << 0),
ProgressShowCancel = (1 << 1),
ProgressHideTime = (1 << 2),
ProgressConfirmStopOrCancel = (1 << 3),
};
enum GenericProgressDialogStyle : int {
ProgressCanAbort = (1 << 0),
ProgressAppModal = (1 << 1),
ProgressShowElapsedTime = (1 << 2),
ProgressSmooth = (1 << 3),
};
enum class ProgressResult : unsigned
{
Cancelled = 0, //<! User says that whatever is happening is undesirable and shouldn't have happened at all
Success, //<! User says nothing, everything works fine, continue doing whatever we're doing
Failed, //<! Something has gone wrong, we should stop and cancel everything we did
Stopped //<! Nothing is wrong, but user says we should stop now and leave things as they are now
};
//! Abstraction of a progress dialog with well defined time-to-completion estimate
class BASIC_UI_API ProgressDialog
{
public:
virtual ~ProgressDialog();
//! Update the bar and poll for clicks. Call only on the main thread.
virtual ProgressResult Poll(
unsigned long long numerator,
unsigned long long denominator,
const TranslatableString &message = {}) = 0;
//! Change an existing dialog's message
virtual void SetMessage(const TranslatableString & message) = 0;
//! Change the dialog's title
virtual void SetDialogTitle(const TranslatableString & title) = 0;
//! Reset the dialog state
virtual void Reinit() = 0;
};
//! Abstraction of a progress dialog with undefined time-to-completion estimate
class BASIC_UI_API GenericProgressDialog
{
public:
virtual ~GenericProgressDialog();
//! Give some visual indication of progress. Call only on the main thread.
virtual ProgressResult Pulse() = 0;
};
//! @}
//! Abstract class defines a few user interface services, not mentioning particular toolkits
/*! The intention is that the application supplies a concrete implementation at
startup. Most code will not use this class directly, but call the inline
functions that follow. */
class BASIC_UI_API Services {
public:
virtual ~Services();
virtual void DoCallAfter(const Action &action) = 0;
virtual void DoYield() = 0;
virtual void DoProcessIdle() = 0;
virtual void DoShowErrorDialog(const WindowPlacement &placement,
const TranslatableString &dlogTitle,
const TranslatableString &message,
const ManualPageID &helpPage,
const ErrorDialogOptions &options) = 0;
virtual MessageBoxResult DoMessageBox(
const TranslatableString& message,
MessageBoxOptions options) = 0;
virtual std::unique_ptr<ProgressDialog>
DoMakeProgress(const TranslatableString &title,
const TranslatableString &message,
unsigned flag,
const TranslatableString &remainingLabelText) = 0;
virtual std::unique_ptr<GenericProgressDialog>
DoMakeGenericProgress(const WindowPlacement &placement,
const TranslatableString &title,
const TranslatableString &message,
int style) = 0;
virtual int DoMultiDialog(const TranslatableString &message,
const TranslatableString &title,
const TranslatableStrings &buttons,
const ManualPageID &helpPage,
const TranslatableString &boxMsg, bool log) = 0;
virtual bool DoOpenInDefaultBrowser(const wxString &url) = 0;
virtual std::unique_ptr<WindowPlacement> DoFindFocus() = 0;
virtual void DoSetFocus(const WindowPlacement &focus) = 0;
virtual bool IsUsingRtlLayout() const = 0;
virtual bool IsUiThread() const = 0;
};
//! Fetch the global instance, or nullptr if none is yet installed
BASIC_UI_API Services *Get();
//! Install an implementation; return the previously installed instance
BASIC_UI_API Services *Install(Services *pInstance);
/*! @name Functions that invoke global Services
These dispatch to the global Services, if supplied. If none was supplied,
they are mostly no-ops, with exceptions as noted. All should be called on
the main thread only, except as noted.
*/
//! @{
//! Schedule an action to be done later, and in the main thread
/*! This function may be called in other threads than the main. If no Services
are yet installed, the action is not lost, but may be dispatched by Yield().
The action may itself invoke CallAfter to enqueue other actions.
*/
BASIC_UI_API void CallAfter(Action action);
//! Dispatch waiting events, including actions enqueued by CallAfter
/*! This function must be called by the main thread. Actions enqueued by
CallAfter before Services were installed will be dispatched in the sequence
they were enqueued, unless an exception thrown by one of them stops the
dispatching.
*/
BASIC_UI_API void Yield();
//! Dispatch waiting events only (no pending mouse, keyboard, or timer events)
BASIC_UI_API void ProcessIdle();
//! Open an URL in default browser
/*! This function may be called in other threads than the main.
*/
BASIC_UI_API bool OpenInDefaultBrowser(const wxString &url);
//! Show an error dialog with a link to the manual for further help
inline void ShowErrorDialog(
const WindowPlacement &placement, //!< how to parent the dialog
const TranslatableString &dlogTitle, //!< Text for title bar
const TranslatableString &message, //!< The main message text
const ManualPageID &helpPage, //!< Identifies manual page (and maybe an anchor)
const ErrorDialogOptions &options = {})
{
if (auto p = Get())
p->DoShowErrorDialog(placement, dlogTitle, message, helpPage, options);
}
//! Show a modal message box with either Ok or Yes and No, and optionally Cancel
/*!
@return indicates which button was pressed
*/
inline MessageBoxResult ShowMessageBox( const TranslatableString &message,
MessageBoxOptions options = {})
{
if (auto p = Get())
return p->DoMessageBox(message, std::move(options));
else
return MessageBoxResult::None;
}
//! Create and display a progress dialog
/*!
@param flags bitwise OR of values in ProgressDialogOptions
@param remainingLabelText if not empty substitutes for "Remaining Time:"
@return nullptr if Services not installed
*/
inline std::unique_ptr<ProgressDialog> MakeProgress(
const TranslatableString & title,
const TranslatableString & message,
unsigned flags = (ProgressShowStop | ProgressShowCancel),
const TranslatableString & remainingLabelText = {})
{
if (auto p = Get())
return p->DoMakeProgress(title, message, flags, remainingLabelText);
else
return nullptr;
}
//! Create and display a progress dialog (return nullptr if Services not installed)
/*!
This function makes a "generic" progress dialog, for the case when time
to completion cannot be estimated, but some indication of progress is still
given
*/
inline std::unique_ptr<GenericProgressDialog> MakeGenericProgress(
const WindowPlacement &placement,
const TranslatableString &title, const TranslatableString &message, int style = (ProgressAppModal | ProgressShowElapsedTime | ProgressSmooth))
{
if (auto p = Get())
return p->DoMakeGenericProgress(placement, title, message, style);
else
return nullptr;
}
/*!
* @brief Helper for the update of a task's progress bar when this task is made
* of a range's subtasks.
* @details For each item from `first` till `last`, forwards the item as
* argument to `action`, as well as a progress reporter that will contribute by
* a fraction to the parent progress reporter. This fraction is the inverse of
* the number of elements in the range.
*/
template <typename ItType, typename FnType>
void SplitProgress(
ItType first, ItType last, FnType action, ProgressReporter parent)
{
auto count = 0;
const auto numIters = std::distance(first, last);
if (numIters == 0)
return;
const ProgressReporter child =
parent ? [&](double progress) { parent((count + progress) / numIters); } :
ProgressReporter {};
for (; first != last; ++first)
{
action(*first, child);
++count;
}
}
//! Display a dialog with radio buttons.
/*!
@return zero-based index of the chosen button, or -1 if Services not installed.
@param message main message in the dialog
@param title dialog title
@param buttons labels for individual radio buttons
@param boxMsg label for the group of buttons
@param helpPage identifies a manual page
@param log whether to add a "Show Log for Details" push button
*/
inline int ShowMultiDialog(const TranslatableString &message,
const TranslatableString &title,
const TranslatableStrings &buttons,
const ManualPageID &helpPage,
const TranslatableString &boxMsg, bool log)
{
if (auto p = Get())
return p->DoMultiDialog(message, title, buttons, helpPage, boxMsg, log);
else
return -1;
}
//! Find the window that is accepting keyboard input, if any
/*!
@post `result: result != nullptr` (but may point to an empty WindowPlacement)
*/
inline std::unique_ptr<WindowPlacement> FindFocus()
{
if (auto p = Get())
if (auto result = p->DoFindFocus())
return result;
return std::make_unique<WindowPlacement>();
}
//! Set the window that accepts keyboard input
inline void SetFocus(const WindowPlacement &focus)
{
if (auto p = Get())
p->DoSetFocus(focus);
}
//! Whether using a right-to-left language layout
inline bool IsUsingRtlLayout()
{
if (auto p = Get())
return p->IsUsingRtlLayout();
return false;
}
//! Whether the current thread is the UI thread
inline bool IsUiThread ()
{
if (auto p = Get())
return p->IsUiThread();
return true;
}
#define ASSERT_MAIN_THREAD() \
assert( \
BasicUI::IsUiThread() && \
"This function should only be called on the main thread")
//! @}
}
#endif
|