2017-02-22 5 views
1

Я хотел бы отметить некоторый текст в GtkSourceView в красном и показать значок. Вот моя текущая попытка, которая, кажется, ничего не делает:GtkSourceView/GtkSourceBuffer - Как пометить строку в красном и показать значок

void plainTextEditor_textView_addLineMarker(int lineNumber, linemarker* marker, context_base* context) 
{ 
    GtkWidget* plainTextEditor_textView = get_plainTextEditor_textView_from_notebook(context->notebook); 
    GtkTextIter iter; 
    GtkTextBuffer * buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(plainTextEditor_textView)); 
    gtk_text_buffer_get_iter_at_line(buffer, &iter, lineNumber); 
    printf("extendedEditor_linebox_markError %i\n", lineNumber); 
    gtk_source_buffer_create_source_mark(GTK_SOURCE_BUFFER(buffer), marker->message, "dialog-error", &iter); 
} 

Печать напечатала правильный линимент.

Если я вызываю метод дважды, GTK печатает некоторые GTK-критический: «Марк myMark уже существует в буфере».

Однако нет никаких видимых изменений на всех на gtksourceview.

Вероятно, я должен сделать что-то с gtk_source_mark_attributes, чтобы изменить свойства для определенной категории? Но как ? Я не могу найти хорошую информацию о том, как ее использовать.

ответ

1

Хорошо, я сам это понял. Здесь версия, которая отмечает красный фон и отображает небольшой элемент диалогового окна ошибки. Если мышь завис над элементом, появится сообщение об ошибке.

//data which is needed in different methods 
typedef struct 
{ 
    GtkSourceMarkAttributes* plainTextEditor_lineMarkers_warningAttributes; 
    GtkSourceMarkAttributes* plainTextEditor_lineMarkers_errorAttributes; 
    ... 
} context_base; 

// things to do only once 
int main(int argc, char *argv[]) 
{ 
    ... 
    GtkWidget * plainTextEditor_textView = gtk_source_view_new(); 
    gtk_source_view_set_highlight_current_line (GTK_SOURCE_VIEW(plainTextEditor_textView),TRUE); 
    gtk_source_view_set_show_line_numbers (GTK_SOURCE_VIEW(plainTextEditor_textView),TRUE); 
    gtk_source_view_set_show_line_marks (GTK_SOURCE_VIEW(plainTextEditor_textView), TRUE); 

    context->plainTextEditor_lineMarkers_errorAttributes = gtk_source_mark_attributes_new(); 
    gtk_source_mark_attributes_set_background(context->plainTextEditor_lineMarkers_errorAttributes, &error_color); 
    gtk_source_mark_attributes_set_icon_name(context->plainTextEditor_lineMarkers_errorAttributes,"dialog-error"); 
    gtk_source_view_set_mark_attributes(GTK_SOURCE_VIEW(plainTextEditor_textView), sourceMarkCategory_error, context->plainTextEditor_lineMarkers_errorAttributes, 10); 
    ... 
} 

// callback to display message when hovering on the linemarker 
gchar* on_lineMarkerTooltip_displayed(GtkSourceMarkAttributes *attributes, GtkSourceMark *mark, linemarker* marker) 
{ 
    if(marker->message == NULL) 
     return NULL; 
    return strdup(marker->message); 
} 

// method to create new linemark 
void plainTextEditor_textView_addLineMarker(int lineNumber, linemarker* marker, context_base* context) 
{ 
    if(context->plainTextEditor_lineMarkers[lineNumber-1]->message != NULL) // there is a message on this line 
    { 
     if(strcmp(context->plainTextEditor_lineMarkers[lineNumber-1]->message, marker->message) == 0) // its the same message, nothing to do 
     { 
      return; 
     } 
    } 
    GtkWidget* plainTextEditor_textView = get_plainTextEditor_textView_from_notebook(context->notebook); 
    GtkTextIter iter; 
    GtkTextBuffer * buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(plainTextEditor_textView)); 
    gtk_text_buffer_get_iter_at_line(buffer, &iter, lineNumber); 

    context->plainTextEditor_lineMarkers[lineNumber-1]->message = strdup(marker->message); 

    char sourceMarkName[sourceMarkNameMaxDigits]; 
    snprintf(sourceMarkName, sourceMarkNameMaxDigits, "%i", lineNumber); 
    gtk_source_view_set_mark_attributes (GTK_SOURCE_VIEW(plainTextEditor_textView),sourceMarkCategory_error,context->plainTextEditor_lineMarkers_errorAttributes,10); 
    gtk_source_buffer_create_source_mark(GTK_SOURCE_BUFFER(buffer), sourceMarkName, sourceMarkCategory_error, &iter); 
    g_signal_connect(G_OBJECT(context->plainTextEditor_lineMarkers_errorAttributes), "query-tooltip-text", G_CALLBACK(on_lineMarkerTooltip_displayed), context->plainTextEditor_lineMarkers[lineNumber-1]); 
} 
Смежные вопросы