Blender  V2.93
space_topbar.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2017 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "MEM_guardedalloc.h"
28 
29 #include "BLI_blenlib.h"
30 #include "BLI_utildefines.h"
31 
32 #include "BLO_readfile.h"
33 #include "BLT_translation.h"
34 
35 #include "BKE_context.h"
36 #include "BKE_global.h"
37 #include "BKE_screen.h"
38 
39 #include "ED_screen.h"
40 #include "ED_space_api.h"
41 
42 #include "UI_interface.h"
43 #include "UI_resources.h"
44 #include "UI_view2d.h"
45 
46 #include "RNA_access.h"
47 
48 #include "WM_api.h"
49 #include "WM_message.h"
50 #include "WM_types.h"
51 
52 /* ******************** default callbacks for topbar space ***************** */
53 
55 {
56  ARegion *region;
57  SpaceTopBar *stopbar;
58 
59  stopbar = MEM_callocN(sizeof(*stopbar), "init topbar");
60  stopbar->spacetype = SPACE_TOPBAR;
61 
62  /* header */
63  region = MEM_callocN(sizeof(ARegion), "left aligned header for topbar");
64  BLI_addtail(&stopbar->regionbase, region);
65  region->regiontype = RGN_TYPE_HEADER;
66  region->alignment = RGN_ALIGN_TOP;
67  region = MEM_callocN(sizeof(ARegion), "right aligned header for topbar");
68  BLI_addtail(&stopbar->regionbase, region);
69  region->regiontype = RGN_TYPE_HEADER;
71 
72  /* main regions */
73  region = MEM_callocN(sizeof(ARegion), "main region of topbar");
74  BLI_addtail(&stopbar->regionbase, region);
75  region->regiontype = RGN_TYPE_WINDOW;
76 
77  return (SpaceLink *)stopbar;
78 }
79 
80 /* not spacelink itself */
81 static void topbar_free(SpaceLink *UNUSED(sl))
82 {
83 }
84 
85 /* spacetype; init callback */
86 static void topbar_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(area))
87 {
88 }
89 
91 {
92  SpaceTopBar *stopbarn = MEM_dupallocN(sl);
93 
94  /* clear or remove stuff from old */
95 
96  return (SpaceLink *)stopbarn;
97 }
98 
99 /* add handlers, stuff you only do once or on area/region changes */
101 {
102  wmKeyMap *keymap;
103 
104  /* force delayed UI_view2d_region_reinit call */
106  region->flag |= RGN_FLAG_DYNAMIC_SIZE;
107  }
108  UI_view2d_region_reinit(&region->v2d, V2D_COMMONVIEW_HEADER, region->winx, region->winy);
109 
110  keymap = WM_keymap_ensure(wm->defaultconf, "View2D Buttons List", 0, 0);
111  WM_event_add_keymap_handler(&region->handlers, keymap);
112 }
113 
114 static void topbar_operatortypes(void)
115 {
116 }
117 
118 static void topbar_keymap(struct wmKeyConfig *UNUSED(keyconf))
119 {
120 }
121 
122 /* add handlers, stuff you only do once or on area/region changes */
124 {
126  region->flag |= RGN_FLAG_DYNAMIC_SIZE;
127  }
128  ED_region_header_init(region);
129 }
130 
132 {
133  ARegion *region = params->region;
134  wmNotifier *wmn = params->notifier;
135 
136  /* context changes */
137  switch (wmn->category) {
138  case NC_WM:
139  if (wmn->data == ND_HISTORY) {
140  ED_region_tag_redraw(region);
141  }
142  break;
143  case NC_SCENE:
144  if (wmn->data == ND_MODE) {
145  ED_region_tag_redraw(region);
146  }
147  break;
148  case NC_SPACE:
149  if (wmn->data == ND_SPACE_VIEW3D) {
150  ED_region_tag_redraw(region);
151  }
152  break;
153  case NC_GPENCIL:
154  if (wmn->data == ND_DATA) {
155  ED_region_tag_redraw(region);
156  }
157  break;
158  }
159 }
160 
162 {
163  ARegion *region = params->region;
164  wmNotifier *wmn = params->notifier;
165 
166  /* context changes */
167  switch (wmn->category) {
168  case NC_WM:
169  if (wmn->data == ND_JOB) {
170  ED_region_tag_redraw(region);
171  }
172  break;
173  case NC_SPACE:
174  if (wmn->data == ND_SPACE_INFO) {
175  ED_region_tag_redraw(region);
176  }
177  break;
178  case NC_SCREEN:
179  if (wmn->data == ND_LAYER) {
180  ED_region_tag_redraw(region);
181  }
182  break;
183  case NC_SCENE:
184  if (wmn->data == ND_SCENEBROWSE) {
185  ED_region_tag_redraw(region);
186  }
187  break;
188  }
189 }
190 
192 {
193  struct wmMsgBus *mbus = params->message_bus;
194  WorkSpace *workspace = params->workspace;
195  ARegion *region = params->region;
196 
197  wmMsgSubscribeValue msg_sub_value_region_tag_redraw = {
198  .owner = region,
199  .user_data = region,
201  };
202 
204  mbus, &workspace->id, workspace, WorkSpace, tools, &msg_sub_value_region_tag_redraw);
205 }
206 
207 static void recent_files_menu_draw(const bContext *UNUSED(C), Menu *menu)
208 {
209  struct RecentFile *recent;
210  uiLayout *layout = menu->layout;
212  if (!BLI_listbase_is_empty(&G.recent_files)) {
213  for (recent = G.recent_files.first; (recent); recent = recent->next) {
214  const char *file = BLI_path_basename(recent->filepath);
215  const int icon = BLO_has_bfile_extension(file) ? ICON_FILE_BLEND : ICON_FILE_BACKUP;
216  PointerRNA ptr;
217  uiItemFullO(layout, "WM_OT_open_mainfile", file, icon, NULL, WM_OP_INVOKE_DEFAULT, 0, &ptr);
218  RNA_string_set(&ptr, "filepath", recent->filepath);
219  RNA_boolean_set(&ptr, "display_file_selector", false);
220  }
221  }
222  else {
223  uiItemL(layout, IFACE_("No Recent Files"), ICON_NONE);
224  }
225 }
226 
227 static void recent_files_menu_register(void)
228 {
229  MenuType *mt;
230 
231  mt = MEM_callocN(sizeof(MenuType), "spacetype info menu recent files");
232  strcpy(mt->idname, "TOPBAR_MT_file_open_recent");
233  strcpy(mt->label, N_("Open Recent"));
236  WM_menutype_add(mt);
237 }
238 
239 /* only called once, from space/spacetypes.c */
241 {
242  SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype topbar");
243  ARegionType *art;
244 
245  st->spaceid = SPACE_TOPBAR;
246  strncpy(st->name, "Top Bar", BKE_ST_MAXNAME);
247 
248  st->create = topbar_create;
249  st->free = topbar_free;
250  st->init = topbar_init;
253  st->keymap = topbar_keymap;
254 
255  /* regions: main window */
256  art = MEM_callocN(sizeof(ARegionType), "spacetype topbar main region");
257  art->regionid = RGN_TYPE_WINDOW;
262  art->prefsizex = UI_UNIT_X * 5; /* Mainly to avoid glitches */
264 
265  BLI_addhead(&st->regiontypes, art);
266 
267  /* regions: header */
268  art = MEM_callocN(sizeof(ARegionType), "spacetype topbar header region");
269  art->regionid = RGN_TYPE_HEADER;
270  art->prefsizey = HEADERY;
271  art->prefsizex = UI_UNIT_X * 5; /* Mainly to avoid glitches */
278 
279  BLI_addhead(&st->regiontypes, art);
280 
282 
284 }
#define BKE_ST_MAXNAME
Definition: BKE_screen.h:68
void BKE_spacetype_register(struct SpaceType *st)
Definition: screen.c:420
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:124
void BLI_addhead(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:87
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
const char * BLI_path_basename(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
Definition: path_util.c:1868
#define UNUSED(x)
#define ELEM(...)
external readfile function prototypes.
bool BLO_has_bfile_extension(const char *str)
Definition: readfile.c:1684
#define IFACE_(msgid)
#define BLT_I18NCONTEXT_DEFAULT_BPYRNA
#define N_(msgid)
#define HEADERY
#define RGN_ALIGN_ENUM_FROM_MASK(align)
@ RGN_ALIGN_TOP
@ RGN_ALIGN_RIGHT
@ RGN_SPLIT_PREV
@ RGN_FLAG_DYNAMIC_SIZE
@ RGN_TYPE_WINDOW
@ RGN_TYPE_HEADER
@ SPACE_TOPBAR
void ED_region_do_msg_notify_tag_redraw(struct bContext *C, struct wmMsgSubscribeKey *msg_key, struct wmMsgSubscribeValue *msg_val)
void ED_region_header_layout(const struct bContext *C, struct ARegion *region)
void ED_region_tag_redraw(struct ARegion *region)
Definition: area.c:667
void ED_region_header_init(struct ARegion *region)
Definition: area.c:3358
@ ED_KEYMAP_UI
Definition: ED_screen.h:440
@ ED_KEYMAP_HEADER
Definition: ED_screen.h:446
@ ED_KEYMAP_VIEW2D
Definition: ED_screen.h:443
void ED_region_header_draw(const struct bContext *C, struct ARegion *region)
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
void uiLayoutSetOperatorContext(uiLayout *layout, int opcontext)
void uiItemFullO(uiLayout *layout, const char *opname, const char *name, int icon, struct IDProperty *properties, int context, int flag, struct PointerRNA *r_opptr)
void uiItemL(uiLayout *layout, const char *name, int icon)
#define UI_UNIT_X
void UI_view2d_region_reinit(struct View2D *v2d, short type, int winx, int winy)
Definition: view2d.c:240
@ V2D_COMMONVIEW_HEADER
Definition: UI_view2d.h:57
#define ND_SPACE_INFO
Definition: WM_types.h:416
#define ND_JOB
Definition: WM_types.h:315
#define NC_WM
Definition: WM_types.h:276
#define ND_DATA
Definition: WM_types.h:408
@ WM_OP_INVOKE_DEFAULT
Definition: WM_types.h:197
#define NC_SCREEN
Definition: WM_types.h:278
#define ND_MODE
Definition: WM_types.h:345
#define NC_SCENE
Definition: WM_types.h:279
#define NC_GPENCIL
Definition: WM_types.h:300
#define ND_LAYER
Definition: WM_types.h:350
#define ND_HISTORY
Definition: WM_types.h:314
#define ND_SPACE_VIEW3D
Definition: WM_types.h:423
#define ND_SCENEBROWSE
Definition: WM_types.h:332
#define NC_SPACE
Definition: WM_types.h:293
FILE * file
Scene scene
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static void area(int d1, int d2, int e1, int e2, float weights[2])
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
Definition: rna_access.c:6550
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
Definition: rna_access.c:6272
static void topbar_free(SpaceLink *UNUSED(sl))
Definition: space_topbar.c:81
static void topbar_keymap(struct wmKeyConfig *UNUSED(keyconf))
Definition: space_topbar.c:118
static void recent_files_menu_register(void)
Definition: space_topbar.c:227
void ED_spacetype_topbar(void)
Definition: space_topbar.c:240
static void topbar_operatortypes(void)
Definition: space_topbar.c:114
static void topbar_header_listener(const wmRegionListenerParams *params)
Definition: space_topbar.c:161
static SpaceLink * topbar_create(const ScrArea *UNUSED(area), const Scene *UNUSED(scene))
Definition: space_topbar.c:54
static void recent_files_menu_draw(const bContext *UNUSED(C), Menu *menu)
Definition: space_topbar.c:207
static void topbar_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(area))
Definition: space_topbar.c:86
static void topbar_header_region_init(wmWindowManager *UNUSED(wm), ARegion *region)
Definition: space_topbar.c:123
static void topbar_main_region_init(wmWindowManager *wm, ARegion *region)
Definition: space_topbar.c:100
static void topbar_header_region_message_subscribe(const wmRegionMessageSubscribeParams *params)
Definition: space_topbar.c:191
static SpaceLink * topbar_duplicate(SpaceLink *sl)
Definition: space_topbar.c:90
static void topbar_main_region_listener(const wmRegionListenerParams *params)
Definition: space_topbar.c:131
void(* draw)(const struct bContext *C, struct ARegion *region)
Definition: BKE_screen.h:169
void(* message_subscribe)(const wmRegionMessageSubscribeParams *params)
Definition: BKE_screen.h:185
void(* listener)(const wmRegionListenerParams *params)
Definition: BKE_screen.h:183
int keymapflag
Definition: BKE_screen.h:226
void(* layout)(const struct bContext *C, struct ARegion *region)
Definition: BKE_screen.h:179
void(* init)(struct wmWindowManager *wm, struct ARegion *region)
Definition: BKE_screen.h:165
ListBase handlers
short alignment
short regiontype
char label[BKE_ST_MAXNAME]
Definition: BKE_screen.h:376
char idname[BKE_ST_MAXNAME]
Definition: BKE_screen.h:375
void(* draw)(const struct bContext *C, struct Menu *menu)
Definition: BKE_screen.h:384
char translation_context[BKE_ST_MAXNAME]
Definition: BKE_screen.h:377
struct uiLayout * layout
Definition: BKE_screen.h:392
struct RecentFile * next
Definition: WM_types.h:990
char * filepath
Definition: WM_types.h:991
ListBase regionbase
struct SpaceLink *(* duplicate)(struct SpaceLink *sl)
Definition: BKE_screen.h:104
ListBase regiontypes
Definition: BKE_screen.h:130
void(* keymap)(struct wmKeyConfig *keyconf)
Definition: BKE_screen.h:109
void(* operatortypes)(void)
Definition: BKE_screen.h:107
void(* free)(struct SpaceLink *sl)
Definition: BKE_screen.h:88
struct SpaceLink *(* create)(const struct ScrArea *area, const struct Scene *scene)
Definition: BKE_screen.h:86
void(* init)(struct wmWindowManager *wm, struct ScrArea *area)
Definition: BKE_screen.h:91
int spaceid
Definition: BKE_screen.h:81
char name[BKE_ST_MAXNAME]
Definition: BKE_screen.h:80
unsigned int data
Definition: WM_types.h:260
unsigned int category
Definition: WM_types.h:260
struct wmKeyConfig * defaultconf
#define G(x, y, z)
wmEventHandler_Keymap * WM_event_add_keymap_handler(ListBase *handlers, wmKeyMap *keymap)
PointerRNA * ptr
Definition: wm_files.c:3157
wmKeyMap * WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
Definition: wm_keymap.c:852
bool WM_menutype_add(MenuType *mt)
Definition: wm_menu_type.c:65
#define WM_msg_subscribe_rna_prop(mbus, id_, data_, type_, prop_, value)