Hello World! GTK+ SDK GUI in Linux

Linux specific forum
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Hello World! GTK+ SDK GUI in Linux

Post by Keya »

On my journey of discovery that Purebasic has put me on, as I try to understand the new-to-me operating systems of Linux & Mac and how Purebasic programs work under them and how they compare to their Windows counterparts I've been reading several beginners books to try and help get my head around these mysterious non-Windows worlds. It's a fun challenge! I have to thank Fred for expanding my horizons heehee :)

Whereas Windows uses CreateWindowEx and so on from user32.dll, Linux offers several "GUI platforms" but if im understanding correctly the main two seem to be GTK+ and QT?

I've been mostly ignoring QT for now as i understand that Purebasic uses GTK+, so it was great to have a whole chapter in this book on GTK+, and the C demos are fairly easy to port to PB.

This simple demo is roughly based on the Hello World demo from Beginning Linux Programming, 4th Edition pg 648,
and i'm assuming it's something along the lines of how a PB program looks under the hood if it just has one OpenWindow and a ButtonGadget reacting to a click event :)
I wont be using any of this as a substitute for PB's cross-platform OpenWindow and gadgets, this is just for my learning purposes to help get my footing, but it's great that because PB also uses GTK+ we can use these API with PB-created windows and gadgets! Oh the power! heehee

Code: Select all

CompilerIf #PB_Compiler_Unicode = 1
  CompilerError "Unicode not supported in this demo, it probably just needs some p-utf8's"
CompilerEndIf


EnableExplicit

ProcedureC button1_click(*gbutton, *puserdata)   ;must be ProcedureC
  MessageRequester("Button clicked","Hello, world!" + #CRLF$ + "Click Ok to call gtk_main_quit")
  gtk_main_quit_()
EndProcedure


Define window1.i, button1.i

;INITIALIZE GTK+
gtk_init_(0,0)    ;argc and argv are usually passed as the params. I think that whenever you create a PB app that uses either a window or a messagerequester it automatically calls gtk_init.

;CREATE WINDOW
window1 = gtk_window_new_(#GTK_WINDOW_TOPLEVEL)
;With a few optional settings.  And because PB also uses GTK+ we can also call these API against our own PB (OpenWindow) WindowId() if we want!
gtk_window_set_title_(window1, @"My window title")
gtk_window_set_position_(window1, #GTK_WIN_POS_CENTER)
gtk_window_set_default_size_(window1, 300, 200)

;CREATE BUTTON
button1 = gtk_button_new_with_label_(@"Hello, world!")
gtk_container_add_(window1, button1)

;REGISTER A CALLBACK FOR EVENT "clicked"
g_signal_connect_(button1, @"clicked", @button1_click(), @"My user data for button1")

;MAKE VISIBLE
gtk_widget_show_(button1)
gtk_widget_show_(window1)

;CALL GTK MAIN. (Doesn't return until gtk_main_quit_() is called)
gtk_main_()

MessageRequester("Done","Program has returned from gtk_main")
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Hello World! GTK+ SDK GUI in Linux

Post by Fred »

It's fun hacking directly with lowlevel API, right ? :). PB indeed includes a lot more options under the hood to ease your life, for example the OpenWindow() function itself is 500 lines long..
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: Hello World! GTK+ SDK GUI in Linux

Post by Keya »

I just like to understand how things work so i feel more comfortable using them, and although that makes things extra challenging at the start (and extra frustrating!) the result is extra rewarding! :)
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Hello World! GTK+ SDK GUI in Linux

Post by bbanelli »

Has PB 5.4 changed approach to const gchar *?

It only seem to accept "pure" string now, otherwise giving macro error (PB 5.4b7) with "bad parameter type: a string is expected".
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: Hello World! GTK+ SDK GUI in Linux

Post by Keya »

I havent tried v5.40 in anything other than Windows yet, so i was just testing the above code in 5.31 Linux

In regards to the string parameter error it seems that its usually just a matter of changing @"stringvariable" to "stringvariable", or vice versa
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Hello World! GTK+ SDK GUI in Linux

Post by Fred »

in PB 5.40, it automatically convert to utf-8 for you when pass a string, so it work in unicode mode as well.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Hello World! GTK+ SDK GUI in Linux

Post by bbanelli »

Fred wrote:in PB 5.40, it automatically convert to utf-8 for you when pass a string, so it work in unicode mode as well.
Doesn't work for gtk_window_set_title() and gtk_button_new_*() functions , at least not in b7.

Image
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Hello World! GTK+ SDK GUI in Linux

Post by Fred »

That's means than the import definition isn't up-to-date, you can check it on the PB doc svn.
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Hello World! GTK+ SDK GUI in Linux

Post by bbanelli »

Fred wrote:That's means than the import definition isn't up-to-date, you can check it on the PB doc svn.
I know that this is probably one of those questions that diminish saying "there are no stupid questions", but I'll give it a try:

http://svn.purebasic.fr:8080/websvn/fil ... k%2Fgtk.pb

Is there anything we users can do in order to update those missing import definitions and how? I'm not really proficient with SVN and couldn't locate any instruction on forum by quick search, so pardon me once again for asking!
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Hello World! GTK+ SDK GUI in Linux

Post by Fred »

You can use an SVN client (like tortoiseSVN on Windows) to checkout the repository, then make some changes, create a "Patch" and post it here. Or if you think you will want to contribute regulary to the SVN I can create a write account for you.
Post Reply