Class: Rabbit::Canvas

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, GetText
Defined in:
lib/rabbit/canvas.rb

Direct Known Subclasses

DependencyCanvas

Constant Summary collapse

INTERNAL_DPI =
80.0

Constants included from GetText

GetText::DOMAIN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GetText

N_, _, bindtextdomain, locale=

Constructor Details

#initialize(logger, renderer) ⇒ Canvas

Returns a new instance of Canvas.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rabbit/canvas.rb', line 134

def initialize(logger, renderer)
  @logger = logger
  @frame = NullFrame.new
  @theme_name = nil
  @saved_image_base_name = nil
  @saved_image_type = "png"
  @processing = false
  @quitted = false
  @parse_request_queue = []
  @apply_theme_request_queue = []
  @auto_redraw_timer = nil
  @output_html = false
  @rss_base_uri = nil
  @source_filename = nil
  @migemo_dictionary_search_path = []
  @migemo_dictionary_name = nil
  @limit_time = nil
  @use_gl = false
  @font_resolution_ratio = 1
  @twitter = nil
  @max_n_comments = 100
  @allotted_time = nil
  @comment_theme = nil
  clear
  @renderer = renderer.new(self)
  @actions = Action.action_group(self)
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



122
123
124
# File 'lib/rabbit/canvas.rb', line 122

def actions
  @actions
end

#allotted_timeObject



624
625
626
627
628
# File 'lib/rabbit/canvas.rb', line 624

def allotted_time
  time = @allotted_time
  time ||= title_slide.allotted_time if title_slide
  Utils.ensure_time(time)
end

#comment_themeObject

Returns the value of attribute comment_theme.



132
133
134
# File 'lib/rabbit/canvas.rb', line 132

def comment_theme
  @comment_theme
end

#commentsObject (readonly)

Returns the value of attribute comments.



122
123
124
# File 'lib/rabbit/canvas.rb', line 122

def comments
  @comments
end

#font_resolution_ratioObject

Returns the value of attribute font_resolution_ratio.



131
132
133
# File 'lib/rabbit/canvas.rb', line 131

def font_resolution_ratio
  @font_resolution_ratio
end

#last_modifiedObject (readonly)

Returns the value of attribute last_modified.



121
122
123
# File 'lib/rabbit/canvas.rb', line 121

def last_modified
  @last_modified
end

#loggerObject (readonly)

Returns the value of attribute logger.



121
122
123
# File 'lib/rabbit/canvas.rb', line 121

def logger
  @logger
end

#max_n_commentsObject

Returns the value of attribute max_n_comments.



132
133
134
# File 'lib/rabbit/canvas.rb', line 132

def max_n_comments
  @max_n_comments
end

#migemo_dictionary_nameObject

Returns the value of attribute migemo_dictionary_name.



130
131
132
# File 'lib/rabbit/canvas.rb', line 130

def migemo_dictionary_name
  @migemo_dictionary_name
end

#migemo_dictionary_search_pathObject

Returns the value of attribute migemo_dictionary_search_path.



130
131
132
# File 'lib/rabbit/canvas.rb', line 130

def migemo_dictionary_search_path
  @migemo_dictionary_search_path
end

#output_htmlObject

Returns the value of attribute output_html.



128
129
130
# File 'lib/rabbit/canvas.rb', line 128

def output_html
  @output_html
end

#output_index_htmlObject

Returns the value of attribute output_index_html.



128
129
130
# File 'lib/rabbit/canvas.rb', line 128

def output_index_html
  @output_index_html
end

#rendererObject (readonly)

Returns the value of attribute renderer.



121
122
123
# File 'lib/rabbit/canvas.rb', line 121

def renderer
  @renderer
end

#rss_base_uriObject

Returns the value of attribute rss_base_uri.



127
128
129
# File 'lib/rabbit/canvas.rb', line 127

def rss_base_uri
  @rss_base_uri
end

#saved_image_base_nameObject



416
417
418
419
420
421
422
423
424
# File 'lib/rabbit/canvas.rb', line 416

def saved_image_base_name
  if @saved_image_base_name
    base_name = GLib.filename_to_utf8(@saved_image_base_name)
  else
    base_name = title.dup
  end
  base_name << "-index" if index_mode?
  base_name
end

#saved_image_typeObject

Returns the value of attribute saved_image_type.



127
128
129
# File 'lib/rabbit/canvas.rb', line 127

def saved_image_type
  @saved_image_type
end

#source_filenameObject

Returns the value of attribute source_filename.



129
130
131
# File 'lib/rabbit/canvas.rb', line 129

def source_filename
  @source_filename
end

#use_gl=(value) ⇒ Object (writeonly)

Sets the attribute use_gl

Parameters:

  • value

    the value to set the attribute use_gl to.



125
126
127
# File 'lib/rabbit/canvas.rb', line 125

def use_gl=(value)
  @use_gl = value
end

Instance Method Details

#<<(slide) ⇒ Object



302
303
304
305
# File 'lib/rabbit/canvas.rb', line 302

def <<(slide)
  slide.index = slides.size
  slides << slide
end

#action(name) ⇒ Object



605
606
607
608
609
610
611
612
613
# File 'lib/rabbit/canvas.rb', line 605

def action(name)
  act = @actions.get_action(name)
  if act
    act
  else
    logger.warn(_("Unknown action: %s") % name)
    false
  end
end

#activate(name, &block) ⇒ Object



595
596
597
598
599
600
601
602
603
# File 'lib/rabbit/canvas.rb', line 595

def activate(name, &block)
  act = action(name)
  if act and act.sensitive?
    act.activate(&block)
    true
  else
    false
  end
end

#append_comment(comment) ⇒ Object



578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/rabbit/canvas.rb', line 578

def append_comment(comment)
  @comments << comment
  @comments.shift if @comments.size > @max_n_comments
  @on_comment_callbacks.each do |name, callback|
    begin
      callback.call(comment)
    rescue
      logger.error($!)
    end
  end
  true
end

#apply_theme(name = nil, &block) ⇒ Object



307
308
309
# File 'lib/rabbit/canvas.rb', line 307

def apply_theme(name=nil, &block)
  _apply_theme(name, Object.new.__id__, &block)
end

#applying?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/rabbit/canvas.rb', line 170

def applying?
  not @apply_theme_request_queue.empty?
end

#attach_to(frame, window, container = nil, &block) ⇒ Object



188
189
190
191
# File 'lib/rabbit/canvas.rb', line 188

def attach_to(frame, window, container=nil, &block)
  @frame = frame if frame
  @renderer.attach_to(window, container, &block) if window
end

#cache_all_slidesObject



534
535
536
537
538
# File 'lib/rabbit/canvas.rb', line 534

def cache_all_slides
  process do
    @renderer.cache_all_slides
  end
end

#change_current_index(new_index) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/rabbit/canvas.rb', line 274

def change_current_index(new_index)
  mode = @index_mode
  if mode
    index = @index_current_index
    @index_current_index = new_index
  else
    index = @current_index
    @current_index = new_index
  end
  if 0 <= current_index and current_index < slide_size
    yield
  end
ensure
  if mode
    @index_current_index = index
  else
    @current_index = index
  end
end

#current_indexObject



266
267
268
269
270
271
272
# File 'lib/rabbit/canvas.rb', line 266

def current_index
  if @index_mode
    @index_current_index
  else
    @current_index
  end
end

#current_slideObject



256
257
258
259
260
261
262
263
264
# File 'lib/rabbit/canvas.rb', line 256

def current_slide
  slide = slides[current_index]
  if slide
    slide
  else
    move_to_first
    slides.first
  end
end

#delete_on_comment_proc_by_name(name) ⇒ Object



572
573
574
575
576
# File 'lib/rabbit/canvas.rb', line 572

def delete_on_comment_proc_by_name(name)
  @on_comment_callbacks.reject! do |callback_name, callback|
    callback_name == name
  end
end

#detachObject



193
194
195
196
# File 'lib/rabbit/canvas.rb', line 193

def detach
  @frame = NullFrame.new
  @renderer.detach
end

#each(&block) ⇒ Object



298
299
300
# File 'lib/rabbit/canvas.rb', line 298

def each(&block)
  slides.each(&block)
end

#find_slide(pattern) ⇒ Object



233
234
235
# File 'lib/rabbit/canvas.rb', line 233

def find_slide(pattern)
  slides.find {|slide| slide.match?(pattern)}
end

#first_slide?Boolean

Returns:

  • (Boolean)


510
511
512
# File 'lib/rabbit/canvas.rb', line 510

def first_slide?
  current_index.zero?
end

#font_resolutionObject



642
643
644
# File 'lib/rabbit/canvas.rb', line 642

def font_resolution
  INTERNAL_DPI * @font_resolution_ratio
end

#front(public_level = nil) ⇒ Object



184
185
186
# File 'lib/rabbit/canvas.rb', line 184

def front(public_level=nil)
  Front.new(self, public_level)
end

#full_path(path) ⇒ Object



370
371
372
# File 'lib/rabbit/canvas.rb', line 370

def full_path(path)
  @source and @source.full_path(path)
end

#fullscreenedObject



404
405
406
# File 'lib/rabbit/canvas.rb', line 404

def fullscreened
  @renderer.post_fullscreen
end

#have_next?Boolean

Returns:

  • (Boolean)


530
531
532
# File 'lib/rabbit/canvas.rb', line 530

def have_next?
  have_next_slide? or (current_slide and !current_slide.last?)
end

#have_next_slide?Boolean

Returns:

  • (Boolean)


526
527
528
# File 'lib/rabbit/canvas.rb', line 526

def have_next_slide?
  slide_size - 1 > current_index
end

#have_previous?Boolean

Returns:

  • (Boolean)


522
523
524
# File 'lib/rabbit/canvas.rb', line 522

def have_previous?
  have_previous_slide? or (current_slide and !current_slide.first?)
end

#have_previous_slide?Boolean

Returns:

  • (Boolean)


518
519
520
# File 'lib/rabbit/canvas.rb', line 518

def have_previous_slide?
  0 < current_index
end

#iconifiedObject



412
413
414
# File 'lib/rabbit/canvas.rb', line 412

def iconified
  @renderer.post_iconify
end

#index_mode?Boolean

Returns:

  • (Boolean)


473
474
475
# File 'lib/rabbit/canvas.rb', line 473

def index_mode?
  @index_mode
end

#last_slide?Boolean

Returns:

  • (Boolean)


514
515
516
# File 'lib/rabbit/canvas.rb', line 514

def last_slide?
  slide_size.zero? or current_index == (slide_size - 1)
end

#merge_theme(name) ⇒ Object



315
316
317
318
319
320
321
# File 'lib/rabbit/canvas.rb', line 315

def merge_theme(name)
  unless @slides.empty?
    manager = Theme::Manager.new(self)
    manager.apply(name)
    @renderer.post_apply_theme
  end
end

#move_to_firstObject



465
466
467
# File 'lib/rabbit/canvas.rb', line 465

def move_to_first
  move_to_if_can(0)
end

#move_to_if_can(index) ⇒ Object



426
427
428
429
430
431
# File 'lib/rabbit/canvas.rb', line 426

def move_to_if_can(index)
  if index and 0 <= index and index < slide_size
    move_to(index)
  end
  current_index
end

#move_to_lastObject



469
470
471
# File 'lib/rabbit/canvas.rb', line 469

def move_to_last
  move_to(slide_size - 1)
end

#move_to_next_if_canObject



433
434
435
436
437
438
439
440
441
442
443
# File 'lib/rabbit/canvas.rb', line 433

def move_to_next_if_can
  slide = current_slide
  if slide and !slide.last?
    old_index = slide.drawing_index
    slide.move_to_next
    Action.update_status(self)
    @renderer.post_move_in_slide(old_index, slide.drawing_index)
  else
    move_to_next_slide_if_can
  end
end

#move_to_next_slide_if_canObject



445
446
447
# File 'lib/rabbit/canvas.rb', line 445

def move_to_next_slide_if_can
  move_to_if_can(current_index + 1)
end

#move_to_previous_if_canObject



449
450
451
452
453
454
455
456
457
458
459
# File 'lib/rabbit/canvas.rb', line 449

def move_to_previous_if_can
  slide = current_slide
  if slide and !slide.first?
    old_index = slide.drawing_index
    slide.move_to_previous
    Action.update_status(self)
    @renderer.post_move_in_slide(old_index, slide.drawing_index)
  else
    move_to_previous_slide_if_can
  end
end

#move_to_previous_slide_if_canObject



461
462
463
# File 'lib/rabbit/canvas.rb', line 461

def move_to_previous_slide_if_can
  move_to_if_can(current_index - 1)
end

#need_reload_source?Boolean

Returns:

  • (Boolean)


366
367
368
# File 'lib/rabbit/canvas.rb', line 366

def need_reload_source?
  !@processing and @source and @source.modified?
end

#next_slideObject



294
295
296
# File 'lib/rabbit/canvas.rb', line 294

def next_slide
  slides[current_index + 1]
end

#on_comment(name, &callback) ⇒ Object



568
569
570
# File 'lib/rabbit/canvas.rb', line 568

def on_comment(name, &callback)
  @on_comment_callbacks << [name, callback]
end

#parse(source = nil, callback = nil, &block) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/rabbit/canvas.rb', line 327

def parse(source=nil, callback=nil, &block)
  id = Object.new.__id__
  @parse_request_queue.push(id)
  @source = source || @source
  begin
    index = current_index
    keep_index do
      @renderer.pre_parse
      clear
      Parser.parse(self, @source)
      set_current_index(index)
      reload_theme do
        if @parse_request_queue.last != id
          raise ParseFinish
        end
        callback.call if callback
      end
      @renderer.post_parse
      index = current_index
    end
    set_current_index(index)
  rescue ParseFinish
  rescue ParseError, UnsupportedFormatError
    if block_given?
      yield($!)
    else
      logger.warn($!.message)
    end
  ensure
    @parse_request_queue.delete_if {|x| x == id}
  end
end

#parsing?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/rabbit/canvas.rb', line 166

def parsing?
  not @parse_request_queue.empty?
end


398
399
400
401
402
# File 'lib/rabbit/canvas.rb', line 398

def print(&block)
  process do
    @renderer.print(&block)
  end
end

#processing?Boolean

Returns:

  • (Boolean)


540
541
542
# File 'lib/rabbit/canvas.rb', line 540

def processing?
  @processing
end

#quitObject



178
179
180
181
182
# File 'lib/rabbit/canvas.rb', line 178

def quit
  clear_twitter
  @quitted = true
  @frame.quit
end

#quitted?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/rabbit/canvas.rb', line 162

def quitted?
  @quitted
end

#reload_source(callback = nil, &block) ⇒ Object



360
361
362
363
364
# File 'lib/rabbit/canvas.rb', line 360

def reload_source(callback=nil, &block)
  if need_reload_source?
    parse(nil, callback, &block)
  end
end

#reload_theme(&block) ⇒ Object



323
324
325
# File 'lib/rabbit/canvas.rb', line 323

def reload_theme(&block)
  apply_theme(@theme_name, &block)
end

#reset_timerObject



638
639
640
# File 'lib/rabbit/canvas.rb', line 638

def reset_timer
  @limit_time = nil
end

#rest_timeObject



634
635
636
# File 'lib/rabbit/canvas.rb', line 634

def rest_time
  @limit_time ? @limit_time - Time.now : nil
end

#save_as_imageObject



384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/rabbit/canvas.rb', line 384

def save_as_image
  process do
    generator = HTML::Generator.new(self,
                                    saved_image_base_name,
                                    @saved_image_type,
                                    @output_html,
                                    @output_index_html,
                                    @rss_base_uri)
    generator.pdf_filename = filename if /\.pdf/i =~ filename.to_s
    generator.source_filename = @source_filename
    generator.save
  end
end

#slide_index(pattern) ⇒ Object



237
238
239
240
241
242
# File 'lib/rabbit/canvas.rb', line 237

def slide_index(pattern)
  slides.each_with_index do |slide, i|
    return i if slide.match?(pattern)
  end
  nil
end

#slide_indexes(pattern) ⇒ Object



244
245
246
247
248
249
250
# File 'lib/rabbit/canvas.rb', line 244

def slide_indexes(pattern)
  indexes = []
  slides.each_with_index do |slide, i|
    indexes << i if slide.match?(pattern)
  end
  indexes
end

#slide_sizeObject



252
253
254
# File 'lib/rabbit/canvas.rb', line 252

def slide_size
  slides.size
end

#slide_text(index = current_index) ⇒ Object



218
219
220
221
222
223
# File 'lib/rabbit/canvas.rb', line 218

def slide_text(index=current_index)
  return "" if slides.empty?
  slide = slides[index]
  return "" if slide.nil?
  slide.text.strip
end

#slide_title(index = current_index) ⇒ Object



207
208
209
210
211
212
213
214
215
216
# File 'lib/rabbit/canvas.rb', line 207

def slide_title(index=current_index)
  return "" if slides.empty?
  slide = slides[index]
  return title if slide.nil?
  if slide.is_a?(Element::TitleSlide)
    slide.title
  else
    "#{title}: #{slide.title}"
  end
end

#slidesObject



225
226
227
228
229
230
231
# File 'lib/rabbit/canvas.rb', line 225

def slides
  if @index_mode
    @index_slides
  else
    @slides
  end
end

#sourceObject



506
507
508
# File 'lib/rabbit/canvas.rb', line 506

def source
  @source.read
end

#source_force_modified(force_modified) {|@source| ... } ⇒ Object

Yields:



499
500
501
502
503
504
# File 'lib/rabbit/canvas.rb', line 499

def source_force_modified(force_modified)
  prev = @source.force_modified
  @source.force_modified = force_modified
  yield @source
  @source.force_modified = prev
end

#start_auto_redraw_timer(interval) ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/rabbit/canvas.rb', line 544

def start_auto_redraw_timer(interval)
  stop_auto_redraw_timer
  timer = GLib::Timeout.add(interval * 1000) do
    if quitted?
      false
    else
      activate("Redraw")
      true
    end
  end
  @auto_redraw_timer = timer
end

#start_timer(limit) ⇒ Object



630
631
632
# File 'lib/rabbit/canvas.rb', line 630

def start_timer(limit)
  @limit_time = Time.now + limit
end

#stop_auto_redraw_timerObject



557
558
559
560
561
562
563
564
565
566
# File 'lib/rabbit/canvas.rb', line 557

def stop_auto_redraw_timer
  if @auto_redraw_timer
    if GLib::Source.respond_to?(:remove)
      GLib::Source.remove(@auto_redraw_timer)
    else
      Gtk.timeout_remove(@auto_redraw_timer)
    end
    @auto_redraw_timer = nil
  end
end

#theme_nameObject



311
312
313
# File 'lib/rabbit/canvas.rb', line 311

def theme_name
  @theme_name || default_theme || "default"
end

#titleObject



198
199
200
201
202
203
204
205
# File 'lib/rabbit/canvas.rb', line 198

def title
  ts = title_slide
  if ts
    ts.title
  else
    "Rabbit"
  end
end

#title_slideObject



591
592
593
# File 'lib/rabbit/canvas.rb', line 591

def title_slide
  @slides.find{|x| x.is_a?(Element::TitleSlide)}
end

#tmp_dir_nameObject



374
375
376
# File 'lib/rabbit/canvas.rb', line 374

def tmp_dir_name
  @source and @source.tmp_dir_name
end

#to_pixbuf(i) ⇒ Object



378
379
380
381
382
# File 'lib/rabbit/canvas.rb', line 378

def to_pixbuf(i)
  move_to_if_can(i)
  current_slide.flush
  @renderer.to_pixbuf(current_slide)
end

#toggle_index_modeObject



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/rabbit/canvas.rb', line 477

def toggle_index_mode
  @renderer.pre_toggle_index_mode
  process do
    if @index_mode
      @index_mode = false
      @renderer.index_mode_off
    else
      update_index_slides
      @index_mode = true
      @renderer.index_mode_on
    end
    modified
  end
  @renderer.post_toggle_index_mode
end

#twitterObject



646
647
648
# File 'lib/rabbit/canvas.rb', line 646

def twitter
  @twitter ||= Twitter.new(@logger)
end

#unfullscreenedObject



408
409
410
# File 'lib/rabbit/canvas.rb', line 408

def unfullscreened
  @renderer.post_unfullscreen
end

#update_index_slidesObject



493
494
495
496
497
# File 'lib/rabbit/canvas.rb', line 493

def update_index_slides
  if @index_slides.empty?
    @index_slides = Element::IndexSlide.make_index_slides(self)
  end
end

#use_gl?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/rabbit/canvas.rb', line 174

def use_gl?
  @use_gl
end

#with_index_mode(new_value) ⇒ Object



615
616
617
618
619
620
621
622
# File 'lib/rabbit/canvas.rb', line 615

def with_index_mode(new_value)
  current_index_mode = @index_mode
  @index_mode = new_value
  update_index_slides if @index_mode
  yield
ensure
  @index_mode = current_index_mode
end