|
|
Home » Community » Newbie corner » Insert Button into GridCtrl Cell (Can you insert buttons (or any widgets) into gridCtrl?)
|
Re: Insert Button into GridCtrl Cell [message #59700 is a reply to message #59699] |
Thu, 09 March 2023 21:09   |
Oblivion
Messages: 1204 Registered: August 2007
|
Senior Contributor |
|
|
Hi,
For ArrayCtrl, you can add buttons and other ctrls. In fact, you can add different ctrls for each row of a given column. All you need to do is use the factory methods and setup a callback:
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
struct MyApp : TopWindow {
ArrayCtrl list;
MyApp()
{
Sizeable().Zoomable().CenterScreen().SetRect(0, 0, 1024, 800);
Add(list.SizePos());
auto sButtonFactory = [this] (int i, One<Ctrl>& c)
{
String s = list.Get(i, 0);
c.Create<Button>()
.SetLabel(s)
.WhenAction = [s] { PromptOK("The value is " + s); };
};
list.AddColumn("texts");
list.AddColumn("buttons").Ctrls(sButtonFactory);
for(int i = 0; i < 100; i++)
list.Add(AsString(100 - i), AsString(i));
}
};
GUI_APP_MAIN
{
MyApp().Run();
}
Same thing can be done with GridCtrl...
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Thu, 09 March 2023 21:39] Report message to a moderator
|
|
|
|
Re: Insert Button into GridCtrl Cell [message #59702 is a reply to message #59701] |
Thu, 09 March 2023 22:08   |
jkastran
Messages: 10 Registered: September 2021 Location: Michigan, US
|
Promising Member |
|
|
Hi, I tried this already but because of the errors and that it was in the ArrayCtrlTest File even though the demo is for GridCtrl, I thought that it is not for GridCtrl. Here is the code and the error I am getting
EDIT:
I moved the factory before where I call the creation of the column and it works.
Now the problem is there is no explanation on how to change label or add functionality to individual buttons with the "MakeButton" method
-
Attachment: GridErr.PNG
(Size: 502.21KB, Downloaded 103 times)
[Updated on: Thu, 09 March 2023 22:30] Report message to a moderator
|
|
|
Re: Insert Button into GridCtrl Cell [message #59703 is a reply to message #59702] |
Thu, 09 March 2023 23:40   |
Oblivion
Messages: 1204 Registered: August 2007
|
Senior Contributor |
|
|
Here's the thing: GridCtrl still uses pre-C++11 callbacks. (Deprecated API)
It should be updated to utilize the new Upp::Function variants and lambda functions with capture...
Good news is that you can achieve -mostly- the same results with the old api calls:
#include <CtrlLib/CtrlLib.h>
#include <GridCtrl/GridCtrl.h>
using namespace Upp;
void ButtonFactory1(One<Ctrl>& c, GridCtrl* list)
{
static int i = 0;
String s = list->Get(i++, 0);
c.Create<Button>()
.SetLabel(s)
.WhenAction = [s] { PromptOK("The value is " + s); };
}
struct MyApp : TopWindow {
GridCtrl list;
typedef MyApp CLASSNAME;
MyApp()
{
Sizeable().Zoomable().CenterScreen().SetRect(0, 0, 1024, 800);
Add(list.SizePos());
list.AddColumn("texts");
list.AddColumn("buttons_1").Ctrls(callback1(ButtonFactory1, &list));
list.AddColumn("buttons_2").Ctrls(THISBACK(ButtonFactory2));
for(int i = 0; i < 100; i++)
list.Add(AsString(i), AsString(i));
}
void ButtonFactory2(One<Ctrl>& c)
{
static int i = 0;
String s = list.Get(i++, 0);
c.Create<Button>()
.SetLabel(s)
.WhenAction = [s] { PromptOK("The value is " + s); };
}
};
GUI_APP_MAIN
{
MyApp().Run();
}
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Thu, 09 March 2023 23:42] Report message to a moderator
|
|
|
Re: Insert Button into GridCtrl Cell [message #59704 is a reply to message #59702] |
Fri, 10 March 2023 00:10  |
Oblivion
Messages: 1204 Registered: August 2007
|
Senior Contributor |
|
|
Quote:Now the problem is there is no explanation on how to change label or add functionality to individual buttons with the "MakeButton" method
There are several ways, a simple one:
#include <CtrlLib/CtrlLib.h>
#include <GridCtrl/GridCtrl.h>
using namespace Upp;
struct MyButton : Button {
Value val;
MyButton() : Button()
{
WhenAction = [this]
{
PromptOK("The value of the button is " + AsString(val));
};
}
void SetData(const Value& v) override
{
SetLabel(AsString(val = v));
}
Value GetData() const override
{
return val;
}
};
struct MyApp : TopWindow {
GridCtrl list;
MyApp()
{
Sizeable().Zoomable().CenterScreen().SetRect(0, 0, 1024, 800);
Add(list.SizePos());
list.AddColumn("texts");
list.AddColumn("buttons").Ctrls<MyButton>();
for(int i = 0; i < 100; i++)
list.Add(AsString(i), i * 1000);
}
};
GUI_APP_MAIN
{
MyApp().Run();
}
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Fri, 10 March 2023 00:26] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Mon Apr 28 09:51:14 CEST 2025
Total time taken to generate the page: 0.00541 seconds
|
|
|