Ejemplo n.º 1
0
void PdfBookmarks::createBookmarks(GtkTreeModel * model, GList * &data, GtkTreeIter * iter, int level, Document * doc) {
	XOJ_CHECK_TYPE(PdfBookmarks);

	XojLinkDest * link = NULL;
	LinkDestination * dest = NULL;

	do {
		gtk_tree_model_get(model, iter, DOCUMENT_LINKS_COLUMN_LINK, &link, -1);
		dest = link->dest;

		g_object_unref(link);

		int page = doc->findPdfPage(dest->getPdfPage());
		if (page == -1) {
			continue;
		}

		data = g_list_append(data, new Bookmark(dest->getName(), level, page, dest->getTop()));

		GtkTreeIter children = { 0 };

		if (gtk_tree_model_iter_children(model, &children, iter)) {
			createBookmarks(model, data, &children, level + 1, doc);
		}
	} while (gtk_tree_model_iter_next(model, iter));
}
bool SidebarIndexPage::selectPageNr(size_t page, size_t pdfPage, GtkTreeIter* parent)
{
	XOJ_CHECK_TYPE(SidebarIndexPage);

	GtkTreeIter iter;

	Document* doc = control->getDocument();
	doc->lock();
	GtkTreeModel* model = doc->getContentsModel();
	if (model == NULL)
	{
		doc->unlock();
		return false;
	}
	g_object_ref(model);
	doc->unlock();

	if (parent == NULL)
	{

		// check if there is already the current page selected
		GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeViewBookmarks));

		if (gtk_tree_selection_get_selected(selection, &model, &iter))
		{
			XojLinkDest* link = NULL;

			gtk_tree_model_get(model, &iter, DOCUMENT_LINKS_COLUMN_LINK, &link, -1);

			if (link && link->dest)
			{
				LinkDestination* dest = link->dest;

				if (dest->getPdfPage() == pdfPage)
				{

					g_object_unref(model);
					g_object_unref(link);

					// already bookmak from this page selected
					return true;
				}
			}

			g_object_unref(link);
		}
	}

	gboolean valid = gtk_tree_model_iter_children(model, &iter, parent);

	while (valid)
	{
		XojLinkDest* link = NULL;

		gtk_tree_model_get(model, &iter, DOCUMENT_LINKS_COLUMN_LINK, &link, -1);

		if (link->dest->getPdfPage() == pdfPage)
		{
			GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeViewBookmarks));
			gtk_tree_selection_select_iter(selection, &iter);

			g_object_unref(link);
			g_object_unref(model);
			return true;
		}
		else
		{
			g_object_unref(link);

			if (selectPageNr(page, pdfPage, &iter))
			{
				g_object_unref(model);
				return true;
			}
			else
			{
				valid = gtk_tree_model_iter_next(model, &iter);
			}
		}
	}

	g_object_unref(model);
	return false;
}
bool SidebarIndexPage::treeBookmarkSelected(GtkWidget* treeview, SidebarIndexPage* sidebar)
{
	XOJ_CHECK_TYPE_OBJ(sidebar, SidebarIndexPage);

	if (sidebar->searchTimeout)
	{
		return false;
	}

	gtk_widget_grab_focus(GTK_WIDGET(treeview));

	GtkTreeSelection* selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));

	if (selection)
	{
		GtkTreeModel* model = NULL;
		GtkTreeIter iter = { 0 };

		if (gtk_tree_selection_get_selected(selection, &model, &iter))
		{
			XojLinkDest* link = NULL;

			gtk_tree_model_get(model, &iter, DOCUMENT_LINKS_COLUMN_LINK, &link, -1);
			if (link && link->dest)
			{
				LinkDestination* dest = link->dest;

				size_t pdfPage = dest->getPdfPage();

				if (pdfPage != size_t_npos)
				{
					Document* doc = sidebar->control->getDocument();
					doc->lock();
					size_t page = doc->findPdfPage(pdfPage);
					doc->unlock();

					if (page == size_t_npos)
					{
						sidebar->askInsertPdfPage(pdfPage);
					}
					else
					{
						if (dest->shouldChangeTop())
						{
							sidebar->control->getScrollHandler()->scrollToPage(page, dest->getTop());
						}
						else
						{
							if (sidebar->control->getCurrentPageNo() != page)
							{
								sidebar->control->getScrollHandler()->scrollToPage(page);
							}
						}
					}

				}
			}
			g_object_unref(link);

			return true;
		}
	}
	return false;
}