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")


