I'm printing a list of files to a pad using the ncurses library in C. I've already managed to code vertical scrolling for both up and down directions by detecting mouse event. Is there some way to detect horizontal scrolling as well?
Example Code
#include <ncurses.h>
#include <stdio.h>
#define MAX_FILES 100
#define FILE_NAME_SZ 100
int main() {
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
mousemask(ALL_MOUSE_EVENTS, NULL);
int pad_ht, pad_width, pad_start_y, pad_start_x, pad_end_y, pad_end_x;
int pad_scroll_y = 0;
int matches = MAX_FILES;
char filelist[MAX_FILES][FILE_NAME_SZ];
int ch;
MEVENT event;
WINDOW *pad;
/* dummy file list */
for (int i = 0; i < matches; i++) {
snprintf(filelist[i], FILE_NAME_SZ, "file_%03d.txt", i);
}
/* Initialize pad */
pad_ht = matches;
pad_width = 3 * (getmaxx(stdscr) / 4);
pad_start_y = 0;
pad_start_x = getmaxx(stdscr) / 4;
pad_end_x = getmaxx(stdscr);
pad_end_y = 3 * (getmaxy(stdscr) / 4);
/* Create pad and enable scrolling */
pad = newpad(pad_ht, pad_width);
scrollok(pad, TRUE);
/* Print list of files into pad and show pad */
wmove(pad, 0, 0);
for (int i = 0; i < matches; i++) {
wprintw(pad, "%d: %s\n", i, filelist[i]);
}
prefresh(pad, pad_scroll_y, 0, pad_start_y, pad_start_x, pad_end_y - 1, pad_end_x - 1);
/* scroll the screen, to see the list displayed */
/* loop to show pad and detect key events */
while (true) {
ch = getch();
/* If mouse event detected, check the direction */
if (ch == KEY_MOUSE) {
if (getmouse(&event) == OK) {
/* Scroll up */
if (event.bstate & BUTTON4_PRESSED) {
/* Decrement pad_scroll_y only if within screen limits */
if (pad_scroll_y > 0)
--pad_scroll_y;
prefresh(pad, pad_scroll_y, 0, pad_start_y, pad_start_x, pad_end_y - 1, pad_end_x - 1);
/* Scroll down */
} else if (event.bstate & BUTTON5_PRESSED) {
/* Increment pad_scroll_y only if within screen limits */
if (pad_scroll_y < matches - (pad_end_y - pad_start_y))
++pad_scroll_y;
prefresh(pad, pad_scroll_y, 0, pad_start_y, pad_start_x, pad_end_y - 1, pad_end_x - 1);
}
}
} else if (ch == 'q') {
/* exit if q is pressed */
break;
}
}
delwin(pad);
endwin();
return 0;
}
I'm aware of the logic used for horizontal scrolling and can implement it using arrow keys, but I'd prefer if there was a way to detect horizontal scrolling on the mouse/track pad. If that's not possible, maybe a way to detect if any modifier key is pressed while scrolling.
No, you cannot do this (barring a lot of work):
mmask_t
.Changing that mmask_t
to be 64 bits for example would change the binary interface. Going from the previous ABI 5 to ncurses 6 was a lot of work by all involved.
One could decode the mouse escape sequences directly, but that also would be a lot of work.
Other than adding a new function (for the rare developer who is able to manipulate a wheel mouse in this fashion), the possibility remains to build an ABI 7 ncurses which would provide the 64-bit mmask_t
. That much was done in May 2020.