Author: laplace Date: 2010-11-01 21:19:25 +0100 (Mon, 01 Nov 2010) New Revision: 39260 Changeset: http://dev.haiku-os.org/changeset/39260 Added: haiku/trunk/src/preferences/print/TransportMenu.cpp haiku/trunk/src/preferences/print/TransportMenu.h Modified: haiku/trunk/src/preferences/print/AddPrinterDialog.cpp haiku/trunk/src/preferences/print/Jamfile Log: * Dynamically create contents of transport sub menu. Modified: haiku/trunk/src/preferences/print/AddPrinterDialog.cpp =================================================================== --- haiku/trunk/src/preferences/print/AddPrinterDialog.cpp 2010-11-01 19:48:23 UTC (rev 39259) +++ haiku/trunk/src/preferences/print/AddPrinterDialog.cpp 2010-11-01 20:19:25 UTC (rev 39260) @@ -29,6 +29,7 @@ #include "Globals.h" #include "Messages.h" #include "PrinterListView.h" +#include "TransportMenu.h" #undef B_TRANSLATE_CONTEXT @@ -379,34 +380,12 @@ } // Create submenu - BMenu* transportMenu = new BMenu(transportName.String()); + BMenu* transportMenu = new TransportMenu(transportName.String(), + kTransportSelectedMsg, transport, transportName); menu->AddItem(transportMenu); transportMenu->SetRadioMode(true); menu->ItemAt(menu->IndexOf(transportMenu))-> SetMessage(new BMessage(kTransportSelectedMsg)); - - - if (reply.FindString("port_id", &portId) != B_OK) { - // Show error message in submenu - BMessage* portMsg = new BMessage(kTransportSelectedMsg); - transportMenu->AddItem(new BMenuItem( - B_TRANSLATE("No printer found!"), portMsg)); - continue; - } - - // Add ports to submenu - for (int32 i = 0; reply.FindString("port_id", i, &portId) == B_OK; - i++) { - if (reply.FindString("port_name", i, &portName) != B_OK - || !portName.Length()) - portName = portId; - - // Create menu item in submenu for port - BMessage* portMsg = new BMessage(kTransportSelectedMsg); - portMsg->AddString("name", transportName); - portMsg->AddString("path", portId); - transportMenu->AddItem(new BMenuItem(portName.String(), portMsg)); - } } } Modified: haiku/trunk/src/preferences/print/Jamfile =================================================================== --- haiku/trunk/src/preferences/print/Jamfile 2010-11-01 19:48:23 UTC (rev 39259) +++ haiku/trunk/src/preferences/print/Jamfile 2010-11-01 20:19:25 UTC (rev 39260) @@ -9,6 +9,7 @@ PrinterListView.cpp JobListView.cpp SpoolFolder.cpp + TransportMenu.cpp Globals.cpp : be @@ -27,5 +28,6 @@ JobListView.cpp PrinterListView.cpp PrintersWindow.cpp + TransportMenu.cpp ; Added: haiku/trunk/src/preferences/print/TransportMenu.cpp =================================================================== --- haiku/trunk/src/preferences/print/TransportMenu.cpp (rev 0) +++ haiku/trunk/src/preferences/print/TransportMenu.cpp 2010-11-01 20:19:25 UTC (rev 39260) @@ -0,0 +1,76 @@ +/* + * Copyright 2002-2010, Haiku. + * Distributed under the terms of the MIT License. + * + * Authors: + * Michael Pfeiffer + * Philippe Houdoin + */ +#include "TransportMenu.h" + + +#include <Catalog.h> +#include <MenuItem.h> + + +#undef B_TRANSLATE_CONTEXT +#define B_TRANSLATE_CONTEXT "TransportMenu" + + +TransportMenu::TransportMenu(const char* title, uint32 what, + const BMessenger& messenger, const BString& transportName) + : + BMenu(title), + fWhat(what), + fMessenger(messenger), + fTransportName(transportName) +{ +} + + +bool +TransportMenu::AddDynamicItem(add_state state) +{ + if (state != B_INITIAL_ADD) + return false; + + BMenuItem* item = RemoveItem((int32)0); + while (item != NULL) { + delete item; + item = RemoveItem((int32)0); + } + + BMessage msg; + msg.MakeEmpty(); + msg.what = B_GET_PROPERTY; + msg.AddSpecifier("Ports"); + BMessage reply; + if (fMessenger.SendMessage(&msg, &reply) != B_OK) + return false; + + BString portId; + BString portName; + if (reply.FindString("port_id", &portId) != B_OK) { + // Show error message in submenu + BMessage* portMsg = new BMessage(fWhat); + AddItem(new BMenuItem( + B_TRANSLATE("No printer found!"), portMsg)); + return false; + } + + // Add ports to submenu + for (int32 i = 0; reply.FindString("port_id", i, &portId) == B_OK; + i++) { + if (reply.FindString("port_name", i, &portName) != B_OK + || !portName.Length()) + portName = portId; + + // Create menu item in submenu for port + BMessage* portMsg = new BMessage(fWhat); + portMsg->AddString("name", fTransportName); + portMsg->AddString("path", portId); + AddItem(new BMenuItem(portName.String(), portMsg)); + } + + return false; +} Added: haiku/trunk/src/preferences/print/TransportMenu.h =================================================================== --- haiku/trunk/src/preferences/print/TransportMenu.h (rev 0) +++ haiku/trunk/src/preferences/print/TransportMenu.h 2010-11-01 20:19:25 UTC (rev 39260) @@ -0,0 +1,32 @@ +/* + * Copyright 2010, Haiku. + * Distributed under the terms of the MIT License. + * + * Authors: + * Michael Pfeiffer + */ + +#ifndef _TRANSPORT_MENU_H +#define _TRANSPORT_MENU_H + + +#include <Menu.h> +#include <Messenger.h> +#include <String.h> + + +class TransportMenu : public BMenu +{ +public: + TransportMenu(const char* title, uint32 what, + const BMessenger& messenger, const BString& transportName); + + bool AddDynamicItem(add_state s); + +private: + uint32 fWhat; + BMessenger fMessenger; + BString fTransportName; +}; + +#endif