Search code examples
iup

IUP strange tab close behavior


I am trying to create a simple text editor with IUP using IupTabs and IupScintilla. However, when running on Linux, I am encountering a strange tab-closing behavior that I cannot seem to resolve.

When there are multiple tabs present, and the first is removed (either with the SHOWCLOSE button or via my callback close_cb, the single remaining tab is left blank. I have tried all combinations of IupUpdate, IupRedraw, IupRefresh, and IupRefreshChildren and nothing seems to force the tab to be drawn correctly.

I even dove into the IUP source and located the gtkTabsCloseButtonClicked function in iupgtk_tabs.c (line 307) to verify that the tab is being removed with the following code, so that my callback close_cb is at least performing the same action.

if (ret == IUP_CONTINUE) /* destroy tab and children */
{
  IupDestroy(child);
  IupRefreshChildren(ih);
}

I have distilled the problem down to the sample code below. I am running this on Lubuntu 12.04 x86-64. IUP version is iup-3.16_Linux32_64_lib, downloaded from Sourceforge. The problem only occurs on GTK and not Windows. I am not sure if this is a bug or if I am just doing something wrong. If it is a bug, I am not sure if it is related to GTK or IUP.

#include <iup.h>
#include <iup_scintilla.h>
#include <stdlib.h>

int new_cb( Ihandle* self )
{
    // create a new Scintilla control
    Ihandle* sci = IupScintilla();
    IupSetAttributes( sci, "TABSIZE=4, EXPAND=YES, VISIBLE=YES, "
        "TABTITLE=Untitled, TABIMAGE=IUP_FileSave" );

    // add the Scintilla to the tabs
    Ihandle* tabs = IupGetHandle( "tabs" );
    IupAppend( tabs, sci );
    IupMap( sci );
    IupSetAttributeHandle( tabs, "VALUE", sci );
    IupSetFocus( sci );
    IupRefresh( tabs );

    return IUP_IGNORE;
}

int close_cb( Ihandle* self )
{
    Ihandle* tabs = IupGetHandle( "tabs" );

    int old_count = IupGetChildCount( tabs );
    if ( old_count == 0 ) return IUP_IGNORE;

    // remove the current tab
    Ihandle* old_tab = IupGetAttributeHandle( tabs, "VALUE" );

    // see iupgtk_tabs.c:307
    IupDestroy( old_tab );
    IupRefreshChildren( tabs );

    int new_count = IupGetChildCount( tabs );
    if ( new_count == 0 ) return IUP_IGNORE;

    // set focus to the new tab
    Ihandle* new_tab = IupGetAttributeHandle( tabs, "VALUE" );
    IupSetFocus( new_tab );

    return IUP_IGNORE;
}

int tabchange_cb( Ihandle* self, Ihandle* old_tab, Ihandle* new_tab )
{
    // set focus to the new tab
    IupSetFocus( new_tab );
    return IUP_CONTINUE;
}

int tabclose_cb( Ihandle* self, int pos )
{
    // allow the old tab to be removed
    return IUP_CONTINUE;
}

int main( int argc, char** argv )
{
    IupOpen( &argc, &argv );
    IupScintillaOpen();
    IupImageLibOpen();

    Ihandle* tabs = IupTabs( NULL );
    IupSetAttribute( tabs, "SHOWCLOSE", "YES" );
    IupSetCallback( tabs, "TABCHANGE_CB", (Icallback)tabchange_cb );
    IupSetCallback( tabs, "TABCLOSE_CB", (Icallback)tabclose_cb );
    IupSetHandle( "tabs", tabs );

    Ihandle* dialog = IupDialog( tabs );
    IupSetAttribute( dialog, "SIZE", "HALFxHALF" );
    IupSetAttribute( dialog, "TITLE", "Tabs Demo" );
    IupSetHandle( "dialog", dialog );

    // Ctrl+N opens a new tab    
    IupSetCallback( dialog, "K_cN", (Icallback)new_cb );

    // Ctrl+W closes the current tab
    IupSetCallback( dialog, "K_cW", (Icallback)close_cb );

    IupShowXY( dialog, IUP_CENTER, IUP_CENTER );

    // add a new tab by default
    new_cb( NULL );

    IupMainLoop();

    IupClose();

    return EXIT_SUCCESS;
}

Screenshot: tabsdemo.png


Solution

  • That was a bug in IUP. It is fixed now in SVN. Since today.