/* DocumentView.c generated by valac 0.56.19, the Vala compiler
 * generated from DocumentView.vala, do not modify */

/* -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-*/

#include "codecore.h"
#include <glib.h>
#include <handy.h>
#include <gtk/gtk.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include <granite.h>
#include <gtksourceview/gtksource.h>
#include <glib/gi18n-lib.h>
#include <gdk/gdk.h>

#define SCRATCH_WIDGETS_DOCUMENT_VIEW_TAB_HISTORY_MAX_ITEMS 20
#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	SCRATCH_WIDGETS_DOCUMENT_VIEW_0_PROPERTY,
	SCRATCH_WIDGETS_DOCUMENT_VIEW_CURRENT_DOCUMENT_PROPERTY,
	SCRATCH_WIDGETS_DOCUMENT_VIEW_WINDOW_PROPERTY,
	SCRATCH_WIDGETS_DOCUMENT_VIEW_OUTLINE_VISIBLE_PROPERTY,
	SCRATCH_WIDGETS_DOCUMENT_VIEW_OUTLINE_WIDTH_PROPERTY,
	SCRATCH_WIDGETS_DOCUMENT_VIEW_NUM_PROPERTIES
};
static GParamSpec* scratch_widgets_document_view_properties[SCRATCH_WIDGETS_DOCUMENT_VIEW_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _Block56Data Block56Data;
typedef struct _ScratchWidgetsDocumentViewOpenDocumentData ScratchWidgetsDocumentViewOpenDocumentData;
typedef struct _Block57Data Block57Data;
#define _g_variant_builder_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_builder_unref (var), NULL)))
#define _g_variant_type_free0(var) ((var == NULL) ? NULL : (var = (g_variant_type_free (var), NULL)))
#define _g_variant_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_unref (var), NULL)))
#define _g_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))
typedef struct _Block58Data Block58Data;
enum  {
	SCRATCH_WIDGETS_DOCUMENT_VIEW_DOCUMENT_CHANGE_SIGNAL,
	SCRATCH_WIDGETS_DOCUMENT_VIEW_REQUEST_PLACEHOLDER_SIGNAL,
	SCRATCH_WIDGETS_DOCUMENT_VIEW_TAB_ADDED_SIGNAL,
	SCRATCH_WIDGETS_DOCUMENT_VIEW_TAB_REMOVED_SIGNAL,
	SCRATCH_WIDGETS_DOCUMENT_VIEW_NUM_SIGNALS
};
static guint scratch_widgets_document_view_signals[SCRATCH_WIDGETS_DOCUMENT_VIEW_NUM_SIGNALS] = {0};

struct _ScratchWidgetsDocumentViewPrivate {
	ScratchServicesDocument* _current_document;
	ScratchMainWindow* _window;
	gboolean _outline_visible;
	gint _outline_width;
	HdyTabView* tab_view;
	HdyTabBar* tab_bar;
	HdyTabPage* tab_menu_target;
	GtkCssProvider* style_provider;
	GtkMenuButton* tab_history_button;
};

struct _Block56Data {
	int _ref_count_;
	ScratchWidgetsDocumentView* self;
	SelectionRange range;
	gpointer _async_data_;
};

struct _ScratchWidgetsDocumentViewOpenDocumentData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	ScratchWidgetsDocumentView* self;
	ScratchServicesDocument* doc;
	gboolean focus;
	gint cursor_position;
	SelectionRange range;
	Block56Data* _data56_;
	SelectionRange _tmp0_;
	gint n;
	gboolean _tmp1_;
	gint _tmp2_;
	GList* _tmp3_;
	ScratchServicesDocument* nth_doc;
	GList* _tmp4_;
	gconstpointer _tmp5_;
	ScratchServicesDocument* _tmp6_;
	ScratchServicesDocument* _tmp7_;
	gboolean _tmp8_;
	ScratchServicesDocument* _tmp9_;
	GFile* _tmp10_;
	GFile* _tmp11_;
	ScratchServicesDocument* _tmp12_;
	GFile* _tmp13_;
	GFile* _tmp14_;
	gchar* _tmp15_;
	gchar* _tmp16_;
	GFile* _tmp17_;
	GFile* _tmp18_;
	gchar* _tmp19_;
	gchar* _tmp20_;
	ScratchServicesDocument* _tmp21_;
	SelectionRange _tmp22_;
	GList* _tmp23_;
	gboolean _tmp24_;
	ScratchServicesDocument* _tmp25_;
	ScratchServicesDocument* _tmp26_;
	SelectionRange _tmp27_;
	ScratchWidgetsSourceView* _tmp28_;
	ScratchWidgetsSourceView* _tmp29_;
	SelectionRange _tmp30_;
	ScratchWidgetsSourceView* _tmp31_;
	ScratchWidgetsSourceView* _tmp32_;
};

struct _Block57Data {
	int _ref_count_;
	ScratchWidgetsDocumentView* self;
	GVariantBuilder* vb;
};

struct _Block58Data {
	int _ref_count_;
	ScratchWidgetsDocumentView* self;
	ScratchServicesDocument* doc;
	HdyTabPage* tab;
};

static gint ScratchWidgetsDocumentView_private_offset;
static gpointer scratch_widgets_document_view_parent_class = NULL;

static void _g_object_unref0_ (gpointer var);
static inline void _g_list_free__g_object_unref0_ (GList* self);
static void scratch_widgets_document_view_update_inline_tab_colors (ScratchWidgetsDocumentView* self);
static gboolean _vala_string_array_contains (gchar* * stack,
                                      gssize stack_length,
                                      const gchar* needle);
static gchar* scratch_widgets_document_view_unsaved_duplicated_file_path_builder (ScratchWidgetsDocumentView* self,
                                                                           const gchar* original_filename);
static void scratch_widgets_document_view_insert_document (ScratchWidgetsDocumentView* self,
                                                    ScratchServicesDocument* doc,
                                                    gint pos);
static gchar* scratch_widgets_document_view_unsaved_file_path_builder (ScratchWidgetsDocumentView* self,
                                                                const gchar* extension);
static void scratch_widgets_document_view_open_document_data_free (gpointer _data);
static gboolean scratch_widgets_document_view_open_document_co (ScratchWidgetsDocumentViewOpenDocumentData* _data_);
static Block56Data* block56_data_ref (Block56Data* _data56_);
static void block56_data_unref (void * _userdata_);
static gboolean _selection_range_equal (const SelectionRange * s1,
                                 const SelectionRange * s2);
static gboolean _______lambda64_ (Block56Data* _data56_);
static gboolean ________lambda64__gsource_func (gpointer self);
static void scratch_widgets_document_view_open_document_ready (GObject* source_object,
                                                        GAsyncResult* _res_,
                                                        gpointer _user_data_);
static Block57Data* block57_data_ref (Block57Data* _data57_);
static void block57_data_unref (void * _userdata_);
static void ___lambda65_ (Block57Data* _data57_,
                   ScratchServicesDocument* doc);
static void ____lambda65__gfunc (gconstpointer data,
                          gpointer self);
static void __lambda66_ (ScratchWidgetsDocumentView* self,
                  ScratchServicesDocument* doc);
static void ___lambda66__gfunc (gconstpointer data,
                         gpointer self);
static void scratch_widgets_document_view_tab_view_setup_menu (ScratchWidgetsDocumentView* self,
                                                        HdyTabPage* page);
static void scratch_widgets_document_view_on_page_detached (ScratchWidgetsDocumentView* self,
                                                     HdyTabPage* tab,
                                                     gint position);
static void scratch_widgets_document_view_rename_tabs_with_same_title (ScratchWidgetsDocumentView* self,
                                                                ScratchServicesDocument* doc);
static void scratch_widgets_document_view_on_doc_reordered (ScratchWidgetsDocumentView* self,
                                                     HdyTabPage* tab,
                                                     gint new_position);
static HdyTabView* scratch_widgets_document_view_on_doc_to_new_window (ScratchWidgetsDocumentView* self,
                                                                HdyTabView* tab_view);
static void scratch_widgets_document_view_on_doc_added (ScratchWidgetsDocumentView* self,
                                                 HdyTabPage* page,
                                                 gint position);
static void scratch_widgets_document_view_save_focused_document_uri (ScratchWidgetsDocumentView* self,
                                                              ScratchServicesDocument* current_document);
static GMenu* scratch_widgets_document_view_create_menu_model (ScratchWidgetsDocumentView* self);
static void scratch_widgets_document_view_drag_received (ScratchWidgetsDocumentView* self,
                                                  GtkWidget* w,
                                                  GdkDragContext* ctx,
                                                  gint x,
                                                  gint y,
                                                  GtkSelectionData* sel,
                                                  guint info,
                                                  guint time);
static GObject * scratch_widgets_document_view_constructor (GType type,
                                                     guint n_construct_properties,
                                                     GObjectConstructParam * construct_properties);
static void _scratch_widgets_document_view_tab_view_setup_menu_hdy_tab_view_setup_menu (HdyTabView* _sender,
                                                                                 HdyTabPage* page,
                                                                                 gpointer self);
static void _scratch_widgets_document_view___lambda67_ (ScratchWidgetsDocumentView* self);
static void __scratch_widgets_document_view___lambda67__g_object_notify (GObject* _sender,
                                                                  GParamSpec* pspec,
                                                                  gpointer self);
static void _scratch_widgets_document_view___lambda68_ (ScratchWidgetsDocumentView* self);
static void __scratch_widgets_document_view___lambda68__gtk_button_clicked (GtkButton* _sender,
                                                                     gpointer self);
static gboolean _scratch_widgets_document_view___lambda69_ (ScratchWidgetsDocumentView* self,
                                                     HdyTabPage* tab);
static Block58Data* block58_data_ref (Block58Data* _data58_);
static void block58_data_unref (void * _userdata_);
static void ___lambda70_ (Block58Data* _data58_,
                   GObject* obj,
                   GAsyncResult* res);
static void ____lambda70__gasync_ready_callback (GObject* source_object,
                                          GAsyncResult* res,
                                          gpointer self);
static gboolean __scratch_widgets_document_view___lambda69__hdy_tab_view_close_page (HdyTabView* _sender,
                                                                              HdyTabPage* page,
                                                                              gpointer self);
static void _scratch_widgets_document_view_on_doc_added_hdy_tab_view_page_attached (HdyTabView* _sender,
                                                                             HdyTabPage* page,
                                                                             gint position,
                                                                             gpointer self);
static void _scratch_widgets_document_view_on_page_detached_hdy_tab_view_page_detached (HdyTabView* _sender,
                                                                                 HdyTabPage* page,
                                                                                 gint position,
                                                                                 gpointer self);
static void _scratch_widgets_document_view_on_doc_reordered_hdy_tab_view_page_reordered (HdyTabView* _sender,
                                                                                  HdyTabPage* page,
                                                                                  gint position,
                                                                                  gpointer self);
static HdyTabView* _scratch_widgets_document_view_on_doc_to_new_window_hdy_tab_view_create_window (HdyTabView* _sender,
                                                                                            gpointer self);
static void _scratch_widgets_document_view_update_inline_tab_colors_g_settings_changed (GSettings* _sender,
                                                                                 const gchar* key,
                                                                                 gpointer self);
static void _scratch_widgets_document_view_update_inline_tab_colors_g_object_notify (GObject* _sender,
                                                                              GParamSpec* pspec,
                                                                              gpointer self);
static void _scratch_widgets_document_view_update_outline_visible_g_object_notify (GObject* _sender,
                                                                            GParamSpec* pspec,
                                                                            gpointer self);
static void _scratch_widgets_document_view___lambda71_ (ScratchWidgetsDocumentView* self);
static void __scratch_widgets_document_view___lambda71__g_object_notify (GObject* _sender,
                                                                  GParamSpec* pspec,
                                                                  gpointer self);
static void _scratch_widgets_document_view_drag_received_gtk_widget_drag_data_received (GtkWidget* _sender,
                                                                                 GdkDragContext* context,
                                                                                 gint x,
                                                                                 gint y,
                                                                                 GtkSelectionData* selection_data,
                                                                                 guint info,
                                                                                 guint time_,
                                                                                 gpointer self);
static void scratch_widgets_document_view_finalize (GObject * obj);
static GType scratch_widgets_document_view_get_type_once (void);
static void _vala_scratch_widgets_document_view_get_property (GObject * object,
                                                       guint property_id,
                                                       GValue * value,
                                                       GParamSpec * pspec);
static void _vala_scratch_widgets_document_view_set_property (GObject * object,
                                                       guint property_id,
                                                       const GValue * value,
                                                       GParamSpec * pspec);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static gssize _vala_array_length (gpointer array);

VALA_EXTERN const SelectionRange SELECTION_RANGE_EMPTY;

static inline gpointer
scratch_widgets_document_view_get_instance_private (ScratchWidgetsDocumentView* self)
{
	return G_STRUCT_MEMBER_P (self, ScratchWidgetsDocumentView_private_offset);
}

 G_GNUC_NO_INLINE static GType
scratch_widgets_document_view_target_type_get_type_once (void)
{
	static const GEnumValue values[] = {{SCRATCH_WIDGETS_DOCUMENT_VIEW_TARGET_TYPE_URI_LIST, "SCRATCH_WIDGETS_DOCUMENT_VIEW_TARGET_TYPE_URI_LIST", "uri-list"}, {0, NULL, NULL}};
	GType scratch_widgets_document_view_target_type_type_id;
	scratch_widgets_document_view_target_type_type_id = g_enum_register_static ("ScratchWidgetsDocumentViewTargetType", values);
	return scratch_widgets_document_view_target_type_type_id;
}

GType
scratch_widgets_document_view_target_type_get_type (void)
{
	static gsize scratch_widgets_document_view_target_type_type_id__once = 0;
	if (g_once_init_enter (&scratch_widgets_document_view_target_type_type_id__once)) {
		GType scratch_widgets_document_view_target_type_type_id;
		scratch_widgets_document_view_target_type_type_id = scratch_widgets_document_view_target_type_get_type_once ();
		g_once_init_leave (&scratch_widgets_document_view_target_type_type_id__once, scratch_widgets_document_view_target_type_type_id);
	}
	return scratch_widgets_document_view_target_type_type_id__once;
}

static void
_g_object_unref0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (g_object_unref (var), NULL));
}

static inline void
_g_list_free__g_object_unref0_ (GList* self)
{
	g_list_free_full (self, (GDestroyNotify) _g_object_unref0_);
}

ScratchWidgetsDocumentView*
scratch_widgets_document_view_construct (GType object_type,
                                         ScratchMainWindow* window)
{
	ScratchWidgetsDocumentView * self = NULL;
	g_return_val_if_fail (window != NULL, NULL);
	self = (ScratchWidgetsDocumentView*) g_object_new (object_type, "window", window, "orientation", GTK_ORIENTATION_VERTICAL, "hexpand", TRUE, "vexpand", TRUE, NULL);
	return self;
}

ScratchWidgetsDocumentView*
scratch_widgets_document_view_new (ScratchMainWindow* window)
{
	return scratch_widgets_document_view_construct (SCRATCH_WIDGETS_TYPE_DOCUMENT_VIEW, window);
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static gboolean
_vala_string_array_contains (gchar* * stack,
                             gssize stack_length,
                             const gchar* needle)
{
	gssize i;
	for (i = 0; i < stack_length; i++) {
		if (g_strcmp0 (stack[i], needle) == 0) {
			return TRUE;
		}
	}
	return FALSE;
}

static void
scratch_widgets_document_view_update_inline_tab_colors (ScratchWidgetsDocumentView* self)
{
	gchar* style_scheme = NULL;
	gchar* _tmp0_;
	GSettings* _tmp1_;
	GtkSourceStyleSchemeManager* sssm = NULL;
	GtkSourceStyleSchemeManager* _tmp9_;
	GtkSourceStyleSchemeManager* _tmp10_;
	const gchar* _tmp11_;
	GtkSourceStyleSchemeManager* _tmp12_;
	gchar** _tmp13_;
	gint _tmp13__length1;
	gchar** _tmp14_;
	gint _tmp14__length1;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = g_strdup ("");
	style_scheme = _tmp0_;
	_tmp1_ = scratch_settings;
	if (g_settings_get_boolean (_tmp1_, "follow-system-style")) {
		gboolean system_prefers_dark = FALSE;
		GraniteSettings* _tmp2_;
		GraniteSettingsColorScheme _tmp3_;
		GraniteSettingsColorScheme _tmp4_;
		_tmp2_ = granite_settings_get_default ();
		_tmp3_ = granite_settings_get_prefers_color_scheme (_tmp2_);
		_tmp4_ = _tmp3_;
		system_prefers_dark = _tmp4_ == GRANITE_SETTINGS_COLOR_SCHEME_DARK;
		if (system_prefers_dark) {
			gchar* _tmp5_;
			_tmp5_ = g_strdup ("elementary-dark");
			_g_free0 (style_scheme);
			style_scheme = _tmp5_;
		} else {
			gchar* _tmp6_;
			_tmp6_ = g_strdup ("elementary-light");
			_g_free0 (style_scheme);
			style_scheme = _tmp6_;
		}
	} else {
		GSettings* _tmp7_;
		gchar* _tmp8_;
		_tmp7_ = scratch_settings;
		_tmp8_ = g_settings_get_string (_tmp7_, "style-scheme");
		_g_free0 (style_scheme);
		style_scheme = _tmp8_;
	}
	_tmp9_ = gtk_source_style_scheme_manager_get_default ();
	_tmp10_ = _g_object_ref0 (_tmp9_);
	sssm = _tmp10_;
	_tmp11_ = style_scheme;
	_tmp12_ = sssm;
	_tmp13_ = gtk_source_style_scheme_manager_get_scheme_ids (_tmp12_);
	_tmp13__length1 = _vala_array_length (_tmp13_);
	_tmp14_ = _tmp13_;
	_tmp14__length1 = _tmp13__length1;
	if (_vala_string_array_contains (_tmp14_, _tmp14__length1, _tmp11_)) {
		GtkSourceStyleScheme* theme = NULL;
		GtkSourceStyleSchemeManager* _tmp15_;
		const gchar* _tmp16_;
		GtkSourceStyleScheme* _tmp17_;
		GtkSourceStyleScheme* _tmp18_;
		GtkSourceStyle* text_color_data = NULL;
		GtkSourceStyleScheme* _tmp19_;
		GtkSourceStyle* _tmp20_;
		GtkSourceStyle* _tmp21_;
		gchar* color = NULL;
		gchar* _tmp22_;
		GtkSourceStyle* _tmp23_;
		gchar* define = NULL;
		const gchar* _tmp27_;
		gchar* _tmp28_;
		_tmp15_ = sssm;
		_tmp16_ = style_scheme;
		_tmp17_ = gtk_source_style_scheme_manager_get_scheme (_tmp15_, _tmp16_);
		_tmp18_ = _g_object_ref0 (_tmp17_);
		theme = _tmp18_;
		_tmp19_ = theme;
		_tmp20_ = gtk_source_style_scheme_get_style (_tmp19_, "text");
		_tmp21_ = _g_object_ref0 (_tmp20_);
		text_color_data = _tmp21_;
		_tmp22_ = g_strdup ("#FFFFFF");
		color = _tmp22_;
		_tmp23_ = text_color_data;
		if (_tmp23_ != NULL) {
			GtkSourceStyle* _tmp24_;
			gchar* _tmp25_;
			gchar* _tmp26_;
			_tmp24_ = text_color_data;
			g_object_get (_tmp24_, "background", &_tmp25_, NULL);
			_tmp26_ = _tmp25_;
			_g_free0 (color);
			color = _tmp26_;
		}
		_tmp27_ = color;
		_tmp28_ = g_strdup_printf ("@define-color tab_base_color %s;", _tmp27_);
		define = _tmp28_;
		{
			GtkCssProvider* _tmp29_;
			const gchar* _tmp30_;
			_tmp29_ = self->priv->style_provider;
			_tmp30_ = define;
			gtk_css_provider_load_from_data (_tmp29_, _tmp30_, (gssize) -1, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
		}
		goto __finally0;
		__catch0_g_error:
		{
			g_clear_error (&_inner_error0_);
			g_critical ("DocumentView.vala:211: Unable to set inline tab styling, going back to" \
" classic notebook tabs");
		}
		__finally0:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_free0 (define);
			_g_free0 (color);
			_g_object_unref0 (text_color_data);
			_g_object_unref0 (theme);
			_g_object_unref0 (sssm);
			_g_free0 (style_scheme);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
		_g_free0 (define);
		_g_free0 (color);
		_g_object_unref0 (text_color_data);
		_g_object_unref0 (theme);
	}
	_g_object_unref0 (sssm);
	_g_free0 (style_scheme);
}

void
scratch_widgets_document_view_transfer_tab_to_new_window (ScratchWidgetsDocumentView* self)
{
	HdyTabPage* _tmp0_ = NULL;
	HdyTabPage* _tmp1_;
	HdyTabPage* target = NULL;
	HdyTabPage* _tmp5_;
	HdyTabPage* _tmp6_;
	ScratchMainWindow* new_window = NULL;
	ScratchMainWindow* _tmp7_;
	HdyTabView* _tmp8_;
	HdyTabPage* _tmp9_;
	ScratchMainWindow* _tmp10_;
	ScratchWidgetsDocumentView* _tmp11_;
	ScratchWidgetsDocumentView* _tmp12_;
	HdyTabView* _tmp13_;
	g_return_if_fail (self != NULL);
	_tmp1_ = self->priv->tab_menu_target;
	_tmp0_ = _tmp1_;
	if (_tmp0_ == NULL) {
		HdyTabView* _tmp2_;
		HdyTabPage* _tmp3_;
		HdyTabPage* _tmp4_;
		_tmp2_ = self->priv->tab_view;
		_tmp3_ = hdy_tab_view_get_selected_page (_tmp2_);
		_tmp4_ = _tmp3_;
		_tmp0_ = _tmp4_;
	}
	_tmp5_ = _g_object_ref0 (_tmp0_);
	target = _tmp5_;
	_tmp6_ = target;
	if (_tmp6_ == NULL) {
		_g_object_unref0 (target);
		return;
	}
	_tmp7_ = scratch_main_window_new (FALSE);
	g_object_ref_sink (_tmp7_);
	new_window = _tmp7_;
	_tmp8_ = self->priv->tab_view;
	_tmp9_ = target;
	_tmp10_ = new_window;
	_tmp11_ = scratch_main_window_get_document_view (_tmp10_);
	_tmp12_ = _tmp11_;
	_tmp13_ = _tmp12_->priv->tab_view;
	hdy_tab_view_transfer_page (_tmp8_, _tmp9_, _tmp13_, 0);
	_g_object_unref0 (new_window);
	_g_object_unref0 (target);
}

void
scratch_widgets_document_view_close_document (ScratchWidgetsDocumentView* self,
                                              ScratchServicesDocument* doc)
{
	HdyTabPage* _tmp3_ = NULL;
	HdyTabPage* _tmp4_;
	HdyTabPage* target = NULL;
	HdyTabPage* _tmp8_;
	HdyTabPage* _tmp9_;
	HdyTabView* _tmp10_;
	HdyTabPage* _tmp11_;
	g_return_if_fail (self != NULL);
	if (doc != NULL) {
		HdyTabView* _tmp0_;
		HdyTabPage* _tmp1_;
		HdyTabPage* _tmp2_;
		_tmp0_ = self->priv->tab_view;
		_tmp1_ = scratch_services_document_get_tab (doc);
		_tmp2_ = _tmp1_;
		hdy_tab_view_close_page (_tmp0_, _tmp2_);
		return;
	}
	_tmp4_ = self->priv->tab_menu_target;
	_tmp3_ = _tmp4_;
	if (_tmp3_ == NULL) {
		HdyTabView* _tmp5_;
		HdyTabPage* _tmp6_;
		HdyTabPage* _tmp7_;
		_tmp5_ = self->priv->tab_view;
		_tmp6_ = hdy_tab_view_get_selected_page (_tmp5_);
		_tmp7_ = _tmp6_;
		_tmp3_ = _tmp7_;
	}
	_tmp8_ = _g_object_ref0 (_tmp3_);
	target = _tmp8_;
	_tmp9_ = target;
	if (_tmp9_ == NULL) {
		_g_object_unref0 (target);
		return;
	}
	_tmp10_ = self->priv->tab_view;
	_tmp11_ = target;
	hdy_tab_view_close_page (_tmp10_, _tmp11_);
	_g_object_unref0 (target);
}

void
scratch_widgets_document_view_close_tabs_to_right (ScratchWidgetsDocumentView* self)
{
	HdyTabPage* _tmp0_ = NULL;
	HdyTabPage* _tmp1_;
	HdyTabPage* target = NULL;
	HdyTabPage* _tmp5_;
	HdyTabPage* _tmp6_;
	ScratchServicesDocument* _tmp7_;
	ScratchServicesDocument* _tmp8_;
	g_return_if_fail (self != NULL);
	_tmp1_ = self->priv->tab_menu_target;
	_tmp0_ = _tmp1_;
	if (_tmp0_ == NULL) {
		HdyTabView* _tmp2_;
		HdyTabPage* _tmp3_;
		HdyTabPage* _tmp4_;
		_tmp2_ = self->priv->tab_view;
		_tmp3_ = hdy_tab_view_get_selected_page (_tmp2_);
		_tmp4_ = _tmp3_;
		_tmp0_ = _tmp4_;
	}
	_tmp5_ = _g_object_ref0 (_tmp0_);
	target = _tmp5_;
	_tmp6_ = target;
	if (_tmp6_ == NULL) {
		_g_object_unref0 (target);
		return;
	}
	_tmp7_ = scratch_widgets_document_view_get_current_document (self);
	_tmp8_ = _tmp7_;
	if (_tmp8_ != NULL) {
		HdyTabView* _tmp9_;
		HdyTabPage* _tmp10_;
		_tmp9_ = self->priv->tab_view;
		_tmp10_ = target;
		hdy_tab_view_close_pages_after (_tmp9_, _tmp10_);
	}
	_g_object_unref0 (target);
}

void
scratch_widgets_document_view_close_other_tabs (ScratchWidgetsDocumentView* self)
{
	HdyTabPage* _tmp0_ = NULL;
	HdyTabPage* _tmp1_;
	HdyTabPage* target = NULL;
	HdyTabPage* _tmp5_;
	HdyTabPage* _tmp6_;
	ScratchServicesDocument* _tmp7_;
	ScratchServicesDocument* _tmp8_;
	g_return_if_fail (self != NULL);
	_tmp1_ = self->priv->tab_menu_target;
	_tmp0_ = _tmp1_;
	if (_tmp0_ == NULL) {
		HdyTabView* _tmp2_;
		HdyTabPage* _tmp3_;
		HdyTabPage* _tmp4_;
		_tmp2_ = self->priv->tab_view;
		_tmp3_ = hdy_tab_view_get_selected_page (_tmp2_);
		_tmp4_ = _tmp3_;
		_tmp0_ = _tmp4_;
	}
	_tmp5_ = _g_object_ref0 (_tmp0_);
	target = _tmp5_;
	_tmp6_ = target;
	if (_tmp6_ == NULL) {
		_g_object_unref0 (target);
		return;
	}
	_tmp7_ = scratch_widgets_document_view_get_current_document (self);
	_tmp8_ = _tmp7_;
	if (_tmp8_ != NULL) {
		HdyTabView* _tmp9_;
		HdyTabPage* _tmp10_;
		_tmp9_ = self->priv->tab_view;
		_tmp10_ = target;
		hdy_tab_view_close_other_pages (_tmp9_, _tmp10_);
	}
	_g_object_unref0 (target);
}

void
scratch_widgets_document_view_duplicate_tab (ScratchWidgetsDocumentView* self)
{
	HdyTabPage* _tmp0_ = NULL;
	HdyTabPage* _tmp1_;
	HdyTabPage* target = NULL;
	HdyTabPage* _tmp5_;
	HdyTabPage* _tmp6_;
	ScratchServicesDocument* original_doc = NULL;
	HdyTabPage* _tmp7_;
	GtkWidget* _tmp8_;
	ScratchServicesDocument* _tmp9_;
	ScratchServicesDocument* _tmp10_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp1_ = self->priv->tab_menu_target;
	_tmp0_ = _tmp1_;
	if (_tmp0_ == NULL) {
		HdyTabView* _tmp2_;
		HdyTabPage* _tmp3_;
		HdyTabPage* _tmp4_;
		_tmp2_ = self->priv->tab_view;
		_tmp3_ = hdy_tab_view_get_selected_page (_tmp2_);
		_tmp4_ = _tmp3_;
		_tmp0_ = _tmp4_;
	}
	_tmp5_ = _g_object_ref0 (_tmp0_);
	target = _tmp5_;
	_tmp6_ = target;
	if (_tmp6_ == NULL) {
		_g_object_unref0 (target);
		return;
	}
	_tmp7_ = target;
	_tmp8_ = hdy_tab_page_get_child (_tmp7_);
	_tmp9_ = _g_object_ref0 (SCRATCH_SERVICES_IS_DOCUMENT (_tmp8_) ? ((ScratchServicesDocument*) _tmp8_) : NULL);
	original_doc = _tmp9_;
	_tmp10_ = original_doc;
	if (_tmp10_ == NULL) {
		_g_object_unref0 (original_doc);
		_g_object_unref0 (target);
		return;
	}
	{
		GFile* file = NULL;
		ScratchServicesDocument* _tmp11_;
		GFile* _tmp12_;
		GFile* _tmp13_;
		gchar* _tmp14_;
		gchar* _tmp15_;
		gchar* _tmp16_;
		gchar* _tmp17_;
		GFile* _tmp18_;
		GFile* _tmp19_;
		GFile* _tmp20_;
		GFileOutputStream* _tmp21_;
		GFileOutputStream* _tmp22_;
		ScratchServicesDocument* doc = NULL;
		ScratchMainWindow* _tmp23_;
		GSimpleActionGroup* _tmp24_;
		GSimpleActionGroup* _tmp25_;
		GFile* _tmp26_;
		ScratchServicesDocument* _tmp27_;
		ScratchServicesDocument* _tmp28_;
		ScratchWidgetsSourceView* _tmp29_;
		ScratchWidgetsSourceView* _tmp30_;
		ScratchServicesDocument* _tmp31_;
		gchar* _tmp32_;
		gchar* _tmp33_;
		ScratchServicesDocument* _tmp34_;
		ScratchWidgetsSourceView* _tmp35_;
		ScratchWidgetsSourceView* _tmp36_;
		ScratchServicesDocument* _tmp37_;
		ScratchWidgetsSourceView* _tmp38_;
		ScratchWidgetsSourceView* _tmp39_;
		GtkSourceLanguage* _tmp40_;
		GtkSourceLanguage* _tmp41_;
		GSettings* _tmp42_;
		ScratchServicesDocument* _tmp44_;
		HdyTabView* _tmp45_;
		HdyTabPage* _tmp46_;
		ScratchServicesDocument* _tmp47_;
		ScratchServicesDocument* _tmp48_;
		_tmp11_ = original_doc;
		_tmp12_ = scratch_services_document_get_file (_tmp11_);
		_tmp13_ = _tmp12_;
		_tmp14_ = g_file_get_basename (_tmp13_);
		_tmp15_ = _tmp14_;
		_tmp16_ = scratch_widgets_document_view_unsaved_duplicated_file_path_builder (self, _tmp15_);
		_tmp17_ = _tmp16_;
		_tmp18_ = g_file_new_for_path (_tmp17_);
		_tmp19_ = _tmp18_;
		_g_free0 (_tmp17_);
		_g_free0 (_tmp15_);
		file = _tmp19_;
		_tmp20_ = file;
		_tmp21_ = g_file_create (_tmp20_, G_FILE_CREATE_PRIVATE, NULL, &_inner_error0_);
		_tmp22_ = _tmp21_;
		_g_object_unref0 (_tmp22_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (file);
			goto __catch0_g_error;
		}
		_tmp23_ = self->priv->_window;
		_tmp24_ = scratch_main_window_get_actions (_tmp23_);
		_tmp25_ = _tmp24_;
		_tmp26_ = file;
		_tmp27_ = scratch_services_document_new (_tmp25_, _tmp26_);
		g_object_ref_sink (_tmp27_);
		doc = _tmp27_;
		_tmp28_ = doc;
		_tmp29_ = scratch_services_document_get_source_view (_tmp28_);
		_tmp30_ = _tmp29_;
		_tmp31_ = original_doc;
		_tmp32_ = scratch_services_document_get_text (_tmp31_);
		_tmp33_ = _tmp32_;
		scratch_widgets_source_view_set_text (_tmp30_, _tmp33_, TRUE);
		_g_free0 (_tmp33_);
		_tmp34_ = doc;
		_tmp35_ = scratch_services_document_get_source_view (_tmp34_);
		_tmp36_ = _tmp35_;
		_tmp37_ = original_doc;
		_tmp38_ = scratch_services_document_get_source_view (_tmp37_);
		_tmp39_ = _tmp38_;
		_tmp40_ = scratch_widgets_source_view_get_language (_tmp39_);
		_tmp41_ = _tmp40_;
		scratch_widgets_source_view_set_language (_tmp36_, _tmp41_);
		_tmp42_ = scratch_settings;
		if (g_settings_get_boolean (_tmp42_, "autosave")) {
			ScratchServicesDocument* _tmp43_;
			_tmp43_ = doc;
			scratch_services_document_save_with_hold (_tmp43_, TRUE, FALSE, NULL, NULL);
		}
		_tmp44_ = doc;
		_tmp45_ = self->priv->tab_view;
		_tmp46_ = target;
		scratch_widgets_document_view_insert_document (self, _tmp44_, hdy_tab_view_get_page_position (_tmp45_, _tmp46_) + 1);
		_tmp47_ = doc;
		scratch_widgets_document_view_set_current_document (self, _tmp47_);
		_tmp48_ = doc;
		scratch_services_document_focus (_tmp48_);
		_g_object_unref0 (doc);
		_g_object_unref0 (file);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		ScratchServicesDocument* _tmp49_;
		gchar* _tmp50_;
		gchar* _tmp51_;
		GError* _tmp52_;
		const gchar* _tmp53_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp49_ = original_doc;
		_tmp50_ = scratch_services_document_get_basename (_tmp49_);
		_tmp51_ = _tmp50_;
		_tmp52_ = e;
		_tmp53_ = _tmp52_->message;
		g_warning ("DocumentView.vala:291: Cannot copy \"%s\": %s", _tmp51_, _tmp53_);
		_g_free0 (_tmp51_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (original_doc);
		_g_object_unref0 (target);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	_g_object_unref0 (original_doc);
	_g_object_unref0 (target);
}

void
scratch_widgets_document_view_new_document (ScratchWidgetsDocumentView* self)
{
	GFile* file = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	GFile* _tmp2_;
	GFile* _tmp3_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = scratch_widgets_document_view_unsaved_file_path_builder (self, "txt");
	_tmp1_ = _tmp0_;
	_tmp2_ = g_file_new_for_path (_tmp1_);
	_tmp3_ = _tmp2_;
	_g_free0 (_tmp1_);
	file = _tmp3_;
	{
		GFileOutputStream* _tmp4_;
		GFileOutputStream* _tmp5_;
		ScratchServicesDocument* doc = NULL;
		ScratchMainWindow* _tmp6_;
		GSimpleActionGroup* _tmp7_;
		GSimpleActionGroup* _tmp8_;
		ScratchServicesDocument* _tmp9_;
		ScratchServicesDocument* _tmp10_;
		_tmp4_ = g_file_create (file, G_FILE_CREATE_PRIVATE, NULL, &_inner_error0_);
		_tmp5_ = _tmp4_;
		_g_object_unref0 (_tmp5_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp6_ = self->priv->_window;
		_tmp7_ = scratch_main_window_get_actions (_tmp6_);
		_tmp8_ = _tmp7_;
		_tmp9_ = scratch_services_document_new (_tmp8_, file);
		g_object_ref_sink (_tmp9_);
		doc = _tmp9_;
		_tmp10_ = doc;
		scratch_widgets_document_view_open_document (self, _tmp10_, TRUE, 0, &SELECTION_RANGE_EMPTY, NULL, NULL);
		_g_object_unref0 (doc);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp11_;
		const gchar* _tmp12_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp11_ = e;
		_tmp12_ = _tmp11_->message;
		g_critical ("DocumentView.vala:304: %s", _tmp12_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (file);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	_g_object_unref0 (file);
}

static guint8*
string_get_data (const gchar* self,
                 gint* result_length1)
{
	guint8* result;
	guint8* res = NULL;
	gint res_length1;
	gint _res_size_;
	gint _tmp0_;
	gint _tmp1_;
	guint8* _tmp2_;
	gint _tmp2__length1;
	g_return_val_if_fail (self != NULL, NULL);
	res = (guint8*) self;
	res_length1 = -1;
	_res_size_ = res_length1;
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	res_length1 = (gint) _tmp1_;
	_tmp2_ = res;
	_tmp2__length1 = res_length1;
	if (result_length1) {
		*result_length1 = _tmp2__length1;
	}
	result = _tmp2_;
	return result;
}

void
scratch_widgets_document_view_new_document_from_clipboard (ScratchWidgetsDocumentView* self,
                                                           const gchar* clipboard)
{
	GFile* file = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	GFile* _tmp2_;
	GFile* _tmp3_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (clipboard != NULL);
	_tmp0_ = scratch_widgets_document_view_unsaved_file_path_builder (self, "txt");
	_tmp1_ = _tmp0_;
	_tmp2_ = g_file_new_for_path (_tmp1_);
	_tmp3_ = _tmp2_;
	_g_free0 (_tmp1_);
	file = _tmp3_;
	{
		GFile* _tmp4_;
		GFileOutputStream* _tmp5_;
		GFileOutputStream* _tmp6_;
		GFile* _tmp7_;
		guint8* _tmp8_;
		gint _tmp8__length1;
		gint _tmp9_ = 0;
		guint8* _tmp10_;
		gint _tmp10__length1;
		ScratchServicesDocument* doc = NULL;
		ScratchMainWindow* _tmp11_;
		GSimpleActionGroup* _tmp12_;
		GSimpleActionGroup* _tmp13_;
		GFile* _tmp14_;
		ScratchServicesDocument* _tmp15_;
		ScratchServicesDocument* _tmp16_;
		_tmp4_ = file;
		_tmp5_ = g_file_create (_tmp4_, G_FILE_CREATE_PRIVATE, NULL, &_inner_error0_);
		_tmp6_ = _tmp5_;
		_g_object_unref0 (_tmp6_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp7_ = file;
		_tmp8_ = string_get_data (clipboard, &_tmp9_);
		_tmp8__length1 = _tmp9_;
		_tmp10_ = _tmp8_;
		_tmp10__length1 = _tmp8__length1;
		g_file_replace_contents (_tmp7_, (const char*) _tmp10_, (gsize) _tmp10__length1, NULL, FALSE, 0, NULL, NULL, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp11_ = self->priv->_window;
		_tmp12_ = scratch_main_window_get_actions (_tmp11_);
		_tmp13_ = _tmp12_;
		_tmp14_ = file;
		_tmp15_ = scratch_services_document_new (_tmp13_, _tmp14_);
		g_object_ref_sink (_tmp15_);
		doc = _tmp15_;
		_tmp16_ = doc;
		scratch_widgets_document_view_open_document (self, _tmp16_, TRUE, 0, &SELECTION_RANGE_EMPTY, NULL, NULL);
		_g_object_unref0 (doc);
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
		g_critical ("DocumentView.vala:321: Cannot insert clipboard: %s", clipboard);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (file);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	_g_object_unref0 (file);
}

static void
scratch_widgets_document_view_open_document_data_free (gpointer _data)
{
	ScratchWidgetsDocumentViewOpenDocumentData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->doc);
	_g_object_unref0 (_data_->self);
	g_slice_free (ScratchWidgetsDocumentViewOpenDocumentData, _data_);
}

void
scratch_widgets_document_view_open_document (ScratchWidgetsDocumentView* self,
                                             ScratchServicesDocument* doc,
                                             gboolean focus,
                                             gint cursor_position,
                                             SelectionRange* range,
                                             GAsyncReadyCallback _callback_,
                                             gpointer _user_data_)
{
	ScratchWidgetsDocumentViewOpenDocumentData* _data_;
	ScratchWidgetsDocumentView* _tmp0_;
	ScratchServicesDocument* _tmp1_;
	SelectionRange _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (doc != NULL);
	g_return_if_fail (range != NULL);
	_data_ = g_slice_new0 (ScratchWidgetsDocumentViewOpenDocumentData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, scratch_widgets_document_view_open_document_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = _g_object_ref0 (doc);
	_g_object_unref0 (_data_->doc);
	_data_->doc = _tmp1_;
	_data_->focus = focus;
	_data_->cursor_position = cursor_position;
	_tmp2_ = *range;
	_data_->range = _tmp2_;
	scratch_widgets_document_view_open_document_co (_data_);
}

void
scratch_widgets_document_view_open_document_finish (ScratchWidgetsDocumentView* self,
                                                    GAsyncResult* _res_)
{
	ScratchWidgetsDocumentViewOpenDocumentData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}

static Block56Data*
block56_data_ref (Block56Data* _data56_)
{
	g_atomic_int_inc (&_data56_->_ref_count_);
	return _data56_;
}

static void
block56_data_unref (void * _userdata_)
{
	Block56Data* _data56_;
	_data56_ = (Block56Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data56_->_ref_count_)) {
		ScratchWidgetsDocumentView* self;
		self = _data56_->self;
		_g_object_unref0 (self);
		g_slice_free (Block56Data, _data56_);
	}
}

static gboolean
_selection_range_equal (const SelectionRange * s1,
                        const SelectionRange * s2)
{
	if (s1 == s2) {
		return TRUE;
	}
	if (s1 == NULL) {
		return FALSE;
	}
	if (s2 == NULL) {
		return FALSE;
	}
	if (s1->start_line != s2->start_line) {
		return FALSE;
	}
	if (s1->start_column != s2->start_column) {
		return FALSE;
	}
	if (s1->end_line != s2->end_line) {
		return FALSE;
	}
	if (s1->end_column != s2->end_column) {
		return FALSE;
	}
	return TRUE;
}

static gboolean
_______lambda64_ (Block56Data* _data56_)
{
	ScratchWidgetsDocumentView* self;
	ScratchServicesDocument* _tmp0_;
	ScratchServicesDocument* _tmp1_;
	ScratchWidgetsSourceView* _tmp2_;
	ScratchWidgetsSourceView* _tmp3_;
	SelectionRange _tmp4_;
	gboolean result;
	self = _data56_->self;
	_tmp0_ = scratch_widgets_document_view_get_current_document (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = scratch_services_document_get_source_view (_tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = _data56_->range;
	scratch_widgets_source_view_select_range (_tmp3_, &_tmp4_);
	scratch_widgets_document_view_update_opened_files_setting (self);
	result = FALSE;
	return result;
}

static gboolean
________lambda64__gsource_func (gpointer self)
{
	gboolean result;
	result = _______lambda64_ (self);
	return result;
}

static void
scratch_widgets_document_view_open_document_ready (GObject* source_object,
                                                   GAsyncResult* _res_,
                                                   gpointer _user_data_)
{
	ScratchWidgetsDocumentViewOpenDocumentData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	scratch_widgets_document_view_open_document_co (_data_);
}

static gboolean
scratch_widgets_document_view_open_document_co (ScratchWidgetsDocumentViewOpenDocumentData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_data56_ = g_slice_new0 (Block56Data);
	_data_->_data56_->_ref_count_ = 1;
	_data_->_data56_->self = g_object_ref (_data_->self);
	_data_->_tmp0_ = _data_->range;
	_data_->_data56_->range = _data_->_tmp0_;
	_data_->_data56_->_async_data_ = _data_;
	{
		_data_->n = 0;
		{
			_data_->_tmp1_ = TRUE;
			while (TRUE) {
				if (!_data_->_tmp1_) {
					_data_->_tmp2_ = _data_->n;
					_data_->n = _data_->_tmp2_ + 1;
				}
				_data_->_tmp1_ = FALSE;
				_data_->_tmp3_ = _data_->self->docs;
				if (!(((guint) _data_->n) <= g_list_length (_data_->_tmp3_))) {
					break;
				}
				_data_->_tmp4_ = _data_->self->docs;
				_data_->_tmp5_ = g_list_nth_data (_data_->_tmp4_, (guint) _data_->n);
				_data_->_tmp6_ = _g_object_ref0 ((ScratchServicesDocument*) _data_->_tmp5_);
				_data_->nth_doc = _data_->_tmp6_;
				_data_->_tmp7_ = _data_->nth_doc;
				if (_data_->_tmp7_ == NULL) {
					_g_object_unref0 (_data_->nth_doc);
					continue;
				}
				_data_->_tmp9_ = _data_->nth_doc;
				_data_->_tmp10_ = scratch_services_document_get_file (_data_->_tmp9_);
				_data_->_tmp11_ = _data_->_tmp10_;
				if (_data_->_tmp11_ != NULL) {
					_data_->_tmp12_ = _data_->nth_doc;
					_data_->_tmp13_ = scratch_services_document_get_file (_data_->_tmp12_);
					_data_->_tmp14_ = _data_->_tmp13_;
					_data_->_tmp15_ = g_file_get_uri (_data_->_tmp14_);
					_data_->_tmp16_ = _data_->_tmp15_;
					_data_->_tmp17_ = scratch_services_document_get_file (_data_->doc);
					_data_->_tmp18_ = _data_->_tmp17_;
					_data_->_tmp19_ = g_file_get_uri (_data_->_tmp18_);
					_data_->_tmp20_ = _data_->_tmp19_;
					_data_->_tmp8_ = g_strcmp0 (_data_->_tmp16_, _data_->_tmp20_) == 0;
					_g_free0 (_data_->_tmp20_);
					_g_free0 (_data_->_tmp16_);
				} else {
					_data_->_tmp8_ = FALSE;
				}
				if (_data_->_tmp8_) {
					if (_data_->focus) {
						_data_->_tmp21_ = _data_->nth_doc;
						scratch_widgets_document_view_set_current_document (_data_->self, _data_->_tmp21_);
					}
					g_debug ("DocumentView.vala:337: This Document was already opened! Not opening a" \
" duplicate!");
					_data_->_tmp22_ = _data_->_data56_->range;
					if (_selection_range_equal (&_data_->_tmp22_, &SELECTION_RANGE_EMPTY) != TRUE) {
						g_idle_add_full (G_PRIORITY_LOW, ________lambda64__gsource_func, block56_data_ref (_data_->_data56_), block56_data_unref);
					}
					_g_object_unref0 (_data_->nth_doc);
					block56_data_unref (_data_->_data56_);
					_data_->_data56_ = NULL;
					g_task_return_pointer (_data_->_async_result, _data_, NULL);
					if (_data_->_state_ != 0) {
						while (!g_task_get_completed (_data_->_async_result)) {
							g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
						}
					}
					g_object_unref (_data_->_async_result);
					return FALSE;
				}
				_g_object_unref0 (_data_->nth_doc);
			}
		}
	}
	_data_->_tmp23_ = _data_->self->docs;
	scratch_widgets_document_view_insert_document (_data_->self, _data_->doc, (gint) g_list_length (_data_->_tmp23_));
	if (_data_->focus) {
		scratch_widgets_document_view_set_current_document (_data_->self, _data_->doc);
	}
	_data_->_state_ = 1;
	scratch_services_document_open (_data_->doc, FALSE, scratch_widgets_document_view_open_document_ready, _data_);
	return FALSE;
	_state_1:
	scratch_services_document_open_finish (_data_->doc, _data_->_res_);
	if (_data_->focus) {
		_data_->_tmp25_ = scratch_widgets_document_view_get_current_document (_data_->self);
		_data_->_tmp26_ = _data_->_tmp25_;
		_data_->_tmp24_ = _data_->doc == _data_->_tmp26_;
	} else {
		_data_->_tmp24_ = FALSE;
	}
	if (_data_->_tmp24_) {
		scratch_services_document_focus (_data_->doc);
	}
	_data_->_tmp27_ = _data_->_data56_->range;
	if (_selection_range_equal (&_data_->_tmp27_, &SELECTION_RANGE_EMPTY) != TRUE) {
		_data_->_tmp28_ = scratch_services_document_get_source_view (_data_->doc);
		_data_->_tmp29_ = _data_->_tmp28_;
		_data_->_tmp30_ = _data_->_data56_->range;
		scratch_widgets_source_view_select_range (_data_->_tmp29_, &_data_->_tmp30_);
	} else {
		if (_data_->cursor_position > 0) {
			_data_->_tmp31_ = scratch_services_document_get_source_view (_data_->doc);
			_data_->_tmp32_ = _data_->_tmp31_;
			scratch_widgets_source_view_set_cursor_position (_data_->_tmp32_, _data_->cursor_position);
		}
	}
	scratch_widgets_document_view_update_opened_files_setting (_data_->self);
	block56_data_unref (_data_->_data56_);
	_data_->_data56_ = NULL;
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

void
scratch_widgets_document_view_next_document (ScratchWidgetsDocumentView* self)
{
	guint current_index = 0U;
	GList* _tmp0_;
	ScratchServicesDocument* _tmp1_;
	ScratchServicesDocument* _tmp2_;
	GList* _tmp3_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->docs;
	_tmp1_ = scratch_widgets_document_view_get_current_document (self);
	_tmp2_ = _tmp1_;
	current_index = (guint) (g_list_index (_tmp0_, _tmp2_) + 1);
	_tmp3_ = self->docs;
	if (current_index < g_list_length (_tmp3_)) {
		ScratchServicesDocument* next_doc = NULL;
		GList* _tmp4_;
		guint _tmp5_;
		gconstpointer _tmp6_;
		ScratchServicesDocument* _tmp7_;
		ScratchServicesDocument* _tmp8_;
		ScratchServicesDocument* _tmp9_;
		_tmp4_ = self->docs;
		_tmp5_ = current_index;
		current_index = _tmp5_ + 1;
		_tmp6_ = g_list_nth_data (_tmp4_, _tmp5_);
		_tmp7_ = _g_object_ref0 ((ScratchServicesDocument*) _tmp6_);
		next_doc = _tmp7_;
		_tmp8_ = next_doc;
		scratch_widgets_document_view_set_current_document (self, _tmp8_);
		_tmp9_ = next_doc;
		scratch_services_document_focus (_tmp9_);
		_g_object_unref0 (next_doc);
	} else {
		GList* _tmp10_;
		_tmp10_ = self->docs;
		if (g_list_length (_tmp10_) > ((guint) 0)) {
			ScratchServicesDocument* next_doc = NULL;
			GList* _tmp11_;
			gconstpointer _tmp12_;
			ScratchServicesDocument* _tmp13_;
			ScratchServicesDocument* _tmp14_;
			ScratchServicesDocument* _tmp15_;
			_tmp11_ = self->docs;
			_tmp12_ = g_list_nth_data (_tmp11_, (guint) 0);
			_tmp13_ = _g_object_ref0 ((ScratchServicesDocument*) _tmp12_);
			next_doc = _tmp13_;
			_tmp14_ = next_doc;
			scratch_widgets_document_view_set_current_document (self, _tmp14_);
			_tmp15_ = next_doc;
			scratch_services_document_focus (_tmp15_);
			_g_object_unref0 (next_doc);
		}
	}
}

void
scratch_widgets_document_view_previous_document (ScratchWidgetsDocumentView* self)
{
	guint current_index = 0U;
	GList* _tmp0_;
	ScratchServicesDocument* _tmp1_;
	ScratchServicesDocument* _tmp2_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->docs;
	_tmp1_ = scratch_widgets_document_view_get_current_document (self);
	_tmp2_ = _tmp1_;
	current_index = (guint) g_list_index (_tmp0_, _tmp2_);
	if (current_index > ((guint) 0)) {
		ScratchServicesDocument* previous_doc = NULL;
		GList* _tmp3_;
		guint _tmp4_;
		gconstpointer _tmp5_;
		ScratchServicesDocument* _tmp6_;
		ScratchServicesDocument* _tmp7_;
		ScratchServicesDocument* _tmp8_;
		_tmp3_ = self->docs;
		current_index = current_index - 1;
		_tmp4_ = current_index;
		_tmp5_ = g_list_nth_data (_tmp3_, _tmp4_);
		_tmp6_ = _g_object_ref0 ((ScratchServicesDocument*) _tmp5_);
		previous_doc = _tmp6_;
		_tmp7_ = previous_doc;
		scratch_widgets_document_view_set_current_document (self, _tmp7_);
		_tmp8_ = previous_doc;
		scratch_services_document_focus (_tmp8_);
		_g_object_unref0 (previous_doc);
	} else {
		GList* _tmp9_;
		_tmp9_ = self->docs;
		if (g_list_length (_tmp9_) > ((guint) 0)) {
			ScratchServicesDocument* previous_doc = NULL;
			GList* _tmp10_;
			GList* _tmp11_;
			gconstpointer _tmp12_;
			ScratchServicesDocument* _tmp13_;
			ScratchServicesDocument* _tmp14_;
			ScratchServicesDocument* _tmp15_;
			_tmp10_ = self->docs;
			_tmp11_ = self->docs;
			_tmp12_ = g_list_nth_data (_tmp10_, g_list_length (_tmp11_) - 1);
			_tmp13_ = _g_object_ref0 ((ScratchServicesDocument*) _tmp12_);
			previous_doc = _tmp13_;
			_tmp14_ = previous_doc;
			scratch_widgets_document_view_set_current_document (self, _tmp14_);
			_tmp15_ = previous_doc;
			scratch_services_document_focus (_tmp15_);
			_g_object_unref0 (previous_doc);
		}
	}
}

void
scratch_widgets_document_view_request_placeholder_if_empty (ScratchWidgetsDocumentView* self)
{
	GList* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->docs;
	if (g_list_length (_tmp0_) == ((guint) 0)) {
		g_signal_emit (self, scratch_widgets_document_view_signals[SCRATCH_WIDGETS_DOCUMENT_VIEW_REQUEST_PLACEHOLDER_SIGNAL], 0);
	}
}

static Block57Data*
block57_data_ref (Block57Data* _data57_)
{
	g_atomic_int_inc (&_data57_->_ref_count_);
	return _data57_;
}

static void
block57_data_unref (void * _userdata_)
{
	Block57Data* _data57_;
	_data57_ = (Block57Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data57_->_ref_count_)) {
		ScratchWidgetsDocumentView* self;
		self = _data57_->self;
		_g_variant_builder_unref0 (_data57_->vb);
		_g_object_unref0 (self);
		g_slice_free (Block57Data, _data57_);
	}
}

static void
___lambda65_ (Block57Data* _data57_,
              ScratchServicesDocument* doc)
{
	ScratchWidgetsDocumentView* self;
	gboolean _tmp0_ = FALSE;
	GFile* _tmp1_;
	GFile* _tmp2_;
	self = _data57_->self;
	g_return_if_fail (doc != NULL);
	_tmp1_ = scratch_services_document_get_file (doc);
	_tmp2_ = _tmp1_;
	if (_tmp2_ != NULL) {
		_tmp0_ = scratch_services_document_exists (doc, NULL);
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		GVariantBuilder* _tmp3_;
		GFile* _tmp4_;
		GFile* _tmp5_;
		gchar* _tmp6_;
		gchar* _tmp7_;
		ScratchWidgetsSourceView* _tmp8_;
		ScratchWidgetsSourceView* _tmp9_;
		gint _tmp10_;
		gint _tmp11_;
		_tmp3_ = _data57_->vb;
		_tmp4_ = scratch_services_document_get_file (doc);
		_tmp5_ = _tmp4_;
		_tmp6_ = g_file_get_uri (_tmp5_);
		_tmp7_ = _tmp6_;
		_tmp8_ = scratch_services_document_get_source_view (doc);
		_tmp9_ = _tmp8_;
		_tmp10_ = scratch_widgets_source_view_get_cursor_position (_tmp9_);
		_tmp11_ = _tmp10_;
		g_variant_builder_add (_tmp3_, "(si)", _tmp7_, _tmp11_, NULL);
		_g_free0 (_tmp7_);
	}
}

static void
____lambda65__gfunc (gconstpointer data,
                     gpointer self)
{
	___lambda65_ (self, (ScratchServicesDocument*) data);
}

void
scratch_widgets_document_view_update_opened_files_setting (ScratchWidgetsDocumentView* self)
{
	GSettings* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = scratch_privacy_settings;
	if (g_settings_get_boolean (_tmp0_, "remember-recent-files")) {
		Block57Data* _data57_;
		GVariantType* _tmp1_;
		GVariantType* _tmp2_;
		GVariantBuilder* _tmp3_;
		GVariantBuilder* _tmp4_;
		GList* _tmp5_;
		GSettings* _tmp6_;
		GVariantBuilder* _tmp7_;
		GVariant* _tmp8_;
		GVariant* _tmp9_;
		_data57_ = g_slice_new0 (Block57Data);
		_data57_->_ref_count_ = 1;
		_data57_->self = g_object_ref (self);
		_tmp1_ = g_variant_type_new ("a(si)");
		_tmp2_ = _tmp1_;
		_tmp3_ = g_variant_builder_new (_tmp2_);
		_tmp4_ = _tmp3_;
		_g_variant_type_free0 (_tmp2_);
		_data57_->vb = _tmp4_;
		_tmp5_ = self->docs;
		g_list_foreach (_tmp5_, ____lambda65__gfunc, _data57_);
		_tmp6_ = scratch_settings;
		_tmp7_ = _data57_->vb;
		_tmp8_ = g_variant_builder_end (_tmp7_);
		g_variant_ref_sink (_tmp8_);
		_tmp9_ = _tmp8_;
		g_settings_set_value (_tmp6_, "opened-files", _tmp9_);
		_g_variant_unref0 (_tmp9_);
		block57_data_unref (_data57_);
		_data57_ = NULL;
	}
}

static void
__lambda66_ (ScratchWidgetsDocumentView* self,
             ScratchServicesDocument* doc)
{
	gboolean _tmp0_;
	g_return_if_fail (doc != NULL);
	_tmp0_ = self->priv->_outline_visible;
	scratch_services_document_show_outline (doc, _tmp0_);
}

static void
___lambda66__gfunc (gconstpointer data,
                    gpointer self)
{
	__lambda66_ ((ScratchWidgetsDocumentView*) self, (ScratchServicesDocument*) data);
}

void
scratch_widgets_document_view_update_outline_visible (ScratchWidgetsDocumentView* self)
{
	GList* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->docs;
	g_list_foreach (_tmp0_, ___lambda66__gfunc, self);
}

void
scratch_widgets_document_view_focus (ScratchWidgetsDocumentView* self)
{
	ScratchServicesDocument* _tmp0_;
	ScratchServicesDocument* _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = scratch_widgets_document_view_get_current_document (self);
	_tmp1_ = _tmp0_;
	scratch_services_document_focus (_tmp1_);
}

static void
scratch_widgets_document_view_tab_view_setup_menu (ScratchWidgetsDocumentView* self,
                                                   HdyTabPage* page)
{
	GSimpleAction* close_other_tabs_action = NULL;
	ScratchMainWindow* _tmp0_;
	GSimpleActionGroup* _tmp1_;
	GSimpleActionGroup* _tmp2_;
	GSimpleAction* _tmp3_;
	GSimpleAction* close_tabs_to_right_action = NULL;
	ScratchMainWindow* _tmp4_;
	GSimpleActionGroup* _tmp5_;
	GSimpleActionGroup* _tmp6_;
	GSimpleAction* _tmp7_;
	gint _tmp8_ = 0;
	gint page_position = 0;
	gboolean _tmp10_ = FALSE;
	GSimpleAction* _tmp14_;
	gboolean _tmp15_ = FALSE;
	GSimpleAction* _tmp19_;
	g_return_if_fail (self != NULL);
	self->priv->tab_menu_target = page;
	_tmp0_ = self->priv->_window;
	_tmp1_ = scratch_main_window_get_actions (_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = scratch_utils_action_from_group (SCRATCH_MAIN_WINDOW_ACTION_CLOSE_OTHER_TABS, _tmp2_);
	close_other_tabs_action = _tmp3_;
	_tmp4_ = self->priv->_window;
	_tmp5_ = scratch_main_window_get_actions (_tmp4_);
	_tmp6_ = _tmp5_;
	_tmp7_ = scratch_utils_action_from_group (SCRATCH_MAIN_WINDOW_ACTION_CLOSE_TABS_TO_RIGHT, _tmp6_);
	close_tabs_to_right_action = _tmp7_;
	if (page != NULL) {
		HdyTabView* _tmp9_;
		_tmp9_ = self->priv->tab_view;
		_tmp8_ = hdy_tab_view_get_page_position (_tmp9_, page);
	} else {
		_tmp8_ = -1;
	}
	page_position = _tmp8_;
	if (page != NULL) {
		HdyTabView* _tmp11_;
		gint _tmp12_;
		gint _tmp13_;
		_tmp11_ = self->priv->tab_view;
		_tmp12_ = hdy_tab_view_get_n_pages (_tmp11_);
		_tmp13_ = _tmp12_;
		_tmp10_ = _tmp13_ > 1;
	} else {
		_tmp10_ = FALSE;
	}
	_tmp14_ = close_other_tabs_action;
	g_simple_action_set_enabled (_tmp14_, _tmp10_);
	if (page != NULL) {
		HdyTabView* _tmp16_;
		gint _tmp17_;
		gint _tmp18_;
		_tmp16_ = self->priv->tab_view;
		_tmp17_ = hdy_tab_view_get_n_pages (_tmp16_);
		_tmp18_ = _tmp17_;
		_tmp15_ = page_position != (_tmp18_ - 1);
	} else {
		_tmp15_ = FALSE;
	}
	_tmp19_ = close_tabs_to_right_action;
	g_simple_action_set_enabled (_tmp19_, _tmp15_);
	_g_object_unref0 (close_tabs_to_right_action);
	_g_object_unref0 (close_other_tabs_action);
}

static void
scratch_widgets_document_view_insert_document (ScratchWidgetsDocumentView* self,
                                               ScratchServicesDocument* doc,
                                               gint pos)
{
	HdyTabView* _tmp0_;
	GSettings* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (doc != NULL);
	_tmp0_ = self->priv->tab_view;
	hdy_tab_view_insert (_tmp0_, (GtkWidget*) doc, pos);
	_tmp1_ = scratch_saved_state;
	if (g_settings_get_boolean (_tmp1_, "outline-visible")) {
		g_debug ("DocumentView.vala:442: setting outline visible");
		scratch_services_document_show_outline (doc, TRUE);
	}
}

static gchar*
scratch_widgets_document_view_unsaved_file_path_builder (ScratchWidgetsDocumentView* self,
                                                         const gchar* extension)
{
	GDateTime* timestamp = NULL;
	GDateTime* _tmp0_;
	gchar* new_text_file = NULL;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	ScratchMainWindow* _tmp5_;
	ScratchApplication* _tmp6_;
	ScratchApplication* _tmp7_;
	const gchar* _tmp8_;
	const gchar* _tmp9_;
	gchar* _tmp10_;
	gchar* _tmp11_;
	gchar* _tmp12_;
	gchar* _tmp13_;
	gchar* _tmp14_;
	gchar* _tmp15_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (extension != NULL, NULL);
	_tmp0_ = g_date_time_new_now_local ();
	timestamp = _tmp0_;
	_tmp1_ = g_date_time_format (timestamp, "%Y-%m-%d %H:%M:%S");
	_tmp2_ = _tmp1_;
	_tmp3_ = g_strdup_printf (_ ("Text file from %s:%d"), _tmp2_, g_date_time_get_microsecond (timestamp));
	_tmp4_ = _tmp3_;
	_g_free0 (_tmp2_);
	new_text_file = _tmp4_;
	_tmp5_ = self->priv->_window;
	_tmp6_ = scratch_main_window_get_app (_tmp5_);
	_tmp7_ = _tmp6_;
	_tmp8_ = scratch_application_get_data_home_folder_unsaved (_tmp7_);
	_tmp9_ = _tmp8_;
	_tmp10_ = g_build_filename (_tmp9_, new_text_file, NULL);
	_tmp11_ = _tmp10_;
	_tmp12_ = g_strconcat (_tmp11_, ".", NULL);
	_tmp13_ = _tmp12_;
	_tmp14_ = g_strconcat (_tmp13_, extension, NULL);
	_tmp15_ = _tmp14_;
	_g_free0 (_tmp13_);
	_g_free0 (_tmp11_);
	result = _tmp15_;
	_g_free0 (new_text_file);
	_g_date_time_unref0 (timestamp);
	return result;
}

static gchar*
scratch_widgets_document_view_unsaved_duplicated_file_path_builder (ScratchWidgetsDocumentView* self,
                                                                    const gchar* original_filename)
{
	gchar* extension = NULL;
	gchar* _tmp0_;
	gchar** parts = NULL;
	gchar** _tmp1_;
	gchar** _tmp2_;
	gint parts_length1;
	gint _parts_size_;
	gchar** _tmp3_;
	gint _tmp3__length1;
	const gchar* _tmp8_;
	gchar* _tmp9_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (original_filename != NULL, NULL);
	_tmp0_ = g_strdup ("txt");
	extension = _tmp0_;
	_tmp2_ = _tmp1_ = g_strsplit (original_filename, ".", 2);
	parts = _tmp2_;
	parts_length1 = _vala_array_length (_tmp1_);
	_parts_size_ = parts_length1;
	_tmp3_ = parts;
	_tmp3__length1 = parts_length1;
	if (_tmp3__length1 > 1) {
		gchar** _tmp4_;
		gint _tmp4__length1;
		gchar** _tmp5_;
		gint _tmp5__length1;
		const gchar* _tmp6_;
		gchar* _tmp7_;
		_tmp4_ = parts;
		_tmp4__length1 = parts_length1;
		_tmp5_ = parts;
		_tmp5__length1 = parts_length1;
		_tmp6_ = _tmp4_[_tmp5__length1 - 1];
		_tmp7_ = g_strdup (_tmp6_);
		_g_free0 (extension);
		extension = _tmp7_;
	}
	_tmp8_ = extension;
	_tmp9_ = scratch_widgets_document_view_unsaved_file_path_builder (self, _tmp8_);
	result = _tmp9_;
	parts = (_vala_array_free (parts, parts_length1, (GDestroyNotify) g_free), NULL);
	_g_free0 (extension);
	return result;
}

static GList*
vala_g_list_remove_full (GList* self,
                         gconstpointer data,
                         GFreeFunc func)
{
	GList* l = NULL;
	GList* result;
	l = self;
	while (TRUE) {
		GList* _tmp0_;
		GList* _tmp1_;
		gconstpointer _tmp2_;
		_tmp0_ = l;
		if (!(_tmp0_ != NULL)) {
			break;
		}
		_tmp1_ = l;
		_tmp2_ = ((GList*) _tmp1_)->data;
		if (_tmp2_ != data) {
			GList* _tmp3_;
			GList* _tmp4_;
			_tmp3_ = l;
			_tmp4_ = ((GList*) _tmp3_)->next;
			l = _tmp4_;
		} else {
			GList* _tmp5_;
			gconstpointer _tmp6_;
			GList* _tmp7_;
			_tmp5_ = l;
			_tmp6_ = ((GList*) _tmp5_)->data;
			func (_tmp6_);
			_tmp7_ = l;
			self = g_list_delete_link (self, (GList*) _tmp7_);
			break;
		}
	}
	result = self;
	return result;
}

static void
scratch_widgets_document_view_on_page_detached (ScratchWidgetsDocumentView* self,
                                                HdyTabPage* tab,
                                                gint position)
{
	ScratchServicesDocument* doc = NULL;
	GtkWidget* _tmp0_;
	ScratchServicesDocument* _tmp1_;
	ScratchServicesDocument* _tmp2_;
	GtkMenuButton* _tmp3_;
	GMenuModel* _tmp4_;
	GMenuModel* _tmp5_;
	gchar* path = NULL;
	ScratchServicesDocument* _tmp9_;
	GFile* _tmp10_;
	GFile* _tmp11_;
	gchar* _tmp12_;
	gboolean path_in_menu = FALSE;
	GMenu* menu = NULL;
	GtkMenuButton* _tmp13_;
	GMenuModel* _tmp14_;
	GMenuModel* _tmp15_;
	GMenu* _tmp16_;
	gint position_in_menu = 0;
	GMenu* _tmp28_;
	GMenu* _tmp30_;
	const gchar* _tmp31_;
	const gchar* _tmp32_;
	gchar* _tmp33_;
	gchar* _tmp34_;
	ScratchServicesDocument* _tmp35_;
	ScratchServicesDocument* _tmp36_;
	ScratchServicesDocumentManager* _tmp37_;
	ScratchServicesDocumentManager* _tmp38_;
	ScratchServicesDocument* _tmp39_;
	GList* _tmp40_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (tab != NULL);
	_tmp0_ = hdy_tab_page_get_child (tab);
	_tmp1_ = _g_object_ref0 (SCRATCH_SERVICES_IS_DOCUMENT (_tmp0_) ? ((ScratchServicesDocument*) _tmp0_) : NULL);
	doc = _tmp1_;
	_tmp2_ = doc;
	if (_tmp2_ == NULL) {
		_g_object_unref0 (doc);
		return;
	}
	_tmp3_ = self->priv->tab_history_button;
	_tmp4_ = gtk_menu_button_get_menu_model (_tmp3_);
	_tmp5_ = _tmp4_;
	if (_tmp5_ == NULL) {
		GtkMenuButton* _tmp6_;
		GMenu* _tmp7_;
		GMenu* _tmp8_;
		_tmp6_ = self->priv->tab_history_button;
		_tmp7_ = g_menu_new ();
		_tmp8_ = _tmp7_;
		gtk_menu_button_set_menu_model (_tmp6_, (GMenuModel*) _tmp8_);
		_g_object_unref0 (_tmp8_);
	}
	_tmp9_ = doc;
	_tmp10_ = scratch_services_document_get_file (_tmp9_);
	_tmp11_ = _tmp10_;
	_tmp12_ = g_file_get_path (_tmp11_);
	path = _tmp12_;
	path_in_menu = FALSE;
	_tmp13_ = self->priv->tab_history_button;
	_tmp14_ = gtk_menu_button_get_menu_model (_tmp13_);
	_tmp15_ = _tmp14_;
	_tmp16_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp15_, g_menu_get_type (), GMenu));
	menu = _tmp16_;
	position_in_menu = -1;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp17_ = FALSE;
			_tmp17_ = TRUE;
			while (TRUE) {
				GMenu* _tmp19_;
				const gchar* _tmp20_;
				GMenu* _tmp21_;
				const GVariantType* _tmp22_;
				GVariant* _tmp23_;
				GVariant* _tmp24_;
				const gchar* _tmp25_;
				gboolean _tmp26_;
				if (!_tmp17_) {
					gint _tmp18_;
					_tmp18_ = i;
					i = _tmp18_ + 1;
				}
				_tmp17_ = FALSE;
				_tmp19_ = menu;
				if (!(i < g_menu_model_get_n_items ((GMenuModel*) _tmp19_))) {
					break;
				}
				_tmp20_ = path;
				_tmp21_ = menu;
				_tmp22_ = G_VARIANT_TYPE_STRING;
				_tmp23_ = g_menu_model_get_item_attribute_value ((GMenuModel*) _tmp21_, i, G_MENU_ATTRIBUTE_TARGET, _tmp22_);
				_tmp24_ = _tmp23_;
				_tmp25_ = g_variant_get_string (_tmp24_, NULL);
				_tmp26_ = g_strcmp0 (_tmp20_, _tmp25_) == 0;
				_g_variant_unref0 (_tmp24_);
				if (_tmp26_) {
					path_in_menu = TRUE;
					position_in_menu = i;
					break;
				}
			}
		}
	}
	if (path_in_menu) {
		GMenu* _tmp27_;
		_tmp27_ = menu;
		g_menu_remove (_tmp27_, position_in_menu);
	}
	_tmp28_ = menu;
	if (g_menu_model_get_n_items ((GMenuModel*) _tmp28_) == SCRATCH_WIDGETS_DOCUMENT_VIEW_TAB_HISTORY_MAX_ITEMS) {
		GMenu* _tmp29_;
		_tmp29_ = menu;
		g_menu_remove (_tmp29_, SCRATCH_WIDGETS_DOCUMENT_VIEW_TAB_HISTORY_MAX_ITEMS - 1);
	}
	_tmp30_ = menu;
	_tmp31_ = path;
	_tmp32_ = path;
	_tmp33_ = g_strdup_printf ("%s::%s", SCRATCH_MAIN_WINDOW_ACTION_PREFIX SCRATCH_MAIN_WINDOW_ACTION_RESTORE_CLOSED_TAB, _tmp32_);
	_tmp34_ = _tmp33_;
	g_menu_prepend (_tmp30_, _tmp31_, _tmp34_);
	_g_free0 (_tmp34_);
	_tmp35_ = doc;
	self->docs = vala_g_list_remove_full (self->docs, _tmp35_, _g_object_unref0_);
	_tmp36_ = doc;
	g_signal_emit (self, scratch_widgets_document_view_signals[SCRATCH_WIDGETS_DOCUMENT_VIEW_TAB_REMOVED_SIGNAL], 0, _tmp36_);
	_tmp37_ = scratch_services_document_manager_get_instance ();
	_tmp38_ = _tmp37_;
	_tmp39_ = doc;
	scratch_services_document_manager_remove_open_document (_tmp38_, _tmp39_);
	_g_object_unref0 (_tmp38_);
	_tmp40_ = self->docs;
	if (g_list_length (_tmp40_) > ((guint) 0)) {
		ScratchServicesDocument* _tmp41_;
		gboolean _tmp42_;
		gboolean _tmp43_;
		_tmp41_ = doc;
		_tmp42_ = scratch_services_document_get_is_file_temporary (_tmp41_);
		_tmp43_ = _tmp42_;
		if (!_tmp43_) {
			GList* _tmp44_;
			_tmp44_ = self->docs;
			{
				GList* d_collection = NULL;
				GList* d_it = NULL;
				d_collection = _tmp44_;
				for (d_it = d_collection; d_it != NULL; d_it = d_it->next) {
					ScratchServicesDocument* _tmp45_;
					ScratchServicesDocument* d = NULL;
					_tmp45_ = _g_object_ref0 ((ScratchServicesDocument*) d_it->data);
					d = _tmp45_;
					{
						ScratchServicesDocument* _tmp46_;
						_tmp46_ = d;
						scratch_widgets_document_view_rename_tabs_with_same_title (self, _tmp46_);
						_g_object_unref0 (d);
					}
				}
			}
		}
	}
	scratch_widgets_document_view_request_placeholder_if_empty (self);
	_g_object_unref0 (menu);
	_g_free0 (path);
	_g_object_unref0 (doc);
}

void
scratch_widgets_document_view_restore_closed_tab (ScratchWidgetsDocumentView* self,
                                                  const gchar* path)
{
	GFile* file = NULL;
	GFile* _tmp0_;
	ScratchServicesDocument* doc = NULL;
	ScratchMainWindow* _tmp1_;
	GSimpleActionGroup* _tmp2_;
	GSimpleActionGroup* _tmp3_;
	GFile* _tmp4_;
	ScratchServicesDocument* _tmp5_;
	ScratchServicesDocument* _tmp6_;
	GMenu* menu = NULL;
	GtkMenuButton* _tmp7_;
	GMenuModel* _tmp8_;
	GMenuModel* _tmp9_;
	GMenu* _tmp10_;
	GMenu* _tmp21_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (path != NULL);
	_tmp0_ = g_file_new_for_path (path);
	file = _tmp0_;
	_tmp1_ = self->priv->_window;
	_tmp2_ = scratch_main_window_get_actions (_tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = file;
	_tmp5_ = scratch_services_document_new (_tmp3_, _tmp4_);
	g_object_ref_sink (_tmp5_);
	doc = _tmp5_;
	_tmp6_ = doc;
	scratch_widgets_document_view_open_document (self, _tmp6_, TRUE, 0, &SELECTION_RANGE_EMPTY, NULL, NULL);
	_tmp7_ = self->priv->tab_history_button;
	_tmp8_ = gtk_menu_button_get_menu_model (_tmp7_);
	_tmp9_ = _tmp8_;
	_tmp10_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp9_, g_menu_get_type (), GMenu));
	menu = _tmp10_;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp11_ = FALSE;
			_tmp11_ = TRUE;
			while (TRUE) {
				GMenu* _tmp13_;
				GMenu* _tmp14_;
				const GVariantType* _tmp15_;
				GVariant* _tmp16_;
				GVariant* _tmp17_;
				const gchar* _tmp18_;
				gboolean _tmp19_;
				if (!_tmp11_) {
					gint _tmp12_;
					_tmp12_ = i;
					i = _tmp12_ + 1;
				}
				_tmp11_ = FALSE;
				_tmp13_ = menu;
				if (!(i < g_menu_model_get_n_items ((GMenuModel*) _tmp13_))) {
					break;
				}
				_tmp14_ = menu;
				_tmp15_ = G_VARIANT_TYPE_STRING;
				_tmp16_ = g_menu_model_get_item_attribute_value ((GMenuModel*) _tmp14_, i, G_MENU_ATTRIBUTE_TARGET, _tmp15_);
				_tmp17_ = _tmp16_;
				_tmp18_ = g_variant_get_string (_tmp17_, NULL);
				_tmp19_ = g_strcmp0 (path, _tmp18_) == 0;
				_g_variant_unref0 (_tmp17_);
				if (_tmp19_) {
					GMenu* _tmp20_;
					_tmp20_ = menu;
					g_menu_remove (_tmp20_, i);
					break;
				}
			}
		}
	}
	_tmp21_ = menu;
	if (g_menu_model_get_n_items ((GMenuModel*) _tmp21_) == 0) {
		GtkMenuButton* _tmp22_;
		_tmp22_ = self->priv->tab_history_button;
		gtk_menu_button_set_menu_model (_tmp22_, NULL);
	}
	_g_object_unref0 (menu);
	_g_object_unref0 (doc);
	_g_object_unref0 (file);
}

static void
scratch_widgets_document_view_on_doc_reordered (ScratchWidgetsDocumentView* self,
                                                HdyTabPage* tab,
                                                gint new_position)
{
	ScratchServicesDocument* doc = NULL;
	GtkWidget* _tmp0_;
	GtkWidget* _tmp1_;
	ScratchServicesDocument* _tmp2_;
	ScratchServicesDocument* _tmp3_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (tab != NULL);
	_tmp0_ = hdy_tab_page_get_child (tab);
	_tmp1_ = _tmp0_;
	_tmp2_ = _g_object_ref0 (SCRATCH_SERVICES_IS_DOCUMENT (_tmp1_) ? ((ScratchServicesDocument*) _tmp1_) : NULL);
	doc = _tmp2_;
	_tmp3_ = doc;
	if (_tmp3_ != NULL) {
		ScratchServicesDocument* _tmp4_;
		ScratchServicesDocument* _tmp5_;
		ScratchServicesDocument* _tmp6_;
		ScratchServicesDocument* _tmp7_;
		_tmp4_ = doc;
		self->docs = vala_g_list_remove_full (self->docs, _tmp4_, _g_object_unref0_);
		_tmp5_ = doc;
		_tmp6_ = _g_object_ref0 (_tmp5_);
		self->docs = g_list_insert (self->docs, _tmp6_, new_position);
		_tmp7_ = doc;
		scratch_widgets_document_view_set_current_document (self, _tmp7_);
	}
	scratch_widgets_document_view_update_opened_files_setting (self);
	_g_object_unref0 (doc);
}

static HdyTabView*
scratch_widgets_document_view_on_doc_to_new_window (ScratchWidgetsDocumentView* self,
                                                    HdyTabView* tab_view)
{
	ScratchMainWindow* other_window = NULL;
	ScratchMainWindow* _tmp0_;
	ScratchWidgetsDocumentView* _tmp1_;
	ScratchWidgetsDocumentView* _tmp2_;
	HdyTabView* _tmp3_;
	HdyTabView* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (tab_view != NULL, NULL);
	_tmp0_ = scratch_main_window_new (FALSE);
	g_object_ref_sink (_tmp0_);
	other_window = _tmp0_;
	_tmp1_ = scratch_main_window_get_document_view (other_window);
	_tmp2_ = _tmp1_;
	_tmp3_ = _tmp2_->priv->tab_view;
	result = _tmp3_;
	_g_object_unref0 (other_window);
	return result;
}

static void
scratch_widgets_document_view_on_doc_added (ScratchWidgetsDocumentView* self,
                                            HdyTabPage* page,
                                            gint position)
{
	ScratchServicesDocument* doc = NULL;
	GtkWidget* _tmp0_;
	ScratchServicesDocument* _tmp1_;
	ScratchServicesDocument* _tmp2_;
	ScratchServicesDocument* _tmp3_;
	ScratchServicesDocument* _tmp4_;
	ScratchServicesDocument* _tmp5_;
	ScratchMainWindow* _tmp6_;
	GSimpleActionGroup* _tmp7_;
	GSimpleActionGroup* _tmp8_;
	ScratchServicesDocumentManager* _tmp9_;
	ScratchServicesDocumentManager* _tmp10_;
	ScratchServicesDocument* _tmp11_;
	ScratchServicesDocument* _tmp12_;
	gboolean _tmp13_;
	gboolean _tmp14_;
	ScratchServicesDocument* _tmp16_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (page != NULL);
	_tmp0_ = hdy_tab_page_get_child (page);
	_tmp1_ = _g_object_ref0 (SCRATCH_SERVICES_IS_DOCUMENT (_tmp0_) ? ((ScratchServicesDocument*) _tmp0_) : NULL);
	doc = _tmp1_;
	_tmp2_ = doc;
	scratch_services_document_init_tab (_tmp2_, page);
	_tmp3_ = doc;
	_tmp4_ = _g_object_ref0 (_tmp3_);
	self->docs = g_list_append (self->docs, _tmp4_);
	_tmp5_ = doc;
	_tmp6_ = self->priv->_window;
	_tmp7_ = scratch_main_window_get_actions (_tmp6_);
	_tmp8_ = _tmp7_;
	scratch_services_document_set_actions (_tmp5_, _tmp8_);
	_tmp9_ = scratch_services_document_manager_get_instance ();
	_tmp10_ = _tmp9_;
	_tmp11_ = doc;
	scratch_services_document_manager_add_open_document (_tmp10_, _tmp11_);
	_g_object_unref0 (_tmp10_);
	_tmp12_ = doc;
	_tmp13_ = scratch_services_document_get_is_file_temporary (_tmp12_);
	_tmp14_ = _tmp13_;
	if (!_tmp14_) {
		ScratchServicesDocument* _tmp15_;
		_tmp15_ = doc;
		scratch_widgets_document_view_rename_tabs_with_same_title (self, _tmp15_);
	}
	_tmp16_ = doc;
	g_signal_emit (self, scratch_widgets_document_view_signals[SCRATCH_WIDGETS_DOCUMENT_VIEW_TAB_ADDED_SIGNAL], 0, _tmp16_);
	_g_object_unref0 (doc);
}

static void
scratch_widgets_document_view_rename_tabs_with_same_title (ScratchWidgetsDocumentView* self,
                                                           ScratchServicesDocument* doc)
{
	gboolean _tmp0_;
	gboolean _tmp1_;
	gchar* doc_tab_name = NULL;
	GFile* _tmp2_;
	GFile* _tmp3_;
	gchar* _tmp4_;
	GList* _tmp5_;
	const gchar* _tmp36_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (doc != NULL);
	_tmp0_ = scratch_services_document_get_is_file_temporary (doc);
	_tmp1_ = _tmp0_;
	if (_tmp1_) {
		return;
	}
	_tmp2_ = scratch_services_document_get_file (doc);
	_tmp3_ = _tmp2_;
	_tmp4_ = g_file_get_basename (_tmp3_);
	doc_tab_name = _tmp4_;
	_tmp5_ = self->docs;
	{
		GList* d_collection = NULL;
		GList* d_it = NULL;
		d_collection = _tmp5_;
		for (d_it = d_collection; d_it != NULL; d_it = d_it->next) {
			ScratchServicesDocument* _tmp6_;
			ScratchServicesDocument* d = NULL;
			_tmp6_ = _g_object_ref0 ((ScratchServicesDocument*) d_it->data);
			d = _tmp6_;
			{
				ScratchServicesDocument* _tmp7_;
				gboolean _tmp8_;
				gboolean _tmp9_;
				gchar* new_tabname_doc = NULL;
				gchar* new_tabname_d = NULL;
				ScratchServicesDocument* _tmp10_;
				GFile* _tmp11_;
				GFile* _tmp12_;
				GFile* _tmp13_;
				GFile* _tmp14_;
				gchar* _tmp15_ = NULL;
				gchar* _tmp16_ = NULL;
				gboolean _tmp17_;
				_tmp7_ = d;
				_tmp8_ = scratch_services_document_get_is_file_temporary (_tmp7_);
				_tmp9_ = _tmp8_;
				if (_tmp9_) {
					_g_object_unref0 (d);
					continue;
				}
				_tmp10_ = d;
				_tmp11_ = scratch_services_document_get_file (_tmp10_);
				_tmp12_ = _tmp11_;
				_tmp13_ = scratch_services_document_get_file (doc);
				_tmp14_ = _tmp13_;
				_tmp17_ = scratch_utils_find_unique_path (_tmp12_, _tmp14_, &_tmp15_, &_tmp16_);
				_g_free0 (new_tabname_d);
				new_tabname_d = _tmp15_;
				_g_free0 (new_tabname_doc);
				new_tabname_doc = _tmp16_;
				if (_tmp17_) {
					ScratchServicesDocument* _tmp18_;
					const gchar* _tmp19_;
					const gchar* _tmp20_;
					gint _tmp21_;
					gint _tmp22_;
					const gchar* _tmp23_;
					gint _tmp24_;
					gint _tmp25_;
					const gchar* _tmp28_;
					gint _tmp29_;
					gint _tmp30_;
					const gchar* _tmp31_;
					gint _tmp32_;
					gint _tmp33_;
					_tmp18_ = d;
					_tmp19_ = scratch_services_document_get_title (_tmp18_);
					_tmp20_ = _tmp19_;
					_tmp21_ = strlen (_tmp20_);
					_tmp22_ = _tmp21_;
					_tmp23_ = new_tabname_d;
					_tmp24_ = strlen (_tmp23_);
					_tmp25_ = _tmp24_;
					if (_tmp22_ < _tmp25_) {
						ScratchServicesDocument* _tmp26_;
						const gchar* _tmp27_;
						_tmp26_ = d;
						_tmp27_ = new_tabname_d;
						scratch_services_document_set_title (_tmp26_, _tmp27_);
					}
					_tmp28_ = doc_tab_name;
					_tmp29_ = strlen (_tmp28_);
					_tmp30_ = _tmp29_;
					_tmp31_ = new_tabname_doc;
					_tmp32_ = strlen (_tmp31_);
					_tmp33_ = _tmp32_;
					if (_tmp30_ < _tmp33_) {
						const gchar* _tmp34_;
						gchar* _tmp35_;
						_tmp34_ = new_tabname_doc;
						_tmp35_ = g_strdup (_tmp34_);
						_g_free0 (doc_tab_name);
						doc_tab_name = _tmp35_;
					}
				}
				_g_free0 (new_tabname_d);
				_g_free0 (new_tabname_doc);
				_g_object_unref0 (d);
			}
		}
	}
	_tmp36_ = doc_tab_name;
	scratch_services_document_set_title (doc, _tmp36_);
	_g_free0 (doc_tab_name);
}

static void
scratch_widgets_document_view_save_focused_document_uri (ScratchWidgetsDocumentView* self,
                                                         ScratchServicesDocument* current_document)
{
	GSettings* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = scratch_privacy_settings;
	if (g_settings_get_boolean (_tmp0_, "remember-recent-files")) {
		gchar* file_uri = NULL;
		gchar* _tmp1_;
		GSettings* _tmp5_;
		const gchar* _tmp6_;
		_tmp1_ = g_strdup ("");
		file_uri = _tmp1_;
		if (current_document != NULL) {
			GFile* _tmp2_;
			GFile* _tmp3_;
			gchar* _tmp4_;
			_tmp2_ = scratch_services_document_get_file (current_document);
			_tmp3_ = _tmp2_;
			_tmp4_ = g_file_get_uri (_tmp3_);
			_g_free0 (file_uri);
			file_uri = _tmp4_;
		}
		_tmp5_ = scratch_settings;
		_tmp6_ = file_uri;
		g_settings_set_string (_tmp5_, "focused-document", _tmp6_);
		_g_free0 (file_uri);
	}
}

static GMenu*
scratch_widgets_document_view_create_menu_model (ScratchWidgetsDocumentView* self)
{
	GMenu* menu = NULL;
	GMenu* _tmp0_;
	GMenu* close_tab_section = NULL;
	GMenu* _tmp1_;
	GMenu* open_tab_section = NULL;
	GMenu* _tmp2_;
	GMenu* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_menu_new ();
	menu = _tmp0_;
	_tmp1_ = g_menu_new ();
	close_tab_section = _tmp1_;
	g_menu_append (close_tab_section, _ ("Close Tabs to the Right"), SCRATCH_MAIN_WINDOW_ACTION_PREFIX SCRATCH_MAIN_WINDOW_ACTION_CLOSE_TABS_TO_RIGHT);
	g_menu_append (close_tab_section, _ ("Close Other Tabs"), SCRATCH_MAIN_WINDOW_ACTION_PREFIX SCRATCH_MAIN_WINDOW_ACTION_CLOSE_OTHER_TABS);
	g_menu_append (close_tab_section, _ ("Close Tab"), SCRATCH_MAIN_WINDOW_ACTION_PREFIX SCRATCH_MAIN_WINDOW_ACTION_CLOSE_TAB "::");
	_tmp2_ = g_menu_new ();
	open_tab_section = _tmp2_;
	g_menu_append (open_tab_section, _ ("Open in New Window"), SCRATCH_MAIN_WINDOW_ACTION_PREFIX SCRATCH_MAIN_WINDOW_ACTION_MOVE_TAB_TO_NEW_WINDOW);
	g_menu_append (open_tab_section, _ ("Duplicate Tab"), SCRATCH_MAIN_WINDOW_ACTION_PREFIX SCRATCH_MAIN_WINDOW_ACTION_DUPLICATE_TAB);
	g_menu_append_section (menu, NULL, (GMenuModel*) open_tab_section);
	g_menu_append_section (menu, NULL, (GMenuModel*) close_tab_section);
	result = menu;
	_g_object_unref0 (open_tab_section);
	_g_object_unref0 (close_tab_section);
	return result;
}

static void
scratch_widgets_document_view_drag_received (ScratchWidgetsDocumentView* self,
                                             GtkWidget* w,
                                             GdkDragContext* ctx,
                                             gint x,
                                             gint y,
                                             GtkSelectionData* sel,
                                             guint info,
                                             guint time)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (w != NULL);
	g_return_if_fail (ctx != NULL);
	g_return_if_fail (sel != NULL);
	if (info == ((guint) SCRATCH_WIDGETS_DOCUMENT_VIEW_TARGET_TYPE_URI_LIST)) {
		gchar** uris = NULL;
		gchar** _tmp0_;
		gchar** _tmp1_;
		gint uris_length1;
		gint _uris_size_;
		gchar** _tmp2_;
		gint _tmp2__length1;
		_tmp1_ = _tmp0_ = gtk_selection_data_get_uris (sel);
		uris = _tmp1_;
		uris_length1 = _vala_array_length (_tmp0_);
		_uris_size_ = uris_length1;
		_tmp2_ = uris;
		_tmp2__length1 = uris_length1;
		{
			gchar** filename_collection = NULL;
			gint filename_collection_length1 = 0;
			gint _filename_collection_size_ = 0;
			gint filename_it = 0;
			filename_collection = _tmp2_;
			filename_collection_length1 = _tmp2__length1;
			for (filename_it = 0; filename_it < filename_collection_length1; filename_it = filename_it + 1) {
				gchar* _tmp3_;
				gchar* filename = NULL;
				_tmp3_ = g_strdup (filename_collection[filename_it]);
				filename = _tmp3_;
				{
					GFile* file = NULL;
					const gchar* _tmp4_;
					GFile* _tmp5_;
					ScratchServicesDocument* doc = NULL;
					ScratchMainWindow* _tmp6_;
					GSimpleActionGroup* _tmp7_;
					GSimpleActionGroup* _tmp8_;
					GFile* _tmp9_;
					ScratchServicesDocument* _tmp10_;
					ScratchServicesDocument* _tmp11_;
					_tmp4_ = filename;
					_tmp5_ = g_file_new_for_uri (_tmp4_);
					file = _tmp5_;
					_tmp6_ = self->priv->_window;
					_tmp7_ = scratch_main_window_get_actions (_tmp6_);
					_tmp8_ = _tmp7_;
					_tmp9_ = file;
					_tmp10_ = scratch_services_document_new (_tmp8_, _tmp9_);
					g_object_ref_sink (_tmp10_);
					doc = _tmp10_;
					_tmp11_ = doc;
					scratch_widgets_document_view_open_document (self, _tmp11_, TRUE, 0, &SELECTION_RANGE_EMPTY, NULL, NULL);
					_g_object_unref0 (doc);
					_g_object_unref0 (file);
					_g_free0 (filename);
				}
			}
		}
		gtk_drag_finish (ctx, TRUE, FALSE, (guint32) time);
		uris = (_vala_array_free (uris, uris_length1, (GDestroyNotify) g_free), NULL);
	}
}

ScratchServicesDocument*
scratch_widgets_document_view_get_current_document (ScratchWidgetsDocumentView* self)
{
	ScratchServicesDocument* result;
	ScratchServicesDocument* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_current_document;
	result = _tmp0_;
	return result;
}

void
scratch_widgets_document_view_set_current_document (ScratchWidgetsDocumentView* self,
                                                    ScratchServicesDocument* value)
{
	gboolean _tmp0_ = FALSE;
	ScratchServicesDocument* _tmp2_;
	ScratchServicesDocument* _tmp3_;
	ScratchServicesDocument* _tmp4_;
	ScratchServicesDocument* _tmp5_;
	ScratchServicesDocument* _tmp6_;
	gboolean _tmp7_ = FALSE;
	HdyTabView* _tmp8_;
	HdyTabPage* _tmp9_;
	HdyTabPage* _tmp10_;
	HdyTabPage* _tmp11_;
	HdyTabPage* _tmp12_;
	g_return_if_fail (self != NULL);
	if (self->is_closing) {
		_tmp0_ = TRUE;
	} else {
		ScratchServicesDocument* _tmp1_;
		_tmp1_ = self->priv->_current_document;
		_tmp0_ = _tmp1_ == value;
	}
	if (_tmp0_) {
		return;
	}
	_tmp2_ = _g_object_ref0 (value);
	_g_object_unref0 (self->priv->_current_document);
	self->priv->_current_document = _tmp2_;
	_tmp3_ = self->priv->_current_document;
	g_signal_emit (self, scratch_widgets_document_view_signals[SCRATCH_WIDGETS_DOCUMENT_VIEW_DOCUMENT_CHANGE_SIGNAL], 0, _tmp3_);
	_tmp4_ = self->priv->_current_document;
	scratch_services_document_focus (_tmp4_);
	_tmp5_ = scratch_widgets_document_view_get_current_document (self);
	_tmp6_ = _tmp5_;
	scratch_widgets_document_view_save_focused_document_uri (self, _tmp6_);
	_tmp8_ = self->priv->tab_view;
	_tmp9_ = hdy_tab_view_get_selected_page (_tmp8_);
	_tmp10_ = _tmp9_;
	_tmp11_ = scratch_services_document_get_tab (value);
	_tmp12_ = _tmp11_;
	if (_tmp10_ != _tmp12_) {
		HdyTabPage* _tmp13_;
		HdyTabPage* _tmp14_;
		_tmp13_ = scratch_services_document_get_tab (value);
		_tmp14_ = _tmp13_;
		_tmp7_ = _tmp14_ != NULL;
	} else {
		_tmp7_ = FALSE;
	}
	if (_tmp7_) {
		HdyTabView* _tmp15_;
		HdyTabPage* _tmp16_;
		HdyTabPage* _tmp17_;
		_tmp15_ = self->priv->tab_view;
		_tmp16_ = scratch_services_document_get_tab (value);
		_tmp17_ = _tmp16_;
		hdy_tab_view_set_selected_page (_tmp15_, _tmp17_);
	}
	g_object_notify_by_pspec ((GObject *) self, scratch_widgets_document_view_properties[SCRATCH_WIDGETS_DOCUMENT_VIEW_CURRENT_DOCUMENT_PROPERTY]);
}

ScratchMainWindow*
scratch_widgets_document_view_get_window (ScratchWidgetsDocumentView* self)
{
	ScratchMainWindow* result;
	ScratchMainWindow* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_window;
	result = _tmp0_;
	return result;
}

static void
scratch_widgets_document_view_set_window (ScratchWidgetsDocumentView* self,
                                          ScratchMainWindow* value)
{
	ScratchMainWindow* old_value;
	g_return_if_fail (self != NULL);
	old_value = scratch_widgets_document_view_get_window (self);
	if (old_value != value) {
		self->priv->_window = value;
		g_object_notify_by_pspec ((GObject *) self, scratch_widgets_document_view_properties[SCRATCH_WIDGETS_DOCUMENT_VIEW_WINDOW_PROPERTY]);
	}
}

gboolean
scratch_widgets_document_view_get_outline_visible (ScratchWidgetsDocumentView* self)
{
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	result = self->priv->_outline_visible;
	return result;
}

void
scratch_widgets_document_view_set_outline_visible (ScratchWidgetsDocumentView* self,
                                                   gboolean value)
{
	gboolean old_value;
	g_return_if_fail (self != NULL);
	old_value = scratch_widgets_document_view_get_outline_visible (self);
	if (old_value != value) {
		self->priv->_outline_visible = value;
		g_object_notify_by_pspec ((GObject *) self, scratch_widgets_document_view_properties[SCRATCH_WIDGETS_DOCUMENT_VIEW_OUTLINE_VISIBLE_PROPERTY]);
	}
}

gint
scratch_widgets_document_view_get_outline_width (ScratchWidgetsDocumentView* self)
{
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	result = self->priv->_outline_width;
	return result;
}

void
scratch_widgets_document_view_set_outline_width (ScratchWidgetsDocumentView* self,
                                                 gint value)
{
	gint old_value;
	g_return_if_fail (self != NULL);
	old_value = scratch_widgets_document_view_get_outline_width (self);
	if (old_value != value) {
		self->priv->_outline_width = value;
		g_object_notify_by_pspec ((GObject *) self, scratch_widgets_document_view_properties[SCRATCH_WIDGETS_DOCUMENT_VIEW_OUTLINE_WIDTH_PROPERTY]);
	}
}

static void
_scratch_widgets_document_view_tab_view_setup_menu_hdy_tab_view_setup_menu (HdyTabView* _sender,
                                                                            HdyTabPage* page,
                                                                            gpointer self)
{
	scratch_widgets_document_view_tab_view_setup_menu ((ScratchWidgetsDocumentView*) self, page);
}

static void
_scratch_widgets_document_view___lambda67_ (ScratchWidgetsDocumentView* self)
{
	HdyTabView* _tmp0_;
	HdyTabPage* _tmp1_;
	HdyTabPage* _tmp2_;
	HdyTabView* _tmp3_;
	HdyTabPage* _tmp4_;
	HdyTabPage* _tmp5_;
	GtkWidget* _tmp6_;
	GtkWidget* _tmp7_;
	_tmp0_ = self->priv->tab_view;
	_tmp1_ = hdy_tab_view_get_selected_page (_tmp0_);
	_tmp2_ = _tmp1_;
	if (_tmp2_ == NULL) {
		return;
	}
	_tmp3_ = self->priv->tab_view;
	_tmp4_ = hdy_tab_view_get_selected_page (_tmp3_);
	_tmp5_ = _tmp4_;
	_tmp6_ = hdy_tab_page_get_child (_tmp5_);
	_tmp7_ = _tmp6_;
	scratch_widgets_document_view_set_current_document (self, SCRATCH_SERVICES_IS_DOCUMENT (_tmp7_) ? ((ScratchServicesDocument*) _tmp7_) : NULL);
}

static void
__scratch_widgets_document_view___lambda67__g_object_notify (GObject* _sender,
                                                             GParamSpec* pspec,
                                                             gpointer self)
{
	_scratch_widgets_document_view___lambda67_ ((ScratchWidgetsDocumentView*) self);
}

static void
_scratch_widgets_document_view___lambda68_ (ScratchWidgetsDocumentView* self)
{
	scratch_widgets_document_view_new_document (self);
}

static void
__scratch_widgets_document_view___lambda68__gtk_button_clicked (GtkButton* _sender,
                                                                gpointer self)
{
	_scratch_widgets_document_view___lambda68_ ((ScratchWidgetsDocumentView*) self);
}

static Block58Data*
block58_data_ref (Block58Data* _data58_)
{
	g_atomic_int_inc (&_data58_->_ref_count_);
	return _data58_;
}

static void
block58_data_unref (void * _userdata_)
{
	Block58Data* _data58_;
	_data58_ = (Block58Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data58_->_ref_count_)) {
		ScratchWidgetsDocumentView* self;
		self = _data58_->self;
		_g_object_unref0 (_data58_->doc);
		_g_object_unref0 (_data58_->tab);
		_g_object_unref0 (self);
		g_slice_free (Block58Data, _data58_);
	}
}

static void
___lambda70_ (Block58Data* _data58_,
              GObject* obj,
              GAsyncResult* res)
{
	ScratchWidgetsDocumentView* self;
	gboolean should_close = FALSE;
	ScratchServicesDocument* _tmp0_;
	HdyTabView* _tmp1_;
	self = _data58_->self;
	g_return_if_fail (res != NULL);
	_tmp0_ = _data58_->doc;
	should_close = scratch_services_document_do_close_finish (_tmp0_, res);
	if (!self->is_closing) {
		scratch_widgets_document_view_update_opened_files_setting (self);
	}
	_tmp1_ = self->priv->tab_view;
	hdy_tab_view_close_page_finish (_tmp1_, _data58_->tab, should_close);
}

static void
____lambda70__gasync_ready_callback (GObject* source_object,
                                     GAsyncResult* res,
                                     gpointer self)
{
	___lambda70_ (self, source_object, res);
	block58_data_unref (self);
}

static gboolean
_scratch_widgets_document_view___lambda69_ (ScratchWidgetsDocumentView* self,
                                            HdyTabPage* tab)
{
	Block58Data* _data58_;
	HdyTabPage* _tmp0_;
	GtkWidget* _tmp1_;
	GtkWidget* _tmp2_;
	ScratchServicesDocument* _tmp3_;
	gboolean _tmp4_ = FALSE;
	ScratchServicesDocument* _tmp5_;
	ScratchServicesDocument* _tmp9_;
	gboolean result;
	g_return_val_if_fail (tab != NULL, FALSE);
	_data58_ = g_slice_new0 (Block58Data);
	_data58_->_ref_count_ = 1;
	_data58_->self = g_object_ref (self);
	_tmp0_ = _g_object_ref0 (tab);
	_g_object_unref0 (_data58_->tab);
	_data58_->tab = _tmp0_;
	_tmp1_ = hdy_tab_page_get_child (_data58_->tab);
	_tmp2_ = _tmp1_;
	_tmp3_ = _g_object_ref0 (SCRATCH_SERVICES_IS_DOCUMENT (_tmp2_) ? ((ScratchServicesDocument*) _tmp2_) : NULL);
	_data58_->doc = _tmp3_;
	_tmp5_ = _data58_->doc;
	if (_tmp5_ == NULL) {
		_tmp4_ = TRUE;
	} else {
		ScratchServicesDocument* _tmp6_;
		gboolean _tmp7_;
		gboolean _tmp8_;
		_tmp6_ = _data58_->doc;
		_tmp7_ = scratch_services_document_get_closing (_tmp6_);
		_tmp8_ = _tmp7_;
		_tmp4_ = _tmp8_;
	}
	if (_tmp4_) {
		result = TRUE;
		block58_data_unref (_data58_);
		_data58_ = NULL;
		return result;
	}
	_tmp9_ = _data58_->doc;
	if (_tmp9_ == NULL) {
		HdyTabView* _tmp10_;
		_tmp10_ = self->priv->tab_view;
		hdy_tab_view_close_page_finish (_tmp10_, _data58_->tab, TRUE);
	} else {
		ScratchServicesDocument* _tmp11_;
		_tmp11_ = _data58_->doc;
		scratch_services_document_do_close (_tmp11_, FALSE, ____lambda70__gasync_ready_callback, block58_data_ref (_data58_));
	}
	result = TRUE;
	block58_data_unref (_data58_);
	_data58_ = NULL;
	return result;
}

static gboolean
__scratch_widgets_document_view___lambda69__hdy_tab_view_close_page (HdyTabView* _sender,
                                                                     HdyTabPage* page,
                                                                     gpointer self)
{
	gboolean result;
	result = _scratch_widgets_document_view___lambda69_ ((ScratchWidgetsDocumentView*) self, page);
	return result;
}

static void
_scratch_widgets_document_view_on_doc_added_hdy_tab_view_page_attached (HdyTabView* _sender,
                                                                        HdyTabPage* page,
                                                                        gint position,
                                                                        gpointer self)
{
	scratch_widgets_document_view_on_doc_added ((ScratchWidgetsDocumentView*) self, page, position);
}

static void
_scratch_widgets_document_view_on_page_detached_hdy_tab_view_page_detached (HdyTabView* _sender,
                                                                            HdyTabPage* page,
                                                                            gint position,
                                                                            gpointer self)
{
	scratch_widgets_document_view_on_page_detached ((ScratchWidgetsDocumentView*) self, page, position);
}

static void
_scratch_widgets_document_view_on_doc_reordered_hdy_tab_view_page_reordered (HdyTabView* _sender,
                                                                             HdyTabPage* page,
                                                                             gint position,
                                                                             gpointer self)
{
	scratch_widgets_document_view_on_doc_reordered ((ScratchWidgetsDocumentView*) self, page, position);
}

static HdyTabView*
_scratch_widgets_document_view_on_doc_to_new_window_hdy_tab_view_create_window (HdyTabView* _sender,
                                                                                gpointer self)
{
	HdyTabView* result;
	result = scratch_widgets_document_view_on_doc_to_new_window ((ScratchWidgetsDocumentView*) self, _sender);
	return result;
}

static void
_scratch_widgets_document_view_update_inline_tab_colors_g_settings_changed (GSettings* _sender,
                                                                            const gchar* key,
                                                                            gpointer self)
{
	scratch_widgets_document_view_update_inline_tab_colors ((ScratchWidgetsDocumentView*) self);
}

static void
_scratch_widgets_document_view_update_inline_tab_colors_g_object_notify (GObject* _sender,
                                                                         GParamSpec* pspec,
                                                                         gpointer self)
{
	scratch_widgets_document_view_update_inline_tab_colors ((ScratchWidgetsDocumentView*) self);
}

static void
_scratch_widgets_document_view_update_outline_visible_g_object_notify (GObject* _sender,
                                                                       GParamSpec* pspec,
                                                                       gpointer self)
{
	scratch_widgets_document_view_update_outline_visible ((ScratchWidgetsDocumentView*) self);
}

static void
_scratch_widgets_document_view___lambda71_ (ScratchWidgetsDocumentView* self)
{
	GList* _tmp0_;
	_tmp0_ = self->docs;
	{
		GList* doc_collection = NULL;
		GList* doc_it = NULL;
		doc_collection = _tmp0_;
		for (doc_it = doc_collection; doc_it != NULL; doc_it = doc_it->next) {
			ScratchServicesDocument* _tmp1_;
			ScratchServicesDocument* doc = NULL;
			_tmp1_ = _g_object_ref0 ((ScratchServicesDocument*) doc_it->data);
			doc = _tmp1_;
			{
				ScratchServicesDocument* _tmp2_;
				gint _tmp3_;
				_tmp2_ = doc;
				_tmp3_ = self->priv->_outline_width;
				scratch_services_document_set_outline_width (_tmp2_, _tmp3_);
				_g_object_unref0 (doc);
			}
		}
	}
}

static void
__scratch_widgets_document_view___lambda71__g_object_notify (GObject* _sender,
                                                             GParamSpec* pspec,
                                                             gpointer self)
{
	_scratch_widgets_document_view___lambda71_ ((ScratchWidgetsDocumentView*) self);
}

static void
_scratch_widgets_document_view_drag_received_gtk_widget_drag_data_received (GtkWidget* _sender,
                                                                            GdkDragContext* context,
                                                                            gint x,
                                                                            gint y,
                                                                            GtkSelectionData* selection_data,
                                                                            guint info,
                                                                            guint time_,
                                                                            gpointer self)
{
	scratch_widgets_document_view_drag_received ((ScratchWidgetsDocumentView*) self, _sender, context, x, y, selection_data, info, time_);
}

static GObject *
scratch_widgets_document_view_constructor (GType type,
                                           guint n_construct_properties,
                                           GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	ScratchWidgetsDocumentView * self;
	GtkApplication* app_instance = NULL;
	GApplication* _tmp0_;
	GtkApplication* _tmp1_;
	HdyTabView* _tmp2_ = NULL;
	HdyTabView* _tmp3_;
	GMenu* _tmp4_;
	GMenu* _tmp5_;
	HdyTabView* _tmp6_;
	HdyTabView* _tmp7_;
	GtkButton* new_tab_button = NULL;
	GtkApplication* _tmp8_;
	gchar** _tmp9_;
	gchar** _tmp10_;
	gchar** _tmp11_;
	gint _tmp11__length1;
	gchar* _tmp12_;
	GtkButton* _tmp13_ = NULL;
	GtkButton* _tmp14_;
	GtkButton* _tmp15_;
	GtkImage* _tmp16_;
	gchar* _tmp17_;
	GtkMenuButton* _tmp18_ = NULL;
	GtkButton* _tmp19_;
	GtkWidget* _tmp20_;
	GtkMenuButton* _tmp21_;
	GtkWidget* _tmp22_;
	HdyTabView* _tmp23_;
	HdyTabView* _tmp24_;
	HdyTabBar* _tmp25_ = NULL;
	HdyTabView* _tmp26_;
	HdyTabView* _tmp27_;
	HdyTabView* _tmp28_;
	HdyTabView* _tmp29_;
	HdyTabView* _tmp30_;
	GtkCssProvider* _tmp31_;
	GdkScreen* _tmp32_;
	GtkCssProvider* _tmp33_;
	GSettings* _tmp34_;
	GSettings* _tmp35_;
	GraniteSettings* granite_settings = NULL;
	GraniteSettings* _tmp36_;
	GraniteSettings* _tmp37_;
	GraniteSettings* _tmp38_;
	GSettings* _tmp39_;
	GtkTargetEntry uris = {0};
	GtkTargetEntry _tmp40_ = {0};
	HdyTabBar* _tmp41_;
	GtkTargetEntry _tmp42_;
	GtkTargetEntry* _tmp43_;
	GtkTargetEntry* _tmp44_;
	gint _tmp44__length1;
	HdyTabBar* _tmp45_;
	HdyTabBar* _tmp46_;
	HdyTabView* _tmp47_;
	parent_class = G_OBJECT_CLASS (scratch_widgets_document_view_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SCRATCH_WIDGETS_TYPE_DOCUMENT_VIEW, ScratchWidgetsDocumentView);
	(self->docs == NULL) ? NULL : (self->docs = (_g_list_free__g_object_unref0_ (self->docs), NULL));
	self->docs = NULL;
	_tmp0_ = g_application_get_default ();
	_tmp1_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp0_, gtk_application_get_type (), GtkApplication));
	app_instance = _tmp1_;
	_tmp2_ = hdy_tab_view_new ();
	gtk_widget_set_hexpand ((GtkWidget*) _tmp2_, TRUE);
	gtk_widget_set_vexpand ((GtkWidget*) _tmp2_, TRUE);
	g_object_ref_sink (_tmp2_);
	_g_object_unref0 (self->priv->tab_view);
	self->priv->tab_view = _tmp2_;
	_tmp3_ = self->priv->tab_view;
	_tmp4_ = scratch_widgets_document_view_create_menu_model (self);
	_tmp5_ = _tmp4_;
	hdy_tab_view_set_menu_model (_tmp3_, (GMenuModel*) _tmp5_);
	_g_object_unref0 (_tmp5_);
	_tmp6_ = self->priv->tab_view;
	g_signal_connect_object (_tmp6_, "setup-menu", (GCallback) _scratch_widgets_document_view_tab_view_setup_menu_hdy_tab_view_setup_menu, self, 0);
	_tmp7_ = self->priv->tab_view;
	g_signal_connect_object ((GObject*) _tmp7_, "notify::selected-page", (GCallback) __scratch_widgets_document_view___lambda67__g_object_notify, self, 0);
	_tmp8_ = app_instance;
	_tmp10_ = _tmp9_ = gtk_application_get_accels_for_action (_tmp8_, SCRATCH_MAIN_WINDOW_ACTION_PREFIX SCRATCH_MAIN_WINDOW_ACTION_NEW_TAB);
	_tmp11_ = _tmp10_;
	_tmp11__length1 = _vala_array_length (_tmp9_);
	_tmp12_ = granite_markup_accel_tooltip (_tmp11_, (gint) _vala_array_length (_tmp9_), _ ("New Tab"));
	_tmp13_ = (GtkButton*) gtk_button_new_from_icon_name ("list-add-symbolic", (GtkIconSize) GTK_ICON_SIZE_BUTTON);
	gtk_button_set_relief (_tmp13_, GTK_RELIEF_NONE);
	gtk_widget_set_tooltip_markup ((GtkWidget*) _tmp13_, _tmp12_);
	_g_free0 (_tmp12_);
	g_object_ref_sink (_tmp13_);
	_tmp14_ = _tmp13_;
	_tmp11_ = (_vala_array_free (_tmp11_, _tmp11__length1, (GDestroyNotify) g_free), NULL);
	new_tab_button = _tmp14_;
	_tmp15_ = new_tab_button;
	g_signal_connect_object (_tmp15_, "clicked", (GCallback) __scratch_widgets_document_view___lambda68__gtk_button_clicked, self, 0);
	_tmp16_ = (GtkImage*) gtk_image_new_from_icon_name ("document-open-recent-symbolic", (GtkIconSize) GTK_ICON_SIZE_MENU);
	g_object_ref_sink (_tmp16_);
	_tmp17_ = g_strdup (_ ("Closed Tabs"));
	_tmp18_ = (GtkMenuButton*) gtk_menu_button_new ();
	gtk_button_set_image ((GtkButton*) _tmp18_, (GtkWidget*) _tmp16_);
	_g_object_unref0 (_tmp16_);
	gtk_widget_set_tooltip_text ((GtkWidget*) _tmp18_, _tmp17_);
	_g_free0 (_tmp17_);
	gtk_menu_button_set_use_popover (_tmp18_, FALSE);
	g_object_ref_sink (_tmp18_);
	_g_object_unref0 (self->priv->tab_history_button);
	self->priv->tab_history_button = _tmp18_;
	_tmp19_ = new_tab_button;
	_tmp20_ = _g_object_ref0 ((GtkWidget*) _tmp19_);
	_tmp21_ = self->priv->tab_history_button;
	_tmp22_ = _g_object_ref0 ((GtkWidget*) _tmp21_);
	_tmp23_ = self->priv->tab_view;
	_tmp24_ = _g_object_ref0 (_tmp23_);
	_tmp25_ = hdy_tab_bar_new ();
	hdy_tab_bar_set_autohide (_tmp25_, FALSE);
	hdy_tab_bar_set_expand_tabs (_tmp25_, FALSE);
	hdy_tab_bar_set_inverted (_tmp25_, TRUE);
	hdy_tab_bar_set_start_action_widget (_tmp25_, _tmp20_);
	_g_object_unref0 (_tmp20_);
	hdy_tab_bar_set_end_action_widget (_tmp25_, _tmp22_);
	_g_object_unref0 (_tmp22_);
	hdy_tab_bar_set_view (_tmp25_, _tmp24_);
	_g_object_unref0 (_tmp24_);
	g_object_ref_sink (_tmp25_);
	_g_object_unref0 (self->priv->tab_bar);
	self->priv->tab_bar = _tmp25_;
	_tmp26_ = self->priv->tab_view;
	g_signal_connect_object (_tmp26_, "close-page", (GCallback) __scratch_widgets_document_view___lambda69__hdy_tab_view_close_page, self, 0);
	_tmp27_ = self->priv->tab_view;
	g_signal_connect_object (_tmp27_, "page-attached", (GCallback) _scratch_widgets_document_view_on_doc_added_hdy_tab_view_page_attached, self, 0);
	_tmp28_ = self->priv->tab_view;
	g_signal_connect_object (_tmp28_, "page-detached", (GCallback) _scratch_widgets_document_view_on_page_detached_hdy_tab_view_page_detached, self, 0);
	_tmp29_ = self->priv->tab_view;
	g_signal_connect_object (_tmp29_, "page-reordered", (GCallback) _scratch_widgets_document_view_on_doc_reordered_hdy_tab_view_page_reordered, self, 0);
	_tmp30_ = self->priv->tab_view;
	g_signal_connect_object (_tmp30_, "create-window", (GCallback) _scratch_widgets_document_view_on_doc_to_new_window_hdy_tab_view_create_window, self, 0);
	_tmp31_ = gtk_css_provider_new ();
	_g_object_unref0 (self->priv->style_provider);
	self->priv->style_provider = _tmp31_;
	_tmp32_ = gdk_screen_get_default ();
	_tmp33_ = self->priv->style_provider;
	gtk_style_context_add_provider_for_screen (_tmp32_, (GtkStyleProvider*) _tmp33_, (guint) GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
	scratch_widgets_document_view_update_inline_tab_colors (self);
	_tmp34_ = scratch_settings;
	g_signal_connect_object (_tmp34_, "changed::style-scheme", (GCallback) _scratch_widgets_document_view_update_inline_tab_colors_g_settings_changed, self, 0);
	_tmp35_ = scratch_settings;
	g_signal_connect_object (_tmp35_, "changed::follow-system-style", (GCallback) _scratch_widgets_document_view_update_inline_tab_colors_g_settings_changed, self, 0);
	_tmp36_ = granite_settings_get_default ();
	_tmp37_ = _g_object_ref0 (_tmp36_);
	granite_settings = _tmp37_;
	_tmp38_ = granite_settings;
	g_signal_connect_object ((GObject*) _tmp38_, "notify::prefers-color-scheme", (GCallback) _scratch_widgets_document_view_update_inline_tab_colors_g_object_notify, self, 0);
	g_signal_connect_object ((GObject*) self, "notify::outline-visible", (GCallback) _scratch_widgets_document_view_update_outline_visible_g_object_notify, self, 0);
	_tmp39_ = scratch_saved_state;
	g_settings_bind (_tmp39_, "outline-width", (GObject*) self, "outline-width", G_SETTINGS_BIND_DEFAULT);
	g_signal_connect_object ((GObject*) self, "notify::outline-width", (GCallback) __scratch_widgets_document_view___lambda71__g_object_notify, self, 0);
	_tmp40_.target = "text/uri-list";
	_tmp40_.flags = (guint) 0;
	_tmp40_.info = (guint) SCRATCH_WIDGETS_DOCUMENT_VIEW_TARGET_TYPE_URI_LIST;
	uris = _tmp40_;
	_tmp41_ = self->priv->tab_bar;
	_tmp42_ = uris;
	_tmp43_ = g_new0 (GtkTargetEntry, 1);
	_tmp43_[0] = _tmp42_;
	_tmp44_ = _tmp43_;
	_tmp44__length1 = 1;
	gtk_drag_dest_set ((GtkWidget*) _tmp41_, GTK_DEST_DEFAULT_ALL, _tmp44_, (gint) 1, GDK_ACTION_COPY);
	_tmp44_ = (g_free (_tmp44_), NULL);
	_tmp45_ = self->priv->tab_bar;
	g_signal_connect_object ((GtkWidget*) _tmp45_, "drag-data-received", (GCallback) _scratch_widgets_document_view_drag_received_gtk_widget_drag_data_received, self, 0);
	_tmp46_ = self->priv->tab_bar;
	gtk_container_add ((GtkContainer*) self, (GtkWidget*) _tmp46_);
	_tmp47_ = self->priv->tab_view;
	gtk_container_add ((GtkContainer*) self, (GtkWidget*) _tmp47_);
	_g_object_unref0 (granite_settings);
	_g_object_unref0 (new_tab_button);
	_g_object_unref0 (app_instance);
	return obj;
}

static void
scratch_widgets_document_view_class_init (ScratchWidgetsDocumentViewClass * klass,
                                          gpointer klass_data)
{
	scratch_widgets_document_view_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ScratchWidgetsDocumentView_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_scratch_widgets_document_view_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_scratch_widgets_document_view_set_property;
	G_OBJECT_CLASS (klass)->constructor = scratch_widgets_document_view_constructor;
	G_OBJECT_CLASS (klass)->finalize = scratch_widgets_document_view_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), SCRATCH_WIDGETS_DOCUMENT_VIEW_CURRENT_DOCUMENT_PROPERTY, scratch_widgets_document_view_properties[SCRATCH_WIDGETS_DOCUMENT_VIEW_CURRENT_DOCUMENT_PROPERTY] = g_param_spec_object ("current-document", "current-document", "current-document", SCRATCH_SERVICES_TYPE_DOCUMENT, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), SCRATCH_WIDGETS_DOCUMENT_VIEW_WINDOW_PROPERTY, scratch_widgets_document_view_properties[SCRATCH_WIDGETS_DOCUMENT_VIEW_WINDOW_PROPERTY] = g_param_spec_object ("window", "window", "window", SCRATCH_TYPE_MAIN_WINDOW, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
	g_object_class_install_property (G_OBJECT_CLASS (klass), SCRATCH_WIDGETS_DOCUMENT_VIEW_OUTLINE_VISIBLE_PROPERTY, scratch_widgets_document_view_properties[SCRATCH_WIDGETS_DOCUMENT_VIEW_OUTLINE_VISIBLE_PROPERTY] = g_param_spec_boolean ("outline-visible", "outline-visible", "outline-visible", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), SCRATCH_WIDGETS_DOCUMENT_VIEW_OUTLINE_WIDTH_PROPERTY, scratch_widgets_document_view_properties[SCRATCH_WIDGETS_DOCUMENT_VIEW_OUTLINE_WIDTH_PROPERTY] = g_param_spec_int ("outline-width", "outline-width", "outline-width", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	scratch_widgets_document_view_signals[SCRATCH_WIDGETS_DOCUMENT_VIEW_DOCUMENT_CHANGE_SIGNAL] = g_signal_new ("document-change", SCRATCH_WIDGETS_TYPE_DOCUMENT_VIEW, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, SCRATCH_SERVICES_TYPE_DOCUMENT);
	scratch_widgets_document_view_signals[SCRATCH_WIDGETS_DOCUMENT_VIEW_REQUEST_PLACEHOLDER_SIGNAL] = g_signal_new ("request-placeholder", SCRATCH_WIDGETS_TYPE_DOCUMENT_VIEW, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
	scratch_widgets_document_view_signals[SCRATCH_WIDGETS_DOCUMENT_VIEW_TAB_ADDED_SIGNAL] = g_signal_new ("tab-added", SCRATCH_WIDGETS_TYPE_DOCUMENT_VIEW, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, SCRATCH_SERVICES_TYPE_DOCUMENT);
	scratch_widgets_document_view_signals[SCRATCH_WIDGETS_DOCUMENT_VIEW_TAB_REMOVED_SIGNAL] = g_signal_new ("tab-removed", SCRATCH_WIDGETS_TYPE_DOCUMENT_VIEW, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, SCRATCH_SERVICES_TYPE_DOCUMENT);
}

static void
scratch_widgets_document_view_instance_init (ScratchWidgetsDocumentView * self,
                                             gpointer klass)
{
	self->priv = scratch_widgets_document_view_get_instance_private (self);
	self->is_closing = FALSE;
	self->priv->_outline_visible = FALSE;
	self->priv->tab_menu_target = NULL;
}

static void
scratch_widgets_document_view_finalize (GObject * obj)
{
	ScratchWidgetsDocumentView * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SCRATCH_WIDGETS_TYPE_DOCUMENT_VIEW, ScratchWidgetsDocumentView);
	(self->docs == NULL) ? NULL : (self->docs = (_g_list_free__g_object_unref0_ (self->docs), NULL));
	_g_object_unref0 (self->priv->_current_document);
	_g_object_unref0 (self->priv->tab_view);
	_g_object_unref0 (self->priv->tab_bar);
	_g_object_unref0 (self->priv->style_provider);
	_g_object_unref0 (self->priv->tab_history_button);
	G_OBJECT_CLASS (scratch_widgets_document_view_parent_class)->finalize (obj);
}

/***
  BEGIN LICENSE

  Copyright (C) 2013 Mario Guerriero <mario@elementaryos.org>
                2024 Colin Kiama <colinkiama@gmail.com>
                2025-2026 elementary LLC. (https://elementary.io),
  This program is free software: you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License version 3, as published
  by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranties of
  MERCHANTABILITY, SATISFACTORY QUALITY, 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 this program.  If not, see <http://www.gnu.org/licenses/>

  END LICENSE
***/
 G_GNUC_NO_INLINE static GType
scratch_widgets_document_view_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ScratchWidgetsDocumentViewClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) scratch_widgets_document_view_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ScratchWidgetsDocumentView), 0, (GInstanceInitFunc) scratch_widgets_document_view_instance_init, NULL };
	GType scratch_widgets_document_view_type_id;
	scratch_widgets_document_view_type_id = g_type_register_static (gtk_box_get_type (), "ScratchWidgetsDocumentView", &g_define_type_info, G_TYPE_FLAG_NONE);
	ScratchWidgetsDocumentView_private_offset = g_type_add_instance_private (scratch_widgets_document_view_type_id, sizeof (ScratchWidgetsDocumentViewPrivate));
	return scratch_widgets_document_view_type_id;
}

GType
scratch_widgets_document_view_get_type (void)
{
	static gsize scratch_widgets_document_view_type_id__once = 0;
	if (g_once_init_enter (&scratch_widgets_document_view_type_id__once)) {
		GType scratch_widgets_document_view_type_id;
		scratch_widgets_document_view_type_id = scratch_widgets_document_view_get_type_once ();
		g_once_init_leave (&scratch_widgets_document_view_type_id__once, scratch_widgets_document_view_type_id);
	}
	return scratch_widgets_document_view_type_id__once;
}

static void
_vala_scratch_widgets_document_view_get_property (GObject * object,
                                                  guint property_id,
                                                  GValue * value,
                                                  GParamSpec * pspec)
{
	ScratchWidgetsDocumentView * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, SCRATCH_WIDGETS_TYPE_DOCUMENT_VIEW, ScratchWidgetsDocumentView);
	switch (property_id) {
		case SCRATCH_WIDGETS_DOCUMENT_VIEW_CURRENT_DOCUMENT_PROPERTY:
		g_value_set_object (value, scratch_widgets_document_view_get_current_document (self));
		break;
		case SCRATCH_WIDGETS_DOCUMENT_VIEW_WINDOW_PROPERTY:
		g_value_set_object (value, scratch_widgets_document_view_get_window (self));
		break;
		case SCRATCH_WIDGETS_DOCUMENT_VIEW_OUTLINE_VISIBLE_PROPERTY:
		g_value_set_boolean (value, scratch_widgets_document_view_get_outline_visible (self));
		break;
		case SCRATCH_WIDGETS_DOCUMENT_VIEW_OUTLINE_WIDTH_PROPERTY:
		g_value_set_int (value, scratch_widgets_document_view_get_outline_width (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_scratch_widgets_document_view_set_property (GObject * object,
                                                  guint property_id,
                                                  const GValue * value,
                                                  GParamSpec * pspec)
{
	ScratchWidgetsDocumentView * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, SCRATCH_WIDGETS_TYPE_DOCUMENT_VIEW, ScratchWidgetsDocumentView);
	switch (property_id) {
		case SCRATCH_WIDGETS_DOCUMENT_VIEW_CURRENT_DOCUMENT_PROPERTY:
		scratch_widgets_document_view_set_current_document (self, g_value_get_object (value));
		break;
		case SCRATCH_WIDGETS_DOCUMENT_VIEW_WINDOW_PROPERTY:
		scratch_widgets_document_view_set_window (self, g_value_get_object (value));
		break;
		case SCRATCH_WIDGETS_DOCUMENT_VIEW_OUTLINE_VISIBLE_PROPERTY:
		scratch_widgets_document_view_set_outline_visible (self, g_value_get_boolean (value));
		break;
		case SCRATCH_WIDGETS_DOCUMENT_VIEW_OUTLINE_WIDTH_PROPERTY:
		scratch_widgets_document_view_set_outline_width (self, g_value_get_int (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static gssize
_vala_array_length (gpointer array)
{
	gssize length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}

