* The printer driver API does not provide a way to inline printer or transport add-on specific settings in the dialog. Therefore removed detail sections. Closes ticket #1013.

* Made window resizable.
* Code style changes.
* Moved code inside MessageReceived into several methods.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20125 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Pfeiffer
2007-02-12 20:08:17 +00:00
parent d2e9104b66
commit e72d32c89b
3 changed files with 225 additions and 226 deletions
+183 -172
View File
@@ -12,7 +12,6 @@
#include "PrinterListView.h" #include "PrinterListView.h"
#include "pr_server.h" #include "pr_server.h"
#include "Globals.h" #include "Globals.h"
#include "Messages.h"
#include <Box.h> #include <Box.h>
#include <Button.h> #include <Button.h>
@@ -37,7 +36,7 @@ AddPrinterDialog::Start()
AddPrinterDialog::AddPrinterDialog() AddPrinterDialog::AddPrinterDialog()
: Inherited(BRect(78.0, 71.0, 400, 300), "Add Printer", : Inherited(BRect(78.0, 71.0, 400, 300), "Add Printer",
B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL, B_NOT_RESIZABLE | B_NOT_ZOOMABLE) B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL, B_NOT_ZOOMABLE)
{ {
BuildGUI(0); BuildGUI(0);
@@ -49,72 +48,26 @@ void
AddPrinterDialog::MessageReceived(BMessage* msg) AddPrinterDialog::MessageReceived(BMessage* msg)
{ {
switch(msg->what) { switch(msg->what) {
case B_OK: { case B_OK:
BMessage m(PSRV_MAKE_PRINTER); AddPrinter(msg);
BMessenger msgr; PostMessage(B_QUIT_REQUESTED);
if (GetPrinterServerMessenger(msgr) != B_OK) break;
BString transport, transportPath;
if (fPrinterText != "Preview") {
transport = fTransportText;
transportPath = fTransportPathText;
}
m.AddString("driver", fPrinterText.String());
m.AddString("transport", transport.String());
m.AddString("transport path", transportPath.String());
m.AddString("printer name", fNameText.String());
m.AddString("connection", "Local");
// request print_server to create printer
msgr.SendMessage(&m);
PostMessage(B_QUIT_REQUESTED);
}
break; break;
case B_CANCEL: case B_CANCEL:
PostMessage(B_QUIT_REQUESTED); PostMessage(B_QUIT_REQUESTED);
break; break;
case MSG_NAME_CHANGED:
fNameText = fName->Text(); Update(); case kNameChangedMsg:
fNameText = fName->Text();
Update();
break; break;
case MSG_PRINTER_SELECTED:
case MSG_TRANSPORT_SELECTED: case kPrinterSelectedMsg:
{ StorePrinter(msg);
BString name, path; break;
if (msg->FindString("name", &name) != B_OK) {
name = ""; case kTransportSelectedMsg:
}
if (msg->what == MSG_PRINTER_SELECTED) {
fPrinterText = name;
} else {
fTransportText = name;
if (msg->FindString("path", &path) == B_OK) {
// transport path selected
fTransportPathText = path;
void* pointer;
// mark sub menu
if (msg->FindPointer("source", &pointer) == B_OK) {
BMenuItem* item = (BMenuItem*)pointer;
BMenu* menu = item->Menu();
int32 index = fTransport->IndexOf(menu);
item = fTransport->ItemAt(index);
if (item) item->SetMarked(true);
}
} else {
// transport selected
fTransportPathText = "";
// remove mark from item in sub menu of transport sub menu
for (int32 i = fTransport->CountItems()-1; i >= 0; i --) {
BMenu* menu = fTransport->SubmenuAt(i);
if (menu) {
BMenuItem* item = menu->FindMarked();
if (item) item->SetMarked(false);
}
}
}
}
Update();
}
break; break;
@@ -124,137 +77,185 @@ AddPrinterDialog::MessageReceived(BMessage* msg)
} }
void
AddPrinterDialog::AddPrinter(BMessage *msg)
{
BMessage m(PSRV_MAKE_PRINTER);
BMessenger msgr;
if (GetPrinterServerMessenger(msgr) != B_OK)
return;
BString transport;
BString transportPath;
if (fPrinterText != "Preview") {
// Preview printer does not use transport add-on
transport = fTransportText;
transportPath = fTransportPathText;
}
m.AddString("driver", fPrinterText.String());
m.AddString("transport", transport.String());
m.AddString("transport path", transportPath.String());
m.AddString("printer name", fNameText.String());
m.AddString("connection", "Local");
msgr.SendMessage(&m);
// request print_server to create printer
}
void
AddPrinterDialog::StorePrinter(BMessage *msg)
{
BString name;
if (msg->FindString("name", &name) != B_OK)
name = "";
fPrinterText = name;
Update();
}
void
AddPrinterDialog::HandleChangedTransport(BMessage *msg)
{
BString name;
if (msg->FindString("name", &name) != B_OK) {
name = "";
}
fTransportText = name;
BString path;
if (msg->FindString("path", &path) == B_OK) {
// transport path selected
fTransportPathText = path;
// mark sub menu
void* pointer;
if (msg->FindPointer("source", &pointer) == B_OK) {
BMenuItem* item = (BMenuItem*)pointer;
BMenu* menu = item->Menu();
int32 index = fTransport->IndexOf(menu);
item = fTransport->ItemAt(index);
if (item != NULL)
item->SetMarked(true);
}
} else {
// transport selected
fTransportPathText = "";
// remove mark from item in sub menu of transport sub menu
for (int32 i = fTransport->CountItems()-1; i >= 0; i --) {
BMenu* menu = fTransport->SubmenuAt(i);
if (menu != NULL) {
BMenuItem* item = menu->FindMarked();
if (item != NULL)
item->SetMarked(false);
}
}
}
Update();
}
void void
AddPrinterDialog::BuildGUI(int stage) AddPrinterDialog::BuildGUI(int stage)
{ {
BRect r, tr; float w, h;
float x, w, h; // preferred size of current control
BButton * ok;
BButton * cancel;
BTextControl * tc;
BPopUpMenu * pum;
BMenuField * mf;
BStringView * sv;
BBox * bb;
#define H_MARGIN 8 const int32 kHMargin = 8;
#define V_MARGIN 8 const int32 kVMargin = 8;
#define LINE_MARGIN 3
#define NAME_LABEL "Printer Name:" #define NAME_LABEL "Printer Name:"
#define KIND_LABEL "Printer Type:" #define KIND_LABEL "Printer Type:"
#define PORT_LABEL "Connected to:" #define PORT_LABEL "Connected to:"
#define DRIVER_SETTINGS_AREA_TEXT "Driver settings area, if any."
#define TRANSPORT_SETTINGS_AREA_TEXT "Transport settings area, if any."
// ------------------------ First of all, create a nice grey backdrop
BBox * panel = new BBox(Bounds(), "backdrop", B_FOLLOW_ALL, BRect r = Bounds();
B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP,
B_PLAIN_BORDER); BView *panel = new BView(r, "panel", B_FOLLOW_ALL, 0);
AddChild(panel); AddChild(panel);
panel->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
r = panel->Bounds();
r.InsetBy(H_MARGIN, V_MARGIN); r.InsetBy(kHMargin, kVMargin);
// add a "printer name" input field // add a "printer name" input field
tc = new BTextControl(r, "printer_name", fName = new BTextControl(r, "printer_name",
NAME_LABEL, B_EMPTY_STRING, NULL); NAME_LABEL, B_EMPTY_STRING, NULL,
fName = tc; B_FOLLOW_LEFT_RIGHT);
tc->SetAlignment(B_ALIGN_LEFT, B_ALIGN_LEFT); fName->SetFont(be_bold_font);
panel->AddChild(tc); fName->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT);
tc->SetModificationMessage(new BMessage(MSG_NAME_CHANGED)); panel->AddChild(fName);
tc->SetFont(be_bold_font); fName->SetModificationMessage(new BMessage(kNameChangedMsg));
tc->GetPreferredSize(&w, &h); fName->GetPreferredSize(&w, &h);
tc->SetDivider(be_bold_font->StringWidth(NAME_LABEL "#")); fName->ResizeTo(r.Width(), h);
tc->ResizeTo(tc->Bounds().Width(), h);
r.OffsetBy(0, h + 2*V_MARGIN); r.OffsetBy(0, h + 2 * kVMargin);
// add a "driver" settings box // add a "driver" popup menu field
bb = new BBox(r, "driver_box", B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT, fPrinter = new BPopUpMenu("<pick one>");
B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP, BMenuField *printerMenuField = new BMenuField(r, "drivers_list", KIND_LABEL, fPrinter,
B_FANCY_BORDER);
panel->AddChild(bb);
pum = new BPopUpMenu("<pick one>");
fPrinter = pum;
mf = new BMenuField(r, "drivers_list", KIND_LABEL, pum,
B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT, B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT,
B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE); B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE);
mf->SetFont(be_plain_font); printerMenuField->SetFont(be_plain_font);
mf->SetDivider(be_plain_font->StringWidth(NAME_LABEL "#")); printerMenuField->SetAlignment(B_ALIGN_RIGHT);
bb->SetLabel(mf); panel->AddChild(printerMenuField);
mf->ResizeToPreferred(); printerMenuField->GetPreferredSize(&w, &h);
mf->GetPreferredSize(&w, &h); printerMenuField->ResizeTo(r.Width(), h);
FillMenu(pum, "Print", MSG_PRINTER_SELECTED); FillMenu(fPrinter, "Print", kPrinterSelectedMsg);
tr = bb->Bounds(); r.OffsetBy(0, printerMenuField->Bounds().Height() + kVMargin);
tr.top += h;
tr.InsetBy(H_MARGIN, V_MARGIN);
sv = new BStringView(tr, NULL, DRIVER_SETTINGS_AREA_TEXT);
bb->AddChild(sv);
sv->ResizeToPreferred();
bb->ResizeTo(bb->Bounds().Width(), 200);
r.OffsetBy(0, bb->Frame().Height() + V_MARGIN);
// add a "transport" settings box
bb = new BBox(r, "driver_box", B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT,
B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP,
B_FANCY_BORDER);
panel->AddChild(bb);
// add a "connected to" (aka transports list) menu field // add a "connected to" (aka transports list) menu field
pum = new BPopUpMenu("<pick one>"); fTransport = new BPopUpMenu("<pick one>");
fTransport = pum; BMenuField *transportMenuField = new BMenuField(r, "transports_list", PORT_LABEL, fTransport,
mf = new BMenuField(r, "transports_list", PORT_LABEL, pum,
B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT, B_FOLLOW_TOP | B_FOLLOW_LEFT_RIGHT,
B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE); B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE);
mf->SetFont(be_plain_font); transportMenuField->SetFont(be_plain_font);
mf->SetDivider(be_plain_font->StringWidth(PORT_LABEL "#")); transportMenuField->SetAlignment(B_ALIGN_RIGHT);
bb->SetLabel(mf); panel->AddChild(transportMenuField);
mf->ResizeToPreferred(); transportMenuField->GetPreferredSize(&w, &h);
mf->GetPreferredSize(&w, &h); transportMenuField->ResizeTo(r.Width(), h);
FillMenu(pum, "Print/transport", MSG_TRANSPORT_SELECTED); FillMenu(fTransport, "Print/transport", kTransportSelectedMsg);
r.OffsetBy(0, transportMenuField->Bounds().Height() + kVMargin);
// update dividers
float divider = be_bold_font->StringWidth(NAME_LABEL "#");
divider = max_c(divider, be_plain_font->StringWidth(NAME_LABEL "#"));
divider = max_c(divider, be_plain_font->StringWidth(PORT_LABEL "#"));
tr = bb->Bounds(); fName->SetDivider(divider);
tr.top += h; printerMenuField->SetDivider(divider);
tr.InsetBy(H_MARGIN, V_MARGIN); transportMenuField->SetDivider(divider);
sv = new BStringView(tr, NULL, TRANSPORT_SETTINGS_AREA_TEXT);
bb->AddChild(sv);
sv->ResizeToPreferred();
bb->ResizeTo(bb->Bounds().Width(), 100);
r.OffsetBy(0, bb->Frame().Height() + V_MARGIN);
// make some space before the buttons row
r.OffsetBy(0, V_MARGIN);
// add a "OK" button, and make it default // add a "OK" button, and make it default
ok = new BButton(r, NULL, "Add", new BMessage(B_OK), B_FOLLOW_RIGHT | B_FOLLOW_TOP); fOk = new BButton(r, NULL, "Add", new BMessage(B_OK), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
fOk = ok; fOk->MakeDefault(true);
ok->MakeDefault(true); fOk->ResizeToPreferred();
ok->ResizeToPreferred(); fOk->GetPreferredSize(&w, &h);
ok->GetPreferredSize(&w, &h); // put the ok bottom at bottom right corner
x = r.right - w; float x = panel->Bounds().right - w - kHMargin;
ok->MoveTo(x, ok->Frame().top); // put the ok bottom at bottom right corner float y = panel->Bounds().bottom - h - kVMargin;
panel->AddChild(ok); fOk->MoveTo(x, y);
panel->AddChild(fOk);
SetDefaultButton(fOk); SetDefaultButton(fOk);
// add a "Cancel button // add a "Cancel button
cancel = new BButton(r, NULL, "Cancel", new BMessage(B_CANCEL), B_FOLLOW_RIGHT | B_FOLLOW_TOP); BButton *cancel = new BButton(r, NULL, "Cancel", new BMessage(B_CANCEL), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
cancel->ResizeToPreferred(); cancel->ResizeToPreferred();
cancel->GetPreferredSize(&w, &h); cancel->GetPreferredSize(&w, &h);
cancel->MoveTo(x - w - H_MARGIN, r.top); // put cancel button left next the ok button // put cancel button left next the ok button
x = fOk->Frame().left - w - kVMargin;
y = fOk->Frame().top;
cancel->MoveTo(x, y);
panel->AddChild(cancel); panel->AddChild(cancel);
// Auto resize window // Auto resize window
ResizeTo(ok->Frame().right + H_MARGIN, ok->Frame().bottom + V_MARGIN); r.bottom = transportMenuField->Frame().bottom + fOk->Bounds().Height() + 2 * kVMargin;
ResizeTo(r.right, r.bottom);
SetSizeLimits(r.right, 10e5, r.bottom, 10e5);
fName->MakeFocus(true); fName->MakeFocus(true);
@@ -315,8 +316,10 @@ AddPrinterDialog::BuildGUI(int stage)
static directory_which gAddonDirs[] = { static directory_which gAddonDirs[] = {
B_BEOS_ADDONS_DIRECTORY, B_BEOS_ADDONS_DIRECTORY,
B_COMMON_ADDONS_DIRECTORY B_COMMON_ADDONS_DIRECTORY
// B_USER_ADDONS_DIRECTORY same as common directory #if HAIKU_COMPATIBLE
// TODO: not in Haiku! // TODO test
// , B_USER_ADDONS_DIRECTORY
#endif
}; };
@@ -325,10 +328,16 @@ AddPrinterDialog::FillMenu(BMenu* menu, const char* path, uint32 what)
{ {
for (uint32 i = 0; i < sizeof(gAddonDirs) / sizeof(directory_which); i ++) { for (uint32 i = 0; i < sizeof(gAddonDirs) / sizeof(directory_which); i ++) {
BPath addonPath; BPath addonPath;
if (find_directory(gAddonDirs[i], &addonPath) != B_OK) continue; if (find_directory(gAddonDirs[i], &addonPath) != B_OK)
if (addonPath.Append(path) != B_OK) continue; continue;
if (addonPath.Append(path) != B_OK)
continue;
BDirectory dir(addonPath.Path()); BDirectory dir(addonPath.Path());
if (dir.InitCheck() != B_OK) continue; if (dir.InitCheck() != B_OK)
continue;
BEntry entry; BEntry entry;
while (dir.GetNextEntry(&entry, true) == B_OK) { while (dir.GetNextEntry(&entry, true) == B_OK) {
if (!entry.IsFile()) if (!entry.IsFile())
@@ -403,11 +412,12 @@ AddPrinterDialog::AddPortSubMenu(BMenu* menu, const char* transport, const char*
subMenu->SetRadioMode(true); subMenu->SetRadioMode(true);
int32 index = menu->IndexOf(subMenu); int32 index = menu->IndexOf(subMenu);
BMenuItem* item = menu->ItemAt(index); BMenuItem* item = menu->ItemAt(index);
if (item) item->SetMessage(new BMessage(MSG_TRANSPORT_SELECTED)); if (item != NULL)
item->SetMessage(new BMessage(kTransportSelectedMsg));
} }
// setup menu item for port // setup menu item for port
BMessage* msg = new BMessage(MSG_TRANSPORT_SELECTED); BMessage* msg = new BMessage(kTransportSelectedMsg);
msg->AddString("name", transport); msg->AddString("name", transport);
msg->AddString("path", path.Leaf()); msg->AddString("path", path.Leaf());
BMenuItem* item = new BMenuItem(path.Leaf(), msg); BMenuItem* item = new BMenuItem(path.Leaf(), msg);
@@ -421,6 +431,7 @@ AddPrinterDialog::Update()
{ {
fOk->SetEnabled(fNameText != "" && fPrinterText != "" && fOk->SetEnabled(fNameText != "" && fPrinterText != "" &&
(fTransportText != "" || fPrinterText == "Preview")); (fTransportText != "" || fPrinterText == "Preview"));
fTransport->SetEnabled(fPrinterText != "" &&fPrinterText != "Preview");
fTransport->SetEnabled(fPrinterText != "" && fPrinterText != "Preview");
} }
+42 -51
View File
@@ -1,34 +1,14 @@
/*****************************************************************************/ /*
// Printers Preference Application. * Copyright 2001-2007, Haiku.
// * Distributed under the terms of the MIT License.
// This application and all source files used in its construction, except *
// where noted, are licensed under the MIT License, and have been written * Authors:
// and are: * Philippe Houdoin
// * Michael Pfeiffer
// Copyright (c) 2001-2003 OpenBeOS Project */
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*****************************************************************************/
#ifndef ADD_PRINTER_DIALOG_H
#ifndef ADDPRINTERDIALOG_H #define ADD_PRINTER_DIALOG_H
#define ADDPRINTERDIALOG_H
class AddPrinterDialog; class AddPrinterDialog;
@@ -40,28 +20,39 @@ class AddPrinterDialog;
class AddPrinterDialog : public BWindow class AddPrinterDialog : public BWindow
{ {
typedef BWindow Inherited; typedef BWindow Inherited;
public:
static status_t Start();
private:
AddPrinterDialog();
void MessageReceived(BMessage* msg);
void BuildGUI(int stage);
void FillMenu(BMenu* menu, const char* path, uint32 what);
void AddPortSubMenu(BMenu* menu, const char* transport, const char* port);
void Update();
BTextControl* fName;
BPopUpMenu* fPrinter;
BPopUpMenu* fTransport;
BButton* fOk;
BString fNameText; public:
BString fPrinterText; static status_t Start();
BString fTransportText;
BString fTransportPathText; private:
enum MessageKind {
kPrinterSelectedMsg = 'adlg',
kTransportSelectedMsg,
kNameChangedMsg,
};
AddPrinterDialog();
void MessageReceived(BMessage *msg);
void AddPrinter(BMessage *msg);
void StorePrinter(BMessage *msg);
void HandleChangedTransport(BMessage *msg);
void BuildGUI(int stage);
void FillMenu(BMenu *menu, const char *path, uint32 what);
void AddPortSubMenu(BMenu *menu, const char *transport, const char *port);
void Update();
BTextControl *fName;
BPopUpMenu *fPrinter;
BPopUpMenu *fTransport;
BButton *fOk;
BString fNameText;
BString fPrinterText;
BString fTransportText;
BString fTransportPathText;
}; };
#endif #endif
-3
View File
@@ -39,7 +39,4 @@ const uint32 MSG_CANCEL_JOB = 'CncJ';
const uint32 MSG_RESTART_JOB = 'RstJ'; const uint32 MSG_RESTART_JOB = 'RstJ';
const uint32 MSG_JOB_SELECTED = 'JSel'; const uint32 MSG_JOB_SELECTED = 'JSel';
const uint32 MSG_TRANSPORT_SELECTED = 'TSel';
const uint32 MSG_NAME_CHANGED = 'NCgd';
#endif #endif