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 04-Nov-2004, 18:31
podarok's Avatar
podarok podarok is offline
New Member
 
Join Date: Nov 2004
Location: Ukraine, Київ
Posts: 23
podarok is on a distinguished road
Lightbulb

log window in fltk


I try to make app, that use log window in itself, and I want to use fltk::Browser for adding new messages, but it can`t autoscroll to the end of list?
Has anyone a code for example for do such log window?
  #2  
Old 07-Nov-2004, 09:42
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello podarok. Welcome to GIDForums™. I apologize for the delay, but I have always wanted to investigate something like this and this gave me the opportunity .

Unfortunately, the code I am giving is linux specific, but could be modified for windows if someone so desires. The linux specific stuff is sleep (defined in unistd.h) and the pthread stuff (pthread.h).

I am using threads so that I can have a seperate calculation loop and in the meantime have the dialog be responsive to my wishes.

CPP / C++ / C Code:
#include <stdlib.h>		//For exit call
#include <unistd.h>		//For sleep call
#include <stdio.h>		//For sprintf call
#include <pthread.h>	//For threaded call

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Light_Button.H>
#include <FL/Fl_Browser.H>

//I use a structure to hold all of the fltk stuff, so that it can be passed in one argument.
//This comes in handy for the call-backs & also avoids the UGLY use of globals!
typedef struct{
	Fl_Double_Window*	wd_dialog;
	Fl_Double_Window*	wd_log;

	Fl_Button*	bt_compute;
	Fl_Button*	bt_log;
	Fl_Button*	bt_exit;
	
	Fl_Browser*	ot_log;
}button_dialog;


//Compute process - This is run as a seperate thread.
void* compute(void* data)
{
	button_dialog*	d = (button_dialog*) data;
	int		x;
	char	string[50];
	
	for(x=0;x<100;x++){
		//This checks the value of the light button.  Calculation stops when this is off.
		while(!d->bt_compute->value())
			usleep(1000);
		sprintf(string,"Calculation step #%d",x);
		d->ot_log->add(string);				//Add line to the bottom of the log window
		d->ot_log->bottomline(x+1);			//Make window scroll to the last line (line count starts at 1 thus the x+1
		Fl::check();						//Force immediate redraw
		sleep(1);
	}
	pthread_exit(0);
}


//Exit callback - Easy...
void cb_exit(Fl_Widget*, void* data)
{
	exit(0);
}

//Simply shows or hides the log window
void cb_log(Fl_Widget*, void* data)
{
	button_dialog* d = (button_dialog*) data;	
	
	if(d->bt_log->value() == 1)
		d->wd_log->show();
	else
		d->wd_log->hide();
}


//Makes the fltk stuff.
button_dialog*	make_button_dialog(){
	button_dialog*	d = new button_dialog;
	
	d->wd_dialog = new Fl_Double_Window(200,200,200,115,"Dialog Window Example");
	
	d->bt_compute = new Fl_Light_Button(60,10,80,25,"Compute");
	d->bt_log = new Fl_Light_Button(60,45,80,25,"Log");
	d->bt_log->callback(cb_log,d);
	d->bt_exit = new Fl_Button(60,80,80,25,"Exit");
	d->bt_exit->callback(cb_exit,d);
	d->wd_dialog->end();
	
	d->wd_log= new Fl_Double_Window(400,200,200,200,"Log Window");
	d->ot_log = new Fl_Browser(5,5,190,190);
	d->wd_log->end();
	
	return d;
	
}

int main(int argc, char **argv)
{
	button_dialog	*d;
	pthread_t*	comp_thread = new pthread_t;

	d = make_button_dialog();

	//This makes a seperate thread for the calculation that is removed from the dialog stuff.
	pthread_create(comp_thread,0,compute,(void*)d);
	
	d->wd_dialog->show();

	return Fl::run();
} 
This is compiled under gcc with the following command (substiting in the appropriate source file, etc.):
Code:
gcc info.cpp `fltk-config --ldflags` -lpthread -o info

I have tried to document the code, so you can see what each call is for (that may not be immediately obvious.

Hope this helps!
  #3  
Old 08-Nov-2004, 02:32
podarok's Avatar
podarok podarok is offline
New Member
 
Join Date: Nov 2004
Location: Ukraine, Київ
Posts: 23
podarok is on a distinguished road
Quote:
Originally Posted by dsmith
Hello podarok. Welcome to GIDForums™.
tnx
Quote:
Originally Posted by dsmith
Unfortunately, the code I am giving is linux specific, but could be modified for windows if someone so desires. The linux specific stuff is sleep (defined in unistd.h) and the pthread stuff (pthread.h).

I am using threads so that I can have a seperate calculation loop and in the meantime have the dialog be responsive to my wishes.
nevermind, I use FreeBSD, I think code will easy ported by me ...
Quote:
Originally Posted by dsmith
d->ot_log->bottomline(x+1); //Make window scroll to the last line (line count starts at 1 thus the x+1
Fl::check(); //Force immediate redraw

hm... I think so too... But I don`t sure about speed of "Make window scroll". When the window refresh? When add() or bottomline() or check() ? if chech() - it is very well...

Quote:
Originally Posted by dsmith
I have tried to document the code, so you can see what each call is for (that may not be immediately obvious.

Hope this helps!
I hope too... tnx
  #4  
Old 08-Nov-2004, 08:25
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by podarok
hm... I think so too... But I don`t sure about speed of "Make window scroll". When the window refresh? When add() or bottomline() or check() ? if chech() -

FLTK seems to be so lightweight that I don't feel that you take a major hit in any of the GUI stuff. While my sample program is not very robust, there is absolutely no feeling of stutter in that call. The window is forced to refresh at each computation loop by Fl::check. This is called after the add and the bottomline call. Not sure if that answers your question though...
  #5  
Old 08-Nov-2004, 08:51
podarok's Avatar
podarok podarok is offline
New Member
 
Join Date: Nov 2004
Location: Ukraine, Київ
Posts: 23
podarok is on a distinguished road
Quote:
Originally Posted by dsmith
FLTK seems to be so lightweight that I don't feel that you take a major hit in any of the GUI stuff. While my sample program is not very robust, there is absolutely no feeling of stutter in that call. The window is forced to refresh at each computation loop by Fl::check. This is called after the add and the bottomline call. Not sure if that answers your question though...
ok, today possible I`ll try it on a large log window and try to check redraw speed... tnx btw
  #6  
Old 11-May-2005, 03:23
podarok's Avatar
podarok podarok is offline
New Member
 
Join Date: Nov 2004
Location: Ukraine, Київ
Posts: 23
podarok is on a distinguished road
Lightbulb

simple as always 8)


CPP / C++ / C Code:
// generated by Fast Light User Interface Designer (fluid) version 1.0106

#include "console.h"
char x;

Fl_Double_Window *chdd_console=(Fl_Double_Window *)0;

Fl_Input *chdd_console_command=(Fl_Input *)0;

static void cb_chdd_console_command(Fl_Input*, void*) {
  chdd_console_process->position(chdd_console_process->size());
chdd_console_process->insert(chdd_console_command->value());
chdd_console_process->insert("\n");
chdd_console_command->value("");
}

Fl_Output *chdd_console_process=(Fl_Output *)0;

static void cb_(Fl_Button*, void*) {
  chdd_console_command->value("Executed");
}

static void cb_X(Fl_Button*, void*) {
  chdd_console_command->value("");
}

static void cb_X1(Fl_Button*, void*) {
  chdd_console_process->value("");
}

int main(int argc, char **argv) {
  Fl_Double_Window* w;
  { Fl_Double_Window* o = chdd_console = new Fl_Double_Window(540, 427, "chdd::console");
    w = o;
    o->labelsize(11);
    { Fl_Input* o = chdd_console_command = new Fl_Input(10, 290, 520, 110, "command:");
      o->type(4);
      o->box(FL_BORDER_BOX);
      o->color(FL_FOREGROUND_COLOR);
      o->labelsize(11);
      o->textsize(11);
      o->textcolor(6);
      o->callback((Fl_Callback*)cb_chdd_console_command);
      o->align(FL_ALIGN_TOP_LEFT);
      o->when(FL_WHEN_ENTER_KEY);
    }
    { Fl_Output* o = chdd_console_process = new Fl_Output(10, 25, 520, 230, "process:");
      o->type(12);
      o->box(FL_BORDER_BOX);
      o->color(FL_GRAY0);
      o->labelsize(11);
      o->textsize(11);
      o->textcolor(2);
      o->align(FL_ALIGN_TOP_LEFT);
      Fl_Group::current()->resizable(o);
    }
    { Fl_Button* o = new Fl_Button(390, 405, 25, 20, "@->");
      o->box(FL_THIN_UP_BOX);
      o->shortcut(0xcff0d);
      o->labelcolor((Fl_Color)1);
      o->callback((Fl_Callback*)cb_);
    }
    { Fl_Button* o = new Fl_Button(425, 405, 25, 20, "X");
      o->box(FL_THIN_UP_BOX);
      o->labelfont(1);
      o->labelsize(11);
      o->labelcolor((Fl_Color)1);
      o->callback((Fl_Callback*)cb_X);
    }
    { Fl_Button* o = new Fl_Button(425, 260, 25, 20, "X");
      o->box(FL_THIN_UP_BOX);
      o->labelfont(1);
      o->labelsize(11);
      o->labelcolor((Fl_Color)1);
      o->callback((Fl_Callback*)cb_X1);
    }
    o->end();
  }
  w->show(argc, argv);
  return Fl::run();
}


////////console.h

// generated by Fast Light User Interface Designer (fluid) version 1.0106

#ifndef console_h
#define console_h
#include <FL/Fl.H>
extern char x;
#include <FL/Fl_Double_Window.H>
extern Fl_Double_Window *chdd_console;
#include <FL/Fl_Input.H>
extern Fl_Input *chdd_console_command;
#include <FL/Fl_Output.H>
extern Fl_Output *chdd_console_process;
#include <FL/Fl_Button.H>
#endif

it is much better and faster log window
Last edited by LuciWiz : 12-May-2005 at 09:24. Reason: Please insert your C++ code between [c++] & [/c++] tags
 
 

Recent GIDBlogAccepted for Ph.D. program 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
FLTK && fluid In Motion cable_guy_67 FLTK Forum 4 20-Mar-2008 04:52
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 16:41
Proper way to close a window? dsmith FLTK Forum 5 13-Jul-2005 16:56
FLTK with GLUT usmsci FLTK Forum 4 26-Nov-2004 17:21
Welcome to the FLTK Forums dsmith FLTK Forum 0 08-Sep-2004 07:58

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

All times are GMT -6. The time now is 20:42.


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