Search code examples
c++wxwidgets

How to make popup image? wxWidgets


I'm trying to make a popup image preview window like it's done in Autodesk Revit Architecture:

see how it looks in Revit

The behaviour of popup image is:

  1. When the mouse stops for 500 milliseconds over the truncated image, a full-sized popup image appears near the mouse cursor.
  2. The Popup image is not a modal dialog, therefore controls of the main window(wxDialog) are still enabled.
  3. The Popup window disappears on mouse movement.

I tried to do it, but i failed. First I put wxStaticBitmap on wxDialog and use ShowModal() to show this full-sized image. It works great but as it's Modal, main window becomes disabled. I tried to make this dialog not modal, but when I try to do it, main window raises(main window is modal) and image disappears.

upd. Now my code:

class PictureFrame: public wxPopupTransientWindow
{
  wxStaticBitmap *m_picture;
public:
  PictureFrame( wxWindow *parent );
  ~PictureFrame();
};

Panel code structure is like this:

class MaterialsPane: public wxPanel
{
  PictureFrame* m_popup;
  wxTimer* m_timer;
public:
  MaterialsPane( wxWindow* parent);
  ~MaterialsPane();
  void OnTimer( wxTimerEvent& event);
  void OnMouseMove( wxMouseEvent& event );
  ....
  DECLARE_EVENT_TABLE()
};

Panel is placed in main modal dialog:

class MaterialsFrame: public wxDialog {
  MaterialsPane* m_materialsPane;
public:
  MaterialsFrame( wxWindow* parent, wxWindowID id = wxID_ANY);
  ~MaterialsFrame();
};

it helped but not completely. As image appears not under mouse cursor but near it (like in the picture of my question), popup window can't catch mouse movements. I tried to catch mouse movements in main dialog, but it failed, because focus is taken by popup window.

My goal is to close popup after any mouse movement.


Solution

  • You should post your code that 'fails'. It is hard to give specific advice when we have no information of what you are doing.

    Have you looked at wxPopupWindow? http://docs.wxwidgets.org/trunk/classwx_popup_window.html

    Personally, I find it easier to roll my own. Here's how the one I am working on right now looks

    cNewDataPopup::cNewDataPopup(  cPatDataset& data )
        : wxDialog(NULL,-1,L"New data",wxPoint(200,200),wxSize(570,242),
            wxDEFAULT_DIALOG_STYLE|wxSTAY_ON_TOP )
        , myData( data )
    { 
        ...
        Show();
    }
    

    To make this popup appear, simply call the constructor.

    You would want to pass in your image to be displayed, store it in an attribute, handle the paint event by drawing your image on the client area.