Welcome!

PowerBuilder Authors: Chris Pollach, Avi Rosenthal, Yakov Fain, RealWire News Distribution, Al Soucy

Related Topics: PowerBuilder

PowerBuilder: Article

Popup Menus: And Now, Something Completely Different

Popup Menus: And Now, Something Completely Different

How many times have you needed to add an extra menu item in a DataWindow popup menu? If you've ever done it, you know what a nightmare it is and how time-consuming it can be.
You'd need to go through the following steps (at least):

  • Create or inherit a new menu for this purpose and add the new menu item needed.
  • Place code in the new menu item's Clicked Event to dynamically call the desired DW method.
  • Override the DW RbuttonUp event and change the reference from m_dw to a new popup menu.
  • Place code in the DW RbuttonUp Event to create and pop up the new menu.
  • Place code in the DW method - the one that will be called from the popup menu.
  • Place code in the Pfc_PreRmbMenu Event.

You're probably getting the picture already - all this work for just one extra popup menu item! And this is how it's done in most PB frameworks today, PFC-based or not. You need to practically override and redesign the whole popup menu service. And you need to do it for every DataWindow whose popup menu needs to be customized.

Creating a Service
Instead you can create what I call an Easy Popup Menu DataWindow Service. Once it's done, you'll need only one line of code in your DataWindow control to create the extra menu item and one line of code to perform an action based on the user's choice. Yes, you're reading this correctly - only two lines of code in a DataWindow control to display an extra popup menu item and perform an action when the user clicks on a newly created menu item.

This Easy Popup Menu technique can be implemented in any PB framework, PFC-based or not.
It was originally implemented in the Hepek PowerBuilder Framework. Since many PB developers aren't familiar with Hepek yet and it's not PFC-based, I'll present the Easy Popup Menu Service as a PFC extension, written using PB7 and PFC7 code examples. We'll need to add some items and methods to m_dw, as well as some events in the u_dw object.

Extending m_dw
First let's add three custom events in m_dw object - m_custom1, m_custom2, and m_custom3 - with three separation lines, m_sep1, m_sep2, and m_sep3, between them. Place them right under the m_table menu item where all other DW menu items are located. Note that the text for each separator line needs to be a dash sign (-). You don't have to limit the number of custom events to three, of course. So far, we have a m_dw menu, as shown in Figure 1.

Now hide all the menu items (custom menu items and separators) you just created - set the Visible menu item property to FALSE. Place one line of code in the Clicked Event for each custom menu item you've created. The code will call the DataWindow event dynamically:

idw_parent.EventDYNAMIC ue_custom_event(1)

(Note that the argument to a DataWindow ue_custom_event will be different for different menu items. For m_custom1 it'll be 1; for m_custom2, 2; and for m_custom3, 3.)

We also need to create one menu function, mf_show_custom, that'll be called from the parent DataWindow. Based on its arguments, the mf_ show_custom function will set the particular custom menu item text, show the item, and eventually show the separator line before the menu item. It takes three arguments - ai_section, as_item_text, and ab_show_separator. The function declaration and function body are shown in Listing 1.

You may think this is 'expensive'; showing and hiding menu items dynamically isn't good practice. Well, it's not true in this case, because this mf_show_custom function will happen after the popup menu is created, but before it becomes visible and pops up. The creation and display of a popup menu using the Easy Popup Menu technique takes no time at all on any machine and it's not resource-intensive either.

Extending u_dw
So far, we've created popup menu items and methods. The next step is to add a ue_custom_event user event in the u_dw object. This event is called from custom menu items created previously and doesn't have any code in the u_dw object. All the code will be in the actual application when using the Easy Popup Menu Service. It's an empty user event that allows you to perform a particular action based on user choice.

The event takes one argument and event declaration as follows:

Event ue_custom_event (integer ai_section)

The ue_popup_menu event will be used in combination with the DW Pfc_PreRmbMenu Event to display the extra menu item and perform a particular action based on the user's choice.

The Pfc_PreRmbMenu Event is an empty user event that allows you to modify m_dw contents before displaying the popup menu. You can optionally add logic to this event to selectively enable or disable popup menu items.

At this point we're ready to use the service in an application. In this example, in the Departments Data Window, we'll add an extra menu item in the popup menu. By clicking on this menu item, users will print the Department Report for the department that currently has focus.

Put this code in the DW pfc_prermbmenu event:

// Display an extra popup menu item for Department
// Activity Report and show the separator line
am_dw.mf_show_custom (1, &
'Department Activity Report', TRUE)

In addition, you need to place the following code in DataWindow ue_custom_event the event:

IF ai_section = 1 THEN
MessageBox('Easy Popup Menu Service', &
'Department Activity Report')
END IF

The popup menu in this example is shown in Figure 2.

Listing 2 is an example of an Employees' DataWindow that's data sensitive and shows or hides popup menu items based on whether the employee is a sales rep.
The following code needs to be placed in the DataWindow Ue_custom_ event Event:

CHOOSE CASE ai_section
CASE 1
MessageBox('Easy Popup Menu Service', &
'Employee Details')
CASE 2
MessageBox('Easy Popup Menu Service', &
'SalesRep Report') END CHOOSE

The popup menu is data sensitive and displays the SalesRep Report item only if the person whose row has focus is a sales rep rather than an ordinary employee. Susan Smith is a sales rep while Matthew Cobb isn't (see Figures 3 and 4).

Conclusion
The Easy Popup Menu technique is a useful, time-saving feature, fully implemented in the Hepek PowerBuilder Framework. Hepek goes even further and uses the same popup menu for DataWindow, ListView, and TreeView controls. To see the Easy Popup Menu in action, visit www.hepek.com and download code examples.

More Stories By Nermin Tanovic

Nermin Tanovic, a senior technology specialist at Bowne Technology Enterprise, a major financial printing company in New York, is the author of the Hepek PowerBuilder Framework. His web site is
www.hepek.com.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.