Implemented open file dialog

This commit is contained in:
Mario Hüttel 2018-05-10 00:14:00 +02:00
parent 34086e2458
commit e245c772f1
3 changed files with 333 additions and 281 deletions

View File

@ -1,3 +1,22 @@
/*
* 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
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License.
*
* 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
/* /*
* What's missing? - A lot: * What's missing? - A lot:
* Support for Boxes * Support for Boxes
@ -312,11 +331,9 @@ int parse_gds_from_file(const char *filename, GList **library_list)
struct gds_cell *current_cell = NULL; struct gds_cell *current_cell = NULL;
struct gds_graphics *current_graphics = NULL; struct gds_graphics *current_graphics = NULL;
struct gds_cell_instance *current_s_reference = NULL; struct gds_cell_instance *current_s_reference = NULL;
int x, y;
//////////// ////////////
GList *lib_list; GList *lib_list;
GList *lib_ptr;
GList *cell_ptr;
GList *cell_ref_ptr;
lib_list = *library_list; lib_list = *library_list;
@ -329,8 +346,6 @@ int parse_gds_from_file(const char *filename, GList **library_list)
/* Record parser */ /* Record parser */
while (run == 1) { while (run == 1) {
switch (state) {
case PARSING_LENGTH:
rec_type = INVALID; rec_type = INVALID;
read = fread(workbuff, sizeof(char), 2, gds_file); read = fread(workbuff, sizeof(char), 2, gds_file);
if (read != 2 && (current_cell != NULL || if (read != 2 && (current_cell != NULL ||
@ -363,9 +378,7 @@ int parse_gds_from_file(const char *filename, GList **library_list)
break; break;
} }
rec_data_length -= 4; rec_data_length -= 4;
state = PARSING_TYPE;
break;
case PARSING_TYPE:
read = fread(workbuff, sizeof(char), 2, gds_file); read = fread(workbuff, sizeof(char), 2, gds_file);
if (read != 2) { if (read != 2) {
run = -2; run = -2;
@ -507,11 +520,12 @@ int parse_gds_from_file(const char *filename, GList **library_list)
default: default:
//GDS_WARN("Record: %04x, len: %u", (unsigned int)rec_type, (unsigned int)rec_data_length); //GDS_WARN("Record: %04x, len: %u", (unsigned int)rec_type, (unsigned int)rec_data_length);
break; break;
} } /* switch(rec_type) */
break;
case PARSING_DAT: /* No Data -> No Processing, go back to top */
if (!rec_data_length) continue;
read = fread(workbuff, sizeof(char), rec_data_length, gds_file); read = fread(workbuff, sizeof(char), rec_data_length, gds_file);
state = PARSING_LENGTH; state = PARSING_LENGTH;
@ -520,8 +534,6 @@ int parse_gds_from_file(const char *filename, GList **library_list)
run = -5; run = -5;
break; break;
} }
/* No Data -> No Processing */
if (!read) break;
switch (rec_type) { switch (rec_type) {
case LIBNAME: case LIBNAME:
@ -535,12 +547,15 @@ int parse_gds_from_file(const char *filename, GList **library_list)
/* Get origin of reference */ /* Get origin of reference */
current_s_reference->origin.x = gds_convert_signed_int(workbuff); current_s_reference->origin.x = gds_convert_signed_int(workbuff);
current_s_reference->origin.y = gds_convert_signed_int(&workbuff[4]); current_s_reference->origin.y = gds_convert_signed_int(&workbuff[4]);
printf("\t\tSet origin to: %d/%d\n", current_s_reference->origin.x,
current_s_reference->origin.y);
} else if (current_graphics) { } else if (current_graphics) {
for (i = 0; i < read/8; i++) { for (i = 0; i < read/8; i++) {
// printf("coords: %d/%d", gds_convert_signed_int(&workbuff[i*8]), gds_convert_signed_int(&workbuff[i*8+4])); x = gds_convert_signed_int(&workbuff[i*8]);
current_graphics->vertices = append_vertex(current_graphics->vertices, y = gds_convert_signed_int(&workbuff[i*8]);
gds_convert_signed_int(&workbuff[i*8]), current_graphics->vertices =
gds_convert_signed_int(&workbuff[i*8])); append_vertex(current_graphics->vertices, x, y);
printf("\t\tSet coordinate: %d/%d\n", x, y);
} }
} }
@ -594,10 +609,8 @@ int parse_gds_from_file(const char *filename, GList **library_list)
break; break;
} }
break; /* PARSING_DAT */
} } /* while(run == 1) */
}
fclose(gds_file); fclose(gds_file);

View File

@ -2,7 +2,7 @@
<!-- Generated with glade 3.22.1 --> <!-- Generated with glade 3.22.1 -->
<interface> <interface>
<requires lib="gtk+" version="3.20"/> <requires lib="gtk+" version="3.20"/>
<object class="GtkWindow"> <object class="GtkWindow" id="main-window">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<signal name="delete-event" handler="on_window_close" swapped="no"/> <signal name="delete-event" handler="on_window_close" swapped="no"/>

53
main.c
View File

@ -3,13 +3,14 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
enum cell_store_columns { enum cell_store_columns {
LIBRARY, LIBRARY,
CELL, CELL,
STORE_COLUMN STORE_COLUMN
}; };
struct open_button_data { struct open_button_data {
GtkWindow *main_window;
GList **list_ptr; GList **list_ptr;
GtkTreeStore *cell_store; GtkTreeStore *cell_store;
}; };
@ -36,11 +37,46 @@ void on_load_gds(gpointer button, gpointer user)
struct gds_cell *gds_c; struct gds_cell *gds_c;
struct open_button_data *ptr = (struct open_button_data *)user; struct open_button_data *ptr = (struct open_button_data *)user;
GtkTreeStore *store = ptr->cell_store; GtkTreeStore *store = ptr->cell_store;
GtkWidget *open_dialog;
GtkFileChooser *file_chooser;
GtkFileFilter *filter;
GtkStyleContext *button_style;
gint dialog_result;
int gds_result;
char *filename;
open_dialog = gtk_file_chooser_dialog_new("Open GDSII File", ptr->main_window, GTK_FILE_CHOOSER_ACTION_OPEN,
"Cancel", GTK_RESPONSE_CANCEL, "Open GDSII", GTK_RESPONSE_ACCEPT, NULL);
file_chooser = GTK_FILE_CHOOSER(open_dialog);
/* Add GDS II Filter */
filter = gtk_file_filter_new();
gtk_file_filter_add_pattern(filter, "*.gds");
gtk_file_filter_set_name(filter, "GDSII-Files");
gtk_file_chooser_add_filter(file_chooser, filter);
dialog_result = gtk_dialog_run(GTK_DIALOG(open_dialog));
if (dialog_result == GTK_RESPONSE_ACCEPT) {
/* Get File name */
filename = gtk_file_chooser_get_filename(file_chooser);
/* Clear Display */
gtk_tree_store_clear(store); gtk_tree_store_clear(store);
/* Delete parsed GDS data */
clear_lib_list(ptr->list_ptr); clear_lib_list(ptr->list_ptr);
parse_gds_from_file("/home/mari/Desktop/test.gds", ptr->list_ptr);
/* Parse new GDSII file */
gds_result = parse_gds_from_file(filename, ptr->list_ptr);
/* Delete file name afterwards */
g_free(filename);
if (gds_result)
goto end_destroy;
/* remove suggested action from Open button */
button_style = gtk_widget_get_style_context(GTK_WIDGET(button));
gtk_style_context_remove_class(button_style, "suggested-action");
for (lib = *(ptr->list_ptr); lib != NULL; lib = lib->next) { for (lib = *(ptr->list_ptr); lib != NULL; lib = lib->next) {
gds_lib = (struct gds_library *)lib->data; gds_lib = (struct gds_library *)lib->data;
@ -53,7 +89,10 @@ void on_load_gds(gpointer button, gpointer user)
gtk_tree_store_set (store, &celliter, CELL, gds_c->name, -1); gtk_tree_store_set (store, &celliter, CELL, gds_c->name, -1);
} }
} }
}
end_destroy:
gtk_widget_destroy(open_dialog);
} }
void on_convert_clicked(gpointer button, gpointer user) void on_convert_clicked(gpointer button, gpointer user)
@ -66,7 +105,6 @@ static GtkTreeStore * setup_cell_selector(GtkTreeView* view)
GtkTreeStore *cell_store; GtkTreeStore *cell_store;
GtkCellRenderer *render; GtkCellRenderer *render;
GtkCellRenderer *render2;
GtkTreeViewColumn *column; GtkTreeViewColumn *column;
cell_store = gtk_tree_store_new(STORE_COLUMN, G_TYPE_STRING, G_TYPE_STRING); cell_store = gtk_tree_store_new(STORE_COLUMN, G_TYPE_STRING, G_TYPE_STRING);
@ -75,7 +113,7 @@ static GtkTreeStore * setup_cell_selector(GtkTreeView* view)
render = gtk_cell_renderer_text_new(); render = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("Library", render, "text", LIBRARY, NULL); column = gtk_tree_view_column_new_with_attributes("Library", render, "text", LIBRARY, NULL);
gtk_tree_view_append_column(view, column); gtk_tree_view_append_column(view, column);
//g_object_unref(column);
column = gtk_tree_view_column_new_with_attributes("Cell", render, "text", CELL, NULL); column = gtk_tree_view_column_new_with_attributes("Cell", render, "text", CELL, NULL);
gtk_tree_view_append_column(view, column); gtk_tree_view_append_column(view, column);
@ -103,6 +141,7 @@ int main(int argc, char **argv)
open_data.cell_store = cell_store; open_data.cell_store = cell_store;
open_data.list_ptr = &gds_libs; open_data.list_ptr = &gds_libs;
open_data.main_window = GTK_WINDOW(gtk_builder_get_object(main_builder, "main-window"));
g_signal_connect(GTK_WIDGET(gtk_builder_get_object(main_builder, "button-load-gds")), g_signal_connect(GTK_WIDGET(gtk_builder_get_object(main_builder, "button-load-gds")),
"clicked", G_CALLBACK(on_load_gds), (gpointer)&open_data); "clicked", G_CALLBACK(on_load_gds), (gpointer)&open_data);