A P P E N D I X  C

Getters and Setters


Introduction

Getters and setters are provided for those widgets which can have a value or a state. This appendix lists the available getters and setters for each of those widgets. These routines are toolkit-independent. To use them, you must first define a Group containing the widget(s) and then set up a Smart Code callback. For more information on these subjects, see:

The widgets covered here are:


How to Use This Information

A table is provided for each widget which shows the X resource which can be accessed using a getter and a setter. One is the default--this is the one which is accessed from the server. The server simply does a get or a set of the "value" of a widget. The "value" is this default.

For each widget, an example of how to use the getters and setters is given in separate subsections for C, C++ and Java code.


More Information

There are extensive online reference documents. Open the following file in an HTML browser for a list of contents:

$XDROOT/lib/locale/<YourLocale>/sc/index.html

where XDROOT is the install directory of your X-Designer and YourLocale is the locale you are using. If you are unsure about your locale, try typing locale into a terminal window. This prints out your locale information. Use the string assigned to LANG. Example locales are:

In addition, once you have generated code, you have a file called index.html in the directory where your code was generated. This contains hypertext links giving you access to the online reference material.


Label, Button and IconGadget

TABLE C-1 Available Getters/Setters for Label and Button

Resource Name

Type

Value

char *

Default

Sensitive

int


C Code Example

In C code, use the SC_GET and SC_SET macros. These macros take the resource name (i.e. what you are getting/setting) as the first parameter and the Group component as the second. For SC_SET, the third parameter is the new value. See Chapter 16 for more details.

This example gets and sets the value of a label. label1 is a member of group mygroup.

char * val = SC_GET(Value,mygroup->label1);
SC_SET(Value,mygroup->label1,"my label");

C++ Code Example

In C++, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above, it gets and sets the value of a label. "label1" is a member of Group mygroup:

mygroup_c * g = (mygroup_c *) getGroup();
char * val = g->label1->getValue();
g->label1->setValue("my label");

Java Code Example

In Java, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above; it gets and sets the value of a label. "label1" is a member of Group mygroup:

mygroup_c g = (mygroup_c ) getGroup();
String val = g.label1.getValue();
g.label1.setValue("my label");


Toggle

TABLE C-2 Available Getters/Setters for Toggle

Resource Name

Type

State

int[1]

Default

Sensitive

int


C Code Example

In C code, use the SC_GET and SC_SET macros. These macros take the resource name (i.e. what you are getting/setting) as the first parameter and the Group component as the second. For SC_SET, the third parameter is the new value. See Chapter 16 for more details.

This example gets and sets the state (whether or not it is set) of a toggle. "toggle1" and "toggle2" are members of group mygroup.

int state = SC_GET(State,mygroup->toggle1);
SC_SET(State,mygroup->toggle2, state);

C++ Code Example

In C++, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above, it gets the state of one toggle and sets the state of another. "toggle1" and "toggle2" are members of Group mygroup:

mygroup_c * g = (mygroup_c *) getGroup();
int state = g->toggle1->getState();
g->toggle2->setState(state);

Java Code Example

In Java, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above; it gets and sets the state of a toggle. "toggle1" and "toggle2" are members of Group mygroup:

mygroup_c g = (mygroup_c ) getGroup();
boolean state = g.toggle1.getState();
g.toggle2.setState(state);

Note that "state" is a boolean here--for C and C++ it would be an int.


Text Field, Text and Scrolled Text

Getters and Setters for text controls are available through the SC_GET() and SC_SET() macros, defined in groups_c/sc_types.h.

TABLE C-3 Available Getters/Setters for Text

Resource Name

Type

Value

char *

Default

Sensitive

int


C Code Example

In C code, use the SC_GET and SC_SET macros. These macros take the resource name (i.e. what you are getting/setting) as the first parameter and the Group component as the second. For SC_SET, the third parameter is the new value. See Chapter 16 for more details.

This example gets and sets the value (that is, the contents) of a text. text1 is a member of group mygroup.

char * contents = SC_GET(Value,mygroup->text1);
SC_SET(Value,mygroup->text1,"a new string");

C++ Code Example

In C++, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above, it gets and sets the contents of a text. "text1" is a member of Group mygroup:

mygroup_c * g = (mygroup_c *) getGroup();
char * contents = g->text1->getValue();
g->text1->setValue("a new string");

Java Code Example

In Java, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above; it gets and sets the contents of a text. "text1" is a member of Group mygroup:

mygroup_c g = (mygroup_c ) getGroup();
String contents = g.text1.getValue();
g.text1.setValue("a new string");


Scale

TABLE C-4 Available Getters/Setters for Scale

Resource Name

Type

Value

int

Default

Sensitive

int


C Code Example

In C code, use the SC_GET and SC_SET macros. These macros take the resource name (i.e. what you are getting/setting) as the first parameter and the Group component as the second. For SC_SET, the third parameter is the new value. See Chapter 16 for more details.

This example gets and sets the value (that is, the number on the scale) of a scale widget. scale1 is a member of group mygroup.

int val = SC_GET(Value,mygroup->scale1);
SC_SET(Value,mygroup->scale1, 1);

C++ Code Example

In C++, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above, it gets and sets the scale number of a scale widget. "scale1" is a member of Group mygroup:

mygroup_c * g = (mygroup_c *) getGroup();
int scaleValue = g->scale1->getValue();
g->scale1->setValue(1);

Java Code Example

In Java, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above; it gets and sets the scale number of a scale widget. "scale1" is a member of Group mygroup:

mygroup_c g = (mygroup_c ) getGroup();
int scaleValue = g.scale1.getValue();
g.scale1.setValue(1);


List, Scrolled List and SimpleSpinBox

TABLE C-5 Available Getters/Setters for List

Resource Name

Type

Items

char **

Sensitive

int

SelectedItems

char **

Default


C Code Example

In C code, use the SC_GET and SC_SET macros. These macros take the resource name (i.e. what you are getting/setting) as the first parameter and the Group component as the second. For SC_SET, the third parameter is the new value. See Chapter 16 for more details.

This example gets and sets the list of selected items of the list widget. list1 is a member of group mygroup. The list of selected items is a null terminated array of strings.

char ** my_stringlist = SC_GET(SelectedItems,mygroup->list1);
SC_SET(SelectedItems,mygroup->list1,a_new_stringlist);

C++ Code Example

In C++, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above, it gets and sets the selected items in a list widget. "list1" is a member of Group mygroup. The list of selected items is a null terminated array of strings.

mygroup_c * g = (mygroup_c *) getGroup();
char ** my_stringlist = g->list1->getSelectedItems();
g->list1->setSelectedItems(a_new_stringlist);

Java Code Example

In Java, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above; it gets and sets the selected items in a list widget. "list1" is a member of Group mygroup. Use the Java built-in "<array>.length" to find out how many Strings there are in the array.

mygroup_c g = (mygroup_c ) getGroup();
String [] my_stringlist = g.list1.getSelectedItems();
int how_many = my_stringlist.length;
g.list1.setSelectedItems(a_new_stringlist);


Option Menu and ComboBox

TABLE C-6 Available Getters/Setters for Option Menu

Resource Name

Type

Label

char *

Sensitive

int

SelectionByName[2]

char *

SelectionByIndex

int

Default


C Code Example

In C code, use the SC_GET and SC_SET macros. These macros take the resource name (i.e. what you are getting/setting) as the first parameter and the Group component as the second. For SC_SET, the third parameter is the new value. See Chapter 16 for more details.

This example gets and sets the selected item in the optionmenu. optionMenu1 is a member of group mygroup.

char * val = SC_GET(SelectionByName, mygroup->optionMenu1);
SC_SET(SelectionByName,mygroup->optionMenu1, "Option 1");

C++ Code Example

In C++, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above, it gets and sets the selected item in the option menu. "optionMenu1" is a member of Group mygroup:

mygroup_c * g = (mygroup_c *) getGroup();
char * val = g->optionMenu1->getSelectionByName();
g->optionMenu1->setSelectionByName("Option 2");

Java Code Example

In Java, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above; it gets and sets the selected item in the option menu. "optionMenu1" is a member of Group mygroup:

mygroup_c g = (mygroup_c ) getGroup();
String val = g.optionMenu1.getSelectionByName();
g.optionMenu1.setSelectionByName("Option 3");


Radio Box

TABLE C-7 Available Getters/Setters for Radio Box

Resource Name

Type

Label

char *

Sensitive

int

SelectionByName[3]

char *

SelectionByIndex

int

Default


C Code Example

In C code, use the SC_GET and SC_SET macros. These macros take the resource name (i.e. what you are getting/setting) as the first parameter and the Group component as the second. For SC_SET, the third parameter is the new value. See Chapter 16 for more details.

This example gets and sets the label of the selected item in a radiobox widget. radiobox1 is a member of group mygroup.

char * str = SC_GET(SelectionByName, mygroup->radiobox1);
SC_SET(SelectionByName,mygroup->radiobox1, "The text to show");

C++ Code Example

In C++, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above, it gets and sets the label of the selected item in a radiobox widget. "radiobox1" is a member of Group mygroup:

mygroup_c * g = (mygroup_c *) getGroup();
char * str = g->radiobox1->getSelectionByName();
g->radiobox1->setSelectionByName("The text to show");

Java Code Example

In Java, the Group is a class, the group member is a variable of this class and is a class itself and the getters and setters are methods of the Group member's class.

This example is the same as the C code example above; it gets and sets the scale number of a scale widget. "scale1" is a member of Group mygroup:

mygroup_c g = (mygroup_c ) getGroup();
String str = g.radiobox1.getSelectionByName();
g.radiobox1.setSelectionByName("The text to show");

 


1 (TableFootnote) For Java, State is boolean.
2 (TableFootnote) This is the string being displayed and not the widget name.
3 (TableFootnote) This is the string being displayed and not the widget name.