Archicad C++ API
About Archicad add-on development using the C++ API.

How to add Check box CheckedIcon and UncheckedIcon in a List Box list items Tab?

Jahidur
Participant
I created a List Box with 2 tabs, In the first tab I insert some text in every list item, and in the second tab, i want to add a check box where i can check and uncheck any list items. I use the SetOnTabItem method to add check box in list items. But the problem is when i select list items the check box gets visible only in that selected list item. But in other list items, the check box gets invisible. That's why i want to add an icon in those unselected list items where an icon will represent is that the list item check box is checked or unchecked. I used the SetTabItemIcon to do this but can't get an acceptable result. Here i share my code files to understand my approach to do this task.

My .hpp File

// *****************************************************************************
// Header file for the AttributeList Example Dialog in DG Test Add-On
// API Development Kit 24; Mac/Win
//
// Namespaces:        Contact person:
//     -None-						LT
//
// [SG compatible] - Yes
// *****************************************************************************

#ifndef TESTDIALOG_H
#define TESTDIALOG_H
#pragma once
#include	"DGModule.hpp"

#define ATTRIBUTE_LIST_DIALOG_RESID			32590

// --- AttributeListDialog -----------------------------------------------------

class TestListDialog : public DG::ModalDialog,
	public DG::PanelObserver,
	public DG::ListBoxObserver,
	public DG::ButtonItemObserver,
	public DG::CheckItemObserver,
	public DG::CompoundItemObserver,
	public DG::PopUpObserver
{
private:
	
	TestListDialog* dialog;
	DG::Button					closeButton;
	DG::Button					hiButton;
	DG::TextEdit				EditBox;
	DG::PopUp					popUpBox;
	DG::CheckBox				checkBox;
	DG::SingleSelListBox		ListBox;
	DG::Button					updateButton;

public:

	void	ButtonClicked(const DG::ButtonClickEvent& ev) override;
	void	PanelResized(const DG::PanelResizeEvent& ev) override;
	void	SetTabData(short dialId, short item);
	virtual void	ListBoxSelectionChanged(const DG::ListBoxSelectionEvent& ev) override;
	void TestInitListBox(short dialId);
	


	TestListDialog();
	~TestListDialog();
};

class Data
{
	// Access specifier
public:

	// Data Members
	GS::UniString name;
	short index;

	// Member Functions()
	void printname()
	{
		//Use it if needed !
	}
};

#endif // ATTRIBUTELISTDIALOG_H

My .cpp File

// *****************************************************************************
// Source code for the AttributeList Example Dialog in DG Test Add-On
// API Development Kit 24; Mac/Win
//
// Namespaces:			Contact person:
//		-None-						LT
//
// [SG compatible] - Yes
// *****************************************************************************

#include	"Test.hpp"

#include	"APIEnvir.h"
#include	"ACAPinc.h"					// also includes APIdefs.h
#include    "APIdefs.h"
//#include	"APICommon.h"
#include "DGModule.hpp"
#include "StringConversion.hpp"
//#include	"UCModule.hpp"
#include <vector>
#include <algorithm>

#include "RSTypes.hpp"
#include "DGListBox.hpp"

//------------------------------------------ Global Variable ------------------------------------

short listSelected = 0;
std::vector<std::pair<short, short>> dataFiles;
//---------------------------- Class AttributeListDialog -----------------------

enum {
	CloseButtonId = 1,
	hiButtonId = 2,
	EditBoxId = 3,
	popUpBoxID = 4,
	checkBoxID = 5,
	ListBoxId = 6,
	UpdateButtonId = 7
};

enum {
	NameTab = 1,
	ControlTab = 2
};


TestListDialog::TestListDialog() :
	DG::ModalDialog(ACAPI_GetOwnResModule(), 32590, ACAPI_GetOwnResModule()),
	closeButton(GetReference(), CloseButtonId),
	hiButton(GetReference(), hiButtonId),
	EditBox(GetReference(), EditBoxId),
	popUpBox(GetReference(), popUpBoxID),
	checkBox(GetReference(), checkBoxID),
	ListBox(GetReference(), ListBoxId),
	updateButton(GetReference(), UpdateButtonId)
{
	hiButton.Hide();
	EditBox.Hide();
	popUpBox.Hide();
	checkBox.Hide();
	Attach(*this);
	AttachToAllItems(*this);
	TestInitListBox(32590);
	/*addPopUpData(32590, popUpBoxID);*/
}


TestListDialog::~TestListDialog()
{
	DetachFromAllItems(*this);
	Detach(*this);
}



void TestListDialog::SetTabData(short dialId, short item)
{
	DGListTabData lTabData[2];
	API_Rect lBox;

	DGGetItemRect(dialId, item, &lBox.left, &lBox.top, &lBox.right, &lBox.bottom);
	lBox.right -= 10;
	short width = (short)(lBox.right - lBox.left);

	lTabData[0].begPos = 0;
	lTabData[0].endPos = (short)(width);
	lTabData[0].justFlag = DG_IS_LEFT;
	lTabData[0].truncFlag = DG_IS_TRUNCEND;
	lTabData[0].hasSeparator = false;
	lTabData[0].disabled = false;

	lTabData[1].begPos = lTabData[0].endPos;;
	lTabData[1].endPos = (short)(lBox.right);//250;
	lTabData[1].justFlag = DG_IS_LEFT;
	lTabData[1].truncFlag = DG_IS_TRUNCEND;
	lTabData[1].hasSeparator = false;
	lTabData[1].disabled = false;

	DGListSetTabData(dialId, item, 2, &lTabData[0]);
}



void TestListDialog::TestInitListBox(short dialId)
{
	
	//------------------------------------------------------------------------------------( New Try )-----------------------------------------------------

	//SetTabData(dialId, ListBoxId);
	ListBox.SetHeaderSynchronState(false);
	ListBox.SetHeaderItemCount(2);

	ListBox.SetHeaderItemSizeableFlag(NameTab, true);
	ListBox.SetHeaderItemSizeableFlag(ControlTab, true);

	//setting the text of the tab item
	ListBox.SetHeaderItemText(NameTab, "Name");
	ListBox.SetHeaderItemText(ControlTab, "Template");

	//setting the header size of the tab
	ListBox.SetHeaderItemSize(NameTab, 200);
	ListBox.SetHeaderItemSize(ControlTab, 300);

	ListBox.SetHeaderItemSizeableFlag(NameTab, true);
	ListBox.SetHeaderItemSizeableFlag(ControlTab, true);

	ListBox.SetHeaderItemStyle(NameTab, DG::ListBox::DefaultJust, DG::ListBox::EndTruncate);
	ListBox.SetHeaderItemStyle(ControlTab, DG::ListBox::DefaultJust, DG::ListBox::EndTruncate);


	ListBox.SetTabFieldCount(2);

	//setting properties of the tab
	short k = ListBox.GetItemWidth();
	short pos = 0;
	ListBox.SetTabFieldProperties(NameTab, pos, pos + 200, DG::ListBox::Left, DG::ListBox::NoTruncate, false);
	pos += 200;
	ListBox.SetTabFieldProperties(ControlTab, pos, pos + 200, DG::ListBox::Left, DG::ListBox::NoTruncate, false);


	//Create List Box items
	for (short i = 1; i <= 4; i++)
	{
		GS::UniString listItemName = "Test - "+ GS::ValueToUniString(i);
		GS::UniString listItemName2 = "Semple Text No - "+ GS::ValueToUniString(i);
		ListBox.AppendItem();
		ListBox.SetTabItemText(i, NameTab, listItemName);
		ListBox.SetTabItemText(i, ControlTab, listItemName2);
	}

	// -------------------------------------------------- Set the UserData in list items -----------------------------------------------------------------
	//DGListSetItemUserData(short  dialId, short  item, short  listItem, DGUserData   value);

	for (short i = 1; i <= 4; i++)
	{
		Data* obj = new Data();
		obj->index = i;
		obj->name = "This is the list item number - " + GS::ValueToUniString(i);
		DGListSetItemUserData(dialId, ListBoxId, i, reinterpret_cast<DGUserData>(obj));
	}
	
	/*short appendedIndex = DGAppendDialogItem(dialId, DG_ITM_CHECKBOX, DG_BT_TEXT, 0, 10, 60, 80, 18);
	DG::Button btn(dialId, appendedIndex);*/
}



void TestListDialog::PanelResized(const DG::PanelResizeEvent& ev)
{
	short hGrow = ev.GetHorizontalChange();
	short vGrow = ev.GetVerticalChange();
	if (hGrow != 0 || vGrow != 0) {
		BeginMoveResizeItems();
		closeButton.MoveAndResize(0, vGrow, hGrow, 0);
		updateButton.MoveAndResize(0, vGrow, hGrow, 0);
		EndMoveResizeItems();
	}

}

void TestListDialog::ListBoxSelectionChanged(const DG::ListBoxSelectionEvent& ev)
{
	
	if (ev.GetSource() == &ListBox)
	{
		short listItemIndex = ListBox.GetSelectedItem();

		//Get the UserData from list items
		//DGListGetItemUserData(short  dialId, short  item, short  listItem);
		short data = DGListGetItemUserData(32590, ListBoxId, 1);
		Data* selectedData = reinterpret_cast<Data*> (DGListGetItemUserData(32590, ListBoxId, listItemIndex));
		GS::UniString dataname = selectedData->name;
		short selectedIndex = selectedData->index;

		if (listItemIndex <= ListBox.GetItemCount() && listItemIndex > 0) {
			if (listItemIndex == 1)
			{
				ListBox.SetOnTabItem(ControlTab, checkBox); //Check Box

				bool ischeck = checkBox.IsChecked();
				const DG::Icon & icon = DG::Icon(ACAPI_GetOwnResModule(), DG::ListBox::SpecialIcons::UncheckedIcon);
				ListBox.SetTabItemIcon(listItemIndex, ControlTab, icon);

			}
			else if (listItemIndex == 2)
			{
				ListBox.SetOnTabItem(ControlTab, checkBox); //Check Box

				bool ischeck = checkBox.IsChecked();
				const DG::Icon & icon = DG::Icon(ACAPI_GetOwnResModule(), DG::ListBox::SpecialIcons::UncheckedIcon);
				ListBox.SetTabItemIcon(listItemIndex, ControlTab, icon);
			}
			else if (listItemIndex == 3)
			{
				ListBox.SetOnTabItem(ControlTab, checkBox); //Check Box

				bool ischeck = checkBox.IsChecked();
				const DG::Icon & icon = DG::Icon(ACAPI_GetOwnResModule(), DG::ListBox::SpecialIcons::UncheckedIcon);
				ListBox.SetTabItemIcon(listItemIndex, ControlTab, icon);
			}
			else if (listItemIndex == 4)
			{
				//Do--------------------------------------------------------------------------------------
				//Do--------------------------------------------------------------------------------------
				ListBox.SetOnTabItem(ControlTab, checkBox); //Check Box
				
				bool ischeck = checkBox.IsChecked();
				const DG::Icon & icon = DG::Icon(ACAPI_GetOwnResModule(), DG::ListBox::SpecialIcons::UncheckedIcon);
				ListBox.SetTabItemIcon(listItemIndex, ControlTab, icon);


				listSelected = listItemIndex;
			}


		}
		//dialog->ListBox.SetOnTabItem(ControlTab, dialog->EditBox);
	}
	//dialog->ListBox.SetOnTabItem(ControlTab, dialog->EditBox);
}


void	TestListDialog::ButtonClicked(const DG::ButtonClickEvent& ev)
{
	if (ev.GetSource() == &closeButton) {
		dataFiles.clear();
		PostCloseRequest(Accept);
	}
	else if (ev.GetSource() == &hiButton)
	{
		PostCloseRequest(Accept);
	}
	else if (ev.GetSource() == &updateButton)
	{
		//Do
		short k = dataFiles.size();
		short a = 0;
	}

}
And the .grc Part of this task

/* ---------------------------------------------------------- Demo Dialog */ //-------------------------------------------------------------------------------------------------------

'GDLG'  32590  Modal | grow         0    0  610  350  "Demo Example Dialog" {
/* [  1] */ Button				  400  280  70   24	   LargePlain  "Close"
/* [  2] */ Button				    0    0  100   20	   LargePlain  "Hi!"
/* [  3] */ TextEdit		        0    0  150  20	   LargePlain  40
/* [  4] */ PopupControl	        0    0  150  20	80  3
/* [  5] */ CheckBox				0    0  150  18		SmallPlain "Check Box"
/* [  6] */ SingleSelList		   30   30  500  230	LargePlain  PartialItems  23 HasHeader 25
/* [  7] */ Button				  300  280  70   24	   LargePlain  "Update"
}

'DLGH'  32590  DLG_32590_AttributeList_Example_Dialog {
1	""							Button_0
2	""							Button_1
3	""							TExt_0
4	""							PopupControl_0
5	""							CheckBox_0
6	""							SingleSelList_0
7	""							Button_2
}
I also share my current status view of this window picture to.
2 REPLIES 2
Oleg
Expert
Use SetTabItemIcon to draw a checkbox in a tab:

list.SetTabItemIcon( item, tabindex, DG::Icon( SysResModule, on ? DG::ListBox::CheckedIcon : DG::ListBox::UncheckedIcon) ;

Handle the ListBoxClicked event:

void TestListDialog::ListBoxClicked( const DG::ListBoxClickEvent& ev )
{
	if ( ev.GetSource() == &mi_list )
	{
		short pos = ev.GetMouseOffset().GetX();
		short beg = mi_list.GetTabFieldBeginPosition( m_table.check );
		short end = mi_list.GetTabFieldEndPosition( m_table.check );
		if (  pos > end || pos < beg )
			return;

		OnListBoxCheckClicked( ev.GetListItem() );
	}
}
Jahidur
Participant
Thank you !

Didn't find the answer?

Check other topics in this Forum

Back to Forum

Read the latest accepted solutions!

Accepted Solutions

Start a new conversation!