I wonder, is that possible to kind of "bake" one cell color using ncurses?
I mean, i want to set one cell color and then, when i use mvprintw with some other color in attron() i want the cell to still be this "baked" color.
//default color
mvprintw(0, 0, my_game_board);
bake(2,4,cell_color);
mvprintw(0, 0, my_game_board); //update game board, and then ONLY 2,4 cell should be cell_color.
.
Its like i want to set color for cell, not for content. (when content change, color shoud be same)
Just using mvpwintw, etc., as illustrated, you cannot. That ultimately resolves into waddch calls (see source), which pay attention to
the window background (see bkgd),
the window attributes (see attr_on) and
the character attributes (see last paragraph in addch).
You could use the panel library to manage the unchanging cell as a separate layer (in its own window), but probably would find it too complicated to use for multiple unchanging cells.
Related
Below code can change only attribute with leaving colors.
init_color(1, 255);
init_color(2, 1);
init_pair(1, 1, 2);
attron(COLOR_PAIR(1)); // only change the pair of foreground and background color
addstr("aaa");
attron(A_BOLD); // only change the attribute
addstr("aaa");
attrset(COLOR_PAIR(1)|A_BOLD); // change both
I want to know if we can change only foreground color, but leave background color.
attron_fg(BACKGROUND_YELLOW); // only change the foreground color
addstr("aaa");
No, you can't.
Ncurses is based on a model where each screen position has a colour pair. The possible colour pairs are in an indexed array, and it's the array index which ncurses stores in its screen representation. So you can only specify a colour pair.
Furthermore, since everything is based on indexed arrays, changing the definition of a colour or a colour pair will probably change the displayed colour of previously painted characters.
That model can be a bit annoying but its fundamental to the design of ncurses, so if you want to use ncurses, you need to adapt to the model.
Historically, there were hardware terminals based on the same model for essentially the same reason (limited memory). These days, such terminals are mostly confined to museums, but ncurses and the rest of the unix terminal handling infrastructure continues to cater to a world in which a diversity of external terminals each presented their own unique facilities and limitations.
These days, the same model is used to compensate for the differing implementations of terminal control sequences by a variety of different terminal emulators. But it also still works (or could work) with consoles connected to embedded devices.
That's an explanation, neither an excuse nor a justification.
So my problem is as follows - I need to create buttons in my GTK/C program that are simple clickable, colored tiles. I found this thread GTK Modifying Background Color of GtkButton and a few other resources that supply a very similar answer, but no matter what I do the set_fg function does nothing and set_bg function either sets a thin frame around the button/widget - which itself stays gray - or also does nothing. Can anyone please help me solve this problem or give me a reasonable alternative to creating a set of colored tiles that can be clicked on and that can dynamically change color?
I am writing an application in ncurses and I am dealing with colour displaying in pads. I add some text into pad p with colour attribute on like this:
if(has_colors())
start_color();
init_pair(0, COLOR_GREEN, COLOR_BLACK);
attron(COLOR_PAIR(0));
while( (ch=fgetc(f)) != EOF){
waddch(p,ch);
}
attroff(COLOR_PAIR(0));
fclose(f);
But then when I display part of the tab with prefresh(p,0, 0, 0,0, LINES-1,COLS-1); the text is printed without any change. (only the background is a bit different because I started colour mode). I am afraid that when the text is copied from the pad to screen, it does not copy the formatting, is that right? If so, is there any way how to accomplish that?
Thanks!
|||-----EDIT------|||
So even when I cahnge the key to something different than zero, it doesn't work. I am printing into the pad with this:
init_pair(3,COLOR_RED,COLOR_CYAN);
attron(COLOR_PAIR(3));
for (i=0;i<str.length();i++){
waddch(p,str[i]);
}
attroff(COLOR_PAIR(3));
and the text after prefresh is displayed as normal.
Color pair 0 is special, because (referring to the manual page) it is always the default foreground and background colors:
Color pair 0 is assumed to be white on black, but is
actually whatever the terminal implements before color
is initialized. It cannot be modified by the application.
There are three components of color pairs which combine to form a cell's color (see manual page):
background character
window attribute set via wattron, etc.
video attributes (including COLOR_PAIR value) passed in the parameter to waddch.
The last two affect the result if a nonzero color-pair is passed; otherwise they do not (and the preceding item on the list is used). These are all window operations. The functions attron/attron apply to stdscr, not to the pad or window p. If those were changed to wattron(p,COLOR_PAIR(3));, etc., the result would be improved.
this excerpt from the man pages for ncurses indicates the problem is using color pair 0.
The init_pair routine changes the definition of a color-pair. It takes three arguments: the number of the color-pair to be changed, the foreground color number, and the background color number. For portable applications:
The value of the first argument must be between 1 and COLOR_PAIRS-1, except that if default colors are used (see use_default_colors) the upper limit is adjusted to allow for extra pairs which use a default color in foreground and/or background. " emphasis mine
I am currently coding in C for linux and I need a GUI, so I took the GTK library to do so . I now have a window with a label (to begin with) but I don't know how to move it (in (x,y) coordinates, not in zPosition) to put it at the top of the window.
Here is my GTK code in main()
//Label
pLabel=gtk_label_new(NULL);
sUtf8 = g_locale_to_utf8("<span font_desc=\"Times New Roman italic 12\" foreground=\"#0000FF\">Neural Network - XOR Example</span>\n"
,-1, NULL, NULL, NULL);
gtk_label_set_markup(GTK_LABEL(pLabel), sUtf8);
g_free(sUtf8);
gtk_label_set_justify(GTK_LABEL(pLabel), GTK_JUSTIFY_CENTER);
//Window
gtk_window_set_position(GTK_WINDOW(pWindow), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(pWindow), "Neural Network");
gtk_window_set_default_size(GTK_WINDOW(pWindow), 900, 600);
gtk_container_add(GTK_CONTAINER(pWindow), pLabel);
g_signal_connect(G_OBJECT(pWindow), "destroy", G_CALLBACK(OnDestroy), NULL);
gtk_widget_show_all(pWindow);
gtk_main();
Has anybody an idea on how to move it ?
Thank you!
Here is what I got:
(source: hostingpics.net)
carl gave you part of the answer.
As to why you see what you have, there are two parts:
First, GtkLabel has an archaic alignment mechanism it inherits from the deprecated GtkMisc class. See GtkMisc's documentation for details: the xalign and yalign properties control the position of the text within the label's allocation, and it's set to centered by default. But remember that they are deprecated, so there's usually better alternatives.
Second, you can only have one control in a GtkWindow. This control gets allocated the entire space of the window.
Put these two facts together and you should see why you have what you have.
So what is correct instead?
You have to use a container, such as GtkBox or GtkGrid, to get more than one widget into a GtkWindow. Each widget carries with it four special properties: hexpand, halign, vexpand, and valign which control the position and size of each member of the container. These are properties of the widgets themselves.
hexpand and vexpand determine whether or not the container allocates whatever space is left over to this control in the X and Y directions, respectively. If multiple widgets have expand set, the space is divided evenly.
halign and valign determine the position of a widget in its allocation. GTK_ALIGN_START, GTK_ALIGN_CENTER, and GTK_ALIGN_END put the widget at the start, middle, and end of the given alignment side, respectively. GTK_ALIGN_FILL fills the widget to fit the allocation.
This page has more information.
If you're trying to do exact positioning of controls, you should first investigate if GtkGrid and GtkBox (applied recursively) provide what you want. An important thing about the default internal centering of GtkLabels is that a GtkLabel in a GtkGrid will already be vertically aligned to its neighbor's text; just set halign to start or end to get the label left-aligned or right-aligned (and set valign to start if its neighbor is something big like a table). There are also functions that provide margins and padding to make the UI look nice.
If you absolutely must position things yourself, however, keep everything in mind.
In processing a group of items, I wanted to display a unified image of the status of the group, so I essentially made a Grid of a number of progressbars with transparent backgrounds and various colored foregrounds all at the same cell.
I'm running into some transparency artifacts (purple bar is actually purple under the green, and sometimes it draws over the top, etc) and it just seems a bit wasteful. So, I decided to make my own, but now I've got a bit of paralysis on how to do it. Do I use the DrawingContext in FrameworkElement's OnRender, or is there something simpler? Is there a set of general rules when it comes to making your own control?
I pondered switching to a pie chart since those are easy to come by, but its high time I did something not off-the-shelf.
Thanks!
I'm not quite sure how you intend the progressbar to combine different progresses, but if say the furthest along progress is at the bottom of the z-index and the least along progress is at the top, then I'd do something on the lines of this:
1) I would probably create a user control for this new progresbar.
2) It would have a property called NumberOfProgresses, that is tied with an array containing status of said progresses.
3) Each progress would be represented by a Border item (or perhaps something more suitable up the visual tree), because it's a simple wpf control with a background property. The background property would be set to nice a looking progress style and the progress color can be bound in the style to say the border's borderbrush property. Making it easy to set the color of the progress.
4) The user control would have a method UpdateProgress which takes the percentage value and the index of the progress in the array as parameters.
5) As progresses are updated you can either, just calculate the appropriate width (user control actual width * percentage) for the border and play around with the Z index to get it displayed at the top/bottom, or stack the borders horizontaly, set the least along progress as first, then for the rest of the progresses you'd have to substract previous progresses lengths to get the same effect.
This way there would be no transparency induced artifacts and no OnRender()...
Mind you, in WPF there should be no reason to mess with OnRender this and OnRender that, like it was required in WinForms with OnPaint.
Just set up the elements via code to get the look you want, and let WPF do it's rendering ;)
I can imagine one problem with this user control though. You'd have to provide feedback to the user as to which color belongs to which progress. But that would probably take you back to square one, meaning it's better/simpler to just display multiple progressbars.