You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.5KB

  1. //
  2. // "$Id: Fl_Cairo.cxx 8198 2011-01-06 10:24:58Z manolo $"
  3. //
  4. // Main header file for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-2010 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems on the following page:
  24. //
  25. // http://www.fltk.org/str.php
  26. //
  27. #include <config.h>
  28. #ifdef FLTK_HAVE_CAIRO
  29. #include <FL/Fl.H>
  30. #include <FL/x.H>
  31. #include <FL/Fl_Window.H>
  32. #ifdef __APPLE__
  33. #include <Carbon/Carbon.h>
  34. #endif
  35. // static Fl module initialization :
  36. Fl_Cairo_State Fl::cairo_state_; ///< contains all necesary info for current cairo context mapping
  37. // Fl cairo features implementation
  38. // Fl_Cairo_State class impl
  39. void Fl_Cairo_State::autolink(bool b) {
  40. #ifdef FLTK_USE_CAIRO
  41. autolink_ = b;
  42. #else
  43. Fl::fatal("In Fl::autolink(bool) : Cairo autolink() feature is only "
  44. "available with the enable-cairoext configure option, now quitting.");
  45. #endif
  46. }
  47. /**
  48. Provides a corresponding cairo context for window \a wi.
  49. This is needed in a draw() override if Fl::cairo_autolink_context()
  50. returns false, which is the default.
  51. The cairo_context() does not need to be freed as it is freed every time
  52. a new cairo context is created. When the program terminates,
  53. a call to Fl::cairo_make_current(0) will destroy any residual context.
  54. \note A new cairo context is not always re-created when this method
  55. is used. In particular, if the current graphical context and the current
  56. window didn't change between two calls, the previous gc is internally kept,
  57. thus optimizing the drawing performances.
  58. Also, after this call, Fl::cairo_gc() is adequately updated with this
  59. cairo context.
  60. \note Only available when configure has the --enable-cairo option
  61. \return the valid cairo_t* cairo context associated to this window.
  62. */
  63. cairo_t * Fl::cairo_make_current(Fl_Window* wi) {
  64. if (!wi) return NULL; // Precondition
  65. if (fl_gc==0) { // means remove current cc
  66. Fl::cairo_cc(0); // destroy any previous cc
  67. cairo_state_.window(0);
  68. return 0;
  69. }
  70. // don't re-create a context if it's the same gc/window couple
  71. if (fl_gc==Fl::cairo_state_.gc() && fl_xid(wi) == (Window) Fl::cairo_state_.window())
  72. return Fl::cairo_cc();
  73. cairo_state_.window(wi);
  74. #if defined(USE_X11)
  75. return Fl::cairo_make_current(0, wi->w(), wi->h());
  76. #else
  77. return Fl::cairo_make_current(fl_gc, wi->w(), wi->h());
  78. #endif
  79. }
  80. /*
  81. Creates transparently a cairo_surface_t object.
  82. gc is an HDC context in WIN32, a CGContext* in Quartz, a display on X11
  83. */
  84. static cairo_surface_t * cairo_create_surface(void * gc, int W, int H) {
  85. # if defined(USE_X11)
  86. return cairo_xlib_surface_create(fl_display, fl_window, fl_visual->visual, W, H);
  87. # elif defined(WIN32)
  88. return cairo_win32_surface_create((HDC) gc);
  89. # elif defined(__APPLE_QUARTZ__)
  90. return cairo_quartz_surface_create_for_cg_context((CGContext*) gc, W, H);
  91. # else
  92. # error Cairo is not supported under this platform.
  93. # endif
  94. }
  95. /**
  96. Creates a cairo context from a \a gc only, get its window size or offscreen size if fl_window is null.
  97. \note Only available when configure has the --enable-cairo option
  98. */
  99. cairo_t * Fl::cairo_make_current(void *gc) {
  100. int W=0,H=0;
  101. #if defined(USE_X11)
  102. //FIXME X11 get W,H
  103. // gc will be the window handle here
  104. # warning FIXME get W,H for cairo_make_current(void*)
  105. #elif defined(__APPLE_QUARTZ__)
  106. if (fl_window) {
  107. Rect portRect;
  108. GetPortBounds(GetWindowPort( Fl_X::i(Fl_Window::current())->window_ref() ), &portRect);
  109. W = portRect.right-portRect.left;
  110. H = portRect.bottom-portRect.top;
  111. }
  112. else {
  113. W = CGBitmapContextGetHeight(fl_gc);
  114. H = CGBitmapContextGetHeight(fl_gc);
  115. }
  116. #elif defined(WIN32)
  117. // we don't need any W,H for WIN32
  118. #else
  119. # error Cairo is not supported under this platform.
  120. #endif
  121. if (!gc) {
  122. Fl::cairo_cc(0);
  123. cairo_state_.gc(0); // keep track for next time
  124. return 0;
  125. }
  126. if (gc==Fl::cairo_state_.gc() && fl_window== (Window) Fl::cairo_state_.window())
  127. return Fl::cairo_cc();
  128. cairo_state_.gc(fl_gc); // keep track for next time
  129. cairo_surface_t * s = cairo_create_surface(gc, W, H);
  130. cairo_t * c = cairo_create(s);
  131. cairo_surface_destroy(s);
  132. Fl::cairo_cc(c);
  133. return c;
  134. }
  135. /**
  136. Creates a cairo context from a \a gc and its size
  137. \note Only available when configure has the --enable-cairo option
  138. */
  139. cairo_t * Fl::cairo_make_current(void *gc, int W, int H) {
  140. cairo_surface_t * s = cairo_create_surface(gc, W, H);
  141. cairo_t * c = cairo_create(s);
  142. cairo_surface_destroy(s);
  143. Fl::cairo_cc(c);
  144. return c;
  145. }
  146. #else
  147. // just don't leave the libfltk_cairo lib empty to avoid warnings
  148. #include <FL/Fl_Export.H>
  149. FL_EXPORT int fltk_cairo_dummy() { return 1;}
  150. #endif // FLTK_HAVE_CAIRO
  151. //
  152. // End of "$Id: Fl_Cairo.cxx 8198 2011-01-06 10:24:58Z manolo $" .
  153. //