libXm get display number from widget - xlib

I'm using libXm4.0.3. I want to get the display number that the widget is being displayed on from the widget.
Going directly to xlib causes a segfault. Xt seems to also segfault.
I started using some of the XmGetXmDisplay or XmGetXmScreen functions, but I'm seeing "Could not find per display information" and I don't know how to overcome this.
How can I get the current display number from a libXm widget?
This is my current code:
int get_disp_num_from_widget(Widget w) {
Screen* xs = w->core.screen; //Xlib screen
if (xs) {
Display* xd = w->core.screen->display; //Xlib display
if (xd) {
XmDisplay d = XmGetXmDisplay(xd); // causes "Couldn't find per display information"
if (d) {
char* string = d->display.bindingsString;
if (string) {
printf("Display binding string: %s", string);
}
}
}
}
return -1;
}

You can get the Display * of any widget using the XtDisplay(Widget *) function.

Related

Brother Printer SDK cannot print label ERROR_WRONG_LABEL_ IOS

I am trying to print a label from my Nativescript+Vue application for IOS. I can successfully connect to printer, however when I try to print an image, it will give me an Error. The printer I am using is the "QL-810W". The label that I have loaded is the 62mm roll (no set length) and can support both red and black.
Here is the surrounding code:
exports.BrotherPrinterClass = BrotherPrinterClass;
var BrotherPrinter = (function () {
function BrotherPrinter() {
}
BrotherPrinter.prototype.print_image = function (PrinterName, ipAddress, image) {
try {
let printer = new BRPtouchPrinter()
printer.printerName = PrinterName
printer.interface = CONNECTION_TYPE_WLAN
printer.setIPAddress(ipAddress)
let settings = new BRPtouchPrintInfo()
settings.strPaperName = "62mmRB"
settings.nPrintMode = 0x03 //PRINT_FIT_TO_PAGE
settings.nAutoCutFlag = 0x00000001 //OPTION_AUTOCUT
settings.nOrientation = 0x00 //ORI_LANDSCAPE
settings.nHorizontalAlign = 0x01 //ALIGN_CENTER
settings.nVerticalAlign = 0x01 //ALIGN_MIDDLE
printer.setPrintInfo(settings)
if (printer.startCommunication()) {
//Print to the printer
let errorCode = printer.printImageCopy(image.ios.CGImage, 1);
if (errorCode != 0)
console.log("ERROR - ", errorCode)
}
printer.endCommunication()
}
else{
console.log("Failed to connect")
}
}
catch(e) {
console.log("Error - ", e);
}
};
This then returns the error "-41" which correlates to ERROR_WRONG_LABEL_. I've tried adjusting all properties, I've also tried settings.strPaperName = "62mm" and that didn't return anything different.
The image that is been passed in is of an ImageSource type, and I've also tried the following line, let errorCode = printer.printImageCopy(image.ios, 1);. This returns an error code of 0 which correlates to ERROR_NONE_ But nothing gets printed out. And as far as I know, this isn't a CGImage like the SDK wants.
I can print from an iPad using the "iPrint&Label" app. Just not from the SDK
EDIT:
I have tried to print from a file using printFilesCopy(). This returns no error when I go to print, but nothing gets printed. Additionally, I can set the settings incorrectly (eg, change '62mmRB' for '102mm') or I can outright not set the settings by commenting out the printer.setPrintInfo(settings) line. These changes do not impact the result.
Furthermore, I have purchased a 62mm black only roll, and tried that, only to find that I have the exact same issue.

How to tell if a cell value has passed validation

I am familiar with the Google Apps script DataValidation object. To get and set validation criteria. But how to tell programatically if a cell value is actually valid. So I can see the little red validation fail message in the spreadsheet but can the fact the cell is currently failing validation be picked up thru code?
I have tried to see if there is a cell property that tells you this but there is not. Also I looked for some sort of DataValidation "validate" method - i.e. test a value against validation rules, but nothing there either
Any ideas? Is this possible??
Specific answer to your question, there is no method within Google Apps Script that will return the validity of a Range such as .isValid(). As you state, you could reverse engineer a programatic one using Range.getDataValidations() and then parsing the results of that in order to validate again the values of a Range.getValues() call.
It's a good suggestion. I've added a feature request to the issue tracker -> Add a Star to vote it up.
I've created a workaround for this issue that works in a very ugly -technically said- and slightly undetermined way.
About the workaround:
It works based on the experience that the web browser implementation of catch() function allows to access thrown errors from the Google's JS code parts.
In case an invalid input into a cell is rejected by a validation rule then the system will display an error message that is catchable by the user written GAS. In order to make it work first the reject value has to be set on the specified cell then its vale has to be re-entered (modified) then -right after this- calling the getDataValidation() built in function allows the user to catch the necessary error.
Only single cells can be tested with this method as setCellValues() ignores any data validation restriction (as of today).
Disadvantages:
The validity won't be necessarily re-checked for this function:
it calls a cell validation function right after the value is inserted into the cell.
Therefore the result of this function might be faulty.
The code messes up the history as cells will be changed - in case they are
valid.
I've tested it successfully on both Firefox and Chromium.
function getCellValidity(cell) {
var origValidRule = cell.getDataValidation();
if (origValidRule == null || ! (cell.getNumRows() == cell.getNumColumns() == 1)) {
return null;
}
var cell_value = cell.getValue();
if (cell_value === '') return true; // empty cell is always valid
var is_valid = true;
var cell_formula = cell.getFormula();
// Storing and checking if cell validation is set to allow invalid input with a warning or reject it
var reject_invalid = ! origValidRule.getAllowInvalid();
// If invalid value is allowed (just warning), then changing validation to reject it
// IMPORTANT: this will not throw an error!
if (! reject_invalid) {
var rejectValidRule = origValidRule.copy().setAllowInvalid(false).build();
cell.setDataValidation(rejectValidRule);
}
// Re-entering value or formula into the cell itself
var cell_formula = cell.getFormula();
if (cell_formula !== '') {
cell.setFormula(cell_formula);
} else {
cell.setValue(cell_value);
}
try {
var tempValidRule = cell.getDataValidation();
} catch(e) {
// Exception: The data that you entered in cell XY violates the data validation rules set on this cell.
// where XY is the A1 style address of the cell
is_valid = false;
}
// Restoring original rule
if (rejectValidRule != null) {
cell.setDataValidation(origValidRule.copy().setAllowInvalid(true).build());
}
return is_valid;
}
I still recommend starring the above Google bug report opened by Jonathon.
I'm using this solution. Simple to learn and fast to use! You may need to adapt this code for your needs. Hope you enjoy
function test_corr(link,name) {
var ss = SpreadsheetApp.openByUrl(link).getSheetByName(name);
var values = ss.getRange(2,3,200,1).getValues();
var types = ss.getRange(2,3,200,1).getDataValidations()
var ans
for (var i = 0; i < types.length; i++) {
if (types[i][0] != null){
var type = types[i][0].getCriteriaType()
var dval_values = types[i][0].getCriteriaValues()
ans = false
if (type == "VALUE_IN_LIST") {
for (var j = 0; j < dval_values[0].length; j++) {
if (dval_values[0][j] == values[i][0]) { ans = true }
}
} else if (type == "NUMBER_BETWEEN") {
if (values[i][0] >= dval_values[0] && values[i][0] <= dval_values[1]) { ans = true }
} else if (type == "CHECKBOX") {
if (values[i][0] == "Да" || values[i][0] == "Нет") { ans = true }
}
if (!ans) { return false }
}
}
return true;
}

IUP, mouse events on matrix

I have basic confusion in understanding of IUP events system.
Now I am talking about matrix.
This is how it is created:
Ihandle *create_mat(void)
{
mat = IupMatrix(NULL);
IupSetAttribute(mat, "READONLY", "YES");
IupSetCallback(mat, "CLICK_CB", (Icallback)click);
IupSetCallback(mat, "BUTTON_CB", (Icallback)button);
return mat;
}
Here are callbacks:
int click(Ihandle *mat, int lin, int col)
{
char* value = IupMatGetAttribute(mat, "", lin, col);
if (!value) value = "NULL";
printf("click_cb(%d, %d)\n", lin, col);
return IUP_DEFAULT;
}
int button(Ihandle *mat, int button, int pressed, int x, int y, char* status)
{
printf("button %d, %d, %d, %d %s\n", button, pressed, x, y, status);
return IUP_DEFAULT;
}
Problem is in that I need both callbacks active but in showed situation CLICK event isn't fired.
If I disable BUTTON_CB then CLICK event is fired. But I need both, for click, left button doubleclick, right button release etc...
Is this normal behavior that BUTTON_CB excludes CLICK_CB or I do something wrong?
Actually, how would I get "lin" and "col" from inside BUTTON_CB or WHEEL_CB handler of matrix if CLICK_CB, ENTERITEM_CB and LEAVEITEM_CB which gives lin and col is not available (not fired in described situation)?
And more, how would I get "active control" (name, type of control with focus) from event handlers used on form's level?
Is this normal behavior that BUTTON_CB excludes CLICK_CB or I do something wrong?
Yes, it is. Because the BUTTON_CB is a IupCanvas callback and CLICK_CB is a IupMatrix callback. Remeber that IupMatrix inherits from IupCanvas. So internally IupMatrix is using the BUTTON_CB callback to implement several features.
So in this case what you have to do is to save the previous callback before assigning a new one, and call the old one from inside your own. Something like this:
old_callback = IupGetCallback(mat, "BUTTON_CB");
IupSetCallback(mat, "BUTTON_CB", new_callback);
int new_callback(...)
{
return old_callback(...)
}
Actually, how would I get "lin" and "col" from inside BUTTON_CB or WHEEL_CB handler of matrix if CLICK_CB, ENTERITEM_CB and LEAVEITEM_CB which gives lin and col is not available (not fired in described situation)?
Use the function pos = IupConvertXYToPos(mat, x, y) where pos=lin*numcol + col. To compute lin and col is quite simple considering they are integer values.
And more, how would I get "active control" (name, type of control with focus) from event handlers used on form's level?
I don't fully undestood your question. But I think that IupGetFocus and IupGetClassName could be the functions you want.
I will answer to my own question by using Antonio's advices so other people interesting for IUP can have benefit from those posts.
If I understand well, what is not likely, here is how I make BUTTON_CB handler for my matrix:
int button(Ihandle *mat, int button, int pressed, int x, int y, char* status)
{
//actually 'name' is Ihandle
//and class name is a 'type'
//in compare with other toolkits
char* name = IupGetClassName(mat);
//so since we have handle already
//we can't be here if we are not in concrete matrix
//and this comparision is not needed
if (strncmp(name, "matrix", sizeof(name)) == 0)
{
//if left mouse button is down
if (button == IUP_BUTTON1 && pressed == 1)
{
//my calculation is not 100% correct
//but good enough for this sample
int pos = IupConvertXYToPos(mat, x, y);
_line = pos/numcol;
_col = pos%numcol;
//if is doubleclick
if (status[5] == 'D')
{
//press ENTER key
//and open another modal dialog
//with argument 'sel'
k_any(mat, K_CR);
printf("Doubleclick\n");
//say HANDLED for IUP
//but not matter when READONLY is "YES"
return IUP_IGNORE;
}
//calculate (public) sel
//for select a clicked line
sel = _line + from - 1;
refreshl(from, sel);
printf("Click\n");
return IUP_IGNORE;
}
}
return IUP_DEFAULT;
}
This work's as expected.
Please further suggestion if something is not OK.

How to tell if anything is selected in CKEditor

I'm trying to determine with Javascript if anything is selected within the CKEditor. I wish there was a bool like editor.hasSelection(). I started out using editor.getSelection().getSelectedText() === "", but if an element with no "text" is selected (like an img) then that will be a blank string, giving me a false negative. I also looked into editor.getSelection().getSelectedElement(), but that gives null if more than one element is selected.
Is there anything that does this that I'm not seeing in the API?
Looks to me as though there is nothing in the CKEditor selection API to do this directly. However, I think the following will do it, although I agree that it's a shame (and surprising) there is no equivalent of the isCollapsed property of the native browser Selection object.
This is untested but looks as though it will work:
function hasSelection(editor) {
var sel = editor.getSelection();
var ranges = sel.getRanges();
for (var i = 0, len = ranges.length; i < len; ++i) {
if (!ranges[i].collapsed) {
return true;
}
}
return false;
}
// Example:
alert( hasSelection(editor) );

Determining object types in Qt

I have a series of QTextEdits and QLineEdits connected to a slot through a QSignalMapper(which emits a textChanged(QWidget*) signal). When the connected slot is called (pasted below), I need to be able to differentiate between the two so I know whether to call the text() or toPlainText() function. What's the easiest way to determine the subclass type of a QWidget?
void MainWindow::changed(QWidget *sender)
{
QTextEdit *temp = qobject_cast<QTextEdit *>(sender);
QString currentText = temp->toPlainText(); // or temp->text() if its
// a QLineEdit...
if(currentText.compare(""))
{
...
}
else
{
...
}
}
I was considering using try-catch but Qt doesn't seem to have very extensive support for Exceptions... Any ideas?
Actually, your solution is already almost there. In fact, qobject_cast will return NULL if it can't perform the cast. So try it on one of the classes, if it's NULL, try it on the other:
QString text;
QTextEdit *textEdit = qobject_cast<QTextEdit*>(sender);
QLineEdit *lineEdit = qobject_cast<QLineEdit*>(sender);
if (textEdit) {
text = textEdit->toPlainText();
} else if (lineEdit) {
text = lineEdit->text();
} else {
// Return an error
}
You can also use sender->metaObject()->className() so you won't make unnecesary casts. Specially if you have a lot of classes to test. The code will be like this:
QString text;
QString senderClass = sender->metaObject()->className();
if (senderClass == "QTextEdit") {
QTextEdit *textEdit = qobject_cast<QTextEdit*>(sender);
text = textEdit->toPlainText();
} else if (senderClass == "QLineEdit") {
QLineEdit *lineEdit = qobject_cast<QLineEdit*>(sender);
text = lineEdit->text();
} else {
// Return an error
}
I know is an old question but I leave this answer just in case it would be useful for somebody...

Resources