Update Turkish translation
[dasher.git] / Src / Gtk2 / Canvas.h
blob6e34c7e9cb441dc4a70cd982498d6c6644ad551e
1 #ifndef __canvas_h__
2 #define __canvas_h__
4 #include <cstdlib>
6 #include "../DasherCore/DasherScreen.h"
7 #include "../DasherCore/DasherTypes.h"
9 #include <gtk/gtk.h>
10 #include <gdk/gdk.h>
11 #include <pango/pango.h>
12 #include <map>
14 #include <iostream>
16 #if WITH_CAIRO
18 #define BEGIN_DRAWING_BACKEND \
19 cairo_save(cr)
21 #define END_DRAWING_BACKEND \
22 cairo_restore(cr)
24 #define SET_COLOR_BACKEND(c) \
25 cairo_set_source(cr, cairo_colours[(c)])
27 #else /* WITHOUT_CAIRO */
29 #define BEGIN_DRAWING_BACKEND \
30 GdkGCValues origvalues; \
31 gdk_gc_get_values(graphics_context,&origvalues)
33 #define END_DRAWING_BACKEND \
34 gdk_gc_set_values(graphics_context,&origvalues,GDK_GC_FOREGROUND)
36 #define SET_COLOR_BACKEND(c) \
37 do { \
38 GdkColor _c = colours[(c)]; \
39 gdk_colormap_alloc_color(colormap, &_c, FALSE, TRUE); \
40 gdk_gc_set_foreground (graphics_context, &_c); \
41 } while (0)
43 #endif /* WITH_CAIRO */
45 // Some other useful macros (for all backends)
47 #define BEGIN_DRAWING \
48 BEGIN_DRAWING_BACKEND
50 #define END_DRAWING \
51 END_DRAWING_BACKEND
53 #define SET_COLOR(c) \
54 SET_COLOR_BACKEND(c)
56 /// CCanvas
57 ///
58 /// Method definitions for CCanvas, implementing the CDasherScreen
59 /// interface. Please think very carefully before implementing new
60 /// functionality in this class. Anything which isn't a 'drawing
61 /// primitive' should really not be here - higher level drawing
62 /// functions belong in CDasherView.
64 class CCanvas:public Dasher::CLabelListScreen {
66 public:
67 typedef Dasher::screenint screenint;
68 /// Creates a new canvas - initially of zero size, so drawing
69 /// operations won't do anything until a call to resize() is made.
70 /// \param pCanvas The GTK drawing area used by the canvas
71 ///
73 CCanvas(GtkWidget *pCanvas);
74 ~CCanvas();
76 ///
77 /// GTK signal handler for exposure of the canvas - cause a redraw to the screen from the buffer.
78 ///
80 bool ExposeEvent( GtkWidget *pWidget, GdkEventExpose *pEvent);
83 // CDasherScreen methods
85 ///
86 /// Set the font used to render the Dasher display
87 /// \param Name The name of the font.
88 /// \todo This needs to be reimplemented for 4.0
89 /// \deprecated In Linux - now handled by the pango cache, but need to think how this fits in with Windows
90 ///
92 void SetFont(const std::string &strName);
94 ///Make a label for use with this screen; caches Pango layout information inside it.
95 CDasherScreen::Label *MakeLabel(const std::string &strText, unsigned int iWrapSize=0) override;
97 ///
98 /// Return the physical extent of a given string being rendered at a given size.
99 /// \param String The string to be rendered
100 /// \param Width Pointer to a variable to be filled with the width
101 /// \param Height Pointer to a variable to be filled with the height
102 /// \param Size Size at which the string will be rendered (units?)
105 std::pair<screenint,screenint> TextSize(CDasherScreen::Label *label, unsigned int Size) override;
108 /// Draw a text string
109 /// \param String The string to be rendered
110 /// \param x1 The x coordinate at which to draw the text (be more precise)
111 /// \param y1 The y coordinate at which to draw the text (be more precise)
112 /// \param Size The size at which to render the rectangle (units?)
115 void DrawString(CDasherScreen::Label *label, screenint x1, screenint y1, unsigned int Size, int iColor) override;
118 /// Draw a rectangle
119 /// \param x1 x coordiate of the top left corner
120 /// \param y1 y coordiate of the top left corner
121 /// \param x2 x coordiate of the bottom right corner
122 /// \param y2 y coordiate of the bottom right corner
123 /// \param Color Colour to fill the rectangle (-1 = don't fill)
124 /// \param iOutlineColour Colour to draw the outline (-1 = use default)
125 /// \param iThickness line width of outline (<=0 = don't outline)
127 void DrawRectangle(screenint x1, screenint y1, screenint x2, screenint y2, int Color, int iOutlineColour, int iThickness) override;
129 void DrawCircle(screenint iCX, screenint iCY, screenint iR, int iFillColour, int iLineColour, int iThickness) override;
132 /// Send a marker to indicate phases of the redraw process. This is
133 /// done so that we can do tripple buffering to minimise the amount
134 /// of costly font rendering which needs to be done. Marker 1
135 /// indicates that start of a new frame. Marker 2 indicates that we
136 /// are now drawing decoration (such as mouse cursors etc.) rather
137 /// than tne background. Only marker 2 will be send while Dasher is
138 /// paused.
139 /// \param iMarker ID of the marker being sent.
142 void SendMarker(int iMarker) override;
144 ///
145 /// Draw a coloured polyline
146 /// \param Points Array of vertices
147 /// \param Number Size of 'Points' array
148 /// \param Colour Colour with which to draw the line
151 void Polyline(point * Points, int Number, int iWidth, int Colour) override;
153 ///
154 /// Draw a closed polygon (linking last vertex back to first)
155 /// @param fillColour colour to fill; -1 => don't fill
156 /// @param outlineColour colour to draw outline...
157 /// @param iWidth ...and line thickness; -1 => don't draw outline
160 void Polygon(point *Points, int Number, int fillColour, int outlineColour, int iWidth) override;
162 ///
163 /// Marks the end of the display process - at this point the offscreen buffer is copied onscreen.
166 void Display() override;
169 /// Update the colour definitions
170 /// \param Colours New colours to use
173 void SetColourScheme(const Dasher::CColourIO::ColourInfo *pColourScheme) override;
175 ///
176 /// Gets the location and size of our canvas.
177 /// Returns true on success, false otherwise.
178 bool GetCanvasSize(GdkRectangle *pRectangle);
180 // Redeclare to make public and adjust cairo/gdk surface sizes
181 void resize(screenint w,screenint h);
182 // Returns true if cursor is over visible part of this window.
183 bool IsWindowUnderCursor() override;
185 private:
188 /// The GTK drawing area for the canvas
191 GtkWidget *m_pCanvas;
193 void InitSurfaces();
194 void DestroySurfaces();
195 #if WITH_CAIRO
197 cairo_surface_t *m_pDisplaySurface;
198 cairo_surface_t *m_pDecorationSurface;
199 //cairo_surface_t *m_pOnscreenSurface;
201 cairo_surface_t *m_pOffscreenbuffer;
203 #else
206 /// The offscreen buffer containing the 'background'
209 GdkPixmap *m_pDisplayBuffer;
211 ///
212 /// The offscreen buffer containing the full display. This is
213 /// constructed by first copying the display buffer across and then
214 /// drawing decorations such as the mouse cursor on top.
217 GdkPixmap *m_pDecorationBuffer;
220 /// The onscreen buffer - copied onscreen whenever an expose event occurs.
223 //GdkPixmap *m_pOnscreenBuffer;
226 /// Pointer to which of the offscreen buffers is currently active.
229 GdkPixmap *m_pOffscreenBuffer;
230 //GdkPixmap *m_pDummyBuffer;
232 #endif
234 std::string m_strFontName;
235 std::map<unsigned int,PangoFontDescription *> m_mFonts;
237 class CPangoLabel : public CLabelListScreen::Label {
238 public:
239 CPangoLabel(CCanvas *pCanvas, const std::string &strText, unsigned int iWrapFontSize)
240 : CLabelListScreen::Label(pCanvas, strText, iWrapFontSize) {
242 std::map<unsigned int,PangoLayout *> m_mLayouts;
245 PangoLayout *GetLayout(CPangoLabel *label, unsigned int iFontSize);
247 #if WITH_CAIRO
248 cairo_t *display_cr;
249 cairo_t *decoration_cr;
251 cairo_t *cr; // offscreen
252 cairo_pattern_t **cairo_colours;
253 #else
254 GdkColor *colours;
255 #endif
259 #endif