Codebase list ruby-fxruby / cfa4e4c lib / fox16 / core.rb
cfa4e4c

Tree @cfa4e4c (Download .tar.gz)

core.rb @cfa4e4craw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
module Fox

  class FXIconItem
    def <=>(otherItem)
      text <=> otherItem.text
    end
  end

  class FXListItem
    def <=>(otherItem)
      text <=> otherItem.text
    end
  end

  class FXTreeItem
    def <=>(otherItem)
      text <=> otherItem.text
    end
  end

  class FXTreeList
    def addItemFirst(*args) # :nodoc:
      warn "addItemFirst() is deprecated; use prependItem() instead"
      prependItem(*args)
    end

    def addItemLast(*args) # :nodoc:
      warn "addItemLast() is deprecated; use appendItem() instead"
      appendItem(*args)
    end

    def addItemAfter(other, *args) # :nodoc:
      warn "addItemAfter() is deprecated; use insertItem() instead"
      insertItem(other.next, other.parent, *args)
    end

    def addItemBefore(other, *args) # :nodoc:
      warn "addItemBefore() is deprecated; use insertItem() instead"
      insertItem(other, other.parent, *args)
    end

    def reparentItem(item, father) # :nodoc:
      warn "reparentItem() is deprecated; use moveItem() instead"
      moveItem(nil, father, item)
    end

    def moveItemBefore(other, item) # :nodoc:
      warn "moveItemBefore() is deprecated; use moveItem() instead"
      moveItem(other, other.parent, item)
    end

    def moveItemAfter(other, item) # :nodoc:
      warn "moveItemAfter() is deprecated; use moveItem() instead"
      moveItem(other.next, other.parent, item)
    end
  end

  class FXTreeListBox
    def addItemFirst(*args) # :nodoc:
      warn "addItemFirst() is deprecated; use prependItem() instead"
      prependItem(*args)
    end
    def addItemLast(*args) # :nodoc:
      warn "addItemLast() is deprecated; use appendItem() instead"
      appendItem(*args)
    end
    def addItemAfter(other, *args) # :nodoc:
      warn "addItemAfter() is deprecated; use insertItem() instead"
      insertItem(other.next, other.parent, *args)
    end
    def addItemBefore(other, *args) # :nodoc:
      warn "addItemBefore() is deprecated; use insertItem() instead"
      insertItem(other, other.parent, *args)
    end
  end

  class FXDataTarget
    #
    # Returns the stringified representation of this
    # data target's value.
    #
    def to_s
      value.to_s
    end
  end

  class FXDockBar
    # Allow docking on the specified side, where _side_ is one of the +ALLOW+
    # constants listed above.
    def allowSide(side)
      self.allowedSides = self.allowedSides | side
    end

    # Disallow docking on the specified side, where _side_ is one of the
    # +ALLOW+ constants listed above.
    def disallowSide(side)
      self.allowedSides = self.allowedSides & ~side
    end

    # Return +true+ if docking is allowed on the specified side, where _side_
    # is one of the +ALLOW+ constants listed above.
    #
    def allowedSide?(side)
      (allowedSides & side) != 0
    end
  end

  class FXFileDialog
    # Allow navigation for this file dialog
    def allowNavigation
      self.navigationAllowed = true
    end

    # Disallow navigation for this file dialog
    def disallowNavigation
      self.navigationAllowed = false
    end
  end

  class FXFileList
    #
    # Show parent directories.
    #
    def showParentDirs
      self.parentDirsShown = true
    end

    #
    # Hide parent directories
    #
    def hideParentDirs
      self.parentDirsShown = false
    end
  end

  class FXFileSelector
    # Allow navigation for this file selector
    def allowNavigation
      self.navigationAllowed = true
    end

    # Disallow navigation for this file selector
    def disallowNavigation
      self.navigationAllowed = false
    end
  end

  class FXHeader
    #
    # Returns true if the specified header item's arrow points
    # up. Raises IndexError if _index_ is out of bounds.
    #
    def arrowUp?(index)
      if index < 0 || index >= numItems
        raise IndexError, "header item index out of bounds"
      else
        getArrowDir(index) == Fox::TRUE
      end
    end

    #
    # Returns true if the specified header item's arrow points
    # down. Raises IndexError if _index_ is out of bounds.
    #
    def arrowDown?(index)
      if index < 0 || index >= numItems
        raise IndexError, "header item index out of bounds"
      else
        getArrowDir(index) == Fox::FALSE
      end
    end

    #
    # Returns true if the specified header item does not display
    # any arrow. Raises IndexError if _index_ is out of bounds.
    #
    def arrowMaybe?(index)
      if index < 0 || index >= numItems
        raise IndexError, "header item index out of bounds"
      else
        getArrowDir(index) == Fox::MAYBE
      end
    end
  end

  class FXHiliteStyle
    #
    # Construct a new FXHiliteStyle instance, with fields initialized from
    # an FXText instance.
    #
    def FXHiliteStyle.from_text(textw)
      hs = new
      hs.activeBackColor = textw.activeBackColor
      hs.hiliteBackColor = textw.hiliteBackColor
      hs.hiliteForeColor = textw.hiliteTextColor
      hs.normalBackColor = textw.backColor
      hs.normalForeColor = textw.textColor
      hs.selectBackColor = textw.selBackColor
      hs.selectForeColor = textw.selTextColor
      hs.style = 0
      hs
    end
  end

  class FXScrollArea
    # Returns a reference to the scroll corner (an FXScrollCorner instance) for this window.
    def scrollCorner
      verticalScrollBar.next
    end
  end

  class FXSettings
    #
    # Read a boolean registry entry from the specified _section_ and _key_.
    # If no value is found, the _default_ value is returned.
    #
    def readBoolEntry(section, key, default=false)
      default = default ? 1 : 0
      readIntEntry(section, key, default) != 0
    end

    #
    # Write a boolean registry _value_ to the specified _section_ and _key_.
    #
    def writeBoolEntry(section, key, value)
      writeIntEntry(section, key, value ? 1 : 0)
    end
  end

  class FXVec2d
    # Convert to array
    def to_a
      [x, y]
    end

    # Convert to string
    def to_s
      to_a.to_s
    end

    def inspect; to_a.inspect; end
  end

  class FXVec2f
    # Convert to array
    def to_a; [x, y]; end

    # Convert to string
    def to_s; to_a.to_s; end

    def inspect; to_a.inspect; end
  end

  class FXVec3d
    # Convert to array
    def to_a; [x, y, z]; end

    # Convert to string
    def to_s; to_a.to_s; end

    def inspect; to_a.inspect; end
  end

  class FXVec3f
    # Convert to array
    def to_a; [x, y, z]; end

    # Convert to string
    def to_s; to_a.to_s; end

    def inspect; to_a.inspect; end
  end

  class FXVec4d
    # Convert to array
    def to_a; [x, y, z, w]; end

    # Convert to string
    def to_s; to_a.to_s; end

    def inspect; to_a.inspect; end
  end

  class FXVec4f
    # Convert to array
    def to_a; [x, y, z, w]; end

    # Convert to string
    def to_s; to_a.to_s; end

    def inspect; to_a.inspect; end
  end

  class FXWindow
    #
    # Iterate over the child windows for this window.
    # Note that this only reaches the direct child windows for this window
    # and no deeper descendants. To traverse the entire widget tree,
    # use #each_child_recursive.
    #
    def each_child # :yields: child
      child = self.first
      while child
        next_child = child.next
        yield child
        child = next_child
      end
    end

    #
    # Traverse the widget tree starting from this window
    # using depth-first traversal.
    #
    def each_child_recursive # :yields: child
      each_child do |child|
        yield child
        child.each_child_recursive do |subchild|
          yield subchild
        end
      end
    end


    # Returns an array containing all child windows of this window
    def children
      kids = []
      each_child { |kid| kids << kid }
      kids
    end

    # Return +true+ if this window (self) comes before sibling window _other_.
    def before?(other)
      FXWindow.before?(other)
    end

    # Return +true+ if this window (_self_) comes after sibling window _other_,
    def after?(other)
      FXWindow.after?(other)
    end

    # Relink this window before sibling window _other_, in the parent's window list.
    def linkBefore(other)
      reparent(self.parent, other)
    end

    # Relink this window after sibling window _other_, in the parent's window list.
    def linkAfter(other)
      reparent(self.parent, other.next)
    end

    # Setting visible to +true+ calls #show, setting it to +false+ calls #hide.
    def visible=(vis)
      if vis
        show
      else
        hide
      end
    end
  end

  #
  # The drag-and-drop data used for colors is a sequence of unsigned short
  # integers, in native byte ordering. Here, we use the 'S' directive for
  # String#unpack (which treats two successive characters as an unsigned short
  # in native byte order) to decode the R, G, B and A values.
  #
  def Fox.fxdecodeColorData(data)
    clr = data.unpack('S4')
    Fox.FXRGBA((clr[0]+128)/257, (clr[1]+128)/257, (clr[2]+128)/257, (clr[3]+128)/257)
  end

  #
  # The drag-and-drop data used for colors is a sequence of unsigned short
  # integers, in native byte ordering. Here, we use the 'S' directive for
  # Array#pack (which treats two successive characters as an unsigned short
  # in native byte order) to encode the R, G, B and A values.
  #
  def Fox.fxencodeColorData(rgba)
    clr = [ 257*Fox.FXREDVAL(rgba), 257*Fox.FXGREENVAL(rgba), 257*Fox.FXBLUEVAL(rgba), 257*Fox.FXALPHAVAL(rgba) ]
    clr.pack('S4')
  end

  #
  # The drag-and-drop data used for clipboard strings (i.e. when the
  # drag type is FXWindow.stringType) is either a null-terminated
  # string (for Microsoft Windows) or a non-null terminated string
  # (for X11). Use this method to convert string data from the
  # clipboard back into a Ruby string.
  #
  def Fox.fxdecodeStringData(data)
    if /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM
      data.chop
    else
      data
    end
  end

  #
  # The drag-and-drop data used for clipboard strings (i.e. when the
  # drag type is FXWindow.stringType) is either a null-terminated
  # string (for Microsoft Windows) or a non-null terminated string
  # (for X11). Use this method to convert Ruby strings into a format
  # appropriate for the current platform.
  #
  def Fox.fxencodeStringData(str)
    if /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM
      str + "\0"
    else
      str
    end
  end

  #
  # FXStreamError is the base class for exceptions which can occur when
  # working with FXStream and its subclasses.
  #
  class FXStreamError < StandardError
    #
    # This is a factory method that takes an FXStreamStatus code
    # as its input and returns the appropriate exception class.
    #
    def FXStreamError.makeStreamError(status)
      case status
      when FXStreamEnd
        FXStreamEndError
      when FXStreamFull
        FXStreamFullError
      when FXStreamNoWrite
        FXStreamNoWriteError
      when FXStreamNoRead
        FXStreamNoReadError
      when FXStreamFormat
        FXStreamFormatError
      when FXStreamUnknown
        FXStreamUnknownError
      when FXStreamAlloc
        FXStreamAllocError
      when FXStreamFailure
        FXStreamFailureError
      else
        FXStreamError
      end
    end
  end

  # Tried to read past the end of a stream
  class FXStreamEndError < FXStreamError ; end

  # Filled up a stream's internal buffer, or the disk is full
  class FXStreamFullError < FXStreamError ; end

  # Unable to open for write
  class FXStreamNoWriteError < FXStreamError ; end

  # Unable to open for read
  class FXStreamNoReadError < FXStreamError ; end

  # Stream format error
  class FXStreamFormatError < FXStreamError ; end

  # Trying to read unknown class
  class FXStreamUnknownError < FXStreamError ; end

  # Alloc failed
  class FXStreamAllocError < FXStreamError ; end

  # General failure
  class FXStreamFailureError < FXStreamError ; end

  class FXCheckButton
    # Return +true+ if this check button is in the checked state.
    def checked?
      self.checkState == TRUE
    end

    # Return +true+ if this check button is in the unchecked state.
    def unchecked?
      self.checkState == FALSE
    end

    # Return +true+ if this check button is in the indeterminate, or "maybe", state.
    def maybe?
      self.checkState == MAYBE
    end
  end

  class FXComboTableItem < FXTableItem
    #
    # Construct new combobox table item
    #
    def initialize(text, ic=nil, ptr=nil)
      super(nil, ic, ptr)
      self.selections = text
    end

    # Create input control for editing this item
    def getControlFor(table)
      combo = FXComboBox.new(table, 1, :opts => COMBOBOX_STATIC, :padLeft => table.marginLeft, :padRight => table.marginRight, :padTop => table.marginTop, :padBottom => table.marginBottom)
      combo.create
      justify = 0
      justify |= JUSTIFY_LEFT   if (self.justify & FXTableItem::LEFT) != 0
      justify |= JUSTIFY_RIGHT  if (self.justify & FXTableItem::RIGHT) != 0
      justify |= JUSTIFY_TOP    if (self.justify & FXTableItem::TOP) != 0
      justify |= JUSTIFY_BOTTOM if (self.justify & FXTableItem::BOTTOM) != 0
      combo.justify = justify
      combo.font = table.font
      combo.backColor = table.backColor
      combo.textColor = table.textColor
      combo.selBackColor = table.selBackColor
      combo.selTextColor = table.selTextColor
      combo.fillItems(selections)
      combo.text = text
      combo.numVisible = [20, combo.numItems].min
      combo
    end

    # Set value from input control
    def setFromControl(comboBox)
      self.text = comboBox.text
    end

    # Set selections as an array of strings
    def selections=(strings)
      @selections = strings
      if @selections.empty?
        self.text = nil
      else
        self.text = @selections[0]
      end
    end

    # Return selections
    def selections
      @selections
    end
  end

  class FXMenuCheck
    # Return +true+ if this menu check button is in the checked state.
    def checked?
      self.checkState == TRUE
    end

    # Return +true+ if this menu check button is in the unchecked state.
    def unchecked?
      self.checkState == FALSE
    end

    # Return +true+ if this menu check button is in the indeterminate, or "maybe", state.
    def maybe?
      self.checkState == MAYBE
    end
  end

  class FXRadioButton
    # Return +true+ if this radio button is in the checked state.
    def checked?
      self.checkState == TRUE
    end

    # Return +true+ if this radio button is in the unchecked state.
    def unchecked?
      self.checkState == FALSE
    end

    # Return +true+ if this radio button is in the indeterminate, or "maybe", state.
    def maybe?
      self.checkState == MAYBE
    end
  end

  class FXMenuRadio
    # Return +true+ if this menu radio button is in the checked state.
    def checked?
      self.checkState == TRUE
    end

    # Return +true+ if this menu radio button is in the unchecked state.
    def unchecked?
      self.checkState == FALSE
    end

    # Return +true+ if this menu radio button is in the indeterminate, or "maybe", state.
    def maybe?
      self.checkState == MAYBE
    end
  end

  class FXObject
    require 'enumerator'
    def self.subclasses
      ObjectSpace.enum_for(:each_object, class << self; self; end).to_a
    end
  end

  class FXDC
    #
    # Draw a circle centered at (_x_, _y_), with specified radius.
    #
    # === Parameters:
    #
    # +x+::	x-coordinate of the circle's center [Integer]
    # +y+::	y-coordinate of the circle's center [Integer]
    # +r+::	radius of the circle, in pixels [Integer]
    #
    # See also #fillCircle.
    #
    def drawCircle(x, y, r)
      drawArc(x-r, y-r, 2*r, 2*r, 0, 360*64)
    end

    #
    # Draw a filled circle centered at (_x_, _y_), with specified radius.
    #
    # === Parameters:
    #
    # +x+::	x-coordinate of the circle's center [Integer]
    # +y+::	y-coordinate of the circle's center [Integer]
    # +r+::	radius of the circle, in pixels [Integer]
    #
    # See also #drawCircle.
    #
    def fillCircle(x, y, r)
      fillArc(x-r, y-r, 2*r, 2*r, 0, 360*64)
    end
  end

  class FXTable
    #
    # Append _numColumns_ columns to the right of the table..
    # If _notify_ is +true+, a <tt>SEL_INSERTED</tt> message is sent to the
    # table�s message target for each cell that is inserted.
    #
    def appendColumns(numColumns=1, notify=false)
      insertColumns(self.numColumns, numColumns, notify)
    end

    #
    # Append _numRows_ rows to the bottom of the table..
    # If _notify_ is +true+, a <tt>SEL_INSERTED</tt> message is sent to the
    # table�s message target for each cell that is inserted.
    #
    def appendRows(numRows=1, notify=false)
      insertRows(self.numRows, numRows, notify)
    end

    # Select cell at (_row_, _col_).
    # If _notify_ is +true+, a +SEL_SELECTED+ message is sent to the table's message target
    # after the item is selected.
    # Raises IndexError if either _row_ or _col_ is out of bounds.
    #
    def selectItem(row, col, notify=false)
      selectRange(row, row, col, col, notify)
    end

=begin

    # Deselect cell at (_row_, _col_).
    # If _notify_ is +true+, a +SEL_DESELECTED+ message is sent to the table's message target
    # after the item is deselected.
    # Raises IndexError if either _row_ or _col_ is out of bounds.
    #
    def deselectItem(row, col, notify=false)
      raise IndexError, "row index out of bounds" if row < 0 || row >= numRows
      raise IndexError, "column index out of bounds" if col < 0 || col >= numColumns
      deselectRange(row, row, col, col, notify)
    end

    # Deselect range.
    # If _notify_ is +true+, a +SEL_DESELECTED+ message is sent to the table's message
    # target for each previously selected cell that becomes deselected as a result of
    # this operation.
    # Raises IndexError if _startRow_, _endRow_, _startColumn_ or _endColumn_ is out of bounds.
    def deselectRange(startRow, endRow, startColumn, endColumn, notify=false)
      raise IndexError, "starting row index out of bounds"    if startRow < 0 || startRow >= numRows
      raise IndexError, "ending row index out of bounds"      if endRow < 0 || endRow >= numRows
      raise IndexError, "starting column index out of bounds" if startColumn < 0 || startColumn >= numColumns
      raise IndexError, "ending column index out of bounds"   if endColumn < 0 || endColumn >= numColumns
      changes = false
      for row in startRow..endRow
        for col in startColumn..endColumn
          changes |= deselectItem(row, col, notify)
        end
      end
      changes
    end

=end

  end

  class FXImage
    alias initialize_without_data_string initialize
    def initialize(a, pix, *args)
      initialize_without_data_string(a, pix, *args)
      @data_string = (options & IMAGE_OWNED) != 0 ? nil : pix
    end

    alias setPixels_without_data_string setPixels
    def setPixels(pix, *args)
      setPixels_without_data_string(pix, *args)
      @data_string = (options & IMAGE_OWNED) != 0 ? nil : pix
    end
  end
end