Replace deprecated types
[anjuta-extras.git] / plugins / scintilla / text_editor_cbs.c
blobc69399f9713eec4cdb125dd62d152ec66b52aeaf
1 /*
2 * text_editor_cbs.
3 * Copyright (C) 2000 Kh. Naba Kumar Singh
4 *
5 * This program 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 * This program 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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <gtk/gtk.h>
24 #include <gdk/gdk.h>
25 #include <gdk/gdkkeysyms.h>
26 #include <ctype.h>
28 #include <libanjuta/resources.h>
29 #include <libanjuta/anjuta-utils.h>
30 #include <libanjuta/anjuta-debug.h>
31 #include <libanjuta/interfaces/ianjuta-file-loader.h>
33 #include "text_editor.h"
34 #include "text_editor_cbs.h"
35 #include "text-editor-iterable.h"
37 #define GTK
38 #undef PLAT_GTK
39 #define PLAT_GTK 1
40 #include "Scintilla.h"
41 #include "SciLexer.h"
42 #include "ScintillaWidget.h"
44 gboolean
45 on_text_editor_scintilla_focus_in (GtkWidget* scintilla, GdkEvent *event,
46 TextEditor *te)
48 GList *node;
50 node = te->views;
51 while (node)
53 if (aneditor_get_widget (GPOINTER_TO_INT (node->data)) == scintilla)
55 DEBUG_PRINT ("%s", "Switching editor view ...");
56 te->editor_id = GPOINTER_TO_INT (node->data);
57 te->scintilla = aneditor_get_widget (te->editor_id);
58 break;
60 node = g_list_next (node);
62 return FALSE;
65 gboolean
66 on_text_editor_text_buttonpress_event (GtkWidget * widget,
67 GdkEventButton * event,
68 gpointer user_data)
70 TextEditor *te = user_data;
71 gtk_widget_grab_focus (GTK_WIDGET (te->scintilla));
72 return FALSE;
75 gboolean
76 on_text_editor_text_keyrelease_event (GtkWidget * widget,
77 GdkEventKey * event,
78 gpointer user_data)
80 if (event->keyval == GDK_KEY_BackSpace)
82 TextEditor *te = user_data;
83 g_signal_emit_by_name(G_OBJECT(te), "backspace");
85 return FALSE;
88 gboolean
89 on_text_editor_text_event (GtkWidget * widget,
90 GdkEvent * event, gpointer user_data)
92 TextEditor *te = TEXT_EDITOR (user_data);
93 GdkEventButton *bevent;
95 if (event->type != GDK_BUTTON_PRESS)
96 return FALSE;
97 if (((GdkEventButton *) event)->button != 3)
98 return FALSE;
99 if (!text_editor_has_selection (te))
101 /* Move cursor to set breakpoints at correct line (#530689) */
102 glong pos;
103 gint x = (gint)((GdkEventButton *) event)->x;
104 gint y = (gint)((GdkEventButton *) event)->y;
106 pos = scintilla_send_message (SCINTILLA (te->scintilla), SCI_POSITIONFROMPOINT, x, y);
107 if (pos >= 0)
109 scintilla_send_message (SCINTILLA (te->scintilla), SCI_GOTOPOS, pos, 0);
112 bevent = (GdkEventButton *) event;
113 bevent->button = 1;
114 gtk_menu_popup (GTK_MENU (te->popup_menu),
115 NULL, NULL, NULL, NULL,
116 bevent->button, bevent->time);
117 return TRUE;
120 static void
121 scintilla_uri_dropped (TextEditor *te, const char *uris)
123 IAnjutaFileLoader *loader;
125 loader = anjuta_shell_get_interface(te->shell, IAnjutaFileLoader, NULL);
126 if (loader)
128 gchar **list;
129 gchar **uri;
131 /* The uri list is separated by DOS end of line */
132 list = g_strsplit_set (uris, "\r\n", -1);
133 for (uri = list; *uri != NULL; uri++)
135 if (**uri != '\0')
137 GFile *file;
139 file = g_file_new_for_uri (*uri);
140 ianjuta_file_loader_load(loader, file, FALSE, NULL);
141 g_object_unref (file);
144 g_strfreev (list);
147 return;
150 gboolean timerclick = FALSE;
152 static gboolean
153 click_timeout (TextEditor *te)
155 gint line = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (te),
156 "marker_line"));
158 /* If not second clic after timeout : Single Click */
159 if (timerclick)
161 timerclick = FALSE;
162 /* Emit (single) clicked signal */
163 g_signal_emit_by_name (G_OBJECT (te), "marker_clicked", FALSE, line);
165 return FALSE;
168 void
169 on_text_editor_scintilla_notify (GtkWidget * sci, gint wParam, gpointer lParam,
170 gpointer data)
172 TextEditor *te;
173 struct SCNotification *nt;
174 gint line, position;
176 te = data;
177 if (te->freeze_count != 0)
178 return;
179 nt = lParam;
180 switch (nt->nmhdr.code)
182 case SCN_URIDROPPED:
183 scintilla_uri_dropped(te, nt->text);
184 break;
185 case SCN_SAVEPOINTREACHED:
186 g_signal_emit_by_name(G_OBJECT (te), "update_save_ui");
187 break;
188 case SCN_SAVEPOINTLEFT:
189 g_signal_emit_by_name(G_OBJECT (te), "update_save_ui");
190 //FIXME:
191 // anjuta_update_title ();
192 // update_main_menubar ();
193 text_editor_update_controls (te);
194 return;
195 case SCN_UPDATEUI:
196 te->current_line = text_editor_get_current_lineno (te);
197 g_signal_emit_by_name(G_OBJECT (te), "update_ui");
198 g_signal_emit_by_name(G_OBJECT (te), "cursor-moved");
199 return;
201 case SCN_CHARADDED:
203 position = text_editor_get_current_position (te) - 1;
204 TextEditorCell *position_iter = text_editor_cell_new (te, position);
205 te->current_line = text_editor_get_current_lineno (te);
206 g_signal_emit_by_name(G_OBJECT (te), "char-added", position_iter,
207 (gchar)nt->ch);
208 g_object_unref (position_iter);
210 text_editor_suggest_completion (te);
212 return;
213 case SCN_AUTOCCANCELLED:
214 text_editor_cancel_completion (te);
215 return;
216 case SCN_AUTOCCHARDELETED:
217 text_editor_suggest_completion (te);
218 return;
219 case SCN_AUTOCSELECTION:
220 case SCN_USERLISTSELECTION:
221 text_editor_select_completion (te);
222 return;
223 case SCN_MODIFIED:
224 if (nt->modificationType & (SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT))
226 TextEditorCell *position_iter =
227 text_editor_cell_new (te, nt->position);
228 gboolean added = nt->modificationType & SC_MOD_INSERTTEXT;
229 g_signal_emit_by_name (G_OBJECT (te), "changed", position_iter,
230 added, nt->length, nt->linesAdded,
231 nt->text);
232 g_object_unref (position_iter);
234 return;
235 case SCN_MARGINCLICK:
236 line = text_editor_get_line_from_position (te, nt->position);
237 if (nt->margin == 1) /* Bookmarks and Breakpoints margin */
239 /* if second click before timeout : Double click */
240 if (timerclick)
242 timerclick = FALSE;
243 text_editor_goto_line (te, line, -1, TRUE);
244 aneditor_command (te->editor_id, ANE_BOOKMARK_TOGGLE, 0, 0);
245 /* Emit (double) clicked signal */
246 g_signal_emit_by_name (G_OBJECT (te), "marker_clicked",
247 TRUE, line);
249 else
251 timerclick = TRUE;
252 g_object_set_data (G_OBJECT (te), "marker_line",
253 GINT_TO_POINTER (line));
254 /* Timeout after 400ms */
255 /* If 2 clicks before the timeout ==> Single Click */
256 g_timeout_add (400, (void*) click_timeout, te);
259 return;
260 case SCN_DWELLSTART:
262 TextEditorCell* cell = nt->position < 0 ? NULL : text_editor_cell_new (te, nt->position);
263 g_signal_emit_by_name (te, "hover-over", cell);
264 if (cell) g_object_unref (cell);
265 return;
268 case SCN_DWELLEND:
270 TextEditorCell* cell = nt->position < 0 ? NULL : text_editor_cell_new (te, nt->position);
272 text_editor_hide_hover_tip (te);
273 g_signal_emit_by_name (te, "hover-leave", cell);
274 if (cell) g_object_unref (cell);
275 return;
278 /* case SCEN_SETFOCUS:
279 case SCEN_KILLFOCUS:
280 case SCN_STYLENEEDED:
281 case SCN_DOUBLECLICK:
282 case SCN_MODIFIED:
283 case SCN_NEEDSHOWN:
285 default:
286 return;
290 void
291 on_text_editor_scintilla_size_allocate (GtkWidget * widget,
292 GtkAllocation * allocation,
293 gpointer data)
295 TextEditor *te;
296 te = data;
297 g_return_if_fail (te != NULL);
299 if (te->first_time_expose == FALSE)
300 return;
302 te->first_time_expose = FALSE;
303 text_editor_goto_line (te, te->current_line, FALSE, FALSE);