Wxwindows custom events




















One of them uses event table macros and allows you to define the binding between events and their handlers only statically, i.

It also allows the direct binding of events to:. On the other hand, event tables are more succinct and centralize all event handler bindings in one place. You can either choose a single approach that you find preferable or freely combine both methods in your program in different classes or even in one and the same class, although this is probably sufficiently confusing to be a bad idea. Also notice that most of the existing wxWidgets tutorials and discussions use the event tables because they historically preceded the apparition of dynamic event handling in wxWidgets.

But this absolutely doesn't mean that using the event tables is the preferred way: handling events dynamically is better in several aspects and you should strongly consider doing it if you are just starting with wxWidgets. On the other hand, you still need to know about the event tables if only because you are going to see them in many samples and examples. So before you make the choice between static event tables and dynamically connecting the event handlers, let us discuss these two ways in more detail.

In the next section we provide a short introduction to handling the events using the event tables. To use an event table you must first decide in which class you wish to handle the events.

The only requirement imposed by wxWidgets is that this class must derive from wxEvtHandler and so, considering that wxWindow derives from it, any classes representing windows can handle events. Simple events such as menu commands are usually processed at the level of a top-level window containing the menu, so let's suppose that you need to handle some events in MyFrame class deriving from wxFrame. First define one or more event handlers. They are just simple methods of the class that take as a parameter a reference to an object of a wxEvent-derived class and have no return value any return information is passed via the argument, which is why it is non-const.

You also need to insert a line with the macro indicating that the class uses an event table, like this:. It doesn't matter where it appears but it's customary to put it at the end because the macro changes the access type internally so it's safest if nothing follows it. The full class declaration might look like this:. Next the event table must be defined and, as with any definition, it must be placed in an implementation file.

The event table tells wxWidgets how to map events to member functions and in our example it could look like this:. Notice that you must mention a method you want to use for the event handling in the event table definition; just defining it in MyFrame class is not enough.

Let us now look at the details of this definition: the first line means that we are defining the event table for MyFrame class and that its base class is wxFrame , so events not processed by MyFrame will, by default, be handled by wxFrame. The next four lines define bindings of individual events to their handlers: the first two of them map menu commands from the items with the identifiers specified as the first macro parameter to two different member functions. Note that this macro doesn't need a window identifier, since normally you are only interested in the current window's size events.

But only command events, so you can't catch mouse move events in a child control in the parent window in the same way because wxMouseEvent doesn't derive from wxCommandEvent. See below for how you can do it. In this case, the button's event table will be searched, then the parent panel's, then the frame's. Finally, you need to implement the event handlers.

As mentioned before, all event handlers take a wxEvent-derived argument whose exact class differs according to the type of event and the class of the originating window. For size events, wxSizeEvent is used. For menu commands and most control commands such as button presses , wxCommandEvent is used.

When controls get more complicated, more specific wxCommandEvent-derived event classes providing additional control-specific information can be used, such as wxTreeEvent for events from wxTreeCtrl windows. In the simplest possible case an event handler may not use the event parameter at all.

For example,. You will find the details about the event table macros and the corresponding wxEvent-derived classes in the discussion of each control generating these events. The possibilities of handling events in this way are rather different.

Event handlers can be bound at any moment. For example, it's possible to do some initialization first and only bind the handlers if and when it succeeds. This can avoid the need to test that the object was properly initialized in the event handlers themselves.

The main features of a window explained; a quick guide to the commonest window classes; base window classes such as wxWindow; top-level windows; container windows; non-static controls; static controls; menus; control bars. Device context principles; the main device context classes described; buffered drawing; drawing tools; device context drawing functions; using the printing framework; 3D graphics with wxGLCanvas.

Handling mouse and mouse wheel events; handling keyboard events; keycodes; modifier key variations; accelerators; handling joystick events. Layout basics; sizers introduced; common features of sizers; programming with sizers.

Further layout issues: dialog units; platform-adaptive layouts; dynamic layouts. Steps in creating a custom dialog; an example: PersonalRecordDialog; deriving a new class; designing data storage; coding the controls and layout; data transfer and validation; handling events; handling UI updates; adding help; adapting dialogs for small devices; further considerations in dialog design; using wxWidgets resource files; loading resources; using binary and embedded resource files; translating resources; the XRC format; writing resource handlers; foreign controls.

Image classes in wxWidgets; programming with wxBitmap; programming with wxIcon; programming with wxCursor; programming with wxImage; image lists and icon bundles; customizing wxWidgets graphics with wxArtProvider. The window will then own the object, and will take care of its deletion. If an existing layout constraints object is already owned by the window, it will be deleted. Used by wxSizer internally to notify the window about being managed by the given sizer. This method should not be called from outside the library, unless you're implementing a custom sizer class — and in the latter case you must call this method with the pointer to the sizer itself whenever a window is added to it and with NULL argument when the window is removed from it.

The cursor may be wxNullCursor in which case the window cursor will be reset back to default. Note that if you use this function you may want to use as the "next" handler of handler the window itself; in this way when handler doesn't process an event, the window itself will have a chance to do it. This function is called by wxWidgets keyboard navigation code when the user gives the focus to this window from keyboard e.

By default this method simply calls SetFocus but can be overridden to do something in addition to this in the derived classes. This function should not be called for the parent window if you don't want its font to be inherited by its children, use SetOwnFont instead in this case and see InheritAttributes for more explanations. Please notice that the given font is not automatically used for wxPaintDC objects associated with this window, you need to call wxDC::SetFont too.

However this font is used by any standard controls for drawing their text as well as by GetTextExtent. The meaning of foreground colour varies according to the window class; it may be the text colour or other colour, or it may not be used at all.

Additionally, not all native controls support changing their foreground colour so this method may change their colour only partially or even not at all. Sets the initial window size if none is given i. A smart SetSize that will fill in default size components with the window's best size values.

Also sets the window's minsize to the value passed in for use with sizers. This means that if a full or partial size is passed to this function then the sizers will use that size instead of the results of GetBestSize to determine the minimum needs of the window for layout. Most controls will use this to set their initial size, and their min size to the passed in value if any.

Sets the maximum client size of the window, to indicate to the sizer layout mechanism that this is the maximum possible size of its client area. Sets the maximum size of the window, to indicate to the sizer layout mechanism that this is the maximum possible size. Sets the minimum client size of the window, to indicate to the sizer layout mechanism that this is the minimum required size of window's client area. You may need to call this if you change the window size after construction and before adding to its parent sizer.

Note, that just as with SetMinSize , calling this method doesn't prevent the program from explicitly making the window smaller than the specified size. Sets the minimum size of the window, to indicate to the sizer layout mechanism that this is the minimum required size. Notice that calling this method doesn't prevent the program from making the window explicitly smaller than the specified size by calling SetSize , it just ensures that it won't become smaller than this size during the automatic layout.

Note that instead you can use PushEventHandler or SetEventHandler to implement a stack of event handlers to override wxWindow 's own event handling mechanism. Sets the background colour of the window but prevents it from being inherited by the children of this window. Sets the foreground colour of the window but prevents it from being inherited by the children of this window.

This is exactly the same as calling Move with the default arguments. Reimplemented in wxScrollBar. The size is specified using a wxRect , wxSize or by a couple of int objects.

Use of this function for windows which are not toplevel windows such as wxDialog or wxFrame is discouraged. If an existing layout constraints object is already owned by the window, it will be deleted if the deleteOld parameter is true.

Note that this function will also call SetAutoLayout implicitly with true parameter if the sizer is non- NULL and false otherwise so that the sizer will be effectively used to layout the window children whenever it is resized. This method calls SetSizer and then wxSizer::SetSizeHints which sets the initial window size to the size needed to accommodate all sizer elements and sets the minimal size to the same size, this preventing the user from resizing this window to be less than this minimal size if it's a top-level window which can be directly resized by the user.

This function tells a window if it should use the system's "theme" code to draw the windows' background instead of its own background drawing code. This does not always have any effect since the underlying platform obviously needs to support the notion of themes in user defined windows.

Dialogs, notebook pages and the status bar have this flag set to true by default so that the default look and feel is simulated best. If the system supports transparent windows, returns true , otherwise returns false and the window remains fully opaque. See also CanSetTransparent. The parameter alpha is in the range Deletes the current validator if any and sets the window validator, having called wxValidator::Clone to create a new validator of this type.

See SetWindowStyleFlag for more info. Please note that some styles cannot be changed after the window creation and that Refresh might need to be called after changing the others for the change to take place immediately. See Window styles for more information about flags.

Window variants currently just differ in size, as can be seen from wxWindowVariant documentation. Under all platforms but macOS, this function does nothing more than change the font used by the window. However under macOS it is implemented natively and selects the appropriate variant of the native widget, which has better appearance than just scaled down or up version of the normal variant, so it should be preferred to directly tweaking the font size.

Return true from here to allow the colours of this window to be changed by InheritAttributes. The base class version returns false , but this method is overridden in wxControl where it returns true.

You may need to call Raise for a top level window if you want to bring it to top, although this is not needed if Show is called immediately after the frame creation. Notice that the default state of newly created top level windows is hidden to allow you to create their contents without flicker unlike for all the other, not derived from wxTopLevelWindow , windows that are by default created in the shown state.

This function shows a window, like Show , but using a special visual effect if possible. Re-enables window updating after a previous call to Freeze. To really thaw the control, it must be called exactly the same number of times as Freeze. This method performs the conversion only if it is not already done by the lower level toolkit, For example, you may want to use this to store window sizes and positions so that they can be re-used regardless of the display DPI:.

Using these methods is discouraged as passing NULL will prevent your application from correctly supporting monitors with different resolutions even in the future wxWidgets versions which will add support for them, and passing non- NULL window is just a less convenient way of calling the non-static ToDIP method. This function cannot be used if the value of the flag is 0 which is often the case for default flags.

Notice that this also calls TransferDataFromWindow for all children recursively. Notice that this also calls TransferDataToWindow for all children recursively. Calling this method immediately repaints the invalidated area of the window and all of its children recursively this normally only happens when the flow of control returns to the event loop.

Notice that this function doesn't invalidate any area of the window so nothing happens if nothing has been invalidated i. Use Refresh first if you want to immediately redraw the window unconditionally. This function sends one or more wxUpdateUIEvent to the window. The particular implementation depends on the window; for example a wxToolBar will send an update UI event for each toolbar button, and a wxFrame will send an update UI event for each menubar menu item.

You can call this function from your application to ensure that your UI is up-to-date at this point as far as your wxUpdateUIEvent handlers are concerned. Some windows update their elements only when necessary, for example when a menu is about to be shown.

The following is an example of how to call UpdateWindowUI from an idle function. Same as UseBgCol. Notice that this also calls Validate for all children recursively. Converts window size size to corresponding client area size In other words, the returned value is what would GetClientSize return if this window had given window size.

Version: 3. Styles This class supports the following styles:. Displays a thin border around the window. Displays a border suitable for a static control. Windows only. Displays a native border suitable for a control, on the current platform. On Windows, this will be a themed border; on most other platforms a sunken border will be used.

For more information for themed borders on Windows, please see Themed borders on Windows. Displays no border, overriding the default border style for the window. The window is transparent, that is, it will not receive paint events. This style is used by wxWidgets for the windows supporting TAB navigation among their children, such as wxDialog and wxPanel.

It should almost never be used in the application code. If you need to use this style in order to get the arrows or etc. On Windows, this style used to disable repainting the window completely when its size is changed. Since this behaviour is now the default, the style is now obsolete and no longer has an effect.

Use this style to enable a vertical scrollbar. Notice that this style cannot be used with native controls which don't support scrollbars nor with top-level windows in most ports. Use this style to enable a horizontal scrollbar. If a window has scrollbars, disable them instead of hiding them when they are not needed i.

Use this style to eliminate flicker caused by the background being repainted, then children being painted over them. Use this style to force a complete redraw of the window whenever it is resized instead of redrawing just the part of the window affected by resizing. Note that this was the behaviour by default before 2. Using this flag for the given window allows blocking this propagation at this window, i.

Dialogs have this flag on by default for the reasons explained in the Events and Event Handling. Under Windows, puts a query button on the caption.

See wxActivateEvent. See wxChildFocusEvent. A right click or other context menu command depending on platform has been detected. See wxContextMenuEvent. See wxHelpEvent. See wxDropFilesEvent. See wxEraseEvent. See wxFocusEvent. See wxIdleEvent. Processes joystick events. See wxJoystickEvent. See wxKeyEvent. See wxMouseCaptureLostEvent. See wxMouseCaptureChangedEvent. See wxMouseEvent. See wxPaintEvent. The system power state changed.

See wxPowerEvent. Process scroll events. See wxScrollWinEvent. See wxSetCursorEvent. See wxSizeEvent. See wxSysColourChangedEvent. Library: wxCore. Category: Miscellaneous Windows. Constructs a window, which can be a child of a frame, dialog or any other non-control window. Parameters parent Pointer to a parent window.

If using the wxWindow class directly, supply an actual position. If no suitable size can be found, the window will be sized to 20x20 pixels so that the window is visible but obviously not correctly sized.

For generic window styles, please see wxWindow. Overridden to indicate whether this window or one of its children accepts focus. Adds a child window. Parameters child Child window to add.

Since 2. Remarks This function is currently not implemented. Prepare for changing positions of multiple child windows. Sets the cached best size value. See also GetBestSize. Can this window have focus right now? Directs all mouse input to this window. Centres the window. Parameters direction Specifies the direction for the centring. Remarks If the window is a top level one i. See also Center. Centres the window on its parent.

Remarks This methods provides for a way to centre top level windows over their parents instead of the entire screen. If there is no parent or if the window is not a top level window, then behaviour is the same as Centre. Clears the window by filling it with the current background colour. Does not cause an erase background event to be generated. Converts to screen coordinates from coordinates relative to this window.

Parameters x A pointer to an integer value for the x coordinate. Pass the client coordinate in, and a screen coordinate will be passed out. Parameters pt The client position for the second form of the function. Converts client area size size to corresponding window size. It doesn't close the window itself, however. Parameters force false if the window's close handler should be able to veto the destruction of this window, true if it cannot.

Returns true if the event was handled and not vetoed, false otherwise. Remarks Close calls the close handler for the window, providing an opportunity for the window to choose whether to destroy the window. Usually it is only used with the top level windows wxFrame and wxDialog classes as the others are not supposed to have any special OnClose logic. The close handler should check whether the window is being deleted forcibly, using wxCloseEvent::CanVeto , in which case it should destroy the window using wxWindow::Destroy.

Note that calling Close does not guarantee that the window will be destroyed; but it provides a way to simulate a manual close of a window, which may or may not be implemented by destroying the window. Converts a point or size from dialog units to pixels. Remarks Dialog units are used for maintaining a dialog's proportions even if the font changes. You can also use these functions programmatically.

Converts a point or size from pixels to dialog units. See also ConvertDialogToPixels. Destroys the window safely. Returns true if the window has either been successfully deleted, or it has been added to the list of windows pending real deletion. Destroys all children of a window.

Called automatically by the destructor. Disables the window. Returns Returns true if the window has been disabled, false if it had been already disabled before the call to this function.

Disable giving focus to this window using the keyboard navigation keys. Since 3. This function can be overridden to fine-tune centring behaviour. Override this method to implement height-for-width best size calculation.

Override this method to return the best size for a custom control. See also Window Sizing Overview Since 2. Override this method to implement width-for-height best size calculation. The default implementation of this function is designed for use in container windows, such as wxPanel , and works something like this: If the window has a sizer then it is used to calculate the best size.

Otherwise if the window has layout constraints then those are used to calculate the best size. Otherwise if the window has children then the best size is set to be large enough to show all the children. Otherwise if there are no children then the window's minimal size will be used as its best size. Otherwise if there is no minimal size set, then the current size is used for the best size. Does the window-specific updating after processing the update event. Enable event. GetEnabled ;.

SetTitle event. GetText ;. Enables or disables eligibility for drop file events OnDropFiles. Parameters accept If true , the window is eligible for drop file events. If false , the window will not accept drop file events. Remarks Windows only until version 2. Cannot be used together with SetDropTarget on non-Windows platforms.

See also SetDropTarget. Enable or disable the window for user input. Request generation of touch events for this window. Enables or disables visible indication of keyboard focus. This method is only implemented on Mac. Fix child window positions after setting all of them at once. Finds the window or control which currently has the keyboard focus. Remarks Note that this is a static function, so it can be called without needing a wxWindow pointer.

See also SetFocus , HasFocus. Find a child of this window, by id. May return this if it matches itself. Find a child of this window, by name. Find the first window with the given id. Find a window by its label. Sizes the window to fit its best size. See also Window Sizing Overview. If the window has any children, they are recursively frozen too.

Convert DPI-independent pixel values to the value in pixels appropriate for the current toolkit. For example, instead of creating a bitmap of the hard coded size of 32 pixels you should use wxBitmap bmp FromDIP 32, 32 ;. Non window-specific DPI-independent pixels conversion functions. Gets the accelerator table for this window. Returns the accessible object for this window, if any. Returns the background colour of the window. Returns the background style of the window.

Returns the best height needed by this window if it had the given width. This functions returns the best acceptable minimal size for the window. Returns the best width needed by this window if it had the given height. Returns the currently captured window. Returns a reference to the list of the window's children. Returns a const reference to the list of the window's children. Returns the default font and colours which are used by the control. See also InheritAttributes. Returns the size of the window 'client area' in pixels.

Returns the factor mapping logical pixels of this window to physical pixels. Note Current behaviour of this function is compatible with wxWidgets 3. Please use the other function if you need to use a scaling factor greater than 1. Return the cursor associated with this window. See also SetCursor. Return the DPI of the display used by this window. Returns the associated drop target, which may be NULL.

Merges the window's best size into the min size and returns the result. Returns the event handler for this window. By default, the window is its own event handler. Returns the font for this window. See also SetFont. Returns the foreground colour of the window. Remarks The meaning of foreground colour varies according to the window class; it may be the text colour or other colour, or it may not be used at all. Returns the grandparent of a window, or NULL if there isn't one. Returns the platform-specific handle of the physical window.

Gets the help text to be used as context-sensitive help for this window. Parameters point Coordinates of the mouse at the moment of help event emission. Returns the identifier of the window. Remarks Each window has an integer identifier. Can anyone help me? Follow Post Reply. Sion Arrowsmith. NewEventType class InvokeEvent wx. I found this idiom somewhere on the Web and can't remember where.

Cool, thanks a bunch! Chris Lambacher. You should be derriving from PyCommandEvent since only CommandEvents are set to propegate, which is probably what you want see the wxwidgets Event handling overview.

In order to use Bind you will need an instance of PyEventBinder. This discussion thread is closed Start new discussion. Similar topics Python. Don't understand wxPython event handling.



0コメント

  • 1000 / 1000