nux-1.16.0
cairo_wrapper.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/CairoWrapper.h"
00021 #include "Nux/VLayout.h"
00022 #include "Nux/WindowThread.h"
00023 #include "NuxGraphics/GraphicsEngine.h"
00024 #include "Nux/TimerProc.h"
00025 
00026 nux::CairoWrapper* g_canvas  = NULL;
00027 nux::TimerFunctor* g_timer   = NULL;
00028 nux::TimerHandle   g_handler = NULL;
00029 
00030 void
00031 DrawRoundedRectangle (cairo_t* cr,
00032                       double   aspect,
00033                       double   x,
00034                       double   y,
00035                       double   cornerRadius,
00036                       double   width,
00037                       double   height)
00038 {
00039   double radius = cornerRadius / aspect;
00040 
00041   // top-left, right of the corner
00042   cairo_move_to (cr, x + radius, y);
00043 
00044   // top-right, left of the corner
00045   cairo_line_to (cr, x + width - radius, y);
00046 
00047   // top-right, below the corner
00048   cairo_arc (cr,
00049              x + width - radius,
00050              y + radius,
00051              radius,
00052              -90.0f * G_PI / 180.0f,
00053              0.0f * G_PI / 180.0f);
00054 
00055   // bottom-right, above the corner
00056   cairo_line_to (cr, x + width, y + height - radius);
00057 
00058   // bottom-right, left of the corner
00059   cairo_arc (cr,
00060              x + width - radius,
00061              y + height - radius,
00062              radius,
00063              0.0f * G_PI / 180.0f,
00064              90.0f * G_PI / 180.0f);
00065 
00066   // bottom-left, right of the corner
00067   cairo_line_to (cr, x + radius, y + height);
00068 
00069   // bottom-left, above the corner
00070   cairo_arc (cr,
00071              x + radius,
00072              y + height - radius,
00073              radius,
00074              90.0f * G_PI / 180.0f,
00075              180.0f * G_PI / 180.0f);
00076 
00077   // top-left, right of the corner
00078   cairo_arc (cr,
00079              x + radius,
00080              y + radius,
00081              radius,
00082              180.0f * G_PI / 180.0f,
00083              270.0f * G_PI / 180.0f);
00084 }
00085 
00086 void
00087 callback (nux::Geometry const& geom, cairo_t* cr)
00088 {
00089   if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
00090     return;
00091 
00092   cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
00093   cairo_paint (cr);
00094   cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
00095   cairo_scale (cr, 1.0, 1.0);
00096   cairo_set_source_rgba (cr, 1.0, 0.5, 0.25, 1.0);
00097   DrawRoundedRectangle (cr, 1.0, 5.0, 5.0, 10.0, 
00098                         (double) geom.width - 10.0,
00099                         (double) geom.height - 10.0);
00100   cairo_fill (cr);
00101 }
00102 
00103 void
00104 terminate (void* data)
00105 {
00106   nux::WindowThread* thread = NUX_STATIC_CAST (nux::WindowThread*, data);
00107   thread->TerminateThread ();
00108 }
00109 
00110 void
00111 example (void* data)
00112 {
00113   nux::Geometry geom = {0, 0, 200, 150};
00114   g_canvas = new nux::CairoWrapper (geom, sigc::ptr_fun (callback));
00115   std::string filename = "/tmp/cairo-wrapper-example.png"; 
00116   g_canvas->DumpToFile (filename);
00117 }
00118 
00119 void ThreadWidgetInit (nux::NThread* thread, void* initData)
00120 {
00121   g_timer = new nux::TimerFunctor ();
00122   g_timer->OnTimerExpired.connect (sigc::ptr_fun (&example));
00123   g_handler = nux::GetTimer().AddTimerHandler (1000,
00124                                                g_timer,
00125                                                nux::GetWindowThread ());
00126 }
00127 
00128 int main (int    argc,
00129           char** argv)
00130 {
00131   nux::NuxInitialize (0);
00132   nux::WindowThread* wt = NULL;
00133 
00134   wt = nux::CreateGUIThread (TEXT ("Cairo-Wrapper Example"),
00135                              400,
00136                              400,
00137                              0,
00138                              &ThreadWidgetInit,
00139                              0);
00140   wt->Run (NULL);
00141   delete wt;
00142   delete g_canvas;
00143   delete g_timer;
00144 
00145   return 0;
00146 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends