* lib/text.h: Added text_get_line() declaration
[dia.git] / lib / focus.c
blob25439573a97150c2b7ae7ea43b09b0e10b7f9d06
1 /* Dia -- a 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 files handles which text elements are currently eligible to get
20 * the input focus, and moving back and forth between them. Objects can
21 * add their texts to the list with request_focus (more than one can be
22 * added), which doesn't give them focus outright, but makes them part of
23 * the focus chain. Actual handling of when focus goes where is handled
24 * in app/disp_callbacks.c
27 #include <config.h>
28 #include "text.h"
29 #include "focus.h"
31 static Focus *active_focus_ptr = NULL;
32 static GList *text_foci = NULL;
34 /** Request that the give focus become active.
35 * Also adds the focus to the list of available foci.
36 * Eventually, this will only add the focus to the list. */
37 void
38 request_focus(Focus *focus)
40 /* Only add to focus list if not already there, and don't snatch focus. */
41 if (!g_list_find(text_foci, focus)) {
42 text_foci = g_list_append(text_foci, focus);
44 return;
45 if (active_focus_ptr != NULL) {
46 active_focus_ptr->has_focus = FALSE;
48 active_focus_ptr = focus;
49 active_focus_ptr->has_focus = TRUE;
50 text_foci = g_list_append(text_foci, focus);
53 /** Return the currently active focus */
54 Focus *
55 active_focus(void)
57 return active_focus_ptr;
60 void
61 give_focus(Focus *focus)
63 if (active_focus_ptr != NULL) {
64 active_focus_ptr->has_focus = FALSE;
66 active_focus_ptr = focus;
67 active_focus_ptr->has_focus = TRUE;
70 /* Return the first focus on the given object
72 Focus *
73 focus_get_first_on_object(DiaObject *obj)
75 GList *tmplist = text_foci;
77 for (; tmplist != NULL; tmplist = g_list_next(tmplist) ) {
78 Focus *focus = (Focus*)tmplist->data;
79 if (focus_get_object(focus) == obj) {
80 return focus;
83 return NULL;
86 /** Return the object that this focus belongs to. Note that each
87 * object may have more than one Text associated with it, the
88 * focus will be on one of those.
90 DiaObject*
91 focus_get_object(Focus *focus)
93 return focus->obj;
96 /** Return the next available focus, if one is already active.
98 Focus *
99 focus_next(void)
101 if (text_foci != NULL && active_focus_ptr != NULL) {
102 GList *listelem = g_list_find(text_foci, active_focus_ptr);
103 listelem = g_list_next(listelem);
104 if (listelem == NULL) listelem = text_foci;
105 return ((Focus*)listelem->data);
107 return NULL;
110 /** Return the previous available focus, if one is already active.
112 Focus *
113 focus_previous(void)
115 if (text_foci != NULL && active_focus_ptr != NULL) {
116 GList *listelem = g_list_find(text_foci, active_focus_ptr);
117 listelem = g_list_previous(listelem);
118 if (listelem == NULL)
119 listelem = g_list_last(text_foci);
120 return (Focus *)listelem->data;
122 return NULL;
125 /** Remove the current focus */
126 void
127 remove_focus(void)
129 if (active_focus_ptr != NULL) {
130 active_focus_ptr->has_focus = FALSE;
132 active_focus_ptr = NULL;
135 /** Reset the list of currently available foci */
136 void
137 reset_foci(void)
139 remove_focus();
140 g_list_free(text_foci);
141 text_foci = NULL;
144 /** Removes all foci owned by the object.
145 * Returns TRUE if the object had the active focus.
147 gboolean
148 remove_focus_object(DiaObject *obj)
150 GList *tmplist = text_foci;
151 gboolean active = FALSE;
153 for (; tmplist != NULL; ) {
154 Focus *focus = (Focus*)tmplist->data;
155 GList *link = tmplist;
156 tmplist = g_list_next(tmplist);
157 if (focus_get_object(focus) == obj) {
158 text_foci = g_list_delete_link(text_foci, link);
159 if (focus == active_focus_ptr) {
160 active = TRUE;
164 return active;