Cyclic Toolbar
Cyclic Toolbar
Abstract
This article shows how to create an arbitrary number of toolbars using
a single TPanel on which all the controls sit.
Each toolbar is displayed on the same area of the screen so that the
rest of the screen area is free for the application to use.
The user can switch toolbars through two buttons placed on the toolbar itself.
A previous button and a next button.
Each toolbar has a set of different controls, such as buttons, edit boxes,
combo boxes, progress bars, etc.
Main features
The main features of the cyclic toolbar are:
1) All the controls on the toolbar can have either variable width and height
or uniform width and height.
2) The toolbar can contain buttons, edit boxes, combo boxes, progress bars, etc.
3) Each toolbar is displayed on the same area of the screen so that the
user has a larger working area available for the application
4) The toolbar can have tridimensional effects and color.
5) Spaces and dividers can be placed on the toolbar.
Thus, the programmer can access virtually an unlimited number of controls
organized on different toolbars. The program described in this article
creates 3 toolbars and their controls.
The TToolbarCy class (Cyclic Toolbar) encapsulates the data and the methods of the toolbar.
This class has a private static data member pri_toolbar which
is used to identify the current toolbar, and it has five member
functions.
Member Functions Description
Get_CurrentToolBar() This function returns the current toolbar id.
Get_NextToolBar() This function returns the next toolbar id.
Get_PrevToolBar() This function returns the previous toolbar id.
Show_ToolBar() This function makes all the controls on the toolbar both
visible and enabled.
Hide_ToolBar() This function makes all the controls on the toolbar both
invisible and disabled.
This program uses unnamed objects. An unnamed object is created when we
invoke one of the TToolBarCy class methods through the constuctor of that class. For example:
TToolBarCy().Show_ToolBar( TToolBarCy().Get_CurrentToolBar() );
will create an unnamed object and it will execute its
Show_ToolBar()
method.
Notice that the traditional (explicit) object creation line
TMyToolBar oToolBar;
is not used in this program.
Program Files
CyclicToolbar.cpp
#include <vcl.h>
#pragma hdrstop
#include "CyclicToolBar.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TToolBarCy oToolBar;
// SECOND DECLARATION AND INITIALIZATION OF THE
// PRIVATE DATA MEMBER pri_toolbar
int TToolBarCy::pri_toolbar = TOOLBAR1;
const int DIM = 16;
TPanel* Panel1;
TSpeedButton* pPrevToolBar;
TSpeedButton* pNextToolBar;
//Controls Tool Bar 1
TButton* pB1;
TButton* pB2;
TButton* pB3;
TBitBtn* pBit1;
TBitBtn* pBit2;
TBitBtn* pBit3;
TSpeedButton* pSpeed[DIM];
//Controls Tool Bar 2
TEdit* pEdit1;
TComboBox* pCombo1;
//Controls Tool Bar 3
TEdit* pEdit2;
TEdit* pEdit3;
TComboBox* pCombo2;
TProgressBar* pProg1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Width = 800;
Height = 600;
Position = poScreenCenter;
Color = (TColor)0xa11bca;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
Panel1 = new TPanel( this );
Panel1->Parent = this;
Panel1->Width = ClientWidth;
Panel1->Height = 27;
Panel1->Top = 1;
Panel1->Color = clPurple;
Panel1->Enabled = true;
Panel1->Visible = true;
Create_ToolBar1( Panel1 );
Create_ToolBar2( Panel1 );
Create_ToolBar3( Panel1 );
oToolBar.Show_ToolBar( oToolBar.Get_CurrentToolBar() );
}
//------------------------------------------------------------------------
TToolBarCy::TToolBarCy()
{
//Default Constructor
pri_toolbar = TOOLBAR1;
}
//------------------------------------------------------------------------
void TToolBarCy::Hide_ToolBar( TPanel* p )
{
//Hide and Disable controls
if ( !p->ControlCount )
{
ShowMessage("Cannot remove controls because all controls have already been removed");
}
for( int i=0; i < p->ControlCount; i++ )
{
TGraphicControl* pGControl =
dynamic_cast ( p->Controls[i] );
TWinControl* pControl =
dynamic_cast ( p->Controls[i] );
if ( pGControl )
{
pGControl->Visible = false;
pGControl->Enabled = false;
}
if ( pControl )
{
pControl->Visible = false;
pControl->Enabled = false;
}
}
}
//-----------------------------------------------------------------------
void TToolBarCy::Show_ToolBar ( int toolbar )
{
//Show Toolbar: Enable and make controls visible
switch( toolbar )
{
case TOOLBAR1:
//Normal Button
pB1->Enabled = true;
pB1->Visible = true;
pB2->Enabled = true;
pB2->Visible = true;
pB3->Enabled = true;
pB3->Visible = true;
//Bit
pBit1->Visible = true;
pBit1->Enabled = true;
pBit2->Visible = true;
pBit2->Enabled = true;
pBit3->Visible = true;
pBit3->Enabled = true;
//Speed
for (int i=0; i<=9; i++)
{
pSpeed[i]->Visible = true;
pSpeed[i]->Enabled = true;
}
//Previous Toolbar
pPrevToolBar->Visible = true;
pPrevToolBar->Enabled = true;
pPrevToolBar->Caption = "<";
pPrevToolBar->OnClick = Form1->SB_PrevToolBarClick;
//Next Toolbar
pNextToolBar->Visible = true;
pNextToolBar->Enabled = true;
pNextToolBar->Caption = ">";
pNextToolBar->OnClick = Form1->SB_NextToolBarClick;
break;
case TOOLBAR2:
//Speed
for ( int i=10; i< DIM; i++ )
{
pSpeed[i]->Visible = true;
pSpeed[i]->Enabled = true;
}
pEdit1->Visible = true;
pEdit1->Enabled = true;
pCombo1->Visible = true;
pCombo1->Enabled = true;
//Previous Toolbar
pPrevToolBar->Visible = true;
pPrevToolBar->Enabled = true;
pPrevToolBar->Caption = "<";
pPrevToolBar->OnClick = Form1->SB_PrevToolBarClick;
//Next Toolbar
pNextToolBar->Visible = true;
pNextToolBar->Enabled = true;
pNextToolBar->Caption = ">";
pNextToolBar->OnClick = Form1->SB_NextToolBarClick;
break;
case TOOLBAR3:
//
pEdit2->Visible = true;
pEdit2->Enabled = true;
pEdit3->Visible = true;
pEdit3->Enabled = true;
pCombo2->Visible = true;
pCombo2->Enabled = true;
pProg1->Visible = true;
pProg1->Enabled = true;
//Previous Toolbar
pPrevToolBar->Visible = true;
pPrevToolBar->Enabled = true;
pPrevToolBar->Caption = "<";
pPrevToolBar->OnClick = Form1->SB_PrevToolBarClick;
//
pNextToolBar->Visible = true;
pNextToolBar->Enabled = true;
pNextToolBar->Caption = ">";
pNextToolBar->OnClick = Form1->SB_NextToolBarClick;
break;
default: ShowMessage("Illegal Tool Bar index");
}
}
//-----------------------------------------------------------------------
void __fastcall TForm1::Create_ToolBar1( TPanel* p )
{
//Create Controls on Tool Bar 1
pB1 = new TButton( p );
pB1->Parent = p;
pB1->Top = 1;
pB1->Left = 0;
pB1->Width = 100;
pB1->Height = 25;
pB1->Enabled = false;
pB1->Visible = false;
pB1->Caption = "Button 1";
pB1->OnClick = Button1Click;
pB2 = new TButton( p );
pB2->Parent = p;
pB2->Top = 1;
pB2->Left = pB1->Left + pB1->Width;
pB2->Width = 100;
pB2->Height = 25;
pB2->Enabled = false;
pB2->Visible = false;
pB2->Caption = "Button 2";
pB2->OnClick = Button2Click;
pB3 = new TButton( p );
pB3->Parent = p;
pB3->Top = 1;
pB3->Left = pB2->Left + pB2->Width;
pB3->Width = 100;
pB3->Height = 25;
pB3->Enabled = false;
pB3->Visible = false;
pB3->Caption = "Button 3";
pB3->OnClick = Button3Click;
pBit1 = new TBitBtn( p );
pBit1->Parent = p;
pBit1->Width = 45;
pBit1->Left = pB3->Left + pB3->Width;
pBit1->Height = 25;
pBit1->Top = 1;
pBit1->Visible = false;
pBit1->Enabled = false;
pBit1->Caption = "3";
pBit1->OnClick = BitBtn1Click;
pBit2 = new TBitBtn( p );
pBit2->Parent = p;
pBit2->Left = pBit1->Left + pBit1->Width;
pBit2->Width = 45;
pBit2->Height = 25;
pBit2->Top = 1;
pBit2->Visible = false;
pBit2->Enabled = false;
pBit2->Caption = "4";
pBit2->OnClick = BitBtn2Click;
pBit3 = new TBitBtn( p );
pBit3->Parent = p;
pBit3->Left = pBit2->Left + pBit2->Width;
pBit3->Width = 45;
pBit3->Height = 25;
pBit3->Top = 1;
pBit3->Visible = false;
pBit3->Enabled = false;
pBit3->Caption = "5";
pBit3->OnClick = BitBtn3Click;
for (int i = 0; i<= 9; i++)
{
pSpeed[i] = new TSpeedButton( p );
pSpeed[i]->Parent = p;
pSpeed[i]->Visible = false;
pSpeed[i]->Enabled = false;
pSpeed[i]->Width = 30;
pSpeed[i]->Height = 25;
pSpeed[i]->Top = 1;
pSpeed[i]->Caption = "S" + String( i+1 );
}
pSpeed[0]->Left = pBit3->Left + pBit3->Width;
pSpeed[1]->Left = pSpeed[0]->Left + pSpeed[0]->Width;
pSpeed[2]->Left = pSpeed[1]->Left + pSpeed[1]->Width;
pSpeed[3]->Left = pSpeed[2]->Left + pSpeed[2]->Width;
pSpeed[4]->Left = pSpeed[3]->Left + pSpeed[3]->Width;
pSpeed[5]->Left = pSpeed[4]->Left + pSpeed[4]->Width;
pSpeed[6]->Left = pSpeed[5]->Left + pSpeed[5]->Width;
pSpeed[7]->Left = pSpeed[6]->Left + pSpeed[6]->Width;
pSpeed[8]->Left = pSpeed[7]->Left + pSpeed[7]->Width;
pSpeed[9]->Left = pSpeed[8]->Left + pSpeed[8]->Width;
//Events Handlers
pSpeed[0]->OnClick = SpeedButton1Click;
pSpeed[1]->OnClick = SpeedButton2Click;
pSpeed[2]->OnClick = SpeedButton3Click;
pSpeed[3]->OnClick = SpeedButton4Click;
pSpeed[4]->OnClick = SpeedButton5Click;
pSpeed[5]->OnClick = SpeedButton6Click;
pSpeed[6]->OnClick = SpeedButton7Click;
pSpeed[7]->OnClick = SpeedButton8Click;
pSpeed[8]->OnClick = SpeedButton9Click;
pSpeed[9]->OnClick = SpeedButton10Click;
pNextToolBar = new TSpeedButton( p );
pNextToolBar->Parent = p;
pNextToolBar->Width = 25;
pNextToolBar->Left = p->Width - pNextToolBar->Width;
pNextToolBar->Height = 25;
pNextToolBar->Top = 1;
pNextToolBar->Visible = false;
pNextToolBar->Enabled = false;
pNextToolBar->Font->Size = 7;
pNextToolBar->Caption = "1 >";
pNextToolBar->OnClick = SB_NextToolBarClick;
pPrevToolBar = new TSpeedButton( p );
pPrevToolBar->Parent = p;
pPrevToolBar->Width = 25;
pPrevToolBar->Left = pNextToolBar->Left - pPrevToolBar->Width;
pPrevToolBar->Height = 25;
pPrevToolBar->Top = 1;
pPrevToolBar->Visible = false;
pPrevToolBar->Enabled = false;
pPrevToolBar->Font->Size = 7;
pPrevToolBar->Caption = "< 3";
pPrevToolBar->OnClick = SB_PrevToolBarClick;
}
//------------------------------------------------------------------------
void __fastcall TForm1::Create_ToolBar2( TPanel* p )
{
//Create Controls on Tool Bar 2
for ( int i = 10; i< DIM; i++ )
{
pSpeed[i] = new TSpeedButton( p );
pSpeed[i]->Parent = p;
pSpeed[i]->Width = 50;
pSpeed[i]->Left += (pSpeed[10]->Width)*(i-10);
pSpeed[i]->Height = 25;
pSpeed[i]->Top = 1;
pSpeed[i]->Visible = false;
pSpeed[i]->Enabled = false;
pSpeed[i]->Caption = "S" + String( i+1 );
}
pSpeed[10]->OnClick = SpeedButton11Click;
pSpeed[11]->OnClick = SpeedButton12Click;
pSpeed[12]->OnClick = SpeedButton13Click;
pSpeed[13]->OnClick = SpeedButton14Click;
pSpeed[14]->OnClick = SpeedButton15Click;
pSpeed[15]->OnClick = SpeedButton16Click;
pEdit1 = new TEdit( p );
pEdit1->Parent = p;
pEdit1->Left = pSpeed[DIM-1]->Left + pSpeed[DIM-1]->Width;
pEdit1->Width = 300;
pEdit1->Height = 25;
pEdit1->Top = 1;
pEdit1->Text = "Edit 1";
pEdit1->Visible = false;
pEdit1->Enabled = false;
pCombo1 = new TComboBox( p );
pCombo1->Parent = p;
pCombo1->Left = pEdit1->Left + pEdit1->Width;
pCombo1->Width = 135;
pCombo1->Height = 25;
pCombo1->Top = 1;
pCombo1->Text = "Combo 1";
pCombo1->Items->Add("1");
pCombo1->Items->Add("2");
pCombo1->Items->Add("3");
pCombo1->Visible = false;
pCombo1->Enabled = false;
pNextToolBar->Caption = "Tool Bar 2";
pNextToolBar->OnClick = SB_NextToolBarClick;
}
//------------------------------------------------------------------------
void __fastcall TForm1::Create_ToolBar3( TPanel* p )
{
//Create Controls on Tool Bar 2
pEdit2 = new TEdit( p );
pEdit2->Parent = p;
pEdit2->Left = 0;
pEdit2->Width = 200;
pEdit2->Height = 25;
pEdit2->Top = 1;
pEdit2->Text = "Edit 2";
pEdit2->Visible = false;
pEdit2->Enabled = false;
pEdit3= new TEdit( p );
pEdit3->Parent = p;
pEdit3->Left = pEdit2->Left + pEdit2->Width;
pEdit3->Width = 200;
pEdit3->Height = 25;
pEdit3->Top = 1;
pEdit3->Text = "Edit 3";
pEdit3->Visible = false;
pEdit3->Enabled = false;
pCombo2 = new TComboBox( p );
pCombo2->Parent = p;
pCombo2->Left = pEdit3->Left + pEdit3->Width;
pCombo2->Width = 100;
pCombo2->Height = 25;
pCombo2->Top = 1;
pCombo2->Text = "Combo";
pCombo2->Items->Add("text 1");
pCombo2->Items->Add("text 2");
pCombo2->Items->Add("text 3");
pCombo2->Items->Add("text 4");
pCombo2->Items->Add("text 5");
pCombo2->Items->Add("text 6");
pCombo2->Visible = false;
pCombo2->Enabled = false;
pProg1 = new TProgressBar( p );
pProg1->Parent = p;
pProg1->Left = 5 + pCombo2->Left + pCombo2->Width;
pProg1->Width = 200;
pProg1->Height = 10;
pProg1->Top = 7;
pProg1->Visible = false;
pProg1->Enabled = false;
pNextToolBar->Caption = "Tool Bar 3";
pNextToolBar->OnClick = SB_NextToolBarClick;
}
//-----------------------------------------------------------------------
void __fastcall TForm1::SB_NextToolBarClick( TObject *Sender )
{
//NEXT TOOLBAR
oToolBar.Hide_ToolBar( Panel1 );
oToolBar.Show_ToolBar ( oToolBar.Get_NextToolBar() );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SB_PrevToolBarClick( TObject *Sender )
{
//PREVIOUS TOOLBAR
oToolBar.Hide_ToolBar( Panel1 );
oToolBar.Show_ToolBar ( oToolBar.Get_PrevToolBar() );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
ShowMessage("SpeedButton1Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
ShowMessage("SpeedButton2Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{
ShowMessage("SpeedButton3Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton4Click(TObject *Sender)
{
ShowMessage("SpeedButton4Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton5Click(TObject *Sender)
{
ShowMessage("SpeedButton5Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton6Click(TObject *Sender)
{
ShowMessage("SpeedButton6Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton7Click(TObject *Sender)
{
ShowMessage("SpeedButton7Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton8Click(TObject *Sender)
{
ShowMessage("SpeedButton8Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton9Click(TObject *Sender)
{
ShowMessage("SpeedButton9Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton10Click(TObject *Sender)
{
ShowMessage("SpeedButton10Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton11Click(TObject *Sender)
{
ShowMessage("SpeedButton11Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton12Click(TObject *Sender)
{
ShowMessage("SpeedButton12Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton13Click(TObject *Sender)
{
ShowMessage("SpeedButton13Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton14Click(TObject *Sender)
{
ShowMessage("SpeedButton14Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton15Click(TObject *Sender)
{
ShowMessage("SpeedButton15Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton16Click(TObject *Sender)
{
ShowMessage("SpeedButton16Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessage("Button1Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ShowMessage("Button2Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
ShowMessage("Button3Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
ShowMessage("BitBtn1Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
ShowMessage("BitBtn2Click()");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
ShowMessage("BitBtn3Click()");
}
CyclicToolBar.h
#ifndef CyclicToolBarH
#define CyclicToolBarH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ToolWin.hpp>
#include <Buttons.hpp>
//---------------------------------------------------------------------------
enum
{
TOOLBAR1 = 1,
TOOLBAR2,
TOOLBAR3,
TOTAL_TOOLBARS = TOOLBAR3
};
class TToolBarCy
{
private:
static int pri_toolbar;
public:
//DEFAULT CONSTRUCTOR
TToolBarCy();
static int Get_CurrentToolBar()
{
return pri_toolbar;
}
static int Get_NextToolBar()
{
if ( pri_toolbar == TOTAL_TOOLBARS )
{
pri_toolbar = TOOLBAR1;
}
else
{
pri_toolbar++;
}
return pri_toolbar;
}
static int Get_PrevToolBar()
{
if ( pri_toolbar == TOOLBAR1 )
{
pri_toolbar = TOTAL_TOOLBARS;
}
else
{
pri_toolbar--;
}
return pri_toolbar;
}
void Show_ToolBar ( int pri_toolbar );
void Hide_ToolBar( TPanel* p );
};
class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormShow(TObject *Sender);
void __fastcall SpeedButton1Click(TObject *Sender);
void __fastcall SpeedButton2Click(TObject *Sender);
void __fastcall SpeedButton3Click(TObject *Sender);
void __fastcall SpeedButton4Click(TObject *Sender);
void __fastcall SpeedButton5Click(TObject *Sender);
void __fastcall SpeedButton6Click(TObject *Sender);
void __fastcall SpeedButton7Click(TObject *Sender);
void __fastcall SpeedButton8Click(TObject *Sender);
void __fastcall SpeedButton9Click(TObject *Sender);
void __fastcall SpeedButton10Click(TObject *Sender);
void __fastcall SpeedButton11Click(TObject *Sender);
void __fastcall SpeedButton12Click(TObject *Sender);
void __fastcall SpeedButton13Click(TObject *Sender);
void __fastcall SpeedButton14Click(TObject *Sender);
void __fastcall SpeedButton15Click(TObject *Sender);
void __fastcall SpeedButton16Click(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall BitBtn1Click(TObject *Sender);
void __fastcall BitBtn2Click(TObject *Sender);
void __fastcall BitBtn3Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall Create_ToolBar1( TPanel* p );
void __fastcall Create_ToolBar2( TPanel* p );
void __fastcall Create_ToolBar3( TPanel* p );
void __fastcall SB_PrevToolBarClick( TObject *Sender );
void __fastcall SB_NextToolBarClick( TObject *Sender );
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Conclusion
The method presented in this article suits applications that require both a large
number of buttons on the toolbar and a large available area of the screen
to display other things, such as, photographs, text, diagrams, etc.
Both large and small applications are candidates for using this type of toolbar.
Homepage
Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.