LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-19-2009, 02:41 AM   #1
Aidin_36
LQ Newbie
 
Registered: Jun 2005
Location: Iran
Distribution: Debian
Posts: 24

Rep: Reputation: 15
help with `gtkmm' needed


I'm trying to write a GUI for one of my projects using `gtkmm'. I'm not a GUI programmer, so I've got some problems with this. Let me explain more:

The program itself provide a TCP/IP connection for the GUIs, and every GUI should connect to it via this protocol. Then, program send them information about objects, and GUI draw them on the screen. I use a `drawable area' widget to handle this.

I put all of the code about connecting to the server, getting information and updating the drawable area into the `on_expose_event'. But it dosen't works correctly.

If I had an option to put whole GUI code (drawing into screen) on a separate class, and then call it whenever a update required, every thing would be OK. But the only way I know for creating a GUI is the one explained in the gtkmm's tutorials: to create a Gtk::Main and call .run()

Here is the part of the code:

Main.cpp:
Code:
int main(){
   Gtk::Main kit(argc, argv);
   Gtk::Window win;

   Drawer d;
   win.add(d);
   d.show();

   Gtk::Main::run(win);
}
Drawer class:
Code:
class Drawer : public Gtk::DrawingArea {

public:
   Drawer();
   virtual ~Drawer();

protected:
   //Override default signal handler:
   virtual bool on_expose_event(GdkEventExpose* event);
}
And in the `on_expose_event' are the codes about connecting to the server using sockets, and so on. Anyone have any suggestion?
 
Old 03-19-2009, 11:50 AM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
Your event handlers can't be part of a C++ class. In fact, you have to link them as C code since this is what gtk expects.

Best thing to do is put event handlers in their own file, and compile that file as a C file, and refer to it from C++ using the extern "C" macro.

Thus, create a file sighandlers.c which contains, for instance,
Code:
G_MODULE_EXPORT void on_drawingarea1_expose_event( GtkWidget *widget, GdkEventExpose *event )
{
	drawarea_expose_event(widget,event);
}
Then in the sighandlers.h file, put this code:
Code:
 extern "C" {
	void on_drawingarea1_expose_event( GtkWidget *widget, GdkEventExpose *event );
}
and include sighandlers.h in your main file.

I should note that you'll probably also need an on_configure_event handler as well as the on_expose_event handler for your drawing area updates.

Now, in the particular example I just gave, the function drawarea_expose_event() is another C function, kept in another C file (which I call cmd_button_handlers.c), and it looks like this:

Code:
gboolean drawarea_expose_event(GtkWidget *widget, GdkEventExpose *event)
{
	draw_drawingarea_drawable(widget,event);
	return FALSE;
}
and draw_drawingarea_drawable() is a routine kept in a C++ file that looks like this:

Code:
void draw_drawingarea_drawable(GtkWidget *object,GdkEventExpose *event)
{
	drawing_area* da=get_drawing_area_object(object);
	da->draw_drawable(event);
}
which is obviously C++ code referring to the drawing_area class which I wrote for this application.

When you make this, you need something that looks like this (only a partial makefile):

Code:
GTKLIBS= -export-dynamic -lgthread-2.0 `pkg-config --libs gtkmm-2.4` `pkg-config --libs gtk+-2.0`

OBJFILES= commander.o cmd_parser.o main_window.o requesters.o requester_base.o sighandlers.o tcp_connect.o cmd_button_handlers.o commander_classes.o drawing_area.o libunp.a

commander: $(OBJFILES) 
	$(CXX) -o commander $(GTKLIBS) $(OBJFILES)

sighandlers.o:
	$(CC) -o $@ $(CFLAGS) -c sighandlers.c

tcp_connect.o:
	$(CC) -o $@ $(CFLAGS) -c tcp_connect.c

cmd_button_handlers.o
	$(CC) -o $@ $(CFLAGS) -c cmd_button_handlers.c

Last edited by jiml8; 03-19-2009 at 11:51 AM.
 
Old 03-19-2009, 12:00 PM   #3
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
Oh, also...

You really should investigate glade for writing a gui using gtk. It makes life a lot simpler.
 
Old 03-20-2009, 01:11 AM   #4
Aidin_36
LQ Newbie
 
Registered: Jun 2005
Location: Iran
Distribution: Debian
Posts: 24

Original Poster
Rep: Reputation: 15
Thanks for you complete answer!
I will try that as soon as I get back home!
 
  


Reply

Tags
gtkmm


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gtkmm strycnine Slackware 6 12-12-2007 04:46 PM
GTKmm and SigC stonehurstX11 Programming 4 05-04-2005 03:17 PM
gtkmm help paulr1984 Linux - Software 1 03-28-2005 03:40 AM
GTKmm Help gtkmike Programming 10 09-22-2004 09:32 AM
gtkmm HOW? Barq Programming 1 08-29-2004 08:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration