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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
|
/*
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
*
* This file is part of lsp-tk-lib
* Created on: 15 июн. 2017 г.
*
* lsp-tk-lib 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
* any later version.
*
* lsp-tk-lib 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 lsp-tk-lib. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef LSP_PLUG_IN_TK_BASE_WIDGET_H_
#define LSP_PLUG_IN_TK_BASE_WIDGET_H_
#ifndef LSP_PLUG_IN_TK_IMPL
#error "use <lsp-plug.in/tk/tk.h>"
#endif
namespace lsp
{
namespace tk
{
class Display;
// Style definition
namespace style
{
LSP_TK_STYLE_DEF_BEGIN(Widget, Style)
prop::Allocation sAllocation; // Widget allocation
prop::Float sScaling; // UI scaling factor
prop::Float sFontScaling; // UI font scaling factor
prop::Float sBrightness; // Brightness
prop::Float sBgBrightness; // Brightness for background
prop::Padding sPadding; // Widget padding
prop::Color sBgColor; // Color of the background of the widget
prop::Boolean sBgInherit; // Inhert background color from parent container
prop::Boolean sVisibility; // Visibility
prop::Pointer sPointer; // Mouse pointer
prop::Integer sTag; // Some tag associated with widget
prop::DrawMode sDrawMode; // Drawing mode
LSP_TK_STYLE_DEF_END
}
/** Basic widget class for any widget in the toolkit
*
*/
class Widget: public ws::IEventHandler
{
private:
Widget & operator = (const Widget &);
Widget(const Widget &);
public:
static const w_class_t metadata;
protected:
enum flags_t
{
INITIALIZED = 1 << 0, // Widget is initialized
FINALIZED = 1 << 1, // Widget is in destroy state
REDRAW_SURFACE = 1 << 2, // Need to redraw surface
REDRAW_CHILD = 1 << 3, // Need to redraw child only
SIZE_INVALID = 1 << 4, // Size limit structure is valid
RESIZE_PENDING = 1 << 5, // The resize request is pending
REALIZE_ACTIVE = 1 << 6 // Realize is active, no need to trigger for realize
};
protected:
class PropListener: public prop::Listener
{
protected:
Widget *pWidget;
public:
inline PropListener(Widget *w) { pWidget = w; }
virtual void notify(Property *prop);
};
protected:
size_t nFlags; // Flags
const w_class_t *pClass; // Widget class descriptor
Display *pDisplay; // Pointer to display
Widget *pParent; // Parent widget
ws::ISurface *pSurface; // Drawing surface
ws::size_limit_t sLimit; // Cached pre-computed size limit
ws::rectangle_t sSize; // Real allocated geometry of widget
SlotSet sSlots; // Slots
Style sStyle; // Style
PropListener sProperties; // Properties listener
prop::Allocation sAllocation; // Widget allocation
prop::Float sScaling; // UI scaling factor
prop::Float sFontScaling; // UI font scaling factor
prop::Float sBrightness; // Brightness
prop::Float sBgBrightness; // Brightness for background
prop::Padding sPadding; // Widget padding
prop::Color sBgColor; // Color of the background of the widget
prop::Boolean sBgInherit; // Inhert background color from parent container
prop::Boolean sBgApplyBrightness; // Apply brightness settings to the background
prop::Boolean sVisibility; // Visibility
prop::Pointer sPointer; // Mouse pointer
prop::Integer sTag; // Some tag associated with widget
prop::DrawMode sDrawMode; // Drawing mode of widget
//---------------------------------------------------------------------------------
// Slot handlers
protected:
static status_t slot_mouse_move(Widget *sender, void *ptr, void *data);
static status_t slot_mouse_down(Widget *sender, void *ptr, void *data);
static status_t slot_mouse_up(Widget *sender, void *ptr, void *data);
static status_t slot_mouse_click(Widget *sender, void *ptr, void *data);
static status_t slot_mouse_dbl_click(Widget *sender, void *ptr, void *data);
static status_t slot_mouse_tri_click(Widget *sender, void *ptr, void *data);
static status_t slot_mouse_scroll(Widget *sender, void *ptr, void *data);
static status_t slot_mouse_in(Widget *sender, void *ptr, void *data);
static status_t slot_mouse_out(Widget *sender, void *ptr, void *data);
static status_t slot_key_down(Widget *sender, void *ptr, void *data);
static status_t slot_key_up(Widget *sender, void *ptr, void *data);
static status_t slot_hide(Widget *sender, void *ptr, void *data);
static status_t slot_show(Widget *sender, void *ptr, void *data);
static status_t slot_destroy(Widget *sender, void *ptr, void *data);
static status_t slot_resize(Widget *sender, void *ptr, void *data);
static status_t slot_resize_parent(Widget *sender, void *ptr, void *data);
static status_t slot_focus_in(Widget *sender, void *ptr, void *data);
static status_t slot_focus_out(Widget *sender, void *ptr, void *data);
static status_t slot_drag_request(Widget *sender, void *ptr, void *data);
static status_t slot_realized(Widget *sender, void *ptr, void *data);
static status_t slot_mouse_pointer(Widget *sender, void *ptr, void *data);
//---------------------------------------------------------------------------------
// Interface for nested classes
protected:
void do_destroy();
void unlink_widget(Widget *widget);
/**
* Callback on call when property has been change
* @param prop property that has been changed
*/
virtual void property_changed(Property *prop);
/** Request widget for it's minimum and maximum size.
* Since parent widget is responsible for padding,
* widget should return it's maximum size without applying
* it's padding parameter
*
* @param limit the widget size constraints to fill
*/
virtual void size_request(ws::size_limit_t *r);
/**
* Realize widget internally
* @param r real area allocated to the widget
*/
virtual void realize(const ws::rectangle_t *r);
/** Hide widget
*
*/
virtual void hide_widget();
/** Show widget
*
*/
virtual void show_widget();
//---------------------------------------------------------------------------------
// Construction and destruction
public:
explicit Widget(Display *dpy);
virtual ~Widget();
/** Initialize wiget
*
*/
virtual status_t init();
/** Destroy widget
*
*/
virtual void destroy();
//---------------------------------------------------------------------------------
// Metadata, casting and type information
public:
/** Get widget class
*
* @return actual widget class metadata
*/
inline const w_class_t *get_class() const { return pClass; }
/**
* Get style class
* @return style class
*/
virtual const char *style_class() const;
/** Check wheter the widget is instance of some class
*
* @param wclass widget class
* @return true if widget is instance of some class
*/
bool instance_of(const w_class_t *wclass) const;
inline bool instance_of(const w_class_t &wclass) const { return instance_of(&wclass); }
/** Another way to check if widget is instance of some class
*
* @return true if widget is instance of some class
*/
template <class LSPTarget>
inline bool instance_of() const { return instance_of(&LSPTarget::metadata); };
/** Cast widget to another type
*
* @return pointer to widget or NULL if cast failed
*/
template <class LSPTarget>
inline LSPTarget *cast() { return instance_of(&LSPTarget::metadata) ? static_cast<LSPTarget *>(this) : NULL; }
/** Cast widget to another type
*
* @return pointer to widget or NULL if cast failed
*/
template <class LSPTarget>
inline const LSPTarget *cast() const { return instance_of(&LSPTarget::metadata) ? static_cast<const LSPTarget *>(this) : NULL; }
/** Get pointer to self as pointer to Widget class
*
* @return pointer to self
*/
inline Widget *self() { return this; }
//---------------------------------------------------------------------------------
// Properties
public:
/** Get display
*
* @return display
*/
inline Display *display() { return pDisplay; }
/** Get actual widget dimensions without padding
*
* @param r real widget dimensions
*/
inline void get_rectangle(ws::rectangle_t *r) { *r = sSize; }
/** Get actual widget dimensions with padding applied
*
* @param r real widget dimensions
*/
inline void get_padded_rectangle(ws::rectangle_t *r) { sPadding.leave(r, &sSize, sScaling.get()); }
/**
* Get position of the widget relative to the screen
* @param r pointer to store result
* @param sr source rectangle that belongs to the widget
* @return status of operation
*/
virtual status_t get_screen_rectangle(ws::rectangle_t *r, const ws::rectangle_t *sr);
virtual status_t get_screen_rectangle(ws::rectangle_t *r);
/**
* Get position of the widget relative to the screen with applied padding
* @param r pointer to store result
* @return status of operation
*/
virtual status_t get_padded_screen_rectangle(ws::rectangle_t *r, const ws::rectangle_t *sr);
virtual status_t get_padded_screen_rectangle(ws::rectangle_t *r);
/** Request widget for it's minimum and maximum size.
* Since parent widget is responsible for padding,
* widget should return it's maximum size without applying
* it's padding parameter
*
* @param l the widget size constraints to fill
*/
void get_size_limits(ws::size_limit_t *l);
/**
* Get size limit with padding
* @param l size limit to calculate
*/
void get_padded_size_limits(ws::size_limit_t *l);
/**
* Get actual padding in pixels
* @param p pointer to store computed padding
*/
void get_padding(padding_t *p);
/**
* Check whether widget is focused
* @return true if widget is focsed
*/
bool has_focus() const;
/**
* Make widget being focused
* @return true if widget has been focused, false if widget already had focus or it is impossible
* to take the focus
*/
virtual bool take_focus();
/**
* Release focus previously acquired by widget
* @return true if focus has been released, false if widget had no focus
*/
virtual bool kill_focus();
/**
* Get current mouse pointer
* @param x current horizontal pointer in window coordinates
* @param y current vertical pointer in window coordinates
* @return current mouse pointer
*/
virtual ws::mouse_pointer_t current_pointer(ssize_t x, ssize_t y);
/** Check if there is redraw request pending
*
* @return true if there is redraw request pending
*/
inline bool redraw_pending() const { return nFlags & (REDRAW_SURFACE | REDRAW_CHILD); }
/** Check if there is resize request pending
*
* @return true if there is resize request pending
*/
inline bool resize_pending() const { return nFlags & (SIZE_INVALID | RESIZE_PENDING); }
/** Check that specified window coordinate lies within widget's bounds
* Always returns false for invisible widgets
*
* @param left x coordinate
* @param top y coordinate
* @return true on success
*/
virtual bool inside(ssize_t left, ssize_t top);
/** Get parent widget
*
* @return parent widget
*/
inline Widget *parent() { return pParent; }
/**
* Find parent widget of the specified type
* @param meta metadata
* @return the parent widget of the specified type or NULL
*/
Widget *parent(const w_class_t *meta);
/** Get slots
*
* @return slots
*/
inline SlotSet *slots() { return &sSlots; }
/** Get slot
*
* @param id slot identifier
* @return pointer to slot or NULL
*/
inline Slot *slot(slot_t id) { return sSlots.slot(id); }
/**
* Check that widget is in valid state
* @return true if widget is in valid state
*/
inline bool valid() const { return (nFlags & (INITIALIZED | FINALIZED)) == INITIALIZED; }
/**
* Check whether widget is visible child of parent widget
* @param parent parent widget
* @return true if widget is visible child of parent widget
*/
bool is_visible_child_of(const Widget *parent) const;
/**
* Compute the actual background color and store it to the passed color value
* @param color color to store the actual value
* @param brightness apply brightness value, if negative then bg_brightness will be applied
*/
virtual void get_actual_bg_color(lsp::Color *color, float brightness = -1.0f) const;
void get_actual_bg_color(lsp::Color &color, float brightness = -1.0f) const;
/**
* Return widget's style
* @return widget's style
*/
LSP_TK_PROPERTY(Style, style, &sStyle)
/** Get widget padding. All widgets should properly handle this parameter
*
* @return widget padding
*/
LSP_TK_PROPERTY(Padding, padding, &sPadding)
/**
* Get background color of the widget
* @return background color of the widget
*/
LSP_TK_PROPERTY(Color, bg_color, &sBgColor)
/**
* Get the inheritance flag of the background color.
* If enabled, the color is taken from the parent widget container if it
* is present. Otherwise, the bg_color() property is used to draw the color.
*
* @return inheritance flag of the background color
*/
LSP_TK_PROPERTY(Boolean, bg_inherit, &sBgInherit)
/**
* Get brightness property
* @return brightness property
*/
LSP_TK_PROPERTY(Float, brightness, &sBrightness)
/**
* Get brightness property for background
* @return brightness property for background
*/
LSP_TK_PROPERTY(Float, bg_brightness, &sBgBrightness)
/**
* Get widget scaling property
* @return widget scaling property
*/
LSP_TK_PROPERTY(Float, scaling, &sScaling)
/**
* Get font scaling property
* @return font scaling property
*/
LSP_TK_PROPERTY(Float, font_scaling, &sFontScaling)
/**
* Widget allocation flags
* @return widget allocation flags
*/
LSP_TK_PROPERTY(Allocation, allocation, &sAllocation)
/**
* Visibility of the widget
* @return visibility of the widget
*/
LSP_TK_PROPERTY(Boolean, visibility, &sVisibility)
/**
* Style of mouse pointer
* @return mouse pointer style
*/
LSP_TK_PROPERTY(Pointer, pointer, &sPointer)
/**
* Some tag associated with widget, can be used as the user decides
*/
LSP_TK_PROPERTY(Integer, tag, &sTag)
/**
* Drawing mode of the widget
*/
LSP_TK_PROPERTY(DrawMode, draw_mode, &sDrawMode)
//---------------------------------------------------------------------------------
// Manipulation
public:
/**
* Return mouse handler
* @param x left coordinate of the mouse pointer relative to the top-level widget
* @param y top coordinate of the mouse pointer relative to the top-level widget
* @return pointer to widget that will handle mouse events
*/
virtual Widget *find_widget(ssize_t x, ssize_t y);
/** Query widget for redraw
*
* @param flags redraw flags
*/
virtual void query_draw(size_t flags = REDRAW_SURFACE);
/**
* Put the widget to the destroy queue of the main loop
* @return status of operation
*/
virtual status_t queue_destroy();
/** Query widget for resize
*
*/
virtual void query_resize();
/** Get widget surface
*
* @param s base surface
* @return widget surface or NULL
*/
ws::ISurface *get_surface(ws::ISurface *s);
/** Get widget surface
*
* @param s base surface
* @param width requested width
* @param height requested height
* @return widget surface or NULL
*/
ws::ISurface *get_surface(ws::ISurface *s, ssize_t width, ssize_t height);
/** Render widget to the external surface
*
* @param surface surface to perform rendering
* @param area the actual area that will be used for drawing
* @param force force child rendering
*/
virtual void render(ws::ISurface *s, const ws::rectangle_t *area, bool force);
/** Draw widget on the internal surface
*
* @param surface surface to perform drawing
*/
virtual void draw(ws::ISurface *s);
/**
* Realize widget by passing it's dimensions.
* Since parent widget is responsible for padding, widget dimensions should
* be passed without padding applied to the widget
*
* @param r real area allocated to the widget
* @param redraw query for redraw
*/
void realize_widget(const ws::rectangle_t *r);
/** Handle UI event from the display
*
* @param e UI event
* @return status of operation
*/
virtual status_t handle_event(const ws::event_t *e);
/** Set parent widget of this widget
*
* @param parent parent widget
*/
void set_parent(Widget *parent);
/** Commit widet redraw
*
*/
virtual void commit_redraw();
/**
* Show widget
*/
virtual void show();
/**
* Hide widget
*/
virtual void hide();
/** Get most top-level widget
*
* @return most top-level widget
*/
Widget *toplevel();
/** Check that widget has the specified widget as parent in the widget hierarchy
*
* @return most top-level widget
*/
bool has_parent(const tk::Widget *w) const;
//---------------------------------------------------------------------------------
// Event handling
public:
/** Widget has taken focus
*
* @param e event
* @return status of operation
*/
virtual status_t on_focus_in(const ws::event_t *e);
/** Widget has lost focus
*
* @param e event
* @return status of operation
*/
virtual status_t on_focus_out(const ws::event_t *e);
/** Handle key press event
* @param e event
* @return status of operation
*/
virtual status_t on_key_down(const ws::event_t *e);
/** Handle key release event
* @param e event
* @return status of operation
*/
virtual status_t on_key_up(const ws::event_t *e);
/** Handle mouse button press event
* @param e event
* @return status of operation
*/
virtual status_t on_mouse_down(const ws::event_t *e);
/** Handle mouse button release event
* @param e event
* @return status of operation
*/
virtual status_t on_mouse_up(const ws::event_t *e);
/** Handle mouse motion event
* @param e event
* @return status of operation
*/
virtual status_t on_mouse_move(const ws::event_t *e);
/** Handle mouse cursor moved into the zone of widget
*
* @param e mouse event that triggered MouseIn
* @return status of operation
*/
virtual status_t on_mouse_in(const ws::event_t *e);
/** Handle mouse cursor moved outside the zone of widget
*
* @param e mouse event that triggered MouseIn
* @return status of operation
*/
virtual status_t on_mouse_out(const ws::event_t *e);
/** Handle mouse scroll event
*
* @param e event
* @return status of operation
*/
virtual status_t on_mouse_scroll(const ws::event_t *e);
/** Handle single mouse click
*
* @param e event
* @return status of operation
*/
virtual status_t on_mouse_click(const ws::event_t *e);
/** Handle mouse double click event
*
* @param e event
* @return status of operation
*/
virtual status_t on_mouse_dbl_click(const ws::event_t *e);
/** Handle mouse triple click
*
* @param e event
* @return status of operation
*/
virtual status_t on_mouse_tri_click(const ws::event_t *e);
/**
* Solve the actual value for the mouse pointer
* @param ev pointer event for determining the mouse pointer
* @return status of operation
*/
virtual status_t on_mouse_pointer(tk::pointer_event_t *ev);
/** Geometry has changed: size or position
*
* @param r the new size of the widget. Handler should not rely
* on data returned by get_rectangle() and get_padded_rectangle()
* since it will contain the previous values
* @return status of operation
*/
virtual status_t on_resize(const ws::rectangle_t *r);
/** Geometry of parent widget has changed: size or position
*
* @param e event
* @return status of operation
*/
virtual status_t on_resize_parent(const ws::rectangle_t *r);
/** The widget becomes hidden
*
* @return status of operation
*/
virtual status_t on_hide();
/** The widget becomes visible
*
* @return status of operation
*/
virtual status_t on_show();
/** The widget becomes destroyed
*
* @return status of operation
*/
virtual status_t on_destroy();
/**
* Process the drag request event
* @param e drag request event
* @param ctype NULL-terminated list of provided content types
* @return status of operation
*/
virtual status_t on_drag_request(const ws::event_t *e, const char * const *ctype);
/** The widget has just been realized
* @param size the actual widget size
* @return status of operation
*/
virtual status_t on_realized(const ws::rectangle_t *size);
};
}
// LLTL interface specifications for widget
namespace lltl
{
template <>
struct hash_spec<tk::Widget>: public ptr_hash_iface {};
template <>
struct compare_spec<tk::Widget>: public ptr_compare_iface {};
}
} /* namespace lsp */
#endif /* LSP_PLUG_IN_TK_BASE_WIDGET_H_ */
|