Click to See Complete Forum and Search --> : X Programming. What Lib? qt? gtk?
zeroth
09-20-2005, 02:49 PM
I'm interested in starting to program for X, but I'm not sure what library to use. The most common one I hear of is GTK, which is used by gaim and I have that installed on my win98, win2k, and linux partitions. where should I start? what is the most practical library to start with? I hear programming in X is quite difficult, and thats coming from John Carmack!
anyhow, I know C very well. I'm not a big fan of C++ however. I've never coded anything with Windows API except for a game engine I was working on, but the only API it used was to open a window. I'm fairly farmiliar with OpenGL, though its not really aplicable here. I've also used random Win32 API in VB6 apps, but thats not saying much.
I'm anxious to hear what you all suggest!
asarch
09-20-2005, 02:56 PM
You DEFINITIVELY should try GTK+ (http://www.gtk.org/tutorial/):
#include <gtk/gtk.h>
/* This is a callback function. The data arguments are ignored
* in this example. More on callbacks below. */
static void hello( GtkWidget *widget,
gpointer data )
{
g_print ("Hello World\n");
}
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
/* If you return FALSE in the "delete_event" signal handler,
* GTK will emit the "destroy" signal. Returning TRUE means
* you don't want the window to be destroyed.
* This is useful for popping up 'are you sure you want to quit?'
* type dialogs. */
g_print ("delete event occurred\n");
/* Change TRUE to FALSE and the main window will be destroyed with
* a "delete_event". */
return TRUE;
}
/* Another callback */
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
int main( int argc,
char *argv[] )
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window;
GtkWidget *button;
/* This is called in all GTK applications. Arguments are parsed
* from the command line and are returned to the application. */
gtk_init (&argc, &argv);
/* create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/* When the window is given the "delete_event" signal (this is given
* by the window manager, usually by the "close" option, or on the
* titlebar), we ask it to call the delete_event () function
* as defined above. The data passed to the callback
* function is NULL and is ignored in the callback function. */
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (delete_event), NULL);
/* Here we connect the "destroy" event to a signal handler.
* This event occurs when we call gtk_widget_destroy() on the window,
* or if we return FALSE in the "delete_event" callback. */
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (destroy), NULL);
/* Sets the border width of the window. */
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
/* Creates a new button with the label "Hello World". */
button = gtk_button_new_with_label ("Hello World");
/* When the button receives the "clicked" signal, it will call the
* function hello() passing it NULL as its argument. The hello()
* function is defined above. */
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (hello), NULL);
/* This will cause the window to be destroyed by calling
* gtk_widget_destroy(window) when "clicked". Again, the destroy
* signal could come from here, or the window manager. */
g_signal_connect_swapped (G_OBJECT (button), "clicked",
G_CALLBACK (gtk_widget_destroy),
G_OBJECT (window));
/* This packs the button into the window (a gtk container). */
gtk_container_add (GTK_CONTAINER (window), button);
/* The final step is to display this newly created widget. */
gtk_widget_show (button);
/* and the window */
gtk_widget_show (window);
/* All GTK applications must have a gtk_main(). Control ends here
* and waits for an event to occur (like a key press or
* mouse event). */
gtk_main ();
return 0;
}and compile this code with:
[$] gcc -Wall -g helloworld.c -o helloworld `pkg-config --cflags gtk+-2.0` \
`pkg-config --libs gtk+-2.0`
asarch
09-20-2005, 02:59 PM
...besides, design an interface with Glade (http://glade.gnome.org/) is a REAL PLACER for me...
asarch
09-20-2005, 03:03 PM
...and design an icon with Inkscape (http://www.inkscape.org/)...
bwkaz
09-20-2005, 07:10 PM
As far as this:
I hear programming in X is quite difficult, and thats coming from John Carmack! goes, he's right, but he's talking about Xlib, not Gtk+ or Qt.
BTW: I'm fairly sure Qt is for C++ only. Gtk+ has a C++ wrapper (called gtkmm, or gtk--), but it's optional. I don't believe you can use Qt from C code, although maybe that changed between Qt 3.1 and now.
(Plus, I think Gtk+ looks a lot nicer, although again, that may have changed in Qt since version 3.1.)
Anyway, I'd echo asarch's suggestion of using Glade to design the UI, too.
cybertron
09-20-2005, 08:08 PM
Everything in QT is a class, so you definitely can't use it with C. And that GTK example above doesn't look anywhere near simple enough to be a true hello world. Setting the window border width? On hello world? What's up with that?;)
endoalpha
09-21-2005, 03:50 AM
I might be shot down for offering a c++ based GUI API (cross platform), but I find wxWidgets, or wxWindows found at wxWidgets (http://www.wxwindows.org/) is entirely suitable for any windowing based system. I don't believe any special runtime libraries are involved (as per GTK for MSWindows). I definitely think c++ is the way to go for gui based systems, unless you are ready to really think about what you are actually doing when drawing dialogue boxes and stuff.
zeroth
09-21-2005, 08:19 AM
I'll look into wxWidgets. Currently, I've got the latest GTK sources installed with all dependancies. I've started with the official GTK tutorial, and it looks a lot simpler than Win32 Programming.
However, I'm not sure I installed GTK+ or its dependancies correctly. I get an error from pkg-config when I try to compile anything, says "no sich file or directory", and I'm sure its referring to "gtk+-2.0", as in `pkg-config --cflags --libs gtk+-2.0`
I installed all dependancies to /usr using --prefix=/usr when configuring sources.
bwkaz
09-21-2005, 07:15 PM
What happens if you run:
pkg-config --libs --cflags gtk+-2.0
from a command line, without the backticks?
tecknophreak
09-22-2005, 07:57 AM
Just want to put my .02 in here. GTK+ is by far the easier GUI programming out of the GTK+/QT duo. The API and tutorial are much easier on the eyes and to read. I feel it gives a little more flexability in it's programming. You'll def like this one more since you're a C programmer as well. Any problems you might have about it either post in the Programming forum here or email to the GTK list. Both you'll get a really quick response.
tecknophreak
09-22-2005, 08:01 AM
As far as that install goes, did you use the the commands
./configure --prefix=/usr
make
make install
To compile the atk, glib, gtk+, and pango packages? I've got a little gtk install howto for RH/FC if you want me to post it here.
zeroth
09-22-2005, 10:57 AM
As far as that install goes, did you use the the commands
./configure --prefix=/usr
make
make install
To compile the atk, glib, gtk+, and pango packages? I've got a little gtk install howto for RH/FC if you want me to post it here.
yep/
./configure --prefix=/usr
make
make install
for atik, glib, gtk+, pango, and pkg-config, ... all the dependencies. I'll try "pkg-config --libs --cflags gtk+-2.0" when I get back to my box, and I'll post again.
edit: This thread is ancient, as well as this post I'm editng. However, I said I'd post, and I never did. I'm editing this because I don't want to bump this thread to the top of the forums. So anyway, GTK+ kicks ***, I love it, bought a book on it, and yeah. I'm lovin the GTK.