1178825Sdfr/* 2178825Sdfr * Copyright (c) 1999-2000, Eric Moon. 3178825Sdfr * All rights reserved. 4178825Sdfr * 5178825Sdfr * Redistribution and use in source and binary forms, with or without 6178825Sdfr * modification, are permitted provided that the following conditions 7178825Sdfr * are met: 8178825Sdfr * 9178825Sdfr * 1. Redistributions of source code must retain the above copyright 10178825Sdfr * notice, this list of conditions, and the following disclaimer. 11178825Sdfr * 12178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright 13178825Sdfr * notice, this list of conditions, and the following disclaimer in the 14178825Sdfr * documentation and/or other materials provided with the distribution. 15178825Sdfr * 16178825Sdfr * 3. The name of the author may not be used to endorse or promote products 17178825Sdfr * derived from this software without specific prior written permission. 18178825Sdfr * 19178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR 20178825Sdfr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21178825Sdfr * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22178825Sdfr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 23178825Sdfr * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24178825Sdfr * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25178825Sdfr * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26178825Sdfr * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 27178825Sdfr * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28178825Sdfr * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29178825Sdfr */ 30178825Sdfr 31178825Sdfr 32178825Sdfr// DiagramView.h (Cortex/DiagramView) 33178825Sdfr// 34178825Sdfr// * PURPOSE 35178825Sdfr// BView and DiagramItemGroup derived class providing the 36178825Sdfr// one and only drawing context all child DiagramItems will 37178825Sdfr// use. 38178825Sdfr// 39178825Sdfr// * HISTORY 40178825Sdfr// c.lenz 25sep99 Begun 41178825Sdfr// 42178825Sdfr 43178825Sdfr#ifndef __DiagramView_H__ 44178825Sdfr#define __DiagramView_H__ 45178825Sdfr 46178825Sdfr#include "DiagramItemGroup.h" 47178825Sdfr 48178825Sdfr#include <Region.h> 49178825Sdfr#include <View.h> 50178825Sdfr 51178825Sdfr//class BBitmap; 52178825Sdfr 53178825Sdfr#include "cortex_defs.h" 54178825Sdfr__BEGIN_CORTEX_NAMESPACE 55178825Sdfr 56178825Sdfr//class DiagramBox; 57178825Sdfrclass DiagramWire; 58178825Sdfrclass DiagramEndPoint; 59178825Sdfr 60178825Sdfrclass DiagramView : public BView, 61178825Sdfr public DiagramItemGroup 62178825Sdfr{ 63178825Sdfr 64178825Sdfrpublic: // *** ctor/dtor 65178825Sdfr 66178825Sdfr DiagramView( 67178825Sdfr BRect frame, 68178825Sdfr const char *name, 69178825Sdfr bool multiSelection, 70178825Sdfr uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP, 71178825Sdfr uint32 flags = B_WILL_DRAW); 72178825Sdfr 73178825Sdfr virtual ~DiagramView(); 74178825Sdfr 75178825Sdfrpublic: // *** hook functions 76178825Sdfr 77178825Sdfr // is called from MessageReceived() if a wire has been dropped 78178825Sdfr // on the background or on an incompatible endpoint 79178825Sdfr virtual void connectionAborted( 80178825Sdfr DiagramEndPoint *fromWhich) 81178825Sdfr { /* does nothing */ } 82178825Sdfr 83178825Sdfr // is called from MessageReceived() if an endpoint has accepted 84178825Sdfr // a wire-drop (i.e. connection) 85178825Sdfr virtual void connectionEstablished( 86178825Sdfr DiagramEndPoint *fromWhich, 87178825Sdfr DiagramEndPoint *toWhich) 88178825Sdfr { /* does nothing */ } 89178825Sdfr 90178825Sdfr // must be implemented to return an instance of the DiagramWire 91178825Sdfr // derived class 92178825Sdfr virtual DiagramWire *createWire( 93178825Sdfr DiagramEndPoint *fromWhich, 94178825Sdfr DiagramEndPoint *woWhich) = 0; 95178825Sdfr 96178825Sdfr // must be implemented to return an instance of the DiagramWire 97178825Sdfr // derived class; this version is for temporary wires used in 98178825Sdfr // drag & drop connecting 99178825Sdfr virtual DiagramWire *createWire( 100178825Sdfr DiagramEndPoint *fromWhich) = 0; 101178825Sdfr 102178825Sdfr // hook called from BackgroundMouseDown() if the background was hit 103178825Sdfr virtual void BackgroundMouseDown( 104178825Sdfr BPoint point, 105178825Sdfr uint32 buttons, 106178825Sdfr uint32 clicks) 107178825Sdfr { /* does nothing */ } 108178825Sdfr 109178825Sdfr // hook called from MouseMoved() if the mouse is floating over 110178825Sdfr // the background (i.e. with no message attached) 111178825Sdfr virtual void MouseOver( 112178825Sdfr BPoint point, 113178825Sdfr uint32 transit) 114178825Sdfr { /* does nothing */ } 115178825Sdfr 116178825Sdfr // hook called from MouseMoved() if a message is being dragged 117178825Sdfr // over the background 118178825Sdfr virtual void MessageDragged( 119178825Sdfr BPoint point, 120178825Sdfr uint32 transit, 121178825Sdfr const BMessage *message); 122178825Sdfr 123178825Sdfr // hook called from MessageReceived() if a message has been 124178825Sdfr // dropped over the background 125178825Sdfr virtual void MessageDropped( 126178825Sdfr BPoint point, 127178825Sdfr BMessage *message); 128178825Sdfr 129178825Sdfrpublic: // derived from BView 130178825Sdfr 131178825Sdfr // initial scrollbar update [e.moon 16nov99] 132178825Sdfr virtual void AttachedToWindow(); 133178825Sdfr 134178825Sdfr // draw the background and all items 135178825Sdfr virtual void Draw( 136178825Sdfr BRect updateRect); 137178825Sdfr 138178825Sdfr // updates the scrollbars 139178825Sdfr virtual void FrameResized( 140178825Sdfr float width, 141178825Sdfr float height); 142178825Sdfr 143178825Sdfr // return data rect [c.lenz 1mar2000] 144178825Sdfr virtual void GetPreferredSize( 145178825Sdfr float *width, 146178825Sdfr float *height); 147178825Sdfr 148 // handles the messages M_SELECTION_CHANGED and M_WIRE_DROPPED 149 // and passes a dropped message on to the item it was dropped on 150 virtual void MessageReceived( 151 BMessage *message); 152 153 // handles the arrow keys for moving DiagramBoxes 154 virtual void KeyDown( 155 const char *bytes, 156 int32 numBytes); 157 158 // if an item is located at the click point, this function calls 159 // that items MouseDown() method; else a deselectAll() command is 160 // made and rect-tracking is initiated 161 virtual void MouseDown( 162 BPoint point); 163 164 // if an item is located under the given point, this function 165 // calls that items MessageDragged() hook if a message is being 166 // dragged, and MouseOver() if not 167 virtual void MouseMoved( 168 BPoint point, 169 uint32 transit, 170 const BMessage *message); 171 172 // ends rect-tracking and wire-tracking 173 virtual void MouseUp( 174 BPoint point); 175 176public: // *** derived from DiagramItemGroup 177 178 // extends the DiagramItemGroup implementation by setting 179 // the items owner and calling the attachedToDiagram() hook 180 // on it 181 virtual bool AddItem( 182 DiagramItem *item); 183 184 // extends the DiagramItemGroup implementation by calling 185 // the detachedToDiagram() hook on the item 186 virtual bool RemoveItem( 187 DiagramItem *item); 188 189public: // *** operations 190 191 // update the temporary wire to follow the mouse cursor 192 void trackWire( 193 BPoint point); 194 195 bool isWireTracking() const 196 { return m_draggedWire; } 197 198protected: // *** internal operations 199 200 // do the actual background drawing 201 void drawBackground( 202 BRect updateRect); 203 204 // returns the current background color 205 rgb_color backgroundColor() const 206 { return m_backgroundColor; } 207 208 // set the background color; does not refresh the display 209 void setBackgroundColor( 210 rgb_color color); 211 212 // set the background bitmap; does not refresh the display 213 void setBackgroundBitmap( 214 BBitmap *bitmap); 215 216 // updates the region containing the rects of all boxes in 217 // the view (and thereby the "data-rect") and then adapts 218 // the scrollbars if necessary 219 void updateDataRect(); 220 221private: // *** internal operations 222 223 // setup a temporary wire for "live" dragging and attaches 224 // a message to the mouse 225 void _beginWireTracking( 226 DiagramEndPoint *fromEndPoint); 227 228 // delete the temporary dragged wire and invalidate display 229 void _endWireTracking(); 230 231 // setups rect-tracking to additionally drag a message for 232 // easier identification in MouseMoved() 233 void _beginRectTracking( 234 BPoint origin); 235 236 // takes care of actually selecting/deselecting boxes when 237 // they intersect with the tracked rect 238 void _trackRect( 239 BPoint origin, 240 BPoint current); 241 242 // updates the scrollbars (if there are any) to represent 243 // the current data-rect 244 void _updateScrollBars(); 245 246private: // *** data 247 248 // the button pressed at the last mouse event 249 int32 m_lastButton; 250 251 // the number of clicks with the last button 252 int32 m_clickCount; 253 254 // the point last clicked in this view 255 BPoint m_lastClickPoint; 256 257 // the button currently pressed (reset to 0 on mouse-up) 258 // [e.moon 16nov99] 259 int32 m_pressedButton; 260 261 // last mouse position in screen coordinates [e.moon 16nov99] 262 // only valid while m_pressedButton != 0 263 BPoint m_lastDragPoint; 264 265 // a pointer to the temporary wire used for wire 266 // tracking 267 DiagramWire *m_draggedWire; 268 269 // contains the rects of all DiagramBoxes in this view 270 BRegion m_boxRegion; 271 272 // contains the rect of the view actually containing something 273 // (i.e. DiagramBoxes) and all free space left/above of that 274 BRect m_dataRect; 275 276 // true if a bitmap is used for the background; false 277 // if a color is used 278 bool m_useBackgroundBitmap; 279 280 // the background color of the view 281 rgb_color m_backgroundColor; 282 283 // the background bitmap of the view 284 BBitmap *m_backgroundBitmap; 285}; 286 287__END_CORTEX_NAMESPACE 288#endif // __DiagramView_H__ 289 290