Using Dialog Prompts (Advanced)

Revision as of 14:31, 5 September 2007 by Paulb (Talk | contribs)

Construction.png Not Ready for Review: This Page is Under Development!

There are two methods of displaying dialog prompts inside EiffelStudio. Firstly there is the simple and effective approach, which requires very little coding but offers very little in the way of customization. The second approach can be simple, but by not means as simple as the first, and can offer a great deal of customization. The information in this page pertains for the latter approach. To use the former please see Using Dialog Prompts (Basic).

Getting Started

If you have ended up here then you are either curious or have decided that you want to use some of the extended features of the EiffelStudio Tool Dialog Prompts. The page is given the title "Advanced" and there are more advanced topics here but using the deep level of customization features is for everyone as it's simple enough to use.

Before continuing you may also want to check out the following pages:

Introducing the Dialog Prompt Classes

There are, as of EiffelStudio 6.1, four common dialog prompt classes for use which are somewhat self-explanatory.

  • ES_ERROR_PROMPT: Used to display a error.
  • ES_WARNING_PROMPT: Used to display a warning
  • ES_INFORMATION_PROMPT: Used to display a piece of informative text.
  • ES_QUESTION_PROMPT: Used to ask the user a yes/no question.

Information.png Note: There is a running convention in EiffelStudio (for classes prefixed with ES_) whereby all dialog prompts end in _PROMPT. From this you can determine if there are any specialized dialog prompts available for you to use, as the base implementation in ES_PROMPT is customizable. You can also use the code browsing tools to check for all descendents of ES_PROMPT.

Each dialog prompt class provide two or more creation routines; make and make_standard. Some prompt classes also have an addition creation routine, make_standard_with_cancel, to augment the standard button set with a Cancel button. The creation routine make offers the most control but requires more information to create the dialog and some understanding of the concpt of a Dialog Buttons Sets. The make_standard creation routines are simpler and only require a dialog prompt message, just like the basic dialog prompt usage detailed in Using Dialog Prompts (Basic).

Dialog Prompt Buttons

The make_standard creation routines initialize the dialog prompt using a standard set of dialog buttons in the order dictated by the executing platform. The standard dialog buttons are as follows (ordering representative on Windows):

  • Error: Ok
  • Warning: Ok
  • Information: Ok
  • Question: Yes|No

As mentioned, some of the prompts come with a third creation routine, make_standard_with_cancel, augmenting the existing dialog prompt's button set with a Cancel button. The only dialog prompt not to have a third creation routine is the Error prompt as an error is usual considered an exception case and general there is no recourse action. The standard dialog buttons "with cancel" are as follows (ordering representative on Windows):

  • Warning: Ok|Cancel
  • Information: Ok|Cancel
  • Question: Yes|No|Cancel

Button Sets

Most dialog prompts have a standard set of buttons and being so ES_DIALOG_BUTTONS provides access to the well-known and commonly used button sets, such as Ok|Cancel, Yes|No and even Abort|Retry|Ignore.

Information.png Note: All dialog prompts inherit ES_SHARED_DIALOG_BUTTONS through the base dialog implementation ES_DIALOG (see Tool Dialogs for more information on ES_DIALOG and dialog button sets.)

For information of button set and creating your own button set see Dialog Buttons Sets.

Setting Default Buttons

Default Active Button

Every prompt has a default active button, also known as the "Default Button". This is the button that will have the focus when the dialog is shown. In the following dialog prompt the No button is the default button.

ask_save_changes
    -- Ask user if they want to save the changes before continuing
  local
    l_prompt: ES_QUESTION_PROMPT
  do
    create l_prompt.make_standard ("If you press Yes, I'll tell you something...")
    l_prompt.show_on_active_window
  end

No is the default set default button


No was set as the default-default button automatically. Unless set, every dialog prompt uses the least dangerous button as the default button. In the above example No is actually set as the automatic default button because No is considered (statically) the least dangerous button for question prompts. If the question prompt was created with make_standard_with_cancel the Cancel button would actually be the default button because Cancel is considered less dangerous than No.

To override the default behavior you'll need to specify the default button. This has to be a button identifier for a button that exists on the dialog prompt. The following code demonstrates this.

ask_save_changes
    -- Ask user if they want to save the changes before continuing
  local
    l_prompt: ES_QUESTION_PROMPT
  do
    create l_prompt.make_standard ("If you press Yes, I'll tell you something...")
    l_prompt.set_default_button ({ES_DIALOG_BUTTONS}.yes_button)
    l_prompt.show_on_active_window
  end

Yes is now the default button


The default button is activated by pressing ENTER or SPACE when the dialog prompt is shown. It is also selected when the user presses CTRL+ENTER, which is inherited behavior from top most inherited class ES_DIALOG.

Default Cancel Button

There also exists a default cancel button, which is activated when the user presses ESC or closes the dialog prompt without pressing one of the dialog prompt buttons. Like the default button, this behavior is inherited from ES_DIALOG. The default cancel button is actually automatically determined for all prompts using ES_DIALOG_BUTTONS.default_cancel_buttons, which is also used to set the default button for all dialog prompts (remember, least dangerous action rule?)

You can set the default cancel button just like you can the default button, however setting the default cancel button will not affect the default button even if you have not set the default button. To set the default cancel button use the routine set_default_cancel_button, specifying a button identifier of a button that exists on the dialog prompt. The code below shows how this is done.

ask_save_changes
    -- Ask user if they want to save the changes before continuing
  local
    l_prompt: ES_QUESTION_PROMPT
  do
    create l_prompt.make_standard ("If you press Yes, I'll tell you something...")
    l_prompt.set_default_button ({ES_DIALOG_BUTTONS}.yes_button)
    l_prompt.set_default_cancel_button ({ES_DIALOG_BUTTONS}.no_button)
    l_prompt.show_on_active_window
  end

Responding to User Interactions

EiffelStudio dialog prompts offer a dual interaction model when responding to user interaction. This frees you from the bounds of choosing one model over the other and lifts the restrictions on using either a single model. Sometimes it may be necessary to use both models. That said, the use of agents if the preferred model inside EiffelStudio.

Agent Assignment

The primary model is the use of agents, assigning them to a button using a button identifier.

After creating a prompt you may associate an agent to a button using ES_PROMPT.set_button_action. The routine takes a button identifier, which must be an identifier of a button on the prompt, as well as an agent action.

ask_save_changes
    -- Ask user if they want to save the changes before continuing
  local
    l_prompt: ES_QUESTION_PROMPT
  do
    create l_prompt.make_standard ("Save changes before continuing?")
    l_prompt.set_button_action ({ES_DIALOG_BUTTONS}.yes_button, agent save_changes)
    l_prompt.show_on_active_window
  end
 
save_changes
    -- Save all changes
  do
    ...
  end

In the example a question prompt was created using the standard set of buttons for question prompts, which in this case are Yes and No buttons. Post-creation and initialization the Yes button is assigned an agent through the call to set_button_action. set_button_action is passed the button identifier yes_button, which is statically accessed from ES_DIALOG_BUTTONS. yes_button could have just as easily been accessed from the dialog_buttons function exported on the prompt class:

l_prompt.set_button_action (l_prompt.dialog_buttons.yes_button, agent save_changes)

However, static access if preferred if appropriate for the situation.

Using Dialog Results

The second model of processing actions or changing execution flow, based on the user's interaction with the dialog prompt, is through the result dialog_result. Like button identifiers, dialog_result is an INTEGER and is set once the dialog has been closed buy the result of a user's interaction.

Here is the same example written using the dialog result model.

ask_save_changes
    -- Ask user if they want to save the changes before continuing
  local
    l_prompt: ES_QUESTION_PROMPT
  do
    create l_prompt.make_standard ("Save changes before continuing?")
    l_prompt.show_on_active_window
 
    if l_prompt.dialog_result = {ES_DIALOG_BUTTONS}.yes_button then
      save_changes
    end
  end
 
save_changes
    -- Save all changes
  do
    ...
  end

Using static access for the dialog button identifiers also allows you to opt to use the Eiffel inspect construct instead of using if...then...elseif...then...end:

inspect l_prompt.dialog_result
when {ES_DIALOG_BUTTONS}.yes_button then
  save_changes
when {ES_DIALOG_BUTTONS}.no_button then
  do_something_else
end

Changing the Title, Subtitle and Text

Changing Button Labels

There are times when a dialog prompt's button labels are not adequate enough to get the full message over to the user, or simply that you want a dialog prompt's buttons to show an extremely terse explaination of their action. Once such dialog in EiffelStudio is the Discard Assertions question prompt:

Dialog shown when finalizing an Eiffel project


The discard assertion dialog prompt is a question prompt that has had it's Yes button label set to Discard Assertions and No button label set to Keep Assertions. There was no need to subclass ES_QUESTION_PROMPT (well actually ES_DISCARDABLE_QUESTION_PROMPT but we have not gotten to discardable dialog prompts yet) and redefine dialog_button_label. Instead set_button_text can be used, in a similar manner as set_button_action is.

ask_save_changes
    -- Ask user if they want to save the changes before continuing
  local
    l_prompt: ES_QUESTION_PROMPT
  do
    create l_prompt.make_standard ("Save changes before continuing?")
    l_prompt.set_button_text ({ES_DIALOG_BUTTONS}.yes_button, "Save Changes")
    l_prompt.set_button_action ({ES_DIALOG_BUTTONS}.yes_button, agent save_changes)
    l_prompt.set_button_text ({ES_DIALOG_BUTTONS}.no_button, "Continue Without Save")
    l_prompt.show_on_active_window
  end

Changing button labels


Not only has the text been changed on the dialog prompt but the buttons have also been resized and both resized to match each width, as per the general user interface guidelines related to dialog buttons. This is just one of the new features of the new dialog implementation in 6.1.

In addition the resizing the key bindings have also been altered. On regular question prompts with Yes and No buttons pressing the Y key will select the Yes button and pressing N will select the No button. With the buttons relabeled from their defaults new key bindings are introduced. Save Changes can be select by pressing S and Continue Without Save selected by pressing C.