Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » TabCtrl » How to dynamically add remove tabs
How to dynamically add remove tabs [message #60184] Mon, 25 September 2023 20:20 Go to next message
nicesai is currently offline  nicesai
Messages: 2
Registered: September 2023
Junior Member
I am doing something like this:

    void addTab() {
	WithTabLay<ParentCtrl> *tabLay  = new WithTabLay<ParentCtrl>();
        CtrlLayout(*tabLay);
        tabLay->HSizePosZ(0, 0).VSizePosZ(0, 0);
        tabs.Add(*tabLay, MyImgs::add20(), "Tab1");
        
        tabLay->closeBut.WhenPush = [=] { 
        	tabs.Remove(*tabLay); 
        	tabLay->~WithTabLay();
        	delete tabLay;
        };
    }


As you can guess,
WithTabLay is the tab layout created in the UI editor.
closeBut is a button within the tab that I want to use to remove the tab.

Question-1: Is it necessary to call the destructor after removing the tab?
tabLay->~WithTabLay();

Question-2: Is it necessary to delete the allocated memory? This line causes the applicatton to crash. So probably I should not, if so, how can I free the allocated memory?
delete tabLay;

Questions-3: Is there any better way to do this without using pointers? If I create the Tab on stack, its free'ed at the end of the function (from RAII I suppose). And the Tab will not be visible.

Re: How to dynamically add remove tabs [message #60185 is a reply to message #60184] Wed, 27 September 2023 00:46 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1094
Registered: August 2007
Senior Contributor
Hello nicesai,


Quote:
Question-1: Is it necessary to call the destructor after removing the tab?
tabLay->~WithTabLay();


No, not unless you want to manually destroy the pane object (tablay).


Quote:
Question-2: Is it necessary to delete the allocated memory? This line causes the applicatton to crash. So probably I should not, if so, how can I free the allocated memory?
delete tabLay;


Well, if you manually allocate the widget on the heap (something we usually avoid in U++, it is not a good practice), yes you need to delete the object when you're done with it (or it will leak)


Quote:
Questions-3: Is there any better way to do this without using pointers? If I create the Tab on stack, its free'ed at the end of the function (from RAII I suppose). And the Tab will not be visible.


In general, if we want to create multiple (a lot of, or indeterminate number of) tabs, we'd usually use containers (e.g. could be Array<WithTabLay<ParentCtrl>>, in this case). Containers take care of mem alloc/dealloc. THAT is the way to go.

An example (just to give you an idea. Not the only way to do it)

#include <CtrlLib/CtrlLib.h>

using namespace Upp;

struct MyApp : TopWindow {
	TabCtrl tabs;
	Array<Button> panes;
	Button bt;
	MyApp()
	{
		Title("Tabs test");
		Sizeable().Zoomable().CenterScreen().SetRect(0, 0, 800, 600);
		Add(tabs.HSizePosZ().VSizePosZ(0, 28));
		Add(bt.SetLabel("Add New Tab").HSizePosZ().BottomPosZ(2, 24));

		auto AddPane = [this] {
			int n = tabs.GetCount();
			Button& b = panes.Add();
			b.SetLabel("Pane " + AsString(n) + " (click to delete)");
			b << [this] { // DelPane
				panes.RemoveIf([this](int i) { return &panes[i] == tabs.GetItem(~tabs).GetSlave(); });
				tabs.Remove(~tabs);
			};
			tabs.Add(b.SizePos(), "Tab " + AsString(n));
		};

		bt << [&] { AddPane(); };
		
		for(int i = 0; i < 5; i++)
			AddPane();
		
	}
};

GUI_APP_MAIN
{
	MyApp().Run();
}




Best regards,
Oblivion


[Updated on: Wed, 27 September 2023 00:51]

Report message to a moderator

Re: How to dynamically add remove tabs [message #60187 is a reply to message #60185] Fri, 29 September 2023 21:36 Go to previous messageGo to next message
nicesai is currently offline  nicesai
Messages: 2
Registered: September 2023
Junior Member
Thank you ─░smail/Oblivion, I did the reference tracking in Array as you suggested, and it works well.

The following part was non intuitive, probably couldn't have guessed at first glance looking at the APIs, but glad I asked and thanks for showing it.
panes.RemoveIf([this](int i) { return &panes[i] == tabs.GetItem(~tabs).GetSlave();

Re: How to dynamically add remove tabs [message #60188 is a reply to message #60187] Sat, 30 September 2023 17:14 Go to previous message
Oblivion is currently offline  Oblivion
Messages: 1094
Registered: August 2007
Senior Contributor
By the way,

The TabCtrl is a bit rudimentary widget. If you need a fully-fledged tab managing, I suggest you try TabBar instaed. It has a ton of features and specialized versions of it (It is basically the tab widget used in TheIDE, editor).

See the example: https://www.ultimatepp.org/reference$FileTabsExample$en-us.h tml

The example demonstrates the capabilities of TabBar package.


Previous Topic: TabCtrl CancelClose No longer work (as intended)
Next Topic: tabctrl contents not visible
Goto Forum:
  


Current Time: Sat May 25 07:24:26 CEST 2024

Total time taken to generate the page: 0.01533 seconds