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