1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkTextRepresentation.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 /**
16  * @class   vtkTextRepresentation
17  * @brief   represent text for vtkTextWidget
18  *
19  * This class represents text for a vtkTextWidget.  This class provides
20  * support for interactively placing text on the 2D overlay plane. The text
21  * is defined by an instance of vtkTextActor.
22  *
23  * @sa
24  * vtkTextRepresentation vtkBorderWidget vtkAbstractWidget vtkWidgetRepresentation
25  */
26 
27 #ifndef vtkTextRepresentation_h
28 #define vtkTextRepresentation_h
29 
30 #include "vtkBorderRepresentation.h"
31 #include "vtkInteractionWidgetsModule.h" // For export macro
32 
33 class vtkRenderer;
34 class vtkTextActor;
35 class vtkTextProperty;
36 class vtkTextRepresentationObserver;
37 
38 class VTKINTERACTIONWIDGETS_EXPORT vtkTextRepresentation : public vtkBorderRepresentation
39 {
40 public:
41   /**
42    * Instantiate class.
43    */
44   static vtkTextRepresentation* New();
45 
46   ///@{
47   /**
48    * Standard VTK methods.
49    */
50   vtkTypeMacro(vtkTextRepresentation, vtkBorderRepresentation);
51   void PrintSelf(ostream& os, vtkIndent indent) override;
52   ///@}
53 
54   ///@{
55   /**
56    * Specify the vtkTextActor to manage. If not specified, then one
57    * is automatically created.
58    */
59   void SetTextActor(vtkTextActor* textActor);
60   vtkGetObjectMacro(TextActor, vtkTextActor);
61   ///@}
62 
63   ///@{
64   /**
65    * Get/Set the text string display by this representation.
66    */
67   void SetText(const char* text);
68   const char* GetText();
69   ///@}
70 
71   /**
72    * Satisfy the superclasses API.
73    */
74   void BuildRepresentation() override;
GetSize(double size[2])75   void GetSize(double size[2]) override
76   {
77     size[0] = 2.0;
78     size[1] = 2.0;
79   }
80 
81   ///@{
82   /**
83    * These methods are necessary to make this representation behave as
84    * a vtkProp.
85    */
86   void GetActors2D(vtkPropCollection*) override;
87   void ReleaseGraphicsResources(vtkWindow*) override;
88   int RenderOverlay(vtkViewport*) override;
89   int RenderOpaqueGeometry(vtkViewport*) override;
90   int RenderTranslucentPolygonalGeometry(vtkViewport*) override;
91   vtkTypeBool HasTranslucentPolygonalGeometry() override;
92   ///@}
93 
94   enum
95   {
96     AnyLocation = 0,
97     LowerLeftCorner,
98     LowerRightCorner,
99     LowerCenter,
100     UpperLeftCorner,
101     UpperRightCorner,
102     UpperCenter
103   };
104 
105   ///@{
106   /**
107    * Set the text position, by enumeration (
108    * AnyLocation = 0,
109    * LowerLeftCorner,
110    * LowerRightCorner,
111    * LowerCenter,
112    * UpperLeftCorner,
113    * UpperRightCorner,
114    * UpperCenter)
115    * related to the render window
116    */
117   virtual void SetWindowLocation(int enumLocation);
118   vtkGetMacro(WindowLocation, int);
119   ///@}
120 
121   ///@{
122   /**
123    * Set the text position, by overriding the same function of
124    * vtkBorderRepresentation so that the Modified() will be called.
125    */
126   void SetPosition(double x, double y) override;
SetPosition(double pos[2])127   void SetPosition(double pos[2]) override { this->SetPosition(pos[0], pos[1]); }
128   ///@}
129 
130   ///@{
131   /**
132    * Internal. Execute events observed by internal observer
133    */
134   void ExecuteTextPropertyModifiedEvent(vtkObject* obj, unsigned long enumEvent, void* p);
135   void ExecuteTextActorModifiedEvent(vtkObject* obj, unsigned long enumEvent, void* p);
136   ///@}
137 
138   ///@{
139   /**
140    * Set/Get the padding between the text and the left border,
141    * in pixels unit.
142    * Default is 0.
143    */
144   vtkSetClampMacro(PaddingLeft, int, 0, 4000);
145   vtkGetMacro(PaddingLeft, int);
146   ///@}
147 
148   ///@{
149   /**
150    * Set/Get the padding between the text and the right border,
151    * in pixels unit.
152    * Default is 0.
153    */
154   vtkSetClampMacro(PaddingRight, int, 0, 4000);
155   vtkGetMacro(PaddingRight, int);
156   ///@}
157 
158   ///@{
159   /**
160    * Set/Get the padding between the text and the top border,
161    * in pixels unit.
162    * Default is 0.
163    */
164   vtkSetClampMacro(PaddingTop, int, 0, 4000);
165   vtkGetMacro(PaddingTop, int);
166   ///@}
167 
168   ///@{
169   /**
170    * Set/Get the padding between the text and the bottom border,
171    * in pixels unit.
172    * Default is 0.
173    */
174   vtkSetClampMacro(PaddingBottom, int, 0, 4000);
175   vtkGetMacro(PaddingBottom, int);
176   ///@}
177 
178   ///@{
179   /**
180    * Set the padding between the text and the left/right/top/bottom
181    * border, in pixels unit.
182    * Default is 0.
183    */
184   void SetPadding(int padding);
185   ///@}
186 
187 protected:
188   vtkTextRepresentation();
189   ~vtkTextRepresentation() override;
190 
191   // Initialize text actor
192   virtual void InitializeTextActor();
193 
194   // Check and adjust boundaries according to the size of the text
195   virtual void CheckTextBoundary();
196 
197   // the text to manage
198   vtkTextActor* TextActor;
199   vtkTextProperty* TextProperty;
200 
201   // Window location by enumeration
202   int WindowLocation;
203   virtual void UpdateWindowLocation();
204 
205   // observer to observe internal TextActor and TextProperty
206   vtkTextRepresentationObserver* Observer;
207 
208   int PaddingLeft = 0;
209   int PaddingRight = 0;
210   int PaddingTop = 0;
211   int PaddingBottom = 0;
212 
213 private:
214   vtkTextRepresentation(const vtkTextRepresentation&) = delete;
215   void operator=(const vtkTextRepresentation&) = delete;
216 };
217 
218 #endif
219