Another small text space usability: option to show right margin (aka print marign)
[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         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                                 case ND_CURSOR:
135                                         ED_area_tag_redraw(sa);
136                                         break;
137                         }
138
139                         switch(wmn->action) {
140                                 case NA_EDITED:
141                                         if(st->text) {
142                                                 text_drawcache_tag_update(st, 1);
143                                                 text_update_edited(st->text);
144                                         }
145
146                                         ED_area_tag_redraw(sa);
147                                         /* no break -- fall down to tag redraw */
148                                 case NA_ADDED:
149                                 case NA_REMOVED:
150                                         ED_area_tag_redraw(sa);
151                                         break;
152                         }
153
154                         break;
155                 case NC_SPACE:
156                         if(wmn->data == ND_SPACE_TEXT)
157                                 ED_area_tag_redraw(sa);
158                         break;
159         }
160 }
161
162 static void text_operatortypes(void)
163 {
164         WM_operatortype_append(TEXT_OT_new);
165         WM_operatortype_append(TEXT_OT_open);
166         WM_operatortype_append(TEXT_OT_reload);
167         WM_operatortype_append(TEXT_OT_unlink);
168         WM_operatortype_append(TEXT_OT_save);
169         WM_operatortype_append(TEXT_OT_save_as);
170         WM_operatortype_append(TEXT_OT_make_internal);
171         WM_operatortype_append(TEXT_OT_run_script);
172         WM_operatortype_append(TEXT_OT_refresh_pyconstraints);
173
174         WM_operatortype_append(TEXT_OT_paste);
175         WM_operatortype_append(TEXT_OT_copy);
176         WM_operatortype_append(TEXT_OT_cut);
177
178         WM_operatortype_append(TEXT_OT_convert_whitespace);
179         WM_operatortype_append(TEXT_OT_uncomment);
180         WM_operatortype_append(TEXT_OT_comment);
181         WM_operatortype_append(TEXT_OT_unindent);
182         WM_operatortype_append(TEXT_OT_indent);
183
184         WM_operatortype_append(TEXT_OT_markers_clear);
185         WM_operatortype_append(TEXT_OT_next_marker);
186         WM_operatortype_append(TEXT_OT_previous_marker);
187
188         WM_operatortype_append(TEXT_OT_select_line);
189         WM_operatortype_append(TEXT_OT_select_all);
190         WM_operatortype_append(TEXT_OT_select_word);
191
192         WM_operatortype_append(TEXT_OT_jump);
193         WM_operatortype_append(TEXT_OT_move);
194         WM_operatortype_append(TEXT_OT_move_select);
195         WM_operatortype_append(TEXT_OT_delete);
196         WM_operatortype_append(TEXT_OT_overwrite_toggle);
197
198         WM_operatortype_append(TEXT_OT_selection_set);
199         WM_operatortype_append(TEXT_OT_cursor_set);
200         WM_operatortype_append(TEXT_OT_scroll);
201         WM_operatortype_append(TEXT_OT_scroll_bar);
202         WM_operatortype_append(TEXT_OT_line_number);
203
204         WM_operatortype_append(TEXT_OT_line_break);
205         WM_operatortype_append(TEXT_OT_insert);
206
207         WM_operatortype_append(TEXT_OT_properties);
208
209         WM_operatortype_append(TEXT_OT_find);
210         WM_operatortype_append(TEXT_OT_find_set_selected);
211         WM_operatortype_append(TEXT_OT_replace);
212         WM_operatortype_append(TEXT_OT_replace_set_selected);
213         WM_operatortype_append(TEXT_OT_mark_all);
214
215         WM_operatortype_append(TEXT_OT_to_3d_object);
216
217         WM_operatortype_append(TEXT_OT_resolve_conflict);
218 }
219
220 static void text_keymap(struct wmKeyConfig *keyconf)
221 {
222         wmKeyMap *keymap;
223         wmKeyMapItem *kmi;
224         
225         keymap= WM_keymap_find(keyconf, "Text", SPACE_TEXT, 0);
226         
227         #ifdef __APPLE__
228         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
229         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", LINE_END);
230         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_ALT, 0)->ptr, "type", PREV_WORD);
231         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0)->ptr, "type", NEXT_WORD);
232         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", UPARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", FILE_TOP);
233         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", DOWNARROWKEY, KM_PRESS, KM_OSKEY, 0)->ptr, "type", FILE_BOTTOM);
234         
235         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", LINE_BEGIN);
236         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", LINE_END);
237         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT|KM_ALT, 0)->ptr, "type", PREV_WORD);
238         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT|KM_ALT, 0)->ptr, "type", NEXT_WORD);
239         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", UPARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", FILE_TOP);
240         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", DOWNARROWKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0)->ptr, "type", FILE_BOTTOM);
241         
242         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_ALT, 0)->ptr, "type", DEL_PREV_WORD);
243         
244         WM_keymap_add_item(keymap, "TEXT_OT_save", SKEY, KM_PRESS, KM_ALT|KM_OSKEY, 0);
245         WM_keymap_add_item(keymap, "TEXT_OT_save_as", SKEY, KM_PRESS, KM_ALT|KM_SHIFT|KM_OSKEY, 0);
246         WM_keymap_add_item(keymap, "TEXT_OT_cut", XKEY, KM_PRESS, KM_OSKEY, 0);
247         WM_keymap_add_item(keymap, "TEXT_OT_copy", CKEY, KM_PRESS, KM_OSKEY, 0); 
248         WM_keymap_add_item(keymap, "TEXT_OT_paste", VKEY, KM_PRESS, KM_OSKEY, 0);
249         WM_keymap_add_item(keymap, "TEXT_OT_properties", FKEY, KM_PRESS, KM_OSKEY, 0);
250         WM_keymap_add_item(keymap, "TEXT_OT_find_set_selected", EKEY, KM_PRESS, KM_OSKEY, 0);
251         WM_keymap_add_item(keymap, "TEXT_OT_find", GKEY, KM_PRESS, KM_OSKEY, 0);
252         WM_keymap_add_item(keymap, "TEXT_OT_select_all", AKEY, KM_PRESS, KM_OSKEY, 0);
253         WM_keymap_add_item(keymap, "TEXT_OT_select_line", AKEY, KM_PRESS, KM_SHIFT|KM_OSKEY, 0);
254         #endif
255         
256         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0);
257         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
258         RNA_boolean_set(kmi->ptr, "reverse", 0);
259         
260         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0);
261         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
262         RNA_boolean_set(kmi->ptr, "reverse", 1);
263
264         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
265         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
266         RNA_boolean_set(kmi->ptr, "reverse", 0);
267         
268         kmi = WM_keymap_add_item(keymap, "WM_OT_context_cycle_int", PADMINUS, KM_PRESS, KM_CTRL, 0);
269         RNA_string_set(kmi->ptr, "data_path", "space_data.font_size");
270         RNA_boolean_set(kmi->ptr, "reverse", 1);
271         
272         WM_keymap_add_item(keymap, "TEXT_OT_new", NKEY, KM_PRESS, KM_ALT, 0);
273         WM_keymap_add_item(keymap, "TEXT_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
274         WM_keymap_add_item(keymap, "TEXT_OT_reload", RKEY, KM_PRESS, KM_ALT, 0);
275         WM_keymap_add_item(keymap, "TEXT_OT_save", SKEY, KM_PRESS, KM_ALT, 0);
276         WM_keymap_add_item(keymap, "TEXT_OT_save_as", SKEY, KM_PRESS, KM_ALT|KM_SHIFT|KM_CTRL, 0);
277
278         WM_keymap_add_item(keymap, "TEXT_OT_run_script", PKEY, KM_PRESS, KM_ALT, 0);
279         
280         WM_keymap_add_item(keymap, "TEXT_OT_cut", XKEY, KM_PRESS, KM_CTRL, 0);
281         WM_keymap_add_item(keymap, "TEXT_OT_copy", CKEY, KM_PRESS, KM_CTRL, 0);
282         WM_keymap_add_item(keymap, "TEXT_OT_paste", VKEY, KM_PRESS, KM_CTRL, 0);
283
284         WM_keymap_add_item(keymap, "TEXT_OT_cut", DELKEY, KM_PRESS, KM_SHIFT, 0);
285         WM_keymap_add_item(keymap, "TEXT_OT_copy", INSERTKEY, KM_PRESS, KM_CTRL, 0);
286         WM_keymap_add_item(keymap, "TEXT_OT_paste", INSERTKEY, KM_PRESS, KM_SHIFT, 0);
287
288         if(U.uiflag & USER_MMB_PASTE) // XXX not dynamic
289                 RNA_boolean_set(WM_keymap_add_item(keymap, "TEXT_OT_paste", MIDDLEMOUSE, KM_PRESS, 0, 0)->ptr, "selection", 1);
290
291         WM_keymap_add_item(keymap, "TEXT_OT_jump", JKEY, KM_PRESS, KM_CTRL, 0);
292         WM_keymap_add_item(keymap, "TEXT_OT_find", GKEY, KM_PRESS, KM_CTRL, 0);
293         
294         WM_keymap_add_item(keymap, "TEXT_OT_properties", FKEY, KM_PRESS, KM_CTRL, 0);
295         WM_keymap_add_item(keymap, "TEXT_OT_replace", HKEY, KM_PRESS, KM_CTRL, 0);
296
297         WM_keymap_add_item(keymap, "TEXT_OT_to_3d_object", MKEY, KM_PRESS, KM_ALT, 0);
298
299         WM_keymap_add_item(keymap, "TEXT_OT_select_all", AKEY, KM_PRESS, KM_CTRL, 0);
300         WM_keymap_add_item(keymap, "TEXT_OT_select_line", AKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0);
301         WM_keymap_add_item(keymap, "TEXT_OT_select_word", LEFTMOUSE, KM_DBL_CLICK, 0, 0);
302         
303
304         WM_keymap_add_item(keymap, "TEXT_OT_indent", TABKEY, KM_PRESS, 0, 0);
305         WM_keymap_add_item(keymap, "TEXT_OT_unindent", TABKEY, KM_PRESS, KM_SHIFT, 0);
306         WM_keymap_add_item(keymap, "TEXT_OT_uncomment", DKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
307
308         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", HOMEKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_BEGIN);
309         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", ENDKEY, KM_PRESS, 0, 0)->ptr, "type", LINE_END);
310         
311         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", EKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", LINE_END);
312         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", EKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0)->ptr, "type", LINE_END);
313         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_CHAR);
314         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_CHAR);
315         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", LEFTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", PREV_WORD);
316         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", RIGHTARROWKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", NEXT_WORD);
317         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", UPARROWKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_LINE);
318         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", DOWNARROWKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_LINE);
319         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "type", PREV_PAGE);
320         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "type", NEXT_PAGE);
321         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", HOMEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", FILE_TOP);
322         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move", ENDKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", FILE_BOTTOM);
323
324         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", HOMEKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", LINE_BEGIN);
325         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", ENDKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", LINE_END);
326         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_CHAR);
327         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_CHAR);
328         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", LEFTARROWKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", PREV_WORD);
329         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", RIGHTARROWKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", NEXT_WORD);
330         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", UPARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_LINE);
331         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", DOWNARROWKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_LINE);
332         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", PAGEUPKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", PREV_PAGE);
333         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "type", NEXT_PAGE);
334         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", HOMEKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", FILE_TOP);
335         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_move_select", ENDKEY, KM_PRESS, KM_SHIFT|KM_CTRL, 0)->ptr, "type", FILE_BOTTOM);
336
337         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DELKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_NEXT_CHAR);
338         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_CHAR);
339         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, 0, 0)->ptr, "type", DEL_PREV_CHAR);
340         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", DELKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_NEXT_WORD);
341         RNA_enum_set(WM_keymap_add_item(keymap, "TEXT_OT_delete", BACKSPACEKEY, KM_PRESS, KM_CTRL, 0)->ptr, "type", DEL_PREV_WORD);
342         
343         WM_keymap_add_item(keymap, "TEXT_OT_overwrite_toggle", INSERTKEY, KM_PRESS, 0, 0);
344
345         WM_keymap_add_item(keymap, "TEXT_OT_scroll", MIDDLEMOUSE, KM_PRESS, 0, 0);
346         WM_keymap_add_item(keymap, "TEXT_OT_scroll", MOUSEPAN, 0, 0, 0);
347         WM_keymap_add_item(keymap, "TEXT_OT_scroll_bar", EVT_TWEAK_L, KM_ANY, 0, 0);
348         WM_keymap_add_item(keymap, "TEXT_OT_selection_set", EVT_TWEAK_L, KM_ANY, 0, 0);
349         WM_keymap_add_item(keymap, "TEXT_OT_cursor_set", LEFTMOUSE, KM_CLICK, 0, 0);
350         RNA_boolean_set(WM_keymap_add_item(keymap, "TEXT_OT_selection_set", LEFTMOUSE, KM_PRESS, KM_SHIFT, 0)->ptr, "select", 1);
351         RNA_int_set(WM_keymap_add_item(keymap, "TEXT_OT_scroll", WHEELUPMOUSE, KM_PRESS, 0, 0)->ptr, "lines", -1);
352         RNA_int_set(WM_keymap_add_item(keymap, "TEXT_OT_scroll", WHEELDOWNMOUSE, KM_PRESS, 0, 0)->ptr, "lines", 1);
353
354         WM_keymap_add_item(keymap, "TEXT_OT_line_break", RETKEY, KM_PRESS, 0, 0);
355         WM_keymap_add_item(keymap, "TEXT_OT_line_break", PADENTER, KM_PRESS, 0, 0);
356
357         WM_keymap_add_menu(keymap, "TEXT_MT_toolbox", RIGHTMOUSE, KM_PRESS, KM_ANY, 0);
358         
359         WM_keymap_add_item(keymap, "TEXT_OT_line_number", KM_TEXTINPUT, KM_ANY, KM_ANY, 0);
360         WM_keymap_add_item(keymap, "TEXT_OT_insert", KM_TEXTINPUT, KM_ANY, KM_ANY, 0); // last!
361 }
362
363 const char *text_context_dir[] = {"edit_text", NULL};
364
365 static int text_context(const bContext *C, const char *member, bContextDataResult *result)
366 {
367         SpaceText *st= CTX_wm_space_text(C);
368
369         if(CTX_data_dir(member)) {
370                 CTX_data_dir_set(result, text_context_dir);
371                 return 1;
372         }
373         else if(CTX_data_equals(member, "edit_text")) {
374                 CTX_data_id_pointer_set(result, &st->text->id);
375                 return 1;
376         }
377
378         return 0;
379 }
380
381 /********************* main region ********************/
382
383 /* add handlers, stuff you only do once or on area/region changes */
384 static void text_main_area_init(wmWindowManager *wm, ARegion *ar)
385 {
386         wmKeyMap *keymap;
387         ListBase *lb;
388         
389         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
390         
391         /* own keymap */
392         keymap= WM_keymap_find(wm->defaultconf, "Text", SPACE_TEXT, 0);
393         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
394         
395         /* add drop boxes */
396         lb = WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW);
397         
398         WM_event_add_dropbox_handler(&ar->handlers, lb);
399 }
400
401 static void text_main_area_draw(const bContext *C, ARegion *ar)
402 {
403         /* draw entirely, view changes should be handled here */
404         SpaceText *st= CTX_wm_space_text(C);
405         //View2D *v2d= &ar->v2d;
406         
407         /* clear and setup matrix */
408         UI_ThemeClearColor(TH_BACK);
409         glClear(GL_COLOR_BUFFER_BIT);
410         
411         // UI_view2d_view_ortho(v2d);
412                 
413         /* data... */
414         draw_text_main(st, ar);
415         
416         /* reset view matrix */
417         // UI_view2d_view_restore(C);
418         
419         /* scrollers? */
420 }
421
422 static void text_cursor(wmWindow *win, ScrArea *UNUSED(sa), ARegion *UNUSED(ar))
423 {
424         WM_cursor_set(win, BC_TEXTEDITCURSOR);
425 }
426
427
428
429 /* ************* dropboxes ************* */
430
431 static int text_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
432 {
433         if(drag->type==WM_DRAG_PATH)
434                 if(ELEM(drag->icon, ICON_FILE_SCRIPT, ICON_FILE_BLANK)) /* rule might not work? */
435                         return 1;
436         return 0;
437 }
438
439 static void text_drop_copy(wmDrag *drag, wmDropBox *drop)
440 {
441         /* copy drag path to properties */
442         RNA_string_set(drop->ptr, "filepath", drag->path);
443 }
444
445 /* this region dropbox definition */
446 static void text_dropboxes(void)
447 {
448         ListBase *lb= WM_dropboxmap_find("Text", SPACE_TEXT, RGN_TYPE_WINDOW);
449         
450         WM_dropbox_add(lb, "TEXT_OT_open", text_drop_poll, text_drop_copy);
451
452 }
453
454 /* ************* end drop *********** */
455
456
457 /****************** header region ******************/
458
459 /* add handlers, stuff you only do once or on area/region changes */
460 static void text_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
461 {
462         ED_region_header_init(ar);
463 }
464
465 static void text_header_area_draw(const bContext *C, ARegion *ar)
466 {
467         ED_region_header(C, ar);
468 }
469
470 /****************** properties region ******************/
471
472 /* add handlers, stuff you only do once or on area/region changes */
473 static void text_properties_area_init(wmWindowManager *wm, ARegion *ar)
474 {
475         ED_region_panels_init(wm, ar);
476 }
477
478 static void text_properties_area_draw(const bContext *C, ARegion *ar)
479 {
480         ED_region_panels(C, ar, 1, NULL, -1);
481 }
482
483 /********************* registration ********************/
484
485 /* only called once, from space/spacetypes.c */
486 void ED_spacetype_text(void)
487 {
488         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype text");
489         ARegionType *art;
490         
491         st->spaceid= SPACE_TEXT;
492         strncpy(st->name, "Text", BKE_ST_MAXNAME);
493         
494         st->new= text_new;
495         st->free= text_free;
496         st->init= text_init;
497         st->duplicate= text_duplicate;
498         st->operatortypes= text_operatortypes;
499         st->keymap= text_keymap;
500         st->listener= text_listener;
501         st->context= text_context;
502         st->dropboxes = text_dropboxes;
503         
504         /* regions: main window */
505         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
506         art->regionid = RGN_TYPE_WINDOW;
507         art->init= text_main_area_init;
508         art->draw= text_main_area_draw;
509         art->cursor= text_cursor;
510
511         BLI_addhead(&st->regiontypes, art);
512         
513         /* regions: properties */
514         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
515         art->regionid = RGN_TYPE_UI;
516         art->prefsizex= UI_COMPACT_PANEL_WIDTH;
517         art->keymapflag= ED_KEYMAP_UI;
518         
519         art->init= text_properties_area_init;
520         art->draw= text_properties_area_draw;
521         BLI_addhead(&st->regiontypes, art);
522
523         /* regions: header */
524         art= MEM_callocN(sizeof(ARegionType), "spacetype text region");
525         art->regionid = RGN_TYPE_HEADER;
526         art->prefsizey= HEADERY;
527         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_HEADER;
528         
529         art->init= text_header_area_init;
530         art->draw= text_header_area_draw;
531
532         BLI_addhead(&st->regiontypes, art);
533
534         BKE_spacetype_register(st);
535 }
536