~thumper/nux/more-leaks

« back to all changes in this revision

Viewing changes to examples/cairo_wrapper.cpp

  • Committer: Mirco Müller
  • Date: 2011-08-10 09:12:53 UTC
  • mfrom: (409.3.3 nux.cairo-wrapper)
  • Revision ID: mirco.mueller@ubuntu.com-20110810091253-t38tlcrwghempc8o
Added class nux::CairoWrapper, which is a complementary class to nux::Canvas.
It intentionally doesn't derive from nux::View and creates bitmaps and
textures based on the passed in nux::Geometry and cairo-drawing callback.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Mirco Müller <mirco.mueller@canonical.com
 
17
 */
 
18
 
 
19
#include "Nux/Nux.h"
 
20
#include "Nux/CairoWrapper.h"
 
21
#include "Nux/VLayout.h"
 
22
#include "Nux/WindowThread.h"
 
23
#include "NuxGraphics/GraphicsEngine.h"
 
24
#include "Nux/TimerProc.h"
 
25
 
 
26
nux::CairoWrapper* g_canvas  = NULL;
 
27
nux::TimerFunctor* g_timer   = NULL;
 
28
nux::TimerHandle   g_handler = NULL;
 
29
 
 
30
void
 
31
DrawRoundedRectangle (cairo_t* cr,
 
32
                      double   aspect,
 
33
                      double   x,
 
34
                      double   y,
 
35
                      double   cornerRadius,
 
36
                      double   width,
 
37
                      double   height)
 
38
{
 
39
  double radius = cornerRadius / aspect;
 
40
 
 
41
  // top-left, right of the corner
 
42
  cairo_move_to (cr, x + radius, y);
 
43
 
 
44
  // top-right, left of the corner
 
45
  cairo_line_to (cr, x + width - radius, y);
 
46
 
 
47
  // top-right, below the corner
 
48
  cairo_arc (cr,
 
49
             x + width - radius,
 
50
             y + radius,
 
51
             radius,
 
52
             -90.0f * G_PI / 180.0f,
 
53
             0.0f * G_PI / 180.0f);
 
54
 
 
55
  // bottom-right, above the corner
 
56
  cairo_line_to (cr, x + width, y + height - radius);
 
57
 
 
58
  // bottom-right, left of the corner
 
59
  cairo_arc (cr,
 
60
             x + width - radius,
 
61
             y + height - radius,
 
62
             radius,
 
63
             0.0f * G_PI / 180.0f,
 
64
             90.0f * G_PI / 180.0f);
 
65
 
 
66
  // bottom-left, right of the corner
 
67
  cairo_line_to (cr, x + radius, y + height);
 
68
 
 
69
  // bottom-left, above the corner
 
70
  cairo_arc (cr,
 
71
             x + radius,
 
72
             y + height - radius,
 
73
             radius,
 
74
             90.0f * G_PI / 180.0f,
 
75
             180.0f * G_PI / 180.0f);
 
76
 
 
77
  // top-left, right of the corner
 
78
  cairo_arc (cr,
 
79
             x + radius,
 
80
             y + radius,
 
81
             radius,
 
82
             180.0f * G_PI / 180.0f,
 
83
             270.0f * G_PI / 180.0f);
 
84
}
 
85
 
 
86
void
 
87
callback (nux::Geometry const& geom, cairo_t* cr)
 
88
{
 
89
  if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
 
90
    return;
 
91
 
 
92
  cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
 
93
  cairo_paint (cr);
 
94
  cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
 
95
  cairo_scale (cr, 1.0, 1.0);
 
96
  cairo_set_source_rgba (cr, 1.0, 0.5, 0.25, 1.0);
 
97
  DrawRoundedRectangle (cr, 1.0, 5.0, 5.0, 10.0, 
 
98
                        (double) geom.width - 10.0,
 
99
                        (double) geom.height - 10.0);
 
100
  cairo_fill (cr);
 
101
}
 
102
 
 
103
void
 
104
terminate (void* data)
 
105
{
 
106
  nux::WindowThread* thread = NUX_STATIC_CAST (nux::WindowThread*, data);
 
107
  thread->TerminateThread ();
 
108
}
 
109
 
 
110
void
 
111
example (void* data)
 
112
{
 
113
  nux::Geometry geom = {0, 0, 200, 150};
 
114
  g_canvas = new nux::CairoWrapper (geom, sigc::ptr_fun (callback));
 
115
  std::string filename = "/tmp/cairo-wrapper-example.png"; 
 
116
  g_canvas->DumpToFile (filename);
 
117
}
 
118
 
 
119
void ThreadWidgetInit (nux::NThread* thread, void* initData)
 
120
{
 
121
  g_timer = new nux::TimerFunctor ();
 
122
  g_timer->OnTimerExpired.connect (sigc::ptr_fun (&example));
 
123
  g_handler = nux::GetTimer().AddTimerHandler (1000,
 
124
                                               g_timer,
 
125
                                               nux::GetWindowThread ());
 
126
}
 
127
 
 
128
int main (int    argc,
 
129
          char** argv)
 
130
{
 
131
  nux::NuxInitialize (0);
 
132
  nux::WindowThread* wt = NULL;
 
133
 
 
134
  wt = nux::CreateGUIThread (TEXT ("Cairo-Wrapper Example"),
 
135
                             400,
 
136
                             400,
 
137
                             0,
 
138
                             &ThreadWidgetInit,
 
139
                             0);
 
140
  wt->Run (NULL);
 
141
  delete wt;
 
142
  delete g_canvas;
 
143
  delete g_timer;
 
144
 
 
145
  return 0;
 
146
}