1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkBalloonRepresentation.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 // .NAME vtkBalloonRepresentation - represent the vtkBalloonWidget
16 // .SECTION Description
17 // The vtkBalloonRepresentation is used to represent the vtkBalloonWidget.
18 // This representation is defined by two items: a text string and an image.
19 // At least one of these two items must be defined, but it is allowable to
20 // specify both, or just an image or just text. If both the text and image
21 // are specified, then methods are available for positioning the text and
22 // image with respect to each other.
23 //
24 // The balloon representation consists of three parts: text, a rectangular
25 // frame behind the text, and an image placed next to the frame and sized
26 // to match the frame.
27 //
28 // The size of the balloon is ultimately controlled by the text properties
29 // (i.e., font size). This representation uses a layout policy as follows.
30 //
31 // If there is just text and no image, then the text properties and padding
32 // are used to control the size of the balloon.
33 //
34 // If there is just an image and no text, then the ImageSize[2] member is
35 // used to control the image size. (The image will fit into this rectangle,
36 // but will not necessarily fill the whole rectangle, i.e., the image is not
37 // stretched).
38 //
39 // If there is text and an image, the following approach ia used. First,
40 // based on the font size and other related properties (e.g., padding),
41 // determine the size of the frame. Second, depending on the layout of the
42 // image and text frame, control the size of the neighboring image (since the
43 // frame and image share a common edge). However, if this results in an image
44 // that is smaller than ImageSize[2], then the image size will be set to
45 // ImageSize[2] and the frame will be adjusted accordingly. The text is
46 // always placed in the center of the frame if the frame is resized.
47 
48 // .SECTION See Also
49 // vtkBalloonWidget
50 
51 
52 #ifndef vtkBalloonRepresentation_h
53 #define vtkBalloonRepresentation_h
54 
55 #include "vtkInteractionWidgetsModule.h" // For export macro
56 #include "vtkWidgetRepresentation.h"
57 
58 class vtkTextMapper;
59 class vtkTextActor;
60 class vtkTextProperty;
61 class vtkPoints;
62 class vtkCellArray;
63 class vtkPolyData;
64 class vtkPolyDataMapper2D;
65 class vtkActor2D;
66 class vtkProperty2D;
67 class vtkImageData;
68 class vtkTexture;
69 class vtkPoints;
70 class vtkPolyData;
71 class vtkPolyDataMapper2D;
72 class vtkActor2D;
73 
74 class VTKINTERACTIONWIDGETS_EXPORT vtkBalloonRepresentation : public vtkWidgetRepresentation
75 {
76 public:
77   // Description:
78   // Instantiate the class.
79   static vtkBalloonRepresentation *New();
80 
81   // Description:
82   // Standard VTK methods.
83   vtkTypeMacro(vtkBalloonRepresentation,vtkWidgetRepresentation);
84   void PrintSelf(ostream& os, vtkIndent indent);
85 
86   // Description:
87   // Specify/retrieve the image to display in the balloon.
88   virtual void SetBalloonImage(vtkImageData *img);
89   vtkGetObjectMacro(BalloonImage,vtkImageData);
90 
91   // Description:
92   // Specify/retrieve the text to display in the balloon.
93   vtkGetStringMacro(BalloonText);
94   vtkSetStringMacro(BalloonText);
95 
96   // Description:
97   // Specify the minimum size for the image. Note that this is a bounding
98   // rectangle, the image will fit inside of it. However, if the balloon
99   // consists of text plus an image, then the image may be bigger than
100   // ImageSize[2] to fit into the balloon frame.
101   vtkSetVector2Macro(ImageSize,int);
102   vtkGetVector2Macro(ImageSize,int);
103 
104   // Description:
105   // Set/get the text property (relevant only if text is shown).
106   virtual void SetTextProperty(vtkTextProperty *p);
107   vtkGetObjectMacro(TextProperty,vtkTextProperty);
108 
109   // Description:
110   // Set/get the frame property (relevant only if text is shown).
111   // The frame lies behind the text.
112   virtual void SetFrameProperty(vtkProperty2D *p);
113   vtkGetObjectMacro(FrameProperty,vtkProperty2D);
114 
115   // Description:
116   // Set/get the image property (relevant only if an image is shown).
117   virtual void SetImageProperty(vtkProperty2D *p);
118   vtkGetObjectMacro(ImageProperty,vtkProperty2D);
119 
120 //BTX
121   enum {ImageLeft=0,ImageRight,ImageBottom,ImageTop};
122 //ETX
123   // Description:
124   // Specify the layout of the image and text within the balloon. Note that
125   // there are reduncies in these methods, for example
126   // SetBalloonLayoutToImageLeft() results in the same effect as
127   // SetBalloonLayoutToTextRight(). If only text is specified, or only an
128   // image is specified, then it doesn't matter how the layout is specified.
129   vtkSetMacro(BalloonLayout,int);
130   vtkGetMacro(BalloonLayout,int);
SetBalloonLayoutToImageLeft()131   void SetBalloonLayoutToImageLeft() {this->SetBalloonLayout(ImageLeft);}
SetBalloonLayoutToImageRight()132   void SetBalloonLayoutToImageRight() {this->SetBalloonLayout(ImageRight);}
SetBalloonLayoutToImageBottom()133   void SetBalloonLayoutToImageBottom() {this->SetBalloonLayout(ImageBottom);}
SetBalloonLayoutToImageTop()134   void SetBalloonLayoutToImageTop() {this->SetBalloonLayout(ImageTop);}
SetBalloonLayoutToTextLeft()135   void SetBalloonLayoutToTextLeft() {this->SetBalloonLayout(ImageRight);}
SetBalloonLayoutToTextRight()136   void SetBalloonLayoutToTextRight() {this->SetBalloonLayout(ImageLeft);}
SetBalloonLayoutToTextTop()137   void SetBalloonLayoutToTextTop() {this->SetBalloonLayout(ImageBottom);}
SetBalloonLayoutToTextBottom()138   void SetBalloonLayoutToTextBottom() {this->SetBalloonLayout(ImageTop);}
139 
140   // Description:
141   // Set/Get the offset from the mouse pointer from which to place the
142   // balloon. The representation will try and honor this offset unless there
143   // is a collision with the side of the renderer, in which case the balloon
144   // will be repositioned to lie within the rendering window.
145   vtkSetVector2Macro(Offset,int);
146   vtkGetVector2Macro(Offset,int);
147 
148   // Description:
149   // Set/Get the padding (in pixels) that is used between the text and the
150   // frame.
151   vtkSetClampMacro(Padding,int,0,100);
152   vtkGetMacro(Padding,int);
153 
154   // Description:
155   // These are methods that satisfy vtkWidgetRepresentation's API.
156   virtual void StartWidgetInteraction(double e[2]);
157   virtual void EndWidgetInteraction(double e[2]);
158   virtual void BuildRepresentation();
159   virtual int ComputeInteractionState(int X, int Y, int modify=0);
160 
161   // Description:
162   // Methods required by vtkProp superclass.
163   virtual void ReleaseGraphicsResources(vtkWindow *w);
164   virtual int RenderOverlay(vtkViewport *viewport);
165 
166   // Description:
167   // State is either outside, or inside (on the text portion ot the image).
168   enum _InteractionState {Outside=0, OnText, OnImage};
169 
170 protected:
171   vtkBalloonRepresentation();
172   ~vtkBalloonRepresentation();
173 
174   // The balloon text and image
175   char         *BalloonText;
176   vtkImageData *BalloonImage;
177 
178   // The layout of the balloon
179   int BalloonLayout;
180 
181   // Controlling placement
182   int Padding;
183   int Offset[2];
184   int ImageSize[2];
185 
186   // Represent the text
187   vtkTextMapper       *TextMapper;
188   vtkActor2D          *TextActor;
189   vtkTextProperty     *TextProperty;
190 
191   // Represent the image
192   vtkTexture          *Texture;
193   vtkPolyData         *TexturePolyData;
194   vtkPoints           *TexturePoints;
195   vtkPolyDataMapper2D *TextureMapper;
196   vtkActor2D          *TextureActor;
197   vtkProperty2D       *ImageProperty;
198 
199   // The frame
200   vtkPoints           *FramePoints;
201   vtkCellArray        *FramePolygon;
202   vtkPolyData         *FramePolyData;
203   vtkPolyDataMapper2D *FrameMapper;
204   vtkActor2D          *FrameActor;
205   vtkProperty2D       *FrameProperty;
206 
207   // Internal variable controlling rendering process
208   int TextVisible;
209   int ImageVisible;
210 
211   // Helper methods
212   void AdjustImageSize(double imageSize[2]);
213   void ScaleImage(double imageSize[2],double scale);
214 
215 private:
216   vtkBalloonRepresentation(const vtkBalloonRepresentation&);  //Not implemented
217   void operator=(const vtkBalloonRepresentation&);  //Not implemented
218 };
219 
220 #endif
221