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 16-May-2009, 12:04
meolic meolic is offline
New Member
 
Join Date: May 2009
Posts: 3
meolic is on a distinguished road

Simple program with many errors


Hi,

I am trying to prepare simple project for lab exercises (YES, we are trying to use fltk 1.1.9 in a course about C++ programming).

Well, I am new to this and I have problems with basic concepts. Here is a "should be simple" program which has some really bad erros, it doesn't even compile. And I am unable to prepare a correct version, Any suggestion will be of great help.

BTW: I have posted the same question also to fltk.org forum, please tell me, if you (GIDForums) do not like such flooding.

CPP / C++ / C Code:
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_JPEG_Image.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>

#include <iostream>
using namespace std;

class MainWindow : public Fl_Window {

  public:
  MainWindow() : Fl_Window(800,600,"My program") {}
};

class Background : Fl_Box {

  public:
  Background(Fl_JPEG_Image bgimg) : Fl_Box(0,0,800,530) {
    image(bgimg);
  }
};

class Toolbar {

  public:
  Toolbar() {
    Fl_Button buttonPlus(100,0,50,50,"+");
    buttonPlus.labelsize(50);
    buttonPlus.callback(cb_buttonPlus);

    Fl_Button buttonMinus(0,0,50,50,"-");
    buttonMinus.labelsize(50);
    buttonMinus.callback(cb_buttonMinus);
  }

  private:
  static void cb_buttonPlus(Fl_Widget *w, void *data) {
    cout << "PLUS" << endl;
  }

  static void cb_buttonMinus(Fl_Widget *w, void *data) {
    cout << "MINUS" << endl;
  }
};

int main() {
  Fl_JPEG_Image slovenija("slovenija-800x530.jpg");
  MainWindow mainWindow;
  mainWindow.begin();
  Background background(slovenija);
  Toolbar toolbar;
  mainWindow.end();
  mainWindow.show();
  return Fl::run();
}
Last edited by LuciWiz : 16-May-2009 at 13:23. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 21-May-2009, 07:22
meolic meolic is offline
New Member
 
Join Date: May 2009
Posts: 3
meolic is on a distinguished road

Re: Simple program with many errors


Hi again,

I found all (?) mistakes and improved the program. It ended with 110 line and was already served as a demonstration example. Quite successful!

The first error was in the foolowing line:

Background(Fl_JPEG_Image bgimg) : Fl_Box(0,0,800,530) {

There is no copy constructor for Fl_JPEG_Image, thus a reference should be used. This was compiler error.

The second error was in the line (and similar a few lines down):

Fl_Button buttonPlus(100,0,50,50,"+");

Created buttonPlus is a local variable for Toolbar::Toolbar(), thus I get
runtime error becuase it does not exist after the constructor finishes.

I am sending here the final code (well, I used a picture called slovenia.jpg, it can be any JPEG, just change the name in the code). Any comments or suggestions?

Best regards, Robert

CPP / C++ / C Code:
// [email]meolic@uni-mb.si[/email], 2009
// This is based on FLTK documentation
// Build with: fltk-config --use-images --compile "%f"
// Build with: g++ -Wall "%f" -lfltk -lfltk_images -lfltk_jpeg -lXext

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_JPEG_Image.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>

#include <iostream>
#include <string>

using namespace std;

void cbButtonPlus(Fl_Widget* w, void* data);
void cbButtonMinus(Fl_Widget* w, void* data);

class MainWindow : public Fl_Window {

  private:
  Fl_Image* imageOrg;
  Fl_Image* imageShown;
  Fl_Box workingBox;
  Fl_Button buttonPlus;
  Fl_Button buttonMinus;
  int zoom;

  public:
  MainWindow(string imageName, Fl_Image& thisImage);
  void zoomIn();
  void zoomOut();

  private:
  void showImage();
};

MainWindow::MainWindow(string imageName, Fl_Image& thisImage) :
    Fl_Window(800,600,imageName.c_str()),
    imageOrg(&thisImage),
    imageShown(0),
    workingBox(160,20,480,480),
    buttonPlus(350,550,50,50),
    buttonMinus(450,550,50,50),
    zoom(100)
{
  color(FL_BLACK); //window background

  begin();

  workingBox.box(FL_FLAT_BOX); //no border
  workingBox.color(FL_BLACK); //box background

  buttonPlus.label("+");
  buttonPlus.labelsize(50);
  buttonPlus.color(FL_YELLOW);
  buttonPlus.callback(cbButtonPlus);

  buttonMinus.label("-");
  buttonMinus.labelsize(50);
  buttonMinus.color(FL_YELLOW);
  buttonMinus.callback(cbButtonMinus);

  end();

  showImage();
}

void MainWindow::zoomIn() {
  if (zoom < 200) {
    zoom = zoom + 10;
    showImage();
  }
}

void MainWindow::zoomOut() {
  if (zoom > 10) {
    zoom = zoom - 10;
    showImage();
  }
}

void MainWindow::showImage() {
  delete(imageShown);
  imageShown = imageOrg->copy((480*zoom)/100,(480*zoom)/100);
  workingBox.image(*imageShown);
  workingBox.redraw();
}

int main() {
  Fl::visual(FL_DOUBLE|FL_INDEX); //recommended initialization
  string imageName = "slovenia.jpg";
  Fl_JPEG_Image image(imageName.c_str());
  MainWindow mainWindow(imageName,image);
  mainWindow.show();
  return Fl::run();
}

// Callback for buttonPlus
void cbButtonPlus(Fl_Widget* w, void* data) {
    MainWindow* mainWindow = ((MainWindow*) w->parent());
    mainWindow->zoomIn();
}

// Callback for button Minus
void cbButtonMinus(Fl_Widget* w, void* data) {
    MainWindow* mainWindow = ((MainWindow*) w->parent());
    mainWindow->zoomOut();
}
  #3  
Old 08-Jun-2009, 01:56
chandrashekhar chandrashekhar is offline
New Member
 
Join Date: Jun 2009
Posts: 6
chandrashekhar is on a distinguished road

Re: Simple program with many errors


Hey how do you compile it?

I tried to compile like this:
gcc -lfltk test2.cxx -o test2

but it's saying:

/tmp/cctxceop.o: In function `main':
test2.cxx:(.text+0x537): undefined reference to `Fl_JPEG_Image::Fl_JPEG_Image(char const*)'
collect2: ld returned 1 exit status


Can you give me the complete compile command?
  #4  
Old 08-Jun-2009, 02:17
chandrashekhar chandrashekhar is offline
New Member
 
Join Date: Jun 2009
Posts: 6
chandrashekhar is on a distinguished road

Re: Simple program with many errors


I tried to put the callback inside the class Main_window...but could not succeed...it's throwing a lot of errors.....can you tell me how to do that....it would be very much like java...
  #5  
Old 08-Jun-2009, 03:06
meolic meolic is offline
New Member
 
Join Date: May 2009
Posts: 3
meolic is on a distinguished road

Re: Simple program with many errors


Hi,

Compiler parameters depend on system and instalation method. For example:

1. On Linux you can use fltk-config which is a nice script:
fltk-config --use-images --compile test.cxx

2. On Linux/Ubuntu 9.04 + FLTK from repository the following line is working for me:
g++ -Wall test.cxx -lfltk -lfltk_images

3. On Linux/Ubuntu old + FLTK compiled from sources I am using:
g++ -Wall test.cxx -lfltk -lfltk_images -lfltk_jpeg -lXext

4. To compile on Windows + VC++ you must follow some instructions, e.g. from a book "Bjarne Stroustrup: Programming -- Principles and Practice Using C++".

Quote:
Originally Posted by chandrashekhar
I tried to put the callback inside the class Main_window...but could not succeed...it's throwing a lot of errors.....can you tell me how to do that....it would be very much like java...

Callback functions cannot be a class member. There will be a problem with types. An explanation for this is given here (but it is not an easy explanation):

http://www.parashift.com/c++-faq-lite/pointers-to-members.html

(Why I cannot post the complete link?? Search for "Pointers to member functions")

I like Java, too. But do not try to use to much Java style in C++ programs.
Going into details, C++ is a very different language!

Robert
  #6  
Old 08-Jun-2009, 04:25
chandrashekhar chandrashekhar is offline
New Member
 
Join Date: Jun 2009
Posts: 6
chandrashekhar is on a distinguished road

Re: Simple program with many errors


Thanks a lot for ur help...it compiled and ran perfectly....also ur explanation on not having callbacks as class members is quite satisfactory...
  #7  
Old 08-Jun-2009, 04:48
chandrashekhar chandrashekhar is offline
New Member
 
Join Date: Jun 2009
Posts: 6
chandrashekhar is on a distinguished road

Re: Simple program with many errors


Hey I succeeded in putting the callbacks inside the class......
Here's the code.......

CPP / C++ / C Code:
// [email]meolic@uni-mb.si[/email], 2009
// This is based on FLTK documentation
// Build with: fltk-config --use-images --compile "%f"
// Build with: g++ -Wall "%f" -lfltk -lfltk_images -lfltk_jpeg -lXext

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_JPEG_Image.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>

#include <iostream>
#include <string>

using namespace std;


class MainWindow : public Fl_Window {

  private:
  Fl_Image* imageOrg;
  Fl_Image* imageShown;
  Fl_Box workingBox;
  Fl_Button buttonPlus;
  Fl_Button buttonMinus;
  int zoom;

  public:
  MainWindow(string imageName, Fl_Image& thisImage);
  void zoomIn();
  void zoomOut();

  private:
  static void cbButtonPlus(Fl_Widget* w, void* data);
  static void cbButtonMinus(Fl_Widget* w, void* data);
  void showImage();
};

MainWindow::MainWindow(string imageName, Fl_Image& thisImage) :
    Fl_Window(800,600,imageName.c_str()),
    imageOrg(&thisImage),
    imageShown(0),
    workingBox(160,20,480,480),
    buttonPlus(350,550,50,50),
    buttonMinus(450,550,50,50),
    zoom(100)
{
  color(FL_BLACK); //window background

  begin();

  workingBox.box(FL_FLAT_BOX); //no border
  workingBox.color(FL_BLACK); //box background

  buttonPlus.label("+");
  buttonPlus.labelsize(50);
  buttonPlus.color(FL_YELLOW);
  buttonPlus.callback(this->cbButtonPlus);

  buttonMinus.label("-");
  buttonMinus.labelsize(50);
  buttonMinus.color(FL_YELLOW);
  buttonMinus.callback(cbButtonMinus);

  end();

  showImage();
}

void MainWindow::zoomIn() {
  if (zoom < 200) {
    zoom = zoom + 10;
    showImage();
  }
}

void MainWindow::zoomOut() {
  if (zoom > 10) {
    zoom = zoom - 10;
    showImage();
  }
}

void MainWindow::showImage() {
  delete(imageShown);
  imageShown = imageOrg->copy((480*zoom)/100,(480*zoom)/100);
  workingBox.image(*imageShown);
  workingBox.redraw();
}

int main() {
  Fl::visual(FL_DOUBLE|FL_INDEX); //recommended initialization
  string imageName = "slovenia.jpg";
  Fl_JPEG_Image image(imageName.c_str());
  MainWindow mainWindow(imageName,image);
  mainWindow.show();
  return Fl::run();
}

// Callback for buttonPlus
void MainWindow::cbButtonPlus(Fl_Widget* w, void* data) {
    MainWindow* mainWindow = ((MainWindow*) w->parent());
    mainWindow->zoomIn();
}

// Callback for button Minus
void MainWindow::cbButtonMinus(Fl_Widget* w, void* data) {
    MainWindow* mainWindow = ((MainWindow*) w->parent());
    mainWindow->zoomOut();
}

 
 

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
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Please help me to convert a simple C++ program into an object-oriented one. zekesteer C++ Forum 5 05-Jan-2008 11:05
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
i need help with a simple program!! alfie27 C++ Forum 1 06-Oct-2006 22:29

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

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


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