1 // Sun, 18 Jun 2000 2 // Y.Takagi 3 4 #include <Button.h> 5 #include <Rect.h> 6 #include <TextControl.h> 7 #include <View.h> 8 #include <Directory.h> 9 #include <Alert.h> 10 11 #include <algorithm> 12 #include <string.h> 13 14 #include "LpsClient.h" 15 #include "LprSetupDlg.h" 16 #include "LprDefs.h" 17 #include "DbgMsg.h" 18 19 20 #define DLG_WIDTH 370 21 #define DLG_HEIGHT 100 22 23 #define BUTTON_WIDTH 70 24 #define BUTTON_HEIGHT 20 25 26 #define SERVER_H 10 27 #define SERVER_V 10 28 #define SERVER_WIDTH (DLG_WIDTH - SERVER_H - SERVER_H) 29 #define SERVER_HEIGHT 20 30 #define SERVER_TEXT "Server name" 31 32 #define QUEUE_H 10 33 #define QUEUE_V SERVER_V + SERVER_HEIGHT + 2 34 #define QUEUE_WIDTH (DLG_WIDTH - QUEUE_H - QUEUE_H) 35 #define QUEUE_HEIGHT 20 36 #define QUEUE_TEXT "Queue name" 37 38 #define OK_H (DLG_WIDTH - BUTTON_WIDTH - 11) 39 #define OK_V (DLG_HEIGHT - BUTTON_HEIGHT - 11) 40 #define OK_TEXT "OK" 41 42 #define CANCEL_H (OK_H - BUTTON_WIDTH - 12) 43 #define CANCEL_V OK_V 44 #define CANCEL_TEXT "Cancel" 45 46 const BRect SERVER_RECT( 47 SERVER_H, 48 SERVER_V, 49 SERVER_H + SERVER_WIDTH, 50 SERVER_V + SERVER_HEIGHT); 51 52 const BRect QUEUE_RECT( 53 QUEUE_H, 54 QUEUE_V, 55 QUEUE_H + QUEUE_WIDTH, 56 QUEUE_V + QUEUE_HEIGHT); 57 58 const BRect OK_RECT( 59 OK_H, 60 OK_V, 61 OK_H + BUTTON_WIDTH, 62 OK_V + BUTTON_HEIGHT); 63 64 const BRect CANCEL_RECT( 65 CANCEL_H, 66 CANCEL_V, 67 CANCEL_H + BUTTON_WIDTH, 68 CANCEL_V + BUTTON_HEIGHT); 69 70 enum MSGS { 71 M_CANCEL = 1, 72 M_OK 73 }; 74 75 76 class LprSetupView : public BView { 77 public: 78 LprSetupView(BRect, BDirectory *); 79 ~LprSetupView() {} 80 virtual void AttachedToWindow(); 81 bool UpdateViewData(); 82 83 private: 84 BTextControl* fServer; 85 BTextControl* fQueue; 86 BDirectory* fDir; 87 }; 88 89 90 LprSetupView::LprSetupView(BRect frame, BDirectory *d) 91 : 92 BView(frame, "", B_FOLLOW_ALL, B_WILL_DRAW), fDir(d) 93 { 94 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 95 } 96 97 98 void 99 LprSetupView::AttachedToWindow() 100 { 101 float width = max(StringWidth(SERVER_TEXT), StringWidth(QUEUE_TEXT)) + 10; 102 103 /* server name box */ 104 105 // TODO remember previous value 106 fServer = new BTextControl(SERVER_RECT, "", SERVER_TEXT, "192.168.0.0", 107 NULL); 108 AddChild(fServer); 109 fServer->SetDivider(width); 110 111 /* queue name box */ 112 113 // TODO remember previous value 114 fQueue = new BTextControl(QUEUE_RECT, "", QUEUE_TEXT, "LPT1_PASSTHRU", 115 NULL); 116 AddChild(fQueue); 117 fQueue->SetDivider(width); 118 119 /* cancel */ 120 121 BButton *button = new BButton(CANCEL_RECT, "", CANCEL_TEXT, 122 new BMessage(M_CANCEL)); 123 AddChild(button); 124 125 /* ok */ 126 127 button = new BButton(OK_RECT, "", OK_TEXT, new BMessage(M_OK)); 128 AddChild(button); 129 button->MakeDefault(true); 130 } 131 132 133 bool 134 LprSetupView::UpdateViewData() 135 { 136 if (*fServer->Text() && *fQueue->Text()) { 137 138 try { 139 LpsClient lpr(fServer->Text()); 140 lpr.connect(); 141 } 142 143 catch (LPSException &err) { 144 BAlert *alert = new BAlert("", err.what(), "OK"); 145 alert->Go(); 146 return false; 147 } 148 149 fDir->WriteAttr(LPR_SERVER_NAME, B_STRING_TYPE, 0, fServer->Text(), 150 strlen(fServer->Text()) + 1); 151 fDir->WriteAttr(LPR_QUEUE_NAME, B_STRING_TYPE, 0, fQueue->Text(), 152 strlen(fQueue->Text()) + 1); 153 return true; 154 } 155 156 BAlert *alert = new BAlert("", "Please enter server address and printer" 157 "queue name.", "OK"); 158 alert->Go(); 159 return false; 160 } 161 162 163 LprSetupDlg::LprSetupDlg(BDirectory *dir) 164 : 165 DialogWindow(BRect(100, 100, 100 + DLG_WIDTH, 100 + DLG_HEIGHT), 166 "LPR Setup", B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL, 167 B_NOT_RESIZABLE | B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE) 168 { 169 fSetupView = new LprSetupView(Bounds(), dir); 170 AddChild(fSetupView); 171 } 172 173 174 void 175 LprSetupDlg::MessageReceived(BMessage *msg) 176 { 177 switch (msg->what) { 178 case M_OK: 179 if (fSetupView->UpdateViewData()) { 180 SetResult(B_OK); 181 PostMessage(B_QUIT_REQUESTED); 182 } 183 break; 184 185 case M_CANCEL: 186 SetResult(B_ERROR); 187 PostMessage(B_QUIT_REQUESTED); 188 break; 189 190 default: 191 DialogWindow::MessageReceived(msg); 192 } 193 } 194