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 08-Sep-2006, 15:41
azark azark is offline
New Member
 
Join Date: Sep 2006
Posts: 6
azark is on a distinguished road

Button press needs recognition


Greetings! This is my first post to this fine forum which I've found quite valuable. Thanks Mark for the reply to my private email.

My problem here is getting a button to be recognized when a routine is running. I've tried to simplify what I am seeking by writing a small demo of two buttons and a progress bar. One button (Start) callback starts a loop counting seconds and displaying the progress on the bar. (I finally stumbled on "flush" to update the window display during the loop iterations!). The 2nd button (Stop) should cause the cessation of the loop -- but currently will not be recognized until the loop in the start callback returns.

Is there a way around this? Pointers to previous forum threads, examples, etc would be welcome. I'm quite rusty on C/C++ but can usually figure things out.

Thanks,
Don

CPP / C++ / C Code:

#include <stdio.h>
#include <unistd.h>
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
extern Fl_Double_Window *progress;
#include <FL/Fl_Progress.H>
#include <FL/Fl_Button.H>
extern Fl_Button *start_button;
extern Fl_Button *stop_button;

Fl_Double_Window *progress=(Fl_Double_Window *)0;
Fl_Button *start_button=(Fl_Button *)0;
Fl_Button *stop_button=(Fl_Button *)0;
Fl_Progress *progr=(Fl_Progress *)0;

int stopflag;

void runcount(void)
	{
	unsigned int c;
	
	progr->minimum((float) 0);
	progr->maximum((float) 10);
	
	for (c=0; (c<10) && (stopflag!=1); c++)
		{
		sleep(1);
		progr->value((float) c+1);
		Fl::flush();
		}
	}

void cb_startb(void)
	{
	stopflag = 0;
	runcount();
	}

void cb_stopb(void)
	{
	stopflag = 1;
	}

int main(int argc, char **argv) {
  Fl_Double_Window* w;
  { Fl_Double_Window* o = progress = new Fl_Double_Window(460, 330, "Test Progress Bar");
    w = o;
    { Fl_Progress* o = progr = new Fl_Progress(115, 80, 240, 25);
      o->box(FL_SHADOW_BOX);
      o->selection_color((Fl_Color)4);
    }
    { Fl_Button* o = start_button = new Fl_Button(45, 220, 135, 25, "START");
      o->box(FL_SHADOW_BOX);
		o->callback((Fl_Callback*)cb_startb,(void*)"start");
    }
    { Fl_Button* o = stop_button = new Fl_Button(260, 220, 135, 25, "STOP");
      o->box(FL_SHADOW_BOX);
		o->callback((Fl_Callback*)cb_stopb,(void*)"stop");
    }
    o->end();
  }
  w->show(argc, argv);
  return Fl::run();
}
  #2  
Old 08-Sep-2006, 16:51
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 press needs recognition


Hello and Welcome to GIDForums™ azark. Code tags on the first post, have a cigar.

Enough of that rot...

While I haven't yet run throught your example I'll give you something that might lead you in the right direction.

CPP / C++ / C Code:
/**********************************************************************/
/* FILENAME:         clock_box.cpp                                    */
/* ORIGINATION DATE: 7/9/05                                           */
/* ORIGINATOR:       M Roth                                           */
/* LICENSE:          gpl ([url]www.gnu.org/copyleft/gpl.html[/url])              */
/**********************************************************************/
/*   Copyright (C)2004, 2005                                          */
/*   Mark Roth - mrothcode[at]gmail[dot]com                           */
/**********************************************************************/
/* clock_box is free software. You can redistribute it and/or modify  */
/* it under the terms of the GNU General Public License as published  */
/* by the Free Software Foundation, either version 2 of the License,  */
/* or (at your option) any later version.                             */
/* gct_data is distributed in the hope that it will be useful,        */
/* but WITHOUT ANY WARRANTY' without even the implied warranty of     */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      */
/* GNU General Public License for more details.                       */
/*                                                                    */
/* You should have received a copy of the GNU General Public License  */
/* along with clock_box. If not, write to :                           */
/*       the Free Software Foundation, Inc.                           */
/*       51 Franklin Street                                           */
/*       Fifth Floor                                                  */
/*       Boston, MA  02110-1301  USA                                  */
/*                                                                    */
/**********************************************************************/
/* DESCRIPTION:                                                       */
/* This is a clock widget for fltk 1.1.x                              */
/* It is an extension of a Fl_Box.  It will display the system time   */
/* in a box with the type set to FL_NO_BOX but you could change that  */
/* using the normal methods.  The only thing you can no longer do is  */
/* change the label as it has been made private.                      */
/**********************************************************************/

#include <FL/Fl.H>
#include <FL/Fl_Window.H>

#ifndef __CLOCKBOX_H__
#define __CLOCKBOX_H__

#include <FL/Fl_Box.H>
#include <ctime>
#include <cstring>


class ClockBox : public Fl_Box{
  public:
    ClockBox(int x, int y, int w, int h, const char* L="tick-tock") : Fl_Box(FL_NO_BOX,x,y,w,h,L) {
      Fl::add_timeout(1.0, update, this);
    };
    ~ClockBox(){};
  private:
    time_t mytime;
    struct tm* my_time;
    static char clock_text[30];
    
    void PutTime(){
      mytime = time(NULL);
      my_time = localtime(&mytime);
      strcpy(clock_text,asctime(my_time));
      this->label(clock_text);
      Fl::repeat_timeout(1.0, update, this);
    };
    static void update(void* user){
      ((ClockBox*)user)->PutTime();
    };
    void label(const char* L) { Fl_Widget::label(L);};
};

char ClockBox::clock_text[30];

#endif

int main (int argc, char *argv[]) {
    
  Fl_Window window(200,20,"class ClockBox");
    ClockBox my_clock(0,0,window.w(),window.h());

  window.show(argc, argv);
  
  return Fl::run();
}


Perhaps using a timeout to "hijack" the event loop would work for you?

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
Last edited by LuciWiz : 09-Sep-2006 at 05:43. Reason: Edited at member's request
  #3  
Old 09-Sep-2006, 04:57
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 press needs recognition


OK azark, here is a stripped down example of your code to show what I am talking about. Instead of just adding a constant value you could check what you are tracking for progress and set your value accordingly.

This will run until either the max value is reached or the stop button is pressed. It will remove the timeout and set the value of the progress bar back to zero if running. If the stop button is pressed the timeout is removed but the value is left untouched.

Now add the logic to track the progress of whatever you're tracking and you are on your way.

HTH,
Mark

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

Fl_Progress* progBar;

void runcount(void*)
{
  if (progBar->value() == progBar->maximum())
  {
    Fl::remove_timeout(runcount);
    progBar->value(0);
  }
  else
  {
    Fl::repeat_timeout(0.5, runcount);
    progBar->value(progBar->value() + 1);
  }
}

void cb_startb(void)
{
  Fl::add_timeout(0.5, runcount);
}

void cb_stopb(void)
{
  Fl::remove_timeout(runcount);
}


int main (int argc, char *argv[]) {

  Fl_Double_Window window(200,70,"ProgressBar Test");
    progBar = new Fl_Progress(5, 10, window.w()-10, 20);
      progBar->box(FL_SHADOW_BOX);
      progBar->selection_color((Fl_Color)4);
      progBar->minimum(0);
      progBar->maximum(10);

    Fl_Button* start_button = new Fl_Button(10, 40, 80, 20, "START");
      start_button->box(FL_SHADOW_BOX);
      start_button->callback((Fl_Callback*)cb_startb,(void*)"start");

    Fl_Button* stop_button = new Fl_Button(110, 40, 80, 20, "STOP");
      stop_button->box(FL_SHADOW_BOX);
      stop_button->callback((Fl_Callback*)cb_stopb,(void*)"stop");

  window.end();
  window.show(argc, argv);

  return Fl::run();
}

__________________
"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
  #4  
Old 09-Sep-2006, 11:32
azark azark is offline
New Member
 
Join Date: Sep 2006
Posts: 6
azark is on a distinguished road

Re: Button press needs recognition


Hi Mark,

Thank you for both of your examples and replies. The 2nd was most helpful and works just like I was expecting. I spent a lot of time last night fooling around with the add_timeout and repeat_timeout but still could not understand their operation too well. Documentation seems a little sparse. Your latest code certainly illustrates their operation much better. As a side note: I even tried the example in the programming manual under "add_timeout" that is supposed to output "TICK" but got no output results.

In my application of FLTK, I'm adapting a command-line interface program which does some unusual output on a serial port to an external device, in the process doing programming of flash and eeprom memory of a microcontroller chip. The timer and progress bar represented (for simplicity) a whole lot of C code that I now need to fashion into the scheme of things.

This is being done under Linux (Debian) and a version will be made for MS Windows use too -- hence the attractiveness of FLTK.

Have a good day and thanks for the pointers.

Don
in the North Arkansas Ozarks
  #5  
Old 10-Sep-2006, 03:12
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 press needs recognition


Glad to help. I tried to use your code in the second example as much as I could. Once I gave it a good read I thought that was what you were after.

It sounds like you may have more questions in the future. Good luck with your project and we'll see you at the next bottleneck.

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
  #6  
Old 14-Sep-2006, 04:51
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 press needs recognition


Quote:
Originally Posted by azark
...
As a side note: I even tried the example in the programming manual under "add_timeout" that is supposed to output "TICK" but got no output results.
...

Here is an updated example that will stay alive and wait for the "TICKs".

CPP / C++ / C Code:
#include <stdio.h>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>

void callback(void*)
{
  puts("TICK");
  Fl::repeat_timeout(1.0, callback);
}

int main()
{
  Fl_Window mywin(100,100,100,100,"testing");
  Fl::add_timeout(1.0, callback);
  mywin.show();
  return Fl::run();
}


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
 
 

Recent GIDBlogProblems with the Navy (Enlisted) 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
Change the caption on button when clicked drewdaman MS Visual C++ / MFC Forum 5 31-May-2009 20:56
Button class Spidy08 FLTK Forum 8 25-Aug-2006 01:52
Server Button Control execute twice bluebeta .NET Forum 2 12-Jun-2006 09:25
Group Round buttons (radio buttons) toaster FLTK Forum 1 03-Jan-2006 06:57
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:43.


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