Using python macro to create a dialog with a combobox. Would it be possible to block a combobox for typing, but stay active to choose an item in the dropdown list?
I tried this
cbb_test.setEditable(False)
and that
add_widget(‘combobox’, ‘ComboBox’, MARGIN, MARGIN + LBL_HEIGHT + SEP, WIDTH - MARGIN * 2, CBB_HEIGHT, {‘StringItemList’: choices, ‘Text’: default, ‘Dropdown’: True, ‘ReadOnly’: True})
but both cases block the combobox preventing the selection of an item
import uno
from com.sun.star.awt import MessageBoxButtons as MSG_BUTTONS
from com.sun.star.awt.PosSize import POS, SIZE, POSSIZE
from com.sun.star.awt.PushButtonType import OK, CANCEL
from com.sun.star.util.MeasureUnit import TWIP
def create_instance(name, with_context=False):
ctx = XSCRIPTCONTEXT.getComponentContext()
sm = ctx.ServiceManager
if with_context:
instance = sm.createInstanceWithContext(name, ctx)
else:
instance = sm.createInstance(name)
return instance
def msg(message, title='LibreOffice', buttons=MSG_BUTTONS.BUTTONS_OK, type_msg='INFOBOX'):
toolkit = create_instance('com.sun.star.awt.Toolkit')
parent = toolkit.getDesktopWindow()
mb = toolkit.createMessageBox(parent, type_msg, buttons, title, str(message)+'\n\n')
return mb.execute()
def dialog_choice(message, title='', choices=[], default='', x=None, y=None):
#https://api.libreoffice.org/docs/idl/ref/servicecom_1_1sun_1_1star_1_1awt_1_1UnoControlComboBoxModel.html
#https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1awt_1_1XTextComponent.html
WIDTH = 520
MARGIN = 8
BUTTON_WIDTH = 100
BUTTON_HEIGHT = 26
SEP = 8
LBL_HEIGHT = 16
CBB_HEIGHT = 26
HEIGHT = MARGIN * 2 + LBL_HEIGHT + SEP + CBB_HEIGHT + SEP + BUTTON_HEIGHT
dialog = create_instance('com.sun.star.awt.UnoControlDialog', True)
dialog_model = create_instance('com.sun.star.awt.UnoControlDialogModel', True)
dialog.setModel(dialog_model)
dialog.setVisible(False)
dialog.setTitle(title)
dialog.setPosSize(0, 0, WIDTH, HEIGHT, SIZE)
def add_widget(name, type, x_, y_, width_, height_, props):
model = dialog_model.createInstance(f'''com.sun.star.awt.UnoControl{type}Model''')
dialog_model.insertByName(name, model)
control = dialog.getControl(name)
control.setPosSize(x_, y_, width_, height_, POSSIZE)
for key, value in props.items():
setattr(model, key, value)
label_width = WIDTH - BUTTON_WIDTH - SEP - MARGIN * 2
add_widget('label', 'FixedText', MARGIN, MARGIN, label_width,
LBL_HEIGHT, {'Label': str(message), 'NoLabel': True})
add_widget('combobox', 'ComboBox', MARGIN, MARGIN + LBL_HEIGHT + SEP, WIDTH - MARGIN * 2,
CBB_HEIGHT, {'StringItemList': choices, 'Text': default, 'Dropdown': True})
add_widget('btn_ok', 'Button', WIDTH - BUTTON_WIDTH - MARGIN, HEIGHT - MARGIN - BUTTON_HEIGHT,
BUTTON_WIDTH, BUTTON_HEIGHT, {'PushButtonType': OK, 'DefaultButton': True})
add_widget('btn_cancel', 'Button', WIDTH - BUTTON_WIDTH - MARGIN * 2 - SEP - BUTTON_WIDTH,
HEIGHT - MARGIN - BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT, {'PushButtonType': CANCEL})
frame = create_instance('com.sun.star.frame.Desktop', True).getCurrentFrame()
window = frame.getContainerWindow() if frame else None
dialog.createPeer(create_instance('com.sun.star.awt.Toolkit', True), window)
if x is not None and y is not None:
ps = dialog.convertSizeToPixel(uno.createUnoStruct('com.sun.star.awt.Size', x, y), TWIP)
_x, _y = ps.Width, ps.Height
elif window:
ps = window.getPosSize()
_x = ps.Width / 2 - WIDTH / 2
_y = ps.Height / 2 - HEIGHT / 2
dialog.setPosSize(_x, _y, 0, 0, POS)
cbb_test = dialog.getControl('combobox')
cbb_test.setFocus()
#cbb_test.setEditable(False)
if dialog.execute():
value = cbb_test.getText()
else:
value = ''
dialog.dispose()
return value
def test():
choices = ['Option 1', 'Option 2', 'Option 3']
default = choices[0]
result = dialog_choice('Choose an option:', 'Title', choices, default)
if result != '':
msg(result)
return