; Module/File: Editor_BackgroundPaintCairo.pb ; Function: Demo: cairo background painting in EditorGadget w. examples - Linux gtk3 ; Author: Omi (+ shardiks window background-code) ; Date: Dec. 28, 2015 ; Version: 0.1 ; Target Compiler: PureBasic 5.40+ ; 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=64417 ;-------------------------------------------------------------- EnableExplicit ImportC "" gdk_cairo_create(*Drawable.GdkDrawable) g_signal_emit_by_name(*instance, detailed_signal.p-utf8, *callback) EndImport ImportC "-lcairo" cairo_arc(*cairoT, xc.d, yc.d, radius.d, angle1.d, angle2.d) cairo_destroy(*cairoT) cairo_fill(*cairoT) cairo_fill_preserve(*cairoT) cairo_image_surface_create_from_png(filename.p-utf8) cairo_move_to(*cairoT, x.d, y.d) cairo_line_to(*cairoT, x.d, y.d) cairo_stroke(*cairoT) cairo_pattern_add_color_stop_rgba(*cairoPatternT, offset.d, red.d, green.d, blue.d, alpha.d) cairo_pattern_create_linear(x0.d, y0.d, x1.d, y1.d) cairo_pattern_create_radial(cx0.d, cy0.d, radius0.d, cx1.d, cy1.d, radius1.d) cairo_pattern_destroy(*cairoPatternT) cairo_rectangle(*cairoT, x.d, y.d, Width.d, Height.d) cairo_select_font_face(*cairoT, family.p-utf8, slant, weight) cairo_set_font_size(*cairoT, size.d) cairo_set_line_width(*cairoT, width.d) cairo_set_source(*cairoT, *cairoPatternT) cairo_set_source_rgba(*cairoT, red.d, green.d, blue.d, alpha.d) cairo_show_text(*cairoT, utf8.p-utf8) cairo_text_extents(*cairoT, utf8.p-utf8, *extents) cairo_text_path(*cairoT, utf8.p-utf8) ; cairo_surface_destroy(*CairoSurface) EndImport ; Object constants #Win_Main = 0 Enumeration #Edt1 #Rb11 #Rb12 #Rb13 #Rb21 #Rb22 #Rb23 EndEnumeration #CAIRO_EXTEND_NONE = 0 #CAIRO_EXTEND_REPEAT = 1 Structure cairo_text_extents_t x_bearing.d y_bearing.d width.d height.d x_advance.d y_advance.d EndStructure ; Global gTExtends.cairo_text_extents_t Global.i gEvent, gEventWin, gQuit Global.s gS= "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness." + #LF$ + #LF$ + "No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do Not know how to pursue pleasure rationally encounter consequences that are extremely painful." + #LF$ + #LF$ + "Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure." + #LF$ + #LF$ + "To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault With a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" + #LF$ + #LF$ + "On the other hand, we denounce With righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain." + #LF$ + #LF$ + "These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business is will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains. " CompilerIf Subsystem("gtk2") ProcedureC WidgetExposeHandler(*Widget.GtkWidget, *Event.GdkEventExpose, user_data); user_data used as WindowId Protected *cairoT= gdk_cairo_create(*Widget\window) CompilerElse ProcedureC WidgetDrawHandler(*Widget.GtkWidget, *cairoT, user_data) CompilerEndIf Protected *cairoPatternT Protected.i I Protected.s pS= "EditorGadget" Static.i Init= 1 Protected gTExtends.cairo_text_extents_t If GetGadgetState(#Rb12); the background gradient color ... *cairoPatternT= cairo_pattern_create_linear(0.0, 0.0, 0.5, GadgetHeight(user_data)) cairo_pattern_add_color_stop_rgba(*cairoPatternT, 0.3, 1, 1, 1, 1) cairo_pattern_add_color_stop_rgba(*cairoPatternT, 1, 0.9, 0.9, 0.7, 1) cairo_rectangle(*cairoT, 0, 0, GadgetWidth(user_data), GadgetHeight(user_data)) cairo_set_source (*cairoT, *cairoPatternT) cairo_fill(*cairoT) cairo_pattern_destroy(*cairoPatternT) ElseIf GetGadgetState(#Rb13); the background lines ... cairo_set_source_rgba(*cairoT, 0.7, 0.7, 0.7, 1) For I= 20 To GadgetHeight(user_data) Step 20 cairo_move_to (*cairoT, 10, I) cairo_line_to (*cairoT, GadgetWidth(user_data)- 30, I) Next I cairo_set_line_width(*cairoT, 1.0) cairo_stroke(*cairoT) EndIf If GetGadgetState(#Rb22); the ball ... *cairoPatternT= cairo_pattern_create_radial(GadgetWidth(user_data)/2-15, GadgetHeight(user_data)/2-15, 25.6, GadgetWidth(user_data)/2-15, GadgetHeight(user_data)/2-15, 128.0); cairo_pattern_add_color_stop_rgba(*cairoPatternT, 0, 1, 1, 1, 1); cairo_pattern_add_color_stop_rgba(*cairoPatternT, 1, 0.7, 0.7, 0.7, 1); cairo_set_source(*cairoT, *cairoPatternT); cairo_arc(*cairoT, GadgetWidth(user_data)/2, GadgetHeight(user_data)/2, 76.8, 0, 2 * #PI); cairo_fill(*cairoT); cairo_pattern_destroy(*cairoPatternT) ElseIf GetGadgetState(#Rb23); the text ... cairo_select_font_face(*cairoT, "Sans", 0, 1) cairo_set_font_size(*cairoT, 48) cairo_text_extents(*cairoT, pS, @gTExtends) cairo_move_to(*cairoT, (GadgetWidth(user_data)-30) / 2 - gTExtends\width / 2, GadgetHeight(user_data) / 2) cairo_set_source_rgba(*cairoT, 0.5, 0.5, 1.0, 0.15) cairo_text_path(*cairoT, pS) cairo_fill_preserve(*cairoT) cairo_set_source_rgba(*cairoT, 0, 0, 0, 0.15); cairo_set_line_width(*cairoT, 2.56); ; cairo_show_text(*cairoT, pS) cairo_stroke(*cairoT) EndIf If Init gtk_widget_queue_draw_(*widget); added for instant display Init= 0 EndIf CompilerIf Subsystem("gtk2") = #True cairo_destroy(*cairoT) CompilerEndIf ProcedureReturn #False EndProcedure Procedure Create_WinMain() If OpenWindow(#Win_Main, 300, 200, 500, 300, "EditorGadget-CairoBackground (gtk3)", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget) WindowBounds(#Win_Main, 360, #PB_Ignore, #PB_Ignore, #PB_Ignore) OptionGadget(#Rb11, 5, 5, 170, 24, "BGnd: empty") OptionGadget(#Rb12, 5, 30, 170, 24, "BGnd: fill gradient") OptionGadget(#Rb13, 5, 55, 170, 24, "BGnd: lined paper") EditorGadget(#Edt1, 5, 80, 490, 215, #PB_Editor_WordWrap) SetGadgetText(#Edt1, gS) OptionGadget(#Rb21, 180, 5, 170, 24, "no add") OptionGadget(#Rb22, 180, 30, 170, 24, "add a ball") OptionGadget(#Rb23, 180, 55, 170, 24, "add a text") EndIf EndProcedure Create_WinMain() CompilerIf Subsystem("gtk2") g_signal_connect_(GadgetID(#Edt1), "expose-event", @WidgetExposeHandler(), #Edt1) g_signal_emit_by_name(GadgetID(#Edt1), "expose-event", @WidgetExposeHandler()); force display CompilerElse g_signal_connect_(GadgetID(#Edt1), "draw", @WidgetDrawHandler(), #Edt1) CompilerEndIf gtk_widget_queue_draw_(WindowID(#Win_Main)); added for instant display Repeat gEvent= WaitWindowEvent() gEventWin= EventWindow() If gEventWin = #Win_Main Select gEvent Case #PB_Event_CloseWindow gQuit= #True Case #PB_Event_SizeWindow ResizeGadget(#Edt1, #PB_Ignore, #PB_Ignore, WindowWidth(#Win_Main)-10, WindowHeight(#Win_Main)-85) Case #PB_Event_Gadget Select EventGadget() Case #Rb11, #Rb12, #Rb13, #Rb21, #Rb22, #Rb23 gtk_widget_queue_draw_(WindowID(#Win_Main)); added for instant display EndSelect EndSelect EndIf Until gQuit ; IDE Options = PureBasic 5.46 LTS (Linux - x64) ; CursorPosition = 41 ; FirstLine = 91 ; Folding = -- ; EnableUnicode ; EnableXP