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