; Module/File: Window_SetTransparentBackground+_gtk2.pb ; Function: Set Window transparency w/o decoration, moveable - Linux gtk2 ; Author: Transparency background method by Shardik, Demo by Omi ; Date: Jan. 02, 2017 ; Version: 0.1 ; Target Compiler: PureBasic 5.22/5.31/5.40/5.5x/5.6x/5.7 ; Target OS: Linux: (X/K/L)ubuntu, Mint, 32/64, Ascii/Uni ; Link to topic: http://www.purebasic.fr/english/viewtopic.php?f=15&t=51042 ;-------------------------------------------------------------- ; http://zetcode.com/gfx/cairo/root/ ; https://stackoverflow.com/questions/3908565/how-to-make-gtk-window-background-transparent#3909283 EnableExplicit ImportC "" g_signal_connect(*instance, detailed_signal.p-utf8, *c_handler, *pdata, destroy= 0, flags= 0) As "g_signal_connect_data" gdk_cairo_create(*window) gdk_display_get_pointer(*display.GdkDisplay, *screen.GdkScreen, *x, *y, *mask); gtk2.2+ gdk_screen_get_rgba_colormap(*screen.GdkScreen) gtk_widget_get_window(*widget.GtkWidget) gtk_widget_set_tooltip_text(*widget, text.p-utf8) gtk_window_set_opacity(*window.GtkWindow, opacity.d) gtk_window_get_opacity.d(*window.GtkWindow) cairo_destroy(*cr) cairo_image_surface_create_from_png(filename.p-utf8) cairo_image_surface_get_height(*surface) cairo_image_surface_get_width(*surface) cairo_paint(*cr) cairo_scale(*cr, sx.d, sy.d) cairo_set_operator(*cr, op) cairo_set_source_rgba(*cr, red.d, green.d, blue.d, alpha.d) cairo_set_source_surface(*CairoContext, *surface, x.d, y.d) cairo_surface_destroy(*surface) EndImport ; Object constants #Win_Main = 0 #MainCont = 0 Enumeration cairo_operator_t #CAIRO_OPERATOR_CLEAR #CAIRO_OPERATOR_SOURCE EndEnumeration Global.i gEvent, gQuit Global *gSurface ProcedureC Callback_ButtonMotion(*widget.GtkWindow, *event.GdkEventButton, window); ; make it draggable, based on a mestnyi-example Static.i isPressed Static.i wX, wY; pointer window relative Static.i wBorderL, wBorderT; left, upper window delta Protected.i rX, rY, rMask; pointer root relative If *event\type = #GDK_BUTTON_PRESS And *event\button= 1; 2 / 3 wX = WindowMouseX(window); see Window_GetInnerMousePos.pb wY = WindowMouseY(window) isPressed= #True gdk_window_get_position_(gtk_widget_get_window(*widget), @wBorderL, @wBorderT) gdk_window_get_root_origin_(gtk_widget_get_window(*widget), @rX, @rY) wBorderL- rX : wBorderT- rY ElseIf *event\type = #GDK_BUTTON_RELEASE isPressed= #False EndIf If isPressed gdk_display_get_pointer(gdk_display_get_default_(), #Null, @rX, @rY, @rMask) gtk_window_move_(*widget, rX- wX- wBorderL, rY- wY- wBorderT) If Not (rMask & #GDK_BUTTON1_MASK) : isPressed= #False : EndIf; against trailing, quick stop EndIf If *event\type = #GDK_BUTTON_RELEASE And *event\button= 3 If gtk_window_get_decorated_(*widget) gtk_window_set_decorated_(*widget, #False) Else gtk_window_set_decorated_(*widget, #True) EndIf EndIf ProcedureReturn #False EndProcedure ProcedureC Callback_ExposeEvent(*widget.GtkWidget, *event.GdkEventExpose, user_data); user_data get #Window Protected *cr= gdk_cairo_create(gtk_widget_get_window(*widget)) cairo_set_source_rgba(*cr, 1.0, 1.0, 1.0, 0.0) cairo_set_operator(*cr, #CAIRO_OPERATOR_SOURCE) cairo_paint(*cr) ;scale a pic and show it ... cairo_scale(*cr, WindowWidth(user_data)/cairo_image_surface_get_width(*gSurface), WindowHeight(user_data)/cairo_image_surface_get_height(*gSurface)) cairo_set_source_surface(*cr, *gSurface, 0, 0) cairo_paint(*cr) cairo_destroy(*cr) ProcedureReturn #False EndProcedure ProcedureC Callback_GtkWindowScroll(*widget.GtkWidget, *Event.GdkEventScroll, user_data) Protected.d Opacity If user_data = #Win_Main If *Event\direction = #GDK_SCROLL_UP Opacity= gtk_window_get_opacity(*widget) If Opacity < 1.0 Opacity+ 0.1 gtk_window_set_opacity(*widget, Opacity) EndIf ElseIf *Event\direction = #GDK_SCROLL_DOWN Opacity= gtk_window_get_opacity(*widget) If Opacity > 0.0 Opacity- 0.1 gtk_window_set_opacity(*widget, Opacity) EndIf EndIf EndIf EndProcedure Procedure Create_WinMain() Protected FixedBox, Screen, Colormap If OpenWindow(#Win_Main, 300, 200, 400, 200, "Transp. window background gtk3", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Invisible) Protected *Window= WindowID(#Win_Main) gtk_window_set_decorated_(*Window, #False) FixedBox= g_list_nth_data_(gtk_container_get_children_(gtk_bin_get_child_(WindowID(#Win_Main))), 0) g_signal_connect_(FixedBox, "expose-event", @Callback_ExposeEvent(), #Win_Main) gtk_widget_set_app_paintable_(FixedBox, #True) ; ----- Delete GdkWindow resources to be able to change colormap gtk_widget_unrealize_(*Window) ; ----- Change colormap to support alpha values and hence allow transparency Screen = gtk_widget_get_screen_(*Window) Colormap= gdk_screen_get_rgba_colormap(Screen) If Colormap gtk_widget_set_colormap_(*Window, Colormap) Else MessageRequester("Error", "Your current system configuration doesn't support transparency!") End EndIf g_signal_connect(*Window, "event", @Callback_ButtonMotion(), #Win_Main); make it draggable g_signal_connect_(*Window, "scroll-event", @Callback_GtkWindowScroll(), #Win_Main) gtk_widget_show_all_(*Window) gtk_widget_set_tooltip_text(*Window, "[Esc] closes window" + #LF$ + "[Left mouse drag] moves window" + #LF$ + "[Scrollwheel] set opacity" + #LF$ + "[Rightclick] set window frame") EndIf EndProcedure *gSurface= cairo_image_surface_create_from_png(#PB_Compiler_Home + "./logo.png") Create_WinMain() AddKeyboardShortcut(#Win_Main, #PB_Shortcut_Escape, #PB_Key_Escape) Repeat gEvent= WaitWindowEvent() Select gEvent Case #PB_Event_CloseWindow cairo_surface_destroy(*gSurface) gQuit= #True Case #PB_Event_Menu If EventMenu() = #PB_Key_Escape gQuit= #True EndIf EndSelect Until gQuit ; IDE Options = PureBasic 5.70 LTS beta 3 (Linux - x64) ; CursorPosition = 5 ; Folding = - ; EnableXP ; SubSystem = gtk2 ; EnableUnicode