Creating a professional-looking (and behaving!) form designer - c++

When I began programming (some 10+ years ago), three things amazed me:
Compilers/interpreters (back then I knew them as "programs that make my programs work", often followed by the qualifier "whatever they are")
Code editors
Form designers
Back then, I accepted all of them as facts of life. I was able to make my own special-purpose programs, but "programs that made my programs work", code editors and form editors were made by the Gods and there was no way I could mess with them.
Then I went to university, and took a course on formal language processing. After learning formal grammars, parsers, abstract syntax trees, etc.; all the magic about compilers, interpreters and code editors was soon gone. Compilers and interpreters could be written in sane and simple ways, and the only non-sane thing a syntax highlighting code editor could require were Windows API hacks.
However, to this day, form editors remain a mystery to me. Either I lack the technical knowledge required to make a form designer, or I have such knowledge, but cannot find a way to use it to implement a form designer.
Using Visual C++ and the MFC, I would like to implement a form designer inspired by the best form designer ever:
In particular, I would like to imitate its two features that I like the most:
The form being designed is inside a container. Thus, an arbitrarily large form may be designed without wasting too much screen real estate, by simply resizing the container to an appropriate size.
The "Align to Grid" option makes designing professional-looking user interfaces a lot less
frustrating. In fact, I would go as far as saying creating professional-looking user interfaces using Visual Basic's form designer is actually easy, fun and enjoyable. Even for left-brained programmers like me.
So, I have the following questions:
How do I make a form designer, in which the form being designed is inside a container? Is the form being designed an actual window contained inside another window? Or is it just a mockup "manually" painted by the form designer?
Do the Windows API and/or the MFC contain functions, classes, whatever that make it easy to create "selectable" items (surrounded by little white or blue boxes when they are selected, resizable when they are "grabbed" by one of these "edges")?
How do I implement the "Align to Grid" functionality?

Both the answers here are good, but left out what I consider to be the really interesting bits (including a couple that you didn't ask directly, but you might find of interest anyhow), so here's my 2c:
Drawing the controls
Ideally, you just go ahead and create a regular instance of the control. You want something that looks like a button? Create a real button. The tricky thing is stopping it from behaving like a button: you want clicks to activate it for moving, not actually 'click' it.
One way of dealing with this - assuming that the controls in question are 'HWND-based' (eg. the standard windows set of button, edit, static, listbox, treeview, etc.) - is to create the control, and then subclass it - ie. override the wndproc with SetWindowLongPtr(GWLP_WNDPROC, ...), so that the designer code can intercept mouse and keyboard input and use it to initiate a move, for example, instead of having the mouse input go through to the actual button code, which would instead interpret it as a 'click' event.
An alternative approach to subclassing is to place an invisible window above the button to capture the input. Same idea of intercepting input, just different implementation.
The above applies to both managed (VB.Net, C#) and unmanaged (C/C++) controls; they're both essentially stock windows HWNDs; the managed versions just have a managed wrapper code handing off to the underlying unmanaged control.
The old (pre-managed code) ActiveX controls, as used in pre-.Net VB, were a whole different ball game. There's a fairly complex relationship between an ActiveX container and the ActiveX controls within it, with many COM interfaces handling things like negotiation of properties, events, painting, and so on. (There's event a set of interfaces that allows an ActiveX control to receive input and draw itself without having its own HWND.) One benefit you get from this complexity, however, is that ActiveX controls have an explicit 'design mode'; so a control knows to respond appropriately in that case and can cooperate with the whole procedure.
The Form Itself...
So basically the controls are just regular controls. So you'd expect the form itself to be a regular form? - Almost. As far as I know, its just another HWND-based window, that's a child of the designer (so it gets clipped, and can be scrolled within it); but I think the designer is doing a bit of 'cheating' here, because usually Windows only draws frames like - with titlebar and min/max buttons that for actual top-level windows. I don't know offhand the exact technique that they're using here, but some options could include: painting it manually to mimic the Windows look; using the Windows "theme" APIs, which allow you to access the graphic elements used for the bits and pieces of titlebars and paint them wherever you want to; or, perhaps less likely, setting the window up as a "MDI Child window" - this is one exception case where windows will draw a frame around a nested window.
Draggable Handles
Simplest approach here is for the designer to create eight small square title-bar-less popup windows that sit above all the other elements - which initiate the appropriate resize code when they are clicked. As the user clicks from control to control, just move the drag handle windows to the currently active control. (Note that in all the above, Windows itself is figuring out who's been clicked, you never have to actually compare mouse coords against element rectangle coordinates and work it out yourself.)
Saving & Recreating
For plain windows system controls that are used by unmanaged C/C++, it's relatively easy: there's a well-known text-based file format - .rc - that describes the controls and locations. Have the designer spit out that (and likely a resource.h file also) and you're done: any C/C++ project can pick up those files and compile them in. Managed code (C#, VB.Net) has a somewhat more complex scheme, but it's still the same basic idea: write out a description in the style that the managed tools expect, and they'll happily compile it and use it.
(ActiveX controls are - you've guessed it - a whole 'nother story. There isn't a standard format that I'm aware of, so the form editor and runtime that consumes the data would be closely tied together - eg. the form editor from pre-.Net VB6 produces forms that only VB can use. - I think. It's been some time ago...)
As for recreating the form: if you have a .rc file, it gets compiled into a dialog resource, Windows has built in support to recreate those. Likewise, the managed code support libraries know how to recreate a form from its particular format. Both basically parse the description, and for each item, create elements of the appropriate classes, and set the appropriate style, text, and other properties as specified. It's not doing anything you can't do yourself, its just helper utility code.
Handling Focus
For a collection of HWNDs in any container, whether in 'test' mode or actually running in the real app, and regardless of whether you let Windows or Winforms handle the form creation or whether you created each HWNDs yourself, you can add tabbing support by calling IsDialogMessage in your message loop: see the MSDN page remarks section for details. (While WinForms could do this, I think it actually does its own focus handling, so that it can have tab order independent from visual stacking Z-Order.)
Other Things To Explore...
Make friends with the Spy++ app (part of the SDK, installs with Visual Studio). If you're going to do anything with HWNDs, managed or unmanaged, it's a real good idea to know how to use this tool: you can point it at any piece of UI on Windows, and see how it's constructed out of a tree of different types of HWNDs. Point it at the VB designer and see what's really happening for yourself. (Click the 'binoculars' icon on the toolbar, then drag the crosshairs the the window you're interested in.)
Also take a look at the resource files that the designer spits out. Everything that you can tweak or move or edit in the forms designer corresponds to some item somewhere in one of those resource files. Make a copy of them, tweak some settings, and then file-compare the two sets, and see what's changed. Try changing some things in the files by hand (I think they're nearly all text), reload, and see if the designer picked up your changes.
Other Things To Note...
Much of the above is specific to Windows - notably the fact that since we're using Window's own building blocks - HWNDs - we can get Windows itself to do some of the hard work for us: it gives us the facilities to reuse the controls themselves at design time so we don't have to draw mock-ups; to intercept input on other controls so we can make a click into a move or whatever other action we want, or figure out which control is clicked without having to do the location math ourselves. If this was a designer for some other UI framework - say Flash - which doesn't use HWNDs internally, it would likely instead use that framework's own internal facilities to do similar work.
Also, it's far easier if you limit the number of controls in the palette to a small finite set, at least at first. If you want to allow any control at all to be dragged in - eg. a 3rd party one, or one you've used in another project; you typically first need some way for that control to be 'registered' so that the designer knows that it's available in the first place. And you may also need some way to discover what icon it uses in the toolbar, what its name is, what properties it supports - and so on.
Have fun exploring!

You implement a form designer almost like a normal GUI. You have stuff you can drag (your widgets), you have stuff you can click (your buttons) and you have stuff you can select (your placed widgets) and that's really about it.
Q: Now, how do you display a window in a GUI?
A: You paint it, simple as that.
Q: And how do you keep stuff inside that window?
A: You check against the bounds of the "parent" object. You could almost say that a form designer is like a little game and you have a scene graph holding all your widgets, connected by parent-child-relations.
Q: Then, how do you select stuff in a GUI?
A: Check the current mouse position on-click against the bounds of all (near) widgets (a scene graph only helps here, like a quadtree).
Q: How do you align widgets on a grid?
A: For the grid alignment, let's have a simple example: Say your real resolution is 100px on the x-axis, but you want your grid to only have a resolution of 10px on x. Now say you move your widget by 28px in real resolution. To get the grid resolution, you simply divide by 10, get 2.8, round that, and finally move the widget 3 tiles on x. The rounding is the key here. only if the grid movement is >= ?.5, you snap to the next tile. Otherwise you simple stay at the old one.
Hope this can give you a general hint on how to start a form designer. Have fun. :)
(PS: Don't know about any specific WinAPI/MFC functions/class to help you along, sorry.)

Just to add a point or two to what #Xeo has already said:
First of all, no, you don't always draw all the content yourself. During normal design phase you're basically just drawing something that looks like the control, but (at least IIRC) it also lets you "run" a form in test mode (certainly the VC++ dialog designer does, and even though VB's was more primitive, I think it did have that particular capability as well). Test mode was when you can "run" a form before you've (necessarily) attached any code to it -- even though clicking a button (for example) doesn't do anything in the surrounding program, the control itself works as normal -- a button clicks normally, an edit control will let you edit, etc.
That is done by actually instantiating a control, telling it the correct position, size, and properties. ActiveX controls do quite a bit to support this, and the previous "Windows custom controls" did as well, though with considerably less sophistication. From the viewpoint of the control, it's working just about exactly like it normally would, receiving input, sending notification to its parent, etc. The only thing that's changed is that the parent most ignores most of the notifications it receives, but the control doesn't really know that.
There are two basic ways to do this. One is to create a collection of controls yourself, along with their positions, sizes, etc., and use CreateWindow (or CreateWindowEx, etc.) to create window of the correct class. While relatively easy to handle, this has the disadvantage that it leaves all the tab-handling to you.
The other possibility is to create a DLGTEMPLATE structure to hold data about the dialog box, and a some DLGITEMTEMPLATES for the individual controls, and finally use CreateDialogIndirect to create a dialog box with those specs, holding those controls. It's tedious, but it works, and when you're done it handles tabbing between controls automatically (and works the same as any other dialog, since it's the same Windows code creating it either way).
Second, since you've tagged this C++, you might want to take a look at some code on CodeProject that actually implements a dialog editor. Although it isn't quite as sophisticated as some of the commercial ones, this is a reasonably complete form/dialog editor, complete with most of what you've asked about.

Related

GUI Composition in Qt

I have experience building GUIs in Java, C#, and C++ using the WinAPI. I'm attempting my first GUI in C++ using Qt and I have a bit of a transition problem. In any of the other three GUI paradigms, I could create my own GUI components, extend existing JPanels, etc. and end up with a custom-made GUI element that I could add to other GUI windows. C# is particularly good at this as anything I create automatically shows up in the Toolbox after it compiles.
From what I can see there are only two options in Qt and I'm hoping there is something I'm missing. The options are:
Create a custom Widget which may then be imported using a widget container promoted to the right widget.
Build what I like and drag it to scratchpad, from where I can replicate it to my heart's content.
The problem is that I want dynamically add/remove GUI elements at run time. For example, I have a Model-View-Control pattern. The view and control are two different panels in the GUI. When I switch to a different view in one panel, I want to remove the control from the other panel and replace it with the right control for the new view.
From what I can tell, I could do this using custom widgets, but it seems extremely heavy handed to do this for tons of minor widgets in composition to a greater capability. And although I can do the kind of composition I want in scratchpad, I can't figure out how to dynamically add something I have saved in scratchpad.
It is also possible that there is a Qt way of doing things which I have not found. For instance, I have some friends that have successfully created the effects above using different tabs. That seems like a cheap answer, which I could use, but I'd like to believe that Qt has sufficient flexibility to let me build any kind of GUI.
Any suggestions would be helpful
---------------------Improved example--------------------
Let me use the example code that #m7913d gave. (http://doc.qt.io/qt-5/qtwidgets-layouts-basiclayouts-example.html) Suppose that I want to make this portion of the gui into a reusable component which i can add to any GUI I want. How do I do that?
If this was C# I would create a new user control, add the GUI elements, set up the code that makes the unit work as a whole and possibly set up event listeners or properties of the user control as a whole. then I can instantiate, add, and remove them at will from the rest of the GUI. That is what I want.

Game pad Menu Navigation Programmatically

I've searched the web using all the search terms that my mind could come up with but all to no avail. I guess it's mainly because I can't explain my question with a couple of keywords. Anyway, here is my question:
You know how in games that use consoles you can navigate through the menu using the game pad buttons? And when you press right, it automatically selects the nearest control on the right of the current control. Likewise with up and down. And when you press a certain control and another list of menu unfolds on top the original control, and you are limited to select one of the new controls and can't select the other older controls even though they still appear on the screen? Well, how is that created programmatically? I'd love it if you can provide obj-c code of an implementation but any language would be fine. If not possible, then a pseudo code would also work fine with me. Even plain text would work with me but I wouldn't recommend it since I'm not a native speaker and may have issues understanding...
Basically, here is my question in bullet points in case I wasn't clear above due to my weak English:
1- How to move through a menu using a game pad.
2- Once a drop down list is selected, how to lock the contrils that can be selected to the controls that unfold from the drop down list.
And that's pretty much all of it. Thanks a lot for the help!
Make a collection of buttons
For each button, make each dpad direction optionally map to a new button
Make each button selection optionally map to a new collection
Done
Hard-coding is one way to map one piece of data to another. Or a file that describes it. Or static data. Or calculated algorithmically.

Need to add anchoring/docking to legacy MFC dialog application

I am working with MFC code that I believe was developed in the early 90's. I've been given the great task of bringing the software into the 21st century, getting it to work on the likes of Windows 7/8. The application targets numerous platforms, of which one is Windows XP. The original software had a fixed window size and looks terrible on certain OS. I have managed to overcome this but sizing the dialog leaves a lot of grey space. I need to incorporate anchors and docking, similar to .NET.
As always, time is limited, so I need quick, "dirty" solutions, until I get time to rewrite the UI layer. The application contains a number of "screens", each following a similar format. Banner at the top, content consisting of copyright, help on the LHS and task buttons on the RHS and a kind of footer control containing "hotkeys".
As a quick fix, I am thinking that resizing the dialog should cause the following.
Banner is anchored left and right
LHS/RHS content is split say 60/40
Footer is as per the banner
This is made more difficult as different controls are used for different target operating systems/platforms. Basically, the OnInitDialog, uses conditional compilation to to add controls, dynamically, depending on the platform.
To implement this I am guessing I need something like the following...
Each control "remembers" its bounds
I expect this to be tricky as no WM_CREATE message for dialog child controls.
Possibly use OnParentNotify.
Sizing the dialog "remembers" its last size and calculates differences in width and height.
The dialog sends a parent resize message to its immediate children so they can re-calculate layout.
My question, finally, is what is the best way to approach this?
One idea I have...
Introduce a new Widget class that extends CWnd and returns anchor details via a virtual method.
Create controls such as CBanner, CCopyright, CFooter etc that implement Widget
Create a RowWidget for content that sizes LHS and RHS content appropriately.
Now that was hard to put into words!
Any help appreciated.
Thanks
Karl
Actually a very common question and your reasoning is sound, but rather than reinventing the wheel, it might be better to first look at some freely available implementations along the lines you describe.
For example this CodeProject article does what I think you need.

D Forms Library TableLayoutPanel?

Every time I try to use the D Forms Library, I run across the fact that it has no TableLayoutPanel -- which makes it practically impossible for me to make a good GUI.
Is there any TableLayoutPanel implementation out there for DFL?
How do people usually use this library without it?
Edit:
A link to another program that uses DFL would be a great bonus for an answer. :)
Posting as an answer because length exceeds comment length limit.
I downvoted your question because it is formulated with elements of flamebait. I would guess that your previous experience with GUI libraries was mostly with libraries supporting box layouts, such as Qt. The Win32 GUI API itself does not provide any primitives for creating box layouts - it uses absolute coordinates through and through. This remains unchanged in many OO libraries that build on top of the API, such as MFC. Some libraries, like VCL, have optional primitives for creating box layouts (panels with alignment and auto-size) - but in the end, all control repositioning has to be done by the application or the GUI framework, so something like this would need to be implemented in DFL from scratch.
So, to answer your questions:
Is there any TableLayoutPanel implementation out there for DFL?
Probably not.
How do people usually use this library without it?
They draw the controls on the form with a mouse, using Entice Designer. (The same is true for MFC/Visual Studio, VCL/Delphi IDE, etc.)
Reply to comment:
how do I put things in a table layout (e.g. two side-by-side, and one below)?
I understand that you'd like to have a fixed-height panel at the bottom, and split the remaining space into two areas which both remain half the form's width when the form is resized.
In Entice Designer, place a Panel, set its dock to BOTTOM. Set its height appropriately.
Place a second panel, set its dock to RIGHT.
In your form's code, add the following method:
protected override void onResize(EventArgs ea)
{
super.onResize(ea);
panel2.width = this.clientRectangle.width / 2;
}
As you can see, it can quickly get messy to get a more complicated "rubber table". I wouldn't bother, or if I really needed complex dynamic layouts, would look for another library.
Or are you saying that's a bad idea in the first place?
Definitely not my point - the advantages of semantic layouts that don't require using an IDE to build are clearly visible. It's just that due to their Win32 API roots, Windows GUI libraries rarely provide good means to build them. Of course, their absence doesn't make building GUIs impossible or even hard - people simply usually go with fixed-size forms, etc. (This is clearly visible to end users switching from Windows to KDE - most KDE dialogs are resizable, while Windows' aren't.)
in lack of a table layout you can use the location and size properties to position stuff on the board (and maybe even implement your own table layout)
you can use the entice designer to make the gui and build further on the generated source
Now that I'm near my code, there are two ways to manage layout. As mentioned by ratchet there is absolute positions and also docking. Docking places the item in 5 possible locations. The Top, Bottom, Left, Right, or Center (fill). You can then place a panel in one of these which can itself contain elements that are docked within it. You assign the docking value to the dock property.
Entice Designer is written with DFL.

How to create something like firefox's bookmark sidebard in Visual Studio 2008 C++ project?

I have visual studio 2008, and want to build an GUI application that on the left side has a frame that can be minimized like the firefox bookmark sidebar. So my questions are: 1) What type of project do I need? 2) What controls actually make up the sidebar. 3) What do I make the main frame so that I can resize it when the sidebar is open. A example would also be cool.
Thanks in advance.
CP
It looks like a TaskPane (CTaskPane ) attached to a simple SDI frame window (taking into account firefox is doing a lot of things custom with their own toolkit (I think, and I've been known to be wrong)
Download the MFC VS2008 feature pack (with the new UI controls) and the feature pack samples and have a look at some of the sample projects (for example "TaskPane" and "VisualStudioDemo"
Though often maligned, as Max pointed out, MFC provides CTaskBar, which is designed for exactly this sort of thing. What you have inside it looks a lot like a CTreeCtrl (or CTreeeView) with an associated CImageList to display the icons for the bookmarks. Resizing the mainframe while the sidebar is open is handled automatically. To create a project that includes this, select the "Visual Studio" application type (after choosing "MFC Application").
Just as a note for the record, I'm reasonably certain that FireFox does not actually use an MFC CTaskBar. FireFox's sidebar is quite limited by comparison -- it only allows one sidebar with one pane in one position. CTaskBar supports multiple task bars, that the user can rearrange at will, each of which can have multiple tiled and/or tabbed panes (and, for example, the user can not only move a complete task bar by dragging, but also drag panes between task bars, drag a pane out on its own to become a separate task bar, etc.
Note that nearly all of this is handled internally by MFC -- you simply create window classes for storing and drawing data in the panes, and it handles things like keeping track of where a pane is at the moment.
well, you can always download the source for FF here and have a look for yourself, you might find more than you expect ;). another good tool to remember is spy++(in VS), very useful for finding properties of windows etc