More honesty about bug 144394, it's not quite fixed yet.
[dia.git] / app / textedit.c
blob1a2a5ace5972785df4554106fdfe634ec5b21fc3
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 /** This file handles the display part of text edit stuff: Making text
20 * edit start and stop at the right time and highlighting the edits.
21 * lib/text.c and lib/focus.c handles internal things like who can have
22 * focus and how to enter text. app/disp_callbacks.c handles the actual
23 * keystrokes.
25 * There's an invariant that all objects in the focus list must be selected.
28 #include <config.h>
30 #include "object.h"
31 #include "focus.h"
32 #include "display.h"
33 #include "highlight.h"
34 #include "textedit.h"
35 #include "object_ops.h"
36 #include "text.h"
38 static void
39 textedit_end_edit(DDisplay *ddisp, Focus *focus)
41 /* During destruction of the diagram the display may already be gone */
42 if (!ddisp)
43 return;
45 /* Leak of focus highlight color here, but should it be handled
46 by highlight or by us?
48 g_assert(focus == active_focus());
49 highlight_object_off(focus->obj, ddisp->diagram);
50 object_add_updates(focus->obj, ddisp->diagram);
53 static void
54 textedit_begin_edit(DDisplay *ddisp, Focus *focus)
56 Color *focus_col = color_new_rgb(1.0, 1.0, 0.0);
58 g_assert(dia_object_is_selected(focus_get_object(focus)));
59 highlight_object(focus->obj, focus_col, ddisp->diagram);
60 object_add_updates(focus->obj, ddisp->diagram);
63 /** Move the text edit focus either backwards or forwards. */
64 Focus *
65 textedit_move_focus(DDisplay *ddisp, Focus *focus, gboolean forwards)
67 if (focus != NULL) {
68 textedit_end_edit(ddisp, focus);
70 if (forwards) {
71 Focus *new_focus = focus_next();
72 if (new_focus != NULL) give_focus(new_focus);
73 } else {
74 Focus *new_focus = focus_previous();
75 if (new_focus != NULL) give_focus(new_focus);
77 focus = active_focus();
79 if (focus != NULL) {
80 textedit_begin_edit(ddisp, focus);
82 diagram_flush(ddisp->diagram);
83 return focus;
86 /** Call when something recieves an actual focus (not to be confused
87 * with doing request_focus(), which merely puts one in the focus list).
89 void
90 textedit_activate_focus(DDisplay *ddisp, Focus *focus, Point *clicked)
92 if (active_focus()) {
93 Focus *old_focus = active_focus();
94 textedit_end_edit(ddisp, old_focus);
96 if (clicked) {
97 text_set_cursor((Text*)focus->user_data, clicked, ddisp->renderer);
99 textedit_begin_edit(ddisp, focus);
100 give_focus(focus);
101 diagram_flush(ddisp->diagram);
104 /** Call when an object is chosen for activation (e.g. due to creation).
106 void
107 textedit_activate_object(DDisplay *ddisp, DiaObject *obj, Point *clicked)
109 Focus *new_focus;
111 if (active_focus()) {
112 Focus *focus = active_focus();
113 textedit_end_edit(ddisp, focus);
115 new_focus = focus_get_first_on_object(obj);
116 if (new_focus != NULL) {
117 give_focus(new_focus);
118 if (clicked) {
119 text_set_cursor((Text*)new_focus->user_data, clicked, ddisp->renderer);
121 textedit_begin_edit(ddisp, new_focus);
122 diagram_flush(ddisp->diagram);
126 /** Call to activate the first editable selected object.
127 * Deactivates the old edit.
129 void
130 textedit_activate_first(DDisplay *ddisp)
132 Focus *new_focus = NULL;
133 GList *selected = diagram_get_sorted_selected(ddisp->diagram);
135 if (active_focus()) {
136 Focus *focus = active_focus();
137 textedit_end_edit(ddisp, focus);
139 while (new_focus != NULL && selected != NULL) {
140 DiaObject *obj = (DiaObject*) selected->data;
141 new_focus = focus_get_first_on_object(obj);
143 if (new_focus != NULL) {
144 give_focus(new_focus);
145 textedit_begin_edit(ddisp, new_focus);
146 diagram_flush(ddisp->diagram);
150 /** Call when something causes the text focus to disappear.
151 * Does not remove objects from the focus list, but removes the
152 * focus highlight and stuff.
153 * Calling remove_focus on the active object or remove_focus_all
154 * implies deactivating the focus. */
155 void
156 textedit_deactivate_focus(void)
158 Focus *focus = active_focus();
159 if (focus != NULL) {
160 textedit_end_edit(ddisplay_active(), focus);
161 remove_focus();
165 /** Call when something should be removed from the focus list */
166 void
167 textedit_remove_focus(DiaObject *obj, Diagram *diagram)
169 Focus *old_focus = active_focus();
170 if (remove_focus_object(obj)) {
171 /* TODO: make sure the focus is deactivated */
172 textedit_end_edit(ddisplay_active(), old_focus);
176 /** Call when the entire list of focusable texts gets reset. */
177 void
178 textedit_remove_focus_all(Diagram *diagram)
180 Focus *focus = active_focus();
181 if (focus != NULL) {
182 /* TODO: make sure the focus is deactivated */
183 textedit_end_edit(ddisplay_active(), focus);
185 reset_foci();