klavaro  3.13
About: Klavaro is a touch typing tutor program.
  Fossies Dox: klavaro-3.13.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

cursor.c
Go to the documentation of this file.
1 /**************************************************************************/
2 /* Klavaro - a flexible touch typing tutor */
3 /* Copyright (C) 2005-2021 Felipe Emmanuel Ferreira de Castro */
4 /* */
5 /* This file is part of Klavaro, which is a free software: you can */
6 /* redistribute it and/or modify it under the terms of the GNU General */
7 /* Public License as published by the Free Software Foundation, either */
8 /* version 3 of the License, or (at your option) any later version. */
9 /* */
10 /* Klavaro 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 (in the file COPYING). */
14 /* You should have received a copy of the GNU General Public License */
15 /* along with Klavaro. If not, see <https://www.gnu.org/licenses/> */
16 /**************************************************************************/
17 
18 /*
19  * Some auxiliar functions for cursor operations on a GtkTextView widget
20  */
21 #include "gtk/gtk.h"
22 
23 #include "auxiliar.h"
24 #include "tutor.h"
25 #include "cursor.h"
26 
27 struct
28 {
29  gint id;
30  gboolean blink;
32 
33 /*******************************************************************************
34  * Interface functions
35  */
36 gboolean
38 {
39  return (cursor.blink);
40 }
41 
42 void
43 cursor_set_blink (gboolean status)
44 {
45  cursor.blink = status;
46 }
47 
48 /*******************************************************************************
49  * Advance the cursor n positions on the tutor text view
50  */
51 gint
53 {
54  gint i;
55  gboolean cursor_out_screen;
56  GtkWidget *wg;
57  GtkTextBuffer *buf;
58  GtkTextIter new_start;
59  GtkTextIter old_start;
60  GtkTextIter end;
61  GtkTextMark *mark;
62 
63  wg = get_wg ("text_tutor");
64  buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (wg));
65 
66  /* Get current position for the iter old_start */
67  gtk_text_buffer_get_iter_at_mark (buf, &new_start, gtk_text_buffer_get_insert (buf));
68  old_start = new_start;
69 
70  /* Get new position for the iter new_start */
71  if (n > 0)
72  for (i = 0; i < n && gtk_text_iter_forward_char (&new_start); i++);
73  else
74  for (i = 0; i > n && gtk_text_iter_backward_char (&new_start); i--);
75 
76  /* Move cursor blinking */
77  end = old_start;
78  gtk_text_iter_forward_char (&end);
79  gtk_text_buffer_remove_tag_by_name (buf, "cursor_blink", &old_start, &end);
80  gtk_text_buffer_remove_tag_by_name (buf, "cursor_unblink", &old_start, &end);
81 
82  end = new_start;
83  gtk_text_iter_forward_char (&end);
84  gtk_text_buffer_apply_tag_by_name (buf, "cursor_blink", &new_start, &end);
85 
86  /* Move cursor */
87  gtk_text_buffer_place_cursor (buf, &new_start);
88 
89  /* Check need for auto-scrolling */
90  if (i == n)
91  {
92  end = new_start;
93  gtk_text_iter_forward_line (&end);
94  mark = gtk_text_buffer_create_mark (buf, "aux", &end, FALSE);
95  cursor_out_screen = gtk_text_view_move_mark_onscreen (GTK_TEXT_VIEW (wg), mark);
96  if (cursor_out_screen)
97  {
98  gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (wg),
99  gtk_text_buffer_get_insert (buf), 0.0, TRUE, 0.5, 0.12);
100  }
101  }
102  return (i);
103 }
104 
105 
106 /*******************************************************************************
107  * Paint the current char (at cursor) with color defined by a tag name
108  */
109 void
110 cursor_paint_char (gchar * color_tag_name)
111 {
112  GtkWidget *wg;
113  GtkTextBuffer *buf;
114  GtkTextIter start;
115  GtkTextIter end;
116 
117  wg = get_wg ("text_tutor");
118  buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (wg));
119  gtk_text_buffer_get_iter_at_mark (buf, &start, gtk_text_buffer_get_insert (buf));
120  end = start;
121  gtk_text_iter_forward_char (&end);
122 
123  gtk_text_buffer_apply_tag_by_name (buf, color_tag_name, &start, &end);
124 }
125 
126 
127 /*******************************************************************************
128  * Get the character under the cursor
129  */
130 gunichar
132 {
133  gunichar chr;
134  gchar *tmp_str;
135  GtkWidget *wg;
136  GtkTextBuffer *buf;
137  GtkTextIter start;
138  GtkTextIter end;
139 
140  wg = get_wg ("text_tutor");
141  buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (wg));
142  gtk_text_buffer_get_iter_at_mark (buf, &start, gtk_text_buffer_get_insert (buf));
143  gtk_text_buffer_get_iter_at_mark (buf, &end, gtk_text_buffer_get_insert (buf));
144  gtk_text_iter_forward_char (&end);
145  tmp_str = gtk_text_buffer_get_text (buf, &start, &end, FALSE);
146  chr = g_utf8_get_char (tmp_str);
147  g_free (tmp_str);
148  return (chr);
149 }
150 
151 /*******************************************************************************
152  * Initialize the cursor so that it blinks
153  */
154 gboolean
155 cursor_init (gpointer data)
156 {
157  cursor.id = 0;
158  cursor.blink = TRUE;
159  cursor_on (NULL);
160  return FALSE;
161 }
162 
163 /**********************************************************************
164  * Turns on the cursor and makes it blink if 'cursor_blink' is TRUE
165  */
166 gboolean
167 cursor_on (gpointer data)
168 {
169  if (cursor.id)
170  {
171  g_source_remove (cursor.id);
172  cursor.id = 0;
173  }
174 
175  if (cursor.blink == FALSE)
176  return FALSE;
177 
178  if (! gtk_widget_get_visible (get_wg ("window_tutor")))
179  {
180  cursor.blink = FALSE;
181  return FALSE;
182  }
183 
184  cursor.id = g_timeout_add (TIME_CURSOR_ON, &cursor_off, NULL);
185 
186  cursor_switch_on ();
187 
188  return FALSE;
189 }
190 
191 /**********************************************************************
192  * Turns off the cursor and makes it blink if 'cursor_blink' is TRUE
193  */
194 gboolean
195 cursor_off (gpointer data)
196 {
197  if (cursor.id)
198  {
199  g_source_remove (cursor.id);
200  cursor.id = 0;
201  }
202 
203  if (cursor.blink == FALSE)
204  return FALSE;
205 
206  if (! gtk_widget_get_visible (get_wg ("window_tutor")))
207  {
208  cursor.blink = FALSE;
209  return FALSE;
210  }
211 
212  cursor.id = g_timeout_add (TIME_CURSOR_OFF, &cursor_on, NULL);
213 
215 
216  return FALSE;
217 }
218 
219 /**********************************************************************
220  * Turns on the cursor immediately
221  */
222 void
224 {
225  GtkTextView *wg_text;
226  GtkTextBuffer *buf;
227  GtkTextIter start;
228  GtkTextIter end;
229 
230  wg_text = GTK_TEXT_VIEW (get_wg ("text_tutor"));
231  buf = gtk_text_view_get_buffer (wg_text);
232 
233  gtk_text_buffer_get_iter_at_mark (buf, &start, gtk_text_buffer_get_insert (buf));
234  gtk_text_buffer_get_iter_at_mark (buf, &end, gtk_text_buffer_get_insert (buf));
235  gtk_text_iter_forward_char (&end);
236  if (tutor_get_correcting ())
237  {
238  gtk_text_buffer_remove_tag_by_name (buf, "cursor_blink", &start, &end);
239  gtk_text_buffer_apply_tag_by_name (buf, "cursor_unblink", &start, &end);
240  }
241  else
242  {
243  gtk_text_buffer_remove_tag_by_name (buf, "cursor_unblink", &start, &end);
244  gtk_text_buffer_apply_tag_by_name (buf, "cursor_blink", &start, &end);
245  }
246 }
247 
248 /**********************************************************************
249  * Turns off the cursor immediately
250  */
251 void
253 {
254  GtkTextView *wg_text;
255  GtkTextBuffer *buf;
256  GtkTextIter start;
257  GtkTextIter end;
258 
259  wg_text = GTK_TEXT_VIEW (get_wg ("text_tutor"));
260  buf = gtk_text_view_get_buffer (wg_text);
261 
262  gtk_text_buffer_get_iter_at_mark (buf, &start, gtk_text_buffer_get_insert (buf));
263  gtk_text_buffer_get_iter_at_mark (buf, &end, gtk_text_buffer_get_insert (buf));
264  gtk_text_iter_forward_char (&end);
265  if (tutor_get_correcting ())
266  {
267  gtk_text_buffer_remove_tag_by_name (buf, "cursor_unblink", &start, &end);
268  gtk_text_buffer_apply_tag_by_name (buf, "cursor_blink", &start, &end);
269  }
270  else
271  {
272  gtk_text_buffer_remove_tag_by_name (buf, "cursor_blink", &start, &end);
273  gtk_text_buffer_apply_tag_by_name (buf, "cursor_unblink", &start, &end);
274  }
275 }
GtkWidget * get_wg(gchar *name)
Definition: auxiliar.c:40
void cursor_switch_off()
Definition: cursor.c:252
void cursor_set_blink(gboolean status)
Definition: cursor.c:43
gboolean cursor_get_blink()
Definition: cursor.c:37
gboolean blink
Definition: cursor.c:30
void cursor_switch_on()
Definition: cursor.c:223
gunichar cursor_get_char()
Definition: cursor.c:131
struct @3 cursor
gboolean cursor_off(gpointer data)
Definition: cursor.c:195
gboolean cursor_on(gpointer data)
Definition: cursor.c:167
void cursor_paint_char(gchar *color_tag_name)
Definition: cursor.c:110
gint id
Definition: cursor.c:29
gint cursor_advance(gint n)
Definition: cursor.c:52
gboolean cursor_init(gpointer data)
Definition: cursor.c:155
#define TIME_CURSOR_ON
Definition: cursor.h:21
#define TIME_CURSOR_OFF
Definition: cursor.h:22
guint i
Definition: keyboard.c:55
gchar * data
Definition: main.c:53
struct @8::@10 mark
gint tutor_get_correcting()
Definition: tutor.c:152