C++ MFC - CTreeCtrl - Difference between SelectItem and SetItemState - c++

I need to create a Tree List for my windows application, with multiple selection of nodes with drag-and-drop support. Since Microsoft doesn't provide any such property for the tree which eases the work of the developer, I have to implement this features on my own.
Can somebody explain me the differences (also, the corresponding getters for the following statements) and when to use
SetItemState(hItem, 1, TVIS_SELECTED);
and
SelectItem(hItem);
and
SetItemState(hItem, TVIS_SELECTED, TVIS_SELECTED);

The primary difference is that SelectItem is functionally built off of SetItemState. Along with anything else it does, SelectItem will use SetItemState (or equivalent internal calls) to set TVIS_SELECTED for the new item, and, say, remove it from the previously selected item.
In order to implement your own multiple-selection control, you have to prevent SelectItem from being called when you want to augment the selection instead of replacing it. There are a lot of cases to consider, ranging from mouse clicks with various modifier keys held to keyboard navigation, so good luck in your implementation!

Related

Menu designed for use without mouse. What is the best way to implement?

I'm writing a GUI using QT for embedded system with linux. This device has no mouse. Only display and specific keyboard with keys: up, down, return and 7 number keys.
The software will be used to set some parameters of device and read them (some charts also).
Example of how menu could look:
after OPTION 1 selected
After SUBOPTION 1 selected some table with data is loaded.
Sometimes after selecting option i need to load specific widget and sometimes just another set of options.
I think it is possible to implement custom labels and kind of list widget that aligns them.
I guess it is also possible to use Qt's MVC classes for it. They are highly customizable, but i never made custom views and delegates.
Maybe i just need to create QtListView with stringlist model and apply stylesheet to it so it gets look more like buttons. And based on selection in list load next widget.
Which way is better and why?
Is there any easier ways to accomplish this?
Any ideas would be appreciated.

Issues with keyboard navigation on list with custom renderer

We have a list that uses a custom renderer containing a label, a checkbox and two icons (which have click events). This list needs to be made WCAG 2.0 compliant and in order to do that we need the list to be keyboard navigable.
The problem is with being able to move from one list item to the next and have the focus move to the label for the next/previous list item. Specifically, when the user enters the list using TAB button, the label for the first list item receives focus (highlighted box around text) and the entire row in the list is highlighted as the selected item.
However, when the user then presses the down arrow key to move to the next list item, the next row becomes highlighted (is now the selected item) but the focus remains on the label of the previous row (highlight still shown around label for row 1). The only way to get the focus to move to the newly selected row is to tab through the checkbox and two icons. This isn't a big deal if there are only a couple list items but would be a pain if there are 20+ rows in the list.
Is there a way to get the focus to move to the label of the newly selected row as soon as the user moves (using up/down cursor keys) to the new list item? I know a picture would help but I don't have anyway of posting a screenshot online. Any help would be greatly appreciated.
You're going to have to dig into how focus works in Flex. This is not a complete answer, but hopefully you can put together a solution that works for you. I did this about 4-5 years ago in Flex 3, but it should be similar in Flex 4.
How Focus Works in Flex
The main things to know are the FocusManager singleton class and the IFocusManagerComponent interface.
The FocusManager moves the focus around the UI based on user interactions (mouse clicks, keyboard navigation, etc.).
If a component implements the IFocusManagerComponent interface, then the FocusManager will include it in the "tab" loop and allow the component to be focused via keyboard navigation.
How Focus Works With Flex List Components
You've already stumbled onto the peculiarities of how focus works with the List component and item renderers. The Flex List components implement IFocusMangerComponent and so when you tab through the UI the FocusManager sends the focus to the list.
The List may or may not focus the item renderers. In Flex 3 you had to be using editable item renderers for this to happen, it may or may not be the same in Flex 4.
Some Ideas for Solutions to Your Problem
I think there are numerous ways to solve this. Use some combination of these techniques:
override the protected keyDownHandler() method of the List component. I don't have the code handy, but if you look at it's implementation in the List class you should be able to make your overridden version set the focus on the next renderer.
use methods of the FocusManager to find components in the tab loop: getNextFocusManagerComponent(), findFocusManagerComponent(). Check the docs there are others that will be useful. For example, when the user presses the down arrow, you can let the next item renderer get selected, then use findFocusManagerComponent() (passing in the newly selected renderer) and then tell the FocusManager to focus it with the setFocus() method. This is probably not exactly the right approach ;)
By the way, the FocusManger is a Flex singleton object, every UIComponent in Flex has a focusManager property you can use to get a reference to it.
consider disabling focus on objects that don't need to receive focus (like the Label in your item renderer). There are numerous properties to do this: focusEnabled, hasFocusableChildren, mouseFocusEnabled, tabEnabled, tabChildren etc.
consider disabling focus on the List component, but then making your item renderers implement the IFocusManagerComponent interface. Implementing the interface is simple, you just declare it in your class (there's no actual methods to implement). The tricky part will be now your item renderers need to have key down handlers (just override the protected keyDownHandler() method that all UIComponent objects have).
I think there are other techniques you can use, it's just been too long since I did this. I'd be happy to provide more help if you get stuck somehwere...

cpp graphical menu

I am making a new graphical menu interface for a project I am making. I don't want to use the menu system provided by windows APIs and want to make one from scratch.
My question is, what is the best method for setting up the structure?
I'm thinking I will need a menu item object, each of which will have to have their own item array list, etc...
Is it considered sloppy to have recursive coding like that? (Ie an object which contains objects of itself, which contains objects of itself, etc...)
I'm thinking I can give the item object a draw interface which checks itself to see if it has an item array that is not null. If it does, it executes the draw command all the way down, thereby giving me a menu with (for my purposes) unlimited submenu level
In my opinion your approach is fine. In nearly all UI frameworks, views contain views as subviews after all.
But the thing is that writing drawing code is too much work for small projects I think. I would consider using a UI framework such as QT and use its view mechanism as a starting point. You can write your own Menu class which will be a subclass of generic View class in the framework.

How to set Tree View item as visible/invisible using Win32 api

I am working on a project implemented using WIN32 APIs, where I need some of the tree-view items at run time to be visible/invisible, based on some data entered by user. I have done some work, where I found that I can add/delete item in a tree-view control but can't find anywhere how to set item visible or invisible(I found some examples where it can be done through MFC).
I am looking for the way to set them as visible/invisible is simply because when I add an item, it requires significant back-end calculations, that repeated addition or removal will result in performance issues. I only want to do that calculation only once per tree view item.
One of the solution, I have thought, if setting tree-view item is not possible, is to simply have a linked list of tree-view items present, and add/delete only those items that are required to be visible/invisible.
Please, tell me if it is possible to set tree-view item's state as visible/invisible, If yes, then how? And if no, what can be other alternate solutions?
The standard TreeView control does not have any concept of node visibility. Adding/deleting nodes is the only option. You will have to maintain a separate linked list cache of data that the nodes display (which you should do anyway so you separate your UI logic from your business logic). Otherwise, you need to write your own TreeView control, or find a third-party implementation, that suites your needs.

Implementing Undo feature (like Ctrl+Z) in Qt/C++

I am using Qt 4.5 and C++ on Windows XP.
Basically I will be having an UI where the user will enter some data. He can go and modify the values available in the UI. The UI will have basic Qt UI elements like QLineEdit,QTableWidget etc.,
So now, if the user presses Undo button (or Ctrl+Z) the previous value should be retained in the corresponding UI element.
Say, if there is QLineEdit with the text 25. Now the user modifies to 30. Now by clicking Undo, the older value 25 should be retained.
Like the Undo feature that usually available in many applications. Is there any way to do it?
You could use Qt's undo framework.
The typical way of implementing Undo is to represent each action done by the user, and store them. You also want the ability to compute the inverse of a given action.
So, for an insert into a text buffer, the action would store the text inserted, and the location at which the insert happened. The inverse then becomes a delete, at the same location and with the size of the inserted text.
When the user asks the application to undo, simply look at the most recent stored action, and execute its inverse. If you now instead of deleting the "spent" action remember it, too, you can implement Redo by moving the other way in the history of actions.
Note that this is an abstract and generic explanation; as other answers point out, Qt has a framwork in place for implementing Undo already, so you should of course investigate that, first.
You could use the Command Pattern to realize undo/redo
QLineEdit has a built-in undo/redo support, exported as public slots, check : http://doc.trolltech.com/4.7/qlineedit.html#undo