kojix2 / LibUI

A portable GUI library for Ruby
MIT License
208 stars 10 forks source link

How to provide TableModelHandler ColumnType for different columns #26

Closed AndyObtiva closed 3 years ago

AndyObtiva commented 3 years ago

Hi,

I have a new question. I noticed that in your implementation of examples/basic_table.rb you provide a single static value for model_handler.ColumnType:

model_handler.ColumnType   = rbcallback(4) { 0 }

examples/basic_table_image.rb also had similar code:

model_handler.ColumnType   = rbcallback(4) { 1 } # Image

This works well if all columns are the same type, but what if the columns are of different types like one text column and one image column? How do you modify the code to handle the variety of columns? And, do you have a quick link/reference for what each column type value is for all column types?

I'd be very grateful for any help with this.

Andy

AndyObtiva commented 3 years ago

OK, nevermind. I figured it out by typing in the following:

@model_handler.ColumnType   = rbcallback(4, [1, 1, 4]) do |_, _, column|
  # return value based on column
kojix2 commented 3 years ago

Hi Andy!

Test page16.c in andlabs/libui is a good reference on how to use the table. The executables for these tests can be downloaded from the release page. (Also, issue #310 shows what andlabs and others had in mind when they designed libui table.)

page16.c screenshot image

Here, they use if statements to set the type of each column.

https://github.com/andlabs/libui/blob/fea45b2d5b75839be0af9acc842a147c5cba9295/test/page16.c#L11-L20

static uiTableValueType modelColumnType(uiTableModelHandler *mh, uiTableModel *m, int column)
{
    if (column == 3 || column == 4)
        return uiTableValueTypeColor;
    if (column == 5)
        return uiTableValueTypeImage;
    if (column == 7 || column == 8)
        return uiTableValueTypeInt;
    return uiTableValueTypeString;
}
mh.ColumnType = modelColumnType;

In Ruby, it would be something like this:

model_handler.ColumnType = rbcallback(4, [1, 1, 4]) do | _mh, _m, column|
  case column
  when 3, 4
    3
  when 5
    1
  when 7, 8
    2
  else
    0
  end
end 

All column types can be referenced from here.

https://github.com/andlabs/libui/blob/fea45b2d5b75839be0af9acc842a147c5cba9295/ui.h#L1199-L1204

_UI_ENUM(uiTableValueType) {
    uiTableValueTypeString,
    uiTableValueTypeImage,
    uiTableValueTypeInt,
    uiTableValueTypeColor,
};

Currently, there is no way to reference uiTableValueType in Ruby's LibUI, so it might be better to define constants.

Thank you!


           ↓
rbcallback(4, [1, 1, 4])

uiTableValueType is an enumeration type. In fiddle, enumeration constants are fine with 4 (int). This may not be documented. I asked the developer of fiddle directly in the online forum.

kojix2 commented 3 years ago

I see you have resolved the issue.

AndyObtiva commented 3 years ago

Thanks a lot for the response. I did need to figure out the numbers to return for each column type, so this is very helpful.