English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 23 April 2016, 13:55   #1
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
How to change a gadtools button's text?

I am trying to find a way to change the text of a button.
I've tried to find anything on the internet and failed

I got 29 gadgets. The first one is of STRING_KIND and all the others of BUTTON_KIND.

I've tried updating the text without removing the gadget and failed.
I've tried updating the text after removing the gadget and then readding the gadget and failed.

(full source here: https://github.com/alexalkis/acalc/blob/master/acalc.c )

Relevant snippet
Code:
case GD_SIN:
      #ifdef USEMPFR
      mpfr_sin(res, res, MPFR_RNDN);
      #else
      res = sin(res);
      #endif
      RemoveGadget(wp, gadgets[1]);
      gadgets[1]->GadgetText->IText = "Ok";
      Gadgetdata[1].ng_GadgetText = "shit";
      int pos = AddGadget(wp, gadgets[1], 0);
      RefreshGList(gadgets[1], wp, NULL, 1);
      GT_RefreshWindow(wp, NULL);
      printf("new gadget pos: %d\n", pos);
What am I doing wrong?

Thanks.
alkis is offline  
Old 24 April 2016, 11:29   #2
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
I am trying to change a button from saying "sin" on the button to anything else. (for example "ok")

The above snippet gets executed, but the button's text stays the same.
alkis is offline  
Old 24 April 2016, 11:55   #3
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
gadget[1] is a pointer to a "Gadget"

Code:
struct Gadget
        {
        struct Gadget *NextGadget;
        WORD LeftEdge, TopEdge;
        WORD Width, Height;
        UWORD Flags;
        UWORD Activation;
        UWORD GadgetType;
        APTR GadgetRender;
        APTR SelectRender;
        struct IntuiText *GadgetText;
        LONG MutualExclude;
        APTR SpecialInfo;
        UWORD GadgetID;
        APTR UserData;
        };
GadgetText is
Code:
struct IntuiText
{
    UBYTE FrontPen, BackPen;	/* the pen numbers for the rendering */
    UBYTE DrawMode;		/* the mode for rendering the text */
    WORD LeftEdge;		/* relative start location for the text */
    WORD TopEdge;		/* relative start location for the text */
    struct TextAttr *ITextFont;	/* if NULL, you accept the default */
    UBYTE *IText;		/* pointer to null-terminated text */
    struct IntuiText *NextText; /* pointer to another IntuiText to render */
};
IText is a (UBYTE *) and so gadgets[1]->GadgetText->IText="Ok"; looks ok.
alkis is offline  
Old 24 April 2016, 12:09   #4
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Code:
RemoveGadget(wp, gadgets[1]);
      gadgets[1]->GadgetText->IText = "Ok";
      Gadgetdata[1].ng_GadgetText = "shit";
      int pos = AddGadget(wp, gadgets[1], 0);
      RefreshGList(gadgets[0], wp, NULL, -1);
      GT_RefreshWindow(wp, NULL);
      printf("new gadget pos: %d with text \"%s\"\n", pos, gadgets[1]->GadgetText->IText);
this prints:
Code:
new gadget pos: 0 with text "Ok"
So, it's a refresh problem.

Further, if I comment out the whole block except the RemoveGadget() then the button is "dead" (i.e. you can't press it) but it is still there!!!
[EDIT] if I just comment out the AddGadget it has the same behavior. Button still visible but dead.
alkis is offline  
Old 24 April 2016, 12:15   #5
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Quote:
Originally Posted by chip View Post
I afraid i can't help you anymore on this, since i know C only for command line programs purposes .-)
No problem, thanks for the effort!
alkis is offline  
Old 24 April 2016, 12:25   #6
Leffmann
 
Join Date: Jul 2008
Location: Sweden
Posts: 2,269
What about GT_SetGadgetAttrs? It's used to change all attributes of gadgets, so if it's doable it should be in there.
Leffmann is offline  
Old 24 April 2016, 12:29   #7
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Only GA_DISABLED is supported on BUTTON_KIND. (http://amigadev.elowar.com/read/ADCD.../node03F6.html)

PS: I've tried GT_SetGadgetAttrs(gadgets[1], wp, NULL, GTTX_Text ,"Ok", TAG_END); and failed anyways
alkis is offline  
Old 24 April 2016, 21:15   #8
bubbob42
Registered User
 
Join Date: Oct 2012
Location: Germany
Posts: 585
I'm not sure, but are you trying to change the text by just assigning a new string?
bubbob42 is offline  
Old 25 April 2016, 00:22   #9
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Quote:
Originally Posted by bubbob42 View Post
I'm not sure, but are you trying to change the text by just assigning a new string?
I've tried that.
I've tried, remove gadget, change text, add gadget

Both fail.
alkis is offline  
Old 25 April 2016, 00:46   #10
bubbob42
Registered User
 
Join Date: Oct 2012
Location: Germany
Posts: 585
Create two strings at the start of the program, remove the gadget, set pointer to 2nd string and add it again.

Or copy new string over old text with strcpy or sprintf, after removing the gadget.
bubbob42 is offline  
Old 25 April 2016, 19:30   #11
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Quote:
Originally Posted by bubbob42 View Post
Create two strings at the start of the program, remove the gadget, set pointer to 2nd string and add it again.

Or copy new string over old text with strcpy or sprintf, after removing the gadget.
The first idea is already tried.
Although the second idea is mega-hack, I've tried it. Still doesn't work.
alkis is offline  
Old 25 April 2016, 19:44   #12
bubbob42
Registered User
 
Join Date: Oct 2012
Location: Germany
Posts: 585
Hrm...I am tempted to try it myself. Maybe at the weekend.
bubbob42 is offline  
Old 25 April 2016, 20:19   #13
bubbob42
Registered User
 
Join Date: Oct 2012
Location: Germany
Posts: 585
If I am interpreting the RKM correctly, gadget data is allocated (and thus copied) when calling CreateGadget(). You cannot modify gadget text via SetGadgetAttr(), as you already found out.

Only way is to remove all gadgets, call FreeGadgets() and create a new gadget list.

Alternative: create your own gadget with pure Intuition or BOOPSI.
bubbob42 is offline  
Old 26 April 2016, 06:08   #14
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Quote:
Originally Posted by bubbob42 View Post
Only way is to remove all gadgets, call FreeGadgets() and create a new gadget list.

Alternative: create your own gadget with pure Intuition or BOOPSI.
I was so afraid that I had to remove all, free all and recreate the whole list. Seems so terribly inefficient.

Oh well, will give it a shot. Thanks.
alkis is offline  
Old 26 April 2016, 07:41   #15
bubbob42
Registered User
 
Join Date: Oct 2012
Location: Germany
Posts: 585
GadTools gadgets have been implemented as an easy, "no hassle"-framework for the most common UI-tasks. That's why you hit the wall quickly if you try something the developers didn't regard as common.

I'm currently struggling with a BOOPSI-class for a button-gadget displaying changing imagery. Believe me, it's not getting any better
bubbob42 is offline  
Old 26 April 2016, 19:11   #16
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
Indeed removing all and readding worked! (Yay)

so this does work

Code:
      Gadgetdata[1].ng_GadgetText = "new";
      ReAddGadgets();
with ReAddGadgets written as:
Code:
void ReAddGadgets(void) {
  struct Gadget *pgad;
  int i;

  if (glist) {
    RemoveGList(wp, glist, -1);
    FreeGadgets(glist);
  }

  pgad = CreateContext(&glist);  /* ContextData ! */

  for (i=0; i < 29; i++) {
    if (gadgets[i] = pgad = CreateGadgetA(
            (i == 0) ? STRING_KIND : BUTTON_KIND,
            pgad,
            &Gadgetdata[i],
            (i == 0) ? (struct TagItem *)&GadgetTags[i]: NULL)) {
      if (i == 0) {
        display = pgad;
      }
    } else {
      printf("Failed to create gadget %d.\n", i);
    }
  }
  AddGList(wp, glist, -1, -1, NULL);
  RefreshGList(glist, wp, NULL, -1);
  GT_RefreshWindow(wp, NULL);
}
That will do, I guess. Thank you all!
alkis is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
gadtools zharn Coders. C/C++ 6 21 February 2014 20:26
change the config with a button click ? turrican3 support.WinUAE 4 08 December 2013 09:54
Change Scalos Icon Text color TronPlayer support.Hardware 3 05 January 2011 23:03
Any way to change Radboot Left Mouse Button? fitzsteve project.ClassicWB 8 20 January 2010 22:53
Change text colors frikilokooo project.ClassicWB 6 08 March 2008 21:26

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 16:14.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.09106 seconds with 13 queries