Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_text / text_header.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/text_header.c
28  *  \ingroup sptext
29  */
30
31 #include "DNA_windowmanager_types.h"
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_blenlib.h"
36
37
38 #include "BKE_context.h"
39 #include "BKE_screen.h"
40
41 #include "ED_screen.h"
42
43 #include "WM_types.h"
44
45 #include "text_intern.h"
46
47 /* ************************ header area region *********************** */
48
49 /************************** properties ******************************/
50
51 static ARegion *text_has_properties_region(ScrArea *sa)
52 {
53         ARegion *ar, *arnew;
54
55         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
56         if (ar) return ar;
57         
58         /* add subdiv level; after header */
59         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
60
61         /* is error! */
62         if (ar == NULL) return NULL;
63         
64         arnew = MEM_callocN(sizeof(ARegion), "properties region");
65         
66         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
67         arnew->regiontype = RGN_TYPE_UI;
68         arnew->alignment = RGN_ALIGN_LEFT;
69         
70         arnew->flag = RGN_FLAG_HIDDEN;
71         
72         return arnew;
73 }
74
75 static int text_properties_poll(bContext *C)
76 {
77         return (CTX_wm_space_text(C) != NULL);
78 }
79
80 static int text_properties_exec(bContext *C, wmOperator *UNUSED(op))
81 {
82         ScrArea *sa = CTX_wm_area(C);
83         ARegion *ar = text_has_properties_region(sa);
84         
85         if (ar)
86                 ED_region_toggle_hidden(C, ar);
87
88         return OPERATOR_FINISHED;
89 }
90
91 void TEXT_OT_properties(wmOperatorType *ot)
92 {
93         /* identifiers */
94         ot->name = "Properties";
95         ot->description = "Toggle the properties region visibility";
96         ot->idname = "TEXT_OT_properties";
97         
98         /* api callbacks */
99         ot->exec = text_properties_exec;
100         ot->poll = text_properties_poll;
101 }
102
103 static int text_text_search_exec(bContext *C, wmOperator *UNUSED(op))
104 {
105         ScrArea *sa = CTX_wm_area(C);
106         ARegion *ar = text_has_properties_region(sa);
107         SpaceText *st = CTX_wm_space_text(C);
108         
109         if (ar) {
110                 if (ar->flag & RGN_FLAG_HIDDEN)
111                         ED_region_toggle_hidden(C, ar);
112                 
113                 /* cannot send a button activate yet for case when region wasn't visible yet */
114                 /* flag gets checked and cleared in main draw callback */
115                 st->flags |= ST_FIND_ACTIVATE;
116                 
117                 ED_region_tag_redraw(ar);
118         }
119         return OPERATOR_FINISHED;
120 }
121
122
123 void TEXT_OT_start_find(wmOperatorType *ot)
124 {
125         /* identifiers */
126         ot->name = "Find";
127         ot->description = "Start searching text";
128         ot->idname = "TEXT_OT_start_find";
129         
130         /* api callbacks */
131         ot->exec = text_text_search_exec;
132         ot->poll = text_properties_poll;
133 }
134
135 /******************** XXX popup menus *******************/
136
137 #if 0
138 {
139         // RMB
140
141         uiPopupMenu *pup;
142
143         if (text) {
144                 pup = UI_popup_menu_begin(C, IFACE_("Text"), ICON_NONE);
145                 if (txt_has_sel(text)) {
146                         uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_cut");
147                         uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_copy");
148                 }
149                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_paste");
150                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_new");
151                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_open");
152                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_save");
153                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_save_as");
154                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_run_script");
155                 UI_popup_menu_end(C, pup);
156         }
157         else {
158                 pup = UI_popup_menu_begin(C, IFACE_("File"), ICON_NONE);
159                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_new");
160                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_open");
161                 UI_popup_menu_end(C, pup);
162         }
163 }
164
165 {
166         // Alt+Shift+E
167
168         uiPopupMenu *pup;
169
170         pup = UI_popup_menu_begin(C, IFACE_("Edit"), ICON_NONE);
171         uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_cut");
172         uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_copy");
173         uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_paste");
174         UI_popup_menu_end(C, pup);
175 }
176
177 {
178         // Alt+Shift+F
179
180         uiPopupMenu *pup;
181
182         if (text) {
183                 pup = UI_popup_menu_begin(C, IFACE_("Text"), ICON_NONE);
184                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_new");
185                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_open");
186                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_save");
187                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_save_as");
188                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_run_script");
189                 UI_popup_menu_end(C, pup);
190         }
191         else {
192                 pup = UI_popup_menu_begin(C, IFACE_("File"), ICON_NONE);
193                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_new");
194                 uiItemO(layout, NULL, ICON_NONE, "TEXT_OT_open");
195                 UI_popup_menu_end(C, pup);
196         }
197 }
198
199 {
200         // Alt+Shift+V
201
202         uiPopupMenu *pup;
203
204         pup = UI_popup_menu_begin(C, IFACE_("Text"), ICON_NONE);
205         uiItemEnumO(layout, "TEXT_OT_move", CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Top of File"),
206                     0, "type", FILE_TOP);
207         uiItemEnumO(layout, "TEXT_OT_move", CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Bottom of File"),
208                     0, "type", FILE_BOTTOM);
209         uiItemEnumO(layout, "TEXT_OT_move", CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Page Up"), 0, "type", PREV_PAGE);
210         uiItemEnumO(layout, "TEXT_OT_move", CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Page Down"),
211                     0, "type", NEXT_PAGE);
212         UI_popup_menu_end(C, pup);
213 }
214 #endif
215