ManaPlus
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends
GuiTable Class Reference

#include <guitable.h>

Inheritance diagram for GuiTable:
Widget MouseListener KeyListener TableModelListener Widget2

Public Member Functions

 GuiTable (const Widget2 *const widget, TableModel *const initial_model, const Opaque opacity)
 
 ~GuiTable ()
 
void setModel (TableModel *const m)
 
const TableModelgetModel () const
 
void setSelected (const int row, const int column)
 
int getSelectedRow () const
 
int getSelectedColumn () const
 
void setSelectedRow (const int selected)
 
void setSelectedColumn (const int selected)
 
bool isWrappingEnabled () const
 
void setWrappingEnabled (bool wrappingEnabled)
 
Rect getChildrenArea ()
 
void setLinewiseSelection (bool linewise)
 
void draw (Graphics *const graphics)
 
void safeDraw (Graphics *const graphics)
 
WidgetgetWidgetAt (int x, int y)
 
void moveToTop (Widget *const widget)
 
void moveToBottom (Widget *const widget)
 
void setFocusHandler (FocusHandler *const focusHandler)
 
void keyPressed (KeyEvent &event)
 
void setOpaque (Opaque opaque)
 
bool isOpaque () const
 
void mousePressed (MouseEvent &event)
 
void mouseWheelMovedUp (MouseEvent &event)
 
void mouseWheelMovedDown (MouseEvent &event)
 
void mouseDragged (MouseEvent &event)
 
void modelUpdated (const bool completed)
 
void requestFocus ()
 
void setSelectableGui (bool b)
 
- Public Member Functions inherited from Widget
 Widget (const Widget2 *const widget)
 
 ~Widget ()
 
virtual void drawFrame (Graphics *graphics)
 
virtual void safeDrawFrame (Graphics *graphics)
 
void setFrameSize (const unsigned int frameSize)
 
unsigned int getFrameSize () const
 
virtual void logic ()
 
WidgetgetParent () const
 
void setWidth (const int width)
 
int getWidth () const
 
void setHeight (const int height)
 
int getHeight () const
 
void setSize (const int width, const int height)
 
void setX (const int x)
 
int getX () const
 
void setY (const int y)
 
int getY () const
 
void setPosition (const int x, const int y)
 
void setDimension (const Rect &dimension)
 
const RectgetDimension () const
 
void setFocusable (const bool focusable)
 
bool isFocusable () const
 
virtual bool isFocused () const
 
void setEnabled (const bool enabled)
 
bool isEnabled () const
 
void setVisible (Visible visible)
 
bool isVisible () const
 
void setBaseColor (const Color &color)
 
const ColorgetBaseColor () const
 
void setForegroundColor (const Color &color)
 
const ColorgetForegroundColor () const
 
void setBackgroundColor (const Color &color)
 
const ColorgetBackgroundColor () const
 
virtual void requestMoveToTop ()
 
virtual void requestMoveToBottom ()
 
FocusHandlergetFocusHandler ()
 
void addActionListener (ActionListener *const actionListener)
 
void removeActionListener (ActionListener *const actionListener)
 
void addDeathListener (WidgetDeathListener *const deathListener)
 
void removeDeathListener (WidgetDeathListener *const deathListener)
 
void addMouseListener (MouseListener *const mouseListener)
 
void removeMouseListener (MouseListener *const mouseListener)
 
void addKeyListener (KeyListener *const keyListener)
 
void removeKeyListener (KeyListener *const keyListener)
 
void addFocusListener (FocusListener *const focusListener)
 
void removeFocusListener (FocusListener *const focusListener)
 
void addWidgetListener (WidgetListener *const widgetListener)
 
void removeWidgetListener (WidgetListener *const widgetListener)
 
void setActionEventId (const std::string &actionEventId)
 
const std::string & getActionEventId () const
 
virtual void getAbsolutePosition (int &x, int &y) const
 
virtual void setParent (Widget *parent)
 
FontgetFont () const
 
void setFont (Font *const font)
 
virtual void fontChanged ()
 
bool isTabInEnabled () const
 
void setTabInEnabled (const bool enabled)
 
bool isTabOutEnabled () const
 
void setTabOutEnabled (const bool enabled)
 
void requestModalFocus ()
 
virtual void requestModalMouseInputFocus ()
 
virtual void releaseModalFocus ()
 
virtual void releaseModalMouseInputFocus ()
 
virtual bool isModalFocused () const
 
virtual bool isModalMouseInputFocused () const
 
const std::list< MouseListener * > & getMouseListeners () const A_CONST
 
const std::list< KeyListener * > & getKeyListeners () const A_CONST
 
const std::list< FocusListener * > & getFocusListeners () const A_CONST
 
virtual FocusHandlergetInternalFocusHandler ()
 
void setInternalFocusHandler (FocusHandler *const internalFocusHandler)
 
virtual void focusNext ()
 
virtual void focusPrevious ()
 
virtual void showWidgetPart (Widget *const widget, const Rect &area)
 
void setId (const std::string &id)
 
const std::string & getId () const
 
virtual void showPart (const Rect &rectangle)
 
bool isAllowLogic () const
 
void setMouseConsume (const bool b)
 
bool isMouseConsume () const
 
void setRedraw (const bool b)
 
virtual bool isSelectable () const
 
void setSelectable (const bool selectable)
 
void windowResized ()
 
virtual void postInit ()
 
- Public Member Functions inherited from Widget2
virtual ~Widget2 ()
 
const ColorgetThemeColor (const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
 
const ColorgetThemeCharColor (const signed char c, bool &valid) const A_INLINE
 
virtual void setWidget2 (const Widget2 *const widget)
 
void setPalette (int palette)
 
void checkPalette ()
 
void setForegroundColor2 (const Color &color)
 
WidgetgetWindow () const
 
virtual void setWindow (Widget *const window)
 
- Public Member Functions inherited from MouseListener
virtual ~MouseListener ()
 
virtual void mouseEntered (MouseEvent &event)
 
virtual void mouseExited (MouseEvent &event)
 
virtual void mouseReleased (MouseEvent &event)
 
virtual void mouseClicked (MouseEvent &event)
 
virtual void mouseMoved (MouseEvent &event)
 
- Public Member Functions inherited from KeyListener
virtual ~KeyListener ()
 
virtual void keyReleased (KeyEvent &event)
 
- Public Member Functions inherited from TableModelListener
 TableModelListener ()
 
virtual ~TableModelListener ()
 

Protected Member Functions

void uninstallActionListeners ()
 
void installActionListeners ()
 
int getRowHeight () const
 
int getColumnWidth (const int i) const
 
- Protected Member Functions inherited from Widget
void distributeActionEvent ()
 
void distributeResizedEvent ()
 
void distributeMovedEvent ()
 
void distributeHiddenEvent ()
 
void distributeShownEvent ()
 
- Protected Member Functions inherited from Widget2
 Widget2 (const Widget2 *const widget)
 
- Protected Member Functions inherited from MouseListener
 MouseListener ()
 
- Protected Member Functions inherited from KeyListener
 KeyListener ()
 

Private Member Functions

int getRowForY (const int y) const
 
int getColumnForX (const int x) const
 
void recomputeDimensions ()
 

Private Attributes

TableModelmModel
 
WidgetmTopWidget
 
std::vector< GuiTableActionListener * > mActionListeners2
 
Color mHighlightColor
 
int mSelectedRow
 
int mSelectedColumn
 
bool mLinewiseMode
 
bool mWrappingEnabled
 
Opaque mOpaque
 
bool mSelectableGui
 

Static Private Attributes

static float mAlpha = 1.0
 

Friends

class GuiTableActionListener
 

Additional Inherited Members

- Static Public Member Functions inherited from Widget
static void setGlobalFont (Font *const font)
 
static FontgetGloablFont ()
 
static void cleanGlobalFont ()
 
static bool widgetExists (const Widget *const widget)
 
static void distributeWindowResizeEvent ()
 
static WidgetcallPostInit (Widget *const widget)
 
- Data Fields inherited from Widget
Visible mVisible
 
- Protected Types inherited from Widget
typedef std::list< MouseListener * > MouseListenerList
 
typedef MouseListenerList::iterator MouseListenerIterator
 
typedef std::list< KeyListener * > KeyListenerList
 
typedef KeyListenerList::iterator KeyListenerIterator
 
typedef std::list< ActionListener * > ActionListenerList
 
typedef ActionListenerList::iterator ActionListenerIterator
 
typedef std::list< WidgetDeathListener * > WidgetDeathListenerList
 
typedef WidgetDeathListenerList::iterator WidgetDeathListenerIterator
 
typedef std::list< FocusListener * > FocusListenerList
 
typedef FocusListenerList::iterator FocusListenerIterator
 
typedef std::list< WidgetListener * > WidgetListenerList
 
typedef WidgetListenerList::iterator WidgetListenerIterator
 
- Protected Attributes inherited from Widget
MouseListenerList mMouseListeners
 
KeyListenerList mKeyListeners
 
ActionListenerList mActionListeners
 
WidgetDeathListenerList mDeathListeners
 
FocusListenerList mFocusListeners
 
WidgetListenerList mWidgetListeners
 
Color mForegroundColor
 
Color mBackgroundColor
 
Color mBaseColor
 
Rect mDimension
 
std::string mActionEventId
 
std::string mId
 
FocusHandlermFocusHandler
 
FocusHandlermInternalFocusHandler
 
WidgetmParent
 
FontmCurrentFont
 
unsigned int mFrameSize
 
bool mFocusable
 
bool mTabIn
 
bool mTabOut
 
bool mEnabled
 
bool mAllowLogic
 
bool mMouseConsume
 
bool mRedraw
 
bool mSelectable
 
- Protected Attributes inherited from Widget2
int mPaletteOffset
 
WidgetmWindow
 
Color mForegroundColor2
 
- Static Protected Attributes inherited from Widget
static FontmGlobalFont = 0
 

Detailed Description

A table, with rows and columns made out of sub-widgets. Largely inspired by (and can be thought of as a generalisation of) the guichan listbox implementation.

Normally you want this within a ScrollArea.

Definition at line 49 of file guitable.h.

Constructor & Destructor Documentation

◆ GuiTable()

GuiTable::GuiTable ( const Widget2 *const  widget,
TableModel *const  initial_model,
const Opaque  opacity 
)

Definition at line 43 of file guitable.cpp.

45  :
46  Widget(widget),
47  MouseListener(),
48  KeyListener(),
50  mModel(nullptr),
51  mTopWidget(nullptr),
53  mHighlightColor(getThemeColor(ThemeColorId::HIGHLIGHT, 255U)),
54  mSelectedRow(-1),
55  mSelectedColumn(-1),
56  mLinewiseMode(false),
57  mWrappingEnabled(false),
58  mOpaque(opacity),
59  mSelectableGui(true)
60 {
61  mAllowLogic = false;
63 
64  setModel(initial_model);
65  setFocusable(true);
66 
67  addMouseListener(this);
68  addKeyListener(this);
69 }
bool mWrappingEnabled
Definition: guitable.h:203
int mSelectedRow
Definition: guitable.h:199
Widget * mTopWidget
Definition: guitable.h:189
Color mHighlightColor
Definition: guitable.h:197
void setModel(TableModel *const m)
Definition: guitable.cpp:85
std::vector< GuiTableActionListener * > mActionListeners2
Definition: guitable.h:192
int mSelectedColumn
Definition: guitable.h:200
bool mLinewiseMode
Definition: guitable.h:202
bool mSelectableGui
Definition: guitable.h:205
TableModel * mModel
Definition: guitable.h:186
Opaque mOpaque
Definition: guitable.h:204
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:45
void setFocusable(const bool focusable)
Definition: widget.cpp:192
Widget(const Widget2 *const widget)
Definition: widget.cpp:85
Color mBackgroundColor
Definition: widget.h:1091
bool mAllowLogic
Definition: widget.h:1160
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:292
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:272

References Widget::addKeyListener(), Widget::addMouseListener(), LinkHighlightMode::BACKGROUND, Widget2::getThemeColor(), Widget::mAllowLogic, Widget::mBackgroundColor, Widget::setFocusable(), and setModel().

◆ ~GuiTable()

GuiTable::~GuiTable ( )

Definition at line 71 of file guitable.cpp.

72 {
73  if (gui != nullptr)
74  gui->removeDragged(this);
75 
78 }
void uninstallActionListeners()
Definition: guitable.cpp:199
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1162
#define delete2(var)
Definition: delete2.h:25
Gui * gui
Definition: gui.cpp:111

References delete2, gui, mModel, Gui::removeDragged(), and uninstallActionListeners().

Member Function Documentation

◆ draw()

void GuiTable::draw ( Graphics *const  graphics)
virtual

Draws the widget. It is called by the parent widget when it is time for the widget to draw itself. The graphics object is set up so that all drawing is relative to the widget, i.e coordinate (0,0) is the top left corner of the widget. It is not possible to draw outside of a widget's dimension.

Parameters
graphicsaA graphics object to draw with.

Implements Widget.

Definition at line 227 of file guitable.cpp.

228 {
229  if (getRowHeight() == 0)
230  return;
231 
232  BLOCK_START("GuiTable::draw")
233  if (settings.guiAlpha != mAlpha)
234  mAlpha = settings.guiAlpha;
235 
236  const Rect &rect = mDimension;
237  const int width = rect.width;
238  const int height = rect.height;
239  const int y = rect.y;
240  if (mOpaque == Opaque_true)
241  {
242  mBackgroundColor.a = CAST_U32(mAlpha * 255.0F);
243  graphics->setColor(mBackgroundColor);
244  graphics->fillRectangle(Rect(0, 0, width, height));
245  }
246 
247  // First, determine how many rows we need to draw,
248  // and where we should start.
249  int rHeight = getRowHeight();
250  if (rHeight == 0)
251  rHeight = 1;
252  int first_row = -(y / rHeight);
253 
254  if (first_row < 0)
255  first_row = 0;
256 
257  unsigned int rows_nr = CAST_U32(1 +
258  height / rHeight); // May overestimate by one.
259  unsigned int max_rows_nr;
260  if (mModel->getRows() < first_row)
261  {
262  max_rows_nr = 0U;
263  }
264  else
265  {
266  max_rows_nr = CAST_U32(
267  mModel->getRows() - first_row); // clip if neccessary:
268  }
269  if (max_rows_nr < rows_nr)
270  rows_nr = max_rows_nr;
271 
272  // Now determine the first and last column
273  // Take the easy way out; these are usually bounded and all visible.
274  const unsigned first_column = 0;
275  const unsigned last_column1 = CAST_U32(
276  mModel->getColumns());
277 
278  int y_offset = first_row * rHeight;
279 
280  for (unsigned int r = CAST_U32(first_row);
281  r < CAST_U32(first_row) + rows_nr;
282  ++r)
283  {
284  int x_offset = 0;
285 
286  for (unsigned c = first_column; c + 1 <= last_column1; ++c)
287  {
288  Widget *const widget = mModel->getElementAt(CAST_S32(r),
289  CAST_S32(c));
290  const int cWidth = CAST_S32(getColumnWidth(
291  CAST_S32(c)));
292  if (widget != nullptr)
293  {
294  Rect bounds(x_offset, y_offset, cWidth, rHeight);
295 
296  if (widget == mTopWidget)
297  {
298  bounds.height = widget->getHeight();
299  bounds.width = widget->getWidth();
300  }
301 
302  widget->setDimension(bounds);
303 
304  if (mSelectedRow > -1)
305  {
307  mAlpha * 255.0F);
308  graphics->setColor(mHighlightColor);
309 
310  if (mLinewiseMode && r == CAST_U32(
311  mSelectedRow) && c == 0)
312  {
313  graphics->fillRectangle(Rect(0, y_offset,
314  width, rHeight));
315  }
316  else if (!mLinewiseMode && mSelectedColumn > 0
317  && c == CAST_U32(mSelectedColumn)
318  && r == CAST_U32(mSelectedRow))
319  {
320  graphics->fillRectangle(Rect(
321  x_offset, y_offset, cWidth, rHeight));
322  }
323  }
324  graphics->pushClipArea(bounds);
325  widget->draw(graphics);
326  graphics->popClipArea();
327  }
328 
329  x_offset += cWidth;
330  }
331 
332  y_offset += rHeight;
333  }
334 
335  if (mTopWidget != nullptr)
336  {
337  const Rect &bounds = mTopWidget->getDimension();
338  graphics->pushClipArea(bounds);
339  mTopWidget->draw(graphics);
340  graphics->popClipArea();
341  }
342  BLOCK_END("GuiTable::draw")
343 }
#define CAST_S32
Definition: cast.h:30
#define CAST_U32
Definition: cast.h:31
unsigned int a
Definition: color.h:251
virtual void popClipArea()
Definition: graphics.cpp:739
virtual void fillRectangle(const Rect &rectangle)=0
virtual void setColor(const Color &color)
Definition: graphics.h:320
virtual void pushClipArea(const Rect &area)
Definition: graphics.cpp:677
static float mAlpha
Definition: guitable.h:184
int getRowHeight() const
Definition: guitable.cpp:141
int getColumnWidth(const int i) const
Definition: guitable.cpp:146
Definition: rect.h:74
virtual int getRows() const =0
virtual Widget * getElementAt(const int row, const int column) const =0
virtual int getColumns() const =0
Definition: widget.h:99
const Rect & getDimension() const
Definition: widget.h:317
Rect mDimension
Definition: widget.h:1101
void setDimension(const Rect &dimension)
Definition: widget.cpp:169
virtual void draw(Graphics *const graphics)=0
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
if(!vert) return
const bool Opaque_true
Definition: opaque.h:30
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
Settings settings
Definition: settings.cpp:32

References Color::a, BLOCK_END, BLOCK_START, CAST_S32, CAST_U32, Widget::draw(), Graphics::fillRectangle(), TableModel::getColumns(), getColumnWidth(), Widget::getDimension(), TableModel::getElementAt(), Widget::getHeight(), getRowHeight(), TableModel::getRows(), Widget::getWidth(), Settings::guiAlpha, Rect::height, mAlpha, Widget::mBackgroundColor, Widget::mDimension, mHighlightColor, mLinewiseMode, mModel, mOpaque, mSelectedColumn, mSelectedRow, mTopWidget, Opaque_true, Graphics::popClipArea(), Graphics::pushClipArea(), Graphics::setColor(), Widget::setDimension(), settings, Rect::width, Rect::y, and y.

◆ getChildrenArea()

Rect GuiTable::getChildrenArea ( )
virtual

Gets the area of the widget occupied by the widget's children. By default this method returns an empty rectangle as not all widgets are containers. If you want to make a container this method should return the area where the children resides. This method is used when drawing children of a widget when computing clip rectangles for the children.

An example of a widget that overloads this method is ScrollArea. A ScrollArea has a view of its contant and that view is the children area. The size of a ScrollArea's children area might vary depending on if the scroll bars of the ScrollArea is shown or not.

Returns
The area of the widget occupied by the widget's children.
See also
BasicContainer
BasicContainer::getChildrenArea
BasicContainer::drawChildren

Reimplemented from Widget.

Definition at line 476 of file guitable.cpp.

477 {
478  return Rect(0, 0, mDimension.width, mDimension.height);
479 }
int width
Definition: rect.h:219
int height
Definition: rect.h:224

References Rect::height, Widget::mDimension, and Rect::width.

◆ getColumnForX()

int GuiTable::getColumnForX ( const int  x) const
private

Definition at line 631 of file guitable.cpp.

632 {
633  int column;
634  int delta = 0;
635 
636  const int colnum = mModel->getColumns();
637  for (column = 0; column < colnum; column ++)
638  {
639  delta += getColumnWidth(column);
640  if (x <= delta)
641  break;
642  }
643 
644  if (column >= colnum)
645  return -1;
646  return column;
647 }

References TableModel::getColumns(), getColumnWidth(), mModel, and x.

Referenced by getWidgetAt(), mouseDragged(), and mousePressed().

◆ getColumnWidth()

int GuiTable::getColumnWidth ( const int  i) const
protected

Definition at line 146 of file guitable.cpp.

147 {
148  return mModel->getColumnWidth(i) + 4; // border
149 }
virtual int getColumnWidth(const int index) const =0

References TableModel::getColumnWidth(), and mModel.

Referenced by draw(), getColumnForX(), recomputeDimensions(), and safeDraw().

◆ getModel()

const TableModel * GuiTable::getModel ( ) const

Retrieves the active table model

Definition at line 80 of file guitable.cpp.

81 {
82  return mModel;
83 }

References mModel.

◆ getRowForY()

int GuiTable::getRowForY ( const int  y) const
private

Definition at line 618 of file guitable.cpp.

619 {
620  int row = -1;
621 
622  const int rowHeight = getRowHeight();
623  if (rowHeight > 0)
624  row = y / rowHeight;
625 
626  if (row < 0 || row >= mModel->getRows())
627  return -1;
628  return row;
629 }

References getRowHeight(), TableModel::getRows(), mModel, and y.

Referenced by getWidgetAt(), mouseDragged(), and mousePressed().

◆ getRowHeight()

int GuiTable::getRowHeight ( ) const
protected

Definition at line 141 of file guitable.cpp.

142 {
143  return mModel->getRowHeight() + 4; // border
144 }
virtual int getRowHeight() const =0

References TableModel::getRowHeight(), and mModel.

Referenced by draw(), getRowForY(), recomputeDimensions(), and safeDraw().

◆ getSelectedColumn()

int GuiTable::getSelectedColumn ( ) const

Definition at line 136 of file guitable.cpp.

137 {
138  return mSelectedColumn;
139 }

References mSelectedColumn.

◆ getSelectedRow()

int GuiTable::getSelectedRow ( ) const

Definition at line 131 of file guitable.cpp.

132 {
133  return mSelectedRow;
134 }

References mSelectedRow.

Referenced by Setup_Relations::action(), mouseWheelMovedDown(), and mouseWheelMovedUp().

◆ getWidgetAt()

Widget * GuiTable::getWidgetAt ( int  x,
int  y 
)
virtual

Gets a widget from a certain position in the widget. This function is used to decide which gets mouse input, thus it can be overloaded to change that behaviour.

NOTE: This always returns NULL if the widget is not a container.

Parameters
xThe x coordinate of the widget to get.
yThe y coordinate of the widget to get.
Returns
The widget at the specified coodinate, NULL if no widget is found.

Reimplemented from Widget.

Definition at line 595 of file guitable.cpp.

596 {
597  if (mModel == nullptr)
598  return nullptr;
599 
600  const int row = getRowForY(y);
601  const int column = getColumnForX(x);
602 
603  if (mTopWidget != nullptr &&
605  {
606  return mTopWidget;
607  }
608 
609  if (row > -1 && column > -1)
610  {
611  Widget *const w = mModel->getElementAt(row, column);
612  if (w != nullptr && w->isFocusable())
613  return w;
614  }
615  return nullptr;
616 }
int getColumnForX(const int x) const
Definition: guitable.cpp:631
int getRowForY(const int y) const
Definition: guitable.cpp:618
bool isPointInRect(const int x_, const int y_) const
Definition: rect.h:197
bool isFocusable() const
Definition: widget.cpp:199

References getColumnForX(), Widget::getDimension(), TableModel::getElementAt(), getRowForY(), Widget::isFocusable(), Rect::isPointInRect(), mModel, mTopWidget, x, and y.

◆ installActionListeners()

void GuiTable::installActionListeners ( )
protected

Installs all action listeners on inner widgets.

Definition at line 205 of file guitable.cpp.

206 {
207  const int rows = mModel->getRows();
208  const int columns = mModel->getColumns();
209 
210  for (int row = 0; row < rows; ++row)
211  {
212  for (int column = 0; column < columns; ++column)
213  {
214  Widget *const widget = mModel->getElementAt(row, column);
215  if (widget != nullptr)
216  {
218  this, widget, row, column));
219  }
220  }
221  }
222 
224 }
void setFocusHandler(FocusHandler *const focusHandler)
Definition: guitable.cpp:649
friend class GuiTableActionListener
Definition: guitable.h:55
FocusHandler * getFocusHandler()
Definition: widget.h:474

References TableModel::getColumns(), TableModel::getElementAt(), Widget::getFocusHandler(), TableModel::getRows(), GuiTableActionListener, mActionListeners2, mModel, and setFocusHandler().

Referenced by modelUpdated(), and setModel().

◆ isOpaque()

bool GuiTable::isOpaque ( ) const
inline

Checks if the table is opaque, that is if the table area displays its background.

Returns
True if the table is opaque, false otherwise.

Definition at line 146 of file guitable.h.

147  { return mOpaque == Opaque_true; }

References mOpaque, and Opaque_true.

◆ isWrappingEnabled()

bool GuiTable::isWrappingEnabled ( ) const
inline

Definition at line 91 of file guitable.h.

92  { return mWrappingEnabled; }

References mWrappingEnabled.

◆ keyPressed()

void GuiTable::keyPressed ( KeyEvent event)
virtual

Called if a key is pressed when the widget has keyboard focus. If a key is held down the widget will generate multiple key presses.

Parameters
eventDiscribes the event.

Reimplemented from KeyListener.

Definition at line 482 of file guitable.cpp.

483 {
484  const InputActionT action = event.getActionId();
485 
486  if (action == InputAction::GUI_SELECT)
487  {
489  event.consume();
490  }
491  else if (action == InputAction::GUI_UP)
492  {
494  event.consume();
495  }
496  else if (action == InputAction::GUI_DOWN)
497  {
499  event.consume();
500  }
501  else if (action == InputAction::GUI_LEFT)
502  {
504  event.consume();
505  }
506  else if (action == InputAction::GUI_RIGHT)
507  {
509  event.consume();
510  }
511  else if (action == InputAction::GUI_HOME)
512  {
513  setSelectedRow(0);
515  event.consume();
516  }
517  else if (action == InputAction::GUI_END && (mModel != nullptr))
518  {
519  setSelectedRow(mModel->getRows() - 1);
521  event.consume();
522  }
523 }
void setSelectedRow(const int selected)
Definition: guitable.cpp:151
void setSelectedColumn(const int selected)
Definition: guitable.cpp:180
void distributeActionEvent()
Definition: widget.cpp:493
InputAction ::T InputActionT
Definition: inputaction.h:717

References Widget::distributeActionEvent(), TableModel::getColumns(), TableModel::getRows(), InputAction::GUI_DOWN, InputAction::GUI_END, InputAction::GUI_HOME, InputAction::GUI_LEFT, InputAction::GUI_RIGHT, InputAction::GUI_SELECT, InputAction::GUI_UP, mModel, mSelectedColumn, mSelectedRow, setSelectedColumn(), and setSelectedRow().

◆ modelUpdated()

void GuiTable::modelUpdated ( const bool  completed)
virtual

Must be invoked by the TableModel whenever a global change is about to occur or has occurred (e.g., when a row or column is being removed or added).

This method is triggered twice, once before and once after the update.

Parameters
completedwhether we are signalling the end of the update

Implements TableModelListener.

Definition at line 581 of file guitable.cpp.

582 {
583  if (completed)
584  {
587  }
588  else
589  { // before the update?
590  mTopWidget = nullptr; // No longer valid in general
592  }
593 }
void installActionListeners()
Definition: guitable.cpp:205
void recomputeDimensions()
Definition: guitable.cpp:100

References installActionListeners(), mTopWidget, recomputeDimensions(), and uninstallActionListeners().

◆ mouseDragged()

void GuiTable::mouseDragged ( MouseEvent event)
virtual

Called when the mouse has moved and the mouse has previously been pressed on the widget.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Definition at line 568 of file guitable.cpp.

569 {
570  if (event.getButton() != MouseButton::LEFT)
571  return;
572 
573  // Make table selection update on drag
574  const int x = std::max(0, event.getX());
575  const int y = std::max(0, event.getY());
576 
579 }
MouseButtonT getButton() const
Definition: mouseevent.h:116
int getX() const
Definition: mouseevent.h:127
int getY() const
Definition: mouseevent.h:138

References MouseEvent::getButton(), getColumnForX(), getRowForY(), MouseEvent::getX(), MouseEvent::getY(), MouseButton::LEFT, setSelectedColumn(), setSelectedRow(), x, and y.

◆ mousePressed()

void GuiTable::mousePressed ( MouseEvent event)
virtual

Called when a mouse button has been pressed on the widget area.

NOTE: A mouse press is NOT equal to a mouse click. Use mouseClickMessage to check for mouse clicks.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Definition at line 526 of file guitable.cpp.

527 {
528  if (!mSelectableGui)
529  return;
530 
531  if (event.getButton() == MouseButton::LEFT)
532  {
533  const int row = getRowForY(event.getY());
534  const int column = getColumnForX(event.getX());
535 
536  if (row > -1 && column > -1 &&
537  row < mModel->getRows() && column < mModel->getColumns())
538  {
539  mSelectedColumn = column;
540  mSelectedRow = row;
541  event.consume();
542  }
543 
545  }
546 }

References Widget::distributeActionEvent(), MouseEvent::getButton(), getColumnForX(), getRowForY(), MouseEvent::getX(), MouseEvent::getY(), MouseButton::LEFT, mSelectableGui, mSelectedColumn, and mSelectedRow.

◆ mouseWheelMovedDown()

void GuiTable::mouseWheelMovedDown ( MouseEvent event)
virtual

Called when the mouse wheel has moved down on the widget area.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Definition at line 559 of file guitable.cpp.

560 {
561  if (isFocused())
562  {
564  event.consume();
565  }
566 }
int getSelectedRow() const
Definition: guitable.cpp:131
virtual bool isFocused() const
Definition: widget.cpp:184

References getSelectedRow(), Widget::isFocused(), and setSelectedRow().

◆ mouseWheelMovedUp()

void GuiTable::mouseWheelMovedUp ( MouseEvent event)
virtual

Called when the mouse wheel has moved up on the widget area.

Parameters
eventDescribes the event.

Reimplemented from MouseListener.

Definition at line 548 of file guitable.cpp.

549 {
550  if (isFocused())
551  {
552  const int selRow = getSelectedRow();
553  if (selRow > 0 || (selRow == 0 && mWrappingEnabled))
554  setSelectedRow(selRow - 1);
555  event.consume();
556  }
557 }

References getSelectedRow(), Widget::isFocused(), mWrappingEnabled, and setSelectedRow().

◆ moveToBottom()

void GuiTable::moveToBottom ( Widget *const  widget)
virtual

Moves a widget in this widget to the bottom of this widget. The moved widget will be drawn below all other widgets in this widget.

Parameters
widgetThe widget to move to the bottom.
See also
moveToTop

Reimplemented from Widget.

Definition at line 469 of file guitable.cpp.

470 {
471  Widget::moveToBottom(widget);
472  if (widget == mTopWidget)
473  mTopWidget = nullptr;
474 }
virtual void moveToBottom(Widget *widget)
Definition: widget.h:877

References Widget::moveToBottom(), and mTopWidget.

◆ moveToTop()

void GuiTable::moveToTop ( Widget *const  widget)
virtual

Moves a widget to the top of this widget. The moved widget will be drawn above all other widgets in this widget.

Parameters
widgetThe widget to move to the top.
See also
moveToBottom

Reimplemented from Widget.

Definition at line 463 of file guitable.cpp.

464 {
465  Widget::moveToTop(widget);
466  mTopWidget = widget;
467 }
virtual void moveToTop(Widget *widget)
Definition: widget.h:867

References Widget::moveToTop(), and mTopWidget.

◆ recomputeDimensions()

void GuiTable::recomputeDimensions ( )
private

Definition at line 100 of file guitable.cpp.

101 {
102  if (mModel == nullptr)
103  return;
104 
105  const int rows_nr = mModel->getRows();
106  const int columns_nr = mModel->getColumns();
107  int width = 0;
108 
109  if (mSelectableGui)
110  {
111  if (mSelectedRow >= rows_nr)
112  mSelectedRow = rows_nr - 1;
113 
114  if (mSelectedColumn >= columns_nr)
115  mSelectedColumn = columns_nr - 1;
116  }
117 
118  for (int i = 0; i < columns_nr; i++)
119  width += getColumnWidth(i);
120 
121  setWidth(width);
122  setHeight(getRowHeight() * rows_nr);
123 }
void setWidth(const int width)
Definition: widget.cpp:133
void setHeight(const int height)
Definition: widget.cpp:140

References TableModel::getColumns(), getColumnWidth(), getRowHeight(), TableModel::getRows(), mModel, mSelectableGui, mSelectedColumn, mSelectedRow, Widget::setHeight(), and Widget::setWidth().

Referenced by modelUpdated(), and setModel().

◆ requestFocus()

void GuiTable::requestFocus ( )
virtual

Requests focus for the widget. A widget will only recieve focus if it is focusable.

Reimplemented from Widget.

Definition at line 671 of file guitable.cpp.

672 {
673  if (mFocusHandler == nullptr)
674  return;
676 }
FocusHandler * mFocusHandler
Definition: widget.h:1116
virtual void requestFocus()
Definition: widget.cpp:204

References Widget::mFocusHandler, and Widget::requestFocus().

◆ safeDraw()

void GuiTable::safeDraw ( Graphics *const  graphics)
virtual

Implements Widget.

Definition at line 345 of file guitable.cpp.

346 {
347  if (getRowHeight() == 0)
348  return;
349 
350  BLOCK_START("GuiTable::draw")
351  if (settings.guiAlpha != mAlpha)
352  mAlpha = settings.guiAlpha;
353 
354  const Rect &rect = mDimension;
355  const int width = rect.width;
356  const int height = rect.height;
357  const int y = rect.y;
358  if (mOpaque == Opaque_true)
359  {
360  mBackgroundColor.a = CAST_U32(mAlpha * 255.0F);
361  graphics->setColor(mBackgroundColor);
362  graphics->fillRectangle(Rect(0, 0, width, height));
363  }
364 
365  // First, determine how many rows we need to draw,
366  // and where we should start.
367  int rHeight = getRowHeight();
368  if (rHeight == 0)
369  rHeight = 1;
370  int first_row = -(y / rHeight);
371 
372  if (first_row < 0)
373  first_row = 0;
374 
375  unsigned int rows_nr = CAST_U32(
376  1 + height / rHeight); // May overestimate by one.
377  unsigned int max_rows_nr;
378  if (mModel->getRows() < first_row)
379  {
380  max_rows_nr = 0;
381  }
382  else
383  {
384  max_rows_nr = CAST_U32(
385  mModel->getRows() - first_row); // clip if neccessary:
386  }
387  if (max_rows_nr < rows_nr)
388  rows_nr = max_rows_nr;
389 
390  // Now determine the first and last column
391  // Take the easy way out; these are usually bounded and all visible.
392  const unsigned int first_column = 0;
393  const unsigned int last_column1 = CAST_U32(
394  mModel->getColumns());
395 
396  int y_offset = first_row * rHeight;
397 
398  for (unsigned int r = CAST_U32(first_row);
399  r < CAST_U32(first_row + CAST_S32(rows_nr));
400  ++r)
401  {
402  int x_offset = 0;
403 
404  for (unsigned c = first_column; c + 1 <= last_column1; ++c)
405  {
406  Widget *const widget = mModel->getElementAt(CAST_S32(r),
407  CAST_S32(c));
408  const int cWidth = CAST_S32(getColumnWidth(
409  CAST_S32(c)));
410  if (widget != nullptr)
411  {
412  Rect bounds(x_offset, y_offset, cWidth, rHeight);
413 
414  if (widget == mTopWidget)
415  {
416  bounds.height = widget->getHeight();
417  bounds.width = widget->getWidth();
418  }
419 
420  widget->setDimension(bounds);
421 
422  if (mSelectedRow > -1)
423  {
425  mAlpha * 255.0F);
426  graphics->setColor(mHighlightColor);
427 
428  if (mLinewiseMode && r == CAST_U32(
429  mSelectedRow) && c == 0)
430  {
431  graphics->fillRectangle(Rect(0, y_offset,
432  width, rHeight));
433  }
434  else if (!mLinewiseMode && mSelectedColumn > 0
435  && c == CAST_U32(mSelectedColumn)
436  && r == CAST_U32(mSelectedRow))
437  {
438  graphics->fillRectangle(Rect(
439  x_offset, y_offset, cWidth, rHeight));
440  }
441  }
442  graphics->pushClipArea(bounds);
443  widget->safeDraw(graphics);
444  graphics->popClipArea();
445  }
446 
447  x_offset += cWidth;
448  }
449 
450  y_offset += rHeight;
451  }
452 
453  if (mTopWidget != nullptr)
454  {
455  const Rect &bounds = mTopWidget->getDimension();
456  graphics->pushClipArea(bounds);
457  mTopWidget->safeDraw(graphics);
458  graphics->popClipArea();
459  }
460  BLOCK_END("GuiTable::draw")
461 }
virtual void safeDraw(Graphics *const graphics)=0

References Color::a, BLOCK_END, BLOCK_START, CAST_S32, CAST_U32, Graphics::fillRectangle(), TableModel::getColumns(), getColumnWidth(), Widget::getDimension(), TableModel::getElementAt(), Widget::getHeight(), getRowHeight(), TableModel::getRows(), Widget::getWidth(), Settings::guiAlpha, Rect::height, mAlpha, Widget::mBackgroundColor, Widget::mDimension, mHighlightColor, mLinewiseMode, mModel, mOpaque, mSelectedColumn, mSelectedRow, mTopWidget, Opaque_true, Graphics::popClipArea(), Graphics::pushClipArea(), Widget::safeDraw(), Graphics::setColor(), Widget::setDimension(), settings, Rect::width, Rect::y, and y.

◆ setFocusHandler()

void GuiTable::setFocusHandler ( FocusHandler *const  focusHandler)
virtual

Sets the focus handler to be used.

WARNING: This function is used internally and should not be called or overloaded unless you know what you are doing.

Parameters
focusHandlerThe focus handler to use.
See also
getFocusHandler

Reimplemented from Widget.

Definition at line 649 of file guitable.cpp.

650 {
651  // add check for focusHandler. may be need remove it?
652 
653  if (focusHandler == nullptr)
654  return;
655 
656  Widget::setFocusHandler(focusHandler);
657 
658  const int rows = mModel->getRows();
659  const int cols = mModel->getColumns();
660  for (int r = 0; r < rows; ++r)
661  {
662  for (int c = 0; c < cols ; ++c)
663  {
664  Widget *const w = mModel->getElementAt(r, c);
665  if (w != nullptr)
666  w->setFocusHandler(focusHandler);
667  }
668  }
669 }
virtual void setFocusHandler(FocusHandler *const focusHandler)
Definition: widget.cpp:238

References TableModel::getColumns(), TableModel::getElementAt(), TableModel::getRows(), mModel, and Widget::setFocusHandler().

Referenced by installActionListeners().

◆ setLinewiseSelection()

void GuiTable::setLinewiseSelection ( bool  linewise)
inline

Toggle whether to use linewise selection mode, in which the table selects an entire line at a time, rather than a single cell.

Note that column information is tracked even in linewise selection mode;

this mode therefore only affects visualisation.

Disabled by default.

Parameters
linewiseWhether to enable linewise selection mode

Definition at line 112 of file guitable.h.

113  { mLinewiseMode = linewise; }

References mLinewiseMode.

Referenced by Setup_Relations::Setup_Relations().

◆ setModel()

void GuiTable::setModel ( TableModel *const  m)

Sets the table model

Note that actions issued by widgets returned from the model will update the table selection, but only AFTER any event handlers installed within the widget have been triggered. To be notified after such an update, add an action listener to the table instead.

Definition at line 85 of file guitable.cpp.

86 {
87  if (mModel != nullptr)
88  {
90  mModel->removeListener(this);
91  }
92 
93  mModel = new_model;
95 
96  new_model->installListener(this);
98 }
virtual void removeListener(TableModelListener *const listener)
Definition: tablemodel.cpp:40

References installActionListeners(), TableModel::installListener(), mModel, recomputeDimensions(), TableModel::removeListener(), and uninstallActionListeners().

Referenced by GuiTable(), and Setup_Relations::updateAll().

◆ setOpaque()

void GuiTable::setOpaque ( Opaque  opaque)
inline

Sets the table to be opaque, that is sets the table to display its background.

Parameters
opaqueTrue if the table should be opaque, false otherwise.

Definition at line 137 of file guitable.h.

138  { mOpaque = opaque; }

References mOpaque.

Referenced by Setup_Relations::Setup_Relations().

◆ setSelectableGui()

void GuiTable::setSelectableGui ( bool  b)
inline

Definition at line 163 of file guitable.h.

164  { mSelectableGui = b; }

References mSelectableGui.

Referenced by Setup_Relations::Setup_Relations().

◆ setSelected()

void GuiTable::setSelected ( const int  row,
const int  column 
)

Definition at line 125 of file guitable.cpp.

126 {
127  mSelectedColumn = column;
128  mSelectedRow = row;
129 }

References mSelectedColumn, and mSelectedRow.

Referenced by GuiTableActionListener::action().

◆ setSelectedColumn()

void GuiTable::setSelectedColumn ( const int  selected)

Definition at line 180 of file guitable.cpp.

181 {
182  const int columns = mModel->getColumns();
183  if ((selected >= columns && mWrappingEnabled) ||
184  (selected < 0 && !mWrappingEnabled))
185  {
186  mSelectedColumn = 0;
187  }
188  else if ((selected >= columns && !mWrappingEnabled) ||
189  (selected < 0 && mWrappingEnabled))
190  {
191  mSelectedColumn = columns - 1;
192  }
193  else
194  {
195  mSelectedColumn = selected;
196  }
197 }

References TableModel::getColumns(), mModel, mSelectedColumn, and mWrappingEnabled.

Referenced by keyPressed(), and mouseDragged().

◆ setSelectedRow()

void GuiTable::setSelectedRow ( const int  selected)

Definition at line 151 of file guitable.cpp.

152 {
153  if (!mSelectableGui)
154  {
155  mSelectedRow = -1;
156  }
157  else
158  {
159  const int rows = mModel->getRows();
160  if (selected < 0 && !mWrappingEnabled)
161  {
162  mSelectedRow = -1;
163  }
164  else if (selected >= rows && mWrappingEnabled)
165  {
166  mSelectedRow = 0;
167  }
168  else if ((selected >= rows && !mWrappingEnabled) ||
169  (selected < 0 && mWrappingEnabled))
170  {
171  mSelectedRow = rows - 1;
172  }
173  else
174  {
175  mSelectedRow = selected;
176  }
177  }
178 }

References TableModel::getRows(), mModel, mSelectableGui, mSelectedRow, and mWrappingEnabled.

Referenced by keyPressed(), mouseDragged(), mouseWheelMovedDown(), and mouseWheelMovedUp().

◆ setWrappingEnabled()

void GuiTable::setWrappingEnabled ( bool  wrappingEnabled)
inline

Definition at line 94 of file guitable.h.

95  { mWrappingEnabled = wrappingEnabled; }

References mWrappingEnabled.

◆ uninstallActionListeners()

void GuiTable::uninstallActionListeners ( )
protected

Frees all action listeners on inner widgets.

Definition at line 199 of file guitable.cpp.

200 {
202  mActionListeners2.clear();
203 }
void delete_all(Container &c)
Definition: dtor.h:56

References delete_all(), and mActionListeners2.

Referenced by modelUpdated(), setModel(), and ~GuiTable().

Friends And Related Function Documentation

◆ GuiTableActionListener

friend class GuiTableActionListener
friend

Definition at line 55 of file guitable.h.

Referenced by installActionListeners().

Field Documentation

◆ mActionListeners2

std::vector<GuiTableActionListener *> GuiTable::mActionListeners2
private

Vector for compactness; used as a list in practice.

Definition at line 192 of file guitable.h.

Referenced by installActionListeners(), and uninstallActionListeners().

◆ mAlpha

float GuiTable::mAlpha = 1.0
staticprivate

Definition at line 184 of file guitable.h.

Referenced by draw(), and safeDraw().

◆ mHighlightColor

Color GuiTable::mHighlightColor
private

Holds the background color of the table.

Definition at line 197 of file guitable.h.

Referenced by draw(), and safeDraw().

◆ mLinewiseMode

bool GuiTable::mLinewiseMode
private

Definition at line 202 of file guitable.h.

Referenced by draw(), safeDraw(), and setLinewiseSelection().

◆ mModel

TableModel* GuiTable::mModel
private

◆ mOpaque

Opaque GuiTable::mOpaque
private

Definition at line 204 of file guitable.h.

Referenced by draw(), isOpaque(), safeDraw(), and setOpaque().

◆ mSelectableGui

bool GuiTable::mSelectableGui
private

Definition at line 205 of file guitable.h.

Referenced by mousePressed(), recomputeDimensions(), setSelectableGui(), and setSelectedRow().

◆ mSelectedColumn

int GuiTable::mSelectedColumn
private

◆ mSelectedRow

int GuiTable::mSelectedRow
private

◆ mTopWidget

Widget* GuiTable::mTopWidget
private

If someone moves a fresh widget to the top, we must display it.

Definition at line 189 of file guitable.h.

Referenced by draw(), getWidgetAt(), modelUpdated(), moveToBottom(), moveToTop(), and safeDraw().

◆ mWrappingEnabled

bool GuiTable::mWrappingEnabled
private

The documentation for this class was generated from the following files: