Editor text color? (GTK/GTK2)

Linux specific forum
Kaiser
Enthusiast
Enthusiast
Posts: 118
Joined: Tue Jan 11, 2005 8:36 am

Editor text color? (GTK/GTK2)

Post by Kaiser »

Yeah, I know how to do it under Windows (or at least, have some code to do it real cool), but what about doing it under Linux? can't use WinAPI on Linux so... yeah, been wondering that :(. Also, how to make the editorgadget transparent as in X-Chat?... just those two, thanks :).
StanDan
User
User
Posts: 57
Joined: Sun Feb 26, 2006 3:43 am
Location: Missouri, United States

This may help.

Post by StanDan »

I've been programming in GTK for a long time, but I've never tried to do this before. Man was I suprised. I was working on a similar project when I found your post. It's harder than I would imagine. In Gtk+ (i.e. Gtk 2.0+) It's easy.

Code: Select all

  // in C
  GdkColor color;
  gdk_color_parse ("red", &color);
  gtk_widget_modify_fg (widget, GTK_STATE_NORMAL, &color);
But I can't seem to get PureBasic to use Gtk+. It seems to be using the old 1.3 version, at least on my machine. If PureBasic only uses Gtk 1.x (as I suspect) then it won't help, but you could try.

Code: Select all

  Debug "gtk-version:"+Str(#GTK_MAJOR_VERSION)+"."+Str(#GTK_MINOR_VERSION)+"."+Str(#GTK_MICRO_VERSION)
My installation reports "gtk-version: 1.3.0". Anyway I tried really hard to do it and haven't quite succeeded yet. I can add text with color to the widget, but I can't set the default text color. This code should work but it won't for some reason.

Code: Select all

Enumeration
	#MainWindow
	#EditorGadget
	#ChangeText
EndEnumeration

If OpenWindow(#MainWindow, 100, 200, 260, 260, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget, "Text Entry Colors")
	Top.l = 220
	If CreateGadgetList(WindowID())
		If EditorGadget(#EditorGadget, 10, 10, 240, 200)
			For i=1 To 7
				AddGadgetItem(#EditorGadget, i, "Line: "+Str(i))
			Next
			Debug "GadgetID(): 0x"+Hex(GadgetID(#EditorGadget))
			Debug "#EditorGadget: "+Str(#EditorGadget)
		EndIf
		ButtonGadget(#ChangeText, 10, Top, 70, 20, "Change")
	EndIf
        *editor.GtkWidget
	*editor = GadgetID(#EditorGadget)
	*tc.GdkColor
	*tf.l
	color.GdkColor
	color\red = 65535
	color\green = 0
	color\blue = 0
	gdk_color_parse_("red", @color)
	*rc_style.GtkRcStyle = gtk_rc_style_new_()
	*tc = *rc_style\fg
	*tf = *rc_style\color_flags
	PokeL(*tc, @color) ; *tc = rc_style->fg[0] = fg[ GTK_STATE_NORMAL ]
	Debug *tf
	*rc_style\color_flags = #GTK_RC_FG
	Debug *rc_style\color_flags
	; Should be this, but rc_style is NULL
	; PokeL(*tf, PeekL(*tf) | #GTK_RC_FG
	gtk_widget_modify_style_(*editor, *rc_style);
        gtk_rc_style_unref_(*rc_style);
	Repeat
		EventID = WaitWindowEvent()
		If EventGadgetID() = #ChangeText
			Debug "Button pushed!"
			gtk_text_insert_(*editor, 0, @color, 0, "colored", -1);
		EndIf
	Until EventID = #PB_Event_CloseWindow
EndIf
The button will add some red text to the widget, but the default color is not changed. I also tried poking a value into the fg array in the GtkStyle object that's part of the editor widget but to no avail. Oh well, maybe an actual guru might have something to say about this.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

you tried to compile with the 'gtk2' subsystem ?
StanDan
User
User
Posts: 57
Joined: Sun Feb 26, 2006 3:43 am
Location: Missouri, United States

And the neophyte bows to the master.

Post by StanDan »

Thanks Fred!

Didn't know about the Gtk2 subsystem. :oops:

But that changes things. Man, pb is so cool.

Now it's working.

Code: Select all

Enumeration
	#MainWindow
	#EditorGadget
	#GreenTextB
	#ChangeTextB
EndEnumeration

If OpenWindow(#MainWindow, 100, 200, 260, 260, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget, "Text Entry Colors")
	Top.l = 220                    ; Top of button row.
	*editor.GtkWidget           ; Pointer to the Gtk+ editor gadget.
	color_green.GdkColor     ; GdkColor green
	color_red.GdkColor         ; GdkColor red
	textstate.l = 0                 ; 0 = red 1 = green
	gdk_color_parse_("green", @color_green);
  gdk_color_parse_("red", @color_red);
	If CreateGadgetList(WindowID())
		If EditorGadget(#EditorGadget, 10, 10, 240, 200)
	    *editor = GadgetID(#EditorGadget)
      gtk_widget_modify_text_(*editor, #GTK_STATE_NORMAL, @color_red)
			For i=1 To 7
				AddGadgetItem(#EditorGadget, i, "Line: "+Str(i))
			Next
		EndIf
		ButtonGadget(#GreenTextB, 10, Top, 90, 30, "Add Green")
		ButtonGadget(#ChangeTextB, 110, Top, 90, 30, "Change")
	EndIf
	Repeat
		EventID = WaitWindowEvent()
		If EventGadgetID() = #GreenTextB
			gtk_text_insert_(*editor, 0, @color_green, 0, "green!", -1);
		ElseIf EventGadgetID() = #ChangeTextB
		  If textstate = 0
		    gtk_widget_modify_text_(*editor, #GTK_STATE_NORMAL, @color_green)
    		textstate = 1
    	Else
	      gtk_widget_modify_text_(*editor, #GTK_STATE_NORMAL, @color_red)
      	textstate = 0
      EndIf
		EndIf
	Until EventID = #PB_Event_CloseWindow
EndIf
bender-rulez
User
User
Posts: 49
Joined: Mon Mar 14, 2005 11:30 am

Post by bender-rulez »

Hi, nice code!
but how to save colored text to harddisk and reload it again in that format?

how to mark text in the editor gadget and color it, specially remark it again and make it bold (in the color the text was)?

Thanks!
Post Reply