Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to provide TableModelHandler ColumnType for different columns #26

Closed
AndyObtiva opened this issue Sep 24, 2021 · 4 comments
Closed

Comments

@AndyObtiva
Copy link
Collaborator

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
Copy link
Collaborator Author

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
Copy link
Owner

kojix2 commented Sep 25, 2021

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
Copy link
Owner

kojix2 commented Sep 25, 2021

I see you have resolved the issue.

@kojix2 kojix2 closed this as completed Sep 25, 2021
@AndyObtiva
Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants