diff options
author | Jeremias Stotter <jeremias@stotter.eu> | 2023-09-24 21:34:59 +0200 |
---|---|---|
committer | Jeremias Stotter <jeremias@stotter.eu> | 2023-09-24 21:34:59 +0200 |
commit | 85d48a93b1fe0dbf0131f0cfdef05f1ca5e42595 (patch) | |
tree | f58e01d887b9ed0c0ead2260f5d2856238493106 | |
parent | 269594fe52b5d396e1a1d4a8208087aac725efcc (diff) | |
download | network-simulator-85d48a93b1fe0dbf0131f0cfdef05f1ca5e42595.tar.gz network-simulator-85d48a93b1fe0dbf0131f0cfdef05f1ca5e42595.tar.bz2 network-simulator-85d48a93b1fe0dbf0131f0cfdef05f1ca5e42595.zip |
Avoid using deprecated GtkIconView
-rw-r--r-- | connection.c | 23 | ||||
-rw-r--r-- | connection.h | 3 | ||||
-rw-r--r-- | device.c | 29 | ||||
-rw-r--r-- | device.h | 3 | ||||
-rw-r--r-- | files.c | 4 | ||||
-rw-r--r-- | gui.c | 122 | ||||
-rw-r--r-- | undo-redo.h | 4 |
7 files changed, 103 insertions, 85 deletions
diff --git a/connection.c b/connection.c index 81b36cf..0b17b2b 100644 --- a/connection.c +++ b/connection.c @@ -24,16 +24,10 @@ #include "helpers.h" #include "lua_functions.h" -// Create a new connection between two interfaces -int new_connection(struct interface* interface1, struct interface* interface2, const char* template_name) { - if(!interface1 || !interface2 || !interface1->template || !interface2->template) return -1; - if(interface1 == interface2) { - fprintf(stderr, "Can't connect a plug to the same port twice!\n"); - return -1; - } +int new_connection_from_template_name(struct interface* interface1, struct interface* interface2, const char* template_name) { struct connection_template* cur_template = connection_templates; bool template_found = false; - for(int i = 0; i < connection_template_n; i++, cur_template += sizeof(struct connection_template)) { + for(int i = 0; i < connection_template_n; i++, cur_template = &connection_templates[i]) { if(strcmp(cur_template->name, template_name) == 0) { template_found = true; break; @@ -41,7 +35,18 @@ int new_connection(struct interface* interface1, struct interface* interface2, c } if(!template_found) { - fprintf(stderr, "Template %s was not found\n", template_name); + fprintf(stderr, "Template %s was not found", template_name); + return -1; + } + + return new_connection(interface1, interface2, cur_template); +} + +// Create a new connection between two interfaces +int new_connection(struct interface* interface1, struct interface* interface2, struct connection_template* cur_template) { + if(!interface1 || !interface2 || !interface1->template || !interface2->template) return -1; + if(interface1 == interface2) { + fprintf(stderr, "Can't connect a plug to the same port twice!\n"); return -1; } diff --git a/connection.h b/connection.h index 36cf8eb..a79abf3 100644 --- a/connection.h +++ b/connection.h @@ -14,7 +14,8 @@ #ifndef NETSIM_CONNECTION #define NETSIM_CONNECTION #include "shared.h" -int new_connection(struct interface* interface1, struct interface* interface2, const char* template_name); +int new_connection_from_template_name(struct interface* interface1, struct interface* interface2, const char* template_name); +int new_connection(struct interface* interface1, struct interface* interface2, struct connection_template* cur_template); int delete_connection(struct connection* del_connection); int connection_send(struct connection* send_connection, int connection_side, const char* send_data, int data_length, const char* data_type); #endif @@ -184,18 +184,7 @@ void append_device(struct device** start, struct device* dev) { dev->deleted = false; } -// Make sure that when this function is called device_name will stay allocated -// start_device is the first device in the linked list of all devices or 0 when -// this is the first device -// This will insert itself to the start of the linked list! -// create_interfaces should be set to true if this should create the interfaces in the template -struct device* new_device(struct device** start, char* device_name, const char* template_name, bool create_interfaces) { - for(struct device* test_dev = *start; test_dev; test_dev = test_dev->next) { - if(strcmp(device_name, test_dev->name) == 0) { - fprintf(stderr, "Device %s already exists\n", device_name); - return NULL; - } - } +struct device* new_device_from_template_name(struct device** start, char* device_name, const char* template_name, bool create_interfaces) { struct dev_template* cur_template = device_templates; bool template_found = false; for(int i = 0; i < device_template_n; i++, cur_template = &device_templates[i]) { @@ -209,6 +198,22 @@ struct device* new_device(struct device** start, char* device_name, const char* fprintf(stderr, "Template %s was not found", template_name); return *start; } + + return new_device(start, device_name, cur_template, create_interfaces); +} + +// Make sure that when this function is called device_name will stay allocated +// start_device is the first device in the linked list of all devices or 0 when +// this is the first device +// This will insert itself to the start of the linked list! +// create_interfaces should be set to true if this should create the interfaces in the template +struct device* new_device(struct device** start, char* device_name, struct dev_template* cur_template, bool create_interfaces) { + for(struct device* test_dev = *start; test_dev; test_dev = test_dev->next) { + if(strcmp(device_name, test_dev->name) == 0) { + fprintf(stderr, "Device %s already exists\n", device_name); + return NULL; + } + } struct device* new_device = malloc_or_abort(sizeof(struct device)); new_device->template = cur_template; @@ -16,7 +16,8 @@ #include "shared.h" extern struct device* start_device; void rename_device(struct device* dev, const char* name); -struct device* new_device(struct device** start_device, char* device_name, const char* template_name, bool create_interfaces); +struct device* new_device(struct device** start, char* device_name, struct dev_template* cur_template, bool create_interfaces); +struct device* new_device_from_template_name(struct device** start, char* device_name, const char* template_name, bool create_interfaces); // Append an existing device to the end of the list void append_device(struct device** start, struct device* dev); int free_device(struct device* del_device); @@ -194,7 +194,7 @@ int load_file(char* filename) { fprintf(stderr, "Couldn't get device TEMPLATE of index %d in savefile\n", i); continue; } - struct device* load_dev = new_device(&start_device, + struct device* load_dev = new_device_from_template_name(&start_device, (char*)json_object_get_string(device_name), (char*)json_object_get_string(device_template), false); @@ -260,7 +260,7 @@ int load_file(char* filename) { continue; } - new_connection(&(dev1->interfaces[json_object_get_int(index1)]), + new_connection_from_template_name(&(dev1->interfaces[json_object_get_int(index1)]), &(dev2->interfaces[json_object_get_int(index2)]), (char*)json_object_get_string(template)); } @@ -156,9 +156,9 @@ void update_mode() { break; } if(area_mode != MODE_NEW_DEVICE) - gtk_icon_view_unselect_all(GTK_ICON_VIEW(device_view)); + gtk_flow_box_unselect_all(GTK_FLOW_BOX(device_view)); if(area_mode != MODE_NEW_CONNECTION) - gtk_icon_view_unselect_all(GTK_ICON_VIEW(connection_view)); + gtk_flow_box_unselect_all(GTK_FLOW_BOX(connection_view)); } // User data should be a pointer but it's easier like this @@ -192,7 +192,7 @@ bool device_in_selection(struct device_selection* selection, const struct device return false; } -char* selected_template = NULL; +//char* selected_template = NULL; void draw_arrow(cairo_t* cr, long x1, long y1, @@ -593,29 +593,21 @@ void toggle_play(GtkWidget* self, gpointer data) { } } -void template_from_path(GtkIconView* self, GtkTreePath* path) { - // Make sure we free selected template before - if(!selected_template) { - free(selected_template); - selected_template = NULL; - } - // Get the newly selected template - GtkTreeIter selected_item_iter; - GtkTreeModel* template_list = gtk_icon_view_get_model(self); - gtk_tree_model_get_iter(template_list, &selected_item_iter, path); - gtk_tree_model_get(template_list, &selected_item_iter, 0, &selected_template, -1); - printf("Template %s selected\n", selected_template); -} +struct dev_template* selected_device_template = NULL; // Retrieve the device from the template list and store it in selected_template -void select_device_template(GtkIconView* self, GtkTreePath* path, void* user_data) { - template_from_path(self, path); +void select_device_template(GtkFlowBox* self, GtkFlowBoxChild* child, void* user_data) { + selected_device_template = &device_templates[gtk_flow_box_child_get_index(child)]; + area_mode = MODE_NEW_DEVICE; update_mode(); } -void select_connection_template(GtkIconView* self, GtkTreePath* path, void* user_data) { - template_from_path(self, path); +struct connection_template* selected_connection_template = NULL; + +void select_connection_template(GtkFlowBox* self, GtkFlowBoxChild* child, void* user_data) { + selected_connection_template = &connection_templates[gtk_flow_box_child_get_index(child)]; + area_mode = MODE_NEW_CONNECTION; connection_interface1 = NULL; update_mode(); @@ -702,14 +694,14 @@ void selection_clicked(GtkButton* self, gpointer user_data) { delete_connection(connection_interface1->connection); delete_connection(((struct interface*)user_data)->connection); // Create the connection - new_connection(connection_interface1, (struct interface*)user_data, selected_template); + new_connection(connection_interface1, (struct interface*)user_data, selected_connection_template); struct undo_entry* undo_new = NULL; struct undo_add_con_data* undo_add_con_new = NULL; NEW_UNDO_STRUCT(undo_add_con, undo_add_con_data, undo_new, undo_add_con_new); undo_add_con_new->con = connection_interface1->connection; undo_add_con_new->int1 = connection_interface1; undo_add_con_new->int2 = (struct interface*)user_data; - undo_add_con_new->template = selected_template; + undo_add_con_new->template = selected_connection_template; undo_add_entry(undo_new); connection_interface1 = NULL; @@ -1039,13 +1031,13 @@ void drawing_area_release(GtkGestureClick* self, gint n_press, gdouble x, gdoubl area_moving = false; break; case MODE_NEW_DEVICE:; - char* dev_name = malloc_or_abort(strlen(selected_template) + 6); - strcpy(dev_name, selected_template); + char* dev_name = malloc_or_abort(strlen(selected_device_template->name) + 6); + strcpy(dev_name, selected_device_template->name); // Use at most two digits for(int i = 1; dev_from_name(start_device, dev_name) && i < 100; i++) { - sprintf(dev_name, "%s (%d)", selected_template, i); + sprintf(dev_name, "%s (%d)", selected_device_template->name, i); } - struct device* created_device = new_device(&start_device, dev_name, selected_template, true); + struct device* created_device = new_device(&start_device, dev_name, selected_device_template, true); free(dev_name); if(created_device) { created_device->X = x - (created_device->template->icon_size >> 2); @@ -1104,7 +1096,7 @@ void drawing_area_release(GtkGestureClick* self, gint n_press, gdouble x, gdoubl NEW_UNDO_STRUCT(undo_del_con, undo_del_con_data, undo_new, undo_del_con_new); undo_del_con_new->int1 = delete_con->interface1; undo_del_con_new->int2 = delete_con->interface2; - undo_del_con_new->template = delete_con->template->name; + undo_del_con_new->template = delete_con->template; undo_add_entry(undo_new); delete_connection(delete_con); break; @@ -1174,7 +1166,7 @@ void copy_selection_clipboard(struct device_selection* selection) { // Create the new device char* dev_name = malloc_or_abort(strlen(dev->name) + strlen(COPY_SUFFIX) + 1); sprintf(dev_name, "%s"COPY_SUFFIX, dev->name); - struct device* new_dev = new_device(&clipboard, dev_name, dev->template->name, false); + struct device* new_dev = new_device(&clipboard, dev_name, selected_device_template, false); // Load the state // Ask the old device to save its state and pass it to the new device if(my_lua_call(dev->lua_vm, "save_state", 1, -1)) { @@ -1209,7 +1201,7 @@ void copy_selection_clipboard(struct device_selection* selection) { if(neigh) { new_connection(&new_dev->interfaces[i], &neigh->interfaces[neigh_index], - dev->interfaces[i].connection->template->name); + dev->interfaces[i].connection->template); } } } @@ -1715,34 +1707,48 @@ void activate_main_window(GtkApplication* app, gpointer user_data) { // Attach the stack to the stack switcher gtk_stack_switcher_set_stack(GTK_STACK_SWITCHER(stack_switcher), GTK_STACK(sidebar_stack)); - // === Create the devices tab - // This list will hold the available templates - GtkListStore* device_template_list = gtk_list_store_new(2, G_TYPE_STRING, GDK_TYPE_PIXBUF); - // Fill the list - GtkTreeIter list_iter; + device_view = gtk_flow_box_new(); + gtk_flow_box_set_activate_on_single_click(GTK_FLOW_BOX(device_view), true); + gtk_flow_box_set_selection_mode(GTK_FLOW_BOX(device_view), GTK_SELECTION_SINGLE); + gtk_flow_box_set_homogeneous(GTK_FLOW_BOX(device_view), true); + for(int i = 0; i < device_template_n; i++) { - gtk_list_store_append(GTK_LIST_STORE(device_template_list), &list_iter); - gtk_list_store_set(GTK_LIST_STORE(device_template_list), &list_iter, 0, device_templates[i].name, 1, device_templates[i].icon, -1); + GtkWidget* device_template_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); + gtk_box_set_homogeneous(GTK_BOX(device_template_box), true); + gtk_widget_set_valign(device_template_box, GTK_ALIGN_CENTER); + + GtkWidget* device_template_icon = gtk_image_new_from_pixbuf(device_templates[i].icon); + gtk_image_set_pixel_size(GTK_IMAGE(device_template_icon), device_templates[i].icon_size); + GtkWidget* device_template_label = gtk_label_new(device_templates[i].name); + + gtk_box_append(GTK_BOX(device_template_box), device_template_icon); + gtk_box_append(GTK_BOX(device_template_box), device_template_label); + + gtk_flow_box_append(GTK_FLOW_BOX(device_view), device_template_box); } - // Create the view from the list - device_view = gtk_icon_view_new_with_model(GTK_TREE_MODEL(device_template_list)); - // Enter device creation mode when a device is clicked - g_signal_connect(G_OBJECT(device_view), "item-activated", G_CALLBACK(select_device_template), NULL); - gtk_icon_view_set_selection_mode(GTK_ICON_VIEW(device_view), GTK_SELECTION_SINGLE); - gtk_icon_view_set_activate_on_single_click(GTK_ICON_VIEW(device_view), true); - // Set the pixbuf and text layer - gtk_icon_view_set_text_column(GTK_ICON_VIEW(device_view), 0); - gtk_icon_view_set_pixbuf_column(GTK_ICON_VIEW(device_view), 1); + + g_signal_connect(G_OBJECT(device_view), "child-activated", G_CALLBACK(select_device_template), NULL); + // Create a scrolled window to put the selection into GtkWidget* device_view_scroll = gtk_scrolled_window_new(); gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(device_view_scroll), device_view); + + gtk_widget_set_vexpand(device_view_scroll, false); + gtk_widget_set_vexpand_set(device_view_scroll, true); // Add everything to the stack gtk_stack_add_titled(GTK_STACK(sidebar_stack), device_view_scroll, NULL, "Devices"); // === Create the connections tab - GtkListStore* connection_template_list = gtk_list_store_new(2, G_TYPE_STRING, GDK_TYPE_PIXBUF); - // Fill the template list + + connection_view = gtk_flow_box_new(); + gtk_flow_box_set_activate_on_single_click(GTK_FLOW_BOX(device_view), true); + gtk_flow_box_set_selection_mode(GTK_FLOW_BOX(device_view), GTK_SELECTION_SINGLE); + gtk_flow_box_set_homogeneous(GTK_FLOW_BOX(connection_view), true); + for(int i = 0; i < connection_template_n; i++) { + GtkWidget* connection_template_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); + gtk_box_set_homogeneous(GTK_BOX(connection_template_box), true); + gtk_widget_set_valign(connection_template_box, GTK_ALIGN_CENTER); // Create a pixbuf with the right color GdkPixbuf* connection_icon = gdk_pixbuf_new(GDK_COLORSPACE_RGB, false, 8, 16, 16); uint32_t color = 0; @@ -1753,18 +1759,18 @@ void activate_main_window(GtkApplication* app, gpointer user_data) { color = color << 8; } gdk_pixbuf_fill(connection_icon, color); - gtk_list_store_append(GTK_LIST_STORE(connection_template_list), &list_iter); - gtk_list_store_set(GTK_LIST_STORE(connection_template_list), &list_iter, 0, connection_templates[i].name, 1, connection_icon, -1); + GtkWidget* connection_template_icon = gtk_image_new_from_pixbuf(connection_icon); + GtkWidget* connection_template_label = gtk_label_new(connection_templates[i].name); + + gtk_box_append(GTK_BOX(connection_template_box), connection_template_icon); + gtk_box_append(GTK_BOX(connection_template_box), connection_template_label); + + gtk_flow_box_append(GTK_FLOW_BOX(connection_view), connection_template_box); } - // Create the view - connection_view = gtk_icon_view_new_with_model(GTK_TREE_MODEL(connection_template_list)); - // Enter connection creation mode when a device is clicked - g_signal_connect(G_OBJECT(connection_view), "item-activated", G_CALLBACK(select_connection_template), NULL); - gtk_icon_view_set_selection_mode(GTK_ICON_VIEW(connection_view), GTK_SELECTION_SINGLE); - gtk_icon_view_set_activate_on_single_click(GTK_ICON_VIEW(connection_view), true); - // Set pixbuf and text layer - gtk_icon_view_set_text_column(GTK_ICON_VIEW(connection_view), 0); - gtk_icon_view_set_pixbuf_column(GTK_ICON_VIEW(connection_view), 1); + + g_signal_connect(G_OBJECT(connection_view), "child-activated", G_CALLBACK(select_connection_template), NULL); + + // Scroll window GtkWidget* connection_view_scroll = gtk_scrolled_window_new(); gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(connection_view_scroll), connection_view); diff --git a/undo-redo.h b/undo-redo.h index 15860bc..5c42fdb 100644 --- a/undo-redo.h +++ b/undo-redo.h @@ -78,13 +78,13 @@ struct undo_add_con_data { struct connection* con; struct interface* int1; struct interface* int2; - const char* template; + struct connection_template* template; }; struct undo_del_con_data { struct interface* int1; struct interface* int2; - const char* template; + struct connection_template* template; }; struct undo_ren_data { |