summaryrefslogtreecommitdiff
path: root/components/help/hyperbola-nav-tree.c
blob: 7a58331abf5f22467c35e0d1732a97654ca8f224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <config.h>
#include <libnautilus/libnautilus.h>
#include "hyperbola-filefmt.h"
#include <gtk/gtk.h>

typedef struct {
  NautilusViewFrame *view_frame;

  GtkWidget *ctree;
  HyperbolaDocTree *doc_tree;

  gint notify_count;
} HyperbolaNavigationTree;

BonoboObject *hyperbola_navigation_tree_new(void);

static void hyperbola_navigation_tree_select_row(GtkCTree *ctree,
						 GtkCTreeNode *node,
						 gint column,
						 HyperbolaNavigationTree *view);
static void hyperbola_navigation_tree_notify_location_change (NautilusViewFrame *view_frame,
							      Nautilus_NavigationInfo *navi,
							      HyperbolaNavigationTree *hview);

typedef struct {
  HyperbolaNavigationTree *view;
  GtkCTreeNode *sibling, *parent;
} PopulateInfo;

static gboolean
ctree_populate_subnode(gpointer key, gpointer value, gpointer user_data)
{
  HyperbolaTreeNode *node = value;
  PopulateInfo *pi = (PopulateInfo *)user_data, subpi;
  gboolean term;

  term = (node->type == HYP_TREE_NODE_PAGE) || !node->children;
  pi->sibling = gtk_ctree_insert_node(GTK_CTREE(pi->view->ctree), pi->parent, NULL, &node->title, 5,
				      NULL, NULL, NULL, NULL, term, FALSE);
  node->user_data = pi->sibling;

  gtk_ctree_node_set_row_data(GTK_CTREE(pi->view->ctree), pi->sibling, node);

  if(node->children)
    {
      subpi.view = pi->view;
      subpi.sibling = NULL;
      subpi.parent = pi->sibling;
      g_tree_traverse(node->children, ctree_populate_subnode, G_IN_ORDER, &subpi);
    }

  return FALSE;
}

static void
ctree_populate(HyperbolaNavigationTree *view)
{
  PopulateInfo subpi = { NULL, NULL, NULL };

  subpi.view = view;

  g_tree_traverse(view->doc_tree->children, ctree_populate_subnode, G_IN_ORDER, &subpi);
}

BonoboObject *
hyperbola_navigation_tree_new(void)
{
  static const char *titles[] = {"Document Tree"};
  GtkWidget *wtmp;
  HyperbolaNavigationTree *view;
  
  view = g_new0(HyperbolaNavigationTree, 1);

  view->ctree = gtk_ctree_new_with_titles(1, 0, (gchar **)titles);
  gtk_clist_freeze(GTK_CLIST(view->ctree));
  gtk_clist_set_selection_mode(GTK_CLIST(view->ctree), GTK_SELECTION_BROWSE);
  gtk_signal_connect(GTK_OBJECT(view->ctree), "tree_select_row", hyperbola_navigation_tree_select_row, view);
  view->doc_tree = hyperbola_doc_tree_new();
  hyperbola_doc_tree_populate(view->doc_tree);

  ctree_populate(view);

  wtmp = gtk_scrolled_window_new(gtk_clist_get_hadjustment(GTK_CLIST(view->ctree)),
				 gtk_clist_get_vadjustment(GTK_CLIST(view->ctree)));
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(wtmp), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);


  gtk_container_add(GTK_CONTAINER(wtmp), view->ctree);
  gtk_clist_columns_autosize(GTK_CLIST(view->ctree));
  gtk_clist_thaw(GTK_CLIST(view->ctree));
  gtk_widget_show(view->ctree);
  gtk_widget_show(wtmp);

  view->view_frame = NAUTILUS_VIEW_FRAME (nautilus_meta_view_frame_new (wtmp));
  gtk_signal_connect(GTK_OBJECT(view->view_frame), "notify_location_change", 
		     hyperbola_navigation_tree_notify_location_change,
		     view);

  return BONOBO_OBJECT (view->view_frame);
}

static void
hyperbola_navigation_tree_notify_location_change (NautilusViewFrame *view_frame,
						  Nautilus_NavigationInfo *navi,
						  HyperbolaNavigationTree *hview)
{
  HyperbolaTreeNode *tnode;

  if(hview->notify_count > 0)
    return;

  hview->notify_count++;

  tnode = g_hash_table_lookup(hview->doc_tree->global_by_uri, navi->requested_uri);

  if(tnode)
    gtk_ctree_select(GTK_CTREE(hview->ctree), tnode->user_data);

  hview->notify_count--;
}

static void hyperbola_navigation_tree_select_row(GtkCTree *ctree, GtkCTreeNode *node,
						 gint column, HyperbolaNavigationTree *view)
{
  HyperbolaTreeNode *tnode;
  Nautilus_NavigationRequestInfo nri;

  if(view->notify_count > 0)
    return;

  tnode = gtk_ctree_node_get_row_data(ctree, node);

  if(!tnode || !tnode->uri)
    return;

  view->notify_count++;

  memset(&nri, 0, sizeof(nri));
  nri.requested_uri = tnode->uri;
  nri.new_window_requested = FALSE;
  nautilus_view_frame_request_location_change(view->view_frame, &nri);

  view->notify_count--;
}