| 1 |
/* Change Display |
| 2 |
* |
| 3 |
* Demonstrates migrating a window between different displays. |
| 4 |
* A display is a mouse and keyboard with some number of |
| 5 |
* associated monitors. The neat thing about having multiple |
| 6 |
* displays is that they can be on completely separate |
| 7 |
* computers, as long as there is a network connection to the |
| 8 |
* computer where the application is running. |
| 9 |
* |
| 10 |
* Only some of the windowing systems where GTK+ runs have the |
| 11 |
* concept of multiple displays. (The X Window System is the |
| 12 |
* main example.) Other windowing systems can only handle one |
| 13 |
* keyboard and mouse, and combine all monitors into |
| 14 |
* a single display. |
| 15 |
* |
| 16 |
* This is a moderately complex example, and demonstrates: |
| 17 |
* |
| 18 |
* - Tracking the currently open displays |
| 19 |
* |
| 20 |
* - Changing the display for a window |
| 21 |
* |
| 22 |
* - Letting the user choose a window by clicking on it |
| 23 |
* |
| 24 |
* - Using Gtk::ListStore and Gtk::TreeView |
| 25 |
* |
| 26 |
* - Using Gtk::Dialog |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <gtkmm.h> |
| 30 |
|
| 31 |
|
| 32 |
class Popup : public Gtk::Window |
| 33 |
{ |
| 34 |
public: |
| 35 |
Popup(const Glib::RefPtr<Gdk::Screen>& screen, const Glib::ustring& prompt); |
| 36 |
~Popup() override; |
| 37 |
|
| 38 |
protected: |
| 39 |
Gtk::Frame m_Frame; |
| 40 |
Gtk::Label m_Label; |
| 41 |
}; |
| 42 |
|
| 43 |
class Example_ChangeDisplay : public Gtk::Dialog |
| 44 |
{ |
| 45 |
public: |
| 46 |
Example_ChangeDisplay(); |
| 47 |
~Example_ChangeDisplay() override; |
| 48 |
|
| 49 |
protected: |
| 50 |
virtual void setup_frame(Gtk::Frame& frame, Gtk::TreeView& treeview, Gtk::Box& buttonbox); |
| 51 |
|
| 52 |
virtual void initialize_displays(); |
| 53 |
virtual void query_change_display(); |
| 54 |
virtual Gtk::Widget* find_toplevel_at_pointer(const Glib::RefPtr<Gdk::Display>& display); |
| 55 |
virtual Gtk::Window* query_for_toplevel(const Glib::RefPtr<Gdk::Screen>& screen, const Glib::ustring& prompt); |
| 56 |
|
| 57 |
//signal handlers: |
| 58 |
virtual void on_button_display_open(); |
| 59 |
virtual void on_button_display_close(); |
| 60 |
|
| 61 |
virtual void on_treeview_display_selection_changed(); |
| 62 |
|
| 63 |
virtual void on_display_closed(bool is_error, Glib::RefPtr<Gdk::Display> display); |
| 64 |
|
| 65 |
virtual bool on_popup_button_release_event(GdkEventButton* event); |
| 66 |
|
| 67 |
void on_response(int response_id) override; |
| 68 |
|
| 69 |
|
| 70 |
class ModelColumns_Display : public Gtk::TreeModelColumnRecord |
| 71 |
{ |
| 72 |
public: |
| 73 |
Gtk::TreeModelColumn<Glib::ustring> m_name; |
| 74 |
Gtk::TreeModelColumn< Glib::RefPtr<Gdk::Display> > m_display; //hidden |
| 75 |
|
| 76 |
ModelColumns_Display() { add(m_name); add(m_display); } |
| 77 |
}; |
| 78 |
ModelColumns_Display m_columns_display; |
| 79 |
|
| 80 |
Gtk::Box m_VBox; |
| 81 |
Gtk::Frame m_Frame_Display; |
| 82 |
Gtk::TreeView m_TreeView_Display; |
| 83 |
Glib::RefPtr<Gtk::ListStore> m_refListStore_Display; |
| 84 |
Gtk::Box m_ButtonBox_Display; |
| 85 |
|
| 86 |
Gtk::Button m_Button_Display_Open, m_Button_Display_Close; |
| 87 |
|
| 88 |
Glib::RefPtr<Gtk::SizeGroup> m_refSizeGroup_Display; |
| 89 |
|
| 90 |
|
| 91 |
Glib::RefPtr<Gdk::Display> m_refCurrentDisplay; |
| 92 |
|
| 93 |
bool m_popup_clicked; |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
Example_ChangeDisplay::Example_ChangeDisplay() |
| 100 |
: Gtk::Dialog("Change Display"), |
| 101 |
m_VBox(Gtk::ORIENTATION_VERTICAL, 5), |
| 102 |
m_Frame_Display("Display"), |
| 103 |
m_ButtonBox_Display(Gtk::ORIENTATION_VERTICAL, 5), |
| 104 |
m_Button_Display_Open("_Open...", true), m_Button_Display_Close("_Close...", true), |
| 105 |
m_popup_clicked(false) |
| 106 |
{ |
| 107 |
add_button("_Close", Gtk::RESPONSE_CLOSE); |
| 108 |
add_button("Change", Gtk::RESPONSE_OK); |
| 109 |
|
| 110 |
set_default_size(300, 400); |
| 111 |
|
| 112 |
m_VBox.set_border_width(8); |
| 113 |
get_content_area()->pack_start(m_VBox); |
| 114 |
|
| 115 |
|
| 116 |
//Display: |
| 117 |
{ |
| 118 |
setup_frame(m_Frame_Display, m_TreeView_Display, m_ButtonBox_Display); |
| 119 |
m_ButtonBox_Display.pack_start(m_Button_Display_Open, Gtk::PACK_SHRINK); |
| 120 |
m_Button_Display_Open.signal_clicked().connect( sigc::mem_fun(*this, &Example_ChangeDisplay::on_button_display_open) ); |
| 121 |
m_ButtonBox_Display.pack_start(m_Button_Display_Close, Gtk::PACK_SHRINK); |
| 122 |
m_Button_Display_Close.signal_clicked().connect( sigc::mem_fun(*this, &Example_ChangeDisplay::on_button_display_close) ); |
| 123 |
|
| 124 |
//Setup TreeView: |
| 125 |
m_refListStore_Display = Gtk::ListStore::create(m_columns_display); |
| 126 |
m_TreeView_Display.set_model(m_refListStore_Display); |
| 127 |
m_TreeView_Display.append_column("Name", m_columns_display.m_name); |
| 128 |
|
| 129 |
//Connect signal: |
| 130 |
Glib::RefPtr<Gtk::TreeView::Selection> refSelection = m_TreeView_Display.get_selection(); |
| 131 |
refSelection->signal_changed().connect( sigc::mem_fun(*this, &Example_ChangeDisplay::on_treeview_display_selection_changed) ); |
| 132 |
|
| 133 |
m_VBox.pack_start(m_Frame_Display); |
| 134 |
|
| 135 |
m_refSizeGroup_Display = Gtk::SizeGroup::create(Gtk::SIZE_GROUP_HORIZONTAL); |
| 136 |
m_refSizeGroup_Display->add_widget(m_ButtonBox_Display); |
| 137 |
} |
| 138 |
|
| 139 |
initialize_displays(); |
| 140 |
|
| 141 |
show_all(); |
| 142 |
} |
| 143 |
|
| 144 |
Example_ChangeDisplay::~Example_ChangeDisplay() |
| 145 |
{ |
| 146 |
} |
| 147 |
|
| 148 |
void Example_ChangeDisplay::setup_frame(Gtk::Frame& frame, Gtk::TreeView& treeview, Gtk::Box& buttonbox) |
| 149 |
{ |
| 150 |
Gtk::Box* pHBox = Gtk::make_managed<Gtk::Box>(Gtk::ORIENTATION_HORIZONTAL, 8); |
| 151 |
pHBox->set_border_width(8); |
| 152 |
frame.add(*pHBox); |
| 153 |
|
| 154 |
Gtk::ScrolledWindow* pScrolledWindow = Gtk::make_managed<Gtk::ScrolledWindow>(); |
| 155 |
pScrolledWindow->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC); |
| 156 |
pScrolledWindow->set_shadow_type(Gtk::SHADOW_IN); |
| 157 |
pHBox->pack_start(*pScrolledWindow); |
| 158 |
|
| 159 |
treeview.set_headers_visible(false); |
| 160 |
pScrolledWindow->add(treeview); |
| 161 |
|
| 162 |
Glib::RefPtr<Gtk::TreeView::Selection> refSelection = treeview.get_selection(); |
| 163 |
refSelection->set_mode(Gtk::SELECTION_BROWSE); |
| 164 |
|
| 165 |
pHBox->pack_start(buttonbox, Gtk::PACK_SHRINK); |
| 166 |
} |
| 167 |
|
| 168 |
void Example_ChangeDisplay::initialize_displays() |
| 169 |
{ |
| 170 |
|
| 171 |
#ifndef G_OS_WIN32 |
| 172 |
Glib::RefPtr<Gdk::DisplayManager> refDisplayManager = Gdk::DisplayManager::get(); |
| 173 |
|
| 174 |
for(auto refDisplay : refDisplayManager->list_displays()) |
| 175 |
{ |
| 176 |
Gtk::TreeRow row = *(m_refListStore_Display->append()); |
| 177 |
row[m_columns_display.m_name] = refDisplay->get_name();; |
| 178 |
row[m_columns_display.m_display] = refDisplay; |
| 179 |
|
| 180 |
refDisplay->signal_closed().connect( |
| 181 |
sigc::bind<-1>( sigc::mem_fun(*this, &Example_ChangeDisplay::on_display_closed), refDisplay) ); |
| 182 |
} |
| 183 |
|
| 184 |
#endif |
| 185 |
} |
| 186 |
|
| 187 |
void Example_ChangeDisplay::on_display_closed(bool /* is_error */, Glib::RefPtr<Gdk::Display> display) |
| 188 |
{ |
| 189 |
for(auto row : m_refListStore_Display->children()) |
| 190 |
{ |
| 191 |
Glib::RefPtr<Gdk::Display> refDisplay = row[m_columns_display.m_display]; |
| 192 |
if(refDisplay == display) |
| 193 |
{ |
| 194 |
m_refListStore_Display->erase(row); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
void Example_ChangeDisplay::on_button_display_open() |
| 200 |
{ |
| 201 |
Gtk::Dialog dialog("Open Display", true); |
| 202 |
dialog.add_button("_Cancel", Gtk::RESPONSE_CANCEL); |
| 203 |
dialog.add_button("_OK", Gtk::RESPONSE_OK); |
| 204 |
|
| 205 |
dialog.set_default_response(Gtk::RESPONSE_OK); |
| 206 |
|
| 207 |
Gtk::Entry entry; |
| 208 |
entry.set_activates_default(); |
| 209 |
|
| 210 |
Gtk::Label label("Please enter the name of\nthe new display\n"); |
| 211 |
|
| 212 |
dialog.get_content_area()->add(label); |
| 213 |
dialog.get_content_area()->add(entry); |
| 214 |
|
| 215 |
entry.grab_focus(); |
| 216 |
|
| 217 |
dialog.show_all_children(); |
| 218 |
|
| 219 |
Glib::RefPtr<Gdk::Display> refResult; |
| 220 |
while (!refResult) |
| 221 |
{ |
| 222 |
gint response_id = dialog.run(); |
| 223 |
if (response_id != Gtk::RESPONSE_OK) |
| 224 |
break; |
| 225 |
|
| 226 |
Glib::ustring new_screen_name = entry.get_text(); |
| 227 |
|
| 228 |
if( !new_screen_name.empty() ) |
| 229 |
{ |
| 230 |
refResult = Gdk::Display::open(new_screen_name); |
| 231 |
if (!refResult) |
| 232 |
{ |
| 233 |
label.set_text("Can't open display.\nplease try another one\n"); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
void Example_ChangeDisplay::on_button_display_close() |
| 240 |
{ |
| 241 |
if(m_refCurrentDisplay) |
| 242 |
m_refCurrentDisplay->close(); |
| 243 |
} |
| 244 |
|
| 245 |
void Example_ChangeDisplay::on_treeview_display_selection_changed() |
| 246 |
{ |
| 247 |
Glib::RefPtr<Gtk::TreeSelection> refSelection = m_TreeView_Display.get_selection(); |
| 248 |
Gtk::TreeModel::iterator iter = refSelection->get_selected(); |
| 249 |
if(iter) |
| 250 |
m_refCurrentDisplay = (*iter)[m_columns_display.m_display]; |
| 251 |
else |
| 252 |
m_refCurrentDisplay.reset(); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
/* Prompts the user for a toplevel window to move, and then moves |
| 257 |
* that window to the currently selected display |
| 258 |
*/ |
| 259 |
void Example_ChangeDisplay::query_change_display() |
| 260 |
{ |
| 261 |
Glib::RefPtr<Gdk::Screen> refScreen = get_screen(); |
| 262 |
Gtk::Window* pTopLevel = query_for_toplevel(refScreen, |
| 263 |
"Please select the toplevel\n" |
| 264 |
"to move to the new screen"); |
| 265 |
|
| 266 |
if (pTopLevel) |
| 267 |
pTopLevel->set_screen( m_refCurrentDisplay->get_default_screen() ); |
| 268 |
else |
| 269 |
refScreen->get_display()->beep(); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
void Example_ChangeDisplay::on_response(int response_id) |
| 274 |
{ |
| 275 |
if (response_id == Gtk::RESPONSE_OK) |
| 276 |
query_change_display(); |
| 277 |
else |
| 278 |
hide(); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
/* Asks the user to click on a window, then waits for them click |
| 283 |
* the mouse. When the mouse is released, returns the toplevel |
| 284 |
* window under the pointer, or NULL, if there is none. |
| 285 |
*/ |
| 286 |
Gtk::Window* Example_ChangeDisplay::query_for_toplevel(const Glib::RefPtr<Gdk::Screen>& screen, const Glib::ustring& prompt) |
| 287 |
{ |
| 288 |
auto device = Glib::wrap(gtk_get_current_event_device(), true); |
| 289 |
if (!device) |
| 290 |
return nullptr; |
| 291 |
|
| 292 |
Glib::RefPtr<Gdk::Display> refDisplay = screen->get_display(); |
| 293 |
|
| 294 |
std::unique_ptr<Popup> pPopup(new Popup(screen, prompt)); |
| 295 |
pPopup->show(); |
| 296 |
|
| 297 |
Glib::RefPtr<Gdk::Cursor> cursor = Gdk::Cursor::create(refDisplay, Gdk::CROSSHAIR); |
| 298 |
|
| 299 |
Gtk::Window* toplevel = nullptr; |
| 300 |
auto seat = device->get_seat(); |
| 301 |
|
| 302 |
if (seat && seat->grab(pPopup->get_window(), Gdk::SEAT_CAPABILITY_ALL_POINTING, |
| 303 |
false, cursor) == Gdk::GRAB_SUCCESS) |
| 304 |
{ |
| 305 |
m_popup_clicked = false; |
| 306 |
pPopup->signal_button_release_event().connect( |
| 307 |
sigc::mem_fun(*this, &Example_ChangeDisplay::on_popup_button_release_event)); |
| 308 |
|
| 309 |
// Process events until clicked is set by button_release_event_cb. |
| 310 |
// We pass in may_block=true since we want to wait if there |
| 311 |
// are no events currently. |
| 312 |
while (!m_popup_clicked) |
| 313 |
Gtk::Main::iteration(true); |
| 314 |
|
| 315 |
toplevel = dynamic_cast<Gtk::Window*>(find_toplevel_at_pointer(refDisplay)); |
| 316 |
if (toplevel == pPopup.get()) |
| 317 |
toplevel = nullptr; |
| 318 |
} |
| 319 |
|
| 320 |
refDisplay->flush(); /* Really release the grab */ |
| 321 |
|
| 322 |
return toplevel; |
| 323 |
} |
| 324 |
|
| 325 |
// Finds the toplevel window under the mouse pointer, if any. |
| 326 |
Gtk::Widget* Example_ChangeDisplay::find_toplevel_at_pointer(const Glib::RefPtr<Gdk::Display>& /* display */) |
| 327 |
{ |
| 328 |
auto device = Glib::wrap(gtk_get_current_event_device(), true); |
| 329 |
if (!device) |
| 330 |
return nullptr; |
| 331 |
|
| 332 |
auto refPointerWindow = device->get_window_at_position(); |
| 333 |
if (refPointerWindow) |
| 334 |
{ |
| 335 |
// The user data field of a GdkWindow is used to store a pointer |
| 336 |
// to the widget that created it. |
| 337 |
gpointer user_data = nullptr; |
| 338 |
refPointerWindow->get_user_data(&user_data); |
| 339 |
GtkWidget* cWidget = static_cast<GtkWidget*>(user_data); |
| 340 |
|
| 341 |
Gtk::Widget* pWidget = Glib::wrap(cWidget); |
| 342 |
if(pWidget) |
| 343 |
return pWidget->get_toplevel(); |
| 344 |
} |
| 345 |
|
| 346 |
return nullptr; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
bool Example_ChangeDisplay::on_popup_button_release_event(GdkEventButton* /* event */) |
| 351 |
{ |
| 352 |
m_popup_clicked = true; |
| 353 |
return true; |
| 354 |
} |
| 355 |
|
| 356 |
Popup::Popup(const Glib::RefPtr<Gdk::Screen>& screen, const Glib::ustring& prompt) |
| 357 |
: Gtk::Window(Gtk::WINDOW_POPUP), |
| 358 |
m_Label(prompt) |
| 359 |
{ |
| 360 |
set_screen(screen); |
| 361 |
set_modal(true); |
| 362 |
set_position(Gtk::WIN_POS_CENTER); |
| 363 |
|
| 364 |
m_Frame.set_shadow_type(Gtk::SHADOW_OUT); |
| 365 |
add(m_Frame); |
| 366 |
|
| 367 |
m_Label.property_margin() = 10; |
| 368 |
m_Frame.add(m_Label); |
| 369 |
|
| 370 |
show_all_children(); |
| 371 |
} |
| 372 |
|
| 373 |
Popup::~Popup() |
| 374 |
{ |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
// called by DemoWindow |
| 379 |
Gtk::Window* do_change_display() |
| 380 |
{ |
| 381 |
return new Example_ChangeDisplay(); |
| 382 |
} |