Fixed UTF-8 file save bug Update to circle start handler Started two
[dasher.git] / Src / Gtk2 / Canvas.h
blob7dbe6ad65236f6706b4c01cc1ebba5eb646f9d15
1 #ifndef __canvas_h__
2 #define __canvas_h__
4 #include "../DasherCore/DasherScreen.h"
5 #include "../DasherCore/DasherTypes.h"
6 #include "../DasherCore/CustomColours.h"
8 #include <gtk/gtk.h>
9 #include <gdk/gdk.h>
10 #include "PangoCache.h"
12 #include <iostream>
14 #if WITH_CAIRO
16 /* Cairo drawing backend */
17 #include <gdk/gdkcairo.h>
19 typedef struct {
20 double r, g, b;
21 } my_cairo_colour_t;
24 #define BEGIN_DRAWING_BACKEND \
25 cairo_save(cr)
27 #define END_DRAWING_BACKEND \
28 cairo_restore(cr)
30 #define SET_COLOR_BACKEND(c) \
31 do { \
32 my_cairo_colour_t _c = cairo_colours[(c)]; \
33 cairo_set_source_rgb(cr, _c.r, _c.g, _c.b); \
34 } while (0)
36 #else /* WITHOUT_CAIRO */
38 #define BEGIN_DRAWING_BACKEND \
39 GdkGCValues origvalues; \
40 gdk_gc_get_values(graphics_context,&origvalues)
42 #define END_DRAWING_BACKEND \
43 gdk_gc_set_values(graphics_context,&origvalues,GDK_GC_FOREGROUND)
45 #define SET_COLOR_BACKEND(c) \
46 do { \
47 GdkColor _c = colours[(c)]; \
48 gdk_colormap_alloc_color(colormap, &_c, FALSE, TRUE); \
49 gdk_gc_set_foreground (graphics_context, &_c); \
50 } while (0)
52 #endif /* WITH_CAIRO */
54 // Some other useful macros (for all backends)
56 #define BEGIN_DRAWING \
57 BEGIN_DRAWING_BACKEND
59 #define END_DRAWING \
60 END_DRAWING_BACKEND
62 #define SET_COLOR(c) \
63 SET_COLOR_BACKEND(c)
65 using namespace Dasher;
67 /// CCanvas
68 ///
69 /// Method definitions for CCanvas, implementing the CDasherScreen
70 /// interface. Please think very carefully before implementing new
71 /// functionality in this class. Anything which isn't a 'drawing
72 /// primitive' should really not be here - higher level drawing
73 /// functions belong in CDasherView.
75 class CCanvas:public Dasher::CDasherScreen {
77 public:
79 ///
80 /// \param pCanvas The GTK drawing area used by the canvas
81 /// \param pPangoCache A cache for precomputed Pango layouts
82 ///
84 CCanvas(GtkWidget * pCanvas, CPangoCache * pPangoCache);
85 ~CCanvas();
87 ///
88 /// GTK signal handler for exposure of the canvas - cause a redraw to the screen from the buffer.
89 ///
91 bool ExposeEvent( GtkWidget *pWidget, GdkEventExpose *pEvent);
94 // CDasherScreen methods
96 ///
97 /// Set the font used to render the Dasher display
98 /// \param Name The name of the font.
99 /// \todo This needs to be reimplemented for 4.0
100 /// \deprecated In Linux - now handled by the pango cache, but need to think how this fits in with Windows
101 ///
103 void SetFont(std::string Name) {
107 /// Set the font size for rendering
108 /// \param fontsize The font size to use
109 /// \deprecated Obsolete
112 void SetFontSize(Dasher::Opts::FontSize fontsize) {
117 /// Get the current font size
118 /// \deprecated To be removed before 4.0 release
119 /// \todo We should not be relying on locally cached variables - check to see whether this is still used or not
122 Dasher::Opts::FontSize GetFontSize() {
123 return Dasher::Opts::FontSize(1);
127 /// Return the physical extent of a given string being rendered at a given size.
128 /// \param String The string to be rendered
129 /// \param Width Pointer to a variable to be filled with the width
130 /// \param Height Pointer to a variable to be filled with the height
131 /// \param Size Size at which the string will be rendered (units?)
134 void TextSize(const std::string &String, screenint *Width, screenint *Height, int Size);
137 /// Draw a text string
138 /// \param String The string to be rendered
139 /// \param x1 The x coordinate at which to draw the text (be more precise)
140 /// \param y1 The y coordinate at which to draw the text (be more precise)
141 /// \param Size The size at which to render the rectangle (units?)
144 void DrawString(const std::string &String, screenint x1, screenint y1, int Size);
147 /// Draw a rectangle
148 /// \param x1 x coordiate of the top left corner
149 /// \param y1 y coordiate of the top left corner
150 /// \param x2 x coordiate of the bottom right corner
151 /// \param y2 y coordiate of the bottom right corner
152 /// \param Color Colour to draw the rectangle
153 /// \param ColorScheme Which of the alternating colour schemes to use (be more precise)
154 /// \param bDrawOutline Whether or not to draw outlines for the boxes
157 void DrawRectangle(screenint x1, screenint y1, screenint x2, screenint y2, int Color, int iOutlineColour, Opts::ColorSchemes ColorScheme, bool bDrawOutine, bool bFill, int iThickness);
159 void DrawCircle(screenint iCX, screenint iCY, screenint iR, int iColour, int iFillColour, int iThickness, bool bFill);
162 /// Send a marker to indicate phases of the redraw process. This is
163 /// done so that we can do tripple buffering to minimise the amount
164 /// of costly font rendering which needs to be done. Marker 1
165 /// indicates that start of a new frame. Marker 2 indicates that we
166 /// are now drawing decoration (such as mouse cursors etc.) rather
167 /// than tne background. Only marker 2 will be send while Dasher is
168 /// paused.
169 /// \param iMarker ID of the marker being sent.
172 void SendMarker(int iMarker);
174 ///
175 /// Draw a coloured polyline
176 /// \param Points Array of vertices
177 /// \param Number Size of 'Points' array
178 /// \param Colour Colour with which to draw the line
181 void Polyline(point * Points, int Number, int iWidth, int Colour);
183 ///
184 /// Like polyline, but fill the shape
185 /// \todo See comments for DrawPolygon
188 void Polygon(point *Points, int Number, int Colour, int iWidth);
191 /// \todo Not implemented
192 /// \todo One of these two routines must be redundant - find out which and kill the other
195 void DrawPolygon(point *Points, int Number, int Color, Opts::ColorSchemes ColorScheme) {
196 // not implemented
199 ///
200 /// Blank the diplay
203 void Blank();
205 ///
206 /// Marks the end of the display process - at this point the offscreen buffer is copied onscreen.
209 void Display();
212 /// Update the colour definitions
213 /// \param Colours New colours to use
216 void SetColourScheme(const CCustomColours *Colours);
218 ///
219 /// Gets the location and size of our canvas.
220 /// Returns true on success, false otherwise.
221 bool GetCanvasSize(GdkRectangle *pRectangle);
223 ///
224 /// Canvas width
227 int m_iWidth;
230 /// Canvas height
233 int m_iHeight;
235 private:
238 /// The GTK drawing area for the canvas
241 GtkWidget *m_pCanvas;
244 /// The offscreen buffer containing the 'background'
247 GdkPixmap *m_pDisplayBuffer;
249 ///
250 /// The offscreen buffer containing the full display. This is
251 /// constructed by first copying the display buffer across and then
252 /// drawing decorations such as the mouse cursor on top.
255 GdkPixmap *m_pDecorationBuffer;
258 /// The onscreen buffer - copied onscreen whenever an expose event occurs.
261 GdkPixmap *m_pOnscreenBuffer;
264 /// Pointer to which of the offscreen buffers is currently active.
267 GdkPixmap *m_pOffscreenBuffer;
268 GdkPixmap *m_pDummyBuffer;
270 ///
271 /// The Pango cache - used to store pre-computed pango layouts as
272 /// they are costly to regenerate every time they are needed.
275 CPangoCache *m_pPangoCache;
278 /// Holder for Pango layout extents.
281 PangoRectangle *m_pPangoInk;
284 /// The signal handler ID for the expose callback - stored so it can
285 /// be disconnected when the CCanvas object is destroyed (which will
286 /// happen whenever the canvas is resized).
289 gulong lSignalHandler;
291 #if WITH_CAIRO
292 cairo_t *display_cr;
293 cairo_t *decoration_cr;
294 cairo_t *cr;
295 my_cairo_colour_t *cairo_colours;
296 #else
297 GdkColor *colours;
298 #endif
302 #endif