Usablity:
[blender-staging.git] / source / blender / editors / space_text / space_text.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
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.
13  *
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 Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_text/space_text.c
28  *  \ingroup sptext
29  */
30
31
32 #include <string.h>
33
34 #include "DNA_text_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_blenlib.h"
39
40 #include "BKE_context.h"
41 #include "BKE_screen.h"
42 #include "BKE_text.h"
43
44 #include "ED_space_api.h"
45 #include "ED_screen.h"
46
47 #include "BIF_gl.h"
48
49 #include "WM_api.h"
50 #include "WM_types.h"
51
52 #include "UI_interface.h"
53 #include "UI_resources.h"
54 #include "UI_view2d.h"
55
56 #include "RNA_access.h"
57
58
59 #include "text_format.h"
60 #include "text_intern.h"  /* own include */
61
62 /* ******************** default callbacks for text space ***************** */
63
64 static SpaceLink *text_new(const bContext *UNUSED(C))
65 {
66         ARegion *ar;
67         SpaceText *stext;
68         
69         stext = MEM_callocN(sizeof(SpaceText), "inittext");
70         stext->spacetype = SPACE_TEXT;
71
72         stext->lheight = 12;
73         stext->tabnumber = 4;
74         stext->margin_column = 80;
75         
76         /* header */
77         ar = MEM_callocN(sizeof(ARegion), "header for text");
78         
79         BLI_addtail(&stext->regionbase, ar);
80         ar->regiontype = RGN_TYPE_HEADER;
81         ar->alignment = RGN_ALIGN_BOTTOM;
82         
83         /* main area */
84         ar = MEM_callocN(sizeof(ARegion), "main area for text");
85         
86         BLI_addtail(&stext->regionbase, ar);
87         ar->regiontype = RGN_TYPE_WINDOW;
88         
89         return (SpaceLink *)stext;
90 }
91
92 /* not spacelink itself */
93 static void text_free(SpaceLink *sl)
94 {       
95         SpaceText *stext = (SpaceText *) sl;
96         
97         stext->text = NULL;
98         text_free_caches(stext);
99 }
100
101
102 /* spacetype; init callback */
103 static void text_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
104 {
105
106 }
107
108 static SpaceLink *text_duplicate(SpaceLink *sl)
109 {
110         SpaceText *stextn = MEM_dupallocN(sl);
111
112         /* clear or remove stuff from old */
113
114         stextn->drawcache = NULL; /* space need it's own cache */
115
116         return (SpaceLink *)stextn;
117 }
118
119 static void text_listener(ScrArea *sa, wmNotifier *wmn)
120 {
121         SpaceText *st = sa->spacedata.first;
122
123         /* context changes */
124         switch (wmn->category) {
125                 case NC_TEXT:
126                         /* check if active text was changed, no need to redraw if text isn't active
127                          * (reference == NULL) means text was unlinked, should update anyway for this
128                          * case -- no way to know was text active before unlinking or not */
129                         if (wmn->reference && wmn->reference != st->text)
130                                 break;
131
132                         switch (wmn->data) {
133                                 case ND_DISPLAY:
134                                         ED_area_tag_redraw(sa);
135                                         break;
136                                 case ND_CURSOR:
137                                         if (st->text && st->text == wmn->reference)
138                                                 text_scroll_to_cursor(st, sa);
139
140                                         ED_area_tag_redraw(sa);
141                                         break;
142                         }
143
144                         switch (wmn->action) {
145                                 case NA_EDITED:
146                                         if (st->text) {
147                                                 text_drawcache_tag_update(st, 1);
148                                                 text_update_edited(st->text);
149                                         }
150
151                                         ED_area_tag_redraw(sa);
152                                 /* no break -- fall down to tag redraw */
153                                 case NA_ADDED:
154                                 case NA_REMOVED:
155                                         ED_area_tag_redraw(sa);
156                                         break;
157                                 case NA_SELECTED:
158                                         if (st->text && st->text == wmn->reference)
159                                                 text_scroll_to_cursor(st, sa);
160
161                                         break;
162                         }
163
164                         break;
165                 case NC_SPACE:
166                         if (wmn->data == ND_SPACE_TEXT)
167                                 ED_area_tag_redraw(sa);
168                         break;
169         }
170 }
171
172 static void text_operatortypes(void)
173 {
174         WM_operatortype_append(TEXT_OT_new);
175         WM_operatortype_append(TEXT_OT_open);
176         WM_operatortype_append(TEXT_OT_reload);
177         WM_operatortype_append(TEXT_OT_unlink);
178         WM_operatortype_append(TEXT_OT_save);
179         WM_operatortype_append(TEXT_OT_save_as);
180         WM_operatortype_append(TEXT_OT_make_internal);
181         WM_operatortype_append(TEXT_OT_run_script);
182         WM_operatortype_append(TEXT_OT_refresh_pyconstraints);
183
184         WM_operatortype_append(TEXT_OT_paste);
185         WM_operatortype_append(TEXT_OT_copy);
186         WM_operatortype_append(TEXT_OT_cut);
187         WM_operatortype_append(TEXT_OT_duplicate_line);
188
189         WM_operatortype_append(TEXT_OT_convert_whitespace);
190         WM_operatortype_append(TEXT_OT_uncomment);
191         WM_operatortype_append(TEXT_OT_comment);
192         WM_operatortype_append(TEXT_OT_unindent);
193         WM_operatortype_append(TEXT_OT_indent);
194
195         WM_operatortype_append(TEXT_OT_select_line);
196         WM_operatortype_append(TEXT_OT_select_all);
197         WM_operatortype_append(TEXT_OT_select_word);
198         
199         WM_operatortype_append(TEXT_OT_move_lines);
200
201         WM_operatortype_append(TEXT_OT_jump);
202         WM_operatortype_append(TEXT_OT_move);
203         WM_operatortype_append(TEXT_OT_move_select);
204         WM_operatortype_append(TEXT_OT_delete);
205         WM_operatortype_append(TEXT_OT_overwrite_toggle);
206
207         WM_operatortype_append(TEXT_OT_selection_set);
208         WM_operatortype_append(TEXT_OT_cursor_set);
209         WM_operatortype_append(TEXT_OT_scroll);
210         WM_operatortype_append(TEXT_OT_scroll_bar);
211         WM_operatortype_append(TEXT_OT_line_number);
212
213         WM_operatortype_append(TEXT_OT_line_break);
214         WM_operatortype_append(TEXT_OT_insert);
215
216         WM_operatortype_append(TEXT_OT_properties);
217
218         WM_operatortype_append(TEXT_OT_find);
219         WM_operatortype_append(TEXT_OT_find_set_selected);
220         WM_operatortype_append(TEXT_OT_replace);
221         WM_operatortype_append(TEXT_OT_replace_set_selected);
222
223         WM_operatortype_append(TEXT_OT_start_find);
224         
225         WM_operatortype_append(TEXT_OT_to_3d_object);
226
227         WM_operatortype_append(TEXT_OT_resolve_conflict);
228
229         WM_operatortype_append(TEXT_OT_autocomplete);
230 }
231
232 static void text_keymap(struct wmKeyConfig *keyconf)
233 {
234         wmKeyMap *keymap;
235         wmKeyMapItem *kmi;
236         
237         keymap = WM_keymap_find(keyconf, "Text Generic", SPACE_TEXT, 0);
238         WM_keymap_add_item(keymap, "TEXT_OT_start_find", FKEY, KM_PRESS, KM_CTRL, 0);
239 #ifdef __APPLE__
240         WM_keymap_add_item(keymap, "TEXT_OT_start_find", FKEY, KM_PRESS, KM_OSKEY, 0);
241 #endif
242         
243         keymap = WM_keymap_find(keyconf, "Text", SPACE_TEXT, 0);
244         
245         #ifdef __APPLE__
246         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
247         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_END);
248         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_ALT, 0)->ptr, "type", PREV_WORD);
249         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0)->ptr, "type", NEXT_WORD);
250         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", UPARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", FILE_TOP);
251         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", DOWNARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", FILE_BOTTOM);
252         
253         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT | KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
254         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT | KM_OSKEY, 0)->ptr, "type", LINE_END);
255         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT | KM_ALT, 0)->ptr, "type", PREV_WORD);
256         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT | KM_ALT, 0)->ptr, "type", NEXT_WORD);
257         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", UPARROWKEY, KM_PRESS, KM_SHIFT | KM_OSKEY, 0)->ptr, "type", FILE_TOP);
258         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", DOWNARROWKEY, KM_PRESS, KM_SHIFT | KM_OSKEY, 0)->ptr, "type", FILE_BOTTOM);
259         
260         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_ALT, 0)->ptr, "type", DEL_PREV_WORD);
261         
262         WM_keymap_add_item(keymap, "TEXT_OT_save", SKEY, KM_PRESS, KM_ALT | KM_OSKEY, 0);
263         WM_keymap_add_item(keymap, "TEXT_OT_save_as", SKEY, KM_PRESS, KM_ALT | KM_SHIFT | KM_OSKEY, 0);
264         WM_keymap_add_item(keymap, "TEXT_OT_cut", XKEY, KM_PRESS, KM_OSKEY, 0);
265         WM_keymap_add_item(keymap, "TEXT_OT_copy", CKEY, KM_PRESS, KM_OSKEY, 0); 
266         WM_keymap_add_item(keymap, "TEXT_OT_paste", VKEY, KM_PRESS, KM_OSKEY, 0);
267         WM_keymap_add_item(keymap, "TEXT_OT_find_set_selected", EKEY, KM_PRESS, KM_OSKEY, 0);
268         WM_keymap_add_item(keymap, "TEXT_OT_find", GKEY, KM_PRESS, KM_OSKEY, 0);
269         WM_keymap_add_item(keymap, "TEXT_OT_select_all", AKEY, KM_PRESS, KM_OSKEY, 0);
270         WM_keymap_add_item(keymap, "TEXT_OT_select_line", AKEY, KM_PRESS, KM_SHIFT | KM_OSKEY, 0);
271         #endif
272         
273         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0);
274         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
275         RNA_boolean_set(kmi->ptr, "reverse", FALSE);
276         
277         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0);
278         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
279         RNA_boolean_set(kmi->ptr, "reverse", TRUE);
280
281         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
282         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
283         RNA_boolean_set(kmi->ptr, "reverse", FALSE);
284         
285         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADMINUS, KM_PRESS, KM_CTRL, 0);
286         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
287         RNA_boolean_set(kmi->ptr, "reverse", TRUE);
288
289         WM_keymap_add_item(keymap, "TEXT_OT_new", NKEY, KM_PRESS, KM_CTRL, 0);
290         WM_keymap_add_item(keymap, "TEXT_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
291         WM_keymap_add_item(keymap, "TEXT_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
292         WM_keymap_add_item(keymap, "TEXT_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
293         WM_keymap_add_item(keymap, "TEXT_OT_save_as", SKEY, KM_PRESS, KM_ALT | KM_SHIFT | KM_CTRL, 0);
294
295         WM_keymap_add_item(keymap, "TEXT_OT_run_script", PKEY, KM_PRESS, KM_ALT, 0);
296         
297         WM_keymap_add_item(keymap, "TEXT_OT_cut", XKEY, KM_PRESS, KM_CTRL, 0);
298         WM_keymap_add_item(keymap, "TEXT_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
299         WM_keymap_add_item(keymap, "TEXT_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
300
301         WM_keymap_add_item(keymap, "TEXT_OT_cut", DELKEY, KM_PRESS, KM_SHIFT, 0);
302         WM_keymap_add_item(keymap, "TEXT_OT_copy", INSERTKEY, KM_PRESS, KM_CTRL, 0);
303         WM_keymap_add_item(keymap, "TEXT_OT_paste", INSERTKEY, KM_PRESS, KM_SHIFT, 0);
304         
305         WM_keymap_add_item(keymap, "TEXT_OT_duplicate_line", DKEY, KM_PRESS, KM_CTRL, 0);
306
307         if (U.uiflag & USER_MMB_PASTE) { // XXX not dynamic
308                 kmi = WM_keymap_add_item(keymap, "TEXT_OT_paste", MIDDLEMOUSE, KM_PRESS, 0, 0);
309                 RNA_boolean_set(kmi->ptr, "selection", TRUE);
310         }
311
312         WM_keymap_add_item(keymap, "TEXT_OT_jump", JKEY, KM_PRESS, KM_CTRL, 0);
313         WM_keymap_add_item(keymap, "TEXT_OT_find", GKEY, KM_PRESS, KM_CTRL, 0);
314         
315         WM_keymap_add_item(keymap, "TEXT_OT_replace", HKEY, KM_PRESS, KM_CTRL, 0);
316
317         kmi = WM_keymap_add_item(keymap, "TEXT_OT_to_3d_object", MKEY, KM_PRESS, KM_ALT, 0);
318         RNA_boolean_set(kmi->ptr, "split_lines", FALSE);
319         kmi = WM_keymap_add_item(keymap, "TEXT_OT_to_3d_object", MKEY, KM_PRESS, KM_CTRL, 0);
320         RNA_boolean_set(kmi->ptr, "split_lines", TRUE);
321
322         WM_keymap_add_item(keymap, "TEXT_OT_select_all", AKEY, KM_PRESS, KM_CTRL, 0);
323         WM_keymap_add_item(keymap, "TEXT_OT_select_line", AKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
324         WM_keymap_add_item(keymap, "TEXT_OT_select_word", LEFTMOUSE, KM_DBL_CLICK, 0, 0);
325
326         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_lines", UPARROWKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0)->ptr, "direction", TXT_MOVE_LINE_UP);
327         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_lines", DOWNARROWKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0)->ptr, "direction", TXT_MOVE_LINE_DOWN);
328         
329         WM_keymap_add_item(keymap, "TEXT_OT_indent", TABKEY, KM_PRESS, 0, 0);
330         WM_keymap_add_item(keymap, "TEXT_OT_unindent", TABKEY, KM_PRESS, KM_SHIFT, 0);
331         WM_keymap_add_item(keymap, "TEXT_OT_uncomment", DKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
332
333         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
334         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
335         
336         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", EKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", LINE_END);
337         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", EKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0)->ptr, "type", LINE_END);
338         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
339         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
340         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
341         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
342         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
343         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
344         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
345         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
346         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", HOMEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", FILE_TOP);
347         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", ENDKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", FILE_BOTTOM);
348
349         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", HOMEKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", LINE_BEGIN);
350         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", ENDKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", LINE_END);
351         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_CHAR);
352         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_CHAR);
353         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0)->ptr, "type", PREV_WORD);
354         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0)->ptr, "type", NEXT_WORD);
355         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", UPARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_LINE);
356         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", DOWNARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_LINE);
357         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", PAGEUPKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_PAGE);
358         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_PAGE);
359         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", HOMEKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0)->ptr, "type", FILE_TOP);
360         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", ENDKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0)->ptr, "type", FILE_BOTTOM);
361
362         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
363         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
364         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", DEL_PREV_CHAR); /* same as above [#26623] */
365         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);
366         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD);
367         
368         WM_keymap_add_item(keymap, "TEXT_OT_overwrite_toggle", INSERTKEY, KM_PRESS, 0, 0);
369
370         WM_keymap_add_item(keymap, "TEXT_OT_scroll_bar", LEFTMOUSE, KM_PRESS, 0, 0);
371         WM_keymap_add_item(keymap, "TEXT_OT_scroll_bar", MIDDLEMOUSE, KM_PRESS, 0, 0);
372
373         WM_keymap_add_item(keymap, "TEXT_OT_scroll", MIDDLEMOUSE, KM_PRESS, 0, 0);
374         WM_keymap_add_item(keymap, "TEXT_OT_scroll", MOUSEPAN, 0, 0, 0);
375         WM_keymap_add_item(keymap, "TEXT_OT_selection_set", EVT_TWEAK_L, KM_ANY, 0, 0);
376         WM_keymap_add_item(keymap, "TEXT_OT_cursor_set", LEFTMOUSE, KM_PRESS, 0, 0);
377         kmi = WM_keymap_add_item(keymap, "TEXT_OT_selection_set", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0);
378         RNA_boolean_set(kmi->ptr, "select", TRUE);
379         RNA_int_set(WM_keymap_add_item(keymap, "TEXT_OT_scroll", WHEELUPMOUSE, KM_PRESS, 0, 0)->ptr, "lines", -1);
380         RNA_int_set(WM_keymap_add_item(keymap, "TEXT_OT_scroll", WHEELDOWNMOUSE, KM_PRESS, 0, 0)->ptr, "lines", 1);
381
382         WM_keymap_add_item(keymap, "TEXT_OT_line_break", RETKEY, KM_PRESS, 0, 0);
383         WM_keymap_add_item(keymap, "TEXT_OT_line_break", PADENTER, KM_PRESS, 0, 0);
384
385         WM_keymap_add_menu(keymap, "TEXT_MT_toolbox", RIGHTMOUSE, KM_PRESS, KM_ANY, 0);
386
387         WM_keymap_add_item(keymap, "TEXT_OT_autocomplete", SPACEKEY, KM_PRESS, KM_CTRL, 0);
388         
389         WM_keymap_add_item(keymap, "TEXT_OT_line_number", KM_TEXTINPUT, KM_ANY, KM_ANY, 0);
390         WM_keymap_add_item(keymap, "TEXT_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
391 }
392
393 const char *text_context_dir[] = {"edit_text", NULL};
394
395 static int text_context(const bContext *C, const char *member, bContextDataResult *result)
396 {
397         SpaceText *st = CTX_wm_space_text(C);
398
399         if (CTX_data_dir(member)) {
400                 CTX_data_dir_set(result, text_context_dir);
401                 return 1;
402         }
403         else if (CTX_data_equals(member, "edit_text")) {
404                 CTX_data_id_pointer_set(result, &st->text->id);
405                 return 1;
406         }
407
408         return 0;
409 }
410
411 /********************* main region ********************/
412
413 /* add handlers, stuff you only do once or on area/region changes */
414 static void text_main_area_init(wmWindowManager *wm, ARegion *ar)
415 {
416         wmKeyMap *keymap;
417         ListBase *lb;
418         
419         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
420         
421         /* own keymap */
422         keymap = WM_keymap_find(wm->defaultconf, "Text Generic", SPACE_TEXT, 0);
423         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
424         keymap = WM_keymap_find(wm->defaultconf, "Text", SPACE_TEXT, 0);
425         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
426         
427         /* add drop boxes */
428         lb = WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW);
429         
430         WM_event_add_dropbox_handler(&ar->handlers, lb);
431 }
432
433 static void text_main_area_draw(const bContext *C, ARegion *ar)
434 {
435         /* draw entirely, view changes should be handled here */
436         SpaceText *st = CTX_wm_space_text(C);
437         //View2D *v2d = &ar->v2d;
438         
439         /* clear and setup matrix */
440         UI_ThemeClearColor(TH_BACK);
441         glClear(GL_COLOR_BUFFER_BIT);
442         
443         // UI_view2d_view_ortho(v2d);
444                 
445         /* data... */
446         draw_text_main(st, ar);
447         
448         /* reset view matrix */
449         // UI_view2d_view_restore(C);
450         
451         /* scrollers? */
452 }
453
454 static void text_cursor(wmWindow *win, ScrArea *UNUSED(sa), ARegion *UNUSED(ar))
455 {
456         WM_cursor_set(win, BC_TEXTEDITCURSOR);
457 }
458
459
460
461 /* ************* dropboxes ************* */
462
463 static int text_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event))
464 {
465         if (drag->type == WM_DRAG_PATH)
466                 if (ELEM(drag->icon, ICON_FILE_SCRIPT, ICON_FILE_BLANK))    /* rule might not work? */
467                         return 1;
468         return 0;
469 }
470
471 static void text_drop_copy(wmDrag *drag, wmDropBox *drop)
472 {
473         /* copy drag path to properties */
474         RNA_string_set(drop->ptr, "filepath", drag->path);
475 }
476
477 /* this region dropbox definition */
478 static void text_dropboxes(void)
479 {
480         ListBase *lb = WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW);
481         
482         WM_dropbox_add(lb, "TEXT_OT_open", text_drop_poll, text_drop_copy);
483
484 }
485
486 /* ************* end drop *********** */
487
488
489 /****************** header region ******************/
490
491 /* add handlers, stuff you only do once or on area/region changes */
492 static void text_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
493 {
494         ED_region_header_init(ar);
495 }
496
497 static void text_header_area_draw(const bContext *C, ARegion *ar)
498 {
499         ED_region_header(C, ar);
500 }
501
502 /****************** properties region ******************/
503
504 /* add handlers, stuff you only do once or on area/region changes */
505 static void text_properties_area_init(wmWindowManager *wm, ARegion *ar)
506 {
507         wmKeyMap *keymap;
508
509         ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
510         ED_region_panels_init(wm, ar);
511
512         /* own keymaps */
513         keymap = WM_keymap_find(wm->defaultconf, "Text Generic", SPACE_TEXT, 0);
514         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
515
516 }
517
518 static void text_properties_area_draw(const bContext *C, ARegion *ar)
519 {
520         SpaceText *st = CTX_wm_space_text(C);
521         
522         ED_region_panels(C, ar, 1, NULL, -1);
523         
524         /* this flag trick is make sure buttons have been added already */
525         if (st->flags & ST_FIND_ACTIVATE) {
526                 
527                 UI_textbutton_activate_event(C, ar, st, "find_text");
528                 st->flags &= ~ST_FIND_ACTIVATE;
529         }
530 }
531
532 /********************* registration ********************/
533
534 /* only called once, from space/spacetypes.c */
535 void ED_spacetype_text(void)
536 {
537         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype text");
538         ARegionType *art;
539         
540         st->spaceid = SPACE_TEXT;
541         strncpy(st->name, "Text", BKE_ST_MAXNAME);
542         
543         st->new = text_new;
544         st->free = text_free;
545         st->init = text_init;
546         st->duplicate = text_duplicate;
547         st->operatortypes = text_operatortypes;
548         st->keymap = text_keymap;
549         st->listener = text_listener;
550         st->context = text_context;
551         st->dropboxes = text_dropboxes;
552         
553         /* regions: main window */
554         art = MEM_callocN(sizeof(ARegionType), "spacetype text region");
555         art->regionid = RGN_TYPE_WINDOW;
556         art->init = text_main_area_init;
557         art->draw = text_main_area_draw;
558         art->cursor = text_cursor;
559
560         BLI_addhead(&st->regiontypes, art);
561         
562         /* regions: properties */
563         art = MEM_callocN(sizeof(ARegionType), "spacetype text region");
564         art->regionid = RGN_TYPE_UI;
565         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
566         art->keymapflag = ED_KEYMAP_UI;
567         
568         art->init = text_properties_area_init;
569         art->draw = text_properties_area_draw;
570         BLI_addhead(&st->regiontypes, art);
571
572         /* regions: header */
573         art = MEM_callocN(sizeof(ARegionType), "spacetype text region");
574         art->regionid = RGN_TYPE_HEADER;
575         art->prefsizey = HEADERY;
576         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER;
577         
578         art->init = text_header_area_init;
579         art->draw = text_header_area_draw;
580
581         BLI_addhead(&st->regiontypes, art);
582
583         BKE_spacetype_register(st);
584
585         /* register formatters */
586         ED_text_format_register_py();
587         ED_text_format_register_osl();
588         ED_text_format_register_lua();
589 }
590