Page 1 of 1

Hello World! GTK+ SDK GUI in Linux

Posted: Tue Sep 22, 2015 3:49 am
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")

Re: Hello World! GTK+ SDK GUI in Linux

Posted: Tue Sep 22, 2015 8:26 am
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..

Re: Hello World! GTK+ SDK GUI in Linux

Posted: Wed Sep 23, 2015 11:15 am
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! :)

Re: Hello World! GTK+ SDK GUI in Linux

Posted: Wed Sep 23, 2015 9:19 pm
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".

Re: Hello World! GTK+ SDK GUI in Linux

Posted: Thu Sep 24, 2015 4:07 am
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

Re: Hello World! GTK+ SDK GUI in Linux

Posted: Thu Sep 24, 2015 7:29 am
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.

Re: Hello World! GTK+ SDK GUI in Linux

Posted: Thu Sep 24, 2015 9:16 am
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

Re: Hello World! GTK+ SDK GUI in Linux

Posted: Thu Sep 24, 2015 9:17 am
by Fred
That's means than the import definition isn't up-to-date, you can check it on the PB doc svn.

Re: Hello World! GTK+ SDK GUI in Linux

Posted: Thu Sep 24, 2015 9:38 am
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!

Re: Hello World! GTK+ SDK GUI in Linux

Posted: Thu Sep 24, 2015 10:17 am
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.