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.