tbx  0.7.5
itemview.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2010 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 /*
26  * itemview.h
27  *
28  * Created on: 24 Mar 2010
29  * Author: alanb
30  */
31 
32 #ifndef TBX_ITEMVIEW_H_
33 #define TBX_ITEMVIEW_H_
34 
35 #include "../window.h"
36 #include "../mouseclicklistener.h"
37 #include "../margin.h"
38 #include "../draghandler.h"
39 #include "../safelist.h"
40 #include "../hasbeenhiddenlistener.h"
41 #include "../scrollrequestlistener.h"
42 #include "itemrenderer.h"
43 #include "selection.h"
44 
45 namespace tbx {
46 
47 namespace view {
48 
52 class ItemViewClickListener;
53 
63 class ItemView :
64  public RedrawListener,
65  public ScrollRequestListener,
66  public MouseClickListener,
67  public SelectionListener
68 {
69 protected:
75  unsigned int _count;
76 
80  enum ViewFlags {NONE = 0,
81  AUTO_SIZE = 1,
86  WANT_AUTO_SIZE = 0x200,
87  LAST_SELECT_MENU = 0x400,
88  NO_UPDATES = 0x800
89  };
90  unsigned int _flags;
91 
92 public:
94  virtual ~ItemView();
95 
96 
101  static const unsigned int NO_INDEX = -1;
102 
108 
109 
113  const tbx::Margin &margin() const {return _margin;}
114  void margin(const tbx::Margin &margin);
115 
123  virtual void auto_size(bool on) = 0;
124 
128  bool auto_size() const {return ((_flags & AUTO_SIZE)!= 0);}
129 
137  void updates_enabled(bool updates) {if (updates) _flags &= ~NO_UPDATES; else _flags |= NO_UPDATES;}
138 
144  bool updates_enabled() const {return ((_flags & NO_UPDATES) == 0);}
145 
149  unsigned int count() const {return _count;}
150 
152 
158  Selection *selection() const {return _selection;}
165 
168 
169  void allow_drag_selection(bool on, bool on_item = false);
170 
171  void menu_selects(bool on);
172  bool menu_selects() const;
173  void clear_menu_selection();
174  bool last_selection_menu() const;
175 
180  virtual void redraw(const tbx::RedrawEvent &event) = 0;
181 
182  // Scroll request override
183  virtual void scroll_request(const tbx::ScrollRequestEvent &event);
184 
185  // Mouse click listener override
186  virtual void mouse_click(tbx::MouseClickEvent &event);
187 
188  // Selection listener override
189  virtual void selection_changed(const SelectionChangedEvent &event);
190 
195  virtual void update_window_extent() = 0;
196 
200  virtual void refresh() = 0;
201 
208  virtual void inserted(unsigned int where, unsigned int how_many) = 0;
209 
216  virtual void removing(unsigned int where, unsigned int how_many) {}
217 
224  virtual void removed(unsigned int where, unsigned int how_many) = 0;
225 
235  virtual void changing(unsigned int where, unsigned int how_many) {};
236 
243  virtual void changed(unsigned int where, unsigned int how_many) = 0;
244 
248  virtual void cleared() = 0;
249 
254  virtual unsigned int insert_index(const Point &scr_pt) const = 0;
255 
264  virtual unsigned int screen_index(const Point &scr_pt) const = 0;
265 
272  virtual unsigned int hit_test(const Point &scr_pt) const = 0;
273 
277  virtual void get_bounds(BBox &bounds, unsigned int index) const = 0;
278 
282  virtual void get_bounds(BBox &bounds, unsigned int first, unsigned int last) const = 0;
283 
284 protected:
293  virtual void process_drag_selection(const BBox &drag_box, bool adjust) {};
294 
295  // Helper methods
296  void process_mouse_click(int index, MouseClickEvent &event);
297 
298 private:
300 
303  class Selector : public tbx::DragHandler
304  {
305  ItemView *_me;
306  bool _adjust;
307  public:
308  Selector(ItemView *me, bool adjust) : _me(me), _adjust(adjust) {};
309 
310  virtual void drag_finished(const tbx::BBox &final);
311  virtual void drag_cancelled();
312  };
314 
315 };
316 
321 {
322  ItemView &_source;
323  unsigned int _index;
324  const MouseClickEvent &_click_event;
325 
326 public:
330  ItemViewClickEvent(ItemView &source, unsigned int index, const MouseClickEvent &click_event) :
331  _source(source), _index(index), _click_event(click_event) {}
332 
336  ItemView &source() const {return _source;}
337 
341  static const unsigned int NO_INDEX = -1;
342 
346  bool item_hit() const {return (_index != NO_INDEX);}
347 
351  unsigned int index() const {return _index;}
352 
356  const MouseClickEvent &click_event() const {return _click_event;}
357 };
358 
363 {
364 public:
366  virtual ~ItemViewClickListener() {}
367 
373  virtual void itemview_clicked(const ItemViewClickEvent &event) = 0;
374 };
375 
384 {
385  ItemView *_view;
386 public:
393 
399  virtual void has_been_hidden(const EventInfo &hidden_event)
400  {
401  _view->clear_menu_selection();
402  }
403 };
404 
405 
406 }
407 
408 }
409 
410 #endif /* ITEMVIEW_H_ */
virtual void update_window_extent()=0
Called to update the window extent needed to contain this view.
Select by dragging box starting on an item allowed.
Definition: itemview.h:83
const MouseClickEvent & click_event() const
Returns original mouse click event on the window containing this item view.
Definition: itemview.h:356
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:34
Need to auto size on removed called.
Definition: itemview.h:86
virtual void refresh()=0
Called to refresh the entire views contents.
static const unsigned int NO_INDEX
Constant for invalid index.
Definition: itemview.h:101
unsigned int index() const
Returns index click occurred on or NO_INDEX if click did not hit an item.
Definition: itemview.h:351
ViewFlags
Internal view flags enum, can be one or more of the following.
Definition: itemview.h:80
Scroll request listener.
Definition: scrollrequestlistener.h:103
Last selection was caused by menu button.
Definition: itemview.h:87
Helper classes to display and edit data.
Class to represent a two dimensional bounding box.
Definition: bbox.h:37
tbx::Window & window()
Return the Window that this item view is attached to.
Definition: itemview.h:107
virtual void cleared()=0
Inform the view that all the data has been cleared.
Selection * selection() const
Get the selection model used for selecting items in the list.
Definition: itemview.h:158
virtual ~ItemView()
Destructor removed any listeners.
Definition: itemview.cc:57
virtual void has_been_hidden(const EventInfo &hidden_event)
Clears the menu selection when the object is hidden.
Definition: itemview.h:399
Base class for views that show an indexed list of items.
Definition: itemview.h:63
void updates_enabled(bool updates)
Enable/disable updating of attached window.
Definition: itemview.h:137
virtual void get_bounds(BBox &bounds, unsigned int index) const =0
Get bounding box of the given index in work area coordinates.
void process_mouse_click(int index, MouseClickEvent &event)
Helper function to process mouse clicks.
Definition: itemview.cc:361
bool item_hit() const
Return true if an item was hit.
Definition: itemview.h:346
virtual unsigned int insert_index(const Point &scr_pt) const =0
Return the index for inserting an item from a screen point.
Selection * _selection
Selection model.
Definition: itemview.h:72
virtual void selection_changed(const SelectionChangedEvent &event)
Selection has changed event handling.
Definition: itemview.cc:378
Simple one way linked list of pointers that provides a single iterator that can be used safely if ite...
Definition: safelist.h:44
Class to represent a margin around an area.
Definition: margin.h:34
ItemViewClickEvent(ItemView &source, unsigned int index, const MouseClickEvent &click_event)
Constructor.
Definition: itemview.h:330
Class to report status of a drag started on a window.
Definition: draghandler.h:35
virtual void mouse_click(tbx::MouseClickEvent &event)
Mouse clicked on window containing list view.
Definition: itemview.cc:290
virtual void removed(unsigned int where, unsigned int how_many)=0
Inform the view that items have been removed.
Class with the details of a select, deselect or toggle operation.
Definition: selection.h:44
virtual void changing(unsigned int where, unsigned int how_many)
Inform the view that items are about to be changed.
Definition: itemview.h:235
bool last_selection_menu() const
Returns true if the last selection was made by the menu button.
Definition: itemview.cc:184
Class to represent a position in two dimensional space.
Definition: point.h:36
void clear_menu_selection()
Clear the current selection if it was made by the menu button.
Definition: itemview.cc:173
Event passed to redraw listener to give details on the area that needs a redraw.
Definition: redrawlistener.h:43
ItemViewClearMenuSelection(ItemView *iv)
Constructor for the given item view.
Definition: itemview.h:392
No flags size.
Definition: itemview.h:80
virtual void removing(unsigned int where, unsigned int how_many)
Inform the view that items are about to be removed.
Definition: itemview.h:216
virtual void redraw(const tbx::RedrawEvent &event)=0
Subclasses must override the redraw event to draw the items in the view.
Listener for mouse clicks on a Window.
Definition: mouseclicklistener.h:129
const tbx::Margin & margin() const
Return the margin around the data in the window.
Definition: itemview.h:113
void allow_drag_selection(bool on, bool on_item=false)
Allow selection of multiple items by using a drag box.
Definition: itemview.cc:133
Event for click on a window.
Definition: mouseclicklistener.h:41
bool auto_size() const
Returns true if auto size is on.
Definition: itemview.h:128
Autosizing on.
Definition: itemview.h:81
void remove_click_listener(ItemViewClickListener *listener)
Remove a listener for a click on the item view.
Definition: itemview.cc:113
Margin _margin
Margin around areas displaying items.
Definition: itemview.h:71
The Window object represents a toolbox window.
Definition: window.h:69
Class to provide information on a toolbox event.
Definition: eventinfo.h:48
virtual unsigned int screen_index(const Point &scr_pt) const =0
Return the index under the screen point, does not check if it hits content of the item renderer...
Redraw event listener.
Definition: redrawlistener.h:75
virtual unsigned int hit_test(const Point &scr_pt) const =0
Return the index under the screen point, checks if it hits content of the item renderer.
Base class for all toolbox event listeners.
Definition: listener.h:33
Selection * selection()
Get the selection model used for selecting items in the list.
Definition: itemview.h:164
unsigned int _count
Number of items displayed in this view.
Definition: itemview.h:75
bool updates_enabled() const
Check if updates of the attached window are enabled.
Definition: itemview.h:144
virtual void scroll_request(const tbx::ScrollRequestEvent &event)
Process scroll requests.
Definition: itemview.cc:213
unsigned int _flags
Internal view flags.
Definition: itemview.h:90
Listener for selection changed events.
Definition: selection.h:129
virtual void changed(unsigned int where, unsigned int how_many)=0
Inform the view that items have been changed.
Event for click on ItemView.
Definition: itemview.h:320
Select by dragging box allowed.
Definition: itemview.h:82
SafeList< ItemViewClickListener > * _click_listeners
Listeners for a mouse click on the view.
Definition: itemview.h:74
Listener for an object has been hidden event.
Definition: hasbeenhiddenlistener.h:36
unsigned int count() const
Returns number of items being displayed.
Definition: itemview.h:149
ItemView & source() const
Return item view this event occurred on.
Definition: itemview.h:336
ItemView(Window window)
Construct the item view with the given renderer.
Definition: itemview.cc:43
void add_click_listener(ItemViewClickListener *listener)
Add a listener for a click on the item view.
Definition: itemview.cc:100
Menu button selects item if no others selected.
Definition: itemview.h:84
Do not update window.
Definition: itemview.h:88
Removing or changing called.
Definition: itemview.h:85
virtual void process_drag_selection(const BBox &drag_box, bool adjust)
Override this method to process selection by dragging.
Definition: itemview.h:293
Listener for clicks on the item view.
Definition: itemview.h:362
Base class for selections of one or more indices from a zero based range.
Definition: selection.h:145
Helper class to clear selection when an object has been hidden if the selection was made by the menu ...
Definition: itemview.h:383
bool menu_selects() const
Returns true if the menu button selects an item.
Definition: itemview.cc:165
Information for ScrollRequest listener.
Definition: scrollrequestlistener.h:45
Window _window
Window displaying this view.
Definition: itemview.h:70
virtual void inserted(unsigned int where, unsigned int how_many)=0
Inform the view that items have been inserted.