Пример #1
0
/*
 * diagram_show_save_as_dialog:
 *
 * Show a dialog for saving the diagram.
 */
static void
diagram_show_save_as_dialog (LdWindowMain *self)
{
	GtkWidget *dialog;

	g_return_if_fail (LD_IS_WINDOW_MAIN (self));

	dialog = gtk_file_chooser_dialog_new (_("Save As..."), GTK_WINDOW (self),
		GTK_FILE_CHOOSER_ACTION_SAVE,
		GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
		GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
		NULL);
	g_object_set (dialog, "do-overwrite-confirmation", TRUE, NULL);
	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog),
		diagram_get_file_filter ());

	if (self->priv->filename)
		gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog),
			self->priv->filename);

	while (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
	{
		gchar *filename;

		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
		if (diagram_save (self, GTK_WINDOW (dialog), filename))
		{
			diagram_set_filename (self, filename);
			break;
		}
		g_free (filename);
	}
	gtk_widget_destroy (dialog);
}
Пример #2
0
int
diagram_load_into(Diagram         *diagram,
		  const char      *filename,
		  DiaImportFilter *ifilter)
{
  /* ToDo: move context further up in the callstack and to sth useful with it's content */
  DiaContext *ctx = dia_context_new(_("Load Into"));

  gboolean was_default = diagram->is_default;
  if (!ifilter)
    ifilter = filter_guess_import_filter(filename);
  /* slightly hacked to avoid 'Not a Dia File' for .shape */
  if (!ifilter && g_str_has_suffix (filename, ".shape"))
    ifilter = filter_import_get_by_name ("dia-svg");
  if (!ifilter)  /* default to native format */
    ifilter = &dia_import_filter;

  dia_context_set_filename (ctx, filename);
  if (ifilter->import_func(filename, diagram->data, ctx, ifilter->user_data)) {
    if (ifilter != &dia_import_filter) {
      /* When loading non-Dia files, change filename to reflect that saving
       * will produce a Dia file. See bug #440093 */
      if (strcmp (diagram->filename, filename) == 0) {
	/* not a real load into but initial load */
	gchar *old_filename = g_strdup (diagram->filename);
        gchar *suffix_offset = g_utf8_strrchr(old_filename, -1, (gunichar)'.');
        gchar *new_filename;
        if (suffix_offset != NULL) {
	  new_filename = g_strndup(old_filename, suffix_offset - old_filename);
	  g_free(old_filename);
	} else {
	  new_filename = old_filename;
	}
        old_filename = g_strconcat(new_filename, ".dia", NULL);
        g_free(new_filename);
        diagram_set_filename(diagram, old_filename);
        g_free(old_filename);
        diagram->unsaved = TRUE;
	diagram_update_for_filename (diagram);
	diagram_modified(diagram);
      }
    } else {
      /* the initial diagram should have confirmed filename  */
      diagram->unsaved = 
	  strcmp(filename, diagram->filename) == 0 ? FALSE : was_default;
    }
    diagram_set_modified(diagram, TRUE);
    dia_context_release(ctx);
    return TRUE;
  } else {
    dia_context_release(ctx);
    return FALSE;
  }
}
Пример #3
0
/*
 * diagram_new:
 *
 * Create a new diagram.
 */
static void
diagram_new (LdWindowMain *self)
{
	g_return_if_fail (LD_IS_WINDOW_MAIN (self));

	if (!may_close_diagram (self, _("Save the changes to diagram \"%s\" before"
		" closing it and creating a new one?")))
		return;

	ld_diagram_clear (self->priv->diagram);
	ld_diagram_set_modified (self->priv->diagram, FALSE);

	ld_diagram_view_set_x (self->priv->view, 0);
	ld_diagram_view_set_y (self->priv->view, 0);
	ld_diagram_view_set_zoom (self->priv->view, 1);

	diagram_set_filename (self, NULL);
}
Пример #4
0
/*
 * diagram_open:
 *
 * Open a diagram from a file.
 */
static gboolean
diagram_open (LdWindowMain *self, const gchar *filename)
{
	GError *error;

	error = NULL;
	ld_diagram_load_from_file (self->priv->diagram, filename, &error);
	if (error)
	{
		GtkWidget *message_dialog;

		g_warning ("loading failed: %s", error->message);

		message_dialog = gtk_message_dialog_new (GTK_WINDOW (self),
			GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
			_("Failed to open the file"));

		if (error->domain != G_FILE_ERROR)
		{
			gchar *display_filename;

			display_filename = g_filename_display_name (filename);
			gtk_message_dialog_format_secondary_text
				(GTK_MESSAGE_DIALOG (message_dialog),
				_("Failed to open file `%s': Invalid contents."), filename);
			g_free (display_filename);
		}
		else
			gtk_message_dialog_format_secondary_text
				(GTK_MESSAGE_DIALOG (message_dialog),
				"%s", error->message);

		gtk_dialog_run (GTK_DIALOG (message_dialog));
		gtk_widget_destroy (message_dialog);

		g_error_free (error);
		return FALSE;
	}

	ld_diagram_set_modified (self->priv->diagram, FALSE);
	diagram_set_filename (self, g_strdup (filename));
	return TRUE;
}
Пример #5
0
/**
 * Respond to a button press (also destroy) in the save as dialog.
 */
static void
file_save_as_response_callback(GtkWidget *fs, 
                               gint       response, 
                               gpointer   user_data)
{
  char *filename;
  Diagram *dia;
  struct stat stat_struct;

  if (response == GTK_RESPONSE_ACCEPT) {
    dia = g_object_get_data (G_OBJECT(fs), "user_data");

    filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fs));
    if (!filename) {
      /* Not getting a filename looks like a contract violation in Gtk+ to me.
       * Still Dia would be crashing (bug #651949) - instead simply go back to the dialog. */
      gtk_window_present (GTK_WINDOW (fs));
      return;
    }

    if (g_stat(filename, &stat_struct) == 0) {
      GtkWidget *dialog = NULL;
      char *utf8filename = NULL;
      if (!g_utf8_validate(filename, -1, NULL)) {
	utf8filename = g_filename_to_utf8(filename, -1, NULL, NULL, NULL);
	if (utf8filename == NULL) {
	  message_warning(_("Some characters in the filename are neither UTF-8\n" 
			    "nor your local encoding.\nSome things will break."));
	}
      }
      if (utf8filename == NULL) utf8filename = g_strdup(filename);


      dialog = gtk_message_dialog_new (GTK_WINDOW(fs),
				       GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION,
				       GTK_BUTTONS_YES_NO,
				       _("File already exists"));
      gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
        _("The file '%s' already exists.\n"
          "Do you want to overwrite it?"), utf8filename);
      g_free(utf8filename);
      gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_YES);

      if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_YES) {
	/* don't hide/destroy the dialog, but simply go back to it */
	gtk_window_present (GTK_WINDOW (fs));
	gtk_widget_destroy(dialog);
        g_free (filename);
	return;
      }
      gtk_widget_destroy(dialog);
    }

    dia->data->is_compressed = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(user_data));

    diagram_update_extents(dia);

    {
      DiaContext *ctx = dia_context_new (_("Save as"));
      diagram_set_filename(dia, filename);
      dia_context_set_filename (ctx, filename);
      if (diagram_save(dia, filename, ctx))
	recent_file_history_add(filename);
      dia_context_release (ctx);
    }
    g_free (filename);
  }
  /* if we have our own reference, drop it before destroy */
  if ((dia = g_object_get_data (G_OBJECT(fs), "user_data")) != NULL) {
    g_object_set_data (G_OBJECT(fs), "user_data", NULL);
    g_object_unref (dia);
  }
  /* if we destroy it gtk_dialog_run wont give the response */
  if (!g_object_get_data (G_OBJECT(fs), "dont-destroy"))
    gtk_widget_destroy(GTK_WIDGET(fs));
}
Пример #6
0
static void
ld_window_main_init (LdWindowMain *self)
{
	LdWindowMainPrivate *priv;
	GError *error;

	self->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE
		(self, LD_TYPE_WINDOW_MAIN, LdWindowMainPrivate);

	/* Construct menu and toolbar. */
	priv->ui_manager = gtk_ui_manager_new ();

	g_signal_connect (priv->ui_manager, "connect-proxy",
		G_CALLBACK (on_ui_proxy_connected), self);
	g_signal_connect (priv->ui_manager, "disconnect-proxy",
		G_CALLBACK (on_ui_proxy_disconnected), self);

	priv->action_group = gtk_action_group_new ("MainActions");
	gtk_action_group_set_translation_domain
		(priv->action_group, GETTEXT_DOMAIN);
	gtk_action_group_add_actions
		(priv->action_group, wm_action_entries,
		G_N_ELEMENTS (wm_action_entries), self);
	gtk_action_group_add_toggle_actions
		(priv->action_group, wm_toggle_action_entries,
		G_N_ELEMENTS (wm_toggle_action_entries), self);
	gtk_ui_manager_insert_action_group (priv->ui_manager,
		priv->action_group, 0);

	error = NULL;
	gtk_ui_manager_add_ui_from_file
		(priv->ui_manager, PROJECT_SHARE_DIR "gui/window-main.ui", &error);
	if (error)
	{
		g_message ("building UI failed: %s", error->message);
		g_error_free (error);
	}

	priv->menu = gtk_ui_manager_get_widget (priv->ui_manager, "/MenuBar");
	priv->toolbar = gtk_ui_manager_get_widget (priv->ui_manager, "/Toolbar");

	/* Create the remaining widgets. */
	priv->library_view = ld_category_tree_view_new (NULL);

	priv->view = LD_DIAGRAM_VIEW (ld_diagram_view_new ());
	priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window),
		GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_container_add (GTK_CONTAINER (priv->scrolled_window),
		GTK_WIDGET (priv->view));

	priv->statusbar = gtk_statusbar_new ();
	priv->statusbar_menu_context_id = gtk_statusbar_get_context_id
		(GTK_STATUSBAR (priv->statusbar), "menu");
	priv->statusbar_symbol_context_id = gtk_statusbar_get_context_id
		(GTK_STATUSBAR (priv->statusbar), "symbol");
	priv->statusbar_hint_context_id = gtk_statusbar_get_context_id
		(GTK_STATUSBAR (priv->statusbar), "hint");

	priv->lv_viewport = gtk_viewport_new (NULL, NULL);
	gtk_viewport_set_shadow_type
		(GTK_VIEWPORT (priv->lv_viewport), GTK_SHADOW_NONE);
	gtk_container_add (GTK_CONTAINER (priv->lv_viewport), priv->library_view);

	priv->lv_window = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->lv_window),
		GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
	gtk_container_add (GTK_CONTAINER (priv->lv_window), priv->lv_viewport);

	priv->paned = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
	gtk_paned_pack1 (GTK_PANED (priv->paned),
		priv->lv_window, FALSE, FALSE);
	gtk_paned_pack2 (GTK_PANED (priv->paned),
		priv->scrolled_window, TRUE, TRUE);
	gtk_paned_set_position (GTK_PANED (priv->paned), 180);

	/* Pack all widgets into the window. */
	priv->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
	gtk_box_pack_start (GTK_BOX (priv->vbox), priv->menu, FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (priv->vbox), priv->toolbar, FALSE, FALSE, 0);
	gtk_box_pack_start (GTK_BOX (priv->vbox), priv->paned, TRUE, TRUE, 0);
	gtk_box_pack_end (GTK_BOX (priv->vbox), priv->statusbar, FALSE, FALSE, 0);

	gtk_container_add (GTK_CONTAINER (self), priv->vbox);

	/* Configure the window. */
	g_signal_connect (self, "delete-event", G_CALLBACK (on_delete), NULL);

	gtk_window_add_accel_group (GTK_WINDOW (self),
		gtk_ui_manager_get_accel_group (priv->ui_manager));
	gtk_window_set_default_size (GTK_WINDOW (self), 640, 440);
	gtk_window_set_position (GTK_WINDOW (self), GTK_WIN_POS_CENTER);

	/* Initialize the backend. */
	priv->diagram = ld_diagram_new ();

	g_signal_connect_after (priv->diagram, "changed",
		G_CALLBACK (on_diagram_changed), self);
	g_signal_connect (priv->diagram, "notify::can-undo",
		G_CALLBACK (on_diagram_history_changed), self);
	g_signal_connect (priv->diagram, "notify::can-redo",
		G_CALLBACK (on_diagram_history_changed), self);
	g_signal_connect_after (priv->diagram, "selection-changed",
		G_CALLBACK (on_diagram_selection_changed), self);

	priv->library = ld_library_new ();
	load_library_directories (priv->library);

	ld_diagram_view_set_diagram (priv->view, priv->diagram);
	ld_diagram_view_set_library (priv->view, priv->library);

	ld_category_view_set_category (LD_CATEGORY_VIEW (priv->library_view),
		ld_library_get_root (priv->library));

	g_signal_connect_after (priv->library_view, "symbol-selected",
		G_CALLBACK (on_symbol_selected), self);
	g_signal_connect_after (priv->library_view, "symbol-deselected",
		G_CALLBACK (on_symbol_deselected), self);

	diagram_set_filename (self, NULL);

	priv->statusbar_hint_drag = gtk_statusbar_push
		(GTK_STATUSBAR (priv->statusbar), priv->statusbar_hint_context_id,
		_("Drag symbols from the library pane to add them to the diagram."));

	priv->zoom_label = gtk_label_new ("");
	gtk_label_set_single_line_mode (GTK_LABEL (priv->zoom_label), TRUE);
	gtk_box_pack_end (GTK_BOX (gtk_statusbar_get_message_area
		(GTK_STATUSBAR (priv->statusbar))), priv->zoom_label, FALSE, FALSE, 0);

	g_signal_connect (priv->view, "notify::zoom",
		G_CALLBACK (on_view_zoom_changed), self);
	g_object_notify (G_OBJECT (priv->view), "zoom");

	action_set_sensitive (self, "Undo", FALSE);
	action_set_sensitive (self, "Redo", FALSE);
	action_set_sensitive (self, "Delete", FALSE);
	action_set_sensitive (self, "NormalSize", FALSE);

	gtk_widget_grab_focus (GTK_WIDGET (priv->view));

	/* Realize the window. */
	gtk_widget_show_all (GTK_WIDGET (self));

	/* Set up GSettings. */
	priv->settings = g_settings_new ("org." PROJECT_NAME);

	g_settings_bind (priv->settings, "show-main-toolbar",
		gtk_action_group_get_action (priv->action_group,
			"MainToolbar"), "active", G_SETTINGS_BIND_DEFAULT);
	g_settings_bind (priv->settings, "show-library-pane",
		gtk_action_group_get_action (priv->action_group,
			"LibraryPane"), "active", G_SETTINGS_BIND_DEFAULT);
	g_settings_bind (priv->settings, "show-grid",
		gtk_action_group_get_action (priv->action_group,
			"ShowGrid"), "active", G_SETTINGS_BIND_DEFAULT);
}