13
0

FittedCanvas: a GtkCanvas wrapper that follows ui-scale and ignores child size requests

This commit is contained in:
Ben Loftis 2021-12-04 08:04:36 -06:00
parent dc826c6521
commit ab8a2cb4d0
3 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,85 @@
/*
Copyright (C) 2019 Paul Davis
Author: Ben Loftis <ben@harrisonconsoles.com>
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.
*/
#include <algorithm>
#include <cairomm/context.h>
#include <pangomm/layout.h>
#include "pbd/compose.h"
#include "gtkmm2ext/keyboard.h"
#include "gtkmm2ext/utils.h"
#include "canvas/canvas.h"
#include "canvas/debug.h"
#include "canvas/utils.h"
#include "actions.h"
#include "gui_thread.h"
#include "utils.h"
#include "timers.h"
#include "fitted_canvas_widget.h"
using namespace std;
using namespace ArdourCanvas;
using namespace Gtk;
using namespace Gtkmm2ext;
using namespace ARDOUR_UI_UTILS;
//a gtk widget with fixed-size semantics
FittedCanvasWidget::FittedCanvasWidget (float w, float h, bool follow_scale)
{
_nominal_width = w;
_nominal_height = h;
_follow_scale = follow_scale;
//our rendering speed suffers if we re-render knobs simply because they are in-between 2 meters that got invalidated (for example)
// set_single_exposure(false);
//#ifdef __APPLE__
// use_intermediate_surface (false);
//#endif
}
void
FittedCanvasWidget::on_size_request (Gtk::Requisition* req)
{
const double scale = _follow_scale ? UIConfiguration::instance().get_ui_scale() : 1;
if (_nominal_width>0) {
req->width = _nominal_width*scale;
}
if (_nominal_height>0) {
req->height = _nominal_height*scale;
}
}
void
FittedCanvasWidget::on_size_allocate (Gtk::Allocation& alloc)
{
GtkCanvas::on_size_allocate(alloc);
if (!_root.items().empty()) {
ArdourCanvas::Item *fitted = *_root.items().begin();
fitted->size_allocate(ArdourCanvas::Rect(0,0,alloc.get_width(), alloc.get_height()));
}
}

View File

@ -0,0 +1,67 @@
/*
Copyright (C) 2021 Paul Davis
Author: Ben Loftis <ben@harrisonconsoles.com>
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 __fitted_canvas_widget__
#define __fitted_canvas_widget__
#include <string>
#include <vector>
#include <pangomm/fontdescription.h>
#include <canvas/canvas.h>
#include <canvas/container.h>
#include <canvas/item.h>
#include <canvas/fill.h>
#include <canvas/outline.h>
#include <canvas/rectangle.h>
#include <gtkmm/layout.h>
#include <ardour/route.h>
#include <ardour/plugin.h>
#include <ardour/plugin_insert.h>
#include <ardour/automation_control.h>
#include "route_ui.h"
/* FittedCanvasWidget has these properties:
* it is provided a 'nominal size' on construction, which it will request of gtk
* if asked, will resize itself when the user gui/font scale changes
* it 'fits' the Item that was first attached to Root (presumably the top-level widget or container)
* the fitted Item will be explicitly resized to fit when the canvas size is allocated
* the fitted Item may be a container; it should allocate child positions during size_allocate()
* */
class FittedCanvasWidget : public ArdourCanvas::GtkCanvas
{
public:
/* per gtk convention you may use -1 for width OR height if you don't care about that dimension */
FittedCanvasWidget(float nominal_width, float nominal_height, bool follow_scale=true);
void on_size_request (Gtk::Requisition* req); //always returns the nominal size, regardless of children
void on_size_allocate (Gtk::Allocation& alloc);
private:
float _nominal_width;
float _nominal_height;
bool _follow_scale;
};
#endif //__fitted_canvas_widget__

View File

@ -120,6 +120,7 @@ gtk2_ardour_sources = [
'export_preset_selector.cc',
'export_report.cc',
'export_timespan_selector.cc',
'fitted_canvas_widget.cc',
'fft.cc',
'fft_graph.cc',
'fft_result.cc',