nux-1.16.0
canvas.cpp
00001 /*
00002  * Copyright (C) 2011 Canonical Ltd
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 3 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Authored by: Mirco Müller <mirco.mueller@canonical.com
00017  */
00018 
00019 #include "Nux/Nux.h"
00020 #include "Nux/Canvas.h"
00021 #include "Nux/View.h"
00022 #include "Nux/VLayout.h"
00023 #include "Nux/WindowThread.h"
00024 #include "NuxGraphics/GraphicsEngine.h"
00025 #include "Nux/Canvas.h"
00026 
00027 void
00028 DrawRoundedRectangle (cairo_t* cr,
00029                       double   aspect,
00030                       double   x,
00031                       double   y,
00032                       double   cornerRadius,
00033                       double   width,
00034                       double   height)
00035 {
00036   double radius = cornerRadius / aspect;
00037 
00038   // top-left, right of the corner
00039   cairo_move_to (cr, x + radius, y);
00040 
00041   // top-right, left of the corner
00042   cairo_line_to (cr, x + width - radius, y);
00043 
00044   // top-right, below the corner
00045   cairo_arc (cr,
00046              x + width - radius,
00047              y + radius,
00048              radius,
00049              -90.0f * G_PI / 180.0f,
00050              0.0f * G_PI / 180.0f);
00051 
00052   // bottom-right, above the corner
00053   cairo_line_to (cr, x + width, y + height - radius);
00054 
00055   // bottom-right, left of the corner
00056   cairo_arc (cr,
00057              x + width - radius,
00058              y + height - radius,
00059              radius,
00060              0.0f * G_PI / 180.0f,
00061              90.0f * G_PI / 180.0f);
00062 
00063   // bottom-left, right of the corner
00064   cairo_line_to (cr, x + radius, y + height);
00065 
00066   // bottom-left, above the corner
00067   cairo_arc (cr,
00068              x + radius,
00069              y + height - radius,
00070              radius,
00071              90.0f * G_PI / 180.0f,
00072              180.0f * G_PI / 180.0f);
00073 
00074   // top-left, right of the corner
00075   cairo_arc (cr,
00076              x + radius,
00077              y + radius,
00078              radius,
00079              180.0f * G_PI / 180.0f,
00080              270.0f * G_PI / 180.0f);
00081 }
00082 
00083 class FooCanvas : public nux::Canvas
00084 {
00085   public:
00086     FooCanvas (NUX_FILE_LINE_DECL);
00087     ~FooCanvas ();
00088 
00089     void Paint ();
00090 };
00091 
00092 FooCanvas::FooCanvas (NUX_FILE_LINE_DECL) : Canvas (NUX_FILE_LINE_PARAM)
00093 {
00094 }
00095 
00096 FooCanvas::~FooCanvas ()
00097 {
00098 }
00099 
00100 void
00101 FooCanvas::Paint ()
00102 {
00103   cairo_t* cr = GetCairoContext ();
00104 
00105   if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
00106     return;
00107 
00108   cairo_scale (cr, 1.0, 1.0);
00109   cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
00110   DrawRoundedRectangle (cr,
00111                         1.0,
00112                         4.0,
00113                         4.0,
00114                         10.0,
00115                         (double) GetLastWidth () - 8.0,
00116                         (double) GetLastHeight () - 8.0);
00117   cairo_set_source_rgba (cr, 1.0f, 0.0f, 0.0f, 1.0f);
00118   cairo_fill_preserve (cr);
00119   cairo_set_source_rgba (cr, 0.0f, 1.0f, 0.0f, 1.0f);
00120   cairo_stroke (cr);
00121   //cairo_surface_write_to_png (GetCairoSurface (), "/tmp/foocanvas.png");
00122 }
00123 
00124 class BarCanvas : public nux::Canvas
00125 {
00126   public:
00127     BarCanvas (NUX_FILE_LINE_DECL);
00128     ~BarCanvas ();
00129 
00130     void Paint ();
00131 };
00132 
00133 BarCanvas::BarCanvas (NUX_FILE_LINE_DECL) : Canvas (NUX_FILE_LINE_PARAM)
00134 {
00135 }
00136 
00137 BarCanvas::~BarCanvas ()
00138 {
00139 }
00140 
00141 void
00142 BarCanvas::Paint ()
00143 {
00144   cairo_t* cr = GetCairoContext ();
00145 
00146   if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
00147     return;
00148 
00149   cairo_scale (cr, 1.0, 1.0);
00150   cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
00151   DrawRoundedRectangle (cr,
00152                         1.0,
00153                         4.0,
00154                         4.0,
00155                         10.0,
00156                         (double) GetLastWidth () - 8.0,
00157                         (double) GetLastHeight () - 8.0);
00158   cairo_set_source_rgba (cr, 0.0f, 0.0f, 1.0f, 1.0f);
00159   cairo_fill_preserve (cr);
00160   cairo_set_source_rgba (cr, 1.0f, 1.0f, 0.0f, 1.0f);
00161   cairo_stroke (cr);
00162   //cairo_surface_write_to_png (GetCairoSurface (), "/tmp/barcanvas.png");
00163 }
00164 
00165 void ThreadWidgetInit (nux::NThread* thread, void* initData)
00166 {
00167   nux::VLayout* layout = new nux::VLayout (TEXT(""), NUX_TRACKER_LOCATION);
00168 
00169   FooCanvas* foocanvas = new FooCanvas (NUX_TRACKER_LOCATION);
00170   BarCanvas* barcanvas = new BarCanvas (NUX_TRACKER_LOCATION);
00171 
00172   layout->AddView (foocanvas, 1, nux::eCenter, nux::eFull);
00173   layout->AddView (barcanvas, 1, nux::eCenter, nux::eFull);
00174   layout->SetContentDistribution (nux::eStackCenter);
00175     
00176   nux::GetWindowThread()->SetLayout (layout);
00177 }
00178 
00179 int main (int    argc,
00180           char** argv)
00181 {
00182   nux::NuxInitialize (0);
00183   nux::WindowThread* wt = NULL;
00184 
00185   wt = nux::CreateGUIThread (TEXT ("Canvas Example"),
00186                              400,
00187                              400,
00188                              0,
00189                              &ThreadWidgetInit,
00190                              0);
00191   wt->Run (NULL);
00192   delete wt;
00193 
00194   return 0;
00195 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends