diff options
| author | Alexander Valavanis <valavanisalex@gmail.com> | 2019-01-01 15:40:49 +0000 |
|---|---|---|
| committer | Alexander Valavanis <valavanisalex@gmail.com> | 2019-01-01 15:40:49 +0000 |
| commit | e34227a4737093107ccd6b4f0a9e221861059adc (patch) | |
| tree | c636ec981c1fb5991a87e0c878663256f0e3bd03 /src/ui | |
| parent | C++ify EekPreview (diff) | |
| download | inkscape-e34227a4737093107ccd6b4f0a9e221861059adc.tar.gz inkscape-e34227a4737093107ccd6b4f0a9e221861059adc.zip | |
Move Preview to Inkscape namespace
Diffstat (limited to 'src/ui')
| -rw-r--r-- | src/ui/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/ui/dialog/color-item.cpp | 39 | ||||
| -rw-r--r-- | src/ui/dialog/color-item.h | 12 | ||||
| -rw-r--r-- | src/ui/dialog/swatches.cpp | 44 | ||||
| -rw-r--r-- | src/ui/previewable.h | 9 | ||||
| -rw-r--r-- | src/ui/previewholder.cpp | 42 | ||||
| -rw-r--r-- | src/ui/previewholder.h | 18 | ||||
| -rw-r--r-- | src/ui/widget/panel.cpp | 4 | ||||
| -rw-r--r-- | src/ui/widget/preview.cpp | 503 | ||||
| -rw-r--r-- | src/ui/widget/preview.h | 163 |
10 files changed, 763 insertions, 73 deletions
diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index a0a540970..80c07f8b9 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -186,6 +186,7 @@ set(ui_SRC widget/panel.cpp widget/point.cpp widget/preferences-widget.cpp + widget/preview.cpp widget/random.cpp widget/registered-widget.cpp widget/registry.cpp @@ -412,6 +413,7 @@ set(ui_SRC widget/panel.h widget/point.h widget/preferences-widget.h + widget/preview.h widget/random.h widget/registered-enums.h widget/registered-widget.h diff --git a/src/ui/dialog/color-item.cpp b/src/ui/dialog/color-item.cpp index d5d254e5d..ad355f8d2 100644 --- a/src/ui/dialog/color-item.cpp +++ b/src/ui/dialog/color-item.cpp @@ -178,7 +178,7 @@ static bool popVal( guint64& numVal, std::string& str ) } // TODO resolve this more cleanly: -extern bool colorItemHandleButtonPress(GdkEventButton* event, EekPreview *preview, gpointer user_data); +extern bool colorItemHandleButtonPress(GdkEventButton* event, UI::Widget::Preview *preview, gpointer user_data); void ColorItem::drag_begin(const Glib::RefPtr<Gdk::DragContext> &dc) @@ -307,18 +307,18 @@ void ColorItem::setState( bool fill, bool stroke ) _isStroke = stroke; for ( auto widget : _previews ) { - auto preview = dynamic_cast<EekPreview *>(widget); + auto preview = dynamic_cast<UI::Widget::Preview *>(widget); if (preview) { int val = preview->get_linked(); - val &= ~(PREVIEW_FILL | PREVIEW_STROKE); + val &= ~(UI::Widget::PREVIEW_FILL | UI::Widget::PREVIEW_STROKE); if ( _isFill ) { - val |= PREVIEW_FILL; + val |= UI::Widget::PREVIEW_FILL; } if ( _isStroke ) { - val |= PREVIEW_STROKE; + val |= UI::Widget::PREVIEW_STROKE; } - preview->set_linked(static_cast<LinkType>(val)); + preview->set_linked(static_cast<UI::Widget::LinkType>(val)); } } } @@ -339,7 +339,7 @@ void ColorItem::setName(const Glib::ustring name) //def.descr = name; for (auto widget : _previews) { - auto preview = dynamic_cast<EekPreview *>(widget); + auto preview = dynamic_cast<UI::Widget::Preview *>(widget); auto label = dynamic_cast<Gtk::Label *>(widget); if (preview) { preview->set_tooltip_text(name); @@ -409,7 +409,7 @@ void ColorItem::_colorDefChanged(void* data) void ColorItem::_updatePreviews() { for (auto widget : _previews) { - auto preview = dynamic_cast<EekPreview *>(widget); + auto preview = dynamic_cast<UI::Widget::Preview *>(widget); if (preview) { _regenPreview(preview); preview->queue_draw(); @@ -480,7 +480,7 @@ void ColorItem::_updatePreviews() } -void ColorItem::_regenPreview(EekPreview * preview) +void ColorItem::_regenPreview(UI::Widget::Preview * preview) { if ( def.getType() != ege::PaintDef::RGB ) { using Inkscape::IO::Resource::get_path; @@ -521,27 +521,32 @@ void ColorItem::_regenPreview(EekPreview * preview) preview->set_pixbuf(pixbuf); } - preview->set_linked((LinkType)((_linkSrc ? PREVIEW_LINK_IN:0) - | (_listeners.empty() ? 0:PREVIEW_LINK_OUT) - | (_isLive ? PREVIEW_LINK_OTHER:0)) ); + preview->set_linked(static_cast<UI::Widget::LinkType>( (_linkSrc ? UI::Widget::PREVIEW_LINK_IN : 0) + | (_listeners.empty() ? 0 : UI::Widget::PREVIEW_LINK_OUT) + | (_isLive ? UI::Widget::PREVIEW_LINK_OTHER:0)) ); } -Gtk::Widget* ColorItem::getPreview(PreviewStyle style, ViewType view, ::PreviewSize size, guint ratio, guint border) +Gtk::Widget* +ColorItem::getPreview(UI::Widget::PreviewStyle style, + UI::Widget::ViewType view, + UI::Widget::PreviewSize size, + guint ratio, + guint border) { Gtk::Widget* widget = nullptr; - if ( style == PREVIEW_STYLE_BLURB) { + if ( style == UI::Widget::PREVIEW_STYLE_BLURB) { Gtk::Label *lbl = new Gtk::Label(def.descr); lbl->set_halign(Gtk::ALIGN_START); lbl->set_valign(Gtk::ALIGN_CENTER); widget = lbl; } else { - auto preview = Gtk::manage(new EekPreview()); + auto preview = Gtk::manage(new UI::Widget::Preview()); preview->set_name("ColorItemPreview"); _regenPreview(preview); - preview->set_details((::ViewType)view, - (::PreviewSize)size, + preview->set_details((UI::Widget::ViewType)view, + (UI::Widget::PreviewSize)size, ratio, border ); diff --git a/src/ui/dialog/color-item.h b/src/ui/dialog/color-item.h index 47a694bae..7210ed292 100644 --- a/src/ui/dialog/color-item.h +++ b/src/ui/dialog/color-item.h @@ -51,11 +51,11 @@ public: ~ColorItem() override; ColorItem(ColorItem const &other); virtual ColorItem &operator=(ColorItem const &other); - Gtk::Widget* getPreview(PreviewStyle style, - ViewType view, - ::PreviewSize size, - guint ratio, - guint border) override; + Gtk::Widget* getPreview(UI::Widget::PreviewStyle style, + UI::Widget::ViewType view, + UI::Widget::PreviewSize size, + guint ratio, + guint border) override; void buttonClicked(bool secondary = false); void setGradient(SPGradient *grad); @@ -88,7 +88,7 @@ private: static void _colorDefChanged(void* data); void _updatePreviews(); - void _regenPreview(EekPreview * preview); + void _regenPreview(UI::Widget::Preview * preview); void _linkTint( ColorItem& other, int percent ); void _linkTone( ColorItem& other, int percent, int grayLevel ); diff --git a/src/ui/dialog/swatches.cpp b/src/ui/dialog/swatches.cpp index 7e348d18b..fabbf720b 100644 --- a/src/ui/dialog/swatches.cpp +++ b/src/ui/dialog/swatches.cpp @@ -248,7 +248,7 @@ static void removeit( GtkWidget *widget, gpointer data ) } /* extern'ed from color-item.cpp */ -bool colorItemHandleButtonPress(GdkEventButton* event, EekPreview *preview, gpointer user_data) +bool colorItemHandleButtonPress(GdkEventButton* event, UI::Widget::Preview *preview, gpointer user_data) { gboolean handled = FALSE; @@ -695,10 +695,10 @@ void SwatchesPanel::_build_menu() if (!_prefs_path.empty()) { Inkscape::Preferences *prefs = Inkscape::Preferences::get(); panel_wrap = prefs->getBool(_prefs_path + "/panel_wrap"); - panel_size = prefs->getIntLimited(_prefs_path + "/panel_size", 1, 0, PREVIEW_SIZE_HUGE); + panel_size = prefs->getIntLimited(_prefs_path + "/panel_size", 1, 0, UI::Widget::PREVIEW_SIZE_HUGE); panel_mode = prefs->getIntLimited(_prefs_path + "/panel_mode", 1, 0, 10); panel_ratio = prefs->getIntLimited(_prefs_path + "/panel_ratio", 100, 0, 500 ); - panel_border = prefs->getIntLimited(_prefs_path + "/panel_border", BORDER_NONE, 0, 2 ); + panel_border = prefs->getIntLimited(_prefs_path + "/panel_border", UI::Widget::BORDER_NONE, 0, 2 ); } _menu = new Gtk::Menu(); @@ -890,25 +890,25 @@ void SwatchesPanel::_updateSettings(int settings, int value) case SWATCHES_SETTINGS_SIZE: { prefs->setInt(_prefs_path + "/panel_size", value); - ViewType curr_type = _holder->getPreviewType(); + auto curr_type = _holder->getPreviewType(); guint curr_ratio = _holder->getPreviewRatio(); - ::BorderStyle curr_border = _holder->getPreviewBorder(); + auto curr_border = _holder->getPreviewBorder(); switch (value) { case 0: - _holder->setStyle(::PREVIEW_SIZE_TINY, curr_type, curr_ratio, curr_border); + _holder->setStyle(UI::Widget::PREVIEW_SIZE_TINY, curr_type, curr_ratio, curr_border); break; case 1: - _holder->setStyle(::PREVIEW_SIZE_SMALL, curr_type, curr_ratio, curr_border); + _holder->setStyle(UI::Widget::PREVIEW_SIZE_SMALL, curr_type, curr_ratio, curr_border); break; case 2: - _holder->setStyle(::PREVIEW_SIZE_MEDIUM, curr_type, curr_ratio, curr_border); + _holder->setStyle(UI::Widget::PREVIEW_SIZE_MEDIUM, curr_type, curr_ratio, curr_border); break; case 3: - _holder->setStyle(::PREVIEW_SIZE_BIG, curr_type, curr_ratio, curr_border); + _holder->setStyle(UI::Widget::PREVIEW_SIZE_BIG, curr_type, curr_ratio, curr_border); break; case 4: - _holder->setStyle(::PREVIEW_SIZE_HUGE, curr_type, curr_ratio, curr_border); + _holder->setStyle(UI::Widget::PREVIEW_SIZE_HUGE, curr_type, curr_ratio, curr_border); break; default: break; @@ -919,15 +919,15 @@ void SwatchesPanel::_updateSettings(int settings, int value) case SWATCHES_SETTINGS_MODE: { prefs->setInt(_prefs_path + "/panel_mode", value); - ::PreviewSize curr_size = _holder->getPreviewSize(); + auto curr_size = _holder->getPreviewSize(); guint curr_ratio = _holder->getPreviewRatio(); - ::BorderStyle curr_border = _holder->getPreviewBorder(); + auto curr_border = _holder->getPreviewBorder(); switch (value) { case 0: - _holder->setStyle(curr_size, VIEW_TYPE_LIST, curr_ratio, curr_border); + _holder->setStyle(curr_size, UI::Widget::VIEW_TYPE_LIST, curr_ratio, curr_border); break; case 1: - _holder->setStyle(curr_size, VIEW_TYPE_GRID, curr_ratio, curr_border); + _holder->setStyle(curr_size, UI::Widget::VIEW_TYPE_GRID, curr_ratio, curr_border); break; default: break; @@ -937,9 +937,9 @@ void SwatchesPanel::_updateSettings(int settings, int value) case SWATCHES_SETTINGS_SHAPE: { prefs->setInt(_prefs_path + "/panel_ratio", value); - ViewType curr_type = _holder->getPreviewType(); - ::PreviewSize curr_size = _holder->getPreviewSize(); - ::BorderStyle curr_border = _holder->getPreviewBorder(); + auto curr_type = _holder->getPreviewType(); + auto curr_size = _holder->getPreviewSize(); + auto curr_border = _holder->getPreviewBorder(); _holder->setStyle(curr_size, curr_type, value, curr_border); break; @@ -947,19 +947,19 @@ void SwatchesPanel::_updateSettings(int settings, int value) case SWATCHES_SETTINGS_BORDER: { prefs->setInt(_prefs_path + "/panel_border", value); - ::PreviewSize curr_size = _holder->getPreviewSize(); - ViewType curr_type = _holder->getPreviewType(); + auto curr_size = _holder->getPreviewSize(); + auto curr_type = _holder->getPreviewType(); guint curr_ratio = _holder->getPreviewRatio(); switch (value) { case 0: - _holder->setStyle(curr_size, curr_type, curr_ratio, BORDER_NONE); + _holder->setStyle(curr_size, curr_type, curr_ratio, UI::Widget::BORDER_NONE); break; case 1: - _holder->setStyle(curr_size, curr_type, curr_ratio, BORDER_SOLID); + _holder->setStyle(curr_size, curr_type, curr_ratio, UI::Widget::BORDER_SOLID); break; case 2: - _holder->setStyle(curr_size, curr_type, curr_ratio, BORDER_WIDE); + _holder->setStyle(curr_size, curr_type, curr_ratio, UI::Widget::BORDER_WIDE); break; default: break; diff --git a/src/ui/previewable.h b/src/ui/previewable.h index 76be394bb..6c611a668 100644 --- a/src/ui/previewable.h +++ b/src/ui/previewable.h @@ -15,7 +15,8 @@ #include <gtkmm/widget.h> -#include "../widgets/eek-preview.h" + +#include "widget/preview.h" namespace Inkscape { namespace UI { @@ -42,7 +43,11 @@ class Previewable public: // TODO need to add some nice parameters virtual ~Previewable() = default; - virtual Gtk::Widget* getPreview( PreviewStyle style, ViewType view, ::PreviewSize size, guint ratio, guint border) = 0; + virtual Gtk::Widget* getPreview(UI::Widget::PreviewStyle style, + UI::Widget::ViewType view, + UI::Widget::PreviewSize size, + guint ratio, + guint border) = 0; }; diff --git a/src/ui/previewholder.cpp b/src/ui/previewholder.cpp index 236612ab3..33ac6f062 100644 --- a/src/ui/previewholder.cpp +++ b/src/ui/previewholder.cpp @@ -37,11 +37,11 @@ PreviewHolder::PreviewHolder() : _prefCols(0), _updatesFrozen(false), _anchor(SP_ANCHOR_CENTER), - _baseSize(PREVIEW_SIZE_SMALL), + _baseSize(UI::Widget::PREVIEW_SIZE_SMALL), _ratio(100), - _view(VIEW_TYPE_LIST), + _view(UI::Widget::VIEW_TYPE_LIST), _wrap(false), - _border(BORDER_NONE) + _border(UI::Widget::BORDER_NONE) { set_name( "PreviewHolder" ); _scroller = Gtk::manage(new Gtk::ScrolledWindow()); @@ -102,7 +102,7 @@ void PreviewHolder::clear() items.clear(); _prefCols = 0; // Kludge to restore scrollbars - if ( !_wrap && (_view != VIEW_TYPE_LIST) && (_anchor == SP_ANCHOR_NORTH || _anchor == SP_ANCHOR_SOUTH) ) { + if ( !_wrap && (_view != UI::Widget::VIEW_TYPE_LIST) && (_anchor == SP_ANCHOR_NORTH || _anchor == SP_ANCHOR_SOUTH) ) { _scroller->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_NEVER ); } rebuildUI(); @@ -123,8 +123,12 @@ void PreviewHolder::addPreview( Previewable* preview ) switch(_view) { case VIEW_TYPE_LIST: { - Gtk::Widget* label = Gtk::manage(preview->getPreview(PREVIEW_STYLE_BLURB, VIEW_TYPE_LIST, _baseSize, _ratio, _border)); - Gtk::Widget* item = Gtk::manage(preview->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_LIST, _baseSize, _ratio, _border)); + Gtk::Widget* label = Gtk::manage(preview->getPreview(UI::Widget::PREVIEW_STYLE_BLURB, + UI::Widget::VIEW_TYPE_LIST, + _baseSize, _ratio, _border)); + Gtk::Widget* item = Gtk::manage(preview->getPreview(UI::Widget::PREVIEW_STYLE_PREVIEW, + UI::Widget::VIEW_TYPE_LIST, + _baseSize, _ratio, _border)); item->set_hexpand(); item->set_vexpand(); @@ -138,7 +142,9 @@ void PreviewHolder::addPreview( Previewable* preview ) break; case VIEW_TYPE_GRID: { - Gtk::Widget* item = Gtk::manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, VIEW_TYPE_GRID, _baseSize, _ratio, _border)); + Gtk::Widget* item = Gtk::manage(items[i]->getPreview(UI::Widget::PREVIEW_STYLE_PREVIEW, + UI::Widget::VIEW_TYPE_GRID, + _baseSize, _ratio, _border)); int ncols = 1; int nrows = 1; @@ -191,7 +197,11 @@ void PreviewHolder::thawUpdates() rebuildUI(); } -void PreviewHolder::setStyle( ::PreviewSize size, ViewType view, guint ratio, ::BorderStyle border ) +void +PreviewHolder::setStyle(UI::Widget::PreviewSize size, + UI::Widget::ViewType view, + guint ratio, + UI::Widget::BorderStyle border ) { if ( size != _baseSize || view != _view || ratio != _ratio || border != _border ) { _baseSize = size; @@ -199,7 +209,7 @@ void PreviewHolder::setStyle( ::PreviewSize size, ViewType view, guint ratio, :: _ratio = ratio; _border = border; // Kludge to restore scrollbars - if ( !_wrap && (_view != VIEW_TYPE_LIST) && (_anchor == SP_ANCHOR_NORTH || _anchor == SP_ANCHOR_SOUTH) ) { + if ( !_wrap && (_view != UI::Widget::VIEW_TYPE_LIST) && (_anchor == SP_ANCHOR_NORTH || _anchor == SP_ANCHOR_SOUTH) ) { _scroller->set_policy( Gtk::POLICY_AUTOMATIC, Gtk::POLICY_NEVER ); } rebuildUI(); @@ -314,7 +324,7 @@ void PreviewHolder::calcGridSize( const Gtk::Widget* item, int itemCount, int& n } } } else { - ncols = (_baseSize == PREVIEW_SIZE_SMALL || _baseSize == PREVIEW_SIZE_TINY) ? + ncols = (_baseSize == UI::Widget::PREVIEW_SIZE_SMALL || _baseSize == UI::Widget::PREVIEW_SIZE_TINY) ? COLUMNS_FOR_SMALL : COLUMNS_FOR_LARGE; if ( _prefCols > 0 ) { ncols = _prefCols; @@ -335,7 +345,7 @@ void PreviewHolder::rebuildUI() _insides->set_column_spacing(0); _insides->set_row_spacing(0); - if (_border == BORDER_WIDE) { + if (_border == UI::Widget::BORDER_WIDE) { _insides->set_column_spacing(1); _insides->set_row_spacing(1); } @@ -346,10 +356,10 @@ void PreviewHolder::rebuildUI() _insides->set_column_spacing(8); for ( unsigned int i = 0; i < items.size(); i++ ) { - Gtk::Widget* label = Gtk::manage(items[i]->getPreview(PREVIEW_STYLE_BLURB, _view, _baseSize, _ratio, _border)); + Gtk::Widget* label = Gtk::manage(items[i]->getPreview(UI::Widget::PREVIEW_STYLE_BLURB, _view, _baseSize, _ratio, _border)); //label->set_alignment(Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER); - Gtk::Widget* item = Gtk::manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, _view, _baseSize, _ratio, _border)); + Gtk::Widget* item = Gtk::manage(items[i]->getPreview(UI::Widget::PREVIEW_STYLE_PREVIEW, _view, _baseSize, _ratio, _border)); item->set_hexpand(); item->set_vexpand(); @@ -372,10 +382,10 @@ void PreviewHolder::rebuildUI() for ( unsigned int i = 0; i < items.size(); i++ ) { // If this is the last row, flag so the previews can draw a bottom - ::BorderStyle border = ((row == nrows -1) && (_border == BORDER_SOLID)) ? - BORDER_SOLID_LAST_ROW : _border; + UI::Widget::BorderStyle border = ((row == nrows -1) && (_border == UI::Widget::BORDER_SOLID)) ? + UI::Widget::BORDER_SOLID_LAST_ROW : _border; - Gtk::Widget* item = Gtk::manage(items[i]->getPreview(PREVIEW_STYLE_PREVIEW, _view, _baseSize, _ratio, border)); + Gtk::Widget* item = Gtk::manage(items[i]->getPreview(UI::Widget::PREVIEW_STYLE_PREVIEW, _view, _baseSize, _ratio, border)); item->set_hexpand(); item->set_vexpand(); diff --git a/src/ui/previewholder.h b/src/ui/previewholder.h index 325e9aa9f..aa1c39ab5 100644 --- a/src/ui/previewholder.h +++ b/src/ui/previewholder.h @@ -21,7 +21,6 @@ class Grid; class ScrolledWindow; } -#include "../widgets/eek-preview.h" #include "enums.h" namespace Inkscape { @@ -39,14 +38,17 @@ public: virtual void addPreview( Previewable* preview ); virtual void freezeUpdates(); virtual void thawUpdates(); - virtual void setStyle( ::PreviewSize size, ViewType view, guint ratio, ::BorderStyle border ); + virtual void setStyle(UI::Widget::PreviewSize size, + UI::Widget::ViewType view, + guint ratio, + UI::Widget::BorderStyle border); virtual void setOrientation(SPAnchorType how); virtual int getColumnPref() const { return _prefCols; } virtual void setColumnPref( int cols ); - virtual ::PreviewSize getPreviewSize() const { return _baseSize; } - virtual ViewType getPreviewType() const { return _view; } + virtual UI::Widget::PreviewSize getPreviewSize() const { return _baseSize; } + virtual UI::Widget::ViewType getPreviewType() const { return _view; } virtual guint getPreviewRatio() const { return _ratio; } - virtual ::BorderStyle getPreviewBorder() const { return _border; } + virtual UI::Widget::BorderStyle getPreviewBorder() const { return _border; } virtual void setWrap( bool wrap ); virtual bool getWrap() const { return _wrap; } @@ -64,11 +66,11 @@ private: int _prefCols; bool _updatesFrozen; SPAnchorType _anchor; - ::PreviewSize _baseSize; + UI::Widget::PreviewSize _baseSize; guint _ratio; - ViewType _view; + UI::Widget::ViewType _view; bool _wrap; - ::BorderStyle _border; + UI::Widget::BorderStyle _border; }; } //namespace UI diff --git a/src/ui/widget/panel.cpp b/src/ui/widget/panel.cpp index d6e170c74..8ca08a091 100644 --- a/src/ui/widget/panel.cpp +++ b/src/ui/widget/panel.cpp @@ -20,7 +20,7 @@ #include "desktop.h" #include "inkscape.h" -#include "widgets/eek-preview.h" +#include "preview.h" namespace Inkscape { namespace UI { @@ -35,7 +35,7 @@ void Panel::prep() { GTK_ICON_SIZE_DND, // Not used by options, but included to make the last size larger GTK_ICON_SIZE_DIALOG }; - EekPreview::set_size_mappings( G_N_ELEMENTS(sizes), sizes ); + Preview::set_size_mappings( G_N_ELEMENTS(sizes), sizes ); } Panel::Panel(gchar const *prefs_path, int verb_num) : diff --git a/src/ui/widget/preview.cpp b/src/ui/widget/preview.cpp new file mode 100644 index 000000000..f22032b65 --- /dev/null +++ b/src/ui/widget/preview.cpp @@ -0,0 +1,503 @@ +// SPDX-License-Identifier: GPL-2.0-or-later OR MPL-1.1 OR LGPL-2.1-or-later +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Eek Preview Stuffs. + * + * The Initial Developer of the Original Code is + * Jon A. Cruz. + * Portions created by the Initial Developer are Copyright (C) 2005 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include <algorithm> +using std::min; + +#include <gdkmm/general.h> +#include "preview.h" +#include "preferences.h" + +namespace Inkscape { +namespace UI { +namespace Widget { + +#define PRIME_BUTTON_MAGIC_NUMBER 1 + +/* Keep in sync with last value in eek-preview.h */ +#define PREVIEW_SIZE_LAST PREVIEW_SIZE_HUGE +#define PREVIEW_SIZE_NEXTFREE (PREVIEW_SIZE_HUGE + 1) + +#define PREVIEW_MAX_RATIO 500 + +void +Preview::set_color(int r, int g, int b ) +{ + _r = r; + _g = g; + _b = b; + + queue_draw(); +} + + +void +Preview::set_pixbuf(const Glib::RefPtr<Gdk::Pixbuf> &pixbuf) +{ + _previewPixbuf = pixbuf; + + queue_draw(); + + if (_scaled) + { + _scaled.reset(); + } + + _scaledW = _previewPixbuf->get_width(); + _scaledH = _previewPixbuf->get_height(); +} + +static gboolean setupDone = FALSE; +static GtkRequisition sizeThings[PREVIEW_SIZE_NEXTFREE]; + +void +Preview::set_size_mappings( guint count, GtkIconSize const* sizes ) +{ + gint width = 0; + gint height = 0; + gint smallest = 512; + gint largest = 0; + guint i = 0; + guint delta = 0; + + for ( i = 0; i < count; ++i ) { + gboolean worked = gtk_icon_size_lookup( sizes[i], &width, &height ); + if ( worked ) { + if ( width < smallest ) { + smallest = width; + } + if ( width > largest ) { + largest = width; + } + } + } + + smallest = (smallest * 3) / 4; + + delta = largest - smallest; + + for ( i = 0; i < G_N_ELEMENTS(sizeThings); ++i ) { + guint val = smallest + ( (i * delta) / (G_N_ELEMENTS(sizeThings) - 1) ); + sizeThings[i].width = val; + sizeThings[i].height = val; + } + + setupDone = TRUE; +} + +void +Preview::size_request(GtkRequisition* req) const +{ + int width = 0; + int height = 0; + + if ( !setupDone ) { + GtkIconSize sizes[] = { + GTK_ICON_SIZE_MENU, + GTK_ICON_SIZE_SMALL_TOOLBAR, + GTK_ICON_SIZE_LARGE_TOOLBAR, + GTK_ICON_SIZE_BUTTON, + GTK_ICON_SIZE_DIALOG + }; + set_size_mappings( G_N_ELEMENTS(sizes), sizes ); + } + + width = sizeThings[_size].width; + height = sizeThings[_size].height; + + if ( _view == VIEW_TYPE_LIST ) { + width *= 3; + } + + if ( _ratio != 100 ) { + width = (width * _ratio) / 100; + if ( width < 0 ) { + width = 1; + } + } + + req->width = width; + req->height = height; +} + +void +Preview::get_preferred_width_vfunc(int &minimal_width, int &natural_width) const +{ + GtkRequisition requisition; + size_request(&requisition); + minimal_width = natural_width = requisition.width; +} + +void +Preview::get_preferred_height_vfunc(int &minimal_height, int &natural_height) const +{ + GtkRequisition requisition; + size_request(&requisition); + minimal_height = natural_height = requisition.height; +} + +bool +Preview::on_draw(const Cairo::RefPtr<Cairo::Context> &cr) +{ + auto allocation = get_allocation(); + + gint insetTop = 0, insetBottom = 0; + gint insetLeft = 0, insetRight = 0; + + if (_border == BORDER_SOLID) { + insetTop = 1; + insetLeft = 1; + } + if (_border == BORDER_SOLID_LAST_ROW) { + insetTop = insetBottom = 1; + insetLeft = 1; + } + if (_border == BORDER_WIDE) { + insetTop = insetBottom = 1; + insetLeft = insetRight = 1; + } + + auto context = get_style_context(); + + context->render_frame(cr, + 0, 0, + allocation.get_width(), allocation.get_height()); + + context->render_background(cr, + 0, 0, + allocation.get_width(), allocation.get_height()); + + // Border + if (_border != BORDER_NONE) { + cr->set_source_rgb(0.0, 0.0, 0.0); + cr->rectangle(0, 0, allocation.get_width(), allocation.get_height()); + cr->fill(); + } + + cr->set_source_rgb(_r/65535.0, _g/65535.0, _b/65535.0 ); + cr->rectangle(insetLeft, insetTop, allocation.get_width() - (insetLeft + insetRight), allocation.get_height() - (insetTop + insetBottom)); + cr->fill(); + + if (_previewPixbuf ) + { + if ((allocation.get_width() != _scaledW) || (allocation.get_height() != _scaledH)) { + if (_scaled) + { + _scaled.reset(); + } + + _scaledW = allocation.get_width() - (insetLeft + insetRight); + _scaledH = allocation.get_height() - (insetTop + insetBottom); + + _scaled = _previewPixbuf->scale_simple(_scaledW, + _scaledH, + Gdk::INTERP_BILINEAR); + } + + Glib::RefPtr<Gdk::Pixbuf> pix = (_scaled) ? _scaled : _previewPixbuf; + + // Border + if (_border != BORDER_NONE) { + cr->set_source_rgb(0.0, 0.0, 0.0); + cr->rectangle(0, 0, allocation.get_width(), allocation.get_height()); + cr->fill(); + } + + Gdk::Cairo::set_source_pixbuf(cr, pix, insetLeft, insetTop); + cr->paint(); + } + + if (_linked) + { + /* Draw arrow */ + GdkRectangle possible = {insetLeft, + insetTop, + (allocation.get_width() - (insetLeft + insetRight)), + (allocation.get_height() - (insetTop + insetBottom)) + }; + + GdkRectangle area = {possible.x, + possible.y, + possible.width / 2, + possible.height / 2 }; + + /* Make it square */ + if ( area.width > area.height ) + area.width = area.height; + if ( area.height > area.width ) + area.height = area.width; + + /* Center it horizontally */ + if ( area.width < possible.width ) { + int diff = (possible.width - area.width) / 2; + area.x += diff; + } + + if (_linked & PREVIEW_LINK_IN) + { + context->render_arrow(cr, + G_PI, // Down-pointing arrow + area.x, area.y, + min(area.width, area.height) + ); + } + + if (_linked & PREVIEW_LINK_OUT) + { + GdkRectangle otherArea = {area.x, area.y, area.width, area.height}; + if ( otherArea.height < possible.height ) { + otherArea.y = possible.y + (possible.height - otherArea.height); + } + + context->render_arrow(cr, + G_PI, // Down-pointing arrow + otherArea.x, otherArea.y, + min(otherArea.width, otherArea.height) + ); + } + + if (_linked & PREVIEW_LINK_OTHER) + { + GdkRectangle otherArea = {insetLeft, area.y, area.width, area.height}; + if ( otherArea.height < possible.height ) { + otherArea.y = possible.y + (possible.height - otherArea.height) / 2; + } + + context->render_arrow(cr, + 1.5*G_PI, // Left-pointing arrow + otherArea.x, otherArea.y, + min(otherArea.width, otherArea.height) + ); + } + + + if (_linked & PREVIEW_FILL) + { + GdkRectangle otherArea = {possible.x + ((possible.width / 4) - (area.width / 2)), + area.y, + area.width, area.height}; + if ( otherArea.height < possible.height ) { + otherArea.y = possible.y + (possible.height - otherArea.height) / 2; + } + context->render_check(cr, + otherArea.x, otherArea.y, + otherArea.width, otherArea.height ); + } + + if (_linked & PREVIEW_STROKE) + { + GdkRectangle otherArea = {possible.x + (((possible.width * 3) / 4) - (area.width / 2)), + area.y, + area.width, area.height}; + if ( otherArea.height < possible.height ) { + otherArea.y = possible.y + (possible.height - otherArea.height) / 2; + } + // This should be a diamond too? + context->render_check(cr, + otherArea.x, otherArea.y, + otherArea.width, otherArea.height ); + } + } + + + if ( has_focus() ) { + allocation = get_allocation(); + + context->render_focus(cr, + 0 + 1, 0 + 1, + allocation.get_width() - 2, allocation.get_height() - 2 ); + } + + return false; +} + + +bool +Preview::on_enter_notify_event(GdkEventCrossing* event ) +{ + _within = true; + set_state_flags(_hot ? Gtk::STATE_FLAG_ACTIVE : Gtk::STATE_FLAG_PRELIGHT, false); + + return false; +} + +bool +Preview::on_leave_notify_event(GdkEventCrossing* event) +{ + _within = false; + set_state_flags(Gtk::STATE_FLAG_NORMAL, false); + + return false; +} + +bool +Preview::on_button_press_event(GdkEventButton *event) +{ + if (_takesFocus && !has_focus() ) + { + grab_focus(); + } + + if ( event->button == PRIME_BUTTON_MAGIC_NUMBER || + event->button == 2 ) + { + _hot = true; + + if ( _within ) + { + set_state_flags(Gtk::STATE_FLAG_ACTIVE, false); + } + } + + return false; +} + +bool +Preview::on_button_release_event(GdkEventButton* event) +{ + _hot = false; + set_state_flags(Gtk::STATE_FLAG_NORMAL, false); + + if (_within && + (event->button == PRIME_BUTTON_MAGIC_NUMBER || + event->button == 2)) + { + gboolean isAlt = ( ((event->state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK) || + (event->button == 2)); + + if ( isAlt ) + { + _signal_alt_clicked(2); + } + else + { + _signal_clicked.emit(); + } + } + + return false; +} + +void +Preview::set_linked(LinkType link) +{ + link = (LinkType)(link & PREVIEW_LINK_ALL); + + if (link != _linked) + { + _linked = link; + + queue_draw(); + } +} + +LinkType +Preview::get_linked() const +{ + return (LinkType)_linked; +} + +void +Preview::set_details(ViewType view, + PreviewSize size, + guint ratio, + guint border) +{ + _view = view; + + if ( size > PREVIEW_SIZE_LAST ) + { + size = PREVIEW_SIZE_LAST; + } + + _size = size; + + if ( ratio > PREVIEW_MAX_RATIO ) + { + ratio = PREVIEW_MAX_RATIO; + } + + _ratio = ratio; + _border = border; + + queue_draw(); +} + +Preview::Preview() + : _r(0x80), + _g(0x80), + _b(0xcc), + _scaledW(0), + _scaledH(0), + _hot(false), + _within(false), + _takesFocus(false), + _view(VIEW_TYPE_LIST), + _size(PREVIEW_SIZE_SMALL), + _ratio(100), + _border(BORDER_NONE), + _previewPixbuf(nullptr), + _scaled(nullptr) +{ + set_can_focus(true); + set_receives_default(true); + + set_sensitive(true); + + add_events(Gdk::BUTTON_PRESS_MASK + |Gdk::BUTTON_RELEASE_MASK + |Gdk::KEY_PRESS_MASK + |Gdk::KEY_RELEASE_MASK + |Gdk::FOCUS_CHANGE_MASK + |Gdk::ENTER_NOTIFY_MASK + |Gdk::LEAVE_NOTIFY_MASK ); +} + +} // namespace Widget +} // namespace UI +} // namespace Inkscape + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/ui/widget/preview.h b/src/ui/widget/preview.h new file mode 100644 index 000000000..b455367a9 --- /dev/null +++ b/src/ui/widget/preview.h @@ -0,0 +1,163 @@ +// SPDX-License-Identifier: GPL-2.0-or-later OR MPL-1.1 OR LGPL-2.1-or-later +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Eek Preview Stuffs. + * + * The Initial Developer of the Original Code is + * Jon A. Cruz. + * Portions created by the Initial Developer are Copyright (C) 2005-2008 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef SEEN_EEK_PREVIEW_H +#define SEEN_EEK_PREVIEW_H + +#include <gtkmm/drawingarea.h> + +/** + * @file + * Generic implementation of an object that can be shown by a preview. + */ + +namespace Inkscape { +namespace UI { +namespace Widget { + +enum PreviewStyle { + PREVIEW_STYLE_ICON = 0, + PREVIEW_STYLE_PREVIEW, + PREVIEW_STYLE_NAME, + PREVIEW_STYLE_BLURB, + PREVIEW_STYLE_ICON_NAME, + PREVIEW_STYLE_ICON_BLURB, + PREVIEW_STYLE_PREVIEW_NAME, + PREVIEW_STYLE_PREVIEW_BLURB +}; + +enum ViewType { + VIEW_TYPE_LIST = 0, + VIEW_TYPE_GRID +}; + +enum PreviewSize { + PREVIEW_SIZE_TINY = 0, + PREVIEW_SIZE_SMALL, + PREVIEW_SIZE_MEDIUM, + PREVIEW_SIZE_BIG, + PREVIEW_SIZE_BIGGER, + PREVIEW_SIZE_HUGE +}; + +enum LinkType { + PREVIEW_LINK_NONE = 0, + PREVIEW_LINK_IN = 1, + PREVIEW_LINK_OUT = 2, + PREVIEW_LINK_OTHER = 4, + PREVIEW_FILL = 8, + PREVIEW_STROKE = 16, + PREVIEW_LINK_ALL = 31 +}; + +enum BorderStyle { + BORDER_NONE = 0, + BORDER_SOLID, + BORDER_WIDE, + BORDER_SOLID_LAST_ROW, +}; + +class Preview : public Gtk::DrawingArea { +private: + int _scaledW; + int _scaledH; + + int _r; + int _g; + int _b; + + bool _hot; + bool _within; + bool _takesFocus; ///< flag to grab focus when clicked + ViewType _view; + PreviewSize _size; + unsigned int _ratio; + LinkType _linked; + unsigned int _border; + + Glib::RefPtr<Gdk::Pixbuf> _previewPixbuf; + Glib::RefPtr<Gdk::Pixbuf> _scaled; + + // signals + sigc::signal<void> _signal_clicked; + sigc::signal<void, int> _signal_alt_clicked; + + void size_request(GtkRequisition *req) const; + +protected: + void get_preferred_width_vfunc(int &minimal_width, int &natural_width) const override; + void get_preferred_height_vfunc(int &minimal_height, int &natural_height) const override; + bool on_draw(const Cairo::RefPtr<Cairo::Context> &cr) override; + bool on_button_press_event(GdkEventButton *button_event) override; + bool on_button_release_event(GdkEventButton *button_event) override; + bool on_enter_notify_event(GdkEventCrossing* event ) override; + bool on_leave_notify_event(GdkEventCrossing* event ) override; + +public: + Preview(); + bool get_focus_on_click() const {return _takesFocus;} + void set_focus_on_click(bool focus_on_click) {_takesFocus = focus_on_click;} + LinkType get_linked() const; + void set_linked(LinkType link); + void set_details(ViewType view, + PreviewSize size, + guint ratio, + guint border); + void set_color(int r, int g, int b); + void set_pixbuf(const Glib::RefPtr<Gdk::Pixbuf> &pixbuf); + static void set_size_mappings(guint count, GtkIconSize const* sizes); + + decltype(_signal_clicked) signal_clicked() {return _signal_clicked;} + decltype(_signal_alt_clicked) signal_alt_clicked() {return _signal_alt_clicked;} +}; + +} // namespace Widget +} // namespace UI +} // namespace Inkscape + +#endif /* SEEN_EEK_PREVIEW_H */ + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : |
