Simple dialog showing pulldown list

Simple dialog showing pulldown list
 1# simple function to allow the user to choose a palette (uses an array)
 2def palette_chooser1():
 3    the_dialog = impact.gui.make_dialog("Choose a palette")
 4
 5    # we must make an array of the palette names
 6    pal_name_array = []
 7    pal_count = 0
 8
 9    # in this example we will only show the Cut palettes
10    for pal in impact.active_drawing.palettes:
11        if pal.palette_type == ipPaletteType.ptCut:
12
13            # we need to resize the array each time we add an item
14            # Redim (resize the list as needed)
15            pal_name_array[count] = pal.full_name
16            count = count +1
17
18    # now add this field to the dialog
19    field_palette_list = the_dialog.fields.add_combo_box(0, "Palettes", pal_name_array)
20
21    # only one button
22    the_dialog.add_button(ipDialogButtonType.dbtOk)
23
24    # display the dialog
25    the_button = the_dialog.show_modal(None)
26
27    # now find with item the user has chosen (for example the third item), and
28    # find which palette name this corresponds to
29    chosen_pal_item = field_palette_list.value
30    chosen_pal = pal_name_array(chosen_pal_item)
31
32    # finally we return this palette name
33    palette_chooser1 = chosen_pal
34
35
36# simple function to allow the user to choose a palette (uses an arraylist, for easy sorting)
37def palette_chooser2():
38    the_dialog = impact.gui.make_dialog("Choose a palette")
39
40    # we must make an array of the palette names, but we will do this with an arraylist object
41    pal_name_array_list = []
42
43    # in this example we will only show the Cut palettes
44    for pal in impact.active_drawing.palettes:
45        if pal.palette_type == ipPaletteType.ptCut:
46
47            # add the palette name to the arraylist
48            pal_name_array_list.append(pal.full_name)
49
50    # now sort the names alphabetically
51    pal_name_array_list.sort()
52
53    # convert the arraylist to an array
54    pal_name_array = pal_name_array_list.to_array()
55
56    # Wrap variant return items with Impact.py wrapper
57    if pal_name_array is not None:
58        pal_name_array = [IMasterSetting(item) for item in pal_name_array]
59
60    # now add this field to the dialog
61    field_palette_list = the_dialog.fields.add_combo_box(0, "Palettes", pal_name_array)
62
63    # only one button
64    the_dialog.add_button(ipDialogButtonType.dbtOk)
65
66    # display the dialog
67    the_button = the_dialog.show_modal(None)
68
69    # now find with item the user has chosen (for example the third item), and
70    # find which palette name this corresponds to
71    chosen_pal_item = field_palette_list.value
72    chosen_pal = pal_name_array(chosen_pal_item)
73
74    # finally we return this palette name
75    palette_chooser2 = chosen_pal
76
77if not impact.active_drawing.isNone():
78    pal_name = palette_chooser2()
79
80    impact.gui.output_toolbox.add("The chosen palette was '" + str(pal_name) + "'")
81