• R/O
  • SSH
  • HTTPS

edmaru: 提交


Commit MetaInfo

修订版48 (tree)
时间2007-04-22 23:19:20
作者bluedwarf

Log Message

Scroll function for Gtk mode.

更改概述

差异

--- gtk/gtk_window.rb (revision 47)
+++ gtk/gtk_window.rb (revision 48)
@@ -26,7 +26,7 @@
2626
2727 #Create TextView widget for this window.
2828 def init_ui
29- @start_y = 0
29+ @p_start_row = 0
3030
3131 @sub_hbox = Gtk::HBox.new(false, 0)
3232 @sub_hbox.show
@@ -189,12 +189,109 @@
189189 @main_vbox
190190 end
191191
192+ def cursor_logical_to_physical(l_cursor)
193+ p_cursor = Edmaru::Cursor.new
194+ return p_cursor if @pango_layout == nil
195+
196+ index = 0
197+ if l_cursor.row == 0
198+ index = l_cursor.column
199+ else
200+ for n in 0 ... l_cursor.row
201+ index += @buffer.lines[n].size + 1
202+ end
203+ index += l_cursor.column
204+ end
205+
206+ p_cursor.row = 0
207+ @pango_layout.lines.each{ |line_layout|
208+ if index < line_layout.start_index + line_layout.length
209+ p_cursor.column = index - line_layout.start_index
210+
211+ if p_cursor.column < 0
212+ p_cursor.row -= 1
213+ line_layout = @pango_layout.lines[p_cursor.row]
214+ p_cursor.column = line_layout.length
215+ end
216+ return p_cursor
217+ end
218+ p_cursor.row += 1
219+ }
220+
221+ p_cursor.row -= 1
222+ line_layout = @pango_layout.lines[p_cursor.row]
223+ p_cursor.column = line_layout.length
224+ return p_cursor
225+ end
226+
227+ def cursor_physical_to_logical(p_cursor)
228+ l_cursor = Edmaru::Cursor.new
229+ return l_cursor if @pango_layout == nil
230+
231+ line_layout = @pango_layout.lines[p_cursor.row]
232+ index = line_layout.start_index + p_cursor.column
233+
234+ i = 0
235+ l_cursor.row = 0
236+ @buffer.lines.each{ |line|
237+ if index < (i + line.size)
238+ l_cursor.column = index - i
239+
240+ if l_cursor.column < 0
241+ l_cursor.row -= 1
242+ l_cursor.column = @buffer.lines[l_cursor.row].size
243+ end
244+ return l_cursor
245+ end
246+ i += line.size + 1
247+ l_cursor.row += 1
248+ }
249+
250+ l_cursor.row -= 1
251+ l_cursor.column = @buffer.lines[l_cursor.row].size
252+ return l_cursor
253+ end
254+
255+ def max_row
256+ width, height = @wrap_sign_layout.pixel_size
257+ return (@drawing_area.allocation.height / height).to_i
258+=begin
259+ row = 0
260+
261+ while (row + @p_start_row) < @pango_layout.line_count
262+ index = @pango_layout.lines[row + @p_start_row].start_index
263+ rect = @pango_layout.index_to_pos(index)
264+ if (rect.y + rect.height) / Pango::SCALE > @drawing_area.allocation.height
265+ return row
266+ end
267+
268+ row += 1
269+ end
270+
271+ return row
272+=end
273+ end
274+
192275 def refresh_cursor
276+ return if @pango_layout == nil
277+
193278 if @modeline != nil
194279 @modeline.set_element_text("cursor-position",
195280 "(#{@cursor.row},#{@cursor.column})")
196281 end
197282
283+ p_cursor = cursor_logical_to_physical(@cursor)
284+
285+ if (p_cursor.row - @p_start_row) >= max_row
286+ while (p_cursor.row - @p_start_row) >= max_row
287+ scroll_down
288+ end
289+ elsif (p_cursor.row - @p_start_row) < 0 && @p_start_row > 0
290+ while (p_cursor.row - @p_start_row) < 0 && @p_start_row > 0
291+ scroll_up
292+ end
293+ end
294+
198295 refresh
199296 end
200297
@@ -210,8 +307,14 @@
210307
211308 #Draw text
212309 @pango_layout.text = @buffer.lines.join("\n")
213- @drawing_area.window.draw_layout(@gdk_gc, 0, -@start_y, @pango_layout)
214310
311+ line_layout = @pango_layout.get_line(@p_start_row)
312+ first_rect = @pango_layout.index_to_pos(line_layout.start_index)
313+
314+ @drawing_area.window.draw_layout(@gdk_gc,
315+ 0, -(first_rect.y / Pango::SCALE).to_i,
316+ @pango_layout)
317+
215318 draw_wrap_sign
216319
217320 #Cursor
@@ -235,9 +338,14 @@
235338 @cursor_layout.text = @buffer.lines[@cursor.row][@cursor.column].chr
236339 end
237340 rect = @pango_layout.index_to_pos(index)
341+
342+ start_index = @pango_layout.lines[@p_start_row].start_index
343+ start_rect = @pango_layout.index_to_pos(start_index)
344+ start_y = (start_rect.y / Pango::SCALE).to_i
345+
238346 @drawing_area.window.draw_layout(@gdk_back_gc,
239347 (rect.x / Pango::SCALE).to_i,
240- (rect.y / Pango::SCALE).to_i - @start_y,
348+ (rect.y / Pango::SCALE).to_i - start_y,
241349 @cursor_layout,
242350 @gtk_background_color,
243351 @gtk_foreground_color)
@@ -244,15 +352,21 @@
244352 end
245353
246354 def draw_wrap_sign
355+ @p_start_row = 0 if @p_start_row == nil
356+
247357 #Clear
248358 @wrap_sign_area.window.draw_rectangle(@gdk_wrap_sign_bg_gc, true,
249359 0, 0, -1, -1)
250360
251- @pango_layout.line_count.times{ |n|
361+ start_index = @pango_layout.lines[@p_start_row].start_index
362+ start_rect = @pango_layout.index_to_pos(start_index)
363+ start_y = (start_rect.y / Pango::SCALE).to_i
364+
365+ for n in @p_start_row ... @pango_layout.line_count
252366 layout_line = @pango_layout.get_line(n)
253367 rect = @pango_layout.index_to_pos(layout_line.start_index)
254368
255- if (rect.y / Pango::SCALE).to_i > @wrap_sign_area.allocation.height
369+ if (rect.y / Pango::SCALE).to_i - start_y > @wrap_sign_area.allocation.height
256370 break
257371 end
258372
@@ -261,10 +375,10 @@
261375 @pango_layout.text[last_index] != nil
262376 @wrap_sign_area.window.draw_layout(@gdk_wrap_sign_fg_gc,
263377 0,
264- (rect.y / Pango::SCALE).to_i - @start_y,
378+ (rect.y / Pango::SCALE).to_i - start_y,
265379 @wrap_sign_layout)
266380 end
267- }
381+ end
268382 end
269383
270384 #Scroll down this window.
@@ -271,7 +385,45 @@
271385 def scroll_down
272386 @view.hide_alert
273387
274- @start_y += 10
388+ end_l_cursor = Edmaru::Cursor.new
389+ end_l_cursor.row = @buffer.lines.size - 1
390+ end_l_cursor.column = @buffer.lines[end_l_cursor.row].size
391+
392+ end_p_cursor = cursor_logical_to_physical(end_l_cursor)
393+ end_p_row = end_p_cursor.row
394+
395+ if @p_start_row + max_row > end_p_row
396+ @view.show_alert("End of buffer")
397+ return
398+ end
399+
400+ prev_p_start_row = @p_start_row
401+ if max_row != 1
402+ @p_start_row += (max_row / 2).to_i
403+ else
404+ @p_start_row += 1
405+ end
406+
407+ if @p_start_row > end_p_row
408+ @p_start_row = prev_p_start_row
409+ end
410+
411+ p_cursor = cursor_logical_to_physical(@cursor)
412+ if p_cursor.row < @p_start_row
413+ if max_row != 1
414+ p_cursor.row += (max_row / 2).to_i
415+ else
416+ p_cursor.row += 1
417+ end
418+ p_cursor.column = 0
419+ l_cursor = cursor_physical_to_logical(p_cursor)
420+ if l_cursor != nil
421+ @cursor = l_cursor
422+ else
423+ @cursor = end_l_cursor
424+ end
425+ end
426+
275427 refresh
276428 end
277429
@@ -279,8 +431,35 @@
279431 def scroll_up
280432 @view.hide_alert
281433
282- @start_y -= 10
283- @start_y = 0 if @start_y < 0
434+
435+ if @p_start_row == 0
436+ @view.show_alert("Beginning of buffer")
437+ return
438+ end
439+
440+ if max_row != 1
441+ @p_start_row -= (max_row / 2)
442+ else
443+ @p_start_row -= 1
444+ end
445+ @p_start_row = 0 if @p_start_row < 0
446+
447+ p_cursor = cursor_logical_to_physical(@cursor)
448+ if p_cursor.row >= @p_start_row + max_row
449+ if max_row != 1
450+ p_cursor.row -= (max_row / 2).to_i
451+ else
452+ p_cursor.row -= 1
453+ end
454+ p_cursor.column = 0
455+ l_cursor = cursor_physical_to_logical(p_cursor)
456+ if l_cursor != nil
457+ @cursor = l_cursor
458+ else
459+ @cursor.row = @cursor.column = 0
460+ end
461+ end
462+
284463 refresh
285464 end
286465
--- ChangeLog (revision 47)
+++ ChangeLog (revision 48)
@@ -1,3 +1,7 @@
1+2007-04-22 Takashi Nakamoto <bluedwarf@bpost.plala.or.jp>
2+
3+ * Scroll function for Gtk mode.
4+
15 2007-04-21 Takashi Nakamoto <bluedwarf@bpost.plala.or.jp>
26
37 * New: meta commands
Show on old repository browser