GIDForums  

Go Back   GIDForums > Computer Programming Forums > FLTK Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Nov-2010, 07:22
sudox sudox is offline
New Member
 
Join Date: Nov 2010
Posts: 2
sudox is on a distinguished road
Question

Changing button text


I'm trying to use callbacks to make a simple program that changes the label for a button when I click a different button. Please pay close attention to what I'm asking.... Let's take the following rhetorical example:
There is a button (a) that has the label "Click". Another button (b) has the label "Button". I want to write the program so that when I click on a the label of b changes to something else, i.e. "FOO".
I've tried doing this using callbacks, but I can only get it to work if I am changing the label of the widget the callback is attached to. The following is my code. In it's current state when I click on "button" it crashes the program.
CPP / C++ / C Code:
#include <E:/FLTK/FL/Fl.H>
#include <E:/FLTK/FL/Fl_Window.H>
#include <E:/FLTK/FL/Fl_Box.H>
#include <E:/FLTK/FL/Fl_Button.H>

void changeIt_cb(Fl_Widget *w, Fl_Widget *cons_button)
{
	cons_button->label("Cakes");
}

int main(int argc, char **argv) {
  Fl_Window *window = new Fl_Window(800,600);
  Fl_Box *box = new Fl_Box(250,250,260,100,"Hello, World!");
  Fl_Button *button = new Fl_Button(100,100,45,45,"Click");
  Fl_Button *cons_button = new Fl_Button(200,200,45,45,"Text");
  box->box(FL_UP_BOX);
  box->labelsize(36);
  box->labelfont(FL_BOLD+FL_ITALIC);
  box->labeltype(FL_SHADOW_LABEL);
  button->callback((Fl_Callback *)changeIt_cb, &cons_button);
  window->end();
  window->show(argc, argv);
  return Fl::run();
}

Notes: I'm using the latest FLTK SVN snapshot and Microsoft Visual Studio C++.
  #2  
Old 21-Nov-2010, 08:43
MatthiasWM MatthiasWM is offline
VIP
 
Join Date: Nov 2004
Posts: 148
MatthiasWM will become famous soon enough

Re: Changing button text


After changing the label, you must call "redraw()" for that widget. Only changing the value of a widget calls redraw() automatically.

Please also note that when you set the label of a widget, the widget will reference that string at every redraw. When in doubt, call "copy_label(...)" instead.
  #3  
Old 22-Nov-2010, 10:12
sudox sudox is offline
New Member
 
Join Date: Nov 2010
Posts: 2
sudox is on a distinguished road

Re: Changing button text


I tried those methods without success, but I looked into the form of the callback. Taking note that the tutorial says to write the callback as:
CPP / C++ / C Code:
button->callback(changeIt_cb, &cons_button);
but it doesn't work unless I write it as:
CPP / C++ / C Code:
button->callback((Fl_Callback *)changeIt_cb, &cons_button)
So I decided it would be a good idea to try writing the second data piece the same way:
CPP / C++ / C Code:
button->callback((Fl_Callback *)changeIt_cb, (Fl_Button *)cons_button)
And that did the trick.
  #4  
Old 29-Nov-2010, 15:27
MatthiasWM MatthiasWM is offline
VIP
 
Join Date: Nov 2004
Posts: 148
MatthiasWM will become famous soon enough

Re: Changing button text


Oh, I did not catch this one:

button->callback((Fl_Callback *)changeIt_cb, &cons_button);

cons_button is already a pointer, but the & passes the address of the pointer (a pointer to a pointer), instead of just the pointer. Just leaving out the & will fix the bug. The redraw() is still needed though.
  #5  
Old 05-Mar-2011, 08:26
quneur quneur is offline
Junior Member
 
Join Date: Mar 2011
Posts: 59
quneur has a spectacular aura about

Re: Changing button text


I have a project where a button on the tool bar has to change the label of a button on the status bar. The button on the status bar is also clickable and changes.

I found it all has to do with scope. In your case, both buttons are instantiated within main; but if one button was a child class of a widget then it would have been a bit tricky. This tutorial has a few pointers:
http://www3.telus.net/public/robark/
in which you would have to do the first option of two widget talking, ie.

buttonone->parent()->child(buttonno.)->label("mylabel");
where buttonno. is second button number.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Change the caption on button when clicked drewdaman MS Visual C++ / MFC Forum 5 31-May-2009 20:56
changing caption of static text control Pandiani MS Visual C++ / MFC Forum 1 20-Sep-2004 01:38
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
Placing Flash/Images/Grapics inside TEXT with DHTML CSS Style Filteres 000 Web Design Forum 1 24-May-2003 11:25

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:49.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.