frang75 / nappgui_src

SDK for building cross-platform desktop apps in ANSI-C
https://www.nappgui.com
MIT License
442 stars 43 forks source link

Mouse position in views #103

Closed SamSandq closed 3 months ago

SamSandq commented 3 months ago

Currently it is possible to obtain the global mouse position using V2Df pos = gui_mouse();

This should be enhanced to accept an argument, a view, and would then return the mouse position inside that view. This is used for instance when one wants use the relative position inside a view for positioning something, where a pos = gui_mouse(myView)would suffice. On a NULL argument the current global position should be returned.

frang75 commented 3 months ago

This commit https://github.com/frang75/nappgui_src/commit/ee5c8c8d76b3604151f5d4bbea2e2f405dbfc2ed (Overlay Windows) adds two functions that can be used for this task. window_control_frame() https://nappgui.com/en/gui/window.html#f26 window_client_to_screen() https://nappgui.com/en/gui/window.html#f27

An example of use: https://nappgui.com/en/howto/guihello.html#h11

SamSandq commented 3 months ago

I checked out your example, and it seems to do the trick nicely. Thanks!

frang75 commented 3 months ago

I close this issue, feel free to reopen if needed.

SamSandq commented 2 months ago

I tested it and it does not achieve want I want. Specifically, my code is...

    if (dragEventType == ddPerform) {
        //where?
        V2Df vpos = mouse_position_in_view(view_native(app->view));
        j_mouse_cell(app, vpos.x, vpos.y, 0);

        //which photo thumbnail?
        uint32_t nPhoto = (app->mouse_cell_y) * app->ncols + app->mouse_cell_x;

        //past end?
        if (app->numImg <= nPhoto) return;

In other words, I am dragging a tag to the view where my thumbnail photos are and want to know where it was dropped. It's not a control, a view, so the window_control_frame cannot be used, and the window_control_frame does not help. I use the same kind of code to find out all drop locations.

I need a mouse_position_in_view(...) like I have, or a better gui_mouse(<view or control or NULL>) .

frang75 commented 2 months ago

To get the mouse position in view coordinates you have to ways:

mpos.x = spos.x - wpos.x - frame.x; mpos.y = ...

SamSandq commented 2 months ago

I actually code very close to your first option; my function mouse_position_in_view(view_native(....)) and it works just fine.

I tried code more or less the same as yours, and it failed --- as does yours:

Screenshot 2024-02-17 at 20 17 17

So something is amiss, but what?

frang75 commented 2 months ago

Without debugging the only thing occurs to me is a bug on window_control_frame. Change the code by this. If issue continue, try the get into window_control_frame for more info.

R2Df window_control_frame(const Window *window, const GuiControl *control)
{
    R2Df r2d;
    GuiComponent *component = (GuiComponent*)control;
    Cell *cell = NULL;
    cassert_no_null(window);
    cassert_no_null(component);
    cassert(_component_window(component) == window);
    cassert(i_in_active_layout(window, component) == TRUE);
    _component_get_origin(component, &r2d.pos);
    _component_get_size(component, &r2d.size);
    cell = component->parent;
    for (; cell != NULL;)
    {
        V2Df panel_pos;
        Layout *layout = _cell_parent(cell);
        Panel *panel = _layout_panel(layout);
        if (panel != NULL)
        {
            GuiComponent *panel_component = (GuiComponent *)panel;
            _component_get_origin(panel_component, &panel_pos);
            r2d.pos.x += panel_pos.x;
            r2d.pos.y += panel_pos.y;
            cell = panel_component->parent;
        }
        else
        {
            cell = NULL;
        }
    }

    return r2d;
}
SamSandq commented 2 months ago

I will check that tomorrow… hope we can it resolved