; Module/File: Editor_BackgroundPaintCssGtk3 ; Function: Set an EditorGadget background painting by CSS - Linux gtk3 ; Author: Omi ; Date: Dec. 06, 2015 ; Version: 0.1 ; Target Compiler: PureBasic 5.22/5.31/5.4x ; 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=64409 ;-------------------------------------------------------------- ;Visibility of backgrounds depends on the distro + version + 'theme'/'Gtk-Design' !!! EnableExplicit ImportC "" gtk_css_provider_load_from_data(*css_provider, data_.p-utf8, length, *error.GError) gtk_css_provider_new() gtk_style_context_add_provider(*context, *provider, priority) gtk_style_context_remove_provider(*context, *provider) gtk_widget_get_style_context(*widget.GtkWidget) pango_context_get_font_description(*context) pango_context_get_metrics(*context, desc, *language); PangoFontMetrics pango_font_metrics_get_ascent(*metrics) pango_font_metrics_get_descent(*metrics) pango_font_metrics_unref(*metrics) pango_language_get_default() EndImport ; Object constants #Win_Main = 0 #Rb11 = 0 #Rb12 = 1 #Rb13 = 2 #Rb14 = 3 #TB1 = 4 #Edt = 5 #PangoUnit= 1024 Global.i gEvent, gEventWin, gQuit Global.i gLineHeight Global.s gCSS= "This is a PureBasic-EditorGadget!" + #LF$ + "On gtk3 and a 'theme', that doesn't surpresses this, the background is set by a CSS-painting!" + #LF$ + #LF$ + "This is the CSS-Code for lined paper ..." + #LF$ + "*{" + #LF$ + " background-color: rgba(255, 255, 255, 0.99);" + #LF$ + " background-image: linear-gradient(to bottom, white 0%, white 95%, LightGrey 100%);" + #LF$ + " background-size: 100% 17px;" + #LF$ + "}" + #LF$ + ":selected {" + #LF$ + " background-color: DarkTurquoise;" + #LF$ + " color: white;" + #LF$ + "}" + #LF$ + #LF$ + "This is the CSS-Code for striped paper ..." + #LF$ + "*{" + #LF$ + " background-color: rgba(170, 170, 170, 0);" + #LF$ + " background-image: linear-gradient(to bottom, #e8e8e8 2%,#f8f8f8 2%,#f8f8f8 48%,#e8e8e8 48%,#ffffff 52%,#ffffff 98%);" + #LF$ + " background-size: 100% 34px;" + #LF$ + "}" + #LF$ + ":selected {" + #LF$ + " background-color: Crimson;" + #LF$ + " color: white;" + #LF$ + "}" + #LF$ Global *gProvider #FontCode = 0 LoadFont(#FontCode, "Monospace", 10) Procedure.i GadgetFont_GetLineHeight(Gadget) Protected PangoContext = gtk_widget_get_pango_context_(GadgetID(Gadget)) Protected PangoFontDescription= pango_context_get_font_description(PangoContext) Protected PangoMetrics = pango_context_get_metrics(PangoContext, PangoFontDescription, pango_language_get_default()) Protected.i LineHeight = pango_font_metrics_get_ascent(PangoMetrics) / #PangoUnit + pango_font_metrics_get_descent(PangoMetrics) / #PangoUnit pango_font_metrics_unref(PangoMetrics) ProcedureReturn LineHeight EndProcedure ;-rad. Grandient Procedure Editor_SetCssBGnd_RadGrad(Gadget); radial gradient Protected.s EdtBGndCSS= "*{" + #LF$ + "background-color: rgba(255, 255, 255, 0);" + #LF$ + "background-image: -gtk-gradient(radial, center center, 0.3, center center, 0.9, from(White), to(MediumTurquoise));" + #LF$ + "background-size: 100% 100%;" + #LF$ + "}" + ":selected {" + #LF$ + "background-color: #77c5cf;" + #LF$ + "color: white; }" gtk_css_provider_load_from_data(*gProvider, EdtBGndCSS, -1, #Null) gtk_style_context_add_provider(gtk_widget_get_style_context(GadgetID(Gadget)), *gProvider, 600) EndProcedure ;-Lines Procedure Editor_SetCssBGnd_Lines(Gadget, Alpha.d); lines Protected.s EdtBGndCSS= "*{" + #LF$ + "background-color: rgba(255, 255, 255, " + Alpha + ");" + #LF$ + "background-image: linear-gradient(to bottom, white 0%, white 95%, LightGrey 100%);" + #LF$ + "background-size: 100% " + Str(gLineHeight) + "px;" + #LF$ + "}" + ":selected {" + #LF$ + "background-color: DarkTurquoise;" + #LF$ + "color: white; }" gtk_css_provider_load_from_data(*gProvider, EdtBGndCSS, -1, #Null) gtk_style_context_add_provider(gtk_widget_get_style_context(GadgetID(Gadget)), *gProvider, 600) EndProcedure ;-Stripes Procedure Editor_SetCssBGnd_Stripes(Gadget); stripes Protected.s EdtBGndCSS= "*{" + #LF$ + "background-color: rgba(170, 170, 170, 0);" + #LF$ + "background-image: linear-gradient(to bottom, #e8e8e8 2%,#f8f8f8 2%,#f8f8f8 48%,#e8e8e8 48%,#ffffff 52%,#ffffff 98%);" + #LF$ + "background-size: 100% " + Str(gLineHeight * 2) + "px;" + #LF$ + "}" + ":selected {" + #LF$ + "background-color: Crimson;" + #LF$ + "color: white; }" gtk_css_provider_load_from_data(*gProvider, EdtBGndCSS, -1, #Null) gtk_style_context_add_provider(gtk_widget_get_style_context(GadgetID(Gadget)), *gProvider, 600) EndProcedure Procedure Create_WinMain() If OpenWindow(#Win_Main, 300, 200, 500, 200, "PB-Editor CSS BackGround (gtk3)", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget) WindowBounds(#Win_Main, 300, 120, #PB_Ignore, #PB_Ignore) OptionGadget(#Rb11, 5, 5, 150, 24, "BGnd: empty") OptionGadget(#Rb12, 5, 28, 150, 24, "BGnd: rad. gradient") OptionGadget(#Rb13, 160, 5, 150, 24, "BGnd: lined paper") OptionGadget(#Rb14, 160, 28, 150, 24, "BGnd: striped paper") TextGadget (#PB_Any, 5, 52, 150, 22, "Background-Alpha:") TrackBarGadget(#TB1, 160, 49, 330, 25, 0, 100) SetGadgetState(#TB1, 0.99*100) EditorGadget (#Edt, 5, 75, 490, 120) SetGadgetText (#Edt, gCSS) SetGadgetFont (#Edt, FontID(#FontCode)) gtk_text_view_set_left_margin_ (GadgetID(#Edt), 4) gtk_text_view_set_right_margin_(GadgetID(#Edt), 4) EndIf EndProcedure Create_WinMain() *gProvider= gtk_css_provider_new() Repeat gEvent= WaitWindowEvent() gEventWin= EventWindow() If gEventWin = #Win_Main Select gEvent Case #PB_Event_CloseWindow gQuit= #True Case #PB_Event_SizeWindow ResizeGadget(#Edt, #PB_Ignore, #PB_Ignore, WindowWidth(#Win_Main)-10, WindowHeight(#Win_Main)-80) ResizeGadget(#TB1, #PB_Ignore, #PB_Ignore, WindowWidth(#Win_Main)-170, #PB_Ignore) Case #PB_Event_Gadget Select EventGadget() Case #TB1 Editor_SetCssBGnd_Lines(#Edt, GetGadgetState(#TB1)/100) Case #Rb11 gtk_style_context_remove_provider(gtk_widget_get_style_context(GadgetID(#Edt)), *gProvider) Case #Rb12 gLineHeight= GadgetFont_GetLineHeight(#Edt) Editor_SetCssBGnd_RadGrad(#Edt) DisableGadget(#TB1, #True) Case #Rb13 gLineHeight= GadgetFont_GetLineHeight(#Edt) Editor_SetCssBGnd_Lines(#Edt, 0.99) DisableGadget(#TB1, #False) Case #Rb14 gLineHeight= GadgetFont_GetLineHeight(#Edt) Editor_SetCssBGnd_Stripes(#Edt) DisableGadget(#TB1, #True) EndSelect EndSelect EndIf Until gQuit g_object_unref_(*gProvider) ; IDE Options = PureBasic 5.45 LTS (Linux - x86) ; CursorPosition = 9 ; Folding = - ; EnableUnicode ; EnableXP