Possible to get active ScrArea from wmWindowManager?

Hi,
Is it possoble o get hold of the active ScrArea from a wmWindowManager pointer?


void function(wmWindowManager *wm){
    ....
    ScrArea *sa = MAGIC(wm); // does this magic function exist, or do I need to pass along bContext?
    ....
}

you would usually use:

ScrArea *sa = CTX_wm_area(C);

C is the full context, but you could check out the CTX_wm_area and see how you can get it from wm

I don’t think there is an actual concept of “active screen area”. I recently asked Campbell about it and, looking at the code, it appears that the closest we’ve got is checking the x/y coords of the last cached wmEvent against the bounds of each area. The following is a snippet that seems to work (at least for UI purposes):

/* only exported for WM */
    wmWindow *win = CTX_wm_window(C);
    bScreen *screen = CTX_wm_screen(C);
    ScrArea *sa;
    
    for (sa = screen->areabase.first; sa; sa = sa->next) {
        /* Get the corners of the screen area */
        short x1 = sa->v1->vec.x;
        short y1 = sa->v1->vec.y;
        short x2 = sa->v3->vec.x;
        short y2 = sa->v3->vec.y;
        
        /* Check if this is the active area */
        rcti testRct;
        BLI_rcti_init(&testRct, x1, x2, y1, y2);
        int areaActive = BLI_rcti_isect_pt(&testRct, win->eventstate->x, win->eventstate->y);
...
}

hm… so what does CTX_wm_area© return then?

As you move around with cursor over different areas, the header regions of areas change their color a little, indicating the active area - so isn’t there an active area?

I asked Campbell that very question and he told me he doesn’t think that there is such a concept in the UI. I was coding up a border highlight around the edges of the “current screen area” and I had to find which one that was through checking the bounds of each area and the mouse coords of the last event (at his suggestion).

weird… i mean even with python you get the current area via bpy.context.area (enter bpy.context.area.type in PyConsole for instance)…

As far as I know, the window manager and window structs do not specifically indicate which area or region is active.

What is stored in bContext is dependant on where in the code you are using it, hence the “context”.

If you are looking at bContext from within an operators function it will have the ScrArea and ARegion of the event that relates to that operator. In the wm_event_do_handlers function of the wm_event_system.c file you can see the context is setup before the event is handled with these 2 lines:

            CTX_wm_area_set(C, area_event_inside(C, &event->x));
            CTX_wm_region_set(C, region_event_inside(C, &event->x));

If you are using bContext from within the drawing code in blender then it stores ScrArea and ARegion of the current area/region you are drawing. You can see this setup in the wm_method_draw_overlap_all function of the wm_draw.c file.

There is another thing that you might be able to use, the bScreen’s subwinactive variable. This is what is used to draw the header color different if the area is active. In the header drawing function ED_region_header of the area.c file you can see this line:

    UI_ThemeClearColor((ED_screen_area_active(C)) ? TH_HEADER : TH_HEADERDESEL);

If you then look in the ED_screen_area_active function in the screen_edit.c file you will see this:

        for (ar = sa->regionbase.first; ar; ar = ar->next)
            if (ar->swinid == sc->subwinactive)
                return 1;

So in theory you could get the current bScreen, loop through the ScrArea and ARegion to find the ARegion’s swinid that matches the bScreen’s subwinactive and then know which ScrArea is active.