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 23-Aug-2006, 12:33
Spidy08 Spidy08 is offline
New Member
 
Join Date: Aug 2006
Posts: 27
Spidy08 is on a distinguished road

Button class


Hi-

What im looking for:
I have two button, I want a button to be highlighted (change color) when clicked on, and when the second button is clicked on, it get highlighted and the first one go back to normal.

What I got:
I've created a button class, got my own draw and handle function, I got it to change color when click on, but I don't know how to tell which button got click on so that I can change the other button back to the original color.

any idea?

Thanks.
  #2  
Old 23-Aug-2006, 20:17
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: Button class


Hello and Welcome to GIDForums™ Spidy08. Your first two posts here sound like interesting things to play with. In order for anyone to offer you helpful information though, you will have to do a few things.

First, read the Guidelines Thread for some good information that will go a long way toward getting help that is actually helpful. It outlines some special syntax highlighters we have for your use here as well as general information on describing your problem.

As I request to anyone new here, please let us know what version of FLTK you are using. Some people use the latest subversion repo stuff, others the packaged snapshots.
Include your code in your post. This is the most important. You will find that the replies you get will be more in line with your coding that way.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #3  
Old 24-Aug-2006, 07:34
MatthiasWM MatthiasWM is offline
VIP
 
Join Date: Nov 2004
Posts: 62
MatthiasWM will become famous soon enough

Re: Button class


Quote:
Originally Posted by Spidy08
Hi-

I've created a button class, got my own draw and handle function, I got it to change color when click on, but I don't know how to tell which button got click on so that I can change the other button back to the original color.

any idea?

Thanks.

Since you are deriving a new class anyways, you can add a member that keeps a pointer to the other partner button and use that to find out which button to change. Or you just use two different callbacks to toggle button colors around.
  #4  
Old 24-Aug-2006, 07:35
Spidy08 Spidy08 is offline
New Member
 
Join Date: Aug 2006
Posts: 27
Spidy08 is on a distinguished road

Re: Button class


Hi -

Sorry... here is more information.

FLTK version 1.1.7

What I got here is a black rectangle on top of a purple rectangle, and when the black one is clicked on, the one in the back change its color to yellow. I want two obj of this class and want only one of them to be selected at one time, so if obj 1 is selected, I only want that border color to change, then when the second one is selected, the first one should change back to purple.

Pretty much like a on/off button... I just need a way to detect which one is selected, and since their both using the same class, I don't know how to do it. Unless there is another way to highlight border... this is easier in vb.

CPP / C++ / C Code:
///////////////////////////////////////////////////////////////////////////////
class CDisplayBtn : public Fl_Button
{
protected:
    virtual void draw();

public:
    CDisplayBtn( int X, int Y, int Btn_Height, int Btn_Width, char *str,
                      Fl_Window* m_pWin);
   virtual int handle( int );

private:
   Fl_Color       m_Color;
   Fl_Color       m_BorderColor;
   Fl_Window* m_pWin;
   int              m_X, m_Y, m_Height, m_Width;
};
CPP / C++ / C Code:
///////////////////////////////////////////////////////////////////////////////
void CDisplayBtn::draw()
{
   // Draw the border rectangle
   fl_rectf( m_X-4, m_Y-4, m_Width+8, m_Height+68, m_BorderColor );

   // Draw the box background.
   fl_rectf( m_X, m_Y, m_Width, m_Height, m_Color );

}

///////////////////////////////////////////////////////////////////////////////
CDisplayBtn::CDisplayBtn(int X, int Y, int Btn_Width, int Btn_Height, char *str, Fl_Window* pWin)
: Fl_Button(X, Y, Btn_Width, Btn_Height, str)
{
   m_X      = X;
   m_Y      = Y;
   m_Height = Btn_Height;
   m_Width  = Btn_Width;
   m_Color  = FL_BLACK;
   m_pWin   = pWin;

   m_BorderColor  = fl_rgb_color(100, 90, 105);

   align     ( FL_ALIGN_TOP_LEFT | FL_ALIGN_WRAP );
	labelcolor( FL_YELLOW );
	color     ( FL_BLACK );

};

///////////////////////////////////////////////////////////////////////////////
int CDisplayBtn::handle( int event )
{
    switch( event )
    {
    case FL_RELEASE :
      // If user click on this retangle, then we change the color of the border
      // to indicate its selected.
      if( Fl::event_button() == FL_LEFT_MOUSE )
      {
         m_BorderColor = FL_YELLOW;
         m_pWin->redraw();
      }

    default:
        return Fl_Button::handle( event );
    }
}
Last edited by cable_guy_67 : 24-Aug-2006 at 08:22. Reason: Please surround your FLTK code with [cpp] YOUR CODE HERE [/cpp]
  #5  
Old 24-Aug-2006, 07:52
MatthiasWM MatthiasWM is offline
VIP
 
Join Date: Nov 2004
Posts: 62
MatthiasWM will become famous soon enough

Re: Button class


You can use a Fl_Toggle_button instead and read Fl_Toggle_button::value(). If you get 1, it's on. If you get 0, it's off. You can then also read or set the other buttons in your interface with value() and value(int);
  #6  
Old 24-Aug-2006, 08:20
Spidy08 Spidy08 is offline
New Member
 
Join Date: Aug 2006
Posts: 27
Spidy08 is on a distinguished road

Re: Button class


Sorry im kinda slow here.... so bare with me.

Fl_Toggle_Button will only toggle the value of that button right? I don't want the value to toggle until the second button is push.

Not sure how to use the callback routine.

CPP / C++ / C Code:
///////////////////////////////////////////////////////////////
void Display_Callback( Fl_Widget *pW, void *data )
{
   
   // OK... how do you toggle the border color for the two obj here?

}


///////////////////////////////////////////////////////////////
int Val = 1;

Rectangle_1 = new CDisplayBtn( 10, 15, 447, 235, "MyRect", this );

Rectangle_1->callback( Display_Callback, &Val ); //???

//---------------------------------------------------------------
int Val2 = 2;

Rectangle_2 = new CDisplayBtn( 470, 15, 447, 235, "MyRect2", this );

Rectangle_2->callback( Display_Callback, &Val2 ); //???
Last edited by LuciWiz : 24-Aug-2006 at 15:21. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #7  
Old 24-Aug-2006, 08:43
MatthiasWM MatthiasWM is offline
VIP
 
Join Date: Nov 2004
Posts: 62
MatthiasWM will become famous soon enough

Re: Button class


Quote:
Originally Posted by Spidy08
Sorry im kinda slow here.... so bare with me.

I'll keep my clothes on if you don't mind ;-)

Anyway, how about this:

CPP / C++ / C Code:

class MyButton : public Fl_Toggle_Button {
public:
  MyButton(int x, int y, int w, int h, const char *l=0) { .. }
  void set_outline() { pOutline = 1; redraw(); }
  void clear_outline() { pOutline = 0; redraw(); }
protected:
  void draw() {
    Fl_Toggle_Button::draw();
    if (pOutline) fl_rect(x(), y(), w(), h(), FL_RED);
  }
};

MyButton *a;
MyButton *b;

void click_a_cb(Fl_Widget*, void*) {
  if (a->value())
    b->set_outline();
  else
    b->clear_outline();
}

main(int argc, char** argv) {
  ...
  a = new MyButton(10, 10, 100, 25, "A");
  a->tooltip("Click me on and off to toggle the outline on B");
  a->callback(click_a_cb);
  b = new MyButton(10, 50, 100, 25, "B");
  ... whatever else your heart desires
}
  #8  
Old 24-Aug-2006, 10:44
Spidy08 Spidy08 is offline
New Member
 
Join Date: Aug 2006
Posts: 27
Spidy08 is on a distinguished road

Re: Button class


Ok.. tried your way... didnt work... so i use some of ur code with mine... and it work perfectly. But it seem like a lot of codes just to do a little thing.

Thanks a lot for ur help.

CPP / C++ / C Code:

///////////////////////////////////////////////////////////////////////////////
class CDisplayBtn : public Fl_Button
{
protected:
    virtual void draw();

public:
   CDisplayBtn( int X, int Y, int Btn_Height, int Btn_Width, char *str,
                Fl_Window* m_pWin);
   virtual int handle( int );
   void set_outline();
   void clear_outline();

private:
   Fl_Color   m_Color;
   Fl_Color   m_BorderColor;
   Fl_Window* m_pWin;
   int        m_X, m_Y, m_Height, m_Width;
   bool       m_Select; 
};


////////////////////////////////////////////////////////////////////////////
void CDisplayBtn::set_outline() 
{ 
   m_BorderColor = FL_YELLOW;
}

void CDisplayBtn::clear_outline()
{ 
   m_BorderColor = fl_rgb_color(100, 90, 105);
}

///////////////////////////////////////////////////////////////////////////////
void CDisplayBtn::draw()
{

   // Draw the border rectangle
   fl_rectf( m_X-4, m_Y-4, m_Width+8, m_Height+68, m_BorderColor );

   // Draw the box background.
   fl_rectf( m_X, m_Y, m_Width, m_Height, m_Color );

}

///////////////////////////////////////////////////////////////////////////////
int CDisplayBtn::handle( int event )
{
    switch( event )
    {
    case FL_RELEASE :
      // If user click on this retangle, then we change the color of the border
      // to indicate its selected.
      if( Fl::event_button() == FL_LEFT_MOUSE )
      {
         if (a->value() == 1)
         {
            a->set_outline();
            b->clear_outline();
            a->value(0);
         }
         else if (m_Warrior->value() == 1)
         {
            b->set_outline();
            a->clear_outline();
            b->value(0);
         }
         m_pWin->redraw();
      }

    default:
        return Fl_Button::handle( event );
    }
}
///////////////////////////////////////////////////////////////////////////////
CDisplayBtn::CDisplayBtn(int X, int Y, int Btn_Width, int Btn_Height, char *str, Fl_Window* pWin)
: Fl_Button(X, Y, Btn_Width, Btn_Height, str)
{
   m_X       = X;
   m_Y       = Y;
   m_Height  = Btn_Height;
   m_Width   = Btn_Width;
   m_Color   = FL_BLACK;
   m_pWin    = pWin;

   m_BorderColor  = fl_rgb_color(100, 90, 105);

   align     ( FL_ALIGN_TOP_LEFT | FL_ALIGN_WRAP );
   labelcolor( FL_YELLOW );
   color     ( FL_BLACK );

};

///////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES

CDisplayBtn   *a;
CDisplayBtn   *b;


main(int argc, char** argv) {
  ...
  a = new CDisplayBtn(10, 10, 100, 25, "A");
  a->type( FL_TOGGLE_BUTTON );
  a->tooltip("Click me on and off to toggle the outline on B");
  a->value(0);

  b = new CDisplayBtn(10, 50, 100, 25, "B");
  b->type( FL_TOGGLE_BUTTON );
  ... whatever else your heart desires
}
Last edited by cable_guy_67 : 24-Aug-2006 at 10:46. Reason: Changed CODE tags to CPP tags for syntax highlighting
  #9  
Old 25-Aug-2006, 01:52
rbp rbp is offline
Junior Member
 
Join Date: Nov 2005
Location: Melbourne, Australia
Posts: 60
rbp will become famous soon enough

Re: Button class


overloading is more powerful, but if you just want a simple example:

CPP / C++ / C Code:
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Button.H>

Fl_Button *b1=(Fl_Button *)0;
Fl_Button *b2=(Fl_Button *)0;

void cb_b1(Fl_Button*, void*) {
    b1->color( FL_RED );
    b2->color( FL_BACKGROUND_COLOR );
    b2->redraw();
}
void cb_b2(Fl_Button*, void*) {
    b2->color( FL_GREEN );
    b1->color( FL_BACKGROUND_COLOR );
    b1->redraw();
}

int main(int argc, char **argv) {
    Fl_Double_Window *w = new Fl_Double_Window(180, 105, "Button test");
    b1 = new Fl_Button(0, 0, 90, 105, "B1");
    b1->callback((Fl_Callback*)cb_b1);
    b2 = new Fl_Button(90, 0, 90, 105, "B2");
    b2->callback((Fl_Callback*)cb_b2);
    w->end();
    w->show(argc, argv);
    return Fl::run();
}
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

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
C++ class -- Please help vnca_1 C++ Forum 3 14-Jun-2006 12:31
a tester class and then some. postage Java Forum 1 06-May-2006 15:48
Group Round buttons (radio buttons) toaster FLTK Forum 1 03-Jan-2006 06:57
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 C++ Forum 2 19-Dec-2004 06:06
Help! Some basal questions about MFC xutingnjupt MS Visual C++ / MFC Forum 1 05-Dec-2004 03:38

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:10.


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