gpt4 book ai didi

c - GtkSourceView/GtkSourceBuffer - 如何用红色标记一行并显示一个图标

转载 作者:太空宇宙 更新时间:2023-11-04 03:24:31 26 4
gpt4 key购买 nike

我想在 GtkSourceView 中用红色标记一些文本并显示一个图标。在这里,我目前的尝试似乎什么也没做:

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 );
}

printf 打印出正确的行号。

如果我调用该方法两次,gtk 会打印一些 gtk-critical:“Mark myMark already exists in the buffer”。

但是在 gtksourceview 上根本没有可见的变化。

也许我应该用 gtk_source_mark_attributes 来改变特定类别的属性?但是怎么办?我找不到任何关于如何使用它的好信息。

最佳答案

好吧,我自己想通了。这里的版本将背景标记为红色并显示一个小的错误对话框项目。如果鼠标悬停在项目上,将显示一条错误消息。

//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]);
}

关于c - GtkSourceView/GtkSourceBuffer - 如何用红色标记一行并显示一个图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42403102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com