2012-01-06 12:12:57 +08:00
|
|
|
// ASEPRITE gui library
|
2012-01-06 11:52:11 +08:00
|
|
|
// Copyright (C) 2001-2012 David Capello
|
2010-09-28 06:18:17 +08:00
|
|
|
//
|
|
|
|
// This source file is ditributed under a BSD-like license, please
|
|
|
|
// read LICENSE.txt for more information.
|
|
|
|
|
2012-06-18 09:49:58 +08:00
|
|
|
#ifndef UI_WIDGET_H_INCLUDED
|
|
|
|
#define UI_WIDGET_H_INCLUDED
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2010-03-11 10:45:19 +08:00
|
|
|
#include <string>
|
|
|
|
|
2011-01-28 10:30:11 +08:00
|
|
|
#include "gfx/border.h"
|
2012-01-02 01:37:03 +08:00
|
|
|
#include "gfx/rect.h"
|
|
|
|
#include "gfx/size.h"
|
2012-06-18 09:49:58 +08:00
|
|
|
#include "ui/base.h"
|
|
|
|
#include "ui/component.h"
|
|
|
|
#include "ui/rect.h"
|
|
|
|
#include "ui/widgets_list.h"
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-06-16 10:37:59 +08:00
|
|
|
#define ASSERT_VALID_WIDGET(widget) ASSERT((widget) != NULL)
|
2008-01-04 07:22:04 +08:00
|
|
|
|
2007-09-19 07:57:02 +08:00
|
|
|
struct FONT;
|
|
|
|
struct BITMAP;
|
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
namespace ui {
|
|
|
|
|
2012-07-09 10:24:42 +08:00
|
|
|
union Message;
|
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
class InitThemeEvent;
|
|
|
|
class Manager;
|
2012-07-09 10:24:42 +08:00
|
|
|
class PaintEvent;
|
|
|
|
class PreferredSizeEvent;
|
|
|
|
class Theme;
|
|
|
|
class Window;
|
2012-06-18 09:02:54 +08:00
|
|
|
|
|
|
|
int ji_register_widget_type();
|
2012-04-16 02:21:24 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
// Position and geometry
|
2011-03-31 11:18:28 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
JRect jwidget_get_rect(Widget* widget);
|
|
|
|
JRect jwidget_get_child_rect(Widget* widget);
|
|
|
|
JRegion jwidget_get_region(Widget* widget);
|
|
|
|
JRegion jwidget_get_drawable_region(Widget* widget, int flags);
|
|
|
|
int jwidget_get_bg_color(Widget* widget);
|
|
|
|
int jwidget_get_text_length(const Widget* widget);
|
|
|
|
int jwidget_get_text_height(const Widget* widget);
|
|
|
|
void jwidget_get_texticon_info(Widget* widget,
|
|
|
|
JRect box, JRect text, JRect icon,
|
|
|
|
int icon_align, int icon_w, int icon_h);
|
|
|
|
|
|
|
|
void jwidget_noborders(Widget* widget);
|
|
|
|
void jwidget_set_border(Widget* widget, int value);
|
|
|
|
void jwidget_set_border(Widget* widget, int l, int t, int r, int b);
|
|
|
|
void jwidget_set_rect(Widget* widget, JRect rect);
|
|
|
|
void jwidget_set_min_size(Widget* widget, int w, int h);
|
|
|
|
void jwidget_set_max_size(Widget* widget, int w, int h);
|
|
|
|
void jwidget_set_bg_color(Widget* widget, int color);
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class Widget : public Component
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int type; /* widget's type */
|
|
|
|
|
|
|
|
JRect rc; /* position rectangle */
|
|
|
|
struct {
|
|
|
|
int l, t, r, b;
|
|
|
|
} border_width; /* border separation with the parent */
|
|
|
|
int child_spacing; /* separation between children */
|
|
|
|
|
|
|
|
/* flags */
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
/* widget size limits */
|
|
|
|
int min_w, min_h;
|
|
|
|
int max_w, max_h;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string m_id; // Widget's id
|
|
|
|
Theme* m_theme; // Widget's theme
|
|
|
|
int m_align; // Widget alignment
|
|
|
|
std::string m_text; // Widget text
|
|
|
|
struct FONT *m_font; // Text font type
|
|
|
|
int m_bg_color; // Background color
|
|
|
|
JRegion m_update_region; // Region to be redrawed.
|
2012-07-18 08:42:02 +08:00
|
|
|
WidgetsList m_children; // Sub-widgets
|
|
|
|
Widget* m_parent; // Who is the parent?
|
2012-06-18 09:02:54 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Extra data for the theme
|
|
|
|
void *theme_data[4];
|
|
|
|
|
|
|
|
/* for user */
|
|
|
|
void *user_data[4];
|
|
|
|
|
|
|
|
// ===============================================================
|
|
|
|
// CTOR & DTOR
|
|
|
|
// ===============================================================
|
|
|
|
|
|
|
|
Widget(int type);
|
|
|
|
virtual ~Widget();
|
|
|
|
|
|
|
|
// Safe way to delete a widget when it is not in the manager message
|
|
|
|
// queue anymore.
|
|
|
|
void deferDelete();
|
|
|
|
|
|
|
|
// Main properties.
|
|
|
|
|
|
|
|
int getType() const { return this->type; }
|
|
|
|
|
|
|
|
const std::string& getId() const { return m_id; }
|
|
|
|
void setId(const char* id) { m_id = id; }
|
|
|
|
|
|
|
|
int getAlign() const { return m_align; }
|
|
|
|
void setAlign(int align) { m_align = align; }
|
|
|
|
|
|
|
|
// Text property.
|
|
|
|
|
|
|
|
bool hasText() const { return flags & JI_NOTEXT ? false: true; }
|
|
|
|
|
|
|
|
const char* getText() const { return m_text.c_str(); }
|
|
|
|
int getTextInt() const;
|
|
|
|
double getTextDouble() const;
|
|
|
|
size_t getTextSize() const { return m_text.size(); }
|
|
|
|
void setText(const char* text);
|
|
|
|
void setTextf(const char* text, ...);
|
|
|
|
void setTextQuiet(const char* text);
|
|
|
|
|
|
|
|
// ===============================================================
|
|
|
|
// COMMON PROPERTIES
|
|
|
|
// ===============================================================
|
|
|
|
|
|
|
|
// True if this widget and all its ancestors are visible.
|
|
|
|
bool isVisible() const;
|
|
|
|
void setVisible(bool state);
|
|
|
|
|
|
|
|
// True if this widget can receive user input (is not disabled).
|
|
|
|
bool isEnabled() const;
|
|
|
|
void setEnabled(bool state);
|
|
|
|
|
|
|
|
// True if this widget is selected (pushed in case of a button, or
|
|
|
|
// checked in the case of a check-box).
|
|
|
|
bool isSelected() const;
|
|
|
|
void setSelected(bool state);
|
|
|
|
|
|
|
|
// True if this widget wants more space when it's inside a Box
|
|
|
|
// parent.
|
|
|
|
bool isExpansive() const;
|
|
|
|
void setExpansive(bool state);
|
|
|
|
|
|
|
|
// True if this is a decorative widget created by the current
|
|
|
|
// theme. Decorative widgets are arranged by the theme instead that
|
|
|
|
// the parent's widget.
|
|
|
|
bool isDecorative() const;
|
|
|
|
void setDecorative(bool state);
|
|
|
|
|
|
|
|
// True if this widget can receive the keyboard focus.
|
|
|
|
bool isFocusStop() const;
|
|
|
|
void setFocusStop(bool state);
|
|
|
|
|
|
|
|
// True if this widget wants the focus by default when it's shown by
|
|
|
|
// first time (e.g. when its parent window is opened).
|
|
|
|
void setFocusMagnet(bool state);
|
|
|
|
bool isFocusMagnet() const;
|
|
|
|
|
|
|
|
// ===============================================================
|
|
|
|
// LOOK & FEEL
|
|
|
|
// ===============================================================
|
|
|
|
|
|
|
|
FONT* getFont() const;
|
|
|
|
void setFont(FONT* font);
|
|
|
|
|
|
|
|
// Gets the background color of the widget.
|
|
|
|
int getBgColor() const {
|
2012-07-18 08:42:02 +08:00
|
|
|
if (m_bg_color < 0 && m_parent)
|
|
|
|
return m_parent->getBgColor();
|
2012-06-18 09:02:54 +08:00
|
|
|
else
|
|
|
|
return m_bg_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the background color of the widget
|
|
|
|
void setBgColor(int bg_color) {
|
|
|
|
m_bg_color = bg_color;
|
|
|
|
}
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
Theme* getTheme() const { return m_theme; }
|
|
|
|
void setTheme(Theme* theme);
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
void initTheme();
|
2007-09-19 07:57:02 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
// ===============================================================
|
|
|
|
// PARENTS & CHILDREN
|
|
|
|
// ===============================================================
|
2009-05-31 05:22:52 +08:00
|
|
|
|
2012-07-09 10:24:42 +08:00
|
|
|
Window* getRoot();
|
2012-07-18 08:42:02 +08:00
|
|
|
Widget* getParent() { return m_parent; }
|
2012-06-18 09:02:54 +08:00
|
|
|
Manager* getManager();
|
2009-05-31 05:22:52 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
// Returns a list of parents (you must free the list), if
|
|
|
|
// "ascendant" is true the list is build from child to parents, else
|
|
|
|
// the list is from parent to children.
|
2012-07-18 08:42:02 +08:00
|
|
|
void getParents(bool ascendant, WidgetsList& parents);
|
2011-06-26 06:10:31 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
// Returns a list of children (you must free the list).
|
2012-07-18 08:42:02 +08:00
|
|
|
const WidgetsList& getChildren() const { return m_children; }
|
2010-01-26 08:38:05 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
// Returns the next or previous siblings.
|
|
|
|
Widget* getNextSibling();
|
|
|
|
Widget* getPreviousSibling();
|
2009-05-31 05:22:52 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
Widget* pick(int x, int y);
|
|
|
|
bool hasChild(Widget* child);
|
|
|
|
Widget* findChild(const char* id);
|
|
|
|
|
|
|
|
// Returns a widget in the same window that is located "sibling".
|
|
|
|
Widget* findSibling(const char* id);
|
|
|
|
|
|
|
|
// Finds a child with the specified ID and dynamic-casts it to type
|
|
|
|
// T.
|
|
|
|
template<class T>
|
|
|
|
T* findChildT(const char* id) {
|
|
|
|
return dynamic_cast<T*>(findChild(id));
|
2012-06-16 10:37:59 +08:00
|
|
|
}
|
2010-12-05 02:56:28 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
template<class T>
|
|
|
|
T* findFirstChildByType() {
|
2012-07-18 08:42:02 +08:00
|
|
|
UI_FOREACH_WIDGET(m_children, it) {
|
|
|
|
Widget* child = *it;
|
2012-06-18 09:02:54 +08:00
|
|
|
if (T* specificChild = dynamic_cast<T*>(child))
|
|
|
|
return specificChild;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addChild(Widget* child);
|
|
|
|
void removeChild(Widget* child);
|
|
|
|
void replaceChild(Widget* oldChild, Widget* newChild);
|
|
|
|
void insertChild(int index, Widget* child);
|
|
|
|
|
|
|
|
// ===============================================================
|
|
|
|
// LAYOUT & CONSTRAINT
|
|
|
|
// ===============================================================
|
2011-03-30 08:35:17 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
void layout();
|
2011-03-09 10:18:04 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
// ===============================================================
|
|
|
|
// POSITION & GEOMETRY
|
|
|
|
// ===============================================================
|
2011-03-09 10:18:04 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
gfx::Rect getBounds() const;
|
|
|
|
gfx::Rect getClientBounds() const;
|
|
|
|
void setBounds(const gfx::Rect& rc);
|
2010-01-26 08:38:05 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
gfx::Border getBorder() const;
|
|
|
|
void setBorder(const gfx::Border& border);
|
2010-01-26 08:38:05 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
// ===============================================================
|
|
|
|
// REFRESH ISSUES
|
|
|
|
// ===============================================================
|
2011-01-28 10:30:11 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
bool isDoubleBuffered();
|
|
|
|
void setDoubleBuffered(bool doubleBuffered);
|
2011-01-22 06:45:04 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
void invalidate();
|
|
|
|
void invalidateRect(const gfx::Rect& rect);
|
|
|
|
void invalidateRect(const JRect rect);
|
|
|
|
void invalidateRegion(const JRegion region);
|
2011-02-06 03:26:01 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
void flushRedraw();
|
2011-01-22 06:45:04 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
void scrollRegion(JRegion region, int dx, int dy);
|
2012-03-21 03:24:46 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
// ===============================================================
|
|
|
|
// GUI MANAGER
|
|
|
|
// ===============================================================
|
2012-03-21 03:13:37 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
bool sendMessage(Message* msg);
|
|
|
|
void closeWindow();
|
2010-01-26 08:38:05 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
void broadcastMouseMessage(WidgetsList& targets);
|
2010-04-25 23:03:25 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
// ===============================================================
|
|
|
|
// SIZE & POSITION
|
|
|
|
// ===============================================================
|
2011-03-31 11:18:28 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
gfx::Size getPreferredSize();
|
|
|
|
gfx::Size getPreferredSize(const gfx::Size& fitIn);
|
|
|
|
void setPreferredSize(const gfx::Size& fixedSize);
|
|
|
|
void setPreferredSize(int fixedWidth, int fixedHeight);
|
2010-08-03 09:57:41 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
// ===============================================================
|
|
|
|
// MOUSE, FOCUS & KEYBOARD
|
|
|
|
// ===============================================================
|
2010-08-03 09:57:41 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
void requestFocus();
|
|
|
|
void releaseFocus();
|
|
|
|
void captureMouse();
|
|
|
|
void releaseMouse();
|
|
|
|
bool hasFocus();
|
|
|
|
bool hasMouse();
|
|
|
|
bool hasMouseOver();
|
|
|
|
bool hasCapture();
|
2010-04-25 23:03:25 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
// Returns lower-case letter that represet the mnemonic of the widget
|
|
|
|
// (the underscored character, i.e. the letter after & symbol).
|
|
|
|
int getMnemonicChar() const;
|
2009-05-31 05:22:52 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
bool isScancodeMnemonic(int scancode) const;
|
2012-04-16 02:43:07 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
protected:
|
2012-04-16 02:43:07 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
// ===============================================================
|
|
|
|
// MESSAGE PROCESSING
|
|
|
|
// ===============================================================
|
2010-08-03 09:57:41 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
virtual bool onProcessMessage(Message* msg);
|
2010-08-03 09:57:41 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
// ===============================================================
|
|
|
|
// EVENTS
|
|
|
|
// ===============================================================
|
2009-11-19 10:59:20 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
virtual void onInvalidateRegion(const JRegion region);
|
|
|
|
virtual void onPreferredSize(PreferredSizeEvent& ev);
|
|
|
|
virtual void onPaint(PaintEvent& ev);
|
|
|
|
virtual void onBroadcastMouseMessage(WidgetsList& targets);
|
|
|
|
virtual void onInitTheme(InitThemeEvent& ev);
|
|
|
|
virtual void onEnable();
|
|
|
|
virtual void onDisable();
|
|
|
|
virtual void onSelect();
|
|
|
|
virtual void onDeselect();
|
|
|
|
virtual void onSetText();
|
2010-08-03 09:57:41 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
private:
|
|
|
|
gfx::Size* m_preferredSize;
|
|
|
|
bool m_doubleBuffered : 1;
|
|
|
|
};
|
2010-08-03 09:57:41 +08:00
|
|
|
|
2012-06-18 09:02:54 +08:00
|
|
|
} // namespace ui
|
2009-05-31 05:22:52 +08:00
|
|
|
|
2009-08-18 05:38:00 +08:00
|
|
|
#endif
|