I am using NCurses to try and get the desired output:
However with the following code:
int main(void)
{
WINDOW *white_space,*red_space,*blue_box;
int maxx,maxy;
initscr();
start_color();
init_pair(1,COLOR_WHITE,COLOR_BLUE);
init_pair(2,COLOR_RED,COLOR_WHITE);
init_pair(3,COLOR_BLACK,COLOR_GREEN);
init_pair(4,COLOR_RED,COLOR_RED);
white_space = subwin(stdscr,10,76,6,2);
red_space = subwin(stdscr,1,80,23,0);
getmaxyx(white_space,maxy,maxx);
blue_box = subwin(white_space,maxy-1,maxx-1,8,20);
if(white_space == NULL)
{
addstr("Unable to create subwindow\n");
endwin();
return 1;
}
bkgd(COLOR_PAIR(1));
addstr("Master window");
wbkgd(white_space,COLOR_PAIR(2));
mvwprintw(white_space, 0, 0, "%46s", "White space");
wbkgd(blue_box,COLOR_PAIR(4));
wbkgd(red_space,COLOR_PAIR(3));
waddstr(red_space,"Alert area");
wrefresh(white_space);
wrefresh(stdscr);
refresh();
getch();
endwin();
return 0;
}
I get the following output:
Is it possible to create a subwindow within a subwindow?
Thanks
ncurses (any non-buggy version of curses) supports subwindows, as noted in the manual.
In the given example, there are a few problems noticed:
white_space = subwin(stdscr,10,76,6,2);
red_space = subwin(stdscr,1,80,23,0);
getmaxyx(white_space,maxy,maxx);
blue_box = subwin(white_space,maxy-1,maxx-1,8,20);
For instance:
the initial size given to white_space does not take into account the actual screen-size (which could be narrower than 80 columns)
the size asked for blue_box is only slightly smaller than white_space, but starts far enough to the right that the window could not (in 80 columns) be shown. If so, no window is created.
There are several programs in ncurses-examples using subwin and the related derwin, which may be useful for comparison.
Related
this is my first post here.
I am trying to solve a problem I am having with my SFML project where I am using multiple clients, that communicate through texts that can be typed in the rendered window and then sent to the other sockets using a selector.
My problem is that everytime i press one button of the keyboard, the window detects like 3 or 4, and if I try it on another machine, the behaviour changes.
I tried almost every solution, including the setKeyRepeatEnabled(false);
This is the update function
void Client::Update(Input* input,sf::Event& Ev, sf::Font& font, sf::RenderWindow& window)
{
if (input->isKeyDown(sf::Keyboard::Return))
{
sf::Packet packet;
packet << id + ": " + text;
socket.send(packet);
sf::Text displayText(text, font, 20);
displayText.setFillColor(sf::Color::Red);
chat.push_back(displayText);
text = "";
input->setKeyUp(sf::Keyboard::Return);
}
else if (input->isKeyDown(sf::Keyboard::Backspace))
{
if (text.size() > 0)
text.pop_back();
}
else if (input->isKeyDown(sf::Keyboard::Space))
{
text += ' ';
}
else if (Ev.type == sf::Event::TextEntered)
{
text += Ev.text.unicode;
return;
}
//sf::Event::TextEntered
//text += Ev.text.unicode;
}
This is the render one.
void Client::Render(sf::Font& font, sf::RenderWindow& window)
{
sf::Packet packet;
socket.receive(packet);
std::string temptext;
if (packet >> temptext)
{
sf::Text displayText(temptext, font, 20);
displayText.setFillColor(sf::Color::Blue);
chat.push_back(displayText);
}
int i = 0;
for (i; i < chat.size(); i++)
{
chat[i].setPosition(0, i * 20);
window.draw(chat[i]);
}
sf::Text drawText(text, font, 20);
drawText.setFillColor(sf::Color::Red);
drawText.setPosition(0, i * 20);
window.draw(drawText);
}
I don't recognize the isKeyDown function as part of SFML, so I assume you either is something you implemented or is part of a previous version of SFML (being the current 2.5.1).
There are three ways to detect input from keyboard in SFML.
sf::Keyboard::isKeyPressed: looks like this is the one you are using. It will be true every cycle that the key is pressed. You definetly don't want this. The window.setKeyRepeatEnabled(false) will obviously not work because you don't get the input through the window, but directly from the keyboard.
sf::Event::KeyPressed and sf::Event::KeyReleased events: for this, window.setKeyRepeatEnabled(false) will work, but still it's not the recommended way to deal with text input, as it would require a lot of juggling by your side to handle key combinations (accents, uppa.
sf::Event::TextEntered event: now, this is the best and recommended way to handle typing. Check the tutorial to see how to use it.
I add my program two RichEdit which the one displays binary contents and the another shows the index of the byte. I hope these two edit always anchor on the same position while/after scrolling.
A possible way is to handle the main RichEdit's WM_VSCROLL and WM_MOUSEWHEEL message and pass the message and parameters to the second RichEdit. I've tried this but I found that the two edits aligned not very well. And the other disadvantage is the both RichEdits' scroll bar needs to be enabled, if I only enable one, the another can't receive WM_MOUSEWHEEL message, but I hope one scroll bar displayed only.
The second way I've tried is using a timer and synchronize with LINESCROLL regularly by the time (< 10ms is better). This works well most of the time, but sometimes still have the unaligned issue.
Is there a better solution to handle this kind of demand?
//---------------------------------------------------------------------------
void __fastcall TBinaryEdit::Timer1Timer(TObject *Sender)
{
int srcLine = 0;
int trgLine = 0;
if (Sender == Timer1) {
srcLine = GetRichEditLineNo(MainEdit);
trgLine = GetRichEditLineNo(IndexEdit);
if (srcLine != trgLine) {
SendMessage(IndexEdit->Handle, EM_LINESCROLL, 0, srcLine - trgLine);
}
}
}
//---------------------------------------------------------------------------
int TBinaryEdit::GetRichEditLineNo(TRichEdit* RE)
{
int line;
int wordpos;
TRect rt;
POINTL pt;
SendMessage(RE->Handle, EM_GETRECT, 0, LPARAM(&rt));
pt.x = RE->Left + rt.left;
pt.y = RE->Top + rt.top;
wordpos = SendMessage(RE->Handle, EM_CHARFROMPOS, 0, LPARAM(&pt));
line = SendMessage(RE->Handle, EM_LINEFROMCHAR, wordpos, 0);
return line;
}
Thanks for your help. I've found a simple solution for my short-term need.
As I mentioned before, the second RichEdit's scroll bar should be enabled for receiving WM_MOUSEWHEEL message, but I don't like both edit have scroll bar displayed. For this reason, I use EM_LINESCROLL instead to replace the mouse wheel message. I write a sub class derive the TRichEdit and overwrite its WM_MOUSEWHEEL handler, then pass EM_LINESCROLL message with the mouse wheel's delta value to both RichEdits.
int zDelta = GET_WHEEL_DELTA_WPARAM(Message.WParam);
int scroll = zDelta == -120 ? 1 : -1;
SendMessage(MainEdit->Handle, EM_LINESCROLL, 0, scroll);
SendMessage(IndexEdit->Handle, EM_LINESCROLL, 0, scroll);
This makes the text vertical offset in RichEdits be aligned. But I think there must be better solution, I will keep research for improvement.
What would be the proper way to get the active window (the one with input focus) with XCB?
reply = xcb_get_input_focus_reply(connection, xcb_get_input_focus(connection), nullptr);
std::cout << "WId: " << reply->focus;
This seems to be work sometimes and sometimes not.
I also saw someone mentioned querying _NET_ACTIVE_WINDOW root window property but I can't figure out how that is done and is it always supported with XCB?
Edit: The approach above with xcb_get_input_focus is only one part, after getting the reply->focus, you need to follow up the parent windows via xcb_query_tree.
As far as I know, EWMH-compliant window managers are expected to set _NET_ACTIVE_WINDOW attribute of root window to the window ID of the currently active window.
In order to get it,
Use xcb_intern_atom to get the atom value for _NET_ACTIVE_WINDOW
Get the root window ID, e.g. using xcb_setup_roots_iterator(xcb_get_setup(connection)).data->root
Use xcb_get_property, xcb_get_property_reply, and xcb_get_property_value to get the value of the attribute of the root window.
_NET_ACTIVE_WINDOW has type of CARDINAL, which, for XCB purposes, has size of 32 bits.
Or you could use libxcb-ewmh which wraps this task into xcb_ewmh_get_active_window function.
This solution works for me, it's more or less migration from some X11 code to XCB. Basically get the focus window and follow up the the path of the parent window until the window id is equal to parent or root id, this is then the top level window.
WId ImageGrabber::getActiveWindow()
{
xcb_connection_t* connection = QX11Info::connection();
xcb_get_input_focus_reply_t* focusReply;
xcb_query_tree_cookie_t treeCookie;
xcb_query_tree_reply_t* treeReply;
focusReply = xcb_get_input_focus_reply(connection, xcb_get_input_focus(connection), nullptr);
xcb_window_t window = focusReply->focus;
while (1) {
treeCookie = xcb_query_tree(connection, window);
treeReply = xcb_query_tree_reply(connection, treeCookie, nullptr);
if (!treeReply) {
window = 0;
break;
}
if (window == treeReply->root || treeReply->parent == treeReply->root) {
break;
} else {
window = treeReply->parent;
}
free(treeReply);
}
free(treeReply);
return window;
}
I tried to google my question in several website but it still no answer.
My problem is look like this http://opencv-users.1802565.n2.nabble.com/Runtime-error-for-createTrackbar-in-control-panel-td7550203.html
I tried to create the control panel in OpenCV window using Qt integration as show in an example of OpenCV Document: http://docs.opencv.org/modules/highgui/doc/qt_new_functions.html
By this function, it should be separate between image window (with 'imshow()') and control panel (with in other window, called control panel).
However, it is not work when run to the code 'createTrackbar(num1, NULL, &val1 , 255, NULL);' the error message 'Null pointer' is shown. However, if I change the parameter to the window name it is work!.
My code is like this:
#include <...opencv.hpp>
#include <...highgui.hpp>
char* num1 = "testTrack";
int val1 = 100;
const string mainwin = "show";
int main()
{
while (true)
{
frame = capture();
createTrackbar(num1, NULL, &val1 , 255, NULL);
process_frame = image_processing(frame);
imshow(mainwin, process_frame);
// [Exit the system]
if (condition)
break;
}
}
Do you have any idea?
I don't know if this answer is useful after all this time, but you need to use an empty string instead of a null pointer.
Try with:
createTrackbar(num1, "", &val1 , 255, NULL);
I have a CListCtrl and I need to change the color of A SPECIFIC character/set of characters (which I choose by comparison) from the text of every cell in the list.
I know how to change the color of the entire text of the cell when I find the character/set of characters (by using 'strstr' command), but I can't find an example which shows how to change ONLY the character/set of characters.
Here is a sample of my code:
void Agenda::OnCustomdrawMyList( NMHDR* pNMHDR, LRESULT* pResult )
{
NMLVCUSTOMDRAW* pLVCD = (NMLVCUSTOMDRAW*)pNMHDR;
*pResult = CDRF_DODEFAULT;
if (CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage)
{
*pResult = CDRF_NOTIFYITEMDRAW;
return;
}else if (CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage)
{
*pResult = CDRF_NOTIFYSUBITEMDRAW;
return;
}else if ( (CDDS_SUBITEM | CDDS_ITEMPREPAINT) == pLVCD->nmcd.dwDrawStage )
{
// So right now I am in the stage where a SUBITEM is PREPAINTED
int nItem = pLVCD->nmcd.dwItemSpec;
int nSubItem = pLVCD->iSubItem;
char a[100];
listControl.GetItemText(nItem,nSubItem,a,100);
COLORREF textColorFound, textColorDefault;
textColorDefault = RGB(0,0,0);
pLVCD->clrText = textColorDefault;
char* startingFrom;
if( (startingFrom = strstr(a,filterText)) != NULL ) {
// Could I set a pointer here or something like that so
// the coloring could start only from 'startingFrom'
// and stop at 'strlen(filterText)' characters?
textColorFound = RGB(205,92,92);
pLVCD->clrText = textColorFound;
}
*pResult = CDRF_DODEFAULT;
}
}
listControl is the variable for my CListCtrl
the other things are pretty self-explanatory
No, you cannot do this. What you will have to do is custom-draw the text in question. This will be tricky because you will have to do it with two different calls, between which you will have to manually adjust the color and the drawing location to account for the intercharacter spacing etc. And you better hope that you don't need to do multi-line output.
Take a look at the article Neat Stuff to Do in List Controls Using Custom Draw by Michael Dunn on CodeProject to get some ideas on how to proceed.
Alternatively, if you can use the Toolkit Pro toolkit from CodeJock you can leverage their "XAML" support (I use quotes because it's not really XAML, but their own implementation of a subset of XAML) and let them do all the hard work.
Digging on the same issue; But I wouldn't go so far as modifying/adding to the default Windows behaviour for painting strings... apparently that would be the endpoint of having it owner-drawn.(aici am murit si eu :).