wxArt2D
cansim.h
Go to the documentation of this file.
1 /*! \file wx/canvas/cansim.h
2  \brief simple canvas which takes as view the whole of the scrollable area.
3  While a2dCanvas display and draws only what is visible, this a2dCanvasSim always
4  draws all that will be scrollable. So scrolling is quick.
5 
6  \author Klaas Holwerda
7 
8  Copyright: 2001-2004 (C) Klaas Holwerda
9 
10  Licence: wxWidgets Licence
11 
12  RCS-ID: $Id: cansim.h,v 1.15 2008/07/19 18:29:43 titato Exp $
13 */
14 
15 #ifndef __A2DCANVASSIM_H__
16 #define __A2DCANVASSIM_H__
17 
18 #ifndef WX_PRECOMP
19 #include "wx/wx.h"
20 #endif
21 
22 #include "wx/image.h"
23 #include "wx/geometry.h"
24 #include "wx/canvas/candefs.h"
25 #include "wx/canvas/canobj.h"
26 #include "wx/artbase/bbox.h"
27 #include "wx/canvas/drawer.h"
28 
29 //! Simple canvas using a whole view for all of the scrolled window
30 /*!
31  This canvas use a wxScrolledWindow as basis.
32  The a2dCanvasView used to draw has a buffer the size of the virtual scrollable area.
33  The scrollable/virtual area is defined in pixels. The window show the part of the virtual area
34  which is visible. One can scroll through the contents of the buffer, and the scroll bars show which
35  part of the buffer is visble.
36  So the scrollable area calculated in world coordinates, defines the maximum boundaries of what can be seen
37  of the drawing in the associated a2dCanvasDocument. One can set the mapping matrixes which calculates
38  device to world coordinates and visa versa, but this does not influence the scrollbars, since the always
39  only show what part of the buffer is visible. Resizing a window, sets the drawing buffer to the new virtual
40  size of the scrolled window, but mapping from world to device coordinates stays the same.
41 
42  This is rather different from what is done in a2dCanvas, where the scrollable area is defined in
43  worldcoordinates, and zooming recalculates the scrollbar positions in such a manner that they indicate
44  what part of the drawing is shown in the canvas window as compared to the total visible area.
45 
46  \sa a2dCanvas
47  a2dCanvasView
48  a2dMemDcDrawer
49  a2dCanvasDocument
50  a2dCanvasObject
51 
52  \ingroup drawer docview
53 */
54 class A2DCANVASDLLEXP a2dCanvasSim: public wxScrolledWindow
55 {
56 public:
57 
58  //!constructor
59  /*!
60  construct a canvas window.
61 
62  Internal a a2dCanvasView and a2dCanvasDocument are created
63  to render all objects stored in the a2dCanvasDocument into this a2dCanvas window.
64 
65  The document and drawer or deleted in the destructor.
66 
67 
68  \remark used for standalone a2dCanvas windows.
69 
70  \param parent parent window (use wxNO_FULL_REPAINT_ON_RESIZE on parent wxFrame)
71  \param id window id
72  \param pos position of window
73  \param size size of window
74  \param style type of window (wxHSCROLL|wxVSCROLL)
75 
76  \remark with new unknown drawer types you can derive or extend the library
77  */
78  a2dCanvasSim( wxWindow* parent, wxWindowID id = -1,
79  const wxPoint& pos = wxDefaultPosition,
80  const wxSize& size = wxDefaultSize,
81  long style = wxScrolledWindowStyle );
82 
83  //!destructor
84  /*!when a tool controller was set is will be deleted also.
85  */
86  virtual ~a2dCanvasSim();
87 
88  //! use the boundingbox of the ShowObject to set the mapping such that it will be displayed completely.
89  /*!
90  \param centre if true centre on window, else to (0,0) of device
91  */
92  void SetMappingShowAll( bool centre = true );
93 
94  a2dDrawingPart* GetDrawingPart() { return m_drawingPart; }
95 
96  void SetDrawingPart( a2dDrawingPart* drawpart );
97 
98  a2dDrawing* GetDrawing() const;
99 
100  //! Get the drawer of the view
101  a2dDrawer2D* GetDrawer2D() const { return m_drawingPart->GetDrawer2D(); }
102 
103  //! Clears the canvas (like wxWindow::ClearBackground)
104  /*!
105  * Calls internally a2dCanvasSim::ClearBackground()
106  * Clear() was renamed in wxWin 2.5 to ClearBackground()
107  *
108  * \todo
109  * Remove this method after wxWin 2.6 release.
110  */
111  void ClearBackground();
112 
113  //!background fill for the canvas
114  void SetBackgroundFill( const a2dFill& backgroundfill );
115 
116  //! Refresh window
117  /*!
118  Next to base class, makes sure all pending objects are processed, and scrolling is set right.
119  */
120  virtual void Refresh( bool eraseBackground = true, const wxRect* rect = NULL );
121 
122  //!Set grid setting for drawing grid in front or back
123  void SetGridAtFront( bool gridatfront ) { m_drawingPart->SetGridAtFront( gridatfront ); }
124 
125  //!Get grid setting for drawing grid in front or back
126  bool GetGridAtFront() { return m_drawingPart->GetGridAtFront(); }
127 
128  //!set stroke used for grid drawing
129  void SetGridStroke( const a2dStroke& gridstroke ) { m_drawingPart->SetGridStroke( gridstroke ); }
130 
131  //!set size of grid circle
132  void SetGridSize( wxUint16 gridsize ) { m_drawingPart->SetGridSize( gridsize ); }
133 
134  //!set brush used for grid drawing
135  void SetGridFill( const a2dFill& gridfill ) { m_drawingPart->SetGridFill( gridfill ); }
136 
137  //!Get grid distance in X
138  double GetGridX() { return m_drawingPart->GetGridX(); }
139 
140  //!Set grid distance in X
141  void SetGridX( double gridx ) { m_drawingPart->SetGridX( gridx ); }
142 
143  //!Get grid distance in Y
144  double GetGridY() { return m_drawingPart->GetGridY(); }
145 
146  //!Set grid distance in Y
147  void SetGridY( double gridy ) { m_drawingPart->SetGridY( gridy ); }
148 
149  //!Set grid on/off
150  void SetGrid( bool grid ) { m_drawingPart->SetGrid( grid ); }
151 
152  //!Get grid setting on/off
153  bool GetGrid() { return m_drawingPart->GetGrid(); }
154 
155  //!Get grid setting for line drawing
156  void SetGridLines( bool gridlines ) { m_drawingPart->SetGridLines( gridlines ); }
157 
158  //!set grid to draw lines instead of points
159  bool GetGridLines() { return m_drawingPart->GetGridLines(); }
160 
161  //!Set showorigin on/off
162  void SetShowOrigin( bool show ) { m_drawingPart->SetShowOrigin( show ); }
163 
164  //!set if the Yaxis goes up or down
165  void SetYaxis( bool up );
166 
167  //!get currently used Yaxis setting
168  bool GetYaxis() const;
169 
170  //! zoomout leafs a border of this amount of pixels around the drawing
171  void SetZoomOutBorder( wxUint16 border ) { m_border = border; }
172 
173  //!Returns if canvas is frozen.
174  bool IsFrozen() { return m_frozen; }
175 
176  //!prevent changing the a2dCanvasView buffer and blitting it to the window
177  /*! To make sure the contents displayed into the a2dCanvasView buffer does not change.
178  Pending objects inside a root will be added to the update list of the a2dCanvasView,
179  but not redrawn into the buffer until Thaw.
180  */
181  void Freeze();
182 
183  //!allow a2dCanvasView buffer to be changed and blitting it to the window
184  /*! Enable updating of the a2dCanvasView buffer contents.
185  */
186  void Thaw();
187 
188  //!Give the virtual size to be displayed, the mappingmatrix will be calculated.
189  /*!
190  The current window size in pixels is used to calculate the mapping such that
191  at least it will display all of the area given.
192 
193  Setting virtual area to boundingbox of a drawing (currently visible ShowObject()
194  \code
195  m_worldcanvas->SetMappingWidthHeight(m_worldcanvas->GetShowObject()->GetXMin(),
196  m_worldcanvas->GetShowObject()->GetYMin(),
197  m_worldcanvas->GetShowObject()->GetWidth(),
198  m_worldcanvas->GetShowObject()->GetHeight())
199  \endcode
200 
201  \remark do not use during start up since window size is not well defined in that case resulting in
202  \remark bad settings for the mapping.
203 
204  \param vx1 minimum world x coordinate
205  \param vy1 minimum world y coordiante ( either Lower or Upper Left corner depending on SetYaxis() )
206  \param width minimum width in world coordinates which we want to display on this window
207  \param height minimum height in world coordinates which we want to display on this window
208  */
209  void SetMappingWidthHeight( double vx1, double vy1, double width, double height );
210 
211  //!Give the virtual size to be displayed, the mappingmatrix will be calculated.
212  /*!
213  To display all of a drawing, set this here to the boundingbox of the show object
214  of the canvas.
215  So vx1 and vx2 to the miminum x and y of the boundingbox.
216  Calculate xpp and ypp in such a manner that it will show the whole drawing.
217 
218  Setting virtual area to boundingbox of a drawing (currently visible group)
219 
220  \code
221  int dx2,dy2;
222  m_canvas->GetClientSize(&dx2,&dy2);
223  double xupp=(m_canvas->GetShowObject()->GetWidth())/dx2;
224  double yupp=(m_canvas->GetShowObject()->GetHeight())/dy2;
225  if (yupp > xupp)
226  xupp=yupp;
227  m_worldcanvas->SetMappingUpp(m_worldcanvas->GetShowObject->GetXMin(),
228  m_worldcanvas->GetShowObject->GetYMin(),xupp,xupp);
229  \endcode
230 
231  If a scrollable area is set, it will be called also to adjust it.
232  \param vx1 minimum world x coordinate
233  \param vy1 minimum world y coordiante ( either Lower or Upper Left corner depending on SetYaxis() )
234  \param xpp: Number of world units per pixel in x
235  \param ypp: Number of world units per pixel in y
236  */
237  void SetMappingUpp( double vx1, double vy1, double xpp, double ypp );
238 
239  //!convert x from window to virtual coordinates
240  inline double DeviceToWorldX( int x ) const { return m_drawingPart->GetDrawer2D()->DeviceToWorldX( x ); }
241  //!convert y from window to virtual coordinates
242  inline double DeviceToWorldY( int y ) const { return m_drawingPart->GetDrawer2D()->DeviceToWorldY( y ); }
243  //!convert x relative from window to virtual coordinates
244  //!use this to convert a Length of a line for instance
245  inline double DeviceToWorldXRel( int x ) const { return m_drawingPart->GetDrawer2D()->DeviceToWorldXRel( x ); }
246  //!convert y relative from window to virtual coordinates
247  //!use this to convert a Length of a line for instance
248  inline double DeviceToWorldYRel( int y ) const { return m_drawingPart->GetDrawer2D()->DeviceToWorldYRel( y ); }
249 
250  //!convert x from virtual to window coordinates
251  inline int WorldToDeviceX( double x ) const { return m_drawingPart->GetDrawer2D()->WorldToDeviceX( x ); }
252  //!convert y from virtual to window coordinates
253  inline int WorldToDeviceY( double y ) const { return m_drawingPart->GetDrawer2D()->WorldToDeviceY( y ); }
254  //!convert x relative from virtual to window coordinates
255  //!use this to convert a Length of a line for instance
256  inline int WorldToDeviceXRel( double x ) const { return m_drawingPart->GetDrawer2D()->WorldToDeviceXRel( x ); }
257  //!convert y relative from virtual to window coordinates
258  //!use this to convert a Length of a line for instance
259  inline int WorldToDeviceYRel( double y ) const { return m_drawingPart->GetDrawer2D()->WorldToDeviceYRel( y ); }
260 
261  //!set object available in the a2dCanvasDocument to be shown on the canvas
262  //!\param name: name of top object
263  //!\return pointer to the object found else NULL
264  a2dCanvasObject* SetShowObject( const wxString& name );
265 
266  //!set top object available in the a2dCanvasDocument to be shown on the canvas
267  //!\param obj: pointer to object to show
268  bool SetShowObject( a2dCanvasObject* obj );
269 
270  //!return pointer of then currently shown object on the canvas.
271  //!\return pointer to the current object that is shown.
272  a2dCanvasObject* GetShowObject() const { return m_drawingPart->GetShowObject(); }
273 
274  //!do a hittest on the canvas at coordinates x,y
275  /*!
276  \param x: x of point to do hittest
277  \param y: y of point to do hittest
278  \param layer only if object is on this layer or if set to wxLAYER_ALL ignore layer id.
279  \param option special hittest options
280  \return the top object that was hit (e.g.in case of groups)
281 
282  \remark hit margin is defined in a2dCanvasDocument containing the root group
283  */
284  a2dCanvasObject* IsHitWorld(
285  double x, double y,
286  int layer = wxLAYER_ALL,
288  );
289 
290  //!Mouse events are handled by the canvas.
291  //!They are redirected to the object hit.
292  //!You can switch this off here, used in most tools.
293  void SetMouseEvents( bool onoff );
294 
295  bool GetMouseEvents();
296 
297  //! write what you see to an SVG( scalable vector graphics file )
298  bool WriteSVG( const wxString& filename, double Width, double Height, wxString unit );
299 
300 protected:
301 
302  //! resize, adjusting buffer of a2dCanvasView if needed.
303  void OnSize( wxSizeEvent& event );
304 
305  //! repaint damaged araes, taking into acount non updated araes in a2dCanvasView.
306  void OnPaint( wxPaintEvent& event );
307 
308  //! Not yet implemented
309  void OnEraseBackground( wxEraseEvent& event );
310 
311  //! remove all pending update areas in a2dCanvasView
312  void DeleteAllPendingAreas();
313 
314 private:
315 
316  //!oversize of buffer compared to screen width and height
317  int m_delta;
318 
319  //!prevent updating activity if true
320  bool m_frozen;
321 
322  a2dSmrtPtr<a2dDrawingPart> m_drawingPart;
323 
324  //! set if the canvas own the drawing
325  a2dDrawingPtr m_drawing;
326 
327  //! border zoomout but leaf around a border of this amount of pixels.
328  wxUint16 m_border;
329 
330  DECLARE_CLASS( a2dCanvasSim )
331  DECLARE_EVENT_TABLE()
332 
333 };
334 
335 #endif
336 // A2DCANVAS
337 
Display Part of a a2dDrawing, in which a2dCanvasObjects are shown.
Definition: drawer.h:470
Base class for all types of strokes, understood by a2dDrawer2D classes.
Definition: stylebase.h:378
void SetGridX(double gridx)
Set grid distance in X.
Definition: cansim.h:141
void SetGridSize(wxUint16 gridsize)
set size of grid circle
Definition: cansim.h:132
double DeviceToWorldX(int x) const
convert x from window to virtual coordinates
Definition: cansim.h:240
Simple canvas using a whole view for all of the scrolled window.
Definition: cansim.h:54
The base class for all drawable objects in a a2dCanvasDocument.
a2dCanvasObject is the base class for Canvas Objects.
Definition: canobj.h:371
void SetGridAtFront(bool gridatfront)
Set grid setting for drawing grid in front or back.
Definition: cansim.h:123
double DeviceToWorldXRel(int x) const
Definition: cansim.h:245
bool GetGrid()
Get grid setting on/off.
Definition: cansim.h:153
int WorldToDeviceXRel(double x) const
Definition: cansim.h:256
defenitions an no more
a2dCanvasObject * GetShowObject() const
Definition: cansim.h:272
void SetGridLines(bool gridlines)
Get grid setting for line drawing.
Definition: cansim.h:156
void SetGridY(double gridy)
Set grid distance in Y.
Definition: cansim.h:147
bool GetGridAtFront()
Get grid setting for drawing grid in front or back.
Definition: cansim.h:126
a2dHitOption
Enum for hit test options.
Definition: canobj.h:76
bounding class for optimizing drawing speed.
Drawing context abstraction.
Definition: drawer2d.h:177
if set, respect layer order, hit testing is done per layer from the top.
Definition: canobj.h:82
double DeviceToWorldYRel(int y) const
Definition: cansim.h:248
double DeviceToWorldY(int y) const
convert y from window to virtual coordinates
Definition: cansim.h:242
if set, don&#39;t hit test the root object or object for which IsHitWorld is called
Definition: canobj.h:87
int WorldToDeviceX(double x) const
convert x from virtual to window coordinates
Definition: cansim.h:251
bool IsFrozen()
Returns if canvas is frozen.
Definition: cansim.h:174
double GetGridX()
Get grid distance in X.
Definition: cansim.h:138
void SetShowOrigin(bool show)
Set showorigin on/off.
Definition: cansim.h:162
void SetZoomOutBorder(wxUint16 border)
zoomout leafs a border of this amount of pixels around the drawing
Definition: cansim.h:171
bool GetGridLines()
set grid to draw lines instead of points
Definition: cansim.h:159
void SetGridFill(const a2dFill &gridfill)
set brush used for grid drawing
Definition: cansim.h:135
void SetGridStroke(const a2dStroke &gridstroke)
set stroke used for grid drawing
Definition: cansim.h:129
int WorldToDeviceYRel(double y) const
Definition: cansim.h:259
the a2dDrawingPart is a a2dView specially designed for displaying parts of a a2dDrawing. It uses a a2dDrawer2D to actually redraw things from the document, by giving that a2dDrawer2D as drawing context to the document, and telling the document to redraw a certain rectangular area. At that last is what this class is for. It optimizes the areas to be redrawn after object in the document were changed. To do that it combines redraw areas to a minimal set of redrawing areas. All the administration for this and the way things will be redrawn is from this view.
double GetGridY()
Get grid distance in Y.
Definition: cansim.h:144
int WorldToDeviceY(double y) const
convert y from virtual to window coordinates
Definition: cansim.h:253
a2dDrawer2D * GetDrawer2D() const
Get the drawer of the view.
Definition: cansim.h:101
Contain one drawing as hierarchical tree of a2dCanvasObject&#39;s.
Definition: drawing.h:434
void SetGrid(bool grid)
Set grid on/off.
Definition: cansim.h:150
cansim.h Source File -- Sun Oct 12 2014 17:04:14 -- Sun Oct 12 2014 -- 1.8.5 -- wxArt2D -- . -- Main Page Reference Documentation