Debian Codegraph @

src/tools/dlgedit/gui_list.cc

raw
1 /*
2 Copyright (C) 2002 Kai Sterker <[email protected]>
3 Part of the Adonthell Project <http://adonthell.nongnu.org>
4
5 Dlgedit is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 Dlgedit is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Dlgedit. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20 * @file gui_list.cc
21 *
22 * @author Kai Sterker
23 * @brief Instant dialogue preview widget
24 */
25
26 #include "gui_list.h"
27 #include "gui_list_events.h"
28
29 GuiList::GuiList (GtkWidget *paned)
30 {
31 // Create list widget
32 GtkWidget *scrolled = gtk_scrolled_window_new (NULL, NULL);
33 gtk_paned_add2 (GTK_PANED (paned), scrolled);
34 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
35 gtk_widget_show (scrolled);
36
37 // the model
38 GtkListStore *model = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_POINTER, G_TYPE_STRING, PANGO_TYPE_WRAP_MODE, G_TYPE_INT);
39
40 // the list
41 list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
42 g_object_ref (list);
43 gtk_tree_view_set_grid_lines(GTK_TREE_VIEW(list), GTK_TREE_VIEW_GRID_LINES_NONE);
44 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);
45 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled), list);
46 gtk_widget_show (list);
47
48 // create the columns
49 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
50 gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(list), -1, "Dialogue", renderer, "text", 0, "foreground", 2, "wrap-mode", 3, "wrap-width", 4, NULL);
51
52 GtkTreeViewColumn *column = gtk_tree_view_get_column(GTK_TREE_VIEW(list), 0);
53 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
54
55 // selection listener
56 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(list));
57
58 // connect callbacks
59 g_signal_connect (G_OBJECT (selection), "changed", G_CALLBACK(on_list_select), this);
60 g_signal_connect (G_OBJECT (list), "size-allocate", G_CALLBACK(on_list_resize), this);
61 }
62
63 // (re)draw the list
64 void GuiList::draw ()
65 {
66 gtk_widget_queue_draw (list);
67 }
68
69 void GuiList::clear ()
70 {
71 gtk_widget_hide (list);
72
73 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));
74 gtk_list_store_clear (GTK_LIST_STORE(model));
75
76 gtk_widget_show (list);
77 }
78
79 // display instant preview for a given node
80 void GuiList::display (DlgNode *node)
81 {
82 // make sure we have a valid node
83 if (node == NULL || node->type() == MODULE) return;
84
85 DlgCircle *circle, *c;
86
87 // draw the new list
88 gtk_widget_hide (list);
89
90 // clear the list
91 clear ();
92
93 // make sure the node is a circle
94 if (node->type () == LINK) circle = (DlgCircle*) node->next (FIRST);
95 else circle = (DlgCircle *) node;
96
97 // first of all, display all parents
98 c = circle->parent (FIRST);
99
100 while (c != NULL)
101 {
102 add (0, c);
103 c = circle->parent (NEXT);
104 }
105
106 // add selected node
107 add (1, circle);
108
109 // finally, add all children
110 c = circle->child (FIRST);
111
112 while (c != NULL)
113 {
114 add (0, c);
115 c = circle->child (NEXT);
116 }
117
118 gtk_widget_show (list);
119 }
120
121 // add an item to the list
122 void GuiList::add (int mode, DlgCircle *circle)
123 {
124 GdkColor color;
125 std::string label_text = circle->text ();
126
127 // see what sort of node we have
128 if (mode == 1)
129 {
130 color.red = 65535;
131
132 if (circle->type () == NPC) color.green = 0;
133 else if (circle->type () == NARRATOR) color.green = 32700;
134 else color.green = 16350;
135
136 color.blue = 0;
137 }
138 // parent and child nodes' text
139 else
140 {
141 color.red = 0;
142 if (circle->type () == NARRATOR) color.green = 27300;
143 else color.green = 0;
144
145 if (circle->type () == NPC) color.blue = 0;
146 else if (circle->type () == NARRATOR) color.blue = 15600;
147 else color.blue = 35000;
148 }
149
150 GtkAllocation allocation;
151 gtk_widget_get_allocation (list, &allocation);
152
153 // add list-item to list
154 GtkTreeIter iter;
155 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));
156 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
157 gtk_list_store_set (GTK_LIST_STORE(model), &iter, 0, label_text.c_str(), 1, circle, 2, gdk_color_to_string(&color), 3, PANGO_WRAP_WORD, 4, allocation.width - 10, -1);
158 }