Moved Toolbar helper functions to own class, to port to GTK3

presentation
Andreas Butti 7 years ago
parent a724ddb5a2
commit 6e1db84938
  1. 4
      src/gui/ToolitemDragDrop.cpp
  2. 5
      src/gui/dialog/toolbarCustomize/ToolbarCustomizeDialog.cpp
  3. 4
      src/gui/dialog/toolbarCustomize/ToolbarDragDropHelper.cpp
  4. 4
      src/gui/toolbarMenubar/AbstractToolItem.cpp
  5. 99
      src/gui/toolbarMenubar/ToolbarUtil.cpp
  6. 23
      src/gui/toolbarMenubar/ToolbarUtil.h
  7. 94
      src/util/Util.cpp
  8. 4
      src/util/Util.h

@ -2,6 +2,8 @@
#include "dialog/toolbarCustomize/ToolbarAdapter.h"
#include "dialog/toolbarCustomize/ToolbarDragDropHelper.h"
#include "dialog/toolbarCustomize/ToolbarDragDropHelper.h"
#include "toolbarMenubar/ToolbarUtil.h"
#include <string.h>
@ -70,7 +72,7 @@ GtkWidget* ToolitemDragDrop::getIcon(ToolItemDragDropData* data)
}
else if (data->type == TOOL_ITEM_SEPARATOR)
{
return Util::newSepeartorImage();
return ToolbarUtil::newSepeartorImage();
}
g_warning("ToolitemDragDrop::getIcon unhandled type: %i\n", data->type);

@ -9,6 +9,7 @@
#include "gui/toolbarMenubar/AbstractToolItem.h"
#include "gui/toolbarMenubar/model/ToolbarData.h"
#include "gui/toolbarMenubar/model/ToolbarModel.h"
#include "gui/toolbarMenubar/ToolbarUtil.h"
#include "gui/toolbarMenubar/ToolMenuHandler.h"
#include "gui/widgets/SelectColor.h"
@ -50,7 +51,7 @@ ToolbarCustomizeDialog::ToolbarCustomizeDialog(GladeSearchpath* gladeSearchPath,
// init separator
GtkWidget* tbSeparator = get("tbSeparator");
GtkWidget* icon = Util::newSepeartorImage();
GtkWidget* icon = ToolbarUtil::newSepeartorImage();
GtkWidget* box = gtk_vbox_new(false, 3);
gtk_widget_show(box);
@ -103,7 +104,7 @@ void ToolbarCustomizeDialog::toolitemDragBeginSeparator(GtkWidget* widget, GdkDr
{
ToolItemDragCurrentData::setData(TOOL_ITEM_SEPARATOR, -1, NULL);
GtkWidget* icon = Util::newSepeartorImage();
GtkWidget* icon = ToolbarUtil::newSepeartorImage();
gtk_drag_set_icon_pixbuf(context, ToolbarDragDropHelper::getImagePixbuf(GTK_IMAGE(icon)), -2, -2);
gtk_widget_unref(icon);
}

@ -1,8 +1,8 @@
#include "ToolbarDragDropHelper.h"
#include "gui/toolbarMenubar/ToolbarUtil.h"
#include "gui/widgets/SelectColor.h"
#include <Util.h>
GdkAtom ToolbarDragDropHelper::atomToolItem = gdk_atom_intern_static_string("application/xournal-ToolbarItem");
GtkTargetEntry ToolbarDragDropHelper::dropTargetEntry = { (gchar *)"move-buffer", GTK_TARGET_SAME_APP, 1 };
@ -37,7 +37,7 @@ GdkPixbuf* ToolbarDragDropHelper::getColorImage(int color)
GtkWidget* icon = selectcolor_new(color);
selectcolor_set_size(icon, 16);
selectcolor_set_circle(icon, true);
GdkPixbuf* image = Util::newPixbufFromWidget(icon, 16);
GdkPixbuf* image = ToolbarUtil::newPixbufFromWidget(icon, 16);
gtk_widget_unref(icon);
return image;

@ -1,6 +1,6 @@
#include "AbstractToolItem.h"
#include "gui/toolbarMenubar/ToolbarUtil.h"
#include <Util.h>
AbstractToolItem::AbstractToolItem(string id, ActionHandler* handler, ActionType type, GtkWidget* menuitem) :
AbstractItem(id, handler, type, menuitem)
@ -166,7 +166,7 @@ GtkWidget* AbstractToolItem::getNewToolIcon()
if (!GTK_IS_IMAGE(icon))
{
GdkPixbuf* pixbuf = Util::newPixbufFromWidget(icon);
GdkPixbuf* pixbuf = ToolbarUtil::newPixbufFromWidget(icon);
gtk_widget_unref(icon);
icon = gtk_image_new_from_pixbuf(pixbuf);
g_object_unref(pixbuf);

@ -0,0 +1,99 @@
#include "ToolbarUtil.h"
ToolbarUtil::ToolbarUtil() { }
ToolbarUtil::~ToolbarUtil() { }
GtkWidget* ToolbarUtil::newSepeartorImage()
{
GtkWidget* separator = gtk_vseparator_new();
GdkPixbuf* pixbuf = ToolbarUtil::newPixbufFromWidget(separator);
gtk_widget_unref(separator);
GtkWidget * w = gtk_image_new_from_pixbuf(pixbuf);
g_object_unref(pixbuf);
return w;
}
void ToolbarUtil::fakeExposeWidget(GtkWidget* widget, GdkPixmap* pixmap)
{
GdkWindow* tmp_window;
GdkEventExpose event;
event.type = GDK_EXPOSE;
event.window = pixmap;
event.send_event = FALSE;
event.area = widget->allocation;
event.region = NULL;
event.count = 0;
tmp_window = widget->window;
widget->window = pixmap;
gtk_widget_send_expose(widget, (GdkEvent*) &event);
widget->window = tmp_window;
}
/*
* Source egg-editable-toolbar.c from evince
*
* We should probably experiment some more with this.
* Right now the rendered icon is pretty good for most
* themes. However, the icon is slightly large for themes
* with large toolbar icons.
*/
GdkPixbuf* ToolbarUtil::newPixbufFromWidget(GtkWidget* widget, int iconSize)
{
GtkWidget* window;
GdkPixbuf* pixbuf;
GtkRequisition requisition;
GtkAllocation allocation;
GdkPixmap* pixmap;
GdkVisual* visual;
gint icon_width;
gint icon_height;
icon_height = icon_width = iconSize;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_add(GTK_CONTAINER(window), widget);
gtk_widget_realize(window);
gtk_widget_show(widget);
gtk_widget_realize(widget);
gtk_widget_map(widget);
/* Gtk will never set the width or height of a window to 0. So setting the width to
* 0 and than getting it will provide us with the minimum width needed to render
* the icon correctly, without any additional window background noise.
* This is needed mostly for pixmap based themes.
*/
gtk_window_set_default_size(GTK_WINDOW(window), icon_width, icon_height);
gtk_window_get_size(GTK_WINDOW(window), &icon_width, &icon_height);
gtk_widget_size_request(window, &requisition);
allocation.x = 0;
allocation.y = 0;
allocation.width = icon_width;
allocation.height = icon_height;
gtk_widget_size_allocate(window, &allocation);
gtk_widget_size_request(window, &requisition);
/* Create a pixmap */
visual = gtk_widget_get_visual(window);
pixmap = gdk_pixmap_new(NULL, icon_width, icon_height, visual->depth);
gdk_drawable_set_colormap(GDK_DRAWABLE(pixmap), gtk_widget_get_colormap(window));
/* Draw the window */
gtk_widget_ensure_style(window);
g_assert(window->style);
g_assert(window->style->font_desc);
fakeExposeWidget(window, pixmap);
fakeExposeWidget(widget, pixmap);
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, icon_width, icon_height);
gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, icon_width, icon_height);
gtk_widget_destroy(window);
return pixbuf;
}

@ -0,0 +1,23 @@
/*
* Xournal++
*
* Utility functions used for Toolbar implementation
*
* @author Xournal++ Team
* https://github.com/xournalpp/xournalpp
*
* @license GNU GPLv2 or later
*/
#include <gtk/gtk.h>
class ToolbarUtil {
private:
ToolbarUtil();
virtual ~ToolbarUtil();
public:
static void fakeExposeWidget(GtkWidget* widget, GdkPixmap* pixmap);
static GdkPixbuf* newPixbufFromWidget(GtkWidget* widget, int iconSize = 24);
static GtkWidget* newSepeartorImage();
};

@ -72,100 +72,6 @@ path Util::getConfigFile(path relativeFileName)
return p;
}
GtkWidget* Util::newSepeartorImage()
{
GtkWidget* separator = gtk_vseparator_new();
GdkPixbuf* pixbuf = Util::newPixbufFromWidget(separator);
gtk_widget_unref(separator);
GtkWidget * w = gtk_image_new_from_pixbuf(pixbuf);
g_object_unref(pixbuf);
return w;
}
void Util::fakeExposeWidget(GtkWidget* widget, GdkPixmap* pixmap)
{
GdkWindow* tmp_window;
GdkEventExpose event;
event.type = GDK_EXPOSE;
event.window = pixmap;
event.send_event = FALSE;
event.area = widget->allocation;
event.region = NULL;
event.count = 0;
tmp_window = widget->window;
widget->window = pixmap;
gtk_widget_send_expose(widget, (GdkEvent*) &event);
widget->window = tmp_window;
}
/*
* Source egg-editable-toolbar.c from evince
*
* We should probably experiment some more with this.
* Right now the rendered icon is pretty good for most
* themes. However, the icon is slightly large for themes
* with large toolbar icons.
*/
GdkPixbuf* Util::newPixbufFromWidget(GtkWidget* widget, int iconSize)
{
GtkWidget* window;
GdkPixbuf* pixbuf;
GtkRequisition requisition;
GtkAllocation allocation;
GdkPixmap* pixmap;
GdkVisual* visual;
gint icon_width;
gint icon_height;
icon_height = icon_width = iconSize;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_add(GTK_CONTAINER(window), widget);
gtk_widget_realize(window);
gtk_widget_show(widget);
gtk_widget_realize(widget);
gtk_widget_map(widget);
/* Gtk will never set the width or height of a window to 0. So setting the width to
* 0 and than getting it will provide us with the minimum width needed to render
* the icon correctly, without any additional window background noise.
* This is needed mostly for pixmap based themes.
*/
gtk_window_set_default_size(GTK_WINDOW(window), icon_width, icon_height);
gtk_window_get_size(GTK_WINDOW(window), &icon_width, &icon_height);
gtk_widget_size_request(window, &requisition);
allocation.x = 0;
allocation.y = 0;
allocation.width = icon_width;
allocation.height = icon_height;
gtk_widget_size_allocate(window, &allocation);
gtk_widget_size_request(window, &requisition);
/* Create a pixmap */
visual = gtk_widget_get_visual(window);
pixmap = gdk_pixmap_new(NULL, icon_width, icon_height, visual->depth);
gdk_drawable_set_colormap(GDK_DRAWABLE(pixmap), gtk_widget_get_colormap(window));
/* Draw the window */
gtk_widget_ensure_style(window);
g_assert(window->style);
g_assert(window->style->font_desc);
fakeExposeWidget(window, pixmap);
fakeExposeWidget(widget, pixmap);
pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, icon_width, icon_height);
gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, icon_width, icon_height);
gtk_widget_destroy(window);
return pixbuf;
}
void Util::openFileWithDefaultApplicaion(path filename)
{
#ifdef __APPLE__

@ -34,10 +34,6 @@ public:
static int getPid();
static void fakeExposeWidget(GtkWidget* widget, GdkPixmap* pixmap);
static GdkPixbuf* newPixbufFromWidget(GtkWidget* widget, int iconSize = 24);
static GtkWidget* newSepeartorImage();
static void openFileWithDefaultApplicaion(path filename);
static void openFileWithFilebrowser(path filename);

Loading…
Cancel
Save