xref: /haiku/src/apps/cortex/DiagramView/DiagramItem.cpp (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
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 // DiagramItem.cpp
33 
34 #include "DiagramItem.h"
35 #include "DiagramView.h"
36 
37 __USE_CORTEX_NAMESPACE
38 
39 #include <Debug.h>
40 #define D_METHOD(x) //PRINT(x)
41 
42 // -------------------------------------------------------- //
43 // *** static member initialization
44 // -------------------------------------------------------- //
45 
46 bigtime_t DiagramItem::m_lastSelectionTime	= 0;
47 int32 DiagramItem::m_countSelected			= 0;
48 
49 // -------------------------------------------------------- //
50 // *** ctor/dtor (public)
51 // -------------------------------------------------------- //
52 
53 DiagramItem::DiagramItem(
54 	uint32 itemType)
55 	: m_type(itemType),
56 	  m_view(0),
57 	  m_group(0),
58 	  m_draggable(false),
59 	  m_selectable(true),
60 	  m_selected(false),
61 	  m_selectionTime(system_time())
62 {
63 	D_METHOD(("DiagramItem::DiagramItem()\n"));
64 }
65 
66 DiagramItem::~DiagramItem()
67 {
68 	D_METHOD(("DiagramItem::~DiagramItem()\n"));
69 }
70 
71 // -------------------------------------------------------- //
72 // *** operations (public)
73 // -------------------------------------------------------- //
74 
75 void DiagramItem::select()
76 {
77 	D_METHOD(("DiagramItem::select()\n"));
78 	if (!m_selected)
79 	{
80 		m_selected = true;
81 		m_lastSelectionTime = m_selectionTime = system_time();
82 		m_countSelected = 1;
83 		selected();
84 		view()->Invalidate(Frame());
85 	}
86 }
87 
88 void DiagramItem::selectAdding()
89 {
90 	D_METHOD(("DiagramItem::selectAdding()\n"));
91 	if (!m_selected)
92 	{
93 		m_selected = true;
94 		m_selectionTime = m_lastSelectionTime - m_countSelected++;
95 		selected();
96 		view()->Invalidate(Frame());
97 	}
98 }
99 
100 void DiagramItem::deselect()
101 {
102 	D_METHOD(("DiagramItem::deselect()\n"));
103 	if (m_selected)
104 	{
105 		m_selected = false;
106 		deselected();
107 		view()->Invalidate(Frame());
108 	}
109 }
110 
111 // -------------------------------------------------------- //
112 // *** hook functions (public)
113 // -------------------------------------------------------- //
114 
115 float DiagramItem::howCloseTo(
116 	BPoint point) const
117 {
118 	D_METHOD(("DiagramItem::howCloseTo()\n"));
119 	if (Frame().Contains(point))
120 	{
121 		return 1.0;
122 	}
123 	return 0.0;
124 }
125 
126 // -------------------------------------------------------- //
127 // *** compare functions (friend)
128 // -------------------------------------------------------- //
129 
130 int __CORTEX_NAMESPACE__ compareSelectionTime(
131 	const void *lValue,
132 	const void *rValue)
133 {
134 	D_METHOD(("compareSelectionTime()\n"));
135 	int returnValue = 0;
136 	const DiagramItem *lItem = *(reinterpret_cast<const DiagramItem* const*>(reinterpret_cast<const void* const*>(lValue)));
137 	const DiagramItem *rItem = *(reinterpret_cast<const DiagramItem* const*>(reinterpret_cast<const void* const*>(rValue)));
138 	if (lItem->m_selectionTime < rItem->m_selectionTime)
139 	{
140 		returnValue = 1;
141 	}
142 	else if (lItem->m_selectionTime > rItem->m_selectionTime)
143 	{
144 		returnValue = -1;
145 	}
146 	return returnValue;
147 }
148 
149 // END -- DiagramItem.cpp --
150