; Module/File: Editor_CountLines&Paragraphs.pb ; Function: Way to count single-lines and paragraphs in EditorGadget with constant font (not perfect) - Linux ; Author: Omi ; Date: Feb. 20, 2016 ; Version: 0.1 ; Target Compiler: PureBasic 5.22/5.31/5.40 ; Target OS: Linux: (X/K/L)ubuntu, Mint, 32/64, Ascii/Uni ;-------------------------------------------------------------- ; no exact function (depends on div. fonts, margins, spaces, ...) ; use GadgetFont_GetLineHeight(Gadget) in GadgetFont_GetFontMetrics.pb for better (or individual) LineDistance-calculation EnableExplicit ; Object constants #Win_Main = 0 #But1 = 0 #Edt1 = 1 Global.i gEvent, gQuit Global.i gCurrTextHeight= 10 Global.s S1= "A little Text to demonstrate a multiple view of the same text being shown and editable in 2 (or more) PureBasic-EditorGadget. " + #LF$ + #LF$ + "To make this possible it is necessary To use the same 'TextBuffer' for several EditorGadgets (TextViews)!" + #LF$ + #LF$ + "Enter some new text here and change the Editor to type in your text ..." + #LF$ + #LF$ + #LF$ + #LF$ + "Some lines to fill the place:" + #LF$ + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. " + #LF$ + "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. " + #LF$ + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. " + #LF$ + "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." + #LF$ + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua." #Font0= 0 LoadFont(#Font0, "Monospace", gCurrTextHeight) Procedure.i Editor_GetLineNumbers(Gadget, ParagraphRet= 0) Protected.GtkTextView *TextView= GadgetID(Gadget) Protected.GtkTextIter iter Protected.i y, ParagraphPixHeight, ParagraphLines, LinePixDistance Protected.i numLines= 0, numParagraph= 0 Protected.d FactDist= 1.6; approximated line-distance gtk_text_buffer_get_start_iter_(gtk_text_view_get_buffer_(*TextView), @iter) LinePixDistance= gCurrTextHeight * FactDist; approximated line-distance While Not gtk_text_iter_is_end_(iter) gtk_text_view_get_line_yrange_(*TextView, @iter, @y, @ParagraphPixHeight) ParagraphLines = Round(ParagraphPixHeight / LinePixDistance, #PB_Round_Nearest) numLines + ParagraphLines LinePixDistance= ParagraphPixHeight / ParagraphLines; recalculated line-distance numParagraph+ 1; every line_yrange-iter is a paragraph! gtk_text_iter_forward_line_(@iter); jump to next paragraph Wend If ParagraphRet ProcedureReturn numParagraph Else ProcedureReturn numLines EndIf EndProcedure If OpenWindow(#Win_Main, 0, 0, 500, 300, "Editor - count lines & paragraphs", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered) ButtonGadget(#But1, 5, 5, 200, 26, "Count lines") EditorGadget(#Edt1, 5, 40, 490, 255, #PB_Editor_WordWrap) AddGadgetItem(#Edt1, -1, S1) SetGadgetFont(#Edt1, FontID(#Font0)) Repeat gEvent= WaitWindowEvent() 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)-45) Case #PB_Event_Gadget Select EventGadget() Case #But1 Debug "Line numbers : " + Str(Editor_GetLineNumbers(#Edt1)) Debug "Paragraph numbers: " + Str(Editor_GetLineNumbers(#Edt1, #True)) EndSelect EndSelect Until gQuit EndIf ; IDE Options = PureBasic 5.42 LTS (Linux - x86) ; CursorPosition = 10 ; Folding = - ; EnableUnicode ; EnableXP