xref: /haiku/src/apps/cortex/DiagramView/DiagramItemGroup.h (revision 19ae20e67e91fc09cc9fc5c0e60e21e24e7a53eb)
1 /*
2  * Copyright (c) 1999-2000, Eric Moon.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions, and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 // DiagramItemGroup.h (Cortex/DiagramView)
33 //
34 // * HISTORY
35 //   c.lenz		28sep99		Begun
36 //
37 #ifndef DIAGRAM_ITEM_GROUP_H
38 #define DIAGRAM_ITEM_GROUP_H
39 
40 
41 #include "cortex_defs.h"
42 
43 #include "DiagramItem.h"
44 
45 #include <List.h>
46 #include <Point.h>
47 
48 
49 
50 __BEGIN_CORTEX_NAMESPACE
51 
52 
53 class DiagramItemGroup {
54 	public:
55 		DiagramItemGroup(uint32 acceptedTypes, bool multiSelection = true);
56 		virtual ~DiagramItemGroup();
57 
58 		// hook function
59 		// is called whenever the current selection has changed
SelectionChanged()60 		virtual void SelectionChanged()
61 		{
62 		}
63 
64 		// item accessors
65 
66 		uint32 CountItems(uint32 whichType = DiagramItem::M_ANY) const;
67 		DiagramItem* ItemAt(uint32 index,
68 			uint32 whichType = DiagramItem::M_ANY) const;
69 		DiagramItem* ItemUnder(BPoint point);
70 
71 		// item operations
72 
73 		virtual bool AddItem(DiagramItem* item);
74 		bool RemoveItem(DiagramItem* item);
75 		void SortItems(uint32 itemType,
76 			int (*compareFunc)(const void *, const void *));
77 		void DrawItems(BRect updateRect, uint32 whichType = DiagramItem::M_ANY,
78 			BRegion *updateRegion = 0);
79 		bool GetClippingAbove(DiagramItem* which, BRegion* outRegion);
80 
81 		// selection accessors
82 
83 		uint32 SelectedType() const;
84 		uint32 CountSelectedItems() const;
85 		DiagramItem* SelectedItemAt(uint32 index) const;
86 
87 		// returns the ability of the group to handle multiple selections
88 		// as set in the constructor
MultipleSelection()89 		bool MultipleSelection() const
90 		{
91 			return fMultiSelection;
92 		}
93 
94 		// selection related operations
95 
96 		bool SelectItem(DiagramItem* which, bool deselectOthers = true);
97 		bool DeselectItem(DiagramItem *which);
98 		void SortSelectedItems(int (*compareFunc)(const void *, const void *));
99 		bool SelectAll(uint32 itemType = DiagramItem::M_ANY);
100 		bool DeselectAll(uint32 itemType = DiagramItem::M_ANY);
101 		void DragSelectionBy(float x, float y, BRegion *updateRegion);
102 		void RemoveSelection();
103 
104 		// alignment related
105 
106 		// set/get the 'item alignment' grid size; this is used when
107 		// items are being dragged and inserted
SetItemAlignment(float horizontal,float vertical)108 		void SetItemAlignment(float horizontal, float vertical)
109 		{
110 			fItemAlignment.Set(horizontal, vertical);
111 		}
112 
113 		void GetItemAlignment(float *horizontal, float *vertical);
114 		void Align(float *x, float *y) const;
115 		BPoint Align(BPoint point) const;
116 
117 	protected: // accessors
118 
119 		/*!	Returns a pointer to the last item returned by the ItemUnder()
120 			function; this can be used by deriving classes to implement a
121 			transit system
122 		*/
_LastItemUnder()123 		DiagramItem* _LastItemUnder()
124 		{
125 			return fLastItemUnder;
126 		}
127 
_ResetItemUnder()128 		void _ResetItemUnder()
129 		{
130 			fLastItemUnder = 0;
131 		}
132 
133 	private: // data members
134 
135 		// pointers to the item-lists (one list for each item type)
136 		BList* 		fBoxes;
137 		BList* 		fWires;
138 		BList* 		fEndPoints;
139 
140 		BList*		fSelection;
141 			// pointer to the list containing the current selection
142 
143 		uint32		fTypes;
144 			// the DiagramItem type(s) of items this group will accept
145 
146 		BPoint		fItemAlignment;
147 			// specifies the "grid"-size for the frames of items
148 
149 		bool		fMultiSelection;
150 			// can multiple items be selected at once ?
151 
152 		DiagramItem* fLastItemUnder;
153 			// cached pointer to the item that was found in ItemUnder()
154 };
155 
156 __END_CORTEX_NAMESPACE
157 #endif	// DIAGRAM_ITEM_GROUP_H
158