00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "xrb_dialog.hpp"
00012
00013 #include "xrb_button.hpp"
00014 #include "xrb_input_events.hpp"
00015 #include "xrb_layout.hpp"
00016 #include "xrb_spacerwidget.hpp"
00017
00018 namespace Xrb
00019 {
00020
00021 Dialog::Dialog (
00022 DialogType const dialog_type,
00023 ContainerWidget *const parent,
00024 std::string const &name)
00025 :
00026 ModalWidget(parent, name),
00027 m_sender_dialog_returned(this),
00028 m_sender_dialog_returned_ok(this),
00029 m_sender_dialog_returned_cancel(this),
00030 m_internal_receiver_ok_button_activated(&Dialog::OKButtonActivated, this),
00031 m_internal_receiver_cancel_button_activated(&Dialog::CancelButtonActivated, this)
00032 {
00033 ASSERT1(parent != NULL && "The top-level widget must be a Screen, not a Dialog");
00034
00035 Dialog::UpdateRenderBackground();
00036
00037
00038
00039 m_dialog_layout = new Layout(VERTICAL, this, "dialog layout");
00040 m_dialog_layout->SetIsUsingZeroedFrameMargins(false);
00041
00042
00043 m_button_layout = new Layout(HORIZONTAL, m_dialog_layout, "button layout");
00044 m_button_layout->SetIsUsingZeroedFrameMargins(false);
00045
00046 m_button_layout->SetStackPriority(SP_STAY_ON_TOP);
00047
00048
00049 Uint32 added_button_count = 0;
00050 m_dialog_type = dialog_type;
00051 m_ok_button = 0;
00052 m_cancel_button = 0;
00053 if (HasOKButton())
00054 {
00055 new SpacerWidget(m_button_layout);
00056 m_ok_button = new Button("OK", m_button_layout, "OK button");
00057 m_ok_button->SetIsHeightFixedToTextHeight(true);
00058 ++added_button_count;
00059
00060 SignalHandler::Connect0(
00061 m_ok_button->SenderReleased(),
00062 &m_internal_receiver_ok_button_activated);
00063 }
00064 if (HasCancelButton())
00065 {
00066 new SpacerWidget(m_button_layout);
00067 m_cancel_button = new Button("Cancel", m_button_layout, "Cancel button");
00068 m_cancel_button->SetIsHeightFixedToTextHeight(true);
00069 ++added_button_count;
00070
00071 SignalHandler::Connect0(
00072 m_cancel_button->SenderReleased(),
00073 &m_internal_receiver_cancel_button_activated);
00074 }
00075 new SpacerWidget(m_button_layout);
00076 ASSERT1(added_button_count > 0 && "No buttons were added to the Dialog");
00077
00078 SetMainWidget(m_dialog_layout);
00079 }
00080
00081 bool Dialog::HasButton (ButtonID const button_id) const
00082 {
00083 switch (button_id) {
00084 case ID_OK:
00085 return HasOKButton();
00086
00087 case ID_CANCEL:
00088 return HasCancelButton();
00089
00090 default:
00091 ASSERT1(false && "Invalid button ID");
00092 return false;
00093 }
00094 }
00095
00096 bool Dialog::ProcessKeyEvent (EventKey const *const e)
00097 {
00098 if (e->IsKeyDownEvent())
00099 {
00100 switch (e->KeyCode())
00101 {
00102 case Key::RETURN:
00103 if (HasOKButton() && e->IsEitherControlKeyPressed())
00104 {
00105 OKButtonActivated();
00106 return true;
00107 }
00108 else
00109 return false;
00110
00111 case Key::ESCAPE:
00112 if (HasCancelButton())
00113 CancelButtonActivated();
00114 return true;
00115
00116 default:
00117 return false;
00118 }
00119 }
00120
00121 return false;
00122 }
00123
00124 void Dialog::OKButtonActivated ()
00125 {
00126 Shutdown();
00127 m_sender_dialog_returned.Signal(ID_OK);
00128 m_sender_dialog_returned_ok.Signal();
00129 }
00130
00131 void Dialog::CancelButtonActivated ()
00132 {
00133 Shutdown();
00134 m_sender_dialog_returned.Signal(ID_CANCEL);
00135 m_sender_dialog_returned_cancel.Signal();
00136 }
00137
00138 }