diff --git a/src/terminal/scrollbar.c b/src/terminal/scrollbar.c index 45eb37f6..9727c389 100644 --- a/src/terminal/scrollbar.c +++ b/src/terminal/scrollbar.c @@ -213,14 +213,8 @@ void guac_terminal_scrollbar_flush(guac_terminal_scrollbar* scrollbar) { calculate_state(scrollbar, &new_state, &new_value); /* Notify of scroll if value is changing */ - if (new_value != old_value) { - - /* TODO: Call scroll value handler */ - guac_client_log(scrollbar->client, GUAC_LOG_DEBUG, - "SCROLL: min=%i max=%i value=%i", - scrollbar->min, scrollbar->max, new_value); - - } + if (new_value != old_value && scrollbar->scroll_handler) + scrollbar->scroll_handler(scrollbar, new_value); /* Reposition container if moved */ if (old_state->container_x != new_state.container_x diff --git a/src/terminal/scrollbar.h b/src/terminal/scrollbar.h index f46a42a8..13c66b12 100644 --- a/src/terminal/scrollbar.h +++ b/src/terminal/scrollbar.h @@ -96,12 +96,21 @@ typedef struct guac_terminal_scrollbar_render_state { } guac_terminal_scrollbar_render_state; +typedef struct guac_terminal_scrollbar guac_terminal_scrollbar; + +/** + * Handler which is called whenever the scrollbar value changes outside a call + * to guac_terminal_scrollbar_set_value(). + */ +typedef void guac_terminal_scrollbar_scroll_handler( + guac_terminal_scrollbar* scrollbar, int value); + /** * A scrollbar, made up of a containing layer and inner draggable handle. The * position of the handle within the layer represents the value of the * scrollbar. */ -typedef struct guac_terminal_scrollbar { +struct guac_terminal_scrollbar { /** * The client associated with this scrollbar. @@ -177,7 +186,18 @@ typedef struct guac_terminal_scrollbar { */ int drag_current_y; -} guac_terminal_scrollbar; + /** + * The function to call when the scrollbar handle is being dragged, and + * the new scrollbar value needs to be handled and assigned. + */ + guac_terminal_scrollbar_scroll_handler* scroll_handler; + + /** + * Arbitrary reference to data related to this scrollbar. + */ + void* data; + +}; /** * Allocates a new scrollbar, associating that scrollbar with the given client diff --git a/src/terminal/terminal.c b/src/terminal/terminal.c index 3c2f7f92..48d9842b 100644 --- a/src/terminal/terminal.c +++ b/src/terminal/terminal.c @@ -264,6 +264,10 @@ guac_terminal* guac_terminal_create(guac_client* client, term->scrollbar = guac_terminal_scrollbar_alloc(term->client, GUAC_DEFAULT_LAYER, width, height, term->term_height); + /* Associate scrollbar with this terminal */ + term->scrollbar->data = term; + term->scrollbar->scroll_handler = guac_terminal_scroll_handler; + /* Init terminal */ guac_terminal_reset(term); @@ -1472,6 +1476,24 @@ int guac_terminal_send_mouse(guac_terminal* term, int x, int y, int mask) { } +void guac_terminal_scroll_handler(guac_terminal_scrollbar* scrollbar, int value) { + + guac_terminal* terminal = (guac_terminal*) scrollbar->data; + + /* Calculate change in scroll offset */ + int delta = -value - terminal->scroll_offset; + + /* Update terminal based on change in scroll offset */ + if (delta < 0) + guac_terminal_scroll_display_down(terminal, -delta); + else if (delta > 0) + guac_terminal_scroll_display_up(terminal, delta); + + /* Update scrollbar value */ + guac_terminal_scrollbar_set_value(scrollbar, value); + +} + void guac_terminal_clipboard_reset(guac_terminal* term, const char* mimetype) { guac_common_clipboard_reset(term->clipboard, mimetype); } diff --git a/src/terminal/terminal.h b/src/terminal/terminal.h index 5f6e7ec2..3619ae15 100644 --- a/src/terminal/terminal.h +++ b/src/terminal/terminal.h @@ -388,6 +388,19 @@ int guac_terminal_send_key(guac_terminal* term, int keysym, int pressed); */ int guac_terminal_send_mouse(guac_terminal* term, int x, int y, int mask); +/** + * Handles a scroll event received from the scrollbar associated with a + * terminal. + * + * @param scrollbar + * The scrollbar that has been scrolled. + * + * @param value + * The new value that should be stored within the scrollbar, and + * represented within the terminal display. + */ +void guac_terminal_scroll_handler(guac_terminal_scrollbar* scrollbar, int value); + /** * Clears the current clipboard contents and sets the mimetype for future * contents.