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