/** * @brief Mouse event focus on image array. * * @param iar Image Array widget. * @param bx X position click. * @param by Y position click. */ static void iar_focus( Widget* iar, double bx, double by ) { double y, h; double scroll_pos, hmax; int yelem; int selected; /* element dimensions */ iar_getDim( iar, NULL, &h ); /* number of elements */ yelem = iar->dat.iar.yelem; /* Test for item click. */ selected = iar_focusImage( iar, bx, by ); if (selected >= 0) { iar->dat.iar.selected = selected; if (iar->dat.iar.fptr != NULL) iar->dat.iar.fptr( iar->wdw, iar->name ); } /* Scrollbar click. */ else if (bx > iar->w - 10.) { /* Get bar position (center). */ hmax = h * (yelem - (int)(iar->h / h)); if (fabs(hmax) < 1e-05) scroll_pos = 0.; else scroll_pos = iar->dat.iar.pos / hmax; y = iar->h - (iar->h - 30.) * scroll_pos - 15.; /* Click below the bar. */ if (by < y-15.) iar_scroll( iar, -2 ); /* Click above the bar. */ else if (by > y+15.) iar_scroll( iar, +2 ); /* Click on the bar. */ else iar->status = WIDGET_STATUS_SCROLLING; } }
/** * @brief Image array widget mouse click handler. * * @param iar Widget receiving the event. * @param mclick The mouse click event. * @return 1 if event is used. */ static int iar_mclick( Widget* iar, int button, int x, int y ) { /* Handle different mouse clicks. */ switch (button) { case SDL_BUTTON_LEFT: iar_focus( iar, x, y ); return 1; case SDL_BUTTON_WHEELUP: iar_scroll( iar, +1 ); return 1; case SDL_BUTTON_WHEELDOWN: iar_scroll( iar, -1 ); return 1; case SDL_BUTTON_RIGHT: iar_focus( iar, x, y ); if (iar->dat.iar.rmptr != NULL) iar->dat.iar.rmptr( iar->wdw, iar->name ); return 1; default: break; } return 0; }
/** * @brief Handles mouse movement for an image array. * * @param iar Widget handling the mouse motion. * @param mmove Mouse motion event to handle. * @return 1 if the event is used. */ static int iar_mmove( Widget* iar, int x, int y, int rx, int ry ) { (void) rx; (void) ry; double w,h; int xelem, yelem; double hmax; if (iar->status == WIDGET_STATUS_SCROLLING) { y = CLAMP( 15, iar->h - 15., iar->h - y ); /* element dimensions */ iar_getDim( iar, &w, &h ); /* number of elements */ xelem = iar->dat.iar.xelem; yelem = iar->dat.iar.yelem; hmax = h * (yelem - (int)(iar->h / h)); iar->dat.iar.pos = (y - 15.) * hmax / (iar->h - 30.); /* Does boundry checks. */ iar_scroll( iar, 0 ); return 1; } else { if ((x < 0) || (x >= iar->w) || (y < 0) || (y >= iar->h)) iar->dat.iar.alt = -1; else { iar->dat.iar.alt = iar_focusImage( iar, x, y ); iar->dat.iar.altx = x; iar->dat.iar.alty = y; } } return 0; }