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
|
/***********************************************************************
* fmenu.h - Widget FMenu *
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2015-2023 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* FINAL CUT is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
/* Inheritance diagram
* ═══════════════════
*
* ▕▔▔▔▔▔▔▔▔▔▏ ▕▔▔▔▔▔▔▔▔▔▏
* ▕ FVTerm ▏ ▕ FObject ▏
* ▕▁▁▁▁▁▁▁▁▁▏ ▕▁▁▁▁▁▁▁▁▁▏
* ▲ ▲
* │ │ *▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
* └─────┬─────┘ ┌ - -▕ FRadioMenuItem ▏
* │ : ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
* ▕▔▔▔▔▔▔▔▔▔▏ :
* ▕ FWidget ▏ : *▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏
* ▕▁▁▁▁▁▁▁▁▁▏ ├ - -▕ FCheckMenuItem ▏
* ▲ : ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏
* │ 1 :
* ▕▔▔▔▔▔▔▔▔▔▏ ▕▔▔▔▔▔▔▔▔▔▔▔▏- ┘ ▕▔▔▔▔▔▔▔▔▔▔▔▏
* ▕ FWindow ▏ ▕ FMenuList ▏- - - -▕ FMenuItem ▏
* ▕▁▁▁▁▁▁▁▁▁▏ ▕▁▁▁▁▁▁▁▁▁▁▁▏1 *▕▁▁▁▁▁▁▁▁▁▁▁▏
* ▲ ▲ 1:
* │ │ :
* └─────┬─────┘ :
* │ 1 :
* ▕▔▔▔▔▔▔▔▏- - - ┘ ▕▔▔▔▔▔▔▔▔▔▔▔▏
* ▕ FMenu ▏- - - - - - - -▕ FMenuItem ▏
* ▕▁▁▁▁▁▁▁▏1 1▕▁▁▁▁▁▁▁▁▁▁▁▏
*/
#ifndef FMENU_H
#define FMENU_H
#if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
#error "Only <final/final.h> can be included directly."
#endif
#include <memory>
#include <tuple>
#include "final/fevent.h"
#include "final/menu/fmenulist.h"
#include "final/widget/fwindow.h"
namespace finalcut
{
// class forward declaration
class FMenuBar;
class FMenuItem;
//----------------------------------------------------------------------
// class FMenu
//----------------------------------------------------------------------
class FMenu : public FWindow
, public FMenuList
{
public:
// Using-declaration
using FMenuList::getItem;
using FMenuList::isSelected;
// Constructor
explicit FMenu (FWidget* = nullptr);
explicit FMenu (FString&&, FWidget* = nullptr);
// Disable copy constructor
FMenu (const FMenu&) = delete;
// Disable move constructor
FMenu (FMenu&&) noexcept = delete;
// Destructor
~FMenu() override;
// Disable copy assignment operator (=)
auto operator = (const FMenu&) -> FMenu& = delete;
// Disable move assignment operator (=)
auto operator = (FMenu&&) noexcept -> FMenu& = delete;
// Accessors
auto getClassName() const -> FString override;
auto getText() const -> FString;
auto getItem() -> FMenuItem*;
// Mutators
auto setEnable (bool = true) -> bool override;
auto unsetEnable() -> bool override;
auto setDisable() -> bool override;
void setSelected();
void unsetSelected();
auto setMenuWidget (bool = true) -> bool;
auto unsetMenuWidget() -> bool;
void setStatusbarMessage (const FString&) override;
void setMenu (FMenu*);
void setText (const FString&);
void resetColors() override;
// Inquiries
auto isSelected() const -> bool;
auto hasHotkey() const -> bool;
auto hasMenu() const -> bool;
// Methods
void show() override;
void hide() override;
// Event handlers
void onKeyPress (FKeyEvent*) override;
void onMouseDown (FMouseEvent*) override;
void onMouseUp (FMouseEvent*) override;
void onMouseMove (FMouseEvent*) override;
void onAccel (FAccelEvent*) override;
// Callback method
void cb_menuitemEnabled();
void cb_menuitemDisabled();
void cb_menuitemToggled (const FMenuItem*) const;
private:
struct MouseStates
{
uChar focus_changed : 1;
uChar hide_sub_menu : 1;
uChar mouse_over_menu : 1;
uChar mouse_over_submenu : 1;
uChar mouse_over_supermenu : 1;
uChar mouse_over_menubar : 1;
uChar : 2; // padding bits
};
struct MenuText
{
FString text;
std::size_t hotkeypos;
bool no_underline;
};
// Constants
static constexpr auto NOT_SET = static_cast<std::size_t>(-1);
static constexpr bool SELECT_ITEM = true;
// Accessors
auto getSuperMenu() const -> FWidget*;
// Mutators
void setSuperMenu (FWidget*);
// Inquiries
auto isDialog (const FWidget*) const -> bool;
auto isMenuBar (const FWidget*) const -> bool;
auto isMenu (const FWidget*) const -> bool;
auto isRadioMenuItem (const FWidget*) const -> bool;
auto isSubMenu() const -> bool;
auto isDialogMenu() const -> bool;
auto isMouseOverMenu (const FPoint&) -> bool;
auto isMouseOverSubMenu (const FPoint&) -> bool;
auto isMouseOverSuperMenu (const FPoint&) -> bool;
auto isMouseOverMenuBar (const FPoint&) const -> bool;
// Methods
void init();
void initCallbacks();
void calculateDimensions();
void adjustItems() const;
auto adjustX(int) const -> int;
void openSubMenu (FMenu*, bool = false);
void closeOpenedSubMenu();
void hideSubMenus();
void hideSuperMenus() const;
auto isMouseOverItem (const FPoint&, const FMenuItem*) const -> bool;
auto mouseDownOverList (const FPoint&) -> bool;
void mouseDownSubmenu (const FMenuItem*);
void mouseDownSelection (FMenuItem*, bool&);
auto mouseUpOverList (const FPoint&) -> bool;
void mouseMoveOverList (const FPoint&, MouseStates&);
void mouseMoveSelection (FMenuItem*, MouseStates&);
void mouseMoveDeselection (FMenuItem*, MouseStates&);
void mouseUpOverBorder();
void mouseMoveOverBorder (MouseStates&) const;
void passEventToSubMenu (const FMouseEvent&) const;
void passEventToSuperMenu (const FMouseEvent&);
void passEventToMenuBar (const FMouseEvent&) const;
template <typename WidgetT>
void passEventToWidget (WidgetT, const FMouseEvent&) const;
auto containsMenuStructure (const FPoint&) -> bool;
auto containsMenuStructure (int, int) -> bool;
auto superMenuAt (const FPoint&) -> FMenu*;
auto superMenuAt (int, int) -> FMenu*;
void selectItem_PostProcessing (FMenuItem*) override;
void keypressMenuBar (FKeyEvent*) const;
auto hotkeyFound (FKey, const FKeyEvent&) const -> bool;
auto hotkeyMenu (FKeyEvent*) -> bool;
void draw() override;
void drawItems();
void drawSeparator (int);
void drawMenuLine (FMenuItem*, int);
void drawCheckMarkPrefix (const FMenuItem*);
void drawMenuText (MenuText&);
void drawSubMenuIndicator (std::size_t&);
void drawAcceleratorKey (std::size_t&, FKey);
void drawTrailingSpaces (std::size_t);
void setLineAttributes (const FMenuItem*, int);
void setCursorToHotkeyPosition (FMenuItem*) const;
void selectPrevMenu (FKeyEvent*);
void selectNextMenu (FKeyEvent*);
void acceptSelection();
void closeMenu();
void processActivate() const;
// Data members
FMenuItem menuitem{};
FWidget* super_menu{nullptr};
FMenu* opened_sub_menu{nullptr};
FMenu* shown_sub_menu{nullptr};
std::size_t max_item_width{0};
std::size_t hotkeypos{NOT_SET};
bool mouse_down{false};
bool has_checkable_items{false};
// Friend functions
friend auto closeOpenMenus (FMenu*, const FPoint&) -> std::tuple<bool, bool>;
// Friend classes
friend class FCheckMenuItem;
friend class FDialog;
friend class FDialogListMenu;
friend class FMenuBar;
friend class FMenuItem;
friend class FRadioMenuItem;
};
// non-member function forward declarations
//----------------------------------------------------------------------
auto closeOpenMenus (FMenu*, const FPoint&) -> std::tuple<bool, bool>;
// FMenu inline functions
//----------------------------------------------------------------------
inline auto FMenu::getClassName() const -> FString
{ return "FMenu"; }
//----------------------------------------------------------------------
inline auto FMenu::getText() const -> FString
{ return menuitem.getText(); }
//----------------------------------------------------------------------
inline auto FMenu::getItem() -> FMenuItem*
{ return &menuitem; }
//----------------------------------------------------------------------
inline auto FMenu::setEnable (bool enable) -> bool
{ return menuitem.setEnable(enable); }
//----------------------------------------------------------------------
inline auto FMenu::unsetEnable() -> bool
{ return menuitem.unsetEnable(); }
//----------------------------------------------------------------------
inline auto FMenu::setDisable() -> bool
{ return menuitem.setDisable(); }
//----------------------------------------------------------------------
inline void FMenu::setSelected()
{ menuitem.setSelected(); }
//----------------------------------------------------------------------
inline void FMenu::unsetSelected()
{ menuitem.unsetSelected(); }
//----------------------------------------------------------------------
inline auto FMenu::unsetMenuWidget() -> bool
{ return setMenuWidget(false); }
//----------------------------------------------------------------------
inline void FMenu::setMenu (FMenu* m)
{ menuitem.setMenu(m); }
//----------------------------------------------------------------------
inline void FMenu::setText (const FString& txt)
{ menuitem.setText(txt); }
//----------------------------------------------------------------------
inline auto FMenu::isSelected() const -> bool
{ return menuitem.isSelected(); }
//----------------------------------------------------------------------
inline auto FMenu::hasHotkey() const -> bool
{ return menuitem.hasHotkey(); }
//----------------------------------------------------------------------
inline auto FMenu::hasMenu() const -> bool
{ return menuitem.hasMenu(); }
//----------------------------------------------------------------------
inline auto FMenu::getSuperMenu() const -> FWidget*
{ return super_menu; }
//----------------------------------------------------------------------
inline void FMenu::setSuperMenu (FWidget* smenu)
{ super_menu = smenu; }
//----------------------------------------------------------------------
template <typename WidgetT>
inline void FMenu::passEventToWidget (WidgetT widget, const FMouseEvent& ev) const
{
// Mouse event handover to given widget
const auto& type = ev.getType();
const auto& tpos = ev.getTermPos();
const auto& par = widget->termToWidgetPos(tpos);
const MouseButton btn = ev.getButton();
const auto& new_ev = \
std::make_shared<FMouseEvent>(type, par, tpos, btn);
setClickedWidget(widget);
widget->mouse_down = true;
widget->onMouseMove(new_ev.get());
}
//----------------------------------------------------------------------
inline auto FMenu::containsMenuStructure (const FPoint& p) -> bool
{ return containsMenuStructure (p.getX(), p.getY()); }
//----------------------------------------------------------------------
inline auto FMenu::superMenuAt (const FPoint& p) -> FMenu*
{ return superMenuAt (p.getX(), p.getY()); }
//----------------------------------------------------------------------
inline void FMenu::onAccel (FAccelEvent* ev)
{ menuitem.onAccel(ev); }
} // namespace finalcut
#endif // FMENU_H
|