ImageGear for C and C++ on Linux v18.10 - Updated
Display an Image with Scroll Bars
User Guide > Getting Started > Samples > Display an Image with Scroll Bars

The Filters sample program demonstrates how to add Image scrolling to your application. This example is based on ImageGear’s IG_dspl_scroll_get(), IG_dspl_scroll_set(), and IG_dspl_scroll_to() functions. If the size of the displayed image is larger than the viewable area of output device, ImageGear automatically gets and sets the scrolling position of the image within the area. The IG_dspl_scroll_get() function returns the minimum, maximum, and current scroll positions in both horizontal and vertical directions. The IG_dspl_scroll_to() function allows you to set a new scroll position.

Perform the following steps:

  1. Create two scroll bars:
     
    Copy Code

    XtSetArg( args[0], XmNorientation, XmHORIZONTAL );
    wHScrollbar = XmCreateScrollBar( main_win, "HScroll", args, 1 );
    XtSetArg( args[0], XmNorientation, XmVERTICAL );
    wVScrollbar = XmCreateScrollBar( main_win, "VScroll", args, 1 );

  2. Prepare two Callback functions for scroll-seven handling. These functions should get the new scroll position from the scrollbar widgets and pass it to ImageGear:
     
    Copy Code

    /****************************************************************************/
    void ctl_H_Scrollbar_callback(Widget w, XtPointer data, XtPointer call_data )
    {
    XmScrollBarCallbackStruct*cbs = (XmScrollBarCallbackStruct*)call_data;
    AT_MODEnScrollMode;
    AT_SCROLL_INFO scroll_info;
    if( cbs->reason==XmCR_VALUE_CHANGED )
    {
    if( IG_image_is_valid(hIGear) )
    {
    IG_dspl_scroll_get( hIGear, IG_GRP_DEFAULT, XtWindow(img_win), &nScrollMode,
    &scroll_info );
    IG_dspl_scroll_to( hIGear, IG_GRP_DEFAULT, XtWindow(img_win),
    + Scroll_info.h_min, scroll_info.h_cur_pos, NULL );
    }
    }}

  3. And Vertical scroll bar:
     
    Copy Code

    /*****************************************************************************/
    void ctl_V_Scrollbar_callback(Widget w, YtPointer data, YtPointer call_data )
    {
    YmScrollBarCallbackStruct*cbs = (YmScrollBarCallbackStruct*)call_data;
    AT_MODE nScrollMode;
    AT_SCROLL_INFO scroll_info;
    if( cbs->reason==YmCR_VALUE_CHANGED )
    {
    if( IG_image_is_valid(hIGear) )
    {
    IG_dspl_scroll_get( hIGear, IG_GRP_DEFAULT, YtWindow(img_win),
    &nScrollMode, &scroll_info );
    IG_dspl_scroll_to( hIGear, IG_GRP_DEFAULT, YtWindow(img_win),
    cbs->value + scroll_info.v_min,
    scroll_info.v_cur_pos, NULL );
    }
    }}

  4. Associate those Callback functions with scroll bar widgets:
     
    Copy Code

    XtManageChild( wHScrollbar );
    XtManageChild( wVScrollbar );
    XtAddCallback( wHScrollbar, XmNvalueChangedCallback,
    (XtCallbackProc)ctl_H_Scrollbar_callback, (XtPointer)NULL );
    XtAddCallback( wVScrollbar, XmNvalueChangedCallback,
    (XtCallbackProc)ctl_V_Scrollbar_callback, (XtPointer)NULL );

  5. Update the scroll bars every time the image size is changed, using the following code:
     
    Copy Code

    void ScrollUpdate()
    {
    AT_MODE nScrollMode;
    AT_SCROLL_INFO scroll_info;
    if( IG_image_is_valid(hIGear) )
    {
    IG_dspl_scroll_get( hIGear, IG_GRP_DEFAULT, XtWindow(img_win), &nScrollMode,
    &scroll_info );
    XtVaSetValues( wHScrollbar,
    XmNminimum, 0,
    XmNmaximum, scroll_info.h_max - scroll_info.h_min + 10,
    XmNsliderSize, 10,
    XmNvalue, (int)(scroll_info.h_cur_pos-scroll_info.h_min),
    NULL );
    XtVaSetValues( wVScrollbar,
    XmNminimum, 0,
    XmNmaximum, scroll_info.v_max - scroll_info.v_min + 10,
    XmNsliderSize, 10,
    XmNvalue, (int)(scroll_info.v_cur_pos-scroll_info.v_min),
    NULL );
    }else
    {
    XtVaSetValues( wHScrollbar,
    XmNminimum, 0,
    XmNmaximum, 0,
    XmNsliderSize, 10,
    XmNvalue, 0,
    NULL );
    XtVaSetValues( wVScrollbar,
    XmNminimum, 0,
    XmNmaximum, 0,
    XmNsliderSize, 10,
    XmNvalue, 0,
    NULL );
    }}