67b98d530a82ca0949f9987ed26f4c05d2bacc9c
[blender-staging.git] / release / scripts / startup / bl_ui / space_text.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8-80 compliant>
20 import bpy
21 from bpy.types import Header, Menu, Panel
22 from bpy.app.translations import pgettext_iface as iface_
23
24
25 class TEXT_HT_header(Header):
26     bl_space_type = 'TEXT_EDITOR'
27
28     def draw(self, context):
29         layout = self.layout
30
31         st = context.space_data
32         text = st.text
33
34         row = layout.row(align=True)
35         row.template_header()
36
37         TEXT_MT_editor_menus.draw_collapsible(context, layout)
38
39         if text and text.is_modified:
40             sub = row.row(align=True)
41             sub.alert = True
42             sub.operator("text.resolve_conflict", text="", icon='HELP')
43
44         row = layout.row(align=True)
45         row.template_ID(st, "text", new="text.new", unlink="text.unlink", open="text.open")
46
47         row = layout.row(align=True)
48         row.prop(st, "show_line_numbers", text="")
49         row.prop(st, "show_word_wrap", text="")
50         row.prop(st, "show_syntax_highlight", text="")
51
52         if text:
53             osl = text.name.endswith(".osl") or text.name.endswith(".oso")
54
55             if osl:
56                 row = layout.row()
57                 row.operator("node.shader_script_update")
58             else:
59                 row = layout.row()
60                 row.operator("text.run_script")
61
62                 row = layout.row()
63                 row.active = text.name.endswith(".py")
64                 row.prop(text, "use_module")
65
66             row = layout.row()
67             if text.filepath:
68                 if text.is_dirty:
69                     row.label(text=iface_("File: *%r (unsaved)") %
70                               text.filepath, translate=False)
71                 else:
72                     row.label(text=iface_("File: %r") %
73                               text.filepath, translate=False)
74             else:
75                 row.label(text="Text: External"
76                           if text.library
77                           else "Text: Internal")
78
79
80 class TEXT_MT_editor_menus(Menu):
81     bl_idname = "TEXT_MT_editor_menus"
82     bl_label = ""
83
84     def draw(self, context):
85         self.draw_menus(self.layout, context)
86
87     @staticmethod
88     def draw_menus(layout, context):
89         st = context.space_data
90         text = st.text
91
92         layout.menu("TEXT_MT_view")
93         layout.menu("TEXT_MT_text")
94
95         if text:
96             layout.menu("TEXT_MT_edit")
97             layout.menu("TEXT_MT_format")
98
99         layout.menu("TEXT_MT_templates")
100
101
102 class TEXT_PT_properties(Panel):
103     bl_space_type = 'TEXT_EDITOR'
104     bl_region_type = 'UI'
105     bl_label = "Properties"
106
107     def draw(self, context):
108         layout = self.layout
109
110         st = context.space_data
111
112         flow = layout.column_flow()
113         flow.prop(st, "show_line_numbers")
114         flow.prop(st, "show_word_wrap")
115         flow.prop(st, "show_syntax_highlight")
116         flow.prop(st, "show_line_highlight")
117         flow.prop(st, "use_live_edit")
118
119         flow = layout.column_flow()
120         flow.prop(st, "font_size")
121         flow.prop(st, "tab_width")
122
123         text = st.text
124         if text:
125             flow.prop(text, "use_tabs_as_spaces")
126
127         flow.prop(st, "show_margin")
128         col = flow.column()
129         col.active = st.show_margin
130         col.prop(st, "margin_column")
131
132
133 class TEXT_PT_find(Panel):
134     bl_space_type = 'TEXT_EDITOR'
135     bl_region_type = 'UI'
136     bl_label = "Find"
137
138     def draw(self, context):
139         layout = self.layout
140
141         st = context.space_data
142
143         # find
144         col = layout.column(align=True)
145         row = col.row(align=True)
146         row.prop(st, "find_text", text="")
147         row.operator("text.find_set_selected", text="", icon='TEXT')
148         col.operator("text.find")
149
150         # replace
151         col = layout.column(align=True)
152         row = col.row(align=True)
153         row.prop(st, "replace_text", text="")
154         row.operator("text.replace_set_selected", text="", icon='TEXT')
155         col.operator("text.replace")
156
157         # settings
158         layout.prop(st, "use_match_case")
159         row = layout.row(align=True)
160         row.prop(st, "use_find_wrap", text="Wrap")
161         row.prop(st, "use_find_all", text="All")
162
163
164 class TEXT_MT_view(Menu):
165     bl_label = "View"
166
167     def draw(self, context):
168         layout = self.layout
169
170         layout.operator("text.properties", icon='MENU_PANEL')
171
172         layout.separator()
173
174         layout.operator("text.move",
175                         text="Top of File",
176                         ).type = 'FILE_TOP'
177         layout.operator("text.move",
178                         text="Bottom of File",
179                         ).type = 'FILE_BOTTOM'
180
181         layout.separator()
182
183         layout.operator("screen.area_dupli")
184         layout.operator("screen.screen_full_area")
185         layout.operator("screen.screen_full_area", text="Toggle Fullscreen Area").use_hide_panels = True
186
187
188 class TEXT_MT_text(Menu):
189     bl_label = "Text"
190
191     def draw(self, context):
192         layout = self.layout
193
194         st = context.space_data
195         text = st.text
196
197         layout.operator("text.new")
198         layout.operator("text.open")
199
200         if text:
201             layout.operator("text.reload")
202
203             layout.column()
204             layout.operator("text.save")
205             layout.operator("text.save_as")
206
207             if text.filepath:
208                 layout.operator("text.make_internal")
209
210             layout.column()
211             layout.operator("text.run_script")
212
213
214 class TEXT_MT_templates_py(Menu):
215     bl_label = "Python"
216
217     def draw(self, context):
218         self.path_menu(
219             bpy.utils.script_paths("templates_py"),
220             "text.open",
221             props_default={"internal": True},
222         )
223
224
225 class TEXT_MT_templates_osl(Menu):
226     bl_label = "Open Shading Language"
227
228     def draw(self, context):
229         self.path_menu(
230             bpy.utils.script_paths("templates_osl"),
231             "text.open",
232             props_default={"internal": True},
233         )
234
235
236 class TEXT_MT_templates(Menu):
237     bl_label = "Templates"
238
239     def draw(self, context):
240         layout = self.layout
241         layout.menu("TEXT_MT_templates_py")
242         layout.menu("TEXT_MT_templates_osl")
243
244
245 class TEXT_MT_edit_select(Menu):
246     bl_label = "Select"
247
248     def draw(self, context):
249         layout = self.layout
250
251         layout.operator("text.select_all")
252         layout.operator("text.select_line")
253
254
255 class TEXT_MT_format(Menu):
256     bl_label = "Format"
257
258     def draw(self, context):
259         layout = self.layout
260
261         layout.operator("text.indent")
262         layout.operator("text.unindent")
263
264         layout.separator()
265
266         layout.operator("text.comment")
267         layout.operator("text.uncomment")
268
269         layout.separator()
270
271         layout.operator_menu_enum("text.convert_whitespace", "type")
272
273
274 class TEXT_MT_edit_to3d(Menu):
275     bl_label = "Text To 3D Object"
276
277     def draw(self, context):
278         layout = self.layout
279
280         layout.operator("text.to_3d_object",
281                         text="One Object",
282                         ).split_lines = False
283         layout.operator("text.to_3d_object",
284                         text="One Object Per Line",
285                         ).split_lines = True
286
287
288 class TEXT_MT_edit(Menu):
289     bl_label = "Edit"
290
291     @classmethod
292     def poll(cls, context):
293         return (context.space_data.text)
294
295     def draw(self, context):
296         layout = self.layout
297
298         layout.operator("ed.undo")
299         layout.operator("ed.redo")
300
301         layout.separator()
302
303         layout.operator("text.cut")
304         layout.operator("text.copy")
305         layout.operator("text.paste")
306         layout.operator("text.duplicate_line")
307
308         layout.separator()
309
310         layout.operator("text.move_lines",
311                         text="Move line(s) up").direction = 'UP'
312         layout.operator("text.move_lines",
313                         text="Move line(s) down").direction = 'DOWN'
314
315         layout.separator()
316
317         layout.menu("TEXT_MT_edit_select")
318
319         layout.separator()
320
321         layout.operator("text.jump")
322         layout.operator("text.start_find", text="Find...")
323         layout.operator("text.autocomplete")
324
325         layout.separator()
326
327         layout.menu("TEXT_MT_edit_to3d")
328
329
330 class TEXT_MT_toolbox(Menu):
331     bl_label = ""
332
333     def draw(self, context):
334         layout = self.layout
335
336         layout.operator_context = 'INVOKE_DEFAULT'
337
338         layout.operator("text.cut")
339         layout.operator("text.copy")
340         layout.operator("text.paste")
341
342         layout.separator()
343
344         layout.operator("text.run_script")
345
346
347 classes = (
348     TEXT_HT_header,
349     TEXT_MT_edit,
350     TEXT_MT_edit_select,
351     TEXT_MT_edit_to3d,
352     TEXT_MT_editor_menus,
353     TEXT_MT_format,
354     TEXT_MT_templates,
355     TEXT_MT_templates_osl,
356     TEXT_MT_templates_py,
357     TEXT_MT_text,
358     TEXT_MT_toolbox,
359     TEXT_MT_view,
360     TEXT_PT_find,
361     TEXT_PT_properties,
362 )
363
364 if __name__ == "__main__":  # only for live edit.
365     from bpy.utils import register_class
366     for cls in classes:
367         register_class(cls)