How to use colors in Motif - c++

I'm new to GUI programming in C and Linux, and I'm having a hard time with it. It seems like a fairly simple/straightforward thing, but I can't find any answers googling. I want to add a background color to a widget. XmNbackground seems to be what I want to use, but I don't understand what I set it to, like a simple color blue, how do I get "blue" to set XmNbackground color to that?

See here for an answer in the function Pixel convert_color_name_to_pixel, and also here.
Hope this helps.

You can use a resource file which includes "*background: blue" or use XSetArgVal to do the same thing in the code.

If you have the widget in a function, you can set the widget background color resource like this, assuming the widget name is w:
XtVaSetValues(w, XmNbackground, XmRstring, "#c4c4c4", 8, NULL);
Where #c4c4c3 is the hex color code for red (first two characters), green (middle two characters) and blue (last two characters).
You can also use any of the textual color names from file rgb.txt - where that file is located seems to be something of a moving target, but on the computer I'm using at the moment it's in /usr/share/X11

Related

how to create an outline of a text with fonts with separate polygons for each letter?

I want the outline of text with the standard Windows font "Bahnschrift". I tried to convert it with "Path->Object to Path" and then setting fill to X and stroke to black, but looks like the font is constructed with separate polygons for each letter. See this example:
Top is Bahnschrift, and bottom is Arial which works fine.
Is it possible to calculate somehow automatically the outline of the top text, without intersections, and without doing it all manually for all the letters? And including the right outline for letters with holes, like the "e"?
Some letters are even kind of twisted internally, looks pretty bad:
So I guess an algorithm to detect if it is a real hole, or if it is a hole from such twists and to ignore it, could be difficult.
Select the complete character in question. Then select from the drop down menu:
Path intersection followed by path union. This fixes the character font outline problem where the outline crosses into the body of the character.

Unusual color of text in console

today i need to print colorful text in console, but the color should be "unusual". I've found the only way to colorize text with windowh.h
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colorNumber)
Main problem is the limited choice of colorNumber, there are only 16 colors, but it is not enough. What if i want to make text color #33F5C8? What if i want to make it transparent? How can i do it?
(nonstandard cpp tools allowed)
With ANSI you can use the following code to set a RGB based color:
\033[38;2;⟨r⟩;⟨g⟩;⟨b⟩m
To reset color after change:
\033[0m
Pay attention that the visual's console might not support it, although the CMD probably does. To check the colors, compile your program and run it using the CMD.

Is it possible to use only a portion of QImage for styling?

Let's take the horizontal sprite of images of my previous question:
So, I have 4 images in 1. Let's say that I want to use only the green circle for styling a QComboBox arrow. I know I can style it with a single image with
QComboBox::down-arrow {
image: url(:/downarrow.png);
}
but is it possible (in the stylesheet or by another means) to get only a piece of the image used?
Unfortunately that is not possibile with Qt Style Sheets, since they do not support absolute values for background-position, as stated in the documentation
The answer to the linked question is still the best way to implement this with Qt.

set file colors on c++

I am drawing a chart in a text file and I want to set the rows color grey and white one by one but I couldn't find the function that can help me.
my compiler is code blocks on windows.
can anyone please tell me the number of these colors and related function?
thanks
It is impossible in c++ to change color of texts in a file
A text file is just text. It has just nothing but the letters and punctuation. It has no notation of color or whatever. So you can't do it in C++ or anything else.
For formatting and colors there are special files - HTML, RTF and others. If you want to write such thing you must ask the question that way.

QPainter colored text (syntax coloring)

I have a custom Qt widget which I used to display disassembly and I am looking to add syntax coloring to it.
Currently, I simply set the QPen to a solid color, construct the text I want to display, and render it to the QPainter at the appropriate coordinates.
The question is, what is the best approach to adding syntax coloring? I've thought of a few:
I could simply divide the coloring into logical blocks, each preceded by setting the QPen to the desired color.
I could have special escape characters which represent a change in the color palette, and render 1 character at a time.
I could do a modification of #1 and create a list of std::pair<QColor, QString>, then I could simply iterate the list setting the color and drawing the text as I pop items off the front of the list.
Something entirely different?
I know that each of the 3 approaches I've listed will technically work, but I'm looking for a very efficient solution. This code will be called a lot. And since this is an interactive debugger, if this code is slow, someone rapidly stepping or tracing will see a visible slowdown.
EDIT: I'm aware of QSyntaxHighlighter and QTextDocument. The main issue is that these don't generally suite my purposes very well. I have several columns which all have dividers and can be slid back and forth. To give you an idea, Here's a link to a screenshot of my debugger. As you can see it isn't really like a text document at all. In fact it is closer to a list or table. But there is already a bunch of custom drawing going on making a normal QTextDocument somewhat impractical.
EDIT: I was incorrect, It seems that QTextDocument can render directly to a QPainter. Looks like what I need!
EDIT: It is unclear how to control where and how QTextDocument or QTextLayout will draw on a QPainter. I've attempted to use them to no avail. So if someone could provide a rudimentary example, that would be very helpful.
EDIT: I was eventually able to get what I wanted using something like this:
painter.setPen(default_color);
QTextDocument doc;
doc.setDefaultFont(font());
doc.setDocumentMargin(0);
doc.setPlainText(text);
highlighter_->setDocument(&doc);
painter.save();
painter.translate(x, y);
QAbstractTextDocumentLayout::PaintContext context;
context.palette.setColor(QPalette::Text, painter.pen().color());
doc.draw(&painter, context);
painter.restore();
Qt provides a QSyntaxHighlighter that is probably exactly what you want. QSyntaxHighlighter uses a QTextDocument to mark each block of code with a specific state which can be associated with a specific presentation format.
The documentation on QSyntaxHighlighter provides a sample demonstrating how this may be accomplished and does some nice things:
Separates the model from presentation
Separates the formatting into different reusable classes (if implemented as such)
Supports the State design pattern if useful to your language
I'd use either QTextEdit or directlay its underlining engine QTextDocument.