File: focus.c

package info (click to toggle)
dia 0.97.3%2Bgit20160930-9
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 54,372 kB
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,833; sh: 540; perl: 137; sed: 19
file content (213 lines) | stat: -rw-r--r-- 5,792 bytes parent folder | download | duplicates (6)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* Dia -- a diagram creation/manipulation program
 * Copyright (C) 1998 Alexander Larsson
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/** This files handles which text elements are currently eligible to get
 * the input focus, and moving back and forth between them.  Objects can
 * add their texts to the list with request_focus (more than one can be
 * added), which doesn't give them focus outright, but makes them part of 
 * the focus chain.  Actual handling of when focus goes where is handled
 * in app/disp_callbacks.c
 */

#include <config.h>
#include "text.h"
#include "focus.h"
#include "diagramdata.h"
#include "object.h"

/** Returns the list of foci for the given diagram */
static GList *
get_text_foci(DiagramData *dia)
{
  return dia->text_edits;
}

/** Get the currently active focus for the given diagram, if any */
Focus *
get_active_focus(DiagramData *dia)
{
  return dia->active_text_edit;
}

/** Set which focus is active for the given diagram.
 * @param dia Diagram to set active focus for.
 * @param focus Focus to be active, or null if no focus should be active.
 * The focus should be on the get_text_foci list.
 */
static void
set_active_focus(DiagramData *dia, Focus *focus)
{
  if (dia->active_text_edit != NULL) {
    dia->active_text_edit->has_focus = FALSE;
  }
  dia->active_text_edit = focus;
  if (focus != NULL) {
    focus->has_focus = TRUE;
  }
}

/** Add a focus to the list of foci that can be activated for the given
 * diagram. */
static void
add_text_focus(DiagramData *dia, Focus *focus)
{
  dia->text_edits = g_list_append(dia->text_edits, focus);
}

/** Set the list of foci for a diagram.  The old list, if any, will be
 * returned, not freed.
 */
static GList *
set_text_foci(DiagramData *dia, GList *foci)
{
  GList *old_foci = get_text_foci(dia);
  dia->text_edits = NULL;
  return old_foci;
}

/** Request that the give focus become active.
 * Also adds the focus to the list of available foci.
 * Eventually, this will only add the focus to the list. */
void
request_focus(Focus *focus)
{
  DiagramData *dia = focus->obj->parent_layer->parent_diagram;
  GList *text_foci = get_text_foci(dia);
  /* Only add to focus list if not already there, and don't snatch focus. */
  if (!g_list_find(text_foci, focus)) {
    add_text_focus(dia, focus);
  }
  return;
}

void
give_focus(Focus *focus)
{
  DiagramData *dia = focus->obj->parent_layer->parent_diagram;

  if (get_active_focus(dia) != NULL) {
    get_active_focus(dia)->has_focus = FALSE;
  }
  set_active_focus(dia, focus);
  focus->has_focus = TRUE;
}

/* Return the first focus on the given object
 */
Focus *
focus_get_first_on_object(DiaObject *obj)
{
  GList *tmplist = get_text_foci(obj->parent_layer->parent_diagram);

  for (; tmplist != NULL; tmplist = g_list_next(tmplist) ) {
    Focus *focus = (Focus*)tmplist->data;
    if (focus_get_object(focus) == obj) {
      return focus;
    }
  }
  return NULL;
}

/** Return the object that this focus belongs to.  Note that each
 * object may have more than one Text associated with it, the
 * focus will be on one of those.
 */
DiaObject*
focus_get_object(Focus *focus)
{
  return focus->obj;
}

Focus *
focus_next_on_diagram(DiagramData *dia)
{
  if (get_text_foci(dia) != NULL && get_active_focus(dia) != NULL) {
    GList *listelem = g_list_find(get_text_foci(dia), get_active_focus(dia));
    listelem = g_list_next(listelem);
    if (listelem == NULL) listelem = get_text_foci(dia);
    return ((Focus*)listelem->data);
  }
  return NULL;
}


Focus *
focus_previous_on_diagram(DiagramData *dia)
{
  GList *text_foci = get_text_foci(dia);
  if (text_foci != NULL && get_active_focus(dia) != NULL) {
    GList *listelem = g_list_find(text_foci, get_active_focus(dia));
    listelem = g_list_previous(listelem);
    if (listelem == NULL) 
      listelem = g_list_last(text_foci);
    return (Focus *)listelem->data;
  }
  return NULL;
}

void
remove_focus_on_diagram(DiagramData *dia)
{
  if (get_active_focus(dia) != NULL) {
    set_active_focus(dia, NULL);
  }
}

void
reset_foci_on_diagram(DiagramData *dia)
{
  GList *old_foci;
  remove_focus_on_diagram(dia);
  old_foci = set_text_foci(dia, NULL);
  g_list_free(old_foci);
}

/** Removes all foci owned by the object.
 * Returns TRUE if the object had the active focus for its diagram.
 */
gboolean
remove_focus_object(DiaObject *obj)
{
  DiagramData *dia = obj->parent_layer->parent_diagram;
  GList *tmplist = get_text_foci(dia);
  gboolean active = FALSE;
  Focus *next_focus = NULL;
  Focus *active_focus = get_active_focus(dia);

  for (; tmplist != NULL; ) {
    Focus *focus = (Focus*)tmplist->data;
    GList *link = tmplist;
    tmplist = g_list_next(tmplist);
    if (focus_get_object(focus) == obj) {
      if (focus == active_focus) {
	next_focus = focus_next_on_diagram(dia);
	active = TRUE;
      }
      set_text_foci(dia, g_list_delete_link(get_text_foci(dia), link));
    }
  }
  if (next_focus != NULL && get_text_foci(dia) != NULL) {
    give_focus(next_focus);
  } else {
    if (get_text_foci(dia) == NULL) {
      set_active_focus(dia, NULL);
    }
  }
  return active;
}