Faire un editeur de texte avancé avec gtk

Programmation d'applications complexes
frederic
Messages : 22
Inscription : dim. 07/août/2005 9:40

Faire un editeur de texte avancé avec gtk

Message par frederic »

J'avais trouvé il y a un moment un exemple de mise en forme de texte sous pb linux http://purebasic.fr/english/viewtopic.p ... 123#176123 , l'inconvénient c'est qu'on ne pouvait pas enlever la mise en forme. J'ai donc complété le code pour ajouter cette fonction. Alors ça n'est probablement pas optimisé mais ça fonctionne :)

Code : Tout sélectionner

Enumeration
        ;windows
        #window_Main
        
        ;gadgets
         
        #gadget_Main_Panel
       
        
        #gadget_Main_Editor_TemplateMsg
        #gadget_Main_Frame_EditorToolbar
        #gadget_Main_Button_Bold
        #gadget_Main_Button_Italic
        #gadget_Main_Button_Underline

        ;menus
        #menu_Main
        #menu_Main_Exit
        #menu_Main_About

EndEnumeration

#PANGO_WEIGHT_BOLD            = 800
#PANGO_STYLE_OBLIQUE          = 1
#PANGO_UNDERLINE_SINGLE       = 1

Global StartIter.GtkTextIter
Global EndIter.GtkTextIter
Global EditorBuffer ; le buffer de l'EditorGadget
Global *TagTable.GtkTextTagTable ; Tableau contenant tous les tags appliqués

Procedure Main_Window()

        If OpenWindow(#window_Main, 0, 0, 625,420, "RichEditor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered )
        
                If CreateGadgetList(WindowID(#window_Main))

                    Frame3DGadget(#gadget_Main_Frame_EditorToolbar, 5, 5, 616, 60, "", #PB_Frame3D_Flat)
          			           
                    CoolButtonGadget(#gadget_Main_Button_Bold, 10,19, 40,40,"", "gtk-bold")

                    CoolButtonGadget(#gadget_Main_Button_Italic, 55, 19, 40, 40, "", "gtk-italic")

                    CoolButtonGadget(#gadget_Main_Button_Underline, 100, 19, 40, 40, "", "gtk-underline")

                    EditorGadget(#gadget_Main_Editor_TemplateMsg, 5, 75, 615, 303)
                    
                    ReadFile(250, "gtkhtml.pb")
                    While Eof(250) = 0           ; loop as long the 'end of file' isn't reached
  
                              AddGadgetItem(#gadget_Main_Editor_TemplateMsg, -1, ReadString(250) )
                    Wend
                    CloseFile(250)               ; close the previously opened file

                    EditorBuffer = gtk_text_view_get_buffer_(GadgetID(#gadget_Main_Editor_TemplateMsg))	

							
                    gtk_text_buffer_create_tag_(EditorBuffer, "bold", "weight", #PANGO_WEIGHT_BOLD)
                    gtk_text_buffer_create_tag_(EditorBuffer, "italic", "style", #PANGO_STYLE_OBLIQUE)
                    gtk_text_buffer_create_tag_(EditorBuffer, "underline", "underline", #PANGO_UNDERLINE_SINGLE)
					
                    ;pointeur vers la table de tags
                    *TagTable = gtk_text_buffer_get_tag_table_(EditorBuffer)
                                                   
                EndIf
                
                If CreateMenu(#menu_Main, WindowID(#window_Main))
                        MenuTitle("Fichier")
                        MenuItem(#menu_Main_Exit, "Quitter")
                        MenuTitle("Aide")
                        MenuItem(#menu_Main_About, "A Propos")
                EndIf
                
        EndIf
EndProcedure



Procedure ApplyStyle(style.s)

          ;début et fin de la selection de texte
          gtk_text_buffer_get_selection_bounds_(EditorBuffer, @StartIter, @EndIter)

          current_tag = gtk_text_tag_table_lookup_(*TagTable , style)
						
          aucuntag            = #True
	begins              = #False
          ends                = #False
          toggles             = #False
          hastagstart         = #False
          hastagend           = #False
						
          If gtk_text_iter_begins_tag_(@StartIter, current_tag) = #True
                    aucuntag = #False
		begins = #True
	EndIf
	
	If gtk_text_iter_ends_tag_(@EndIter, current_tag) = #True
	          aucuntag = #False
		ends =  #True
	EndIf

	If gtk_text_iter_toggles_tag_(@StartIter,current_tag) = 1
	          aucuntag = #False
		toggles = #True
	EndIf
	
	If gtk_text_iter_has_tag_ (@EndIter,current_tag) = 1
	          aucuntag = #False
		hastagend = #True
	EndIf
	
	If gtk_text_iter_has_tag_ (@StartIter,current_tag) = 1
	          aucuntag = #False
		hastagstart = #True
	EndIf
						
	If aucuntag = #True
	          gtk_text_buffer_apply_tag_by_name_(EditorBuffer, style, @StartIter, @EndIter)
	ElseIf begins = #True And ends = #True And toggles=#True And hastagstart = #True
	          gtk_text_buffer_remove_tag_(EditorBuffer, current_tag, @StartIter, @EndIter)
	ElseIf ends = #True  And hastagstart = #True
	          gtk_text_buffer_remove_tag_(EditorBuffer, current_tag, @StartIter, @EndIter)	
	ElseIf begins = #True  And hastagstart = #True
	          gtk_text_buffer_remove_tag_(EditorBuffer, current_tag, @StartIter, @EndIter)	  
	ElseIf  toggles=#True 
	          gtk_text_buffer_apply_tag_by_name_(EditorBuffer, style, @StartIter, @EndIter)
	ElseIf  hastagstart=#True And hastagend = #False
	          gtk_text_buffer_apply_tag_by_name_(EditorBuffer, style, @StartIter, @EndIter)
	ElseIf  hastagstart=#False And hastagend = #True
	          gtk_text_buffer_apply_tag_by_name_(EditorBuffer, style, @StartIter, @EndIter)
	ElseIf  hastagstart=#True And hastagend = #True
	          gtk_text_buffer_remove_tag_(EditorBuffer, current_tag, @StartIter, @EndIter)	
	EndIf
                        			
EndProcedure





Procedure main()

        Main_Window()

        Repeat
          
                Event = WaitWindowEvent()

                Select Event
     
                        Case #PB_Event_Gadget
                        	Select EventGadget()
                        		Case #gadget_Main_Button_Bold
                                                  ApplyStyle("bold")
                                       Case #gadget_Main_Button_Italic
                                                  ApplyStyle("italic")
                                       Case #gadget_Main_Button_Underline
                                                  ApplyStyle("underline")
                        	EndSelect		
     
                EndSelect

        Until Event = #PB_Event_CloseWindow

EndProcedure


main()