Create buttons in a for(...) loop

19 posts / 0 new
Last post
mritter0
mritter0's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2014-04-21 21:15
Create buttons in a for(...) loop

How would I go about changing the below code to have each button be made in a loop with custom details (text, id, etc)?

  1. if (!(Objects[GAD_BUTTONS_GROUP]=HGroupObject,
  2. LAYOUT_SpaceOuter, TRUE,
  3. LAYOUT_SpaceInner, TRUE,
  4. LAYOUT_AddChild, Objects[GAD_BUTTON1]=NewObject(ButtonClass,NULL,
  5. GA_ID, GAD_BUTTON1,
  6. GA_RelVerify, TRUE,
  7. GA_Text, "Button 1",
  8. TAG_END),
  9. LAYOUT_AddChild, Objects[GAD_BUTTON2]=NewObject(ButtonClass,NULL,
  10. GA_ID, GAD_BUTTON2,
  11. GA_RelVerify, TRUE,
  12. GA_Text, "Button 2",
  13. TAG_END),
  14. LAYOUT_AddChild, Objects[GAD_BUTTON3]=NewObject(ButtonClass,NULL,
  15. GA_ID, GAD_BUTTON3,
  16. GA_RelVerify, TRUE,
  17. GA_Text, "Button 3",
  18. TAG_END),
  19. EndHGroup))
  20. return(FALSE);
  21. else
  22. return(TRUE);

Start with the HGroupObject.
Go through a for(...) loop to make each button.
Finish with EndHGroup.

  1. if (!(Objects[GAD_BUTTONS_GROUP]=HGroupObject,
  2. LAYOUT_SpaceOuter, TRUE,
  3. LAYOUT_SpaceInner, TRUE,
  4.  
  5. for(x=0; x<3; x++)
  6. {
  7. LAYOUT_AddChild, Objects[x]=NewObject(ButtonClass,NULL,
  8. GA_ID, x,
  9. GA_RelVerify, TRUE,
  10. GA_Text, Text[x],
  11. TAG_END),
  12. }
  13.  
  14. EndHGroup))
  15. return(FALSE);
  16. else
  17. return(TRUE);

THEN...later on how would I add another button to the end, and remove a button.

salass00
salass00's picture
Offline
Last seen: 6 months 3 weeks ago
Joined: 2011-02-03 11:27
You can use LAYOUT_AddChild

You can use LAYOUT_AddChild with SetAttrs():

  1. if ((Objects[GAD_BUTTONS_GROUP] = HGroupObject,
  2. LAYOUT_SpaceOuter, TRUE,
  3. LAYOUT_SpaceInner, TRUE,
  4. EndHGroup) != NULL)
  5. {
  6. for(x = 0; x < 3; x++)
  7. {
  8. Objects[x] = NewObject(ButtonClass, NULL,
  9. GA_ID, x,
  10. GA_RelVerify, TRUE,
  11. GA_Text, Text[x],
  12. TAG_END);
  13. if (Objects[x] == NULL) {
  14. DisposeObject(Objects[GAD_BUTTONS_GROUP]);
  15. Objects[GAD_BUTTONS_GROUP] = NULL;
  16. return FALSE;
  17. }
  18. SetAttrs(Objects[GAD_BUTTONS_GROUP],
  19. LAYOUT_AddChild, Objects[x],
  20. TAG_END);
  21. }
  22. return TRUE;
  23. }
  24. return FALSE;
mritter0
mritter0's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2014-04-21 21:15
Thanks. I figured it out

Thanks. I figured it out after some sleep. Very similar to yours.

mritter0
mritter0's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2014-04-21 21:15
I changed the group settings

I changed the group settings to no space

  1. LAYOUT_InnerSpacing, 0,
  2. LAYOUT_LeftSpacing, 0,
  3. LAYOUT_RightSpacing, 0,
  4. LAYOUT_TopSpacing, 0,
  5. LAYOUT_BottomSpacing, 0,

When there is a button added to the group all is fine.

When there are no buttons added to the group, it still adds 2 vertical lines, even though the group is empty. Can a group have a space of 0 horiz and vert?

mritter0
mritter0's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2014-04-21 21:15
Actually it

Actually it is

  1. LAYOUT_AddChild, Objects[TBG_LAYOUT]=NewObject(LayoutClass,NULL,
  2. LAYOUT_InnerSpacing, 0,
  3. LAYOUT_LeftSpacing, 0,
  4. LAYOUT_RightSpacing, 0,
  5. LAYOUT_TopSpacing, 0,
  6. LAYOUT_BottomSpacing, 0,

Can a LayoutClass not be 0x0 if nothing is in it?

zzd10h
zzd10h's picture
Offline
Last seen: 7 years 4 weeks ago
Joined: 2012-08-24 20:56
Hi, I'm a newbie in Reaction

Hi,
I'm a newbie in Reaction but I tried to use this snippet in my code without luck.

It displays only my fixed bitmap/buttons

IMAGE(http://zzd10h.amiga-ng.org/Divers/FastView_Capture_15-05-10_002744.JPG)
I tried too to DoMethod(win, WM_RETHINK);...

Thank you for your help

  1. void make_window()
  2. {
  3. win = NewObject(NULL, "window.class",
  4. WINDOW_Position, WPOS_CENTERSCREEN,
  5. WINDOW_Layout, VLayoutObject,
  6.  
  7.  
  8. LAYOUT_AddChild, OBJ(OBJ_BUT_GRP) = HGroupObject,
  9. LAYOUT_AddChild, LayoutObject,
  10. LAYOUT_VertAlignment, LALIGN_CENTER,
  11. LAYOUT_HorizAlignment, LALIGN_CENTER,
  12.  
  13. LAYOUT_AddChild, OBJ(OBJ_IBUT_1) = ButtonObject,
  14. GA_ID, OBJ_IBUT_1,
  15. GA_RelVerify, TRUE,
  16. BUTTON_BevelStyle, BVS_NONE,
  17. BUTTON_Transparent, TRUE,
  18. BUTTON_RenderImage, OBJ(OBJ_SEL_1) = BitMapObject,
  19. BITMAP_SourceFile, "images/pdf",
  20. BITMAP_DisabledSourceFile, "images/pdf_g",
  21. BITMAP_Screen, screen,
  22. BITMAP_Masking, TRUE,
  23. BitMapEnd,
  24. BUTTON_SelectImage, OBJ(OBJ_UNSEL_1) = BitMapObject,
  25. BITMAP_SourceFile, "images/pdf_s",
  26. BITMAP_Screen, screen,
  27. BITMAP_Masking, TRUE,
  28. BitMapEnd,
  29. ButtonEnd,
  30. LAYOUT_AddChild, OBJ(OBJ_IBUT_2) = ButtonObject,
  31. GA_ID, OBJ_IBUT_2,
  32. GA_RelVerify, TRUE,
  33. BUTTON_BevelStyle, BVS_NONE,
  34. BUTTON_Transparent, TRUE,
  35. BUTTON_RenderImage, OBJ(OBJ_SEL_2) = BitMapObject,
  36. BITMAP_SourceFile, "images/picture",
  37. BITMAP_DisabledSourceFile, "images/picture_g",
  38. BITMAP_Screen, screen,
  39. BITMAP_Masking, TRUE,
  40. BitMapEnd,
  41. BUTTON_SelectImage, OBJ(OBJ_UNSEL_2) = BitMapObject,
  42. BITMAP_SourceFile, "images/picture_s",
  43. BITMAP_Screen, screen,
  44. BITMAP_Masking, TRUE,
  45. BitMapEnd,
  46. ButtonEnd,
  47.  
  48. LabelEnd,
  49.  
  50. LayoutEnd,
  51. EndHGroup,
  52.  
  53. CHILD_WeightedHeight, 0,
  54.  
  55. End, // VLayout
  56. WindowEnd;
  57.  
  58. int x = 0 ;
  59. for(x = 0; x < 3; x++)
  60. {
  61. Objects[x] = NewObject(NULL, "button.gadget",
  62. GA_ID,x,
  63. GA_RelVerify, TRUE,
  64. GA_Text,"xxx",
  65. TAG_END);
  66.  
  67. if (Objects[x] == NULL)
  68. {
  69. DisposeObject(OBJ(OBJ_BUT_GRP));
  70. Objects[OBJ_BUT_GRP] = NULL;
  71. return ;
  72. }
  73. SetAttrs(OBJ(OBJ_BUT_GRP),
  74. LAYOUT_AddChild, Objects[x],
  75. TAG_END);
  76. printf("x %d \n",x);
  77. }
  78.  
  79. }

In this code, the first 2 fixed buttons are well displayed but not the 3 dynamic last ones.

jabirulo
jabirulo's picture
Offline
Last seen: 4 weeks 1 day ago
Joined: 2013-05-30 00:53
Hi I add a button to my

Hi I add a button to my already "created" ReAction window object (Object *win2;) this way:

  1. struct Window *window2;
  2. ..
  3. win2 = IIntuition->NewObject(WindowClass, NULL,
  4. ..
  5. TAG_END);
  6.  
  7. if(KM3) // if set add KEYMAP3 button
  8. {
  9. OBJ(OBJ_BTN3) = IIntuition->NewObject(ButtonClass, NULL,
  10. GA_ID, OBJ_BTN3,
  11. GA_RelVerify, TRUE,
  12. GA_Underscore, ~0,
  13. BUTTON_BevelStyle, BVS_THIN,
  14. //BUTTON_Transparent, TRUE,
  15. BUTTON_RenderImage, OBJ(GID_KM3) = IIntuition->NewObject(BitMapClass, NULL,
  16. IA_Width, /*dd->size.width*/IMG_W + 8,
  17. IA_Height, /*dd->size.height*/IMG_H + 8,
  18. IA_Scalable, dd->scaleflag > 1.0? TRUE:FALSE,
  19. BITMAP_SourceFile, keymap2Flag,
  20. BITMAP_Screen, dd->scr,
  21. BITMAP_Masking, TRUE,
  22. //BITMAP_Transparent,TRUE,
  23. TAG_DONE),
  24. TAG_DONE);
  25.  
  26. if( !OBJ(GID_KM3) ) // language NOT valid/available
  27. IIntuition->SetAttrs(OBJ(OBJ_BTN3), GA_Text,dd->KeymapName[2], GA_Disabled,TRUE, TAG_DONE);
  28. /*
  29.   struct lmAddChild msg;
  30.   msg.MethodID = LM_ADDCHILD;
  31.   msg.lm_Window = window2;
  32.   msg.lm_Object = OBJ(OBJ_BTN3);
  33.   msg.lm_ObjectAttrs = NULL;
  34.   IIntuition->IDoMethodA(OBJ(OBJ_MAIN), (void*)&msg);
  35. */
  36. IIntuition->IDoMethod(OBJ(OBJ_MAIN), LM_ADDCHILD, window2, OBJ(OBJ_BTN3), NULL);
  37. }
  38. ..
  39. if(win2)
  40. {
  41. if( (window2=(struct Window *)IIntuition->IDoMethod(win2, WM_OPEN, NULL)) )
  42. ..

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P

zzd10h
zzd10h's picture
Offline
Last seen: 7 years 4 weeks ago
Joined: 2012-08-24 20:56
Thank you very much,

Thank you very much,

IDoMethod(OBJ(OBJ_BUT_GRP), LM_ADDCHILD, window, OBJ(OBJ_BUT), NULL);
DoMethod(win, WM_RETHINK);

works well.

But it works only with fix name objects, not in a "for" loop with Objects[x].

Thank you again !

zzd10h
zzd10h's picture
Offline
Last seen: 7 years 4 weeks ago
Joined: 2012-08-24 20:56
Jabirulo fixed my

Jabirulo fixed my problem.

He changed
WINDOW_Layout, VLayoutObject,
by
WINDOW_Layout, OBJ(OBJ_MAIN) = NewObject(NULL, "layout.gadget"

IDoMethod(OBJ(OBJ_MAIN), LM_ADDCHILD, window, OBJ(OBJ_BTN), NULL);

Thank you very much, Javier, very kind :)

thomas
thomas's picture
Offline
Last seen: 3 months 2 weeks ago
Joined: 2011-05-16 14:23
But this adds the buttons to

But this adds the buttons to the vertical main group and not to the horizontal button group where they should appear.

Moreover LM_ADDCHILD is only needed if the window is already open. If you define the buttons before the window opens you can use SetAttrs as you did before.

Anyway here is a working example: http://thomas-rapp.homepage.t-online.de/examples/dyngrp.c

zzd10h
zzd10h's picture
Offline
Last seen: 7 years 4 weeks ago
Joined: 2012-08-24 20:56
Thank you for your help,

Thank you for your help, thomas.

It works well, buttons are well displayed horizontally or vertically by using the same
LAYOUT_Orientation for WINDOW_Layout and Button_Group layout.

WINDOW_Layout, OBJ(OBJ_MAIN) = NewObject(NULL, "layout.gadget",
LAYOUT_Orientation, LAYOUT_ORIENT_HORIZ,

LAYOUT_AddChild, OBJ(OBJ_BUT_GRP) = HGroupObject,
LAYOUT_AddChild, LayoutObject,
LAYOUT_Orientation, LAYOUT_ORIENT_HORIZ,

thomas
thomas's picture
Offline
Last seen: 3 months 2 weeks ago
Joined: 2011-05-16 14:23
If the main group is

If the main group is horizontal then your entire GUI is horizontal, you are not able to place objects below that group. And of course your OBJ_BUT_GRP group becomes superfluous because it remains empty.

zzd10h
zzd10h's picture
Offline
Last seen: 7 years 4 weeks ago
Joined: 2012-08-24 20:56
Yes, you are right for

Yes, you are right for OBJ_BUT_GRP... And unfortunately for me, you are right too for orientation...

My window is very simple, empty and HORIZontal

  1. WINDOW_Layout, OBJ(OBJ_MAIN) =NewObject(NULL, "layout.gadget",
  2. LAYOUT_Orientation, LAYOUT_ORIENT_HORIZ,
  3.  
  4.  
  5. End,

When I try to insert 2 objects vertically aligned in this window (even the "parent" VERT layout OBJ_LAYOUT is useless), they are displayed horizontally.

  1. OBJ(OBJ_LAYOUT) = NewObject(NULL,"layout.gadget",
  2. LAYOUT_Orientation, LAYOUT_ORIENT_VERT,
  3. OBJ(OBJ_BTN) = NewObject(NULL, "button.gadget",
  4. GA_ID, LAST_NUM + x,
  5. GA_RelVerify, TRUE,
  6. BUTTON_BevelStyle, BVS_NONE,
  7. BUTTON_Transparent, TRUE,
  8. BUTTON_RenderImage, iconProgramme->do_Gadget.GadgetRender,
  9. TAG_DONE),
  10. CHILD_Label,OBJ(OBJ_LBL) = NewObject(NULL, "button.gadget",
  11. GA_ReadOnly, TRUE,
  12. GA_Text, "XXX",
  13. BUTTON_BevelStyle, BVS_NONE ,
  14. BUTTON_Transparent, TRUE,
  15. TAG_DONE),
  16. TAG_DONE);
  17.  
  18. IDoMethod(OBJ(OBJ_MAIN), LM_ADDCHILD, window, OBJ(OBJ_LAYOUT), NULL);
  19. IDoMethod(OBJ(OBJ_MAIN), LM_ADDCHILD, window, OBJ(OBJ_BTN), NULL);
  20. IDoMethod(OBJ(OBJ_MAIN), LM_ADDCHILD, window, OBJ(OBJ_LBL), NULL);
  21. or SetAttrs, same results.
  22. SetAttrs(OBJ(OBJ_MAIN),LAYOUT_AddChild, OBJ(OBJ_LAYOUT),TAG_END);
  23. SetAttrs(OBJ(OBJ_MAIN),LAYOUT_AddChild, OBJ(OBJ_BTN),TAG_END);
  24. SetAttrs(OBJ(OBJ_MAIN),LAYOUT_AddChild, OBJ(OBJ_LBL),TAG_END);

IMAGE(http://zzd10h.amiga-ng.org/Divers/Horiz.JPG)

Vertical orientation, all is OK
IMAGE(http://zzd10h.amiga-ng.org/Divers/Vertical.JPG)

Is somebody know a way to insert a VGroup in an HGroup window/Layout ?

Thank you by advance ;)

thomas
thomas's picture
Offline
Last seen: 3 months 2 weeks ago
Joined: 2011-05-16 14:23
See my example above.

See my example above.

zzd10h
zzd10h's picture
Offline
Last seen: 7 years 4 weeks ago
Joined: 2012-08-24 20:56
Certainly that I missed

Certainly that I missed something in your example, but I tried to reproduce it, it's the same problem.

My window is horizontal.
My objects are added horizontally. I would like that they were added by 2 by 2 vertically.

picture above his label | picture above his label | picture above his label ...

now there are

picture | label | picture | label | picture | label ...

Following, I think, your example, I tried

  1. WINDOW_ParentGroup, VGroupObject,
  2.  
  3.  
  4. LAYOUT_AddChild, OBJ(OBJ_MAIN) = HGroupObject,
  5. LAYOUT_SpaceOuter, FALSE,
  6. LAYOUT_EvenSize, TRUE,
  7. EndHGroup,
  8.  
  9. End, // VGroupObject
  10.  
  11.  
  12. OBJ(OBJ_BTN) = NewObject(NULL, "button.gadget",
  13. GA_ID, LAST_NUM + x,
  14. GA_RelVerify, TRUE,
  15. BUTTON_BevelStyle, BVS_NONE,
  16. BUTTON_Transparent, TRUE,
  17. BUTTON_RenderImage, iconProgramme->do_Gadget.GadgetRender,
  18. TAG_DONE),
  19. CHILD_Label,OBJ(OBJ_LBL) = NewObject(NULL, "button.gadget",
  20. GA_ReadOnly, TRUE,
  21. GA_Text, tab_program_label[x],
  22. BUTTON_TextPen, penText,
  23. BUTTON_BevelStyle, BVS_NONE ,
  24. BUTTON_Transparent, TRUE,
  25. TAG_DONE);
  26.  
  27. SetAttrs(OBJ(OBJ_MAIN),LAYOUT_AddChild, OBJ(OBJ_BTN),TAG_END);
  28. SetAttrs(OBJ(OBJ_MAIN),LAYOUT_AddChild, OBJ(OBJ_LBL),TAG_END);

Thank you very much for your help.

thomas
thomas's picture
Offline
Last seen: 3 months 2 weeks ago
Joined: 2011-05-16 14:23
Ok, now I understand, I

Ok, now I understand, I didn't get your problem before.

A layout group can have only one orientation. If you want another orientation inside, you have to add another group or groups.

  1. HGroup
  2. VGroup
  3. Button
  4. Label
  5. End
  6. VGroup
  7. Button
  8. Label
  9. End
  10. VGroup
  11. Button
  12. Label
  13. End
  14. ...
  15. End

So what you have to do in your for() loop is to create a VGroup containing a Button and its Label and add that VGroup to the main HGroup.

zzd10h
zzd10h's picture
Offline
Last seen: 7 years 4 weeks ago
Joined: 2012-08-24 20:56
Is it not what I tried here

Is it not what I tried here ?
http://www.os4coding.net/forum/create-buttons-loop#comment-2284

In this code, I added the 3 objects (VLayout, Button and Label) but I had tried to add only the VLayout without more luck.

Thank you for your patience.

thomas
thomas's picture
Offline
Last seen: 3 months 2 weeks ago
Joined: 2011-05-16 14:23
This code does some bad

This code does some bad things.

The first thing is very bad. You add the same buttons to two groups. This cannot work. An object can only be part of one group at a time. If you add it to another group without removing it first, it corrupts list pointers. It's pure luck that your test didn't crash.

The second thing you probably didn't notice or drew the wrong conclusion. You use a button object with CHILD_Label. This cannot work, either. CHILD_Label expects to get an object of a sub-class of imageclass, but buttons are objects of gadgetclass (that's why it's called button.gadget and not button.image).

If you want to use a button as a label, you should use LAYOUT_AddChild. If you insist on CHILD_Label, then you should use label.image instead of button.gadget.

But using CHILD_Label is part of your problem. Layout.gadget places the CHILD_Label always left or right of the object (depending on the value of LAYOUT_LabelColumn), never above or below. If you want the text to respect LAYOUT_Orientation, you have to use LAYOUT_AddChild or LAYOUT_AddImage (depending on whether you want to add a gadget or an image object).

Here is a new example with icons and labels below them: http://thomas-rapp.homepage.t-online.de/examples/iconbar.c

zzd10h
zzd10h's picture
Offline
Last seen: 7 years 4 weeks ago
Joined: 2012-08-24 20:56
Thank you very much Thomas

Thank you very much Thomas for your explanations and example.

Using it,

  1. if ( ( OBJ(OBJ_BTN) = (APTR) VGroupObject,
  2. LAYOUT_AddChild, ButtonObject,
  3. GA_ID, LAST_NUM + x,
  4. GA_RelVerify, TRUE,
  5. BUTTON_BevelStyle, BVS_NONE,
  6. BUTTON_Transparent, TRUE,
  7. BUTTON_RenderImage, iconProgramme->do_Gadget.GadgetRender,
  8. EndObject,
  9. LAYOUT_AddChild, ButtonObject,
  10. GA_ReadOnly, TRUE,
  11. GA_Text, tab_program_label[x],
  12. BUTTON_BevelStyle, BVS_NONE ,
  13. BUTTON_Transparent, TRUE,
  14. EndObject,
  15. EndVGroup))
  16. {
  17. SetAttrs(OBJ(OBJ_MAIN),LAYOUT_AddChild, OBJ(OBJ_BTN),TAG_END);
  18. }

It works !

IMAGE(http://zzd10h.amiga-ng.org/Divers/FastView_Capture_15-05-12_192619.JPG)

Log in or register to post comments