Linux Lib: Notifications ans Tooltips

Developed or developing a new product in PureBasic? Tell the world about it.
walker
Enthusiast
Enthusiast
Posts: 634
Joined: Wed May 05, 2004 4:04 pm
Location: Germany

Linux Lib: Notifications ans Tooltips

Post by walker »

Not a real lib yet, but an includefile ... the lib is planned but there are much things to improve so I decided to release it as includefile...

Following commands are to be used (all other in the includefile are internally called or for functions not included yet)

notify_init(*pgm_name.s) - a pointer to the program which wants to use notifications MUST be called before the first use
notify_uninit()- to be called at programs end
notify_show(titel.s,msg.s,timeout, urgency, hwnd, img.s)
titel.s= the title of the Notification or the tooltip
msg.s = the message text of the notification or the tooltip
timeout = defines how many seconds the notification/tooltip will be shown; a value of 0 = forever until closed by user or program
urgency = can be #NOTIFY_URGENCY_LOW , #NOTIFY_URGENCY_NORMAL or #NOTIFY_URGENCY_CRITICAL (in values: 0, 1 or 2)
hwnd = the GadgetID or WindowID for the tooltip/notification OR 0; then it will be shown in the notification area of the screen
img.s= an image (all formats GTK+ knows) to be shown in the tooltip/notification (don't use too big images... up to 100x100 will work)
notify_add_action(*notification.l,*action.l,*label.l,callback.l, user_data.l, free_func.l)
*notification = the notification returned by notify_show()
*action= a pointer to a sting; always "clicked"
*label= a pointer to a string; whatever you want
callback= a pointer to a procedure like
user_data = 0
free_func = 0
notify_close(*notification) - closes the given notification/tooltip
notify_register_tooltip(*gadget, title.s, body.s, img.s, timeout.l, urgency.l)
*gadget= the ID (gadgetid or WindowID) the tooltip should be attached to
titel.s= the title of the Notification or the tooltip
body.s = the message text of the notification or the tooltip
img.s= an image (all formats GTK+ knows) to be shown in the tooltip/notification (don't use too big images... up to 100x100 will work)
timeout = defines how many seconds the notification/tooltip will be shown; a value of 0 = forever until closed by user or program
urgency = can be #NOTIFY_URGENCY_LOW , #NOTIFY_URGENCY_NORMAL or #NOTIFY_URGENCY_CRITICAL (in values: 0, 1 or 2)

at present the tooltip is shown if the mouse is over the gadget for about 1 sec
should work with all gadgets that could have an "enter"-event

Download: http://home.arcor.de/x-linux/pure/notify/notify_lib.pb
To use this include you must have libnotify installed... but this should be on most distros.... (the code below should work on all ubuntu and ubuntu based distros without a change.... all others have to adapt the path to an image.. or none will be shown :shock: )

and here a small example for testing:

Code: Select all

; small demo for the use of notifications
; 2007 walker
;
;always include as first
XIncludeFile "notify_lib.pb"

;this callback procedure is used by one of the notifications
Procedure mycallback()
; if the notification is clicked somwhere, this code will be executed...except you click on the close-button
sn=notify_show("Tooltip for a Screen","Click to close",5,#NOTIFY_URGENCY_CRITICAL,0,"/usr/share/icons/gnome/32x32/status/dialog-information.png")
EndProcedure

;open a small window and add some gadgets
hwnd=OpenWindow(0,0,0,500,350,"Tooltips in a different way...",#PB_Window_ScreenCentered)
pgm.s=ProgramFilename()
notify_init(@pgm); necessary to init the notification server for your program

CreateGadgetList(WindowID(0))
    ButtonGadget(1,10,10,100,30,"Test")
    notify_register_tooltip(GadgetID(1), "Gadget-Tooltip", "Click this button to see more...", "/usr/share/icons/gnome/32x32/status/dialog-information.png", 5,  #NOTIFY_URGENCY_NORMAL)
    ButtonGadget(2,380,300,100,0,"Exit")
    notify_register_tooltip(GadgetID(2), "Gadget-Tooltip", "Click here to exit....", "/usr/share/icons/gnome/32x32/status/dialog-information.png", 5,  #NOTIFY_URGENCY_LOW)
    CheckBoxGadget(3,10,50,100,25,"testit")
    notify_register_tooltip(GadgetID(3), "Gadget-Tooltip", "a checkbox.", "/usr/share/icons/gnome/32x32/status/dialog-information.png", 5,  #NOTIFY_URGENCY_LOW)
CloseGadgetList()
;---------------------

Repeat
    ev=WaitWindowEvent(1)
    ge=EventGadget()
    If ge=1
        wn=notify_show("Tooltip for a Window","Click here to view"+#LF$+"one for a screen"+#LF$+#LF$+"(lower right corner)",5,#NOTIFY_URGENCY_CRITICAL,WindowID(0),"/usr/share/icons/gnome/32x32/status/dialog-information.png"); the notification is added to a gadget
        action.s="default"
        label.s="clicked"
        notify_add_action(wn,@action,@label,@mycallback(),0,0)     
    ElseIf ge=2
        notify_uninit()
        End
    EndIf  
ForEver
End

Enjoy :!: