GTK3 HeaderBar Example

Linux specific forum
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

GTK3 HeaderBar Example

Post by fsw »

Don't know about you guys/gals, but I like the HeaderBar...

Code: Select all

ImportC "-lgtk-3"
  gtk_application_new(Id.p-utf8, Flags=0)
  gtk_application_window_new(*application)
  gtk_application_add_window(*application, *window)
  gtk_application_set_app_menu(*application, *app_menu)
  gtk_application_set_menubar(*application, *menubar)
  gtk_builder_new()
  gtk_builder_add_from_string(*builder, buffer.p-utf8, length=-1, *error=0)
  gtk_builder_get_object(*builder, name.p-utf8)
  gtk_widget_show_all(*Wigdet)
  gtk_window_set_title(*Window, Title.p-utf8)
  gtk_window_set_default_size(*Window, Width.l, Height.l)
  gtk_window_set_position(*Window, Position.l)
  
  gtk_window_set_decorated (*Window, flag.i)
  gtk_window_set_titlebar (*Window, *bar)
  
  gtk_header_bar_new ()
  gtk_header_bar_set_title (*bar, Title.p-utf8)
  gtk_header_bar_set_subtitle (*bar, SubTitle.p-utf8)
  gtk_header_bar_set_show_close_button (*bar, flag.i)
  gtk_header_bar_pack_start (*bar, *child)
  gtk_header_bar_pack_end (*bar, *child)
  
  gtk_button_new ();
  gtk_button_new_with_label (*label.p-utf8);
  
EndImport
ImportC "-lgobject-2.0"
  g_signal_connect_data(*Instance, Signal.p-utf8, *Handler, *Data=0, Flags.l=0)
EndImport
ImportC "-lgio-2.0"
  g_action_map_add_action(*application, *action)
  g_application_run(*application, argc=0, *argv=0)
  g_application_quit(*application)
  g_simple_action_new(name.p-utf8, parameter_type=0)
EndImport

Global *app, *win

ProcedureC PreferencesActivated()
  Debug "PreferencesActivated"
EndProcedure

; quit_activated (GSimpleAction *action, GVariant *parameter, gpointer app)
ProcedureC QuitActivated() ;*action, *parameter, *application)
  Debug "QuitActivated"
  g_application_quit(*app)
EndProcedure

ProcedureC Startup(*appl)
  Debug("Startup")
 
  *win = gtk_application_window_new(*appl)
  gtk_window_set_default_size(*win, 275, 275)
  gtk_window_set_position(*win, #GTK_WIN_POS_CENTER)
  
  *tbar = gtk_header_bar_new ()
  gtk_header_bar_set_show_close_button(*tbar, #True)
  gtk_header_bar_set_title (*tbar, "GTK3")
  gtk_header_bar_set_subtitle (*tbar, "Test")
 
  ; make the headerbar the new titlebar
  gtk_window_set_titlebar(*win, *tbar)
  
  
  ; add window to application
  gtk_application_add_window(*appl, *win)
 
  s.s = "<?xml version='1.0'?>" +
  "<interface>" +
  "<menu id='appmenu'>" +
  "  <section>" +
  "  <item>" +
  "    <attribute name='label' translatable='yes'>_Preferences</attribute>" +
  "    <attribute name='action'>app.preferences</attribute>" +
  "  </item>" +
  "  </section>" +
  "  <section>" +
  "  <item>" +
  "    <attribute name='label' translatable='yes'>_Quit</attribute>" +
  "    <attribute name='action'>app.quit</attribute>" +
  "    <attribute name='accel'><Primary>q</attribute>" +
  "  </item>" +
  "  </section>" +
  "</menu>" +
  "</interface>"
 
  *builder = gtk_builder_new()
  gtk_builder_add_from_string(*builder, s)
  *appmenu = gtk_builder_get_object(*builder, "appmenu")
  gtk_application_set_app_menu(*appl, *appmenu)
 
  *menubar = gtk_builder_get_object(*builder, "menubar")
  gtk_application_set_menubar(*appl, *menubar)
 
  *actpref = g_simple_action_new("preferences")
  g_signal_connect_data(*actpref, "activate", @PreferencesActivated())
  g_action_map_add_action(*appl, *actpref)

  *actquit = g_simple_action_new("quit")
  g_signal_connect_data(*actquit, "activate", @QuitActivated())
  g_action_map_add_action(*appl, *actquit)
    
EndProcedure

ProcedureC Activate(*appl)
  Debug("Activate")
  gtk_widget_show_all(*win)
EndProcedure

ProcedureC Shutdown(*appl)
  Debug("Shutdown")
EndProcedure

Procedure Main()
  *app = gtk_application_new("purebasic.gnome")
  
  g_signal_connect_data(*app, "startup",  @Startup())
  g_signal_connect_data(*app, "activate", @Activate())
  g_signal_connect_data(*app, "shutdown", @Shutdown())
  
  g_application_run(*app)
  g_object_unref_(*app)
EndProcedure

Main()
Most of the code is from this forum anyhow (also shows the GNOME SHELL MenuBar menu).
Maybe it's useful for someone.

I am to provide the public with beneficial shocks.
Alfred Hitshock
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: GTK3 HeaderBar Example

Post by Shardik »

fsw,

thank you for your nice GtkHeaderBar example which uses only Gtk3 API functions. I have tried to program a stripped down version which uses a PB window and PB menu bar so that the event handling becomes much easier. I am currently only able to test my example successfully on Ubuntu 14.04 x64 with KDE and PB 5.40 Beta 8 (Unity and Enlightenment don't display or handle the GtkHeaderBar correctly as documented in this thread).

My example has one improvement to your example: the menu bar is fully integrated into the header bar while your example displays the menu bar below the header bar.

Code: Select all

EnableExplicit

ImportC ""
  gtk_header_bar_new()
  gtk_header_bar_pack_start (*HeaderBar, *Child)
  gtk_header_bar_set_show_close_button(*HeaderBar, ShowCloseButton.I)
  gtk_header_bar_set_subtitle(*HeaderBar, Title.P-UTF8)
  gtk_header_bar_set_title(*HeaderBar, Subtitle.P-UTF8)
  gtk_window_set_titlebar(*Window.GtkWindow, *Titlebar)
EndImport

Define HeaderBar.I

OpenWindow(0, 0, 0, 275, 275, "GtkHeaderBar demo",
  #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CreateMenu(0, WindowID(0))
MenuTitle("Application")
MenuItem(0, "Preferences")
MenuBar()
MenuItem(1, "Quit")

HeaderBar = gtk_header_bar_new()

If HeaderBar
  gtk_header_bar_set_title(HeaderBar, "GTK3")
  gtk_header_bar_set_subtitle(HeaderBar, "Test")
  gtk_header_bar_set_show_close_button(HeaderBar, #True)
  gtk_window_set_titlebar(WindowID(0), HeaderBar)
  gtk_widget_reparent_(MenuID(0), HeaderBar)
EndIf

gtk_widget_show_all_(WindowID(0))

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: GTK3 HeaderBar Example

Post by fsw »

That's a very nice example.
Shardik wrote:My example has one improvement to your example: the menu bar is fully integrated into the header bar...
Your code works probably better on all Linux desktop environments.
Shardik wrote:...while your example displays the menu bar below the header bar.
Not on Gnome 3.
There it's shown in the application-menu (inside the single black panel at the top of the screen) and not inside the window.
(looks the same as the OSX menus...)

Your version will be better suited in conjunction with all the other PB GUI elements.
Thanks

EDIT
Just realized that your example app doesn't recognize the maximize keyboard shortcut (super & arrow-up) and other shortcuts.
(normalize size = super & arrow-down, move to the left side = super & arrow-left, move to the right side = super & arrow-right).
Suppose they are only available on Gnome 3.
This is because gtk_application_new is not used...

I am to provide the public with beneficial shocks.
Alfred Hitshock
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: GTK3 HeaderBar Example

Post by mestnyi »

Ubuntu 14.04 lts purebasic 531 lts не работают ваши примеры Your examples do not work the compiler complains
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Re: GTK3 HeaderBar Example

Post by Oma »

@mestnyi
Like the topic says: GTK3 HeaderBar... :wink:
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
Post Reply