April 23, 2024, 03:24:04 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Tab switcher

Started by Parker, February 15, 2007, 09:01:48 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Parker

This is something I thought of a couple years ago but never got around to it. For anyone using (or thinking about using) a tabbed interface, a component like this should make it a lot easier to handle showing and hiding the controls on each sheet.

Usage:
- Call the Attach(CWindow*) method to attach it to a certain window.
- Don't give your controls (except the tab) the AWS_VISIBLE style. Instead, call SetTab(0) after you are done setting up your window/dialog.
- Instead of specifying a control ID yourself, use the Control(int) method. Pass it the tab sheet index to place the control on.
- In your tab change handler, call SetTab(int) with the current selected tab ID. This will update showing/hiding the controls.

Known issues:
- Only one TabSwitcher can be used per window because it has a fixed first ID. If you need more than one tab set, feel free to add an optional parameter to the Attach method specifying which ID to start at.

Feedback, comments, suggestions welcome. Bug reports are not as welcome but still let me know ;)

Bruce Peaslee

Is there something to download?
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Rock Ridge Farm (Larry)


Parker

Oops :-[
I thought I'd posted it already...

class TabSwitcher
{
private:
CPointerList *ctrls;
int ctrlNum;
CWindow *window;

public:
declare TabSwitcher( );
declare _TabSwitcher( );

declare Attach( CWindow *wnd );
declare Control( int tabnum ),int;
declare SetTab( int num );
}

class TestDlg : CDialog
{
declare OnInitDialog( ),int;
declare OnClose( ),int;
declare OnControl( int nid, int nNotifyCode, unsigned int hcontrol ),int;

TabSwitcher *ts;
}

TabSwitcher::TabSwitcher( )
{
ctrlNum = 1024; // a base number unlikely to be chosen
ctrls = new (CPointerList, 1);
ctrls->Create( );
CIntList *il = new (CIntList, 1);
il->Create( );
ctrls->Add( il );
}

TabSwitcher::_TabSwitcher( )
{
void *node = ctrls->GetFirst( );
CIntList *il;
while( node != null )
{
il = ctrls->GetData( node );
delete il;
node = ctrls->GetNext( node );
}
delete ctrls;
}

TabSwitcher::Attach( CWindow *wnd )
{
window = wnd;
}

TabSwitcher::Control( int tabnum )
{
ctrlNum++;
// add this number to the specified control
void *node = ctrls->GetFirst( ), *newnode;
CIntList *il = ctrls->GetData( node );
int i = 0;

for( i = 1; i <= tabnum; i++ )
{
newnode = ctrls->GetNext( node );
if( newnode == null )
{
il = new (CIntList, 1);
il->Create( );
ctrls->Add( il );
node = ctrls->GetNext( node );
}
else
{
node = newnode;
il = ctrls->GetData( node );
}
}
il->Add( ctrlNum );
return ctrlNum;
}

TabSwitcher::SetTab( int num )
{
CIntList *il;
void *node = ctrls->GetFirst( ), *node2;
int i = 0;
CControl *control;

while( node != null )
{
if( i == num )
{
// show all the controls
il = ctrls->GetData( node );
node2 = il->GetFirst( );
while( node2 != null )
{
control = window->GetControl( il->GetData( node2 ) );
if( control )
control->ShowWindow( SWSHOW );
node2 = il->GetNext( node2 );
}
}
else
{
// hide all the controls
il = ctrls->GetData( node );
node2 = il->GetFirst( );
while( node2 != null )
{
control = window->GetControl( il->GetData( node2 ) );
if( control )
control->ShowWindow( SWHIDE );
node2 = il->GetNext( node2 );
}
}

node = ctrls->GetNext( node );
i++;
}
}

//-----

TestDlg::OnClose( )
{
CloseDialog( 1 );
return true;
}

TestDlg::OnControl( int nid, int nNotifyCode, unsigned int hcontrol )
{
CTabCtrl *tab = GetControl( 1 );
if( nid == 1 && nNotifyCode == TCNSELCHANGE )
{
ts->SetTab( tab->GetSelectedTab( ) );
}
}

TestDlg::OnInitDialog( )
{
CTabCtrl *tab = GetControl( 1 );
tab->SetFont( "Arial", 10, 100, 0 );
tab->InsertTab( 0, "Tab 1" );
tab->InsertTab( 1, "Tab 2" );
ts->SetTab( 0 );
}

//-----

global sub main( )
{
TestDlg d;
TabSwitcher ts;
d.Create( 0, 0, 300, 200, AWS_CAPTION | AWS_SYSMENU | AWS_MINIMIZEBOX | AWS_VISIBLE, 0, "Test", 0 );
ts.Attach( d );
d.ts = &ts;
d.AddControl( CTTABCTRL, "", 0, 0, 300, 200, AWS_CHILD | AWS_VISIBLE, 0, 1 );
d.AddControl( CTEDIT, "", 100, 75, 70, 20, AWS_CHILD, 0, ts.Control( 0 ) );
d.AddControl( CTBUTTON, "Button", 100, 100, 70, 20, AWS_CHILD, 0, ts.Control( 0 ) );
d.AddControl( CTEDIT, "", 100, 100, 70, 20, AWS_CHILD, 0, ts.Control( 1 ) );

d.DoModal( );
return 0;
}

John S

thanks Parker,
I'll study this example this weekend.
John Siino, Advanced Engineering Services and Software