Blender V5.3
space_topbar.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2017 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#include <fmt/format.h>
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_listbase.hh"
16#include "BLI_string_utf8.hh"
17#include "BLI_utildefines.hh"
18
19#include "BLT_translation.hh"
20
22#include "BKE_context.hh"
23#include "BKE_screen.hh"
24#include "BKE_undo_system.hh"
25
26#include "ED_screen.hh"
27#include "ED_space_api.hh"
28
29#include "UI_interface.hh"
31#include "UI_resources.hh"
32#include "UI_view2d.hh"
33
34#include "BLO_read_write.hh"
35
36#include "RNA_access.hh"
37
38#include "WM_api.hh"
39#include "WM_message.hh"
40#include "WM_types.hh"
41
42namespace blender {
43
44/* ******************** default callbacks for topbar space ***************** */
45
46static SpaceLink *topbar_create(const ScrArea * /*area*/, const Scene * /*scene*/)
47{
48 ARegion *region;
49 SpaceTopBar *stopbar;
50
51 stopbar = MEM_new<SpaceTopBar>("init topbar");
52 stopbar->spacetype = SPACE_TOPBAR;
53
54 /* header */
55 region = BKE_area_region_new();
56 BLI_addtail(&stopbar->regionbase, region);
58 region->alignment = RGN_ALIGN_TOP;
59 region = BKE_area_region_new();
60 BLI_addtail(&stopbar->regionbase, region);
63
64 /* main regions */
65 region = BKE_area_region_new();
66 BLI_addtail(&stopbar->regionbase, region);
68
69 return reinterpret_cast<SpaceLink *>(stopbar);
70}
71
72/* Doesn't free the space-link itself. */
73static void topbar_free(SpaceLink * /*sl*/) {}
74
75/* spacetype; init callback */
76static void topbar_init(wmWindowManager * /*wm*/, ScrArea * /*area*/) {}
77
79{
80 SpaceTopBar *stopbarn = MEM_dupalloc(reinterpret_cast<SpaceTopBar *>(sl));
81
82 /* clear or remove stuff from old */
83
84 return reinterpret_cast<SpaceLink *>(stopbarn);
85}
86
87/* add handlers, stuff you only do once or on area/region changes */
89{
90 wmKeyMap *keymap;
91
92 /* force delayed view2d_region_reinit call */
94 region->flag |= RGN_FLAG_DYNAMIC_SIZE;
95 }
97
98 keymap = WM_keymap_ensure(
99 wm->runtime->defaultconf, "View2D Buttons List", SPACE_EMPTY, RGN_TYPE_WINDOW);
101}
102
103static void topbar_operatortypes() {}
104
105static void topbar_keymap(wmKeyConfig * /*keyconf*/) {}
106
107/* add handlers, stuff you only do once or on area/region changes */
109{
111 region->flag |= RGN_FLAG_DYNAMIC_SIZE;
112 }
113 ED_region_header_init(region);
114}
115
117{
118 ARegion *region = params->region;
119 const wmNotifier *wmn = params->notifier;
120
121 /* context changes */
122 switch (wmn->category) {
123 case NC_WM:
124 if (wmn->data == ND_HISTORY) {
125 ED_region_tag_redraw(region);
126 }
127 break;
128 case NC_SCENE:
129 if (wmn->data == ND_MODE) {
130 ED_region_tag_redraw(region);
131 }
132 break;
133 case NC_SPACE:
134 if (wmn->data == ND_SPACE_VIEW3D) {
135 ED_region_tag_redraw(region);
136 }
137 break;
138 case NC_GPENCIL:
139 if (wmn->data == ND_DATA) {
140 ED_region_tag_redraw(region);
141 }
142 break;
143 }
144}
145
147{
148 ARegion *region = params->region;
149 const wmNotifier *wmn = params->notifier;
150
151 /* context changes */
152 switch (wmn->category) {
153 case NC_WM:
154 if (wmn->data == ND_JOB) {
155 ED_region_tag_redraw(region);
156 }
157 break;
158 case NC_WORKSPACE:
159 ED_region_tag_redraw(region);
160 break;
161 case NC_SPACE:
162 if (wmn->data == ND_SPACE_INFO) {
163 ED_region_tag_redraw(region);
164 }
165 break;
166 case NC_SCREEN:
167 if (wmn->data == ND_LAYER) {
168 ED_region_tag_redraw(region);
169 }
170 break;
171 case NC_SCENE:
172 if (wmn->data == ND_SCENEBROWSE) {
173 ED_region_tag_redraw(region);
174 }
175 break;
176 }
177}
178
180{
181 wmMsgBus *mbus = params->message_bus;
182 WorkSpace *workspace = params->workspace;
183 ARegion *region = params->region;
184
185 wmMsgSubscribeValue msg_sub_value_region_tag_redraw{};
186 msg_sub_value_region_tag_redraw.owner = region;
187 msg_sub_value_region_tag_redraw.user_data = region;
188 msg_sub_value_region_tag_redraw.notify = ED_region_do_msg_notify_tag_redraw;
189
191 mbus, &workspace->id, workspace, WorkSpace, tools, &msg_sub_value_region_tag_redraw);
192}
193
194static void recent_files_menu_label(ui::Layout &layout, const StringRef text)
195{
196 /* Label without icon space, fully aligned to the left. */
197 ui::Block *block = layout.block();
198 ui::block_layout_set_current(block, &layout);
200 block, ui::ButtonType::Label, text, 0, 0, 200, UI_UNIT_Y, nullptr, 0.0, 0.0, "");
202}
203
204static int recent_files_menu_list(const bContext *C, ui::Layout &layout, const int rows)
205{
206 const std::optional<std::string> project_name = BKE_blender_project_read_callback(
207 CTX_data_main(C), [](const bke::BlenderProject *project) -> std::optional<std::string> {
208 if (project == nullptr) {
209 return std::nullopt;
210 }
211 return std::string(project->get_name());
212 });
213 if (!project_name) {
214 return template_recent_files(&layout, rows);
215 }
216
217 /* Project and other recent files separately. Project and other labels
218 * are added afterwards, so we can hide them if they are empty. */
219 ui::Layout &project_title = layout.column(false);
220 const int found_project = template_recent_files(&layout, rows, ui::RecentFilesFilter::Project);
221 ui::Layout &other_title = layout.column(false);
222 const int found_other = template_recent_files(&layout, rows, ui::RecentFilesFilter::Other);
223
224 if (found_project) {
225 recent_files_menu_label(project_title,
226 fmt::format(fmt::runtime(IFACE_("Project: {}")), *project_name));
227 }
228 if (found_other) {
229 if (found_project) {
230 other_title.separator();
231 }
232 recent_files_menu_label(other_title,
233 found_project ? IFACE_("Other Recent Files") : IFACE_("Recent Files"));
234 }
235
236 return found_project + found_other;
237}
238
239static void recent_files_menu_draw(const bContext *C, Menu *menu)
240{
241 ui::Layout &layout = *menu->layout;
243 const bool is_menu_search = CTX_data_int_get(C, "is_menu_search").value_or(false);
244 if (is_menu_search) {
245 template_recent_files(&layout, U.recent_files);
246 }
247 else {
248 const int limit = std::min<int>(U.recent_files, 20);
249 if (recent_files_menu_list(C, layout, limit) != 0) {
250 layout.separator();
251 PointerRNA search_props = layout.op(
252 "WM_OT_search_single_menu", IFACE_("More..."), ICON_VIEWZOOM);
253 RNA_string_set(&search_props, "menu_idname", "TOPBAR_MT_file_open_recent");
254 layout.op("WM_OT_clear_recent_files", IFACE_("Clear Recent Files List..."), ICON_TRASH);
255 }
256 else {
257 layout.label(IFACE_("No Recent Files"), ICON_NONE);
258 }
259 }
260}
261
263{
264 MenuType *mt;
265
266 mt = MEM_new_zeroed<MenuType>("spacetype info menu recent files");
267 STRNCPY_UTF8(mt->idname, "TOPBAR_MT_file_open_recent");
268 STRNCPY_UTF8(mt->label, N_("Open Recent"));
271 WM_menutype_add(mt);
272}
273
274static void undo_history_draw_menu(const bContext *C, Menu *menu)
275{
277 if (wm->runtime->undo_stack == nullptr) {
278 return;
279 }
280
281 int undo_step_count = 0;
282 int undo_step_count_all = 0;
283 for (UndoStep &us : wm->runtime->undo_stack->steps.items_reversed()) {
284 undo_step_count_all += 1;
285 if (us.skip) {
286 continue;
287 }
288 undo_step_count += 1;
289 }
290
291 ui::Layout &split = menu->layout->split(0.0f, false);
292 ui::Layout *column = nullptr;
293
294 const int col_size = 20 + (undo_step_count / 12);
295
296 undo_step_count = 0;
297
298 /* Reverse the order so the most recent state is first in the menu. */
299 int i = undo_step_count_all - 1;
300 for (UndoStep *us = wm->runtime->undo_stack->steps.last(); us; us = us->prev, i--) {
301 if (us->skip) {
302 continue;
303 }
304 if (!(undo_step_count % col_size)) {
305 column = &split.column(false);
306 }
307 const bool is_active = (us == wm->runtime->undo_stack->step_active);
308 ui::Layout &row = column->row(false);
309 row.enabled_set(!is_active);
310 PointerRNA op_ptr = row.op("ED_OT_undo_history",
312 is_active ? ICON_LAYER_ACTIVE : ICON_NONE);
313 RNA_int_set(&op_ptr, "item", i);
314 undo_step_count += 1;
315 }
316}
317
319{
320 MenuType *mt;
321
322 mt = MEM_new_zeroed<MenuType>(__func__);
323 STRNCPY_UTF8(mt->idname, "TOPBAR_MT_undo_history");
324 STRNCPY_UTF8(mt->label, N_("Undo History"));
327 WM_menutype_add(mt);
328}
329
331{
332 writer->write_struct_cast<SpaceTopBar>(sl);
333}
334
336{
337 std::unique_ptr<SpaceType> st = std::make_unique<SpaceType>();
338 ARegionType *art;
339
340 st->spaceid = SPACE_TOPBAR;
341 STRNCPY_UTF8(st->name, "Top Bar");
342 st->struct_size = sizeof(SpaceTopBar);
343
344 st->create = topbar_create;
345 st->free = topbar_free;
346 st->init = topbar_init;
347 st->duplicate = topbar_duplicate;
348 st->operatortypes = topbar_operatortypes;
349 st->keymap = topbar_keymap;
350 st->blend_write = topbar_space_blend_write;
351
352 /* regions: main window */
353 art = MEM_new_zeroed<ARegionType>("spacetype topbar main region");
359 art->prefsizex = UI_UNIT_X * 5; /* Mainly to avoid glitches */
361
362 BLI_addhead(&st->regiontypes, art);
363
364 /* regions: header */
365 art = MEM_new_zeroed<ARegionType>("spacetype topbar header region");
367 art->prefsizey = HEADERY;
368 art->prefsizex = UI_UNIT_X * 5; /* Mainly to avoid glitches */
375
376 BLI_addhead(&st->regiontypes, art);
377
380
381 BKE_spacetype_register(std::move(st));
382}
383
384} // namespace blender
#define STRNCPY_UTF8(dst, src)
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_DEFAULT_BPYRNA
#define HEADERY
#define RGN_ALIGN_ENUM_FROM_MASK(align)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
#define ND_SPACE_INFO
Definition WM_types.hh:564
#define ND_JOB
Definition WM_types.hh:432
#define NC_WM
Definition WM_types.hh:387
#define ND_DATA
Definition WM_types.hh:552
#define NC_SCREEN
Definition WM_types.hh:390
#define ND_MODE
Definition WM_types.hh:464
#define NC_SCENE
Definition WM_types.hh:391
#define NC_WORKSPACE
Definition WM_types.hh:389
#define NC_GPENCIL
Definition WM_types.hh:412
#define ND_LAYER
Definition WM_types.hh:469
#define ND_HISTORY
Definition WM_types.hh:431
#define ND_SPACE_VIEW3D
Definition WM_types.hh:571
#define ND_SCENEBROWSE
Definition WM_types.hh:451
#define NC_SPACE
Definition WM_types.hh:405
#define ELEM(...)
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
void view2d_region_reinit(View2D *v2d, short type, int winx, int winy)
Definition view2d.cc:223
void button_drawflag_enable(Button *but, int flag)
Button * uiDefBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, StringRef str, int x, int y, short width, short height, void *poin, float min, float max, std::optional< StringRef > tip)
@ V2D_COMMONVIEW_HEADER
Definition UI_view2d.hh:64
void block_layout_set_current(Block *block, Layout *layout)
BMesh Iterators.
UserDef U
Definition blender.cc:58
static void topbar_init(wmWindowManager *, ScrArea *)
static void topbar_space_blend_write(BlendWriter *writer, SpaceLink *sl)
static void topbar_header_listener(const wmRegionListenerParams *params)
static void undo_history_draw_menu(const bContext *C, Menu *menu)
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
static void topbar_header_region_init(wmWindowManager *, ARegion *region)
auto BKE_blender_project_read_callback(const Main *bmain, Fn lambda)
void ED_region_header_draw(const bContext *C, ARegion *region)
Definition area.cc:4358
static void undo_history_menu_register()
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
static int recent_files_menu_list(const bContext *C, ui::Layout &layout, const int rows)
static void topbar_main_region_listener(const wmRegionListenerParams *params)
static SpaceLink * topbar_duplicate(SpaceLink *sl)
void ED_spacetype_topbar()
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
wmWindowManager * CTX_wm_manager(const bContext *C)
static void topbar_operatortypes()
static void topbar_free(SpaceLink *)
static void topbar_keymap(wmKeyConfig *)
wmKeyMap * WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
Definition wm_keymap.cc:935
wmEventHandler_Keymap * WM_event_add_keymap_handler(ListBaseT< wmEventHandler > *handlers, wmKeyMap *keymap)
ARegion * BKE_area_region_new()
Definition screen.cc:522
@ ED_KEYMAP_UI
Definition ED_screen.hh:852
@ ED_KEYMAP_HEADER
Definition ED_screen.hh:858
@ ED_KEYMAP_VIEW2D
Definition ED_screen.hh:855
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
bool WM_menutype_add(MenuType *mt)
void BKE_spacetype_register(std::unique_ptr< SpaceType > st)
Definition screen.cc:379
void ED_region_header_init(ARegion *region)
Definition area.cc:4406
static SpaceLink * topbar_create(const ScrArea *, const Scene *)
std::optional< int64_t > CTX_data_int_get(const bContext *C, const char *member)
void ED_region_header_layout(const bContext *C, ARegion *region)
Definition area.cc:4257
void ED_region_do_msg_notify_tag_redraw(bContext *C, wmMsgSubscribeKey *msg_key, wmMsgSubscribeValue *msg_val)
Definition area.cc:394
static void recent_files_menu_label(ui::Layout &layout, const StringRef text)
static void topbar_header_region_message_subscribe(const wmRegionMessageSubscribeParams *params)
static void recent_files_menu_register()
static void topbar_main_region_init(wmWindowManager *wm, ARegion *region)
static void recent_files_menu_draw(const bContext *C, Menu *menu)
void(*) message_subscribe(const wmRegionMessageSubscribeParams *params)
void(*) draw(const bContext *C, ARegion *region)
void(*) listener(const wmRegionListenerParams *params)
void(*) layout(const bContext *C, ARegion *region)
void(*) init(wmWindowManager *wm, ARegion *region)
bke::ARegionRuntime * runtime
eRegion_Alignment alignment
eRegion_Type regiontype
void write_struct_cast(const void *data, BlendStructWriterTypedFn< T > fn=nullptr)
char idname[BKE_ST_MAXNAME]
char translation_context[BKE_ST_MAXNAME]
char label[BKE_ST_MAXNAME]
void(*) draw(const bContext *C, Menu *menu)
ui::Layout * layout
ListBaseT< ARegion > regionbase
ListBaseT< wmEventHandler > handlers
void enabled_set(bool enabled)
void operator_context_set(wm::OpCallContext opcontext)
void label(StringRef name, int icon)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
Layout & split(float percentage, bool align)
Layout & column(bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
Layout & row(bool align)
unsigned int category
Definition WM_types.hh:371
unsigned int data
Definition WM_types.hh:371
#define N_(msgid)
#define WM_msg_subscribe_rna_prop(mbus, id_, data_, type_, prop_, value)