FINAL CUT
fmessagebox.h
1 /***********************************************************************
2 * fmessagebox.h - Widget FMessageBox (a text message window) *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2014-2024 Markus Gans *
7 * *
8 * FINAL CUT is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation; either version 3 of *
11 * the License, or (at your option) any later version. *
12 * *
13 * FINAL CUT is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this program. If not, see *
20 * <http://www.gnu.org/licenses/>. *
21 ***********************************************************************/
22 
23 /* Inheritance diagram
24  * ═══════════════════
25  *
26  * ▕▔▔▔▔▔▔▔▔▔▏ ▕▔▔▔▔▔▔▔▔▔▏
27  * ▕ FVTerm ▏ ▕ FObject ▏
28  * ▕▁▁▁▁▁▁▁▁▁▏ ▕▁▁▁▁▁▁▁▁▁▏
29  * ▲ ▲
30  * │ │
31  * └─────┬─────┘
32  * │
33  * ▕▔▔▔▔▔▔▔▔▔▏
34  * ▕ FWidget ▏
35  * ▕▁▁▁▁▁▁▁▁▁▏
36  * ▲
37  * │
38  * ▕▔▔▔▔▔▔▔▔▔▏
39  * ▕ FWindow ▏
40  * ▕▁▁▁▁▁▁▁▁▁▏
41  * ▲
42  * │
43  * ▕▔▔▔▔▔▔▔▔▔▏
44  * ▕ FDialog ▏
45  * ▕▁▁▁▁▁▁▁▁▁▏
46  * ▲
47  * │
48  * ▕▔▔▔▔▔▔▔▔▔▔▔▔▔▏
49  * ▕ FMessageBox ▏
50  * ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▏
51  */
52 
53 #ifndef FMESSAGEBOX_H
54 #define FMESSAGEBOX_H
55 
56 #if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
57  #error "Only <final/final.h> can be included directly."
58 #endif
59 
60 #include <array>
61 #include <cstring>
62 #include <memory>
63 #include <utility>
64 
65 #include "final/dialog/fdialog.h"
66 #include "final/fwidgetcolors.h"
67 #include "final/widget/fbutton.h"
68 
69 namespace finalcut
70 {
71 
72 // class forward declaration
73 class FButton;
74 
75 //----------------------------------------------------------------------
76 // class FMessageBox
77 //----------------------------------------------------------------------
78 
79 class FMessageBox : public FDialog
80 {
81  public:
82  // Enumeration
83  enum class ButtonType : std::size_t
84  {
85  Reject = 0,
86  Ok = 1,
87  Cancel = 2,
88  Yes = 3,
89  No = 4,
90  Abort = 5,
91  Retry = 6,
92  Ignore = 7
93  };
94 
95  // Constructors
96  explicit FMessageBox (FWidget* = nullptr);
97 
98  FMessageBox ( const FString&, FString&&
99  , ButtonType, ButtonType, ButtonType
100  , FWidget* = nullptr );
101 
102  // Destructor
103  ~FMessageBox() noexcept override;
104 
105  // Accessor
106  auto getClassName() const -> FString override;
107  auto getTitlebarText() const -> FString;
108  auto getHeadline() const -> FString;
109  auto getText() const -> FString override;
110 
111  // Mutator
112  void setTitlebarText (const FString&);
113  void setHeadline (const FString&);
114  void setCenterText (bool = true);
115  void unsetCenterText();
116  void setText (const FString&) override;
117 
118  // Methods
119  auto exec() -> ButtonType;
120  template <typename messageType>
121  static auto info ( FWidget*
122  , const FString&
123  , const messageType&
124  , ButtonType = ButtonType::Ok
125  , ButtonType = ButtonType::Reject
126  , ButtonType = ButtonType::Reject ) -> ButtonType;
127 
128  template <typename messageType>
129  static auto error ( FWidget*
130  , const messageType&
131  , ButtonType = ButtonType::Ok
132  , ButtonType = ButtonType::Reject
133  , ButtonType = ButtonType::Reject ) -> ButtonType;
134  protected:
135  // Method
136  void initLayout() override;
137  void adjustSize() override;
138  void done (ButtonType);
139 
140  // Callback method
141  void cb_processClick (ButtonType);
142 
143  private:
144  // Constants
145  static constexpr std::size_t MAX_BUTTONS = 3;
146 
147  // Using-declaration
148  using FButtons = std::array<std::unique_ptr<FButton>, MAX_BUTTONS>;
149  using FButtonsDigit = std::array<ButtonType, MAX_BUTTONS>;
150 
151  // Methods
152  void init();
153  void allocation();
154  void initCallbacks();
155  void calculateDimensions();
156  void draw() override;
157  auto getButtonLengthArray() const -> std::array<std::size_t, MAX_BUTTONS>;
158  auto calculateMaxButtonSize() const -> std::size_t;
159  void resizeButtons() const;
160  void adjustButtons();
161 
162  // Data members
163  FString headline_text{};
164  FString text{};
165  FStringList text_components{};
166  FButtons button{};
167  std::size_t max_line_width{0};
168  FColor emphasis_color{getColorTheme()->dialog.emphasis_fg};
169  ButtonType result_code{ButtonType::Reject};
170  FButtonsDigit button_digit{};
171  std::size_t num_buttons{0};
172  std::size_t text_num_lines{0};
173  bool center_text{false};
174 };
175 
176 
177 // FMessageBox inline functions
178 //----------------------------------------------------------------------
179 inline auto FMessageBox::getClassName() const -> FString
180 { return "FMessageBox"; }
181 
182 //----------------------------------------------------------------------
183 inline auto FMessageBox::getTitlebarText() const -> FString
184 {
185  const FString& title = FDialog::getText(); // initialize text
186  return title;
187 }
188 
189 //----------------------------------------------------------------------
190 inline auto FMessageBox::getHeadline() const -> FString
191 { return headline_text; }
192 
193 //----------------------------------------------------------------------
194 inline auto FMessageBox::getText() const -> FString
195 { return text; }
196 
197 //----------------------------------------------------------------------
198 inline void FMessageBox::setTitlebarText (const FString& txt)
199 { return FDialog::setText(txt); }
200 
201 //----------------------------------------------------------------------
202 inline void FMessageBox::setCenterText (bool enable)
203 { center_text = enable; }
204 
205 //----------------------------------------------------------------------
206 inline void FMessageBox::unsetCenterText()
207 { setCenterText(false); }
208 
209 //----------------------------------------------------------------------
210 template <typename messageType>
211 auto FMessageBox::info ( FWidget* parent
212  , const FString& caption
213  , const messageType& message
214  , ButtonType button0
215  , ButtonType button1
216  , ButtonType button2 ) -> ButtonType
217 {
218  FMessageBox mbox ( caption
219  , std::move(FString() << message)
220  , button0, button1, button2
221  , parent );
222  const ButtonType reply = mbox.exec();
223  return reply;
224 }
225 
226 //----------------------------------------------------------------------
227 template <typename messageType>
228 auto FMessageBox::error ( FWidget* parent
229  , const messageType& message
230  , ButtonType button0
231  , ButtonType button1
232  , ButtonType button2 ) -> ButtonType
233 {
234  const FString caption{"Error message"};
235 
236  FMessageBox mbox ( caption
237  , std::move(FString() << message)
238  , button0, button1, button2
239  , parent );
240  FVTerm::getFOutput()->beep();
241  mbox.setHeadline("Warning:");
242  mbox.setCenterText();
243  const auto& wc = getColorTheme();
244  mbox.setForegroundColor(wc->error_box.fg);
245  mbox.setBackgroundColor(wc->error_box.bg);
246  mbox.emphasis_color = wc->error_box.emphasis_fg;
247  const ButtonType reply = mbox.exec();
248  return reply;
249 }
250 
251 } // namespace finalcut
252 
253 #endif // FMESSAGEBOX_H
Definition: class_template.cpp:25
Definition: fstring.h:79
Definition: fdialog.h:69
Definition: fwidget.h:129
Definition: fmessagebox.h:79