2010-05-01 30 views
1

here報價:我如何理解以下內容?

if (to_end) 
    { 
    /* If we want to scroll to the end, including horizontal scrolling, 
    * then we just create a mark with right gravity at the end of the 
    * buffer. It will stay at the end unless explicitely moved with 
    * gtk_text_buffer_move_mark. 
    */ 
    gtk_text_buffer_create_mark (buffer, "end", &iter, FALSE); 

    /* Add scrolling timeout. */ 
    return g_timeout_add (50, (GSourceFunc) scroll_to_end, textview); 
    } 
    else 
    { 
    /* If we want to scroll to the bottom, but not scroll horizontally, 
    * then an end mark won't do the job. Just create a mark so we can 
    * use it with gtk_text_view_scroll_mark_onscreen, we'll position it 
    * explicitely when needed. Use left gravity so the mark stays where 
    * we put it after inserting new text. 
    */ 
    gtk_text_buffer_create_mark (buffer, "scroll", &iter, TRUE); 

    /* Add scrolling timeout. */ 
    return g_timeout_add (100, (GSourceFunc) scroll_to_bottom, textview); 
    } 

雖然有意見相當多的線,我還是不明白其中的邏輯,特別是,什麼是馬克和滾動的位置之間的關係吧

UPDATE

看來,我這個評論誤導:

/* and place the mark at iter. the mark will stay there after we 
    * insert some text at the end because it has right gravity. 
    */ 

說了,scroll商標已經離開重力,不重力,是這樣嗎?

回答

0

檢入GTK文檔後,看起來mark是滾動區域中的某種指定位置,而滾動條的位置是您實際滾動到的緩衝區中的哪個位置。如果您對HTML更熟悉,則看起來像mark稍微等同於您可以通過編程方式滾動到的網頁上的錨點。

+0

但是我沒有看到爲什麼'mark'必須是左**或**右**重力的原因。你能解釋一下嗎? – Gtker 2010-05-01 16:57:57

+0

如果在同一行上添加了文本,「標記」的「重力」控制着「標記」被重新定位到的位置,也就是說,'左'重力將位於文本的左側,而'右'把它放在文本的右邊。就個人而言,似乎是一個相當鈍的命名方案。 – Kitsune 2010-05-01 17:03:38

+0

我不這麼認爲。 'mark'似乎只是緩衝區中的一個點,您可以稍後使用它來滾動(或至少記住以備後用)。 – Kitsune 2010-05-01 17:16:06

0

在滾動功能中,功能gtk_text_view_scroll_mark_onscreen()用於定位滾動條。這似乎是同步文本位置和滾動條位置的最簡單方法。

他們使用兩個函數來模擬「顯示文本的結尾」(即可能包含水平偏移量的東西)和「顯示最後一行文本」(其中水平偏移量保持不變) 。

+0

「scroll_to_end」的左引力和「scroll_to_bottom」的右引力是什麼原因? – Gtker 2010-05-01 17:17:25

1

標記有無論如何到滾動條的位置。

它們使用該代碼中的標記,因爲它對於計算標記位置的快捷方式功能gtk_text_view_scroll_mark_onscreen()非常方便,然後將滾動條移動到該位置。

"end"標記是一個右對齊標記,所以當它們將文本添加到緩衝區的末尾時,標記停留在最後。這樣,當他們scroll_mark_onscreen,垂直和水平滾動條都移動時,顯示最後一行的結尾。

"scroll"標記具有引力。當他們將文本添加到緩衝區的末尾時,他們並不在乎它將文本添加到最後一行的開始的,因爲他們自己將文本添加到緩衝區的末尾。這樣,當他們scroll_mark_onscreen,只有垂直滾動條移動,以顯示開始的最後一行。

他們可能對兩個商標都具有正確的引力,因爲他們並不關心"scroll"這個商標的去向。

這兩個標記都可以留下重力,並且在添加文本時手動將"end"標記移動到最後一行的末尾,但是它們沒有,因爲它們可以自動獲得相同的效果通過給予"end"標記正確的重力。