Move NSGlView into libgtkmm2ext

This allows to re-use the concept with CairoWidget
This commit is contained in:
Robin Gareus 2017-03-20 04:21:08 +01:00
parent b726255007
commit b5e613d453
7 changed files with 91 additions and 41 deletions

View File

@ -46,7 +46,7 @@
#ifdef __APPLE__
#include <gdk/gdk.h>
#include "canvas/nsglview.h"
#include "gtkmm2ext/nsglview.h"
#endif
using namespace std;
@ -404,7 +404,7 @@ GtkCanvas::GtkCanvas ()
#ifdef ARDOUR_CANVAS_NSVIEW_TAG // patched gdkquartz.h
# ifndef __ppc__ // would need to flip RGBA <> RGBA
_nsglview = nsglview_create (this);
_nsglview = Gtkmm2ext::nsglview_create (this);
# endif
#endif
}
@ -773,7 +773,7 @@ GtkCanvas::on_realize ()
Gtk::EventBox::on_realize();
#ifdef __APPLE__
if (_nsglview) {
nsglview_overlay (_nsglview, get_window()->gobj());
Gtkmm2ext::nsglview_overlay (_nsglview, get_window()->gobj());
}
#endif
}
@ -802,7 +802,7 @@ GtkCanvas::on_size_allocate (Gtk::Allocation& a)
GTK_WIDGET(gobj()),
GTK_WIDGET(get_toplevel()->gobj()),
0, 0, &xx, &yy);
nsglview_resize (_nsglview, xx, yy, a.get_width(), a.get_height());
Gtkmm2ext::nsglview_resize (_nsglview, xx, yy, a.get_width(), a.get_height());
}
#endif
@ -820,7 +820,7 @@ GtkCanvas::on_expose_event (GdkEventExpose* ev)
}
#ifdef __APPLE__
if (_nsglview) {
nsglview_queue_draw (_nsglview, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
Gtkmm2ext::nsglview_queue_draw (_nsglview, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
return true;
}
#endif
@ -878,7 +878,7 @@ GtkCanvas::on_expose_event (GdkEventExpose* ev)
/* render canvas */
if ( _single_exposure ) {
render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), draw_context);
Canvas::render (Rect (ev->area.x, ev->area.y, ev->area.x + ev->area.width, ev->area.y + ev->area.height), draw_context);
} else {
GdkRectangle* rects;
@ -887,7 +887,7 @@ GtkCanvas::on_expose_event (GdkEventExpose* ev)
gdk_region_get_rectangles (ev->region, &rects, &nrects);
for (gint n = 0; n < nrects; ++n) {
draw_context->set_identity_matrix(); //reset the cairo matrix, just in case someone left it transformed after drawing ( cough )
render (Rect (rects[n].x, rects[n].y, rects[n].x + rects[n].width, rects[n].y + rects[n].height), draw_context);
Canvas::render (Rect (rects[n].x, rects[n].y, rects[n].x + rects[n].width, rects[n].y + rects[n].height), draw_context);
}
g_free (rects);
}

View File

@ -34,8 +34,9 @@
#include "pbd/signals.h"
#include "canvas/visibility.h"
#include "gtkmm2ext/cairo_canvas.h"
#include "canvas/visibility.h"
#include "canvas/root_group.h"
namespace Gtk {
@ -174,7 +175,7 @@ public:
};
/** A canvas which renders onto a GTK EventBox */
class LIBCANVAS_API GtkCanvas : public Canvas, public Gtk::EventBox
class LIBCANVAS_API GtkCanvas : public Canvas, public Gtk::EventBox, public Gtkmm2ext::CairoCanvas
{
public:
GtkCanvas ();
@ -203,6 +204,14 @@ public:
Glib::RefPtr<Pango::Context> get_pango_context();
void render (Cairo::RefPtr<Cairo::Context> const & ctx, cairo_rectangle_t* r)
{
ArdourCanvas::Rect rect (r->x, r->y, r->width + r->x, r->height + r->y);
Canvas::render (rect, ctx);
}
uint32_t background_color() { return Canvas::background_color (); }
protected:
void on_size_allocate (Gtk::Allocation&);
bool on_scroll_event (GdkEventScroll *);

View File

@ -3,7 +3,6 @@ from waflib.extras import autowaf as autowaf
from waflib import Options
from waflib import TaskGen
import os
import sys
# Version of this package (even if built as a child)
MAJOR = '0'
@ -97,9 +96,6 @@ def build(bld):
obj.install_path = bld.env['LIBDIR']
obj.defines += [ 'PACKAGE="' + I18N_PACKAGE + '"' ]
if sys.platform == 'darwin':
obj.source += [ 'nsglview.mm']
# canvas unit-tests are outdated
if False and bld.env['BUILD_TESTS'] and bld.is_defined('HAVE_CPPUNIT'):
unit_testobj = bld(features = 'cxx cxxprogram')

View File

@ -0,0 +1,38 @@
/*
Copyright (C) 2009 Paul Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __gtk2_ardour_cairo_canvas_h__
#define __gtk2_ardour_cairo_canvas_h__
#include <cairomm/context.h>
#include "gtkmm2ext/visibility.h"
namespace Gtkmm2ext {
class LIBGTKMM2EXT_API CairoCanvas
{
public:
virtual ~CairoCanvas () {}
virtual void render (Cairo::RefPtr<Cairo::Context> const&, cairo_rectangle_t*) = 0;
virtual uint32_t background_color () = 0;
};
} /* namespace */
#endif

View File

@ -3,11 +3,11 @@
#include <gdk/gdk.h>
namespace ArdourCanvas
namespace Gtkmm2ext
{
class GtkCanvas;
class CairoCanvas;
void* nsglview_create (GtkCanvas*);
void* nsglview_create (CairoCanvas*);
void nsglview_overlay (void*, GdkWindow*);
void nsglview_resize (void*, int x, int y, int w, int h);
void nsglview_queue_draw (void*, int x, int y, int w, int h);

View File

@ -22,9 +22,9 @@
/* include order matter due to apple defines */
#include <gtkmm/window.h>
#include "canvas/canvas.h"
#include "canvas/utils.h"
#include "canvas/nsglview.h"
#include "gtkmm2ext/cairo_canvas.h"
#include "gtkmm2ext/nsglview.h"
#include "gtkmm2ext/rgb_macros.h"
#include <gdk/gdkquartz.h>
@ -43,14 +43,14 @@ __attribute__ ((visibility ("hidden")))
int _width;
int _height;
Cairo::RefPtr<Cairo::ImageSurface> surf;
ArdourCanvas::GtkCanvas *gtkcanvas;
Gtkmm2ext::CairoCanvas *cairo_canvas;
}
@property (readwrite) NSInteger tag;
- (id) initWithFrame:(NSRect)frame;
- (void) dealloc;
- (void) setArdourCanvas:(ArdourCanvas::GtkCanvas*)c;
- (void) setCairoCanvas:(Gtkmm2ext::CairoCanvas*)c;
- (void) reshape;
- (void) drawRect:(NSRect)rect;
- (BOOL) canBecomeKeyWindow:(id)sender;
@ -113,9 +113,9 @@ __attribute__ ((visibility ("hidden")))
[super dealloc];
}
- (void) setArdourCanvas:(ArdourCanvas::GtkCanvas*)c
- (void) setCairoCanvas:(Gtkmm2ext::CairoCanvas*)c
{
gtkcanvas = c;
cairo_canvas = c;
}
- (BOOL) canBecomeKeyWindow:(id)sender{
@ -172,18 +172,21 @@ __attribute__ ((visibility ("hidden")))
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
/* call back into GtkCanvas */
/* call back into CairoCanvas */
cairo_rectangle_t cairo_rect;
ArdourCanvas::Rect crect (rect.origin.x, rect.origin.y,
rect.size.width + rect.origin.x,
rect.size.height + rect.origin.y);
cairo_rect.x = rect.origin.x;
cairo_rect.y = rect.origin.y;
cairo_rect.width = rect.size.width;
cairo_rect.height = rect.size.height;
if (!surf || surf->get_width () != _width || surf->get_height() != _height) {
surf = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
crect.x0 = crect.y0 = 0;
crect.x1 = _width;
crect.y1 = _height;
cairo_rect.x = 0;
cairo_rect.y = 0;
cairo_rect.width = _width;
cairo_rect.height = _height;
}
Cairo::RefPtr<Cairo::Context> ctx = Cairo::Context::create (surf);
@ -191,13 +194,18 @@ __attribute__ ((visibility ("hidden")))
// TODO: check retina screen, scaling factor.
// cairo_surface_get_device_scale () or explicit scale
ctx->rectangle (crect.x0, crect.y0, crect.width(), crect.height());
ctx->rectangle (cairo_rect.x, cairo_rect.y, cairo_rect.width, cairo_rect.height);
ctx->clip_preserve ();
/* draw background color */
ArdourCanvas::set_source_rgba (ctx, gtkcanvas->background_color ());
{
/* draw background color */
uint32_t col = cairo_canvas->background_color ();
int r, g, b, a;
UINT_TO_RGBA (col, &r, &g, &b, &a);
ctx->set_source_rgba (r/255.0, g/255.0, b/255.0, a/255.0);
}
ctx->fill ();
gtkcanvas->render (crect, ctx);
cairo_canvas->render (ctx, &cairo_rect);
surf->flush ();
uint8_t* imgdata = surf->get_data ();
@ -239,23 +247,22 @@ __attribute__ ((visibility ("hidden")))
glSwapAPPLE();
[NSOpenGLContext clearCurrentContext];
}
@end
void*
ArdourCanvas::nsglview_create (GtkCanvas* canvas)
Gtkmm2ext::nsglview_create (Gtkmm2ext::CairoCanvas* canvas)
{
ArdourCanvasOpenGLView* gl_view = [ArdourCanvasOpenGLView new];
if (!gl_view) {
return 0;
}
[gl_view setArdourCanvas:canvas];
[gl_view setCairoCanvas:canvas];
return gl_view;
}
void
ArdourCanvas::nsglview_overlay (void* glv, GdkWindow* window)
Gtkmm2ext::nsglview_overlay (void* glv, GdkWindow* window)
{
ArdourCanvasOpenGLView* gl_view = (ArdourCanvasOpenGLView*) glv;
NSView* view = gdk_quartz_window_get_nsview (window);
@ -263,14 +270,14 @@ ArdourCanvas::nsglview_overlay (void* glv, GdkWindow* window)
}
void
ArdourCanvas::nsglview_resize (void* glv, int x, int y, int w, int h)
Gtkmm2ext::nsglview_resize (void* glv, int x, int y, int w, int h)
{
ArdourCanvasOpenGLView* gl_view = (ArdourCanvasOpenGLView*) glv;
[gl_view setFrame:NSMakeRect(x, y, w, h)];
}
void
ArdourCanvas::nsglview_queue_draw (void* glv, int x, int y, int w, int h)
Gtkmm2ext::nsglview_queue_draw (void* glv, int x, int y, int w, int h)
{
ArdourCanvasOpenGLView* gl_view = (ArdourCanvasOpenGLView*) glv;
[gl_view setNeedsDisplayInRect:NSMakeRect(x, y, w, h)];

View File

@ -118,7 +118,7 @@ def build(bld):
'LOCALEDIR="' + os.path.join(
os.path.normpath(bld.env['DATADIR']), 'locale') + '"']
if sys.platform == 'darwin':
obj.source += ['gtkapplication_quartz.mm']
obj.source += ['gtkapplication_quartz.mm', 'nsglview.mm']
else:
obj.source += ['gtkapplication_x11.c']