Another fix for r54414 (cleaner to "import" pgettext once at top of files, also now...
[blender.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
23 _ = bpy.app.translations.pgettext
24
25
26 class TEXT_HT_header(Header):
27     bl_space_type = 'TEXT_EDITOR'
28
29     def draw(self, context):
30         layout = self.layout
31
32         st = context.space_data
33         text = st.text
34
35         row = layout.row(align=True)
36         row.template_header()
37
38         if context.area.show_menus:
39             row.menu("TEXT_MT_view")
40             row.menu("TEXT_MT_text")
41
42             if text:
43                 row.menu("TEXT_MT_edit")
44                 row.menu("TEXT_MT_format")
45
46             row.menu("TEXT_MT_templates")
47
48         if text and text.is_modified:
49             sub = row.row()
50             sub.alert = True
51             sub.operator("text.resolve_conflict", text="", icon='HELP')
52
53         row.template_ID(st, "text", new="text.new", unlink="text.unlink")
54
55         row = layout.row(align=True)
56         row.prop(st, "show_line_numbers", text="")
57         row.prop(st, "show_word_wrap", text="")
58         row.prop(st, "show_syntax_highlight", text="")
59
60         if text:
61             osl = text.name.endswith(".osl") or text.name.endswith(".oso")
62
63             if osl:
64                 row = layout.row()
65                 row.operator("node.shader_script_update")
66             else:
67                 row = layout.row()
68                 row.operator("text.run_script")
69
70                 row = layout.row()
71                 row.active = text.name.endswith(".py")
72                 row.prop(text, "use_module")
73
74             row = layout.row()
75             if text.filepath:
76                 if text.is_dirty:
77                     row.label(text=_("File: *%r (unsaved)") %
78                               text.filepath, translate=False)
79                 else:
80                     row.label(text=pgettext("File: %r") %
81                               text.filepath, translate=False)
82             else:
83                 row.label(text="Text: External"
84                           if text.library
85                           else "Text: Internal")
86
87
88 class TEXT_PT_properties(Panel):
89     bl_space_type = 'TEXT_EDITOR'
90     bl_region_type = 'UI'
91     bl_label = "Properties"
92
93     def draw(self, context):
94         layout = self.layout
95
96         st = context.space_data
97
98         flow = layout.column_flow()
99         flow.prop(st, "show_line_numbers")
100         flow.prop(st, "show_word_wrap")
101         flow.prop(st, "show_syntax_highlight")
102         flow.prop(st, "show_line_highlight")
103         flow.prop(st, "use_live_edit")
104
105         flow = layout.column_flow()
106         flow.prop(st, "font_size")
107         flow.prop(st, "tab_width")
108
109         text = st.text
110         if text:
111             flow.prop(text, "use_tabs_as_spaces")
112
113         flow.prop(st, "show_margin")
114         col = flow.column()
115         col.active = st.show_margin
116         col.prop(st, "margin_column")
117
118
119 class TEXT_PT_find(Panel):
120     bl_space_type = 'TEXT_EDITOR'
121     bl_region_type = 'UI'
122     bl_label = "Find"
123
124     def draw(self, context):
125         layout = self.layout
126
127         st = context.space_data
128
129         # find
130         col = layout.column(align=True)
131         row = col.row()
132         row.prop(st, "find_text", text="")
133         row.operator("text.find_set_selected", text="", icon='TEXT')
134         col.operator("text.find")
135
136         # replace
137         col = layout.column(align=True)
138         row = col.row()
139         row.prop(st, "replace_text", text="")
140         row.operator("text.replace_set_selected", text="", icon='TEXT')
141         col.operator("text.replace")
142
143         # settings
144         layout.prop(st, "use_match_case")
145         row = layout.row()
146         row.prop(st, "use_find_wrap", text="Wrap")
147         row.prop(st, "use_find_all", text="All")
148
149
150 class TEXT_MT_view(Menu):
151     bl_label = "View"
152
153     def draw(self, context):
154         layout = self.layout
155
156         layout.operator("text.properties", icon='MENU_PANEL')
157
158         layout.separator()
159
160         layout.operator("text.move",
161                         text="Top of File",
162                         ).type = 'FILE_TOP'
163         layout.operator("text.move",
164                         text="Bottom of File",
165                         ).type = 'FILE_BOTTOM'
166
167         layout.separator()
168
169         layout.operator("screen.area_dupli")
170         layout.operator("screen.screen_full_area")
171
172
173 class TEXT_MT_text(Menu):
174     bl_label = "Text"
175
176     def draw(self, context):
177         layout = self.layout
178
179         st = context.space_data
180         text = st.text
181
182         layout.operator("text.new")
183         layout.operator("text.open")
184
185         if text:
186             layout.operator("text.reload")
187
188             layout.column()
189             layout.operator("text.save")
190             layout.operator("text.save_as")
191
192             if text.filepath:
193                 layout.operator("text.make_internal")
194
195             layout.column()
196             layout.operator("text.run_script")
197
198
199 class TEXT_MT_templates_py(Menu):
200     bl_label = "Python"
201
202     def draw(self, context):
203         self.path_menu(bpy.utils.script_paths("templates_py"),
204                        "text.open",
205                        {"internal": True},
206                        )
207
208
209 class TEXT_MT_templates_osl(Menu):
210     bl_label = "Open Shading Language"
211
212     def draw(self, context):
213         self.path_menu(bpy.utils.script_paths("templates_osl"),
214                        "text.open",
215                        {"internal": True},
216                        )
217
218
219 class TEXT_MT_templates(Menu):
220     bl_label = "Templates"
221
222     def draw(self, context):
223         layout = self.layout
224         layout.menu("TEXT_MT_templates_py")
225         layout.menu("TEXT_MT_templates_osl")
226
227
228 class TEXT_MT_edit_select(Menu):
229     bl_label = "Select"
230
231     def draw(self, context):
232         layout = self.layout
233
234         layout.operator("text.select_all")
235         layout.operator("text.select_line")
236
237
238 class TEXT_MT_format(Menu):
239     bl_label = "Format"
240
241     def draw(self, context):
242         layout = self.layout
243
244         layout.operator("text.indent")
245         layout.operator("text.unindent")
246
247         layout.separator()
248
249         layout.operator("text.comment")
250         layout.operator("text.uncomment")
251
252         layout.separator()
253
254         layout.operator_menu_enum("text.convert_whitespace", "type")
255
256
257 class TEXT_MT_edit_to3d(Menu):
258     bl_label = "Text To 3D Object"
259
260     def draw(self, context):
261         layout = self.layout
262
263         layout.operator("text.to_3d_object",
264                         text="One Object",
265                         ).split_lines = False
266         layout.operator("text.to_3d_object",
267                         text="One Object Per Line",
268                         ).split_lines = True
269
270
271 class TEXT_MT_edit(Menu):
272     bl_label = "Edit"
273
274     @classmethod
275     def poll(cls, context):
276         return (context.space_data.text)
277
278     def draw(self, context):
279         layout = self.layout
280
281         layout.operator("ed.undo")
282         layout.operator("ed.redo")
283
284         layout.separator()
285
286         layout.operator("text.cut")
287         layout.operator("text.copy")
288         layout.operator("text.paste")
289         layout.operator("text.duplicate_line")
290
291         layout.separator()
292
293         layout.operator("text.move_lines",
294                         text="Move line(s) up").direction = 'UP'
295         layout.operator("text.move_lines",
296                         text="Move line(s) down").direction = 'DOWN'
297
298         layout.separator()
299
300         layout.menu("TEXT_MT_edit_select")
301
302         layout.separator()
303
304         layout.operator("text.jump")
305         layout.operator("text.properties", text="Find...")
306         layout.operator("text.autocomplete")
307
308         layout.separator()
309
310         layout.menu("TEXT_MT_edit_to3d")
311
312
313 class TEXT_MT_toolbox(Menu):
314     bl_label = ""
315
316     def draw(self, context):
317         layout = self.layout
318
319         layout.operator_context = 'INVOKE_DEFAULT'
320
321         layout.operator("text.cut")
322         layout.operator("text.copy")
323         layout.operator("text.paste")
324
325         layout.separator()
326
327         layout.operator("text.run_script")
328
329 if __name__ == "__main__":  # only for live edit.
330     bpy.utils.register_module(__name__)