tbx  0.7.5
selection.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  * selection.h
27  *
28  * Created on: 12 Mar 2010
29  * Author: alanb
30  */
31 
32 #ifndef SELECTION_H_
33 #define SELECTION_H_
34 
35 #include <vector>
36 #include "../listener.h"
37 
38 namespace tbx {
39 namespace view {
40 
45 {
46 public:
55  SelectionChangedEvent(unsigned int first, unsigned int last, bool selected, bool final = true) :
56  _first(first), _last(last), _selected(selected), _final(final) {}
60  SelectionChangedEvent() : _first(0),_last(0), _selected(true), _final(true) {};
61 
62 private:
63  unsigned int _first;
64  unsigned int _last;
65  bool _selected;
66  bool _final;
67 
68 public:
72  unsigned int first() const {return _first;}
73 
77  void first(unsigned int value) {_first = value;}
78 
82  unsigned int last() const {return _last;}
83 
87  void last(unsigned int value) {_last = value;}
88 
96  bool selected() const {return _selected;}
97 
101  void selected(bool value) {_selected = value;}
102 
103 
117  bool final() const {return _final;}
118 
122  void final(bool value) {_final = value;}
123 
124 };
125 
130 {
131 public:
132  SelectionListener() {}
133  virtual ~SelectionListener() {}
134 
138  virtual void selection_changed(const SelectionChangedEvent &event) = 0;
139 };
140 
146 {
147 private:
148  std::vector<SelectionListener *> _listeners;
149 
150 protected:
155 
156  void fire_event(const SelectionChangedEvent &event);
157  void fire_event(unsigned int index, bool selected, bool final);
158  void fire_event(unsigned int from, unsigned int to, bool selected, bool final);
159 
160 public:
161  virtual ~Selection() {}
162 
166  void add_listener(SelectionListener *listener);
167  void remove_listener(SelectionListener *listener);
168 
172  enum Type {NONE, SINGLE, MULTIPLE};
173 
177  static const unsigned int NO_SELECTION = -1;
178 
182  virtual Type type() const = 0;
183 
189  virtual bool selected(unsigned int index) const = 0;
190 
197  virtual unsigned int first() const = 0;
201  virtual unsigned int last() const = 0;
202 
206  virtual bool empty() const = 0;
207 
214  virtual unsigned int count() const = 0;
215 
219  virtual bool any() const = 0;
220 
224  virtual bool one() const = 0;
225 
229  virtual bool many() const = 0;
230 
238  virtual void inserted(unsigned int index, unsigned int count) = 0;
239 
247  virtual void removed(unsigned int index, unsigned int count) = 0;
248 
252  virtual void clear() = 0;
253 
261  virtual void set(unsigned int index) = 0;
262 
268  virtual void select(unsigned int index) = 0;
269 
275  virtual void deselect(unsigned int index) = 0;
276 
282  virtual void toggle(unsigned int index) = 0;
283 
293  virtual void set(unsigned int from, unsigned int to) = 0;
294 
301  virtual void select(unsigned int from, unsigned int to) = 0;
302 
309  virtual void deselect(unsigned int from, unsigned int to) = 0;
310 
317  virtual void toggle(unsigned int from, unsigned int to) = 0;
318 
319 protected:
325  {
326  unsigned int _refcount;
327  public:
331  IteratorImpl() : _refcount(1) {}
335  virtual ~IteratorImpl() {}
339  void add_ref() {++_refcount;}
344  void release() {if (--_refcount == 0) delete this;}
351  bool shared() const {return _refcount > 1;}
352 
359  virtual IteratorImpl *clone() = 0;
365  virtual unsigned int index() const = 0;
370  virtual void next() = 0;
371  };
378  virtual IteratorImpl *get_iterator_impl() const = 0;
379 
380 public:
384  class Iterator
385  {
386  IteratorImpl *_impl;
387  friend class Selection;
388  Iterator() {_impl = 0;}
389  Iterator(IteratorImpl *impl) : _impl(impl) {};
390 
391  public:
392  Iterator(const Iterator &other);
393  ~Iterator();
394  Iterator &operator=(const Iterator &other);
395  bool operator==(const Iterator &other) const;
396  bool operator!=(const Iterator &other) const;
397  unsigned int operator*() const;
398 
399  Iterator &operator++();
400  Iterator operator++(int);
401  };
402 
403  Iterator begin() const;
404  Iterator end() const;
405 
406 };
407 
412 {
413 private:
414  unsigned int _selected;
415 
416 public:
417  SingleSelection() {_selected = NO_SELECTION;}
418 
422  virtual Type type() const {return SINGLE;}
423 
429  virtual bool selected(unsigned int index) const {return _selected == index;}
430 
436  virtual unsigned int first() const {return _selected;}
440  virtual unsigned int last() const {return _selected;}
441 
445  virtual bool empty() const {return (_selected == NO_SELECTION);}
446 
450  virtual unsigned int count() const {return (_selected == NO_SELECTION) ? 0 : 1;}
451 
455  virtual bool any() const {return (_selected != NO_SELECTION);}
456 
460  virtual bool one() const {return (_selected != NO_SELECTION);};
461 
465  virtual bool many() const {return false;}
466 
467  // Methods called by owner of this selection
468  virtual void inserted(unsigned int index, unsigned int count);
469  virtual void removed(unsigned int index, unsigned int count);
470 
471  // Methods called to manipulate the selection
472  virtual void clear();
473  virtual void set(unsigned int index);
474  virtual void select(unsigned int index);
475  virtual void deselect(unsigned int index);
476  virtual void toggle(unsigned int index);
477  virtual void set(unsigned int from, unsigned int to);
478  virtual void select(unsigned int from, unsigned int to);
479  virtual void deselect(unsigned int from, unsigned int to);
480  virtual void toggle(unsigned int from, unsigned int to);
481 
482 protected:
486  class SingleIteratorImpl : public IteratorImpl
487  {
488  unsigned int _index;
489  public:
495  SingleIteratorImpl(unsigned int index) : _index(index) {}
501  virtual IteratorImpl *clone() {add_ref(); return this;}
507  virtual unsigned int index() const {return _index;}
513  virtual void next() {_index = NO_SELECTION;}
514  };
515  virtual IteratorImpl *get_iterator_impl() const {return new SingleIteratorImpl(_selected);}
516 
517 };
518 
522 class MultiSelection : public Selection
523 {
524  typedef std::pair<unsigned int, unsigned int> Range;
525  unsigned int _first;
526  unsigned int _last;
527  std::vector<Range> _selected;
528  typedef std::vector<Range>::iterator RangeIterator;
529  typedef std::vector<Range>::const_iterator ConstRangeIterator;
530 public:
531  MultiSelection() : _first(NO_SELECTION), _last(NO_SELECTION) {}
532  virtual ~MultiSelection() {}
533 
537  virtual Type type() const {return MULTIPLE;}
538 
544  virtual bool selected(unsigned int index) const;
545 
551  virtual unsigned int first() const {return _first;}
555  virtual unsigned int last() const {return _last;}
556 
560  virtual bool empty() const {return (_first == NO_SELECTION);}
561 
565  virtual unsigned int count() const;
566 
570  virtual bool any() const {return (_first != NO_SELECTION);}
571 
575  virtual bool one() const {return (_first != NO_SELECTION && _first == _last);};
576 
580  virtual bool many() const {return (_first != NO_SELECTION) && (_first < _last);}
581 
582  // Methods called by owner of this selection
583  virtual void inserted(unsigned int index, unsigned int count);
584  virtual void removed(unsigned int index, unsigned int count);
585 
586  // Methods called to manipulate the selection
587  virtual void clear();
588  virtual void set(unsigned int index);
589  virtual void select(unsigned int index);
590  virtual void deselect(unsigned int index);
591  virtual void toggle(unsigned int index);
592  virtual void set(unsigned int from, unsigned int to);
593  virtual void select(unsigned int from, unsigned int to);
594  virtual void deselect(unsigned int from, unsigned int to);
595  virtual void toggle(unsigned int from, unsigned int to);
596 
597 protected:
602  {
603  ConstRangeIterator _current;
604  ConstRangeIterator _end;
605  unsigned int _index;
606 
607  public:
608  MultiIteratorImpl(ConstRangeIterator start, ConstRangeIterator end);
609  // Return a copy of the implementation
610  virtual IteratorImpl *clone();
611  // return the current index or NO_SELECTION if at end
612  virtual unsigned int index() const {return _index;}
613  // advance iterator
614  virtual void next();
615  };
616  virtual IteratorImpl *get_iterator_impl() const;
617 
618 private:
619  // Helper functions
620  RangeIterator find_last_ge(unsigned int index);
621  void fire_changes(std::vector<SelectionChangedEvent> &changes);
622 
623 };
624 
625 }
626 }
627 
628 #endif /* SELECTION_H_ */
virtual unsigned int first() const
Returns first selected item.
Definition: selection.h:436
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:34
SelectionChangedEvent(unsigned int first, unsigned int last, bool selected, bool final=true)
Construct a selection changed event.
Definition: selection.h:55
unsigned int first() const
Returns the first selected/deselected item in the range.
Definition: selection.h:72
virtual IteratorImpl * clone()
Get copy of this iterator implentation.
Definition: selection.h:501
IteratorImpl()
Construct iterator implementation.
Definition: selection.h:331
virtual bool selected(unsigned int index) const
Returns true if an item is selected.
Definition: selection.h:429
Class derived to implement iterator.
Definition: selection.h:601
Helper classes to display and edit data.
virtual bool many() const
Always returns false.
Definition: selection.h:465
SingleIteratorImpl(unsigned int index)
Construct iterator for item with given index.
Definition: selection.h:495
bool shared() const
Check if the iterator implementation is being shared between two or more iterators.
Definition: selection.h:351
Class derived from in subclasses to actually provided the iterator implementation.
Definition: selection.h:324
Selection()
Protected constructor, must always use a subclass.
Definition: selection.h:154
void release()
Decrease reference count and delete this if reference count becomes zero.
Definition: selection.h:344
void add_ref()
Increase reference count.
Definition: selection.h:339
virtual bool any() const
Returns true if one or more items are selected.
Definition: selection.h:455
void first(unsigned int value)
Set first selected.
Definition: selection.h:77
Class to implement multiple selections.
Definition: selection.h:522
bool selected() const
Returns the type of the event.
Definition: selection.h:96
virtual bool any() const
Returns true if one or more items are selected.
Definition: selection.h:570
virtual unsigned int last() const
Returns last selected item.
Definition: selection.h:440
virtual unsigned int first() const
Returns first selected item.
Definition: selection.h:551
SelectionChangedEvent()
Default constructor creates a selection of item 0.
Definition: selection.h:60
virtual bool many() const
Returns true if there are more than one item selected.
Definition: selection.h:580
Class with the details of a select, deselect or toggle operation.
Definition: selection.h:44
virtual unsigned int count() const
Return the number of selected items.
Definition: selection.h:450
virtual IteratorImpl * get_iterator_impl() const
Override to return the iterator implementation for the selection iterator to used.
Definition: selection.h:515
bool final() const
Returns true if this is the last event in a group of events triggered by a single action...
Definition: selection.h:117
virtual unsigned int index() const
Get the current index.
Definition: selection.h:507
unsigned int last() const
Returns the last selected/deselected item in the range.
Definition: selection.h:82
virtual bool empty() const
Returns true if selection is empty.
Definition: selection.h:560
void selected(bool value)
Set selected flag.
Definition: selection.h:101
Type
Type of selection.
Definition: selection.h:172
virtual void next()
Advance iterator.
Definition: selection.h:513
Class to iterate through all the selected indices.
Definition: selection.h:384
virtual unsigned int last() const
Returns last selected item.
Definition: selection.h:555
virtual bool empty() const
Returns true if selection is empty.
Definition: selection.h:445
virtual bool one() const
Returns true if only one item is selected.
Definition: selection.h:575
Base class for all toolbox event listeners.
Definition: listener.h:33
virtual unsigned int index() const
Override this to get the current index.
Definition: selection.h:612
Class to represent a single selection.
Definition: selection.h:411
Class derived to implement iterator.
Definition: selection.h:486
Listener for selection changed events.
Definition: selection.h:129
void last(unsigned int value)
Set last selected.
Definition: selection.h:87
virtual ~IteratorImpl()
Destructor does nothing.
Definition: selection.h:335
virtual Type type() const
Returns the type of this selection class.
Definition: selection.h:537
Base class for selections of one or more indices from a zero based range.
Definition: selection.h:145
virtual Type type() const
Returns the type of this selection class.
Definition: selection.h:422
virtual bool one() const
Returns true if only one item is selected.
Definition: selection.h:460