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