 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
Jamiil Familiar Face
Joined: 22 Sep 2005 Posts: 20
|
Posted: Thu Sep 22, 2005 12:56 pm Post subject: callback function |
|
|
Hi folks!
I have been racking my brains trying to find informatio about writing a callback function that receives two parameters one that indicates the type of icon I want displayed and the other a character string variable that holds the different messages this callback function will pass to a a dialogbox.
This is what the callback function looks like, or I think it would look like:
typedef struct{
gchar* str; // Message
GtkWidget* obj; // Icon
}DATA;
void GeneralMsg(DATA&);
void GeneralMsg(DATA& d){
// Create and display a message box with the information in the DATA variable
.....
}
Is this possible?
TIA |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 376 Location: State College, Pennsylvania
|
Posted: Thu Sep 22, 2005 4:00 pm Post subject: |
|
|
Are you trying to create a new signal/callback or use a specific one? If you are using an existing signal/callback, which one do you want to use?
You can reference them at http://www.gtk.org/api/2.6/gtk/index.html if you want... |
|
| Back to top |
|
 |
Jamiil Familiar Face
Joined: 22 Sep 2005 Posts: 20
|
Posted: Thu Sep 22, 2005 8:02 pm Post subject: Thanks Openldev. |
|
|
Well, firsto of all I'd like to thank you for your response, and second of all... No, I am not trying to create a new signal.
What I am trying to do, though, is to find out if it is possible to have a callback function, that will accept a number of parameters that will determin the look, and why not, the feel of the message box that this callback function will displayed.
This same methodology will be used when the user presses a button and to the callback function information would be passed, so that the new window do something with it.
Right now, all I can do is write a new callback function for each of the different messages that I want to displayed to the users, which is kind of a drag... you know what I mean.
It would be nice if I can write one callback function and pass to it the data I want to displayed, is this possible?
Thanks in advance
Reply with quote
New post
Are you trying to create a new signal/callback or use a specific one? If you are using an existing signal/callback, which one do you want to use?
You can reference them at http://www.gtk.org/api/2.6/gtk/index.html if you want.. |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 376 Location: State College, Pennsylvania
|
Posted: Thu Sep 22, 2005 8:11 pm Post subject: |
|
|
Ok, I think I know what you're trying to do.
Basically, first you want to connect the signal using:
g_signal_connect (G_OBJECT(button), "clicked", G_CALLBACK(on_button_pressed), (gpointer)data);
In this connection, "data" is your DATA structure you want to pass to your callback.
Then use for your callback:
void on_button_pressed (GtkButton *button, gpointer data)
{
DATA *ptr = (DATA*) data;
.....
}
This callback function casts the gpointer back to a DATA structure. Then you can use your "ptr->str" or whatever from the structure. I know there's other ways of doing this, but this is the most efficient way that I have found.
I hope this answers your question! |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 376 Location: State College, Pennsylvania
|
Posted: Thu Sep 22, 2005 8:13 pm Post subject: |
|
|
| Oh, and one more thing. I casted it because I was assuming DATA was a pointer. You can make it a non-pointer, but I always find sending data around that needs recasted constantly easier to use as pointers... :) |
|
| Back to top |
|
 |
Jamiil Familiar Face
Joined: 22 Sep 2005 Posts: 20
|
Posted: Sat Sep 24, 2005 12:37 am Post subject: Wow! |
|
|
You've come as a blessing from above.
I have not tried your suggestion, but I can tell that you understand my predicament.
One last question.
Thanks soooo much. |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 376 Location: State College, Pennsylvania
|
Posted: Sat Sep 24, 2005 2:05 am Post subject: |
|
|
| Honesly, it's really no problem. I am so excited about this forum because when I was first learning Gtk I had so much trouble at first, but there were a ton of people that helped me out. Now I figure it is my turn to give back. The joys of Open Source... :) |
|
| Back to top |
|
 |
Jamiil Familiar Face
Joined: 22 Sep 2005 Posts: 20
|
Posted: Sat Sep 24, 2005 2:15 am Post subject: thanks openldev |
|
|
OK, I did as you suggested, but I get an error message saying that 'data of type DATA could not be converted to void*'
Any ideas?
TIA |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 376 Location: State College, Pennsylvania
|
Posted: Sat Sep 24, 2005 2:19 am Post subject: |
|
|
| Can you post the code you are using? |
|
| Back to top |
|
 |
Jamiil Familiar Face
Joined: 22 Sep 2005 Posts: 20
|
Posted: Sat Sep 24, 2005 2:48 am Post subject: Just an update |
|
|
Instead of using the 'C' casting operator I use reinterpret_cast operator and it worked, I for got to mention that I am using GTK+ as the front end of an application that was written in C++.
OK, now let's see if I can get my GTK+ programming going.
Thanks!! :o |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 376 Location: State College, Pennsylvania
|
Posted: Sat Sep 24, 2005 2:53 am Post subject: |
|
|
| Yes, that is actually what I do in OpenLDev. The only bump in the road I found with using c++ with Gtk+ is that you have to make callback functions private friends if you want them to have access to a specific class that you send to it with the gpointer. Good luck! :) |
|
| Back to top |
|
 |
Jamiil Familiar Face
Joined: 22 Sep 2005 Posts: 20
|
Posted: Sun Sep 25, 2005 4:22 pm Post subject: Tried it, did not work, HEEEEELLP |
|
|
I tried to provide as little code as possible, but to make what my intentions are clearer I had to add quite a bit, please bear with me…
This program runs, but when click on any button, with the exception of the “Exit” button, I get a segmentation fault error. I really don’t know why, but what I want to do is display a message-box with some data when the user presses any of the buttons, aside from the “Exit” button.
Can you help?
TIA
-- messae.hpp --
#include <string>
#include <iostream>
#include <gtk/gtk.h>
#ifndef GTK_MESSAGE_HPP
#define GTK_MESSAGE_HPP
typedef struct{
gchar* str;
GtkWidget* obj;
}DATA;
void GeneralMsg(GtkButton* button, gpointer d);
#endif
-- end of file --
-- message.cpp --
#ifndef GTK_MESSAGE_HPP
#include "Message.hpp"
#endif
void GeneralMsg(GtkButton* button, gpointer p) {
DATA* d;
reinterpret_cast<DATA*>(p);
GtkWidget* label;
GtkWidget* dialog;
try {
dialog = new GtkWidget;
label = new GtkWidget;
d = new DATA;
} catch(std::bad_alloc& e) {
std::cout << e.what() << std::endl;
}
dialog = gtk_message_dialog_new(
reinterpret_cast<GtkWindow*>(d->obj), //Parent
static_cast<GtkDialogFlags>(
GTK_DIALOG_MODAL |
GTK_DIALOG_DESTROY_WITH_PARENT),//Flags
reinterpret_cast<GtkMessageType>(
GTK_MESSAGE_WARNING), // Message type
reinterpret_cast<GtkButtonsType>(
GTK_BUTTONS_OK), //Button type
d->str); //Message format
// Destroy the dialog when the user responds to it (e.g. clicks a button)
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
delete d;
delete dialog;
delete label;
}
-- eof --
--- main.hpp ---
//Declare the objects
GtkWidget* MainMenueWin;
GtkWidget* btnCreateAccount;
.....
DATA* data;
void Quit(GtkButton* , gpointer);
gboolean SystemQuit(GtkWidget*, GdkEvent*, gpointer);
//Callbacks
void Quit(GtkButton* was_clicked, gpointer user_data){
gtk_main_quit();
}
gboolean SystemQuit(GtkWidget* widget, GdkEvent* event, gpointer user_data){
gtk_widget_destroy(widget);
gtk_main_quit();
return(TRUE);
}
#endif
-- end of main.hpp --
--- main.cpp ---
#include <gtk/gtk.h>
#include <glib-object.h>
#include <string>
#include <iostream>
#ifndef GTK_MESSAGE_HPP
#include "Message.hpp"
#endif
#include "main.hpp"
int main(int argc, char *argv[]){
try{data = new DATA;}
catch(std::bad_alloc& e){std::cout << e.what() << std::endl;}
//Assign data to the object passased to the callback function
data->str = g_strdup("Under Comstruction ... I am very sorry, but this \
feature has not been implemented yet. Please check \
my email me for any new updates on this program \
El-Mago@hotmail.com");
data->obj = MainMenueWin;
// 1. Initialize the environment
gtk_set_locale ();
gtk_init(&argc, &argv);
//-liconv -liconv
// 2. Create Widgets
MainMenueWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
VBx = gtk_vbox_new(FALSE,10);
Label = gtk_label_new("Jaime");
Separator = gtk_hseparator_new();
btnCreateAccount = gtk_button_new_with_label("Create");
...
btn5 = gtk_button_new_with_label("Unknown");
btnExit = gtk_button_new_with_label("Exit");
// 3. Set attributs
gtk_window_set_title(GTK_WINDOW(MainMenueWin), "Jaime");
gtk_window_set_policy(GTK_WINDOW(MainMenueWin), //which_window
FALSE, // allow_shrink
FALSE, // allow_grow
FALSE); // auto_shrink
gtk_container_set_border_width(GTK_CONTAINER(MainMenueWin),15);
gtk_widget_set_name(MainMenueWin,"Gtk++ One, Dos, Tres");
gtk_widget_set_name(VBx,"vertical box");
gtk_widget_set_name(Label,"Label");
gtk_widget_set_name(Separator, "Separator");
// 4. Register CallBacks
// These three callback fun' are only to exit the application
g_signal_connect( GTK_OBJECT(btnExit), "clicked",
G_CALLBACK(Quit), NULL);
g_signal_connect( GTK_OBJECT(MainMenueWin), "delete-event",
GTK_SIGNAL_FUNC(SystemQuit), NULL);
g_signal_connect(GTK_OBJECT(MainMenueWin), "destroy",
G_CALLBACK(SystemQuit), NULL);
g_signal_connect(GTK_OBJECT(btnCreateAccount),
"clicked",
G_CALLBACK(GeneralMsg),
reinterpret_cast<gpointer>(&data));
return 0;
}
* You cannot exercise your power to a point of humiliation.
- Jean Chretien
* It's amazing how the small seeds of distrust and misunderstanding
can yield a crop of hate and death...
Ma'assalama! Adiós! bye! |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 376 Location: State College, Pennsylvania
|
Posted: Sun Sep 25, 2005 4:42 pm Post subject: |
|
|
| I believe someone already answered your question on the Gtk-list... |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|