static void image_move(Image *image, Point *to) { image->element.corner = *to; image_update_data(image); }
static Object * image_create(Point *startpoint, void *user_data, Handle **handle1, Handle **handle2) { Image *image; Element *elem; Object *obj; int i; image = g_malloc(sizeof(Image)); elem = (Element *) image; obj = (Object *) image; obj->type = &image_type; obj->ops = &image_ops; elem->corner = *startpoint; elem->width = DEFAULT_WIDTH; elem->height = DEFAULT_WIDTH; image_init_defaults(); image->border_width = attributes_get_default_linewidth(); image->border_color = attributes_get_foreground(); image->line_style = default_properties.line_style; element_init(elem, 8, 8); for (i=0;i<8;i++) { obj->connections[i] = &image->connections[i]; image->connections[i].object = obj; image->connections[i].connected = NULL; } if (strcmp(default_properties.file, "")) { image->file = g_strdup(default_properties.file); image->image = dia_image_load(image->file); if (image->image) { elem->width = (elem->width*(float)dia_image_width(image->image))/ (float)dia_image_height(image->image); } } else { image->file = g_strdup(""); image->image = NULL; } image->draw_border = default_properties.draw_border; image->keep_aspect = default_properties.keep_aspect; image_update_data(image); *handle1 = NULL; *handle2 = obj->handles[7]; return (Object *)image; }
static void image_set_props(Image *image, GPtrArray *props) { struct stat st; time_t mtime = 0; char *old_file = image->file ? g_strdup(image->file) : NULL; const GdkPixbuf *old_pixbuf = dia_image_pixbuf (image->image); gboolean was_inline = image->inline_data; object_set_props_from_offsets(&image->element.object, image_offsets, props); if (old_pixbuf != image->pixbuf) { if (!image->file || *image->file == '\0') { GdkPixbuf *pixbuf = NULL; image->inline_data = TRUE; /* otherwise we'll loose it */ /* somebody deleting the filename? */ if (!image->pixbuf && image->image) pixbuf = g_object_ref ((GdkPixbuf *)dia_image_pixbuf (image->image)); if (image->image) g_object_unref (image->image); image->image = dia_image_new_from_pixbuf (image->pixbuf ? image->pixbuf : pixbuf); image->pixbuf = (GdkPixbuf *)dia_image_pixbuf (image->image); if (pixbuf) g_object_unref (pixbuf); } else { if (image->pixbuf) message_warning ("FIXME: handle pixbuf change!"); } } /* use old value on error */ if (!image->file || g_stat (image->file, &st) != 0) mtime = image->mtime; else mtime = st.st_mtime; /* handle changing the image. */ if (image->file && image->pixbuf && was_inline && !image->inline_data) { /* export inline data */ if (was_inline && !image->inline_data) /* if saving fails we keep it inline */ image->inline_data = !dia_image_save (image->image, image->file); } else if (image->file && ((old_file && strcmp(image->file, old_file) != 0) || image->mtime != mtime)) { Element *elem = &image->element; DiaImage *img = NULL; if ((img = dia_image_load(image->file)) != NULL) image->image = img; else if (!image->pixbuf) /* dont overwrite inlined */ image->image = dia_image_get_broken(); elem->height = (elem->width*(float)dia_image_height(image->image))/ (float)dia_image_width(image->image); } g_free(old_file); /* remember it */ image->mtime = mtime; image_update_data(image); }
static DiaObject * image_create(Point *startpoint, void *user_data, Handle **handle1, Handle **handle2) { Image *image; Element *elem; DiaObject *obj; int i; image = g_malloc0(sizeof(Image)); elem = &image->element; obj = &elem->object; obj->type = &image_type; obj->ops = &image_ops; elem->corner = *startpoint; elem->width = DEFAULT_WIDTH; elem->height = DEFAULT_HEIGHT; image->border_width = attributes_get_default_linewidth(); image->border_color = attributes_get_foreground(); attributes_get_default_line_style(&image->line_style, &image->dashlength); element_init(elem, 8, NUM_CONNECTIONS); for (i=0; i<NUM_CONNECTIONS; i++) { obj->connections[i] = &image->connections[i]; image->connections[i].object = obj; image->connections[i].connected = NULL; } image->connections[8].flags = CP_FLAGS_MAIN; if (strcmp(default_properties.file, "")) { image->file = g_strdup(default_properties.file); image->image = dia_image_load(image->file); if (image->image) { elem->width = (elem->width*(float)dia_image_width(image->image))/ (float)dia_image_height(image->image); } } else { image->file = g_strdup(""); image->image = NULL; } image->draw_border = default_properties.draw_border; image->keep_aspect = default_properties.keep_aspect; image_update_data(image); *handle1 = NULL; *handle2 = obj->handles[7]; return &image->element.object; }
static ObjectChange* image_move(EImage *image, Point *to) { image->element.corner = *to; image_update_data(image); return NULL; }
static ObjectChange * image_apply_properties(Image *image) { gchar *new_file; ObjectState *old_state; if (image != image_properties_dialog->image) { message_warning("Image dialog problem: %p != %p\n", image, image_properties_dialog->image); image = image_properties_dialog->image; } old_state = (ObjectState *)image_get_state(image); image->border_width = gtk_spin_button_get_value_as_float(image_properties_dialog->border_width); dia_color_selector_get_color(image_properties_dialog->fg_color, &image->border_color); dia_line_style_selector_get_linestyle(image_properties_dialog->line_style, &image->line_style, NULL); image->draw_border = gtk_toggle_button_get_active(image_properties_dialog->draw_border); image->keep_aspect = gtk_toggle_button_get_active(image_properties_dialog->keep_aspect); new_file = dia_file_selector_get_file(image_properties_dialog->file); if (image->file) g_free(image->file); if (image->image) { dia_image_release(image->image); } image->image = dia_image_load(new_file); if ((image->image != NULL) && (image->keep_aspect)) { /* Must... keep... aspect... ratio... */ float ratio = (float)dia_image_height(image->image)/ (float)dia_image_width(image->image); image->element.height = image->element.width * ratio; } image->file = g_strdup(new_file); image_update_data(image); return new_object_state_change((Object *)image, old_state, (GetStateFunc)image_get_state, (SetStateFunc)image_set_state); }
static void image_set_state(Image *image, ImageState *state) { image->border_width = state->border_width; image->border_color = state->border_color; image->line_style = state->line_style; if (image->image) dia_image_release(image->image); image->image = state->image; if (image->file) g_free(image->file); image->file = state->file; image->draw_border = state->draw_border; image->keep_aspect = state->keep_aspect; g_free(state); image_update_data(image); }
static void image_set_props(EImage *image, GPtrArray *props) { struct stat st; time_t mtime = 0; char *old_file = image->file ? g_strdup(image->file) : ""; object_set_props_from_offsets(&image->element.object, image_offsets, props); /* use old value on error */ if (g_stat (image->file, &st) != 0) mtime = image->mtime; else mtime = st.st_mtime; /* handle changing the image. */ if (strcmp(image->file, old_file) != 0 || image->mtime != mtime) { Element *elem = &image->element; DiaImage *img = NULL; img = dia_image_load(image->file); if (img) image->image = img; else image->image = dia_image_get_broken(); elem->height = (elem->width*(float)dia_image_height(image->image))/ (float)dia_image_width(image->image); } g_free(old_file); /* remember it */ image->mtime = mtime; register_embed_id(image->embed_id); image_update_data(image); }
static DiaObject * image_load(ObjectNode obj_node, int version, const char *filename) { EImage *image; Element *elem; DiaObject *obj; int i; AttributeNode attr; char *diafile_dir; Diagram *dia; GList *list; image = g_malloc0(sizeof(EImage)); elem = &image->element; obj = &elem->object; obj->type = &eimage_type; obj->ops = &eimage_ops; element_load(elem, obj_node); image->border_width = 0.1; attr = object_find_attribute(obj_node, "border_width"); if (attr != NULL) image->border_width = data_real( attribute_first_data(attr) ); image->border_color = color_black; attr = object_find_attribute(obj_node, "border_color"); if (attr != NULL) data_color(attribute_first_data(attr), &image->border_color); image->line_style = LINESTYLE_SOLID; attr = object_find_attribute(obj_node, "line_style"); if (attr != NULL) image->line_style = data_enum( attribute_first_data(attr) ); image->dashlength = DEFAULT_LINESTYLE_DASHLEN; attr = object_find_attribute(obj_node, "dashlength"); if (attr != NULL) image->dashlength = data_real(attribute_first_data(attr)); image->draw_border = TRUE; attr = object_find_attribute(obj_node, "draw_border"); if (attr != NULL) image->draw_border = data_boolean( attribute_first_data(attr) ); image->keep_aspect = TRUE; attr = object_find_attribute(obj_node, "keep_aspect"); if (attr != NULL) image->keep_aspect = data_boolean( attribute_first_data(attr) ); image->keep_orig_aspect = TRUE; attr = object_find_attribute(obj_node, "keep_orig_aspect"); if (attr != NULL) image->keep_orig_aspect = data_boolean( attribute_first_data(attr) ); attr = object_find_attribute(obj_node, "file"); if (attr != NULL) { image->file = data_filename( attribute_first_data(attr) ); } else { image->file = g_strdup(""); } attr = object_find_attribute(obj_node, "embed_id"); if (attr) { image->embed_id = dtree_conv_longname_from_xml(data_string(attribute_first_data(attr))); } else { image->embed_id = get_default_embed_id("embed_image"); } register_embed_id(image->embed_id); element_init(elem, 8, NUM_CONNECTIONS); for (i=0;i<NUM_CONNECTIONS;i++) { obj->connections[i] = &image->connections[i]; image->connections[i].object = obj; image->connections[i].connected = NULL; } image->connections[8].flags = CP_FLAGS_MAIN; image->image = NULL; if (strcmp(image->file, "")!=0) { diafile_dir = get_directory(filename); if (g_path_is_absolute(image->file)) { /* Absolute pathname */ image->image = dia_image_load(image->file); if (image->image == NULL) { /* Not found as abs path, try in same dir as diagram. */ char *temp_string; const char *image_file_name = image->file; const char *psep; psep = strrchr(image->file, G_DIR_SEPARATOR); /* try the other G_OS as well */ if (!psep) psep = strrchr(image->file, G_DIR_SEPARATOR == '/' ? '\\' : '/'); if (psep) image_file_name = psep + 1; temp_string = g_build_filename(diafile_dir, image_file_name, NULL); image->image = dia_image_load(temp_string); if (image->image != NULL) { /* Found file in same dir as diagram. */ message_warning(_("The image file '%s' was not found in that directory.\n" "Using the file '%s' instead\n"), image->file, temp_string); g_free(image->file); image->file = temp_string; } else { g_free(temp_string); image->image = dia_image_load((char *)image_file_name); if (image->image != NULL) { char *tmp; /* Found file in current dir. */ message_warning(_("The image file '%s' was not found in that directory.\n" "Using the file '%s' instead\n"), image->file, image_file_name); tmp = image->file; image->file = g_strdup(image_file_name); g_free(tmp); } else { message_warning(_("The image file '%s' was not found.\n"), image_file_name); } } } } else { /* Relative pathname: */ char *temp_string; temp_string = g_build_filename (diafile_dir, image->file, NULL); image->image = dia_image_load(temp_string); if (image->image != NULL) { /* Found file in same dir as diagram. */ g_free(image->file); image->file = temp_string; } else { g_free(temp_string); image->image = dia_image_load(image->file); if (image->image == NULL) { /* Didn't find file in current dir. */ message_warning(_("The image file '%s' was not found.\n"), image->file); } } } g_free(diafile_dir); } /* update mtime */ { struct stat st; if (g_stat (image->file, &st) != 0) st.st_mtime = 0; image->mtime = st.st_mtime; } image_update_data(image); obj->node = NULL; list = dia_open_diagrams(); while (list != NULL) { dia = (Diagram *)list->data; if (!g_strcmp0(dia->filename,filename)) { obj->node = dtree_set_data_by_longname(DIA_DIAGRAM_DATA(dia)->dtree, image->embed_id,&image->element.object); } list = g_list_next(list); } return &image->element.object; }
static ObjectChange* image_move_handle(EImage *image, Handle *handle, Point *to, ConnectionPoint *cp, HandleMoveReason reason, ModifierKeys modifiers) { Element *elem = &image->element; assert(image!=NULL); assert(handle!=NULL); assert(to!=NULL); if (image->keep_aspect) { float width, height; float new_width, new_height; width = image->element.width; height = image->element.height; switch (handle->id) { case HANDLE_RESIZE_NW: new_width = -(to->x-image->element.corner.x)+width; new_height = -(to->y-image->element.corner.y)+height; if (new_height == 0 || new_width/new_height > width/height) { new_height = new_width*height/width; } else { new_width = new_height*width/height; } to->x = image->element.corner.x+(image->element.width-new_width); to->y = image->element.corner.y+(image->element.height-new_height); element_move_handle(elem, HANDLE_RESIZE_NW, to, cp, reason, modifiers); break; case HANDLE_RESIZE_N: new_width = (-(to->y-image->element.corner.y)+height)*width/height; to->x = image->element.corner.x+new_width; element_move_handle(elem, HANDLE_RESIZE_NE, to, cp, reason, modifiers); break; case HANDLE_RESIZE_NE: new_width = to->x-image->element.corner.x; new_height = -(to->y-image->element.corner.y)+height; if (new_height == 0 || new_width/new_height > width/height) { new_height = new_width*height/width; } else { new_width = new_height*width/height; } to->x = image->element.corner.x+new_width; to->y = image->element.corner.y+(image->element.height-new_height); element_move_handle(elem, HANDLE_RESIZE_NE, to, cp, reason, modifiers); break; case HANDLE_RESIZE_E: new_height = (to->x-image->element.corner.x)*height/width; to->y = image->element.corner.y+new_height; element_move_handle(elem, HANDLE_RESIZE_SE, to, cp, reason, modifiers); break; case HANDLE_RESIZE_SE: new_width = to->x-image->element.corner.x; new_height = to->y-image->element.corner.y; if (new_height == 0 || new_width/new_height > width/height) { new_height = new_width*height/width; } else { new_width = new_height*width/height; } to->x = image->element.corner.x+new_width; to->y = image->element.corner.y+new_height; element_move_handle(elem, HANDLE_RESIZE_SE, to, cp, reason, modifiers); break; case HANDLE_RESIZE_S: new_width = (to->y-image->element.corner.y)*width/height; to->x = image->element.corner.x+new_width; element_move_handle(elem, HANDLE_RESIZE_SE, to, cp, reason, modifiers); break; case HANDLE_RESIZE_SW: new_width = -(to->x-image->element.corner.x)+width; new_height = to->y-image->element.corner.y; if (new_height == 0 || new_width/new_height > width/height) { new_height = new_width*height/width; } else { new_width = new_height*width/height; } to->x = image->element.corner.x+(image->element.width-new_width); to->y = image->element.corner.y+new_height; element_move_handle(elem, HANDLE_RESIZE_SW, to, cp, reason, modifiers); break; case HANDLE_RESIZE_W: new_height = (-(to->x-image->element.corner.x)+width)*height/width; to->y = image->element.corner.y+new_height; element_move_handle(elem, HANDLE_RESIZE_SW, to, cp, reason, modifiers); break; default: message_warning("Unforeseen handle in image_move_handle: %d\n", handle->id); } } else { element_move_handle(elem, handle->id, to, cp, reason, modifiers); } image_update_data(image); return NULL; }
static Object * image_load(ObjectNode obj_node, int version, const char *filename) { Image *image; Element *elem; Object *obj; int i; AttributeNode attr; char *diafile_dir; image = g_malloc(sizeof(Image)); elem = (Element *)image; obj = (Object *)image; obj->type = &image_type; obj->ops = &image_ops; element_load(elem, obj_node); image->border_width = 0.1; attr = object_find_attribute(obj_node, "border_width"); if (attr != NULL) image->border_width = data_real( attribute_first_data(attr) ); image->border_color = color_black; attr = object_find_attribute(obj_node, "border_color"); if (attr != NULL) data_color(attribute_first_data(attr), &image->border_color); image->line_style = LINESTYLE_SOLID; attr = object_find_attribute(obj_node, "line_style"); if (attr != NULL) image->line_style = data_enum( attribute_first_data(attr) ); image->draw_border = TRUE; attr = object_find_attribute(obj_node, "draw_border"); if (attr != NULL) image->draw_border = data_boolean( attribute_first_data(attr) ); image->keep_aspect = TRUE; attr = object_find_attribute(obj_node, "keep_aspect"); if (attr != NULL) image->keep_aspect = data_boolean( attribute_first_data(attr) ); attr = object_find_attribute(obj_node, "file"); if (attr != NULL) { image->file = data_string( attribute_first_data(attr) ); } else { image->file = g_strdup(""); } element_init(elem, 8, 8); for (i=0;i<8;i++) { obj->connections[i] = &image->connections[i]; image->connections[i].object = obj; image->connections[i].connected = NULL; } image->image = NULL; if (strcmp(image->file, "")!=0) { diafile_dir = get_directory(filename); if (g_path_is_absolute(image->file)) { /* Absolute pathname */ image->image = dia_image_load(image->file); if (image->image == NULL) { /* Not found as abs path, try in same dir as diagram. */ char *temp_string; const char *image_file_name; image_file_name = strrchr(image->file, G_DIR_SEPARATOR) + 1; temp_string = g_malloc(strlen(diafile_dir) + strlen(image_file_name) +1); strcpy(temp_string, diafile_dir); strcat(temp_string, image_file_name); image->image = dia_image_load(temp_string); if (image->image != NULL) { /* Found file in same dir as diagram. */ message_warning(_("The image file '%s' was not found in that directory.\n" "Using the file '%s' instead\n"), image->file, temp_string); g_free(image->file); image->file = temp_string; } else { g_free(temp_string); image->image = dia_image_load((char *)image_file_name); if (image->image != NULL) { char *tmp; /* Found file in current dir. */ message_warning(_("The image file '%s' was not found in that directory.\n" "Using the file '%s' instead\n"), image->file, image_file_name); tmp = image->file; image->file = strdup(image_file_name); g_free(tmp); } else { message_warning(_("The image file '%s' was not found.\n"), image_file_name); } } } } else { /* Relative pathname: */ char *temp_string; temp_string = g_malloc(strlen(diafile_dir) + strlen(image->file) +1); strcpy(temp_string, diafile_dir); strcat(temp_string, image->file); image->image = dia_image_load(temp_string); if (image->image != NULL) { /* Found file in same dir as diagram. */ g_free(image->file); image->file = temp_string; } else { g_free(temp_string); image->image = dia_image_load(image->file); if (image->image == NULL) { /* Didn't find file in current dir. */ message_warning(_("The image file '%s' was not found.\n"), image->file); } } } g_free(diafile_dir); } image_update_data(image); return (Object *)image; }
static DiaObject * image_load(ObjectNode obj_node, int version, DiaContext *ctx) { Image *image; Element *elem; DiaObject *obj; int i; AttributeNode attr; char *diafile_dir; image = g_malloc0(sizeof(Image)); elem = &image->element; obj = &elem->object; obj->type = &image_type; obj->ops = &image_ops; element_load(elem, obj_node, ctx); image->border_width = 0.1; attr = object_find_attribute(obj_node, "border_width"); if (attr != NULL) image->border_width = data_real(attribute_first_data(attr), ctx); image->border_color = color_black; attr = object_find_attribute(obj_node, "border_color"); if (attr != NULL) data_color(attribute_first_data(attr), &image->border_color, ctx); image->line_style = LINESTYLE_SOLID; attr = object_find_attribute(obj_node, "line_style"); if (attr != NULL) image->line_style = data_enum(attribute_first_data(attr), ctx); image->dashlength = DEFAULT_LINESTYLE_DASHLEN; attr = object_find_attribute(obj_node, "dashlength"); if (attr != NULL) image->dashlength = data_real(attribute_first_data(attr), ctx); image->draw_border = TRUE; attr = object_find_attribute(obj_node, "draw_border"); if (attr != NULL) image->draw_border = data_boolean(attribute_first_data(attr), ctx); image->keep_aspect = TRUE; attr = object_find_attribute(obj_node, "keep_aspect"); if (attr != NULL) image->keep_aspect = data_boolean(attribute_first_data(attr), ctx); attr = object_find_attribute(obj_node, "file"); if (attr != NULL) { image->file = data_filename(attribute_first_data(attr), ctx); } else { image->file = g_strdup(""); } element_init(elem, 8, NUM_CONNECTIONS); for (i=0;i<NUM_CONNECTIONS;i++) { obj->connections[i] = &image->connections[i]; image->connections[i].object = obj; image->connections[i].connected = NULL; } image->connections[8].flags = CP_FLAGS_MAIN; image->image = NULL; if (strcmp(image->file, "")!=0) { diafile_dir = get_directory(dia_context_get_filename(ctx)); if (g_path_is_absolute(image->file)) { /* Absolute pathname */ image->image = dia_image_load(image->file); if (image->image == NULL) { /* Not found as abs path, try in same dir as diagram. */ char *temp_string; const char *image_file_name = image->file; const char *psep; psep = strrchr(image->file, G_DIR_SEPARATOR); /* try the other G_OS as well */ if (!psep) psep = strrchr(image->file, G_DIR_SEPARATOR == '/' ? '\\' : '/'); if (psep) image_file_name = psep + 1; temp_string = g_build_filename(diafile_dir, image_file_name, NULL); image->image = dia_image_load(temp_string); if (image->image != NULL) { /* Found file in same dir as diagram. */ message_warning(_("The image file '%s' was not found in the specified directory.\n" "Using the file '%s' instead.\n"), image->file, temp_string); g_free(image->file); image->file = temp_string; } else { g_free(temp_string); image->image = dia_image_load((char *)image_file_name); if (image->image != NULL) { char *tmp; /* Found file in current dir. */ message_warning(_("The image file '%s' was not found in the specified directory.\n" "Using the file '%s' instead.\n"), image->file, image_file_name); tmp = image->file; image->file = g_strdup(image_file_name); g_free(tmp); } else { message_warning(_("The image file '%s' was not found.\n"), image_file_name); } } } } else { /* Relative pathname: */ char *temp_string; temp_string = g_build_filename (diafile_dir, image->file, NULL); image->image = dia_image_load(temp_string); if (image->image != NULL) { /* Found file in same dir as diagram. */ g_free(image->file); image->file = temp_string; } else { g_free(temp_string); image->image = dia_image_load(image->file); if (image->image == NULL) { /* Didn't find file in current dir. */ message_warning(_("The image file '%s' was not found.\n"), image->file); } } } g_free(diafile_dir); } /* if we don't have an image yet try to recover it from inlined data */ if (!image->image) { attr = object_find_attribute(obj_node, "pixbuf"); if (attr != NULL) { GdkPixbuf *pixbuf = data_pixbuf (attribute_first_data(attr)); if (pixbuf) { image->image = dia_image_new_from_pixbuf (pixbuf); image->inline_data = TRUE; /* avoid loosing it */ /* FIXME: should we reset the filename? */ g_object_unref (pixbuf); } } } /* update mtime */ { struct stat st; if (g_stat (image->file, &st) != 0) st.st_mtime = 0; image->mtime = st.st_mtime; } image_update_data(image); return &image->element.object; }