2018-05-16 16:27:38 +02:00
|
|
|
/*
|
|
|
|
* GDSII-Converter
|
|
|
|
* Copyright (C) 2018 Mario Hüttel <mario.huettel@gmx.net>
|
|
|
|
*
|
|
|
|
* This file is part of GDSII-Converter.
|
|
|
|
*
|
|
|
|
* GDSII-Converter is free software: you can redistribute it and/or modify
|
2018-05-16 16:29:34 +02:00
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
2018-05-16 16:27:38 +02:00
|
|
|
*
|
|
|
|
* GDSII-Converter 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 GDSII-Converter. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-07-24 18:47:29 +02:00
|
|
|
/**
|
2018-12-10 23:37:00 +01:00
|
|
|
* @file layer-selector.c
|
2018-07-24 18:47:29 +02:00
|
|
|
* @brief Implementation of the layer selector
|
|
|
|
* @author Mario Hüttel <mario.huettel@gmx.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup MainApplication
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2018-05-16 16:20:09 +02:00
|
|
|
#include "layer-selector.h"
|
2019-03-14 21:30:37 +01:00
|
|
|
#include "layer-info.h"
|
2019-03-13 20:32:09 +01:00
|
|
|
#include "../gds-parser/gds-parser.h"
|
|
|
|
#include "../widgets/layer-element.h"
|
2019-03-14 21:30:37 +01:00
|
|
|
#include "../mapping-parser.h"
|
2018-05-17 21:46:14 +02:00
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h>
|
2018-05-18 22:47:39 +02:00
|
|
|
#include <stdio.h>
|
2018-06-04 11:37:36 +02:00
|
|
|
#include <stdlib.h>
|
2018-05-16 16:20:09 +02:00
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
struct _LayerSelector {
|
|
|
|
/* Parent */
|
|
|
|
GObject parent;
|
|
|
|
/* Own fields */
|
|
|
|
GtkWidget *associated_load_button;
|
|
|
|
GtkWidget *associated_save_button;
|
|
|
|
GtkWindow *load_parent_window;
|
|
|
|
GtkWindow *save_parent_window;
|
|
|
|
GtkListBox *list_box;
|
2018-05-16 16:20:09 +02:00
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
gpointer dummy[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE(LayerSelector, layer_selector, G_TYPE_OBJECT)
|
|
|
|
|
|
|
|
|
|
|
|
/* Drag and drop code */
|
|
|
|
static GtkTargetEntry entries[] = {
|
|
|
|
{ "GTK_LIST_BOX_ROW", GTK_TARGET_SAME_APP, 0 }
|
|
|
|
};
|
|
|
|
|
2019-03-15 23:17:03 +01:00
|
|
|
static void sel_layer_element_drag_begin(GtkWidget *widget,
|
|
|
|
GdkDragContext *context,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkWidget *row;
|
|
|
|
GtkAllocation alloc;
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
|
|
|
int x, y;
|
|
|
|
(void)data;
|
|
|
|
|
|
|
|
row = gtk_widget_get_ancestor(widget, GTK_TYPE_LIST_BOX_ROW);
|
|
|
|
gtk_widget_get_allocation(row, &alloc);
|
|
|
|
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, alloc.width, alloc.height);
|
|
|
|
cr = cairo_create(surface);
|
|
|
|
|
|
|
|
gtk_style_context_add_class (gtk_widget_get_style_context(row), "drag-icon");
|
|
|
|
gtk_widget_draw (row, cr);
|
|
|
|
gtk_style_context_remove_class(gtk_widget_get_style_context(row), "drag-icon");
|
|
|
|
|
|
|
|
gtk_widget_translate_coordinates (widget, row, 0, 0, &x, &y);
|
|
|
|
cairo_surface_set_device_offset (surface, -x, -y);
|
|
|
|
gtk_drag_set_icon_surface (context, surface);
|
|
|
|
|
|
|
|
cairo_destroy (cr);
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
|
|
|
|
g_object_set_data(G_OBJECT(gtk_widget_get_parent(row)), "drag-row", row);
|
|
|
|
gtk_style_context_add_class(gtk_widget_get_style_context(row), "drag-row");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sel_layer_element_drag_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
|
|
|
|
{
|
|
|
|
GtkWidget *row;
|
|
|
|
(void)context;
|
|
|
|
(void)data;
|
|
|
|
|
|
|
|
row = gtk_widget_get_ancestor(widget, GTK_TYPE_LIST_BOX_ROW);
|
|
|
|
g_object_set_data(G_OBJECT(gtk_widget_get_parent(row)), "drag-row", NULL);
|
|
|
|
gtk_style_context_remove_class(gtk_widget_get_style_context(row), "drag-row");
|
|
|
|
gtk_style_context_remove_class(gtk_widget_get_style_context(row), "drag-hover");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sel_layer_element_drag_data_get(GtkWidget *widget, GdkDragContext *context, GtkSelectionData *selection_data,
|
|
|
|
guint info, guint time, gpointer data)
|
|
|
|
{
|
|
|
|
(void)context;
|
|
|
|
(void)info;
|
|
|
|
(void)time;
|
|
|
|
(void)data;
|
|
|
|
|
|
|
|
gtk_selection_data_set(selection_data, gdk_atom_intern_static_string("GTK_LIST_BOX_ROW"),
|
|
|
|
32, (const guchar *)&widget, sizeof(gpointer));
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
static GtkListBoxRow *layer_selector_get_last_row (GtkListBox *list)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
GtkListBoxRow *row;
|
|
|
|
|
|
|
|
row = NULL;
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
GtkListBoxRow *tmp;
|
|
|
|
tmp = gtk_list_box_get_row_at_index(list, i);
|
|
|
|
if (tmp == NULL)
|
|
|
|
break;
|
|
|
|
row = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkListBoxRow *layer_selector_get_row_before (GtkListBox *list, GtkListBoxRow *row)
|
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
pos = gtk_list_box_row_get_index (row);
|
|
|
|
return gtk_list_box_get_row_at_index (list, pos - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkListBoxRow *layer_selector_get_row_after (GtkListBox *list, GtkListBoxRow *row)
|
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
pos = gtk_list_box_row_get_index(row);
|
|
|
|
return gtk_list_box_get_row_at_index(list, pos + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void layer_selector_drag_data_received(GtkWidget *widget, GdkDragContext *context, gint x, gint y,
|
|
|
|
GtkSelectionData *selection_data, guint info, guint32 time,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkWidget *row_before, *row_after;
|
|
|
|
GtkWidget *row;
|
|
|
|
GtkWidget *source;
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
row_before = GTK_WIDGET(g_object_get_data(G_OBJECT(widget), "row-before"));
|
|
|
|
row_after = GTK_WIDGET(g_object_get_data(G_OBJECT(widget), "row-after"));
|
|
|
|
|
|
|
|
g_object_set_data(G_OBJECT(widget), "row-before", NULL);
|
|
|
|
g_object_set_data(G_OBJECT(widget), "row-after", NULL);
|
|
|
|
|
|
|
|
if (row_before)
|
|
|
|
gtk_style_context_remove_class(gtk_widget_get_style_context(row_before), "drag-hover-bottom");
|
|
|
|
if (row_after)
|
|
|
|
gtk_style_context_remove_class(gtk_widget_get_style_context(row_after), "drag-hover-top");
|
|
|
|
|
|
|
|
row = (gpointer) *((gpointer *)gtk_selection_data_get_data(selection_data));
|
|
|
|
source = gtk_widget_get_ancestor(row, GTK_TYPE_LIST_BOX_ROW);
|
|
|
|
|
|
|
|
if (source == row_after)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_object_ref(source);
|
|
|
|
gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(source)), source);
|
|
|
|
|
|
|
|
if (row_after)
|
|
|
|
pos = gtk_list_box_row_get_index(GTK_LIST_BOX_ROW(row_after));
|
|
|
|
else
|
|
|
|
pos = gtk_list_box_row_get_index(GTK_LIST_BOX_ROW(row_before)) + 1;
|
|
|
|
|
|
|
|
gtk_list_box_insert(GTK_LIST_BOX(widget), source, pos);
|
|
|
|
g_object_unref(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean layer_selector_drag_motion(GtkWidget *widget, GdkDragContext *context, int x, int y, guint time)
|
|
|
|
{
|
|
|
|
GtkAllocation alloc;
|
|
|
|
GtkWidget *row;
|
|
|
|
int hover_row_y;
|
|
|
|
int hover_row_height;
|
|
|
|
GtkWidget *drag_row;
|
|
|
|
GtkWidget *row_before;
|
|
|
|
GtkWidget *row_after;
|
|
|
|
|
|
|
|
row = GTK_WIDGET(gtk_list_box_get_row_at_y(GTK_LIST_BOX(widget), y));
|
|
|
|
|
|
|
|
drag_row = GTK_WIDGET(g_object_get_data(G_OBJECT(widget), "drag-row"));
|
|
|
|
row_after = GTK_WIDGET(g_object_get_data(G_OBJECT(widget), "row-after"));
|
|
|
|
row_before = GTK_WIDGET(g_object_get_data(G_OBJECT(widget), "row-before"));
|
|
|
|
|
|
|
|
gtk_style_context_remove_class(gtk_widget_get_style_context(drag_row), "drag-hover");
|
|
|
|
if (row_before)
|
|
|
|
gtk_style_context_remove_class(gtk_widget_get_style_context(row_before), "drag-hover-bottom");
|
|
|
|
if (row_after)
|
|
|
|
gtk_style_context_remove_class(gtk_widget_get_style_context(row_after), "drag-hover-top");
|
|
|
|
|
|
|
|
if (row) {
|
|
|
|
gtk_widget_get_allocation(row, &alloc);
|
|
|
|
hover_row_y = alloc.y;
|
|
|
|
hover_row_height = alloc.height;
|
|
|
|
|
|
|
|
if (y < hover_row_y + hover_row_height/2) {
|
|
|
|
row_after = row;
|
|
|
|
row_before = GTK_WIDGET(layer_selector_get_row_before(GTK_LIST_BOX(widget), GTK_LIST_BOX_ROW(row)));
|
|
|
|
} else {
|
|
|
|
row_before = row;
|
|
|
|
row_after = GTK_WIDGET(layer_selector_get_row_after(GTK_LIST_BOX(widget), GTK_LIST_BOX_ROW(row)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
row_before = GTK_WIDGET(layer_selector_get_last_row(GTK_LIST_BOX(widget)));
|
|
|
|
row_after = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_set_data(G_OBJECT(widget), "row-before", row_before);
|
|
|
|
g_object_set_data(G_OBJECT(widget), "row-after", row_after);
|
|
|
|
|
|
|
|
if (drag_row == row_before || drag_row == row_after) {
|
|
|
|
gtk_style_context_add_class(gtk_widget_get_style_context(drag_row), "drag-hover");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (row_before)
|
|
|
|
gtk_style_context_add_class(gtk_widget_get_style_context(row_before), "drag-hover-bottom");
|
|
|
|
if (row_after)
|
|
|
|
gtk_style_context_add_class(gtk_widget_get_style_context(row_after), "drag-hover-top");
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void layer_selector_drag_leave(GtkWidget *widget, GdkDragContext *context, guint time)
|
|
|
|
{
|
|
|
|
GtkWidget *drag_row;
|
|
|
|
GtkWidget *row_before;
|
|
|
|
GtkWidget *row_after;
|
|
|
|
|
|
|
|
drag_row = GTK_WIDGET(g_object_get_data(G_OBJECT(widget), "drag-row"));
|
|
|
|
row_before = GTK_WIDGET(g_object_get_data(G_OBJECT(widget), "row-before"));
|
|
|
|
row_after = GTK_WIDGET(g_object_get_data(G_OBJECT(widget), "row-after"));
|
|
|
|
|
|
|
|
gtk_style_context_remove_class(gtk_widget_get_style_context(drag_row), "drag-hover");
|
|
|
|
if (row_before)
|
|
|
|
gtk_style_context_remove_class(gtk_widget_get_style_context(row_before), "drag-hover-bottom");
|
|
|
|
if (row_after)
|
|
|
|
gtk_style_context_remove_class(gtk_widget_get_style_context(row_after), "drag-hover-top");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *dnd_additional_css =
|
|
|
|
".row:not(:first-child) { "
|
|
|
|
" border-top: 1px solid alpha(gray,0.5); "
|
|
|
|
" border-bottom: 1px solid transparent; "
|
|
|
|
"}"
|
|
|
|
".row:first-child { "
|
|
|
|
" border-top: 1px solid transparent; "
|
|
|
|
" border-bottom: 1px solid transparent; "
|
|
|
|
"}"
|
|
|
|
".row:last-child { "
|
|
|
|
" border-top: 1px solid alpha(gray,0.5); "
|
|
|
|
" border-bottom: 1px solid alpha(gray,0.5); "
|
|
|
|
"}"
|
|
|
|
".row.drag-icon { "
|
|
|
|
" background: #282828; "
|
|
|
|
" border: 1px solid blue; "
|
|
|
|
"}"
|
|
|
|
".row.drag-row { "
|
|
|
|
" color: gray; "
|
|
|
|
" background: alpha(gray,0.2); "
|
|
|
|
"}"
|
|
|
|
".row.drag-row.drag-hover { "
|
|
|
|
" border-top: 1px solid #4e9a06; "
|
|
|
|
" border-bottom: 1px solid #4e9a06; "
|
|
|
|
"}"
|
|
|
|
".row.drag-hover image, "
|
|
|
|
".row.drag-hover label { "
|
|
|
|
" color: #4e9a06; "
|
|
|
|
"}"
|
|
|
|
".row.drag-hover-top {"
|
|
|
|
" border-top: 1px solid #4e9a06; "
|
|
|
|
"}"
|
|
|
|
".row.drag-hover-bottom {"
|
|
|
|
" border-bottom: 1px solid #4e9a06; "
|
|
|
|
"}";
|
|
|
|
|
|
|
|
static void layer_selector_dispose(GObject *self)
|
|
|
|
{
|
|
|
|
LayerSelector *sel = LAYER_SELECTOR(self);
|
|
|
|
|
|
|
|
g_clear_object(&sel->list_box);
|
|
|
|
g_clear_object(&sel->load_parent_window);
|
|
|
|
g_clear_object(&sel->save_parent_window);
|
|
|
|
g_clear_object(&sel->associated_load_button);
|
|
|
|
g_clear_object(&sel->associated_save_button);
|
|
|
|
|
|
|
|
/* Chain up to parent's dispose function */
|
|
|
|
G_OBJECT_CLASS(layer_selector_parent_class)->dispose(self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void layer_selector_class_init(LayerSelectorClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS(klass);
|
|
|
|
GtkCssProvider *provider;
|
|
|
|
|
|
|
|
/* Implement handles to virtual functions */
|
|
|
|
object_class->dispose = layer_selector_dispose;
|
|
|
|
|
|
|
|
/* Setup the CSS provider for the drag and drop animations once */
|
|
|
|
provider = gtk_css_provider_new();
|
|
|
|
gtk_css_provider_load_from_data(provider, dnd_additional_css, -1, NULL);
|
|
|
|
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), GTK_STYLE_PROVIDER(provider), 800);
|
|
|
|
|
|
|
|
g_object_unref(provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void layer_selector_setup_dnd(GtkListBox *box)
|
|
|
|
{
|
|
|
|
gtk_drag_dest_set(GTK_WIDGET(box), GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP, entries, 1, GDK_ACTION_MOVE);
|
|
|
|
g_signal_connect(box, "drag-data-received", G_CALLBACK(layer_selector_drag_data_received), NULL);
|
|
|
|
g_signal_connect(box, "drag-motion", G_CALLBACK(layer_selector_drag_motion), NULL);
|
|
|
|
g_signal_connect(box, "drag-leave", G_CALLBACK(layer_selector_drag_leave), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Drag and drop end */
|
|
|
|
|
|
|
|
static void layer_selector_init(LayerSelector *self)
|
|
|
|
{
|
|
|
|
/* Setup drag and drop for associated list box */
|
|
|
|
self->load_parent_window = NULL;
|
|
|
|
self->save_parent_window = NULL;
|
|
|
|
self->associated_load_button = NULL;
|
|
|
|
self->associated_save_button = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
LayerSelector *layer_selector_new(GtkListBox *list_box)
|
|
|
|
{
|
|
|
|
LayerSelector *selector;
|
|
|
|
|
|
|
|
if (GTK_IS_LIST_BOX(list_box) == FALSE)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
selector = LAYER_SELECTOR(g_object_new(TYPE_LAYER_SELECTOR, NULL));
|
|
|
|
selector->list_box = list_box;
|
|
|
|
layer_selector_setup_dnd(list_box);
|
|
|
|
g_object_ref(G_OBJECT(list_box));
|
|
|
|
|
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
GList *layer_selector_export_rendered_layer_info(LayerSelector *selector)
|
2018-05-16 16:20:09 +02:00
|
|
|
{
|
2018-05-22 16:17:14 +02:00
|
|
|
GList *info_list = NULL;
|
|
|
|
LayerElement *le;
|
|
|
|
struct layer_info *linfo;
|
2018-05-23 01:03:16 +02:00
|
|
|
GList *row_list;
|
|
|
|
GList *temp;
|
|
|
|
int i;
|
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
if (!selector)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
row_list = gtk_container_get_children(GTK_CONTAINER(selector->list_box));
|
2018-05-16 16:20:09 +02:00
|
|
|
|
2018-05-22 16:17:14 +02:00
|
|
|
/* Iterate through widgets and add layers that shall be exported */
|
2018-05-23 01:03:16 +02:00
|
|
|
for (i = 0, temp = row_list; temp != NULL; temp = temp->next, i++) {
|
2018-05-16 16:20:09 +02:00
|
|
|
|
2018-05-23 01:03:16 +02:00
|
|
|
le = LAYER_ELEMENT(temp->data);
|
2018-05-16 16:20:09 +02:00
|
|
|
|
2018-05-22 16:17:14 +02:00
|
|
|
if (layer_element_get_export(le) == TRUE) {
|
|
|
|
/* Allocate new info and fill with info */
|
|
|
|
linfo = (struct layer_info *)malloc(sizeof(struct layer_info));
|
|
|
|
layer_element_get_color(le, &linfo->color);
|
|
|
|
linfo->layer = layer_element_get_layer(le);
|
2018-05-23 01:03:16 +02:00
|
|
|
linfo->stacked_position = i;
|
2018-05-31 00:06:00 +02:00
|
|
|
linfo->name = (char *)layer_element_get_name(le);
|
2018-05-16 16:20:09 +02:00
|
|
|
|
2018-05-22 16:17:14 +02:00
|
|
|
/* Append to list */
|
|
|
|
info_list = g_list_append(info_list, (gpointer)linfo);
|
|
|
|
}
|
|
|
|
}
|
2018-05-16 16:20:09 +02:00
|
|
|
|
2018-05-22 16:17:14 +02:00
|
|
|
return info_list;
|
2018-05-16 16:20:09 +02:00
|
|
|
}
|
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
static void layer_selector_clear_widgets(LayerSelector *self)
|
2018-05-16 16:20:09 +02:00
|
|
|
{
|
|
|
|
GList *list;
|
2018-05-23 01:03:16 +02:00
|
|
|
GList *temp;
|
2018-05-16 16:20:09 +02:00
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
list = gtk_container_get_children(GTK_CONTAINER(self->list_box));
|
2018-05-23 01:03:16 +02:00
|
|
|
for (temp = list; temp != NULL; temp = temp->next) {
|
2019-03-14 23:40:26 +01:00
|
|
|
gtk_container_remove(GTK_CONTAINER(self->list_box), GTK_WIDGET(temp->data));
|
2018-05-16 16:20:09 +02:00
|
|
|
}
|
2018-05-17 21:46:14 +02:00
|
|
|
/* Widgets are already destroyed when removed from box because they are only referenced inside the container */
|
2018-05-23 01:03:16 +02:00
|
|
|
|
|
|
|
g_list_free(list);
|
2018-05-17 21:46:14 +02:00
|
|
|
|
|
|
|
/* Deactivate buttons */
|
2019-03-14 23:40:26 +01:00
|
|
|
if (self->associated_load_button)
|
|
|
|
gtk_widget_set_sensitive(self->associated_load_button, FALSE);
|
|
|
|
if (self->associated_save_button)
|
|
|
|
gtk_widget_set_sensitive(self->associated_save_button, FALSE);
|
2018-05-16 16:20:09 +02:00
|
|
|
}
|
|
|
|
|
2018-07-24 18:47:29 +02:00
|
|
|
/**
|
|
|
|
* @brief Check if specific layer number is present in list box
|
|
|
|
* @param layer Layer nu,ber
|
|
|
|
* @return TRUE if present
|
|
|
|
*/
|
2019-03-14 23:40:26 +01:00
|
|
|
static gboolean layer_selector_check_if_layer_widget_exists(LayerSelector *self, int layer) {
|
2018-05-16 16:20:09 +02:00
|
|
|
GList *list;
|
2018-05-23 01:03:16 +02:00
|
|
|
GList *temp;
|
2018-05-16 16:20:09 +02:00
|
|
|
LayerElement *widget;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
list = gtk_container_get_children(GTK_CONTAINER(self->list_box));
|
2018-05-23 01:03:16 +02:00
|
|
|
|
|
|
|
for (temp = list; temp != NULL; temp = temp->next) {
|
|
|
|
widget = LAYER_ELEMENT(temp->data);
|
2018-05-16 16:20:09 +02:00
|
|
|
if (layer_element_get_layer(widget) == layer) {
|
|
|
|
ret = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 01:03:16 +02:00
|
|
|
|
|
|
|
g_list_free(list);
|
|
|
|
|
2018-05-16 16:20:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-15 23:17:03 +01:00
|
|
|
static void sel_layer_element_setup_dnd_callbacks(LayerElement *element)
|
|
|
|
{
|
|
|
|
layer_element_set_dnd_callbacks(element, entries, 1,
|
|
|
|
sel_layer_element_drag_begin,
|
|
|
|
sel_layer_element_drag_data_get,
|
|
|
|
sel_layer_element_drag_end);
|
|
|
|
}
|
|
|
|
|
2018-07-24 18:47:29 +02:00
|
|
|
/**
|
|
|
|
* @brief Analyze \p cell and append used layers to list box
|
|
|
|
* @param listbox listbox to add layer
|
|
|
|
* @param cell Cell to analyze
|
|
|
|
*/
|
2019-03-14 23:40:26 +01:00
|
|
|
static void layer_selector_analyze_cell_layers(LayerSelector *self, struct gds_cell *cell)
|
2018-05-16 16:20:09 +02:00
|
|
|
{
|
|
|
|
GList *graphics;
|
|
|
|
struct gds_graphics *gfx;
|
|
|
|
int layer;
|
|
|
|
GtkWidget *le;
|
|
|
|
|
|
|
|
for (graphics = cell->graphic_objs; graphics != NULL; graphics = graphics->next) {
|
|
|
|
gfx = (struct gds_graphics *)graphics->data;
|
|
|
|
layer = (int)gfx->layer;
|
2019-03-14 23:40:26 +01:00
|
|
|
if (layer_selector_check_if_layer_widget_exists(self, layer) == FALSE) {
|
2018-05-16 16:20:09 +02:00
|
|
|
le = layer_element_new();
|
2019-03-15 23:17:03 +01:00
|
|
|
sel_layer_element_setup_dnd_callbacks(LAYER_ELEMENT(le));
|
2018-05-16 16:20:09 +02:00
|
|
|
layer_element_set_layer(LAYER_ELEMENT(le), layer);
|
2019-03-14 23:40:26 +01:00
|
|
|
gtk_list_box_insert(self->list_box, le, -1);
|
2018-05-16 16:20:09 +02:00
|
|
|
gtk_widget_show(le);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 18:47:29 +02:00
|
|
|
/**
|
|
|
|
* @brief sort_func Sort callback for list box
|
|
|
|
* @param row1
|
|
|
|
* @param row2
|
|
|
|
* @param unused
|
2019-03-12 19:26:31 +01:00
|
|
|
* @note Do not use this function. This is an internal callback
|
|
|
|
* @return See sort function documentation of GTK+
|
2018-07-24 18:47:29 +02:00
|
|
|
*/
|
2019-03-14 23:40:26 +01:00
|
|
|
static gint layer_selector_sort_func(GtkListBoxRow *row1, GtkListBoxRow *row2, gpointer unused)
|
2018-05-16 16:20:09 +02:00
|
|
|
{
|
|
|
|
LayerElement *le1, *le2;
|
|
|
|
gint ret;
|
2019-03-12 19:28:17 +01:00
|
|
|
static const enum layer_selector_sort_algo default_sort = LAYER_SELECTOR_SORT_DOWN;
|
|
|
|
const enum layer_selector_sort_algo *algo = (const enum layer_selector_sort_algo *)unused;
|
2019-03-12 19:26:31 +01:00
|
|
|
|
|
|
|
/* Assume downward sorting */
|
2019-03-12 19:28:17 +01:00
|
|
|
/* TODO: This is nasty. Find a better way */
|
2019-03-12 19:26:31 +01:00
|
|
|
if (!algo)
|
|
|
|
algo = &default_sort;
|
2018-05-16 16:20:09 +02:00
|
|
|
|
2018-05-23 01:03:16 +02:00
|
|
|
le1 = LAYER_ELEMENT(row1);
|
|
|
|
le2 = LAYER_ELEMENT(row2);
|
2018-05-16 16:20:09 +02:00
|
|
|
|
2019-03-12 19:26:31 +01:00
|
|
|
/* Determine sort fow downward sort */
|
2018-05-16 16:20:09 +02:00
|
|
|
ret = layer_element_get_layer(le1) - layer_element_get_layer(le2);
|
|
|
|
|
2019-03-12 19:26:31 +01:00
|
|
|
/* Change order if upward sort is requested */
|
|
|
|
ret *= (*algo == LAYER_SELECTOR_SORT_DOWN ? 1 : -1);
|
|
|
|
|
2018-05-16 16:20:09 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
void layer_selector_generate_layer_widgets(LayerSelector *selector, GList *libs)
|
2018-05-16 16:20:09 +02:00
|
|
|
{
|
|
|
|
GList *cell_list = NULL;
|
|
|
|
struct gds_library *lib;
|
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
layer_selector_clear_widgets(selector);
|
2018-05-16 16:20:09 +02:00
|
|
|
|
|
|
|
for (; libs != NULL; libs = libs->next) {
|
|
|
|
lib = (struct gds_library *)libs->data;
|
|
|
|
for (cell_list = lib->cells; cell_list != NULL; cell_list = cell_list->next) {
|
2019-03-14 23:40:26 +01:00
|
|
|
layer_selector_analyze_cell_layers(selector, (struct gds_cell *)cell_list->data);
|
2018-05-16 16:20:09 +02:00
|
|
|
} /* For Cell List */
|
|
|
|
} /* For libs */
|
|
|
|
|
2019-03-12 19:39:36 +01:00
|
|
|
/* Sort the layers */
|
2019-03-14 23:40:26 +01:00
|
|
|
layer_selector_force_sort(selector, LAYER_SELECTOR_SORT_DOWN);
|
2018-05-23 01:03:16 +02:00
|
|
|
|
2018-05-17 21:46:14 +02:00
|
|
|
/* Activate Buttons */
|
2019-03-14 23:40:26 +01:00
|
|
|
if (selector->associated_load_button)
|
|
|
|
gtk_widget_set_sensitive(selector->associated_load_button, TRUE);
|
|
|
|
if (selector->associated_save_button)
|
|
|
|
gtk_widget_set_sensitive(selector->associated_save_button, TRUE);
|
2018-05-17 21:46:14 +02:00
|
|
|
}
|
|
|
|
|
2018-07-24 18:47:29 +02:00
|
|
|
/**
|
|
|
|
* @brief Find LayerElement in list with specified layer number
|
|
|
|
* @param el_list List with elements of type LayerElement
|
|
|
|
* @param layer Layer number
|
|
|
|
* @return Found LayerElement. If nothing is found, NULL.
|
|
|
|
*/
|
2019-03-14 23:40:26 +01:00
|
|
|
static LayerElement *layer_selector_find_layer_element_in_list(GList *el_list, int layer)
|
2018-05-18 22:47:39 +02:00
|
|
|
{
|
2018-05-23 01:03:16 +02:00
|
|
|
LayerElement *ret = NULL;
|
|
|
|
for (; el_list != NULL; el_list = el_list->next) {
|
|
|
|
if (layer_element_get_layer(LAYER_ELEMENT(el_list->data)) == layer) {
|
|
|
|
ret = LAYER_ELEMENT(el_list->data);
|
2018-05-18 22:47:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 01:03:16 +02:00
|
|
|
return ret;
|
2018-05-18 22:47:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-24 18:47:29 +02:00
|
|
|
/**
|
|
|
|
* @brief Load file and apply layer definitions to listbox
|
|
|
|
* @param file_name CSV Layer Mapping File
|
|
|
|
*/
|
2019-03-14 23:40:26 +01:00
|
|
|
static void layer_selector_load_layer_mapping_from_file(LayerSelector *self, gchar *file_name)
|
2018-05-18 22:47:39 +02:00
|
|
|
{
|
|
|
|
GFile *file;
|
|
|
|
GFileInputStream *stream;
|
|
|
|
GDataInputStream *dstream;
|
|
|
|
LayerElement *le;
|
|
|
|
char *name;
|
|
|
|
gboolean export;
|
|
|
|
int layer;
|
|
|
|
GdkRGBA color;
|
|
|
|
int result;
|
2018-05-23 01:03:16 +02:00
|
|
|
GList *rows;
|
|
|
|
GList *temp;
|
2018-05-18 22:47:39 +02:00
|
|
|
|
|
|
|
file = g_file_new_for_path(file_name);
|
|
|
|
stream = g_file_read(file, NULL, NULL);
|
|
|
|
|
|
|
|
if (!stream)
|
|
|
|
goto destroy_file;
|
|
|
|
|
|
|
|
dstream = g_data_input_stream_new(G_INPUT_STREAM(stream));
|
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
rows = gtk_container_get_children(GTK_CONTAINER(self->list_box));
|
2018-05-23 01:03:16 +02:00
|
|
|
|
|
|
|
/* Reference and remove all rows from box */
|
|
|
|
for (temp = rows; temp != NULL; temp = temp->next) {
|
|
|
|
le = LAYER_ELEMENT(temp->data);
|
|
|
|
/* Referencing protets the widget from being deleted when removed */
|
|
|
|
g_object_ref(G_OBJECT(le));
|
2019-03-14 23:40:26 +01:00
|
|
|
gtk_container_remove(GTK_CONTAINER(self->list_box), GTK_WIDGET(le));
|
2018-05-23 01:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while((result = load_csv_line(dstream, &export, &name, &layer, &color)) >= 0) {
|
2018-05-18 22:47:39 +02:00
|
|
|
/* skip broken line */
|
|
|
|
if (result == 1)
|
|
|
|
continue;
|
|
|
|
|
2018-05-23 01:03:16 +02:00
|
|
|
/* Add rows in the same order as in file */
|
2019-03-14 23:40:26 +01:00
|
|
|
if ((le = layer_selector_find_layer_element_in_list(rows, layer))) {
|
|
|
|
gtk_list_box_insert(self->list_box, GTK_WIDGET(le), -1);
|
2018-05-23 01:03:16 +02:00
|
|
|
|
2018-05-18 22:47:39 +02:00
|
|
|
layer_element_set_color(le, &color);
|
|
|
|
layer_element_set_export(le, export);
|
2018-05-23 01:03:16 +02:00
|
|
|
layer_element_set_name(le, name);
|
2018-05-23 13:17:10 +02:00
|
|
|
g_free(name);
|
2018-05-23 01:03:16 +02:00
|
|
|
|
|
|
|
/* Dereference and remove from list */
|
|
|
|
g_object_unref(G_OBJECT(le));
|
|
|
|
rows = g_list_remove(rows, le);
|
2018-05-18 22:47:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 01:03:16 +02:00
|
|
|
/* Add remaining elements */
|
|
|
|
for (temp = rows; temp != NULL; temp = temp->next) {
|
|
|
|
le = LAYER_ELEMENT(temp->data);
|
|
|
|
/* Referencing protets the widget from being deleted when removed */
|
2019-03-14 23:40:26 +01:00
|
|
|
gtk_list_box_insert(self->list_box, GTK_WIDGET(le), -1);
|
2018-05-23 01:03:16 +02:00
|
|
|
g_object_unref(G_OBJECT(le));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete list */
|
|
|
|
g_list_free(rows);
|
|
|
|
|
2018-05-18 22:47:39 +02:00
|
|
|
/* read line */
|
|
|
|
g_object_unref(dstream);
|
|
|
|
g_object_unref(stream);
|
|
|
|
destroy_file:
|
|
|
|
g_object_unref(file);
|
|
|
|
}
|
|
|
|
|
2018-07-24 18:47:29 +02:00
|
|
|
/**
|
|
|
|
* @brief Callback for Load Mapping Button
|
|
|
|
* @param button
|
|
|
|
* @param user_data
|
|
|
|
*/
|
2019-03-14 23:40:26 +01:00
|
|
|
static void layer_selector_load_mapping_clicked(GtkWidget *button, gpointer user_data)
|
2018-05-17 21:46:14 +02:00
|
|
|
{
|
2019-03-14 23:40:26 +01:00
|
|
|
LayerSelector *sel;
|
2018-05-18 22:47:39 +02:00
|
|
|
GtkWidget *dialog;
|
|
|
|
gint res;
|
|
|
|
gchar *file_name;
|
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
sel = LAYER_SELECTOR(user_data);
|
|
|
|
|
|
|
|
dialog = gtk_file_chooser_dialog_new("Load Mapping File", GTK_WINDOW(sel->load_parent_window), GTK_FILE_CHOOSER_ACTION_OPEN,
|
2018-05-18 22:47:39 +02:00
|
|
|
"Cancel", GTK_RESPONSE_CANCEL, "Load Mapping", GTK_RESPONSE_ACCEPT, NULL);
|
|
|
|
res = gtk_dialog_run(GTK_DIALOG(dialog));
|
|
|
|
if (res == GTK_RESPONSE_ACCEPT) {
|
|
|
|
file_name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
|
2019-03-14 23:40:26 +01:00
|
|
|
layer_selector_load_layer_mapping_from_file(sel, file_name);
|
2018-05-18 22:47:39 +02:00
|
|
|
g_free(file_name);
|
|
|
|
}
|
|
|
|
gtk_widget_destroy(dialog);
|
2018-05-17 21:46:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-24 18:47:29 +02:00
|
|
|
/**
|
|
|
|
* @brief Save layer mapping of whole list box into file
|
|
|
|
* @param file_name layer mapping file
|
|
|
|
* @param list_box listbox
|
|
|
|
*/
|
2019-03-14 23:40:26 +01:00
|
|
|
static void layer_selector_save_layer_mapping_data(LayerSelector *self, const gchar *file_name)
|
2018-05-17 21:46:14 +02:00
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
char workbuff[512];
|
|
|
|
GList *le_list;
|
2018-05-23 01:03:16 +02:00
|
|
|
GList *temp;
|
2018-05-17 21:46:14 +02:00
|
|
|
|
|
|
|
/* Overwrite existing file */
|
|
|
|
file = fopen((const char *)file_name, "w");
|
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
le_list = gtk_container_get_children(GTK_CONTAINER(self->list_box));
|
2018-05-23 01:03:16 +02:00
|
|
|
|
|
|
|
/* File format is CSV: <Layer>,<target_pos>,<R>,<G>,<B>,<Alpha>,<Export?>,<Name> */
|
|
|
|
for (temp = le_list; temp != NULL; temp = temp->next) {
|
2018-05-17 21:46:14 +02:00
|
|
|
/* To be sure it is a valid string */
|
|
|
|
workbuff[0] = 0;
|
2018-05-23 01:03:16 +02:00
|
|
|
create_csv_line(LAYER_ELEMENT(temp->data), workbuff, sizeof(workbuff));
|
2018-05-17 21:46:14 +02:00
|
|
|
fwrite(workbuff, sizeof(char), strlen(workbuff), file);
|
|
|
|
}
|
|
|
|
|
2018-05-23 01:03:16 +02:00
|
|
|
g_list_free(le_list);
|
|
|
|
|
2018-05-17 21:46:14 +02:00
|
|
|
/* Save File */
|
|
|
|
fflush(file);
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
2018-07-24 18:47:29 +02:00
|
|
|
/**
|
|
|
|
* @brief Callback for Save Layer Mapping Button
|
|
|
|
* @param button
|
|
|
|
* @param user_data
|
|
|
|
*/
|
2019-03-14 23:40:26 +01:00
|
|
|
static void layer_selector_save_mapping_clicked(GtkWidget *button, gpointer user_data)
|
2018-05-17 21:46:14 +02:00
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
gint res;
|
|
|
|
gchar *file_name;
|
2019-03-14 23:40:26 +01:00
|
|
|
LayerSelector *sel;
|
|
|
|
|
|
|
|
sel = LAYER_SELECTOR(user_data);
|
2018-05-17 21:46:14 +02:00
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
dialog = gtk_file_chooser_dialog_new("Save Mapping File", GTK_WINDOW(sel->save_parent_window), GTK_FILE_CHOOSER_ACTION_SAVE,
|
2018-05-18 22:47:39 +02:00
|
|
|
"Cancel", GTK_RESPONSE_CANCEL, "Save Mapping", GTK_RESPONSE_ACCEPT, NULL);
|
2018-07-28 10:01:02 +02:00
|
|
|
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
|
|
|
|
|
2018-05-17 21:46:14 +02:00
|
|
|
res = gtk_dialog_run(GTK_DIALOG(dialog));
|
|
|
|
if (res == GTK_RESPONSE_ACCEPT) {
|
|
|
|
file_name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
|
2019-03-14 23:40:26 +01:00
|
|
|
layer_selector_save_layer_mapping_data(sel, file_name);
|
2018-05-17 21:46:14 +02:00
|
|
|
g_free(file_name);
|
|
|
|
}
|
|
|
|
gtk_widget_destroy(dialog);
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
void layer_selector_set_load_mapping_button(LayerSelector *selector, GtkWidget *button, GtkWindow *main_window)
|
2018-05-17 21:46:14 +02:00
|
|
|
{
|
2019-03-14 23:40:26 +01:00
|
|
|
g_clear_object(&selector->load_parent_window);
|
|
|
|
g_clear_object(&selector->associated_load_button);
|
|
|
|
|
2018-05-17 21:46:14 +02:00
|
|
|
g_object_ref(G_OBJECT(button));
|
2019-03-14 23:40:26 +01:00
|
|
|
g_object_ref(G_OBJECT(main_window));
|
|
|
|
selector->associated_load_button = button;
|
|
|
|
selector->load_parent_window = main_window;
|
|
|
|
g_signal_connect(button, "clicked", G_CALLBACK(layer_selector_load_mapping_clicked), selector);
|
2018-05-17 21:46:14 +02:00
|
|
|
}
|
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
void layer_selector_set_save_mapping_button(LayerSelector *selector, GtkWidget *button, GtkWindow *main_window)
|
2018-05-17 21:46:14 +02:00
|
|
|
{
|
2019-03-14 23:40:26 +01:00
|
|
|
g_clear_object(&selector->save_parent_window);
|
|
|
|
g_clear_object(&selector->associated_save_button);
|
|
|
|
|
2018-05-17 21:46:14 +02:00
|
|
|
g_object_ref(G_OBJECT(button));
|
2019-03-14 23:40:26 +01:00
|
|
|
g_object_ref(G_OBJECT(main_window));
|
|
|
|
selector->associated_save_button = button;
|
|
|
|
selector->save_parent_window = main_window;
|
|
|
|
g_signal_connect(button, "clicked", G_CALLBACK(layer_selector_save_mapping_clicked), selector);
|
2018-05-16 16:20:09 +02:00
|
|
|
}
|
2018-07-24 18:47:29 +02:00
|
|
|
|
2019-03-14 23:40:26 +01:00
|
|
|
void layer_selector_force_sort(LayerSelector *selector, enum layer_selector_sort_algo sort_function)
|
2019-03-12 19:39:36 +01:00
|
|
|
{
|
2019-03-14 23:40:26 +01:00
|
|
|
GtkListBox *box;
|
|
|
|
|
|
|
|
if (!selector)
|
|
|
|
return;
|
|
|
|
|
|
|
|
box = selector->list_box;
|
|
|
|
if (!box)
|
2019-03-12 19:39:36 +01:00
|
|
|
return;
|
|
|
|
|
2019-03-15 23:17:03 +01:00
|
|
|
/* Set sorting function, sort, and disable sorting function */
|
2019-03-14 23:40:26 +01:00
|
|
|
gtk_list_box_set_sort_func(box, layer_selector_sort_func, (gpointer)&sort_function, NULL);
|
|
|
|
gtk_list_box_invalidate_sort(box);
|
|
|
|
gtk_list_box_set_sort_func(box, NULL, NULL, NULL);
|
2019-03-12 19:39:36 +01:00
|
|
|
}
|
|
|
|
|
2018-07-24 18:47:29 +02:00
|
|
|
/** @} */
|